quotearg.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* quotearg.c - quote arguments for output
  2. Copyright (C) 1998-2002, 2004-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert <eggert@twinsun.com> */
  14. /* Without this pragma, gcc 4.7.0 20111124 mistakenly suggests that
  15. the quoting_options_from_style function might be candidate for
  16. attribute 'pure' */
  17. #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
  18. # pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
  19. #endif
  20. #include <config.h>
  21. #include "quotearg.h"
  22. #include "quote.h"
  23. #include "xalloc.h"
  24. #include "c-strcaseeq.h"
  25. #include "localcharset.h"
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <stdbool.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <wchar.h>
  33. #include <wctype.h>
  34. #include "gettext.h"
  35. #define _(msgid) gettext (msgid)
  36. #define N_(msgid) msgid
  37. #ifndef SIZE_MAX
  38. # define SIZE_MAX ((size_t) -1)
  39. #endif
  40. #define INT_BITS (sizeof (int) * CHAR_BIT)
  41. struct quoting_options
  42. {
  43. /* Basic quoting style. */
  44. enum quoting_style style;
  45. /* Additional flags. Bitwise combination of enum quoting_flags. */
  46. int flags;
  47. /* Quote the characters indicated by this bit vector even if the
  48. quoting style would not normally require them to be quoted. */
  49. unsigned int quote_these_too[(UCHAR_MAX / INT_BITS) + 1];
  50. /* The left quote for custom_quoting_style. */
  51. char const *left_quote;
  52. /* The right quote for custom_quoting_style. */
  53. char const *right_quote;
  54. };
  55. /* Names of quoting styles. */
  56. char const *const quoting_style_args[] =
  57. {
  58. "literal",
  59. "shell",
  60. "shell-always",
  61. "c",
  62. "c-maybe",
  63. "escape",
  64. "locale",
  65. "clocale",
  66. 0
  67. };
  68. /* Correspondences to quoting style names. */
  69. enum quoting_style const quoting_style_vals[] =
  70. {
  71. literal_quoting_style,
  72. shell_quoting_style,
  73. shell_always_quoting_style,
  74. c_quoting_style,
  75. c_maybe_quoting_style,
  76. escape_quoting_style,
  77. locale_quoting_style,
  78. clocale_quoting_style
  79. };
  80. /* The default quoting options. */
  81. static struct quoting_options default_quoting_options;
  82. /* Allocate a new set of quoting options, with contents initially identical
  83. to O if O is not null, or to the default if O is null.
  84. It is the caller's responsibility to free the result. */
  85. struct quoting_options *
  86. clone_quoting_options (struct quoting_options *o)
  87. {
  88. int e = errno;
  89. struct quoting_options *p = xmemdup (o ? o : &default_quoting_options,
  90. sizeof *o);
  91. errno = e;
  92. return p;
  93. }
  94. /* Get the value of O's quoting style. If O is null, use the default. */
  95. enum quoting_style
  96. get_quoting_style (struct quoting_options *o)
  97. {
  98. return (o ? o : &default_quoting_options)->style;
  99. }
  100. /* In O (or in the default if O is null),
  101. set the value of the quoting style to S. */
  102. void
  103. set_quoting_style (struct quoting_options *o, enum quoting_style s)
  104. {
  105. (o ? o : &default_quoting_options)->style = s;
  106. }
  107. /* In O (or in the default if O is null),
  108. set the value of the quoting options for character C to I.
  109. Return the old value. Currently, the only values defined for I are
  110. 0 (the default) and 1 (which means to quote the character even if
  111. it would not otherwise be quoted). */
  112. int
  113. set_char_quoting (struct quoting_options *o, char c, int i)
  114. {
  115. unsigned char uc = c;
  116. unsigned int *p =
  117. (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
  118. int shift = uc % INT_BITS;
  119. int r = (*p >> shift) & 1;
  120. *p ^= ((i & 1) ^ r) << shift;
  121. return r;
  122. }
  123. /* In O (or in the default if O is null),
  124. set the value of the quoting options flag to I, which can be a
  125. bitwise combination of enum quoting_flags, or 0 for default
  126. behavior. Return the old value. */
  127. int
  128. set_quoting_flags (struct quoting_options *o, int i)
  129. {
  130. int r;
  131. if (!o)
  132. o = &default_quoting_options;
  133. r = o->flags;
  134. o->flags = i;
  135. return r;
  136. }
  137. void
  138. set_custom_quoting (struct quoting_options *o,
  139. char const *left_quote, char const *right_quote)
  140. {
  141. if (!o)
  142. o = &default_quoting_options;
  143. o->style = custom_quoting_style;
  144. if (!left_quote || !right_quote)
  145. abort ();
  146. o->left_quote = left_quote;
  147. o->right_quote = right_quote;
  148. }
  149. /* Return quoting options for STYLE, with no extra quoting. */
  150. static struct quoting_options /* NOT PURE!! */
  151. quoting_options_from_style (enum quoting_style style)
  152. {
  153. struct quoting_options o = { 0, 0, { 0 }, NULL, NULL };
  154. if (style == custom_quoting_style)
  155. abort ();
  156. o.style = style;
  157. return o;
  158. }
  159. /* MSGID approximates a quotation mark. Return its translation if it
  160. has one; otherwise, return either it or "\"", depending on S.
  161. S is either clocale_quoting_style or locale_quoting_style. */
  162. static char const *
  163. gettext_quote (char const *msgid, enum quoting_style s)
  164. {
  165. char const *translation = _(msgid);
  166. char const *locale_code;
  167. if (translation != msgid)
  168. return translation;
  169. /* For UTF-8 and GB-18030, use single quotes U+2018 and U+2019.
  170. Here is a list of other locales that include U+2018 and U+2019:
  171. ISO-8859-7 0xA1 KOI8-T 0x91
  172. CP869 0x8B CP874 0x91
  173. CP932 0x81 0x65 CP936 0xA1 0xAE
  174. CP949 0xA1 0xAE CP950 0xA1 0xA5
  175. CP1250 0x91 CP1251 0x91
  176. CP1252 0x91 CP1253 0x91
  177. CP1254 0x91 CP1255 0x91
  178. CP1256 0x91 CP1257 0x91
  179. EUC-JP 0xA1 0xC6 EUC-KR 0xA1 0xAE
  180. EUC-TW 0xA1 0xE4 BIG5 0xA1 0xA5
  181. BIG5-HKSCS 0xA1 0xA5 EUC-CN 0xA1 0xAE
  182. GBK 0xA1 0xAE Georgian-PS 0x91
  183. PT154 0x91
  184. None of these is still in wide use; using iconv is overkill. */
  185. locale_code = locale_charset ();
  186. if (STRCASEEQ (locale_code, "UTF-8", 'U','T','F','-','8',0,0,0,0))
  187. return msgid[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99";
  188. if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0))
  189. return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf";
  190. return (s == clocale_quoting_style ? "\"" : "'");
  191. }
  192. /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
  193. argument ARG (of size ARGSIZE), using QUOTING_STYLE, FLAGS, and
  194. QUOTE_THESE_TOO to control quoting.
  195. Terminate the output with a null character, and return the written
  196. size of the output, not counting the terminating null.
  197. If BUFFERSIZE is too small to store the output string, return the
  198. value that would have been returned had BUFFERSIZE been large enough.
  199. If ARGSIZE is SIZE_MAX, use the string length of the argument for ARGSIZE.
  200. This function acts like quotearg_buffer (BUFFER, BUFFERSIZE, ARG,
  201. ARGSIZE, O), except it breaks O into its component pieces and is
  202. not careful about errno. */
  203. static size_t
  204. quotearg_buffer_restyled (char *buffer, size_t buffersize,
  205. char const *arg, size_t argsize,
  206. enum quoting_style quoting_style, int flags,
  207. unsigned int const *quote_these_too,
  208. char const *left_quote,
  209. char const *right_quote)
  210. {
  211. size_t i;
  212. size_t len = 0;
  213. char const *quote_string = 0;
  214. size_t quote_string_len = 0;
  215. bool backslash_escapes = false;
  216. bool unibyte_locale = MB_CUR_MAX == 1;
  217. bool elide_outer_quotes = (flags & QA_ELIDE_OUTER_QUOTES) != 0;
  218. #define STORE(c) \
  219. do \
  220. { \
  221. if (len < buffersize) \
  222. buffer[len] = (c); \
  223. len++; \
  224. } \
  225. while (0)
  226. switch (quoting_style)
  227. {
  228. case c_maybe_quoting_style:
  229. quoting_style = c_quoting_style;
  230. elide_outer_quotes = true;
  231. /* Fall through. */
  232. case c_quoting_style:
  233. if (!elide_outer_quotes)
  234. STORE ('"');
  235. backslash_escapes = true;
  236. quote_string = "\"";
  237. quote_string_len = 1;
  238. break;
  239. case escape_quoting_style:
  240. backslash_escapes = true;
  241. elide_outer_quotes = false;
  242. break;
  243. case locale_quoting_style:
  244. case clocale_quoting_style:
  245. case custom_quoting_style:
  246. {
  247. if (quoting_style != custom_quoting_style)
  248. {
  249. /* TRANSLATORS:
  250. Get translations for open and closing quotation marks.
  251. The message catalog should translate "`" to a left
  252. quotation mark suitable for the locale, and similarly for
  253. "'". For example, a French Unicode local should translate
  254. these to U+00AB (LEFT-POINTING DOUBLE ANGLE
  255. QUOTATION MARK), and U+00BB (RIGHT-POINTING DOUBLE ANGLE
  256. QUOTATION MARK), respectively.
  257. If the catalog has no translation, we will try to
  258. use Unicode U+2018 (LEFT SINGLE QUOTATION MARK) and
  259. Unicode U+2019 (RIGHT SINGLE QUOTATION MARK). If the
  260. current locale is not Unicode, locale_quoting_style
  261. will quote 'like this', and clocale_quoting_style will
  262. quote "like this". You should always include translations
  263. for "`" and "'" even if U+2018 and U+2019 are appropriate
  264. for your locale.
  265. If you don't know what to put here, please see
  266. <http://en.wikipedia.org/wiki/Quotation_marks_in_other_languages>
  267. and use glyphs suitable for your language. */
  268. left_quote = gettext_quote (N_("`"), quoting_style);
  269. right_quote = gettext_quote (N_("'"), quoting_style);
  270. }
  271. if (!elide_outer_quotes)
  272. for (quote_string = left_quote; *quote_string; quote_string++)
  273. STORE (*quote_string);
  274. backslash_escapes = true;
  275. quote_string = right_quote;
  276. quote_string_len = strlen (quote_string);
  277. }
  278. break;
  279. case shell_quoting_style:
  280. quoting_style = shell_always_quoting_style;
  281. elide_outer_quotes = true;
  282. /* Fall through. */
  283. case shell_always_quoting_style:
  284. if (!elide_outer_quotes)
  285. STORE ('\'');
  286. quote_string = "'";
  287. quote_string_len = 1;
  288. break;
  289. case literal_quoting_style:
  290. elide_outer_quotes = false;
  291. break;
  292. default:
  293. abort ();
  294. }
  295. for (i = 0; ! (argsize == SIZE_MAX ? arg[i] == '\0' : i == argsize); i++)
  296. {
  297. unsigned char c;
  298. unsigned char esc;
  299. bool is_right_quote = false;
  300. if (backslash_escapes
  301. && quote_string_len
  302. && (i + quote_string_len
  303. <= (argsize == SIZE_MAX && 1 < quote_string_len
  304. /* Use strlen only if we must: when argsize is SIZE_MAX,
  305. and when the quote string is more than 1 byte long.
  306. If we do call strlen, save the result. */
  307. ? (argsize = strlen (arg)) : argsize))
  308. && memcmp (arg + i, quote_string, quote_string_len) == 0)
  309. {
  310. if (elide_outer_quotes)
  311. goto force_outer_quoting_style;
  312. is_right_quote = true;
  313. }
  314. c = arg[i];
  315. switch (c)
  316. {
  317. case '\0':
  318. if (backslash_escapes)
  319. {
  320. if (elide_outer_quotes)
  321. goto force_outer_quoting_style;
  322. STORE ('\\');
  323. /* If quote_string were to begin with digits, we'd need to
  324. test for the end of the arg as well. However, it's
  325. hard to imagine any locale that would use digits in
  326. quotes, and set_custom_quoting is documented not to
  327. accept them. */
  328. if (i + 1 < argsize && '0' <= arg[i + 1] && arg[i + 1] <= '9')
  329. {
  330. STORE ('0');
  331. STORE ('0');
  332. }
  333. c = '0';
  334. /* We don't have to worry that this last '0' will be
  335. backslash-escaped because, again, quote_string should
  336. not start with it and because quote_these_too is
  337. documented as not accepting it. */
  338. }
  339. else if (flags & QA_ELIDE_NULL_BYTES)
  340. continue;
  341. break;
  342. case '?':
  343. switch (quoting_style)
  344. {
  345. case shell_always_quoting_style:
  346. if (elide_outer_quotes)
  347. goto force_outer_quoting_style;
  348. break;
  349. case c_quoting_style:
  350. if ((flags & QA_SPLIT_TRIGRAPHS)
  351. && i + 2 < argsize && arg[i + 1] == '?')
  352. switch (arg[i + 2])
  353. {
  354. case '!': case '\'':
  355. case '(': case ')': case '-': case '/':
  356. case '<': case '=': case '>':
  357. /* Escape the second '?' in what would otherwise be
  358. a trigraph. */
  359. if (elide_outer_quotes)
  360. goto force_outer_quoting_style;
  361. c = arg[i + 2];
  362. i += 2;
  363. STORE ('?');
  364. STORE ('"');
  365. STORE ('"');
  366. STORE ('?');
  367. break;
  368. default:
  369. break;
  370. }
  371. break;
  372. default:
  373. break;
  374. }
  375. break;
  376. case '\a': esc = 'a'; goto c_escape;
  377. case '\b': esc = 'b'; goto c_escape;
  378. case '\f': esc = 'f'; goto c_escape;
  379. case '\n': esc = 'n'; goto c_and_shell_escape;
  380. case '\r': esc = 'r'; goto c_and_shell_escape;
  381. case '\t': esc = 't'; goto c_and_shell_escape;
  382. case '\v': esc = 'v'; goto c_escape;
  383. case '\\': esc = c;
  384. /* No need to escape the escape if we are trying to elide
  385. outer quotes and nothing else is problematic. */
  386. if (backslash_escapes && elide_outer_quotes && quote_string_len)
  387. goto store_c;
  388. c_and_shell_escape:
  389. if (quoting_style == shell_always_quoting_style
  390. && elide_outer_quotes)
  391. goto force_outer_quoting_style;
  392. /* Fall through. */
  393. c_escape:
  394. if (backslash_escapes)
  395. {
  396. c = esc;
  397. goto store_escape;
  398. }
  399. break;
  400. case '{': case '}': /* sometimes special if isolated */
  401. if (! (argsize == SIZE_MAX ? arg[1] == '\0' : argsize == 1))
  402. break;
  403. /* Fall through. */
  404. case '#': case '~':
  405. if (i != 0)
  406. break;
  407. /* Fall through. */
  408. case ' ':
  409. case '!': /* special in bash */
  410. case '"': case '$': case '&':
  411. case '(': case ')': case '*': case ';':
  412. case '<':
  413. case '=': /* sometimes special in 0th or (with "set -k") later args */
  414. case '>': case '[':
  415. case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
  416. case '`': case '|':
  417. /* A shell special character. In theory, '$' and '`' could
  418. be the first bytes of multibyte characters, which means
  419. we should check them with mbrtowc, but in practice this
  420. doesn't happen so it's not worth worrying about. */
  421. if (quoting_style == shell_always_quoting_style
  422. && elide_outer_quotes)
  423. goto force_outer_quoting_style;
  424. break;
  425. case '\'':
  426. if (quoting_style == shell_always_quoting_style)
  427. {
  428. if (elide_outer_quotes)
  429. goto force_outer_quoting_style;
  430. STORE ('\'');
  431. STORE ('\\');
  432. STORE ('\'');
  433. }
  434. break;
  435. case '%': case '+': case ',': case '-': case '.': case '/':
  436. case '0': case '1': case '2': case '3': case '4': case '5':
  437. case '6': case '7': case '8': case '9': case ':':
  438. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  439. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  440. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  441. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  442. case 'Y': case 'Z': case ']': case '_': case 'a': case 'b':
  443. case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
  444. case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
  445. case 'o': case 'p': case 'q': case 'r': case 's': case 't':
  446. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  447. /* These characters don't cause problems, no matter what the
  448. quoting style is. They cannot start multibyte sequences.
  449. A digit or a special letter would cause trouble if it
  450. appeared at the beginning of quote_string because we'd then
  451. escape by prepending a backslash. However, it's hard to
  452. imagine any locale that would use digits or letters as
  453. quotes, and set_custom_quoting is documented not to accept
  454. them. Also, a digit or a special letter would cause
  455. trouble if it appeared in quote_these_too, but that's also
  456. documented as not accepting them. */
  457. break;
  458. default:
  459. /* If we have a multibyte sequence, copy it until we reach
  460. its end, find an error, or come back to the initial shift
  461. state. For C-like styles, if the sequence has
  462. unprintable characters, escape the whole sequence, since
  463. we can't easily escape single characters within it. */
  464. {
  465. /* Length of multibyte sequence found so far. */
  466. size_t m;
  467. bool printable;
  468. if (unibyte_locale)
  469. {
  470. m = 1;
  471. printable = isprint (c) != 0;
  472. }
  473. else
  474. {
  475. mbstate_t mbstate;
  476. memset (&mbstate, 0, sizeof mbstate);
  477. m = 0;
  478. printable = true;
  479. if (argsize == SIZE_MAX)
  480. argsize = strlen (arg);
  481. do
  482. {
  483. wchar_t w;
  484. size_t bytes = mbrtowc (&w, &arg[i + m],
  485. argsize - (i + m), &mbstate);
  486. if (bytes == 0)
  487. break;
  488. else if (bytes == (size_t) -1)
  489. {
  490. printable = false;
  491. break;
  492. }
  493. else if (bytes == (size_t) -2)
  494. {
  495. printable = false;
  496. while (i + m < argsize && arg[i + m])
  497. m++;
  498. break;
  499. }
  500. else
  501. {
  502. /* Work around a bug with older shells that "see" a '\'
  503. that is really the 2nd byte of a multibyte character.
  504. In practice the problem is limited to ASCII
  505. chars >= '@' that are shell special chars. */
  506. if ('[' == 0x5b && elide_outer_quotes
  507. && quoting_style == shell_always_quoting_style)
  508. {
  509. size_t j;
  510. for (j = 1; j < bytes; j++)
  511. switch (arg[i + m + j])
  512. {
  513. case '[': case '\\': case '^':
  514. case '`': case '|':
  515. goto force_outer_quoting_style;
  516. default:
  517. break;
  518. }
  519. }
  520. if (! iswprint (w))
  521. printable = false;
  522. m += bytes;
  523. }
  524. }
  525. while (! mbsinit (&mbstate));
  526. }
  527. if (1 < m || (backslash_escapes && ! printable))
  528. {
  529. /* Output a multibyte sequence, or an escaped
  530. unprintable unibyte character. */
  531. size_t ilim = i + m;
  532. for (;;)
  533. {
  534. if (backslash_escapes && ! printable)
  535. {
  536. if (elide_outer_quotes)
  537. goto force_outer_quoting_style;
  538. STORE ('\\');
  539. STORE ('0' + (c >> 6));
  540. STORE ('0' + ((c >> 3) & 7));
  541. c = '0' + (c & 7);
  542. }
  543. else if (is_right_quote)
  544. {
  545. STORE ('\\');
  546. is_right_quote = false;
  547. }
  548. if (ilim <= i + 1)
  549. break;
  550. STORE (c);
  551. c = arg[++i];
  552. }
  553. goto store_c;
  554. }
  555. }
  556. }
  557. if (! ((backslash_escapes || elide_outer_quotes)
  558. && quote_these_too
  559. && quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS)))
  560. && !is_right_quote)
  561. goto store_c;
  562. store_escape:
  563. if (elide_outer_quotes)
  564. goto force_outer_quoting_style;
  565. STORE ('\\');
  566. store_c:
  567. STORE (c);
  568. }
  569. if (len == 0 && quoting_style == shell_always_quoting_style
  570. && elide_outer_quotes)
  571. goto force_outer_quoting_style;
  572. if (quote_string && !elide_outer_quotes)
  573. for (; *quote_string; quote_string++)
  574. STORE (*quote_string);
  575. if (len < buffersize)
  576. buffer[len] = '\0';
  577. return len;
  578. force_outer_quoting_style:
  579. /* Don't reuse quote_these_too, since the addition of outer quotes
  580. sufficiently quotes the specified characters. */
  581. return quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
  582. quoting_style,
  583. flags & ~QA_ELIDE_OUTER_QUOTES, NULL,
  584. left_quote, right_quote);
  585. }
  586. /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
  587. argument ARG (of size ARGSIZE), using O to control quoting.
  588. If O is null, use the default.
  589. Terminate the output with a null character, and return the written
  590. size of the output, not counting the terminating null.
  591. If BUFFERSIZE is too small to store the output string, return the
  592. value that would have been returned had BUFFERSIZE been large enough.
  593. If ARGSIZE is SIZE_MAX, use the string length of the argument for
  594. ARGSIZE. */
  595. size_t
  596. quotearg_buffer (char *buffer, size_t buffersize,
  597. char const *arg, size_t argsize,
  598. struct quoting_options const *o)
  599. {
  600. struct quoting_options const *p = o ? o : &default_quoting_options;
  601. int e = errno;
  602. size_t r = quotearg_buffer_restyled (buffer, buffersize, arg, argsize,
  603. p->style, p->flags, p->quote_these_too,
  604. p->left_quote, p->right_quote);
  605. errno = e;
  606. return r;
  607. }
  608. /* Equivalent to quotearg_alloc (ARG, ARGSIZE, NULL, O). */
  609. char *
  610. quotearg_alloc (char const *arg, size_t argsize,
  611. struct quoting_options const *o)
  612. {
  613. return quotearg_alloc_mem (arg, argsize, NULL, o);
  614. }
  615. /* Like quotearg_buffer (..., ARG, ARGSIZE, O), except return newly
  616. allocated storage containing the quoted string, and store the
  617. resulting size into *SIZE, if non-NULL. The result can contain
  618. embedded null bytes only if ARGSIZE is not SIZE_MAX, SIZE is not
  619. NULL, and set_quoting_flags has not set the null byte elision
  620. flag. */
  621. char *
  622. quotearg_alloc_mem (char const *arg, size_t argsize, size_t *size,
  623. struct quoting_options const *o)
  624. {
  625. struct quoting_options const *p = o ? o : &default_quoting_options;
  626. int e = errno;
  627. /* Elide embedded null bytes if we can't return a size. */
  628. int flags = p->flags | (size ? 0 : QA_ELIDE_NULL_BYTES);
  629. size_t bufsize = quotearg_buffer_restyled (0, 0, arg, argsize, p->style,
  630. flags, p->quote_these_too,
  631. p->left_quote,
  632. p->right_quote) + 1;
  633. char *buf = xcharalloc (bufsize);
  634. quotearg_buffer_restyled (buf, bufsize, arg, argsize, p->style, flags,
  635. p->quote_these_too,
  636. p->left_quote, p->right_quote);
  637. errno = e;
  638. if (size)
  639. *size = bufsize - 1;
  640. return buf;
  641. }
  642. /* A storage slot with size and pointer to a value. */
  643. struct slotvec
  644. {
  645. size_t size;
  646. char *val;
  647. };
  648. /* Preallocate a slot 0 buffer, so that the caller can always quote
  649. one small component of a "memory exhausted" message in slot 0. */
  650. static char slot0[256];
  651. static unsigned int nslots = 1;
  652. static struct slotvec slotvec0 = {sizeof slot0, slot0};
  653. static struct slotvec *slotvec = &slotvec0;
  654. void
  655. quotearg_free (void)
  656. {
  657. struct slotvec *sv = slotvec;
  658. unsigned int i;
  659. for (i = 1; i < nslots; i++)
  660. free (sv[i].val);
  661. if (sv[0].val != slot0)
  662. {
  663. free (sv[0].val);
  664. slotvec0.size = sizeof slot0;
  665. slotvec0.val = slot0;
  666. }
  667. if (sv != &slotvec0)
  668. {
  669. free (sv);
  670. slotvec = &slotvec0;
  671. }
  672. nslots = 1;
  673. }
  674. /* Use storage slot N to return a quoted version of argument ARG.
  675. ARG is of size ARGSIZE, but if that is SIZE_MAX, ARG is a
  676. null-terminated string.
  677. OPTIONS specifies the quoting options.
  678. The returned value points to static storage that can be
  679. reused by the next call to this function with the same value of N.
  680. N must be nonnegative. N is deliberately declared with type "int"
  681. to allow for future extensions (using negative values). */
  682. static char *
  683. quotearg_n_options (int n, char const *arg, size_t argsize,
  684. struct quoting_options const *options)
  685. {
  686. int e = errno;
  687. unsigned int n0 = n;
  688. struct slotvec *sv = slotvec;
  689. if (n < 0)
  690. abort ();
  691. if (nslots <= n0)
  692. {
  693. /* FIXME: technically, the type of n1 should be 'unsigned int',
  694. but that evokes an unsuppressible warning from gcc-4.0.1 and
  695. older. If gcc ever provides an option to suppress that warning,
  696. revert to the original type, so that the test in xalloc_oversized
  697. is once again performed only at compile time. */
  698. size_t n1 = n0 + 1;
  699. bool preallocated = (sv == &slotvec0);
  700. if (xalloc_oversized (n1, sizeof *sv))
  701. xalloc_die ();
  702. slotvec = sv = xrealloc (preallocated ? NULL : sv, n1 * sizeof *sv);
  703. if (preallocated)
  704. *sv = slotvec0;
  705. memset (sv + nslots, 0, (n1 - nslots) * sizeof *sv);
  706. nslots = n1;
  707. }
  708. {
  709. size_t size = sv[n].size;
  710. char *val = sv[n].val;
  711. /* Elide embedded null bytes since we don't return a size. */
  712. int flags = options->flags | QA_ELIDE_NULL_BYTES;
  713. size_t qsize = quotearg_buffer_restyled (val, size, arg, argsize,
  714. options->style, flags,
  715. options->quote_these_too,
  716. options->left_quote,
  717. options->right_quote);
  718. if (size <= qsize)
  719. {
  720. sv[n].size = size = qsize + 1;
  721. if (val != slot0)
  722. free (val);
  723. sv[n].val = val = xcharalloc (size);
  724. quotearg_buffer_restyled (val, size, arg, argsize, options->style,
  725. flags, options->quote_these_too,
  726. options->left_quote,
  727. options->right_quote);
  728. }
  729. errno = e;
  730. return val;
  731. }
  732. }
  733. char *
  734. quotearg_n (int n, char const *arg)
  735. {
  736. return quotearg_n_options (n, arg, SIZE_MAX, &default_quoting_options);
  737. }
  738. char *
  739. quotearg_n_mem (int n, char const *arg, size_t argsize)
  740. {
  741. return quotearg_n_options (n, arg, argsize, &default_quoting_options);
  742. }
  743. char *
  744. quotearg (char const *arg)
  745. {
  746. return quotearg_n (0, arg);
  747. }
  748. char *
  749. quotearg_mem (char const *arg, size_t argsize)
  750. {
  751. return quotearg_n_mem (0, arg, argsize);
  752. }
  753. char *
  754. quotearg_n_style (int n, enum quoting_style s, char const *arg)
  755. {
  756. struct quoting_options const o = quoting_options_from_style (s);
  757. return quotearg_n_options (n, arg, SIZE_MAX, &o);
  758. }
  759. char *
  760. quotearg_n_style_mem (int n, enum quoting_style s,
  761. char const *arg, size_t argsize)
  762. {
  763. struct quoting_options const o = quoting_options_from_style (s);
  764. return quotearg_n_options (n, arg, argsize, &o);
  765. }
  766. char *
  767. quotearg_style (enum quoting_style s, char const *arg)
  768. {
  769. return quotearg_n_style (0, s, arg);
  770. }
  771. char *
  772. quotearg_style_mem (enum quoting_style s, char const *arg, size_t argsize)
  773. {
  774. return quotearg_n_style_mem (0, s, arg, argsize);
  775. }
  776. char *
  777. quotearg_char_mem (char const *arg, size_t argsize, char ch)
  778. {
  779. struct quoting_options options;
  780. options = default_quoting_options;
  781. set_char_quoting (&options, ch, 1);
  782. return quotearg_n_options (0, arg, argsize, &options);
  783. }
  784. char *
  785. quotearg_char (char const *arg, char ch)
  786. {
  787. return quotearg_char_mem (arg, SIZE_MAX, ch);
  788. }
  789. char *
  790. quotearg_colon (char const *arg)
  791. {
  792. return quotearg_char (arg, ':');
  793. }
  794. char *
  795. quotearg_colon_mem (char const *arg, size_t argsize)
  796. {
  797. return quotearg_char_mem (arg, argsize, ':');
  798. }
  799. char *
  800. quotearg_n_custom (int n, char const *left_quote,
  801. char const *right_quote, char const *arg)
  802. {
  803. return quotearg_n_custom_mem (n, left_quote, right_quote, arg,
  804. SIZE_MAX);
  805. }
  806. char *
  807. quotearg_n_custom_mem (int n, char const *left_quote,
  808. char const *right_quote,
  809. char const *arg, size_t argsize)
  810. {
  811. struct quoting_options o = default_quoting_options;
  812. set_custom_quoting (&o, left_quote, right_quote);
  813. return quotearg_n_options (n, arg, argsize, &o);
  814. }
  815. char *
  816. quotearg_custom (char const *left_quote, char const *right_quote,
  817. char const *arg)
  818. {
  819. return quotearg_n_custom (0, left_quote, right_quote, arg);
  820. }
  821. char *
  822. quotearg_custom_mem (char const *left_quote, char const *right_quote,
  823. char const *arg, size_t argsize)
  824. {
  825. return quotearg_n_custom_mem (0, left_quote, right_quote, arg,
  826. argsize);
  827. }
  828. /* The quoting option used by the functions of quote.h. */
  829. struct quoting_options quote_quoting_options =
  830. {
  831. locale_quoting_style,
  832. 0,
  833. { 0 },
  834. NULL, NULL
  835. };
  836. char const *
  837. quote_n_mem (int n, char const *arg, size_t argsize)
  838. {
  839. return quotearg_n_options (n, arg, argsize, &quote_quoting_options);
  840. }
  841. char const *
  842. quote_mem (char const *arg, size_t argsize)
  843. {
  844. return quote_n_mem (0, arg, argsize);
  845. }
  846. char const *
  847. quote_n (int n, char const *arg)
  848. {
  849. return quote_n_mem (n, arg, SIZE_MAX);
  850. }
  851. char const *
  852. quote (char const *arg)
  853. {
  854. return quote_n (0, arg);
  855. }