quotearg.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* quotearg.h - quote arguments for output
  2. Copyright (C) 1998-2002, 2004, 2006, 2008-2016 Free Software Foundation,
  3. Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by Paul Eggert <eggert@twinsun.com> */
  15. #ifndef QUOTEARG_H_
  16. # define QUOTEARG_H_ 1
  17. # include <stddef.h>
  18. /* Basic quoting styles. For each style, an example is given on the
  19. input strings "simple", "\0 \t\n'\"\033?""?/\\", and "a:b", using
  20. quotearg_buffer, quotearg_mem, and quotearg_colon_mem with that
  21. style and the default flags and quoted characters. Note that the
  22. examples are shown here as valid C strings rather than what
  23. displays on a terminal (with "??/" as a trigraph for "\\"). */
  24. enum quoting_style
  25. {
  26. /* Output names as-is (ls --quoting-style=literal). Can result in
  27. embedded null bytes if QA_ELIDE_NULL_BYTES is not in
  28. effect.
  29. quotearg_buffer:
  30. "simple", "\0 \t\n'\"\033??/\\", "a:b"
  31. quotearg:
  32. "simple", " \t\n'\"\033??/\\", "a:b"
  33. quotearg_colon:
  34. "simple", " \t\n'\"\033??/\\", "a:b"
  35. */
  36. literal_quoting_style,
  37. /* Quote names for the shell if they contain shell metacharacters
  38. or would cause ambiguous output (ls --quoting-style=shell).
  39. Can result in embedded null bytes if QA_ELIDE_NULL_BYTES is not
  40. in effect.
  41. quotearg_buffer:
  42. "simple", "'\0 \t\n'\\''\"\033??/\\'", "a:b"
  43. quotearg:
  44. "simple", "' \t\n'\\''\"\033??/\\'", "a:b"
  45. quotearg_colon:
  46. "simple", "' \t\n'\\''\"\033??/\\'", "'a:b'"
  47. */
  48. shell_quoting_style,
  49. /* Quote names for the shell, even if they would normally not
  50. require quoting (ls --quoting-style=shell-always). Can result
  51. in embedded null bytes if QA_ELIDE_NULL_BYTES is not in effect.
  52. Behaves like shell_quoting_style if QA_ELIDE_OUTER_QUOTES is in
  53. effect.
  54. quotearg_buffer:
  55. "'simple'", "'\0 \t\n'\\''\"\033??/\\'", "'a:b'"
  56. quotearg:
  57. "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
  58. quotearg_colon:
  59. "'simple'", "' \t\n'\\''\"\033??/\\'", "'a:b'"
  60. */
  61. shell_always_quoting_style,
  62. /* Quote names for the shell if they contain shell metacharacters
  63. or other problematic characters (ls --quoting-style=shell-escape).
  64. Non printable characters are quoted using the $'...' syntax,
  65. which originated in ksh93 and is widely supported by most shells,
  66. and proposed for inclusion in POSIX.
  67. quotearg_buffer:
  68. "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\\'", "a:b"
  69. quotearg:
  70. "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\\'", "a:b"
  71. quotearg_colon:
  72. "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\\'", "'a:b'"
  73. */
  74. shell_escape_quoting_style,
  75. /* Quote names for the shell even if they would normally not
  76. require quoting (ls --quoting-style=shell-escape).
  77. Non printable characters are quoted using the $'...' syntax,
  78. which originated in ksh93 and is widely supported by most shells,
  79. and proposed for inclusion in POSIX. Behaves like
  80. shell_escape_quoting_style if QA_ELIDE_OUTER_QUOTES is in effect.
  81. quotearg_buffer:
  82. "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\'", "a:b"
  83. quotearg:
  84. "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\'", "a:b"
  85. quotearg_colon:
  86. "simple", "''$'\\0'' '$'\\t\\n'\\''\"'$'\\033''??/\'", "'a:b'"
  87. */
  88. shell_escape_always_quoting_style,
  89. /* Quote names as for a C language string (ls --quoting-style=c).
  90. Behaves like c_maybe_quoting_style if QA_ELIDE_OUTER_QUOTES is
  91. in effect. Split into consecutive strings if
  92. QA_SPLIT_TRIGRAPHS.
  93. quotearg_buffer:
  94. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  95. quotearg:
  96. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  97. quotearg_colon:
  98. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
  99. */
  100. c_quoting_style,
  101. /* Like c_quoting_style except omit the surrounding double-quote
  102. characters if no quoted characters are encountered.
  103. quotearg_buffer:
  104. "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
  105. quotearg:
  106. "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
  107. quotearg_colon:
  108. "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  109. */
  110. c_maybe_quoting_style,
  111. /* Like c_quoting_style except always omit the surrounding
  112. double-quote characters and ignore QA_SPLIT_TRIGRAPHS
  113. (ls --quoting-style=escape).
  114. quotearg_buffer:
  115. "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
  116. quotearg:
  117. "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
  118. quotearg_colon:
  119. "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
  120. */
  121. escape_quoting_style,
  122. /* Like clocale_quoting_style, but use single quotes in the
  123. default C locale or if the program does not use gettext
  124. (ls --quoting-style=locale). For UTF-8 locales, quote
  125. characters will use Unicode.
  126. LC_MESSAGES=C
  127. quotearg_buffer:
  128. "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
  129. quotearg:
  130. "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
  131. quotearg_colon:
  132. "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a\\:b'"
  133. LC_MESSAGES=pt_PT.utf8
  134. quotearg_buffer:
  135. "\302\253simple\302\273",
  136. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  137. quotearg:
  138. "\302\253simple\302\273",
  139. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  140. quotearg_colon:
  141. "\302\253simple\302\273",
  142. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
  143. */
  144. locale_quoting_style,
  145. /* Like c_quoting_style except use quotation marks appropriate for
  146. the locale and ignore QA_SPLIT_TRIGRAPHS
  147. (ls --quoting-style=clocale).
  148. LC_MESSAGES=C
  149. quotearg_buffer:
  150. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  151. quotearg:
  152. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  153. quotearg_colon:
  154. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
  155. LC_MESSAGES=pt_PT.utf8
  156. quotearg_buffer:
  157. "\302\253simple\302\273",
  158. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  159. quotearg:
  160. "\302\253simple\302\273",
  161. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  162. quotearg_colon:
  163. "\302\253simple\302\273",
  164. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
  165. */
  166. clocale_quoting_style,
  167. /* Like clocale_quoting_style except use the custom quotation marks
  168. set by set_custom_quoting. If custom quotation marks are not
  169. set, the behavior is undefined.
  170. left_quote = right_quote = "'"
  171. quotearg_buffer:
  172. "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
  173. quotearg:
  174. "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
  175. quotearg_colon:
  176. "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
  177. left_quote = "(" and right_quote = ")"
  178. quotearg_buffer:
  179. "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
  180. quotearg:
  181. "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
  182. quotearg_colon:
  183. "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a\\:b)"
  184. left_quote = ":" and right_quote = " "
  185. quotearg_buffer:
  186. ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
  187. quotearg:
  188. ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
  189. quotearg_colon:
  190. ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a\\:b "
  191. left_quote = "\"'" and right_quote = "'\""
  192. Notice that this is treated as a single level of quotes or two
  193. levels where the outer quote need not be escaped within the inner
  194. quotes. For two levels where the outer quote must be escaped
  195. within the inner quotes, you must use separate quotearg
  196. invocations.
  197. quotearg_buffer:
  198. "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
  199. quotearg:
  200. "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
  201. quotearg_colon:
  202. "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a\\:b'\""
  203. */
  204. custom_quoting_style
  205. };
  206. /* Flags for use in set_quoting_flags. */
  207. enum quoting_flags
  208. {
  209. /* Always elide null bytes from styles that do not quote them,
  210. even when the length of the result is available to the
  211. caller. */
  212. QA_ELIDE_NULL_BYTES = 0x01,
  213. /* Omit the surrounding quote characters if no escaped characters
  214. are encountered. Note that if no other character needs
  215. escaping, then neither does the escape character. */
  216. QA_ELIDE_OUTER_QUOTES = 0x02,
  217. /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
  218. trigraph sequences into concatenated strings (for example,
  219. "?""?/" rather than "??/", which could be confused with
  220. "\\"). */
  221. QA_SPLIT_TRIGRAPHS = 0x04
  222. };
  223. /* For now, --quoting-style=literal is the default, but this may change. */
  224. # ifndef DEFAULT_QUOTING_STYLE
  225. # define DEFAULT_QUOTING_STYLE literal_quoting_style
  226. # endif
  227. /* Names of quoting styles and their corresponding values. */
  228. extern char const *const quoting_style_args[];
  229. extern enum quoting_style const quoting_style_vals[];
  230. struct quoting_options;
  231. /* The functions listed below set and use a hidden variable
  232. that contains the default quoting style options. */
  233. /* Allocate a new set of quoting options, with contents initially identical
  234. to O if O is not null, or to the default if O is null.
  235. It is the caller's responsibility to free the result. */
  236. struct quoting_options *clone_quoting_options (struct quoting_options *o);
  237. /* Get the value of O's quoting style. If O is null, use the default. */
  238. enum quoting_style get_quoting_style (struct quoting_options const *o);
  239. /* In O (or in the default if O is null),
  240. set the value of the quoting style to S. */
  241. void set_quoting_style (struct quoting_options *o, enum quoting_style s);
  242. /* In O (or in the default if O is null),
  243. set the value of the quoting options for character C to I.
  244. Return the old value. Currently, the only values defined for I are
  245. 0 (the default) and 1 (which means to quote the character even if
  246. it would not otherwise be quoted). C must never be a digit or a
  247. letter that has special meaning after a backslash (for example, "\t"
  248. for tab). */
  249. int set_char_quoting (struct quoting_options *o, char c, int i);
  250. /* In O (or in the default if O is null),
  251. set the value of the quoting options flag to I, which can be a
  252. bitwise combination of enum quoting_flags, or 0 for default
  253. behavior. Return the old value. */
  254. int set_quoting_flags (struct quoting_options *o, int i);
  255. /* In O (or in the default if O is null),
  256. set the value of the quoting style to custom_quoting_style,
  257. set the left quote to LEFT_QUOTE, and set the right quote to
  258. RIGHT_QUOTE. Each of LEFT_QUOTE and RIGHT_QUOTE must be
  259. null-terminated and can be the empty string. Because backslashes are
  260. used for escaping, it does not make sense for RIGHT_QUOTE to contain
  261. a backslash. RIGHT_QUOTE must not begin with a digit or a letter
  262. that has special meaning after a backslash (for example, "\t" for
  263. tab). */
  264. void set_custom_quoting (struct quoting_options *o,
  265. char const *left_quote,
  266. char const *right_quote);
  267. /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
  268. argument ARG (of size ARGSIZE), using O to control quoting.
  269. If O is null, use the default.
  270. Terminate the output with a null character, and return the written
  271. size of the output, not counting the terminating null.
  272. If BUFFERSIZE is too small to store the output string, return the
  273. value that would have been returned had BUFFERSIZE been large enough.
  274. If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
  275. On output, BUFFER might contain embedded null bytes if ARGSIZE was
  276. not -1, the style of O does not use backslash escapes, and the
  277. flags of O do not request elision of null bytes.*/
  278. size_t quotearg_buffer (char *buffer, size_t buffersize,
  279. char const *arg, size_t argsize,
  280. struct quoting_options const *o);
  281. /* Like quotearg_buffer, except return the result in a newly allocated
  282. buffer. It is the caller's responsibility to free the result. The
  283. result will not contain embedded null bytes. */
  284. char *quotearg_alloc (char const *arg, size_t argsize,
  285. struct quoting_options const *o);
  286. /* Like quotearg_alloc, except that the length of the result,
  287. excluding the terminating null byte, is stored into SIZE if it is
  288. non-NULL. The result might contain embedded null bytes if ARGSIZE
  289. was not -1, SIZE was not NULL, the style of O does not use
  290. backslash escapes, and the flags of O do not request elision of
  291. null bytes.*/
  292. char *quotearg_alloc_mem (char const *arg, size_t argsize,
  293. size_t *size, struct quoting_options const *o);
  294. /* Use storage slot N to return a quoted version of the string ARG.
  295. Use the default quoting options.
  296. The returned value points to static storage that can be
  297. reused by the next call to this function with the same value of N.
  298. N must be nonnegative. The output of all functions in the
  299. quotearg_n family are guaranteed to not contain embedded null
  300. bytes.*/
  301. char *quotearg_n (int n, char const *arg);
  302. /* Equivalent to quotearg_n (0, ARG). */
  303. char *quotearg (char const *arg);
  304. /* Use storage slot N to return a quoted version of the argument ARG
  305. of size ARGSIZE. This is like quotearg_n (N, ARG), except it can
  306. quote null bytes. */
  307. char *quotearg_n_mem (int n, char const *arg, size_t argsize);
  308. /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE). */
  309. char *quotearg_mem (char const *arg, size_t argsize);
  310. /* Use style S and storage slot N to return a quoted version of the string ARG.
  311. This is like quotearg_n (N, ARG), except that it uses S with no other
  312. options to specify the quoting method. */
  313. char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
  314. /* Use style S and storage slot N to return a quoted version of the
  315. argument ARG of size ARGSIZE. This is like quotearg_n_style
  316. (N, S, ARG), except it can quote null bytes. */
  317. char *quotearg_n_style_mem (int n, enum quoting_style s,
  318. char const *arg, size_t argsize);
  319. /* Equivalent to quotearg_n_style (0, S, ARG). */
  320. char *quotearg_style (enum quoting_style s, char const *arg);
  321. /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE). */
  322. char *quotearg_style_mem (enum quoting_style s,
  323. char const *arg, size_t argsize);
  324. /* Like quotearg (ARG), except also quote any instances of CH.
  325. See set_char_quoting for a description of acceptable CH values. */
  326. char *quotearg_char (char const *arg, char ch);
  327. /* Like quotearg_char (ARG, CH), except it can quote null bytes. */
  328. char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
  329. /* Equivalent to quotearg_char (ARG, ':'). */
  330. char *quotearg_colon (char const *arg);
  331. /* Like quotearg_colon (ARG), except it can quote null bytes. */
  332. char *quotearg_colon_mem (char const *arg, size_t argsize);
  333. /* Like quotearg_n_style, except with ':' quoting enabled. */
  334. char *quotearg_n_style_colon (int n, enum quoting_style s, char const *arg);
  335. /* Like quotearg_n_style (N, S, ARG) but with S as custom_quoting_style
  336. with left quote as LEFT_QUOTE and right quote as RIGHT_QUOTE. See
  337. set_custom_quoting for a description of acceptable LEFT_QUOTE and
  338. RIGHT_QUOTE values. */
  339. char *quotearg_n_custom (int n, char const *left_quote,
  340. char const *right_quote, char const *arg);
  341. /* Like quotearg_n_custom (N, LEFT_QUOTE, RIGHT_QUOTE, ARG) except it
  342. can quote null bytes. */
  343. char *quotearg_n_custom_mem (int n, char const *left_quote,
  344. char const *right_quote,
  345. char const *arg, size_t argsize);
  346. /* Equivalent to quotearg_n_custom (0, LEFT_QUOTE, RIGHT_QUOTE, ARG). */
  347. char *quotearg_custom (char const *left_quote, char const *right_quote,
  348. char const *arg);
  349. /* Equivalent to quotearg_n_custom_mem (0, LEFT_QUOTE, RIGHT_QUOTE, ARG,
  350. ARGSIZE). */
  351. char *quotearg_custom_mem (char const *left_quote,
  352. char const *right_quote,
  353. char const *arg, size_t argsize);
  354. /* Free any dynamically allocated memory. */
  355. void quotearg_free (void);
  356. #endif /* !QUOTEARG_H_ */