quote.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "cache.h"
  2. #include "quote.h"
  3. int quote_path_fully = 1;
  4. /* Help to copy the thing properly quoted for the shell safety.
  5. * any single quote is replaced with '\'', any exclamation point
  6. * is replaced with '\!', and the whole thing is enclosed in a
  7. *
  8. * E.g.
  9. * original sq_quote result
  10. * name ==> name ==> 'name'
  11. * a b ==> a b ==> 'a b'
  12. * a'b ==> a'\''b ==> 'a'\''b'
  13. * a!b ==> a'\!'b ==> 'a'\!'b'
  14. */
  15. static inline int need_bs_quote(char c)
  16. {
  17. return (c == '\'' || c == '!');
  18. }
  19. void sq_quote_buf(struct strbuf *dst, const char *src)
  20. {
  21. char *to_free = NULL;
  22. if (dst->buf == src)
  23. to_free = strbuf_detach(dst, NULL);
  24. strbuf_addch(dst, '\'');
  25. while (*src) {
  26. size_t len = strcspn(src, "'!");
  27. strbuf_add(dst, src, len);
  28. src += len;
  29. while (need_bs_quote(*src)) {
  30. strbuf_addstr(dst, "'\\");
  31. strbuf_addch(dst, *src++);
  32. strbuf_addch(dst, '\'');
  33. }
  34. }
  35. strbuf_addch(dst, '\'');
  36. free(to_free);
  37. }
  38. void sq_quote_print(FILE *stream, const char *src)
  39. {
  40. char c;
  41. fputc('\'', stream);
  42. while ((c = *src++)) {
  43. if (need_bs_quote(c)) {
  44. fputs("'\\", stream);
  45. fputc(c, stream);
  46. fputc('\'', stream);
  47. } else {
  48. fputc(c, stream);
  49. }
  50. }
  51. fputc('\'', stream);
  52. }
  53. void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
  54. {
  55. int i;
  56. /* Copy into destination buffer. */
  57. strbuf_grow(dst, 255);
  58. for (i = 0; argv[i]; ++i) {
  59. strbuf_addch(dst, ' ');
  60. sq_quote_buf(dst, argv[i]);
  61. if (maxlen && dst->len > maxlen)
  62. die("Too many or long arguments");
  63. }
  64. }
  65. char *sq_dequote_step(char *arg, char **next)
  66. {
  67. char *dst = arg;
  68. char *src = arg;
  69. char c;
  70. if (*src != '\'')
  71. return NULL;
  72. for (;;) {
  73. c = *++src;
  74. if (!c)
  75. return NULL;
  76. if (c != '\'') {
  77. *dst++ = c;
  78. continue;
  79. }
  80. /* We stepped out of sq */
  81. switch (*++src) {
  82. case '\0':
  83. *dst = 0;
  84. if (next)
  85. *next = NULL;
  86. return arg;
  87. case '\\':
  88. c = *++src;
  89. if (need_bs_quote(c) && *++src == '\'') {
  90. *dst++ = c;
  91. continue;
  92. }
  93. /* Fallthrough */
  94. default:
  95. if (!next || !isspace(*src))
  96. return NULL;
  97. do {
  98. c = *++src;
  99. } while (isspace(c));
  100. *dst = 0;
  101. *next = src;
  102. return arg;
  103. }
  104. }
  105. }
  106. char *sq_dequote(char *arg)
  107. {
  108. return sq_dequote_step(arg, NULL);
  109. }
  110. int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
  111. {
  112. char *next = arg;
  113. if (!*arg)
  114. return 0;
  115. do {
  116. char *dequoted = sq_dequote_step(next, &next);
  117. if (!dequoted)
  118. return -1;
  119. ALLOC_GROW(*argv, *nr + 1, *alloc);
  120. (*argv)[(*nr)++] = dequoted;
  121. } while (next);
  122. return 0;
  123. }
  124. /* 1 means: quote as octal
  125. * 0 means: quote as octal if (quote_path_fully)
  126. * -1 means: never quote
  127. * c: quote as "\\c"
  128. */
  129. #define X8(x) x, x, x, x, x, x, x, x
  130. #define X16(x) X8(x), X8(x)
  131. static signed char const sq_lookup[256] = {
  132. /* 0 1 2 3 4 5 6 7 */
  133. /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
  134. /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
  135. /* 0x10 */ X16(1),
  136. /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
  137. /* 0x28 */ X16(-1), X16(-1), X16(-1),
  138. /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
  139. /* 0x60 */ X16(-1), X8(-1),
  140. /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
  141. /* 0x80 */ /* set to 0 */
  142. };
  143. static inline int sq_must_quote(char c)
  144. {
  145. return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
  146. }
  147. /* returns the longest prefix not needing a quote up to maxlen if positive.
  148. This stops at the first \0 because it's marked as a character needing an
  149. escape */
  150. static size_t next_quote_pos(const char *s, ssize_t maxlen)
  151. {
  152. size_t len;
  153. if (maxlen < 0) {
  154. for (len = 0; !sq_must_quote(s[len]); len++);
  155. } else {
  156. for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
  157. }
  158. return len;
  159. }
  160. /*
  161. * C-style name quoting.
  162. *
  163. * (1) if sb and fp are both NULL, inspect the input name and counts the
  164. * number of bytes that are needed to hold c_style quoted version of name,
  165. * counting the double quotes around it but not terminating NUL, and
  166. * returns it.
  167. * However, if name does not need c_style quoting, it returns 0.
  168. *
  169. * (2) if sb or fp are not NULL, it emits the c_style quoted version
  170. * of name, enclosed with double quotes if asked and needed only.
  171. * Return value is the same as in (1).
  172. */
  173. static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
  174. struct strbuf *sb, FILE *fp, int no_dq)
  175. {
  176. #undef EMIT
  177. #define EMIT(c) \
  178. do { \
  179. if (sb) strbuf_addch(sb, (c)); \
  180. if (fp) fputc((c), fp); \
  181. count++; \
  182. } while (0)
  183. #define EMITBUF(s, l) \
  184. do { \
  185. int __ret; \
  186. if (sb) strbuf_add(sb, (s), (l)); \
  187. if (fp) __ret = fwrite((s), (l), 1, fp); \
  188. count += (l); \
  189. } while (0)
  190. size_t len, count = 0;
  191. const char *p = name;
  192. for (;;) {
  193. int ch;
  194. len = next_quote_pos(p, maxlen);
  195. if (len == maxlen || !p[len])
  196. break;
  197. if (!no_dq && p == name)
  198. EMIT('"');
  199. EMITBUF(p, len);
  200. EMIT('\\');
  201. p += len;
  202. ch = (unsigned char)*p++;
  203. if (sq_lookup[ch] >= ' ') {
  204. EMIT(sq_lookup[ch]);
  205. } else {
  206. EMIT(((ch >> 6) & 03) + '0');
  207. EMIT(((ch >> 3) & 07) + '0');
  208. EMIT(((ch >> 0) & 07) + '0');
  209. }
  210. }
  211. EMITBUF(p, len);
  212. if (p == name) /* no ending quote needed */
  213. return 0;
  214. if (!no_dq)
  215. EMIT('"');
  216. return count;
  217. }
  218. size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
  219. {
  220. return quote_c_style_counted(name, -1, sb, fp, nodq);
  221. }
  222. void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
  223. {
  224. if (quote_c_style(prefix, NULL, NULL, 0) ||
  225. quote_c_style(path, NULL, NULL, 0)) {
  226. if (!nodq)
  227. strbuf_addch(sb, '"');
  228. quote_c_style(prefix, sb, NULL, 1);
  229. quote_c_style(path, sb, NULL, 1);
  230. if (!nodq)
  231. strbuf_addch(sb, '"');
  232. } else {
  233. strbuf_addstr(sb, prefix);
  234. strbuf_addstr(sb, path);
  235. }
  236. }
  237. void write_name_quoted(const char *name, FILE *fp, int terminator)
  238. {
  239. if (terminator) {
  240. quote_c_style(name, NULL, fp, 0);
  241. } else {
  242. fputs(name, fp);
  243. }
  244. fputc(terminator, fp);
  245. }
  246. extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
  247. const char *name, FILE *fp, int terminator)
  248. {
  249. int needquote = 0;
  250. if (terminator) {
  251. needquote = next_quote_pos(pfx, pfxlen) < pfxlen
  252. || name[next_quote_pos(name, -1)];
  253. }
  254. if (needquote) {
  255. fputc('"', fp);
  256. quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
  257. quote_c_style(name, NULL, fp, 1);
  258. fputc('"', fp);
  259. } else {
  260. int ret;
  261. ret = fwrite(pfx, pfxlen, 1, fp);
  262. fputs(name, fp);
  263. }
  264. fputc(terminator, fp);
  265. }
  266. /* quote path as relative to the given prefix */
  267. char *quote_path_relative(const char *in, int len,
  268. struct strbuf *out, const char *prefix)
  269. {
  270. int needquote;
  271. if (len < 0)
  272. len = strlen(in);
  273. /* "../" prefix itself does not need quoting, but "in" might. */
  274. needquote = next_quote_pos(in, len) < len;
  275. strbuf_setlen(out, 0);
  276. strbuf_grow(out, len);
  277. if (needquote)
  278. strbuf_addch(out, '"');
  279. if (prefix) {
  280. int off = 0;
  281. while (prefix[off] && off < len && prefix[off] == in[off])
  282. if (prefix[off] == '/') {
  283. prefix += off + 1;
  284. in += off + 1;
  285. len -= off + 1;
  286. off = 0;
  287. } else
  288. off++;
  289. for (; *prefix; prefix++)
  290. if (*prefix == '/')
  291. strbuf_addstr(out, "../");
  292. }
  293. quote_c_style_counted (in, len, out, NULL, 1);
  294. if (needquote)
  295. strbuf_addch(out, '"');
  296. if (!out->len)
  297. strbuf_addstr(out, "./");
  298. return out->buf;
  299. }
  300. /*
  301. * C-style name unquoting.
  302. *
  303. * Quoted should point at the opening double quote.
  304. * + Returns 0 if it was able to unquote the string properly, and appends the
  305. * result in the strbuf `sb'.
  306. * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
  307. * that this function will allocate memory in the strbuf, so calling
  308. * strbuf_release is mandatory whichever result unquote_c_style returns.
  309. *
  310. * Updates endp pointer to point at one past the ending double quote if given.
  311. */
  312. int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
  313. {
  314. size_t oldlen = sb->len, len;
  315. int ch, ac;
  316. if (*quoted++ != '"')
  317. return -1;
  318. for (;;) {
  319. len = strcspn(quoted, "\"\\");
  320. strbuf_add(sb, quoted, len);
  321. quoted += len;
  322. switch (*quoted++) {
  323. case '"':
  324. if (endp)
  325. *endp = quoted;
  326. return 0;
  327. case '\\':
  328. break;
  329. default:
  330. goto error;
  331. }
  332. switch ((ch = *quoted++)) {
  333. case 'a': ch = '\a'; break;
  334. case 'b': ch = '\b'; break;
  335. case 'f': ch = '\f'; break;
  336. case 'n': ch = '\n'; break;
  337. case 'r': ch = '\r'; break;
  338. case 't': ch = '\t'; break;
  339. case 'v': ch = '\v'; break;
  340. case '\\': case '"':
  341. break; /* verbatim */
  342. /* octal values with first digit over 4 overflow */
  343. case '0': case '1': case '2': case '3':
  344. ac = ((ch - '0') << 6);
  345. if ((ch = *quoted++) < '0' || '7' < ch)
  346. goto error;
  347. ac |= ((ch - '0') << 3);
  348. if ((ch = *quoted++) < '0' || '7' < ch)
  349. goto error;
  350. ac |= (ch - '0');
  351. ch = ac;
  352. break;
  353. default:
  354. goto error;
  355. }
  356. strbuf_addch(sb, ch);
  357. }
  358. error:
  359. strbuf_setlen(sb, oldlen);
  360. return -1;
  361. }
  362. /* quoting as a string literal for other languages */
  363. void perl_quote_print(FILE *stream, const char *src)
  364. {
  365. const char sq = '\'';
  366. const char bq = '\\';
  367. char c;
  368. fputc(sq, stream);
  369. while ((c = *src++)) {
  370. if (c == sq || c == bq)
  371. fputc(bq, stream);
  372. fputc(c, stream);
  373. }
  374. fputc(sq, stream);
  375. }
  376. void python_quote_print(FILE *stream, const char *src)
  377. {
  378. const char sq = '\'';
  379. const char bq = '\\';
  380. const char nl = '\n';
  381. char c;
  382. fputc(sq, stream);
  383. while ((c = *src++)) {
  384. if (c == nl) {
  385. fputc(bq, stream);
  386. fputc('n', stream);
  387. continue;
  388. }
  389. if (c == sq || c == bq)
  390. fputc(bq, stream);
  391. fputc(c, stream);
  392. }
  393. fputc(sq, stream);
  394. }
  395. void tcl_quote_print(FILE *stream, const char *src)
  396. {
  397. char c;
  398. fputc('"', stream);
  399. while ((c = *src++)) {
  400. switch (c) {
  401. case '[': case ']':
  402. case '{': case '}':
  403. case '$': case '\\': case '"':
  404. fputc('\\', stream);
  405. default:
  406. fputc(c, stream);
  407. break;
  408. case '\f':
  409. fputs("\\f", stream);
  410. break;
  411. case '\r':
  412. fputs("\\r", stream);
  413. break;
  414. case '\n':
  415. fputs("\\n", stream);
  416. break;
  417. case '\t':
  418. fputs("\\t", stream);
  419. break;
  420. case '\v':
  421. fputs("\\v", stream);
  422. break;
  423. }
  424. }
  425. fputc('"', stream);
  426. }