4
0

quotearg.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* quotearg.c - quote arguments for output
  2. Copyright (C) 1998, 1999 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 2, or (at your option)
  6. 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, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Paul Eggert <[email protected]> */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <sys/types.h>
  19. #include <quotearg.h>
  20. #include <xalloc.h>
  21. #include <ctype.h>
  22. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  23. # define ISASCII(c) 1
  24. #else
  25. # define ISASCII(c) isascii (c)
  26. #endif
  27. #ifdef isgraph
  28. # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  29. #else
  30. # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  31. #endif
  32. #if HAVE_LIMITS_H
  33. # include <limits.h>
  34. #endif
  35. #ifndef CHAR_BIT
  36. # define CHAR_BIT 8
  37. #endif
  38. #ifndef UCHAR_MAX
  39. # define UCHAR_MAX ((unsigned char) -1)
  40. #endif
  41. #if HAVE_STDLIB_H
  42. # include <stdlib.h>
  43. #endif
  44. #if HAVE_STRING_H
  45. # include <string.h>
  46. #endif
  47. #define INT_BITS (sizeof (int) * CHAR_BIT)
  48. struct quoting_options
  49. {
  50. /* Basic quoting style. */
  51. enum quoting_style style;
  52. /* Quote the chararacters indicated by this bit vector even if the
  53. quoting style would not normally require them to be quoted. */
  54. int quote_these_too[((UCHAR_MAX + 1) / INT_BITS
  55. + ((UCHAR_MAX + 1) % INT_BITS != 0))];
  56. };
  57. /* Names of quoting styles. */
  58. char const *const quoting_style_args[] =
  59. {
  60. "literal",
  61. "shell",
  62. "shell-always",
  63. "c",
  64. "escape",
  65. 0
  66. };
  67. /* Correspondances to quoting style names. */
  68. enum quoting_style const quoting_style_vals[] =
  69. {
  70. literal_quoting_style,
  71. shell_quoting_style,
  72. shell_always_quoting_style,
  73. c_quoting_style,
  74. escape_quoting_style
  75. };
  76. /* The default quoting options. */
  77. static struct quoting_options default_quoting_options;
  78. /* Allocate a new set of quoting options, with contents initially identical
  79. to O if O is not null, or to the default if O is null.
  80. It is the caller's responsibility to free the result. */
  81. struct quoting_options *
  82. clone_quoting_options (struct quoting_options *o)
  83. {
  84. struct quoting_options *p
  85. = (struct quoting_options *) xmalloc (sizeof (struct quoting_options));
  86. *p = *(o ? o : &default_quoting_options);
  87. return p;
  88. }
  89. /* Get the value of O's quoting style. If O is null, use the default. */
  90. enum quoting_style
  91. get_quoting_style (struct quoting_options *o)
  92. {
  93. return (o ? o : &default_quoting_options)->style;
  94. }
  95. /* In O (or in the default if O is null),
  96. set the value of the quoting style to S. */
  97. void
  98. set_quoting_style (struct quoting_options *o, enum quoting_style s)
  99. {
  100. (o ? o : &default_quoting_options)->style = s;
  101. }
  102. /* In O (or in the default if O is null),
  103. set the value of the quoting options for character C to I.
  104. Return the old value. Currently, the only values defined for I are
  105. 0 (the default) and 1 (which means to quote the character even if
  106. it would not otherwise be quoted). */
  107. int
  108. set_char_quoting (struct quoting_options *o, char c, int i)
  109. {
  110. unsigned char uc = c;
  111. int *p = (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
  112. int shift = uc % INT_BITS;
  113. int r = (*p >> shift) & 1;
  114. *p ^= ((i & 1) ^ r) << shift;
  115. return r;
  116. }
  117. /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
  118. argument ARG (of size ARGSIZE), using O to control quoting.
  119. If O is null, use the default.
  120. Terminate the output with a null character, and return the written
  121. size of the output, not counting the terminating null.
  122. If BUFFERSIZE is too small to store the output string, return the
  123. value that would have been returned had BUFFERSIZE been large enough.
  124. If ARGSIZE is -1, use the string length of the argument for ARGSIZE. */
  125. size_t
  126. quotearg_buffer (char *buffer, size_t buffersize,
  127. char const *arg, size_t argsize,
  128. struct quoting_options const *o)
  129. {
  130. unsigned char c;
  131. size_t i;
  132. size_t len;
  133. int quote_mark;
  134. struct quoting_options const *p = o ? o : &default_quoting_options;
  135. enum quoting_style quoting_style = p->style;
  136. #define STORE(c) \
  137. do \
  138. { \
  139. if (len < buffersize) \
  140. buffer[len] = (c); \
  141. len++; \
  142. } \
  143. while (0)
  144. switch (quoting_style)
  145. {
  146. case shell_quoting_style:
  147. if (! (argsize == (size_t) -1 ? arg[0] == '\0' : argsize == 0))
  148. {
  149. switch (arg[0])
  150. {
  151. case '#': case '~':
  152. break;
  153. default:
  154. len = 0;
  155. for (i = 0; ; i++)
  156. {
  157. if (argsize == (size_t) -1 ? arg[i] == '\0' : i == argsize)
  158. goto done;
  159. c = arg[i];
  160. switch (c)
  161. {
  162. case '\t': case '\n': case ' ':
  163. case '!': /* special in csh */
  164. case '"': case '$': case '&': case '\'':
  165. case '(': case ')': case '*': case ';':
  166. case '<': case '>': case '?': case '[': case '\\':
  167. case '^': /* special in old /bin/sh, e.g. SunOS 4.1.4 */
  168. case '`': case '|':
  169. goto needs_quoting;
  170. }
  171. if (p->quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS)))
  172. goto needs_quoting;
  173. STORE (c);
  174. }
  175. needs_quoting:;
  176. break;
  177. }
  178. }
  179. /* Fall through. */
  180. case shell_always_quoting_style:
  181. quote_mark = '\'';
  182. break;
  183. case c_quoting_style:
  184. quote_mark = '"';
  185. break;
  186. default:
  187. quote_mark = 0;
  188. break;
  189. }
  190. len = 0;
  191. if (quote_mark)
  192. STORE (quote_mark);
  193. for (i = 0; ! (argsize == (size_t) -1 ? arg[i] == '\0' : i == argsize); i++)
  194. {
  195. c = arg[i];
  196. switch (quoting_style)
  197. {
  198. case literal_quoting_style:
  199. break;
  200. case shell_quoting_style:
  201. case shell_always_quoting_style:
  202. if (c == '\'')
  203. {
  204. STORE ('\'');
  205. STORE ('\\');
  206. STORE ('\'');
  207. }
  208. break;
  209. case c_quoting_style:
  210. case escape_quoting_style:
  211. switch (c)
  212. {
  213. case '?': /* Do not generate trigraphs. */
  214. case '\\': goto store_escape;
  215. /* Not all C compilers know what \a means. */
  216. case 7 : c = 'a'; goto store_escape;
  217. case '\b': c = 'b'; goto store_escape;
  218. case '\f': c = 'f'; goto store_escape;
  219. case '\n': c = 'n'; goto store_escape;
  220. case '\r': c = 'r'; goto store_escape;
  221. case '\t': c = 't'; goto store_escape;
  222. case '\v': c = 'v'; goto store_escape;
  223. case '"':
  224. if (quoting_style == c_quoting_style)
  225. goto store_escape;
  226. break;
  227. default:
  228. if (!ISGRAPH (c))
  229. {
  230. STORE ('\\');
  231. STORE ('0' + (c >> 6));
  232. STORE ('0' + ((c >> 3) & 7));
  233. c = '0' + (c & 7);
  234. goto store_c;
  235. }
  236. break;
  237. }
  238. if (! (p->quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS))))
  239. goto store_c;
  240. store_escape:
  241. STORE ('\\');
  242. }
  243. store_c:
  244. STORE (c);
  245. }
  246. if (quote_mark)
  247. STORE (quote_mark);
  248. done:
  249. if (len < buffersize)
  250. buffer[len] = '\0';
  251. return len;
  252. }
  253. /* Use storage slot N to return a quoted version of the string ARG.
  254. OPTIONS specifies the quoting options.
  255. The returned value points to static storage that can be
  256. reused by the next call to this function with the same value of N.
  257. N must be nonnegative. N is deliberately declared with type `int'
  258. to allow for future extensions (using negative values). */
  259. static char *
  260. quotearg_n_options (int n, char const *arg,
  261. struct quoting_options const *options)
  262. {
  263. static unsigned int nslots;
  264. static struct slotvec
  265. {
  266. size_t size;
  267. char *val;
  268. } *slotvec;
  269. if (nslots <= n)
  270. {
  271. int n1 = n + 1;
  272. size_t s = n1 * sizeof (struct slotvec);
  273. if (! (0 < n1 && n1 == s / sizeof (struct slotvec)))
  274. abort ();
  275. slotvec = (struct slotvec *) xrealloc (slotvec, s);
  276. memset (slotvec + nslots, 0, (n1 - nslots) * sizeof (struct slotvec));
  277. nslots = n;
  278. }
  279. {
  280. size_t size = slotvec[n].size;
  281. char *val = slotvec[n].val;
  282. size_t qsize = quotearg_buffer (val, size, arg, (size_t) -1, options);
  283. if (size <= qsize)
  284. {
  285. slotvec[n].size = size = qsize + 1;
  286. slotvec[n].val = val = xrealloc (val, size);
  287. quotearg_buffer (val, size, arg, (size_t) -1, options);
  288. }
  289. return val;
  290. }
  291. }
  292. char *
  293. quotearg_n (unsigned int n, char const *arg)
  294. {
  295. return quotearg_n_options (n, arg, &default_quoting_options);
  296. }
  297. char *
  298. quotearg (char const *arg)
  299. {
  300. return quotearg_n (0, arg);
  301. }
  302. char *
  303. quotearg_char (char const *arg, char ch)
  304. {
  305. struct quoting_options options;
  306. options = default_quoting_options;
  307. set_char_quoting (&options, ch, 1);
  308. return quotearg_n_options (0, arg, &options);
  309. }
  310. char *
  311. quotearg_colon (char const *arg)
  312. {
  313. return quotearg_char (arg, ':');
  314. }