quotearg.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* quotearg.h - quote arguments for output
  2. Copyright (C) 1998-2002, 2004, 2006, 2008-2013 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 as for a C language string (ls --quoting-style=c).
  63. Behaves like c_maybe_quoting_style if QA_ELIDE_OUTER_QUOTES is
  64. in effect. Split into consecutive strings if
  65. QA_SPLIT_TRIGRAPHS.
  66. quotearg_buffer:
  67. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  68. quotearg:
  69. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  70. quotearg_colon:
  71. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
  72. */
  73. c_quoting_style,
  74. /* Like c_quoting_style except omit the surrounding double-quote
  75. characters if no quoted characters are encountered.
  76. quotearg_buffer:
  77. "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
  78. quotearg:
  79. "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "a:b"
  80. quotearg_colon:
  81. "simple", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  82. */
  83. c_maybe_quoting_style,
  84. /* Like c_quoting_style except always omit the surrounding
  85. double-quote characters and ignore QA_SPLIT_TRIGRAPHS
  86. (ls --quoting-style=escape).
  87. quotearg_buffer:
  88. "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
  89. quotearg:
  90. "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a:b"
  91. quotearg_colon:
  92. "simple", "\\0 \\t\\n'\"\\033??/\\\\", "a\\:b"
  93. */
  94. escape_quoting_style,
  95. /* Like clocale_quoting_style, but use single quotes in the
  96. default C locale or if the program does not use gettext
  97. (ls --quoting-style=locale). For UTF-8 locales, quote
  98. characters will use Unicode.
  99. LC_MESSAGES=C
  100. quotearg_buffer:
  101. "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
  102. quotearg:
  103. "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a:b'"
  104. quotearg_colon:
  105. "`simple'", "`\\0 \\t\\n\\'\"\\033??/\\\\'", "`a\\:b'"
  106. LC_MESSAGES=pt_PT.utf8
  107. quotearg_buffer:
  108. "\302\253simple\302\273",
  109. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  110. quotearg:
  111. "\302\253simple\302\273",
  112. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  113. quotearg_colon:
  114. "\302\253simple\302\273",
  115. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
  116. */
  117. locale_quoting_style,
  118. /* Like c_quoting_style except use quotation marks appropriate for
  119. the locale and ignore QA_SPLIT_TRIGRAPHS
  120. (ls --quoting-style=clocale).
  121. LC_MESSAGES=C
  122. quotearg_buffer:
  123. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  124. quotearg:
  125. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a:b\""
  126. quotearg_colon:
  127. "\"simple\"", "\"\\0 \\t\\n'\\\"\\033??/\\\\\"", "\"a\\:b\""
  128. LC_MESSAGES=pt_PT.utf8
  129. quotearg_buffer:
  130. "\302\253simple\302\273",
  131. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  132. quotearg:
  133. "\302\253simple\302\273",
  134. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a:b\302\273"
  135. quotearg_colon:
  136. "\302\253simple\302\273",
  137. "\302\253\\0 \\t\\n'\"\\033??/\\\\\302\253", "\302\253a\\:b\302\273"
  138. */
  139. clocale_quoting_style,
  140. /* Like clocale_quoting_style except use the custom quotation marks
  141. set by set_custom_quoting. If custom quotation marks are not
  142. set, the behavior is undefined.
  143. left_quote = right_quote = "'"
  144. quotearg_buffer:
  145. "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
  146. quotearg:
  147. "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a:b'"
  148. quotearg_colon:
  149. "'simple'", "'\\0 \\t\\n\\'\"\\033??/\\\\'", "'a\\:b'"
  150. left_quote = "(" and right_quote = ")"
  151. quotearg_buffer:
  152. "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
  153. quotearg:
  154. "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a:b)"
  155. quotearg_colon:
  156. "(simple)", "(\\0 \\t\\n'\"\\033??/\\\\)", "(a\\:b)"
  157. left_quote = ":" and right_quote = " "
  158. quotearg_buffer:
  159. ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
  160. quotearg:
  161. ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a:b "
  162. quotearg_colon:
  163. ":simple ", ":\\0\\ \\t\\n'\"\\033??/\\\\ ", ":a\\:b "
  164. left_quote = "\"'" and right_quote = "'\""
  165. Notice that this is treated as a single level of quotes or two
  166. levels where the outer quote need not be escaped within the inner
  167. quotes. For two levels where the outer quote must be escaped
  168. within the inner quotes, you must use separate quotearg
  169. invocations.
  170. quotearg_buffer:
  171. "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
  172. quotearg:
  173. "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a:b'\""
  174. quotearg_colon:
  175. "\"'simple'\"", "\"'\\0 \\t\\n\\'\"\\033??/\\\\'\"", "\"'a\\:b'\""
  176. */
  177. custom_quoting_style
  178. };
  179. /* Flags for use in set_quoting_flags. */
  180. enum quoting_flags
  181. {
  182. /* Always elide null bytes from styles that do not quote them,
  183. even when the length of the result is available to the
  184. caller. */
  185. QA_ELIDE_NULL_BYTES = 0x01,
  186. /* Omit the surrounding quote characters if no escaped characters
  187. are encountered. Note that if no other character needs
  188. escaping, then neither does the escape character. */
  189. QA_ELIDE_OUTER_QUOTES = 0x02,
  190. /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
  191. trigraph sequences into concatenated strings (for example,
  192. "?""?/" rather than "??/", which could be confused with
  193. "\\"). */
  194. QA_SPLIT_TRIGRAPHS = 0x04
  195. };
  196. /* For now, --quoting-style=literal is the default, but this may change. */
  197. # ifndef DEFAULT_QUOTING_STYLE
  198. # define DEFAULT_QUOTING_STYLE literal_quoting_style
  199. # endif
  200. /* Names of quoting styles and their corresponding values. */
  201. extern char const *const quoting_style_args[];
  202. extern enum quoting_style const quoting_style_vals[];
  203. struct quoting_options;
  204. /* The functions listed below set and use a hidden variable
  205. that contains the default quoting style options. */
  206. /* Allocate a new set of quoting options, with contents initially identical
  207. to O if O is not null, or to the default if O is null.
  208. It is the caller's responsibility to free the result. */
  209. struct quoting_options *clone_quoting_options (struct quoting_options *o);
  210. /* Get the value of O's quoting style. If O is null, use the default. */
  211. enum quoting_style get_quoting_style (struct quoting_options *o);
  212. /* In O (or in the default if O is null),
  213. set the value of the quoting style to S. */
  214. void set_quoting_style (struct quoting_options *o, enum quoting_style s);
  215. /* In O (or in the default if O is null),
  216. set the value of the quoting options for character C to I.
  217. Return the old value. Currently, the only values defined for I are
  218. 0 (the default) and 1 (which means to quote the character even if
  219. it would not otherwise be quoted). C must never be a digit or a
  220. letter that has special meaning after a backslash (for example, "\t"
  221. for tab). */
  222. int set_char_quoting (struct quoting_options *o, char c, int i);
  223. /* In O (or in the default if O is null),
  224. set the value of the quoting options flag to I, which can be a
  225. bitwise combination of enum quoting_flags, or 0 for default
  226. behavior. Return the old value. */
  227. int set_quoting_flags (struct quoting_options *o, int i);
  228. /* In O (or in the default if O is null),
  229. set the value of the quoting style to custom_quoting_style,
  230. set the left quote to LEFT_QUOTE, and set the right quote to
  231. RIGHT_QUOTE. Each of LEFT_QUOTE and RIGHT_QUOTE must be
  232. null-terminated and can be the empty string. Because backslashes are
  233. used for escaping, it does not make sense for RIGHT_QUOTE to contain
  234. a backslash. RIGHT_QUOTE must not begin with a digit or a letter
  235. that has special meaning after a backslash (for example, "\t" for
  236. tab). */
  237. void set_custom_quoting (struct quoting_options *o,
  238. char const *left_quote,
  239. char const *right_quote);
  240. /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
  241. argument ARG (of size ARGSIZE), using O to control quoting.
  242. If O is null, use the default.
  243. Terminate the output with a null character, and return the written
  244. size of the output, not counting the terminating null.
  245. If BUFFERSIZE is too small to store the output string, return the
  246. value that would have been returned had BUFFERSIZE been large enough.
  247. If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
  248. On output, BUFFER might contain embedded null bytes if ARGSIZE was
  249. not -1, the style of O does not use backslash escapes, and the
  250. flags of O do not request elision of null bytes.*/
  251. size_t quotearg_buffer (char *buffer, size_t buffersize,
  252. char const *arg, size_t argsize,
  253. struct quoting_options const *o);
  254. /* Like quotearg_buffer, except return the result in a newly allocated
  255. buffer. It is the caller's responsibility to free the result. The
  256. result will not contain embedded null bytes. */
  257. char *quotearg_alloc (char const *arg, size_t argsize,
  258. struct quoting_options const *o);
  259. /* Like quotearg_alloc, except that the length of the result,
  260. excluding the terminating null byte, is stored into SIZE if it is
  261. non-NULL. The result might contain embedded null bytes if ARGSIZE
  262. was not -1, SIZE was not NULL, the style of O does not use
  263. backslash escapes, and the flags of O do not request elision of
  264. null bytes.*/
  265. char *quotearg_alloc_mem (char const *arg, size_t argsize,
  266. size_t *size, struct quoting_options const *o);
  267. /* Use storage slot N to return a quoted version of the string ARG.
  268. Use the default quoting options.
  269. The returned value points to static storage that can be
  270. reused by the next call to this function with the same value of N.
  271. N must be nonnegative. The output of all functions in the
  272. quotearg_n family are guaranteed to not contain embedded null
  273. bytes.*/
  274. char *quotearg_n (int n, char const *arg);
  275. /* Equivalent to quotearg_n (0, ARG). */
  276. char *quotearg (char const *arg);
  277. /* Use storage slot N to return a quoted version of the argument ARG
  278. of size ARGSIZE. This is like quotearg_n (N, ARG), except it can
  279. quote null bytes. */
  280. char *quotearg_n_mem (int n, char const *arg, size_t argsize);
  281. /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE). */
  282. char *quotearg_mem (char const *arg, size_t argsize);
  283. /* Use style S and storage slot N to return a quoted version of the string ARG.
  284. This is like quotearg_n (N, ARG), except that it uses S with no other
  285. options to specify the quoting method. */
  286. char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
  287. /* Use style S and storage slot N to return a quoted version of the
  288. argument ARG of size ARGSIZE. This is like quotearg_n_style
  289. (N, S, ARG), except it can quote null bytes. */
  290. char *quotearg_n_style_mem (int n, enum quoting_style s,
  291. char const *arg, size_t argsize);
  292. /* Equivalent to quotearg_n_style (0, S, ARG). */
  293. char *quotearg_style (enum quoting_style s, char const *arg);
  294. /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE). */
  295. char *quotearg_style_mem (enum quoting_style s,
  296. char const *arg, size_t argsize);
  297. /* Like quotearg (ARG), except also quote any instances of CH.
  298. See set_char_quoting for a description of acceptable CH values. */
  299. char *quotearg_char (char const *arg, char ch);
  300. /* Like quotearg_char (ARG, CH), except it can quote null bytes. */
  301. char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
  302. /* Equivalent to quotearg_char (ARG, ':'). */
  303. char *quotearg_colon (char const *arg);
  304. /* Like quotearg_colon (ARG), except it can quote null bytes. */
  305. char *quotearg_colon_mem (char const *arg, size_t argsize);
  306. /* Like quotearg_n_style (N, S, ARG) but with S as custom_quoting_style
  307. with left quote as LEFT_QUOTE and right quote as RIGHT_QUOTE. See
  308. set_custom_quoting for a description of acceptable LEFT_QUOTE and
  309. RIGHT_QUOTE values. */
  310. char *quotearg_n_custom (int n, char const *left_quote,
  311. char const *right_quote, char const *arg);
  312. /* Like quotearg_n_custom (N, LEFT_QUOTE, RIGHT_QUOTE, ARG) except it
  313. can quote null bytes. */
  314. char *quotearg_n_custom_mem (int n, char const *left_quote,
  315. char const *right_quote,
  316. char const *arg, size_t argsize);
  317. /* Equivalent to quotearg_n_custom (0, LEFT_QUOTE, RIGHT_QUOTE, ARG). */
  318. char *quotearg_custom (char const *left_quote, char const *right_quote,
  319. char const *arg);
  320. /* Equivalent to quotearg_n_custom_mem (0, LEFT_QUOTE, RIGHT_QUOTE, ARG,
  321. ARGSIZE). */
  322. char *quotearg_custom_mem (char const *left_quote,
  323. char const *right_quote,
  324. char const *arg, size_t argsize);
  325. /* Free any dynamically allocated memory. */
  326. void quotearg_free (void);
  327. #endif /* !QUOTEARG_H_ */