quote.c 10.3 KB
Newer Older
1 2
#include "cache.h"
#include "quote.h"
3
#include "argv-array.h"
4

5 6
int quote_path_fully = 1;

7
/* Help to copy the thing properly quoted for the shell safety.
H. Peter Anvin's avatar
H. Peter Anvin committed
8 9
 * any single quote is replaced with '\'', any exclamation point
 * is replaced with '\!', and the whole thing is enclosed in a
10 11 12 13 14 15
 *
 * E.g.
 *  original     sq_quote     result
 *  name     ==> name      ==> 'name'
 *  a b      ==> a b       ==> 'a b'
 *  a'b      ==> a'\''b    ==> 'a'\''b'
H. Peter Anvin's avatar
H. Peter Anvin committed
16
 *  a!b      ==> a'\!'b    ==> 'a'\!'b'
17
 */
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22
static inline int need_bs_quote(char c)
{
	return (c == '\'' || c == '!');
}

23
void sq_quote_buf(struct strbuf *dst, const char *src)
H. Peter Anvin's avatar
H. Peter Anvin committed
24
{
25 26 27
	char *to_free = NULL;

	if (dst->buf == src)
28
		to_free = strbuf_detach(dst, NULL);
29 30 31

	strbuf_addch(dst, '\'');
	while (*src) {
32
		size_t len = strcspn(src, "'!");
33 34 35 36 37 38
		strbuf_add(dst, src, len);
		src += len;
		while (need_bs_quote(*src)) {
			strbuf_addstr(dst, "'\\");
			strbuf_addch(dst, *src++);
			strbuf_addch(dst, '\'');
39 40
		}
	}
41 42
	strbuf_addch(dst, '\'');
	free(to_free);
H. Peter Anvin's avatar
H. Peter Anvin committed
43 44
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
void sq_quote_print(FILE *stream, const char *src)
{
	char c;

	fputc('\'', stream);
	while ((c = *src++)) {
		if (need_bs_quote(c)) {
			fputs("'\\", stream);
			fputc(c, stream);
			fputc('\'', stream);
		} else {
			fputc(c, stream);
		}
	}
	fputc('\'', stream);
}

62
void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
63 64 65 66
{
	int i;

	/* Copy into destination buffer. */
67 68
	strbuf_grow(dst, 255);
	for (i = 0; argv[i]; ++i) {
69 70 71 72
		strbuf_addch(dst, ' ');
		sq_quote_buf(dst, argv[i]);
		if (maxlen && dst->len > maxlen)
			die("Too many or long arguments");
73
	}
74 75
}

76
static char *sq_dequote_step(char *arg, char **next)
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
	char *dst = arg;
	char *src = arg;
	char c;

	if (*src != '\'')
		return NULL;
	for (;;) {
		c = *++src;
		if (!c)
			return NULL;
		if (c != '\'') {
			*dst++ = c;
			continue;
		}
		/* We stepped out of sq */
		switch (*++src) {
		case '\0':
			*dst = 0;
96 97
			if (next)
				*next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101 102 103 104 105 106
			return arg;
		case '\\':
			c = *++src;
			if (need_bs_quote(c) && *++src == '\'') {
				*dst++ = c;
				continue;
			}
		/* Fallthrough */
		default:
107 108 109 110 111 112 113 114
			if (!next || !isspace(*src))
				return NULL;
			do {
				c = *++src;
			} while (isspace(c));
			*dst = 0;
			*next = src;
			return arg;
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118
		}
	}
}

119 120 121 122 123
char *sq_dequote(char *arg)
{
	return sq_dequote_step(arg, NULL);
}

124 125 126
static int sq_dequote_to_argv_internal(char *arg,
				       const char ***argv, int *nr, int *alloc,
				       struct argv_array *array)
127 128 129 130 131 132 133 134 135
{
	char *next = arg;

	if (!*arg)
		return 0;
	do {
		char *dequoted = sq_dequote_step(next, &next);
		if (!dequoted)
			return -1;
136 137 138 139 140 141
		if (argv) {
			ALLOC_GROW(*argv, *nr + 1, *alloc);
			(*argv)[(*nr)++] = dequoted;
		}
		if (array)
			argv_array_push(array, dequoted);
142 143 144 145 146
	} while (next);

	return 0;
}

147 148 149 150 151 152 153 154 155 156
int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
{
	return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
}

