quotearg.c 34 KB

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