quote.c 10.0 KB

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