int sq_dequote_to_argv_array(char *arg, struct argv_array *array)
{
	return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* 1 means: quote as octal
 * 0 means: quote as octal if (quote_path_fully)
 * -1 means: never quote
 * c: quote as "\\c"
 */
#define X8(x)   x, x, x, x, x, x, x, x
#define X16(x)  X8(x), X8(x)
static signed char const sq_lookup[256] = {
	/*           0    1    2    3    4    5    6    7 */
	/* 0x00 */   1,   1,   1,   1,   1,   1,   1, 'a',
	/* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r',   1,   1,
	/* 0x10 */ X16(1),
	/* 0x20 */  -1,  -1, '"',  -1,  -1,  -1,  -1,  -1,
	/* 0x28 */ X16(-1), X16(-1), X16(-1),
	/* 0x58 */  -1,  -1,  -1,  -1,'\\',  -1,  -1,  -1,
	/* 0x60 */ X16(-1), X8(-1),
	/* 0x78 */  -1,  -1,  -1,  -1,  -1,  -1,  -1,   1,
	/* 0x80 */ /* set to 0 */
};

177 178
static inline int sq_must_quote(char c)
{
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
}

/* returns the longest prefix not needing a quote up to maxlen if positive.
   This stops at the first \0 because it's marked as a character needing an
   escape */
static size_t next_quote_pos(const char *s, ssize_t maxlen)
{
	size_t len;
	if (maxlen < 0) {
		for (len = 0; !sq_must_quote(s[len]); len++);
	} else {
		for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
	}
	return len;
}

196 197 198
/*
 * C-style name quoting.
 *
199 200 201 202 203
 * (1) if sb and fp are both NULL, inspect the input name and counts the
 *     number of bytes that are needed to hold c_style quoted version of name,
 *     counting the double quotes around it but not terminating NUL, and
 *     returns it.
 *     However, if name does not need c_style quoting, it returns 0.
204
 *
205 206 207
 * (2) if sb or fp are not NULL, it emits the c_style quoted version
 *     of name, enclosed with double quotes if asked and needed only.
 *     Return value is the same as in (1).
208
 */
209 210
static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
                                    struct strbuf *sb, FILE *fp, int no_dq)
211 212
{
#undef EMIT
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
#define EMIT(c)                                 \
	do {                                        \
		if (sb) strbuf_addch(sb, (c));          \
		if (fp) fputc((c), fp);                 \
		count++;                                \
	} while (0)
#define EMITBUF(s, l)                           \
	do {                                        \
		if (sb) strbuf_add(sb, (s), (l));       \
		if (fp) fwrite((s), (l), 1, fp);        \
		count += (l);                           \
	} while (0)

	size_t len, count = 0;
	const char *p = name;
228

229 230
	for (;;) {
		int ch;
231

232
		len = next_quote_pos(p, maxlen);
233
		if (len == maxlen || (maxlen < 0 && !p[len]))
234
			break;
235 236 237 238 239 240 241 242

		if (!no_dq && p == name)
			EMIT('"');

		EMITBUF(p, len);
		EMIT('\\');
		p += len;
		ch = (unsigned char)*p++;
243 244
		if (maxlen >= 0)
			maxlen -= len + 1;
245 246 247 248 249 250
		if (sq_lookup[ch] >= ' ') {
			EMIT(sq_lookup[ch]);
		} else {
			EMIT(((ch >> 6) & 03) + '0');
			EMIT(((ch >> 3) & 07) + '0');
			EMIT(((ch >> 0) & 07) + '0');
251 252
		}
	}
253 254 255 256 257

	EMITBUF(p, len);
	if (p == name)   /* no ending quote needed */
		return 0;

258 259
	if (!no_dq)
		EMIT('"');
260 261
	return count;
}
262

263 264 265
size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
{
	return quote_c_style_counted(name, -1, sb, fp, nodq);
266 267
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
{
	if (quote_c_style(prefix, NULL, NULL, 0) ||
	    quote_c_style(path, NULL, NULL, 0)) {
		if (!nodq)
			strbuf_addch(sb, '"');
		quote_c_style(prefix, sb, NULL, 1);
		quote_c_style(path, sb, NULL, 1);
		if (!nodq)
			strbuf_addch(sb, '"');
	} else {
		strbuf_addstr(sb, prefix);
		strbuf_addstr(sb, path);
	}
}

284
void write_name_quoted(const char *name, FILE *fp, int terminator)
285
{
286 287 288 289 290 291 292 293
	if (terminator) {
		quote_c_style(name, NULL, fp, 0);
	} else {
		fputs(name, fp);
	}
	fputc(terminator, fp);
}

294 295
void write_name_quotedpfx(const char *pfx, size_t pfxlen,
			  const char *name, FILE *fp, int terminator)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
	int needquote = 0;

	if (terminator) {
		needquote = next_quote_pos(pfx, pfxlen) < pfxlen
			|| name[next_quote_pos(name, -1)];
	}
	if (needquote) {
		fputc('"', fp);
		quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
		quote_c_style(name, NULL, fp, 1);
		fputc('"', fp);
	} else {
		fwrite(pfx, pfxlen, 1, fp);
		fputs(name, fp);
	}
	fputc(terminator, fp);
313 314
}

