quote.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. if (sb) strbuf_add(sb, (s), (l)); \
  186. if (fp) fwrite((s), (l), 1, fp); \
  187. count += (l); \
  188. } while (0)
  189. size_t len, count = 0;
  190. const char *p = name;
  191. for (;;) {
  192. int ch;
  193. len = next_quote_pos(p, maxlen);
  194. if (len == maxlen || !p[len])
  195. break;
  196. if (!no_dq && p == name)
  197. EMIT('"');
  198. EMITBUF(p, len);
  199. EMIT('\\');
  200. p += len;
  201. ch = (unsigned char)*p++;
  202. if (sq_lookup[ch] >= ' ') {
  203. EMIT(sq_lookup[ch]);
  204. } else {
  205. EMIT(((ch >> 6) & 03) + '0');
  206. EMIT(((ch >> 3) & 07) + '0');
  207. EMIT(((ch >> 0) & 07) + '0');
  208. }
  209. }
  210. EMITBUF(p, len);
  211. if (p == name) /* no ending quote needed */
  212. return 0;
  213. if (!no_dq)
  214. EMIT('"');
  215. return count;
  216. }
  217. size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
  218. {
  219. return quote_c_style_counted(name, -1, sb, fp, nodq);
  220. }
  221. void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
  222. {
  223. if (quote_c_style(prefix, NULL, NULL, 0) ||
  224. quote_c_style(path, NULL, NULL, 0)) {
  225. if (!nodq)
  226. strbuf_addch(sb, '"');
  227. quote_c_style(prefix, sb, NULL, 1);
  228. quote_c_style(path, sb, NULL, 1);
  229. if (!nodq)
  230. strbuf_addch(sb, '"');
  231. } else {
  232. strbuf_addstr(sb, prefix);
  233. strbuf_addstr(sb, path);
  234. }
  235. }
  236. void write_name_quoted(const char *name, FILE *fp, int terminator)
  237. {
  238. if (terminator) {
  239. quote_c_style(name, NULL, fp, 0);
  240. } else {
  241. fputs(name, fp);
  242. }
  243. fputc(terminator, fp);
  244. }
  245. extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
  246. const char *name, FILE *fp, int terminator)
  247. {
  248. int needquote = 0;
  249. if (terminator) {
  250. needquote = next_quote_pos(pfx, pfxlen) < pfxlen
  251. || name[next_quote_pos(name, -1)];
  252. }
  253. if (needquote) {
  254. fputc('"', fp);
  255. quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
  256. quote_c_style(name, NULL, fp, 1);
  257. fputc('"', fp);
  258. } else {
  259. fwrite(pfx, pfxlen, 1, fp);
  260. fputs(name, fp);
  261. }
  262. fputc(terminator, fp);
  263. }
  264. /* quote path as relative to the given prefix */
  265. char *quote_path_relative(const char *in, int len,
  266. struct strbuf *out, const char *prefix)
  267. {
  268. int needquote;
  269. if (len < 0)
  270. len = strlen(in);
  271. /* "../" prefix itself does not need quoting, but "in" might. */
  272. needquote = next_quote_pos(in, len) < len;
  273. strbuf_setlen(out, 0);
  274. strbuf_grow(out, len);
  275. if (needquote)
  276. strbuf_addch(out, '"');
  277. if (prefix) {
  278. int off = 0;
  279. while (prefix[off] && off < len && prefix[off] == in[off])
  280. if (prefix[off] == '/') {
  281. prefix += off + 1;
  282. in += off + 1;
  283. len -= off + 1;
  284. off = 0;
  285. } else
  286. off++;
  287. for (; *prefix; prefix++)
  288. if (*prefix == '/')
  289. strbuf_addstr(out, "../");
  290. }
  291. quote_c_style_counted (in, len, out, NULL, 1);
  292. if (needquote)
  293. strbuf_addch(out, '"');
  294. if (!out->len)
  295. strbuf_addstr(out, "./");
  296. return out->buf;
  297. }
  298. /*
  299. * C-style name unquoting.
  300. *
  301. * Quoted should point at the opening double quote.
  302. * + Returns 0 if it was able to unquote the string properly, and appends the
  303. * result in the strbuf `sb'.
  304. * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
  305. * that this function will allocate memory in the strbuf, so calling
  306. * strbuf_release is mandatory whichever result unquote_c_style returns.
  307. *
  308. * Updates endp pointer to point at one past the ending double quote if given.
  309. */
  310. int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
  311. {
  312. size_t oldlen = sb->len, len;
  313. int ch, ac;
  314. if (*quoted++ != '"')
  315. return -1;
  316. for (;;) {
  317. len = strcspn(quoted, "\"\\");
  318. strbuf_add(sb, quoted, len);
  319. quoted += len;
  320. switch (*quoted++) {
  321. case '"':
  322. if (endp)
  323. *endp = quoted;
  324. return 0;
  325. case '\\':
  326. break;
  327. default:
  328. goto error;
  329. }
  330. switch ((ch = *quoted++)) {
  331. case 'a': ch = '\a'; break;
  332. case 'b': ch = '\b'; break;
  333. case 'f': ch = '\f'; break;
  334. case 'n': ch = '\n'; break;
  335. case 'r': ch = '\r'; break;
  336. case 't': ch = '\t'; break;
  337. case 'v': ch = '\v'; break;
  338. case '\\': case '"':
  339. break; /* verbatim */
  340. /* octal values with first digit over 4 overflow */
  341. case '0': case '1': case '2': case '3':
  342. ac = ((ch - '0') << 6);
  343. if ((ch = *quoted++) < '0' || '7' < ch)
  344. goto error;
  345. ac |= ((ch - '0') << 3);
  346. if ((ch = *quoted++) < '0' || '7' < ch)
  347. goto error;
  348. ac |= (ch - '0');
  349. ch = ac;
  350. break;
  351. default:
  352. goto error;
  353. }
  354. strbuf_addch(sb, ch);
  355. }
  356. error:
  357. strbuf_setlen(sb, oldlen);
  358. return -1;
  359. }
  360. /* quoting as a string literal for other languages */
  361. void perl_quote_print(FILE *stream, const char *src)
  362. {
  363. const char sq = '\'';
  364. const char bq = '\\';
  365. char c;
  366. fputc(sq, stream);
  367. while ((c = *src++)) {
  368. if (c == sq || c == bq)
  369. fputc(bq, stream);
  370. fputc(c, stream);
  371. }
  372. fputc(sq, stream);
  373. }
  374. void python_quote_print(FILE *stream, const char *src)
  375. {
  376. const char sq = '\'';
  377. const char bq = '\\';
  378. const char nl = '\n';
  379. char c;
  380. fputc(sq, stream);
  381. while ((c = *src++)) {
  382. if (c == nl) {
  383. fputc(bq, stream);
  384. fputc('n', stream);
  385. continue;
  386. }
  387. if (c == sq || c == bq)
  388. fputc(bq, stream);
  389. fputc(c, stream);
  390. }
  391. fputc(sq, stream);
  392. }
  393. void tcl_quote_print(FILE *stream, const char *src)
  394. {
  395. char c;
  396. fputc('"', stream);
  397. while ((c = *src++)) {
  398. switch (c) {
  399. case '[': case ']':
  400. case '{': case '}':
  401. case '$': case '\\': case '"':
  402. fputc('\\', stream);
  403. default:
  404. fputc(c, stream);
  405. break;
  406. case '\f':
  407. fputs("\\f", stream);
  408. break;
  409. case '\r':
  410. fputs("\\r", stream);
  411. break;
  412. case '\n':
  413. fputs("\\n", stream);
  414. break;
  415. case '\t':
  416. fputs("\\t", stream);
  417. break;
  418. case '\v':
  419. fputs("\\v", stream);
  420. break;
  421. }
  422. }
  423. fputc('"', stream);
  424. }