quotearg.c 34 KB

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