315
void write_name_quoted_relative(const char *name, const char *prefix,
316
				FILE *fp, int terminator)
317
{
318 319
	struct strbuf sb = STRBUF_INIT;

320
	name = relative_path(name, prefix, &sb);
321 322 323 324 325 326
	write_name_quoted(name, fp, terminator);

	strbuf_release(&sb);
}

/* quote path as relative to the given prefix */
327 328
char *quote_path_relative(const char *in, const char *prefix,
			  struct strbuf *out)
329 330
{
	struct strbuf sb = STRBUF_INIT;
331
	const char *rel = relative_path(in, prefix, &sb);
332 333 334
	strbuf_reset(out);
	quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
	strbuf_release(&sb);
335 336 337 338

	return out->buf;
}

339 340 341
/*
 * C-style name unquoting.
 *
342 343 344 345 346 347 348 349
 * Quoted should point at the opening double quote.
 * + Returns 0 if it was able to unquote the string properly, and appends the
 *   result in the strbuf `sb'.
 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
 *   that this function will allocate memory in the strbuf, so calling
 *   strbuf_release is mandatory whichever result unquote_c_style returns.
 *
 * Updates endp pointer to point at one past the ending double quote if given.
350
 */
351
int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
352
{
353 354
	size_t oldlen = sb->len, len;
	int ch, ac;
355 356

	if (*quoted++ != '"')
357 358 359 360 361 362
		return -1;

	for (;;) {
		len = strcspn(quoted, "\"\\");
		strbuf_add(sb, quoted, len);
		quoted += len;
363

364 365 366
		switch (*quoted++) {
		  case '"':
			if (endp)
367
				*endp = quoted;
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
			return 0;
		  case '\\':
			break;
		  default:
			goto error;
		}

		switch ((ch = *quoted++)) {
		case 'a': ch = '\a'; break;
		case 'b': ch = '\b'; break;
		case 'f': ch = '\f'; break;
		case 'n': ch = '\n'; break;
		case 'r': ch = '\r'; break;
		case 't': ch = '\t'; break;
		case 'v': ch = '\v'; break;

		case '\\': case '"':
			break; /* verbatim */

		/* octal values with first digit over 4 overflow */
		case '0': case '1': case '2': case '3':
389
					ac = ((ch - '0') << 6);
390 391
			if ((ch = *quoted++) < '0' || '7' < ch)
				goto error;
392
					ac |= ((ch - '0') << 3);
393 394
			if ((ch = *quoted++) < '0' || '7' < ch)
				goto error;
395 396 397 398
					ac |= (ch - '0');
					ch = ac;
					break;
				default:
399
			goto error;
400
			}
401
		strbuf_addch(sb, ch);
402 403
		}

404 405 406
  error:
	strbuf_setlen(sb, oldlen);
	return -1;
407 408
}

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
/* quoting as a string literal for other languages */

void perl_quote_print(FILE *stream, const char *src)
{
	const char sq = '\'';
	const char bq = '\\';
	char c;

	fputc(sq, stream);
	while ((c = *src++)) {
		if (c == sq || c == bq)
			fputc(bq, stream);
		fputc(c, stream);
	}
	fputc(sq, stream);
}

void python_quote_print(FILE *stream, const char *src)
{
	const char sq = '\'';
	const char bq = '\\';
	const char nl = '\n';
	char c;

	fputc(sq, stream);
	while ((c = *src++)) {
		if (c == nl) {
			fputc(bq, stream);
			fputc('n', stream);
			continue;
		}
		if (c == sq || c == bq)
			fputc(bq, stream);
		fputc(c, stream);
	}
	fputc(sq, stream);
}
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

void tcl_quote_print(FILE *stream, const char *src)
{
	char c;

	fputc('"', stream);
	while ((c = *src++)) {
		switch (c) {
		case '[': case ']':
		case '{': case '}':
		case '$': case '\\': case '"':
			fputc('\\', stream);
		default:
			fputc(c, stream);
			break;
		case '\f':
			fputs("\\f", stream);
			break;
		case '\r':
			fputs("\\r", stream);
			break;
		case '\n':
			fputs("\\n", stream);
			break;
		case '\t':
			fputs("\\t", stream);
			break;
		case '\v':
			fputs("\\v", stream);
			break;
		}
	}
	fputc('"', stream);
}