4
0

wordsplit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* wordsplit - a word splitter
  2. Copyright (C) 2009-2018 Sergey Poznyakoff
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3 of the License, or (at your
  6. 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 along
  12. with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef __WORDSPLIT_H
  14. #define __WORDSPLIT_H
  15. #include <stddef.h>
  16. #include <attribute.h>
  17. typedef struct wordsplit wordsplit_t;
  18. /* Structure used to direct the splitting. Members marked with [Input]
  19. can be defined before calling wordsplit(), those marked with [Output]
  20. provide return values when the function returns. If neither mark is
  21. used, the member is internal and must not be used by the caller.
  22. In the comments below, the identifiers in parentheses indicate bits that
  23. must be set (or unset, if starting with !) in ws_flags (if starting with
  24. WRDSF_) or ws_options (if starting with WRDSO_) to initialize or use the
  25. given member.
  26. If not redefined explicitly, most of them are set to some reasonable
  27. default value upon entry to wordsplit(). */
  28. struct wordsplit
  29. {
  30. size_t ws_wordc; /* [Output] Number of words in ws_wordv. */
  31. char **ws_wordv; /* [Output] Array of parsed out words. */
  32. size_t ws_offs; /* [Input] (WRDSF_DOOFFS) Number of initial
  33. elements in ws_wordv to fill with NULLs. */
  34. size_t ws_wordn; /* Number of elements ws_wordv can accommodate. */
  35. unsigned ws_flags; /* [Input] Flags passed to wordsplit. */
  36. unsigned ws_options; /* [Input] (WRDSF_OPTIONS)
  37. Additional options. */
  38. size_t ws_maxwords; /* [Input] (WRDSO_MAXWORDS) Return at most that
  39. many words */
  40. size_t ws_wordi; /* [Output] (WRDSF_INCREMENTAL) Total number of
  41. words returned so far */
  42. const char *ws_delim; /* [Input] (WRDSF_DELIM) Word delimiters. */
  43. const char *ws_comment; /* [Input] (WRDSF_COMMENT) Comment characters. */
  44. const char *ws_escape[2]; /* [Input] (WRDSF_ESCAPE) Characters to be escaped
  45. with backslash. */
  46. void (*ws_alloc_die) (wordsplit_t *wsp);
  47. /* [Input] (WRDSF_ALLOC_DIE) Function called when
  48. out of memory. Must not return. */
  49. void (*ws_error) (const char *, ...)
  50. ATTRIBUTE_FORMAT ((printf, 1, 2));
  51. /* [Input] (WRDSF_ERROR) Function used for error
  52. reporting */
  53. void (*ws_debug) (const char *, ...)
  54. ATTRIBUTE_FORMAT ((printf, 1, 2));
  55. /* [Input] (WRDSF_DEBUG) Function used for debug
  56. output. */
  57. const char **ws_env; /* [Input] (WRDSF_ENV, !WRDSF_NOVAR) Array of
  58. environment variables. */
  59. char **ws_envbuf;
  60. size_t ws_envidx;
  61. size_t ws_envsiz;
  62. int (*ws_getvar) (char **ret, const char *var, size_t len, void *clos);
  63. /* [Input] (WRDSF_GETVAR, !WRDSF_NOVAR) Looks up
  64. the name VAR (LEN bytes long) in the table of
  65. variables and if found returns in memory
  66. location pointed to by RET the value of that
  67. variable. Returns WRDSE_OK (0) on success,
  68. and an error code (see WRDSE_* defines below)
  69. on error. User-specific errors can be returned
  70. by storing the error diagnostic string in RET
  71. and returning WRDSE_USERERR.
  72. Whatever is stored in RET, it must be allocated
  73. using malloc(3). */
  74. void *ws_closure; /* [Input] (WRDSF_CLOSURE) Passed as the CLOS
  75. argument to ws_getvar and ws_command. */
  76. int (*ws_command) (char **ret, const char *cmd, size_t len, char **argv,
  77. void *clos);
  78. /* [Input] (!WRDSF_NOCMD) Returns in the memory
  79. location pointed to by RET the expansion of
  80. the command CMD (LEN bytes long). If WRDSO_ARGV
  81. option is set, ARGV contains CMD split out to
  82. words. Otherwise ARGV is NULL.
  83. See ws_getvar for a discussion of possible
  84. return values. */
  85. const char *ws_input; /* Input string (the S argument to wordsplit. */
  86. size_t ws_len; /* Length of ws_input. */
  87. size_t ws_endp; /* Points past the last processed byte in
  88. ws_input. */
  89. int ws_errno; /* [Output] Error code, if an error occurred. */
  90. char *ws_usererr; /* Points to textual description of
  91. the error, if ws_errno is WRDSE_USERERR. Must
  92. be allocated with malloc(3). */
  93. struct wordsplit_node *ws_head, *ws_tail;
  94. /* Doubly-linked list of parsed out nodes. */
  95. int ws_lvl; /* Invocation nesting level. */
  96. };
  97. /* Initial size for ws_env, if allocated automatically */
  98. #define WORDSPLIT_ENV_INIT 16
  99. /* Wordsplit flags. */
  100. /* Append the words found to the array resulting from a previous
  101. call. */
  102. #define WRDSF_APPEND 0x00000001
  103. /* Insert ws_offs initial NULLs in the array ws_wordv.
  104. (These are not counted in the returned ws_wordc.) */
  105. #define WRDSF_DOOFFS 0x00000002
  106. /* Don't do command substitution. */
  107. #define WRDSF_NOCMD 0x00000004
  108. /* The parameter p resulted from a previous call to
  109. wordsplit(), and wordsplit_free() was not called. Reuse the
  110. allocated storage. */
  111. #define WRDSF_REUSE 0x00000008
  112. /* Print errors */
  113. #define WRDSF_SHOWERR 0x00000010
  114. /* Consider it an error if an undefined variable is expanded. */
  115. #define WRDSF_UNDEF 0x00000020
  116. /* Don't do variable expansion. */
  117. #define WRDSF_NOVAR 0x00000040
  118. /* Abort on ENOMEM error */
  119. #define WRDSF_ENOMEMABRT 0x00000080
  120. /* Trim off any leading and trailind whitespace */
  121. #define WRDSF_WS 0x00000100
  122. /* Handle single quotes */
  123. #define WRDSF_SQUOTE 0x00000200
  124. /* Handle double quotes */
  125. #define WRDSF_DQUOTE 0x00000400
  126. /* Handle single and double quotes */
  127. #define WRDSF_QUOTE (WRDSF_SQUOTE|WRDSF_DQUOTE)
  128. /* Replace each input sequence of repeated delimiters with a single
  129. delimiter */
  130. #define WRDSF_SQUEEZE_DELIMS 0x00000800
  131. /* Return delimiters */
  132. #define WRDSF_RETURN_DELIMS 0x00001000
  133. /* Treat sed expressions as words */
  134. #define WRDSF_SED_EXPR 0x00002000
  135. /* ws_delim field is initialized */
  136. #define WRDSF_DELIM 0x00004000
  137. /* ws_comment field is initialized */
  138. #define WRDSF_COMMENT 0x00008000
  139. /* ws_alloc_die field is initialized */
  140. #define WRDSF_ALLOC_DIE 0x00010000
  141. /* ws_error field is initialized */
  142. #define WRDSF_ERROR 0x00020000
  143. /* ws_debug field is initialized */
  144. #define WRDSF_DEBUG 0x00040000
  145. /* ws_env field is initialized */
  146. #define WRDSF_ENV 0x00080000
  147. /* ws_getvar field is initialized */
  148. #define WRDSF_GETVAR 0x00100000
  149. /* enable debugging */
  150. #define WRDSF_SHOWDBG 0x00200000
  151. /* Don't split input into words. Useful for side effects. */
  152. #define WRDSF_NOSPLIT 0x00400000
  153. /* Keep undefined variables in place, instead of expanding them to
  154. empty strings. */
  155. #define WRDSF_KEEPUNDEF 0x00800000
  156. /* Warn about undefined variables */
  157. #define WRDSF_WARNUNDEF 0x01000000
  158. /* Handle C escapes */
  159. #define WRDSF_CESCAPES 0x02000000
  160. /* ws_closure is set */
  161. #define WRDSF_CLOSURE 0x04000000
  162. /* ws_env is a Key/Value environment, i.e. the value of a variable is
  163. stored in the element that follows its name. */
  164. #define WRDSF_ENV_KV 0x08000000
  165. /* ws_escape is set */
  166. #define WRDSF_ESCAPE 0x10000000
  167. /* Incremental mode */
  168. #define WRDSF_INCREMENTAL 0x20000000
  169. /* Perform pathname and tilde expansion */
  170. #define WRDSF_PATHEXPAND 0x40000000
  171. /* ws_options is initialized */
  172. #define WRDSF_OPTIONS 0x80000000
  173. #define WRDSF_DEFFLAGS \
  174. (WRDSF_NOVAR | WRDSF_NOCMD | \
  175. WRDSF_QUOTE | WRDSF_SQUEEZE_DELIMS | WRDSF_CESCAPES)
  176. /* Remove the word that produces empty string after path expansion */
  177. #define WRDSO_NULLGLOB 0x00000001
  178. /* Print error message if path expansion produces empty string */
  179. #define WRDSO_FAILGLOB 0x00000002
  180. /* Allow a leading period to be matched by metacharacters. */
  181. #define WRDSO_DOTGLOB 0x00000004
  182. /* ws_command needs argv parameter */
  183. #define WRDSO_ARGV 0x00000008
  184. /* Keep backslash in unrecognized escape sequences in words */
  185. #define WRDSO_BSKEEP_WORD 0x00000010
  186. /* Handle octal escapes in words */
  187. #define WRDSO_OESC_WORD 0x00000020
  188. /* Handle hex escapes in words */
  189. #define WRDSO_XESC_WORD 0x00000040
  190. /* ws_maxwords field is initialized */
  191. #define WRDSO_MAXWORDS 0x00000080
  192. /* Keep backslash in unrecognized escape sequences in quoted strings */
  193. #define WRDSO_BSKEEP_QUOTE 0x00000100
  194. /* Handle octal escapes in quoted strings */
  195. #define WRDSO_OESC_QUOTE 0x00000200
  196. /* Handle hex escapes in quoted strings */
  197. #define WRDSO_XESC_QUOTE 0x00000400
  198. #define WRDSO_BSKEEP WRDSO_BSKEEP_WORD
  199. #define WRDSO_OESC WRDSO_OESC_WORD
  200. #define WRDSO_XESC WRDSO_XESC_WORD
  201. /* Indices into ws_escape */
  202. #define WRDSX_WORD 0
  203. #define WRDSX_QUOTE 1
  204. /* Set escape option F in WS for words (Q==0) or quoted strings (Q==1) */
  205. #define WRDSO_ESC_SET(ws,q,f) ((ws)->ws_options |= ((f) << 4*(q)))
  206. /* Test WS for escape option F for words (Q==0) or quoted strings (Q==1) */
  207. #define WRDSO_ESC_TEST(ws,q,f) ((ws)->ws_options & ((f) << 4*(q)))
  208. #define WRDSE_OK 0
  209. #define WRDSE_EOF WRDSE_OK
  210. #define WRDSE_QUOTE 1
  211. #define WRDSE_NOSPACE 2
  212. #define WRDSE_USAGE 3
  213. #define WRDSE_CBRACE 4
  214. #define WRDSE_UNDEF 5
  215. #define WRDSE_NOINPUT 6
  216. #define WRDSE_PAREN 7
  217. #define WRDSE_GLOBERR 8
  218. #define WRDSE_USERERR 9
  219. int wordsplit (const char *s, wordsplit_t *ws, unsigned flags);
  220. int wordsplit_len (const char *s, size_t len, wordsplit_t *ws, unsigned flags);
  221. void wordsplit_free (wordsplit_t *ws);
  222. void wordsplit_free_words (wordsplit_t *ws);
  223. void wordsplit_free_envbuf (wordsplit_t *ws);
  224. int wordsplit_get_words (wordsplit_t *ws, size_t *wordc, char ***wordv);
  225. int wordsplit_append (wordsplit_t *wsp, int argc, char **argv);
  226. int wordsplit_c_unquote_char (int c);
  227. int wordsplit_c_quote_char (int c);
  228. size_t wordsplit_c_quoted_length (const char *str, int quote_hex, int *quote);
  229. void wordsplit_c_quote_copy (char *dst, const char *src, int quote_hex);
  230. void wordsplit_perror (wordsplit_t *ws);
  231. const char *wordsplit_strerror (wordsplit_t *ws);
  232. void wordsplit_clearerr (wordsplit_t *ws);
  233. #endif