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