THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
12KB

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