wordsplit.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* wordsplit - a word splitter
  2. Copyright (C) 2009-2014, 2016 Free Software Foundation, Inc.
  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. Written by Sergey Poznyakoff
  14. */
  15. #ifndef __WORDSPLIT_H
  16. #define __WORDSPLIT_H
  17. #include <stddef.h>
  18. #if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
  19. # define __WORDSPLIT_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
  20. #else
  21. # define __WORDSPLIT_ATTRIBUTE_FORMAT(spec) /* empty */
  22. #endif
  23. struct wordsplit
  24. {
  25. size_t ws_wordc;
  26. char **ws_wordv;
  27. size_t ws_offs;
  28. size_t ws_wordn;
  29. int ws_flags;
  30. const char *ws_delim;
  31. const char *ws_comment;
  32. const char *ws_escape;
  33. void (*ws_alloc_die) (struct wordsplit * wsp);
  34. void (*ws_error) (const char *, ...)
  35. __WORDSPLIT_ATTRIBUTE_FORMAT ((__printf__, 1, 2));
  36. void (*ws_debug) (const char *, ...)
  37. __WORDSPLIT_ATTRIBUTE_FORMAT ((__printf__, 1, 2));
  38. const char **ws_env;
  39. const char *(*ws_getvar) (const char *, size_t, void *);
  40. void *ws_closure;
  41. const char *ws_input;
  42. size_t ws_len;
  43. size_t ws_endp;
  44. int ws_errno;
  45. struct wordsplit_node *ws_head, *ws_tail;
  46. };
  47. /* Wordsplit flags. Only 2 bits of a 32-bit word remain unused.
  48. It is getting crowded... */
  49. /* Append the words found to the array resulting from a previous
  50. call. */
  51. #define WRDSF_APPEND 0x00000001
  52. /* Insert we_offs initial NULLs in the array ws_wordv.
  53. (These are not counted in the returned ws_wordc.) */
  54. #define WRDSF_DOOFFS 0x00000002
  55. /* Don't do command substitution. Reserved for future use. */
  56. #define WRDSF_NOCMD 0x00000004
  57. /* The parameter p resulted from a previous call to
  58. wordsplit(), and wordsplit_free() was not called. Reuse the
  59. allocated storage. */
  60. #define WRDSF_REUSE 0x00000008
  61. /* Print errors */
  62. #define WRDSF_SHOWERR 0x00000010
  63. /* Consider it an error if an undefined shell variable
  64. is expanded. */
  65. #define WRDSF_UNDEF 0x00000020
  66. /* Don't do variable expansion. */
  67. #define WRDSF_NOVAR 0x00000040
  68. /* Abort on ENOMEM error */
  69. #define WRDSF_ENOMEMABRT 0x00000080
  70. /* Trim off any leading and trailind whitespace */
  71. #define WRDSF_WS 0x00000100
  72. /* Handle single quotes */
  73. #define WRDSF_SQUOTE 0x00000200
  74. /* Handle double quotes */
  75. #define WRDSF_DQUOTE 0x00000400
  76. /* Handle quotes and escape directives */
  77. #define WRDSF_QUOTE (WRDSF_SQUOTE|WRDSF_DQUOTE)
  78. /* Replace each input sequence of repeated delimiters with a single
  79. delimiter */
  80. #define WRDSF_SQUEEZE_DELIMS 0x00000800
  81. /* Return delimiters */
  82. #define WRDSF_RETURN_DELIMS 0x00001000
  83. /* Treat sed expressions as words */
  84. #define WRDSF_SED_EXPR 0x00002000
  85. /* ws_delim field is initialized */
  86. #define WRDSF_DELIM 0x00004000
  87. /* ws_comment field is initialized */
  88. #define WRDSF_COMMENT 0x00008000
  89. /* ws_alloc_die field is initialized */
  90. #define WRDSF_ALLOC_DIE 0x00010000
  91. /* ws_error field is initialized */
  92. #define WRDSF_ERROR 0x00020000
  93. /* ws_debug field is initialized */
  94. #define WRDSF_DEBUG 0x00040000
  95. /* ws_env field is initialized */
  96. #define WRDSF_ENV 0x00080000
  97. /* ws_getvar field is initialized */
  98. #define WRDSF_GETVAR 0x00100000
  99. /* enable debugging */
  100. #define WRDSF_SHOWDBG 0x00200000
  101. /* Don't split input into words. Useful for side effects. */
  102. #define WRDSF_NOSPLIT 0x00400000
  103. /* Keep undefined variables in place, instead of expanding them to
  104. empty string */
  105. #define WRDSF_KEEPUNDEF 0x00800000
  106. /* Warn about undefined variables */
  107. #define WRDSF_WARNUNDEF 0x01000000
  108. /* Handle C escapes */
  109. #define WRDSF_CESCAPES 0x02000000
  110. /* ws_closure is set */
  111. #define WRDSF_CLOSURE 0x04000000
  112. /* ws_env is a Key/Value environment, i.e. the value of a variable is
  113. stored in the element that follows its name. */
  114. #define WRDSF_ENV_KV 0x08000000
  115. /* ws_escape is set */
  116. #define WRDSF_ESCAPE 0x10000000
  117. /* Incremental mode */
  118. #define WRDSF_INCREMENTAL 0x20000000
  119. #define WRDSF_DEFFLAGS \
  120. (WRDSF_NOVAR | WRDSF_NOCMD | \
  121. WRDSF_QUOTE | WRDSF_SQUEEZE_DELIMS | WRDSF_CESCAPES)
  122. #define WRDSE_EOF 0
  123. #define WRDSE_QUOTE 1
  124. #define WRDSE_NOSPACE 2
  125. #define WRDSE_NOSUPP 3
  126. #define WRDSE_USAGE 4
  127. #define WRDSE_CBRACE 5
  128. #define WRDSE_UNDEF 6
  129. #define WRDSE_NOINPUT 7
  130. int wordsplit (const char *s, struct wordsplit *p, int flags);
  131. int wordsplit_len (const char *s, size_t len,
  132. struct wordsplit *p, int flags);
  133. void wordsplit_free (struct wordsplit *p);
  134. void wordsplit_free_words (struct wordsplit *ws);
  135. int wordsplit_c_unquote_char (int c);
  136. int wordsplit_c_quote_char (int c);
  137. size_t wordsplit_c_quoted_length (const char *str, int quote_hex,
  138. int *quote);
  139. void wordsplit_general_unquote_copy (char *dst, const char *src, size_t n,
  140. const char *escapable);
  141. void wordsplit_sh_unquote_copy (char *dst, const char *src, size_t n);
  142. void wordsplit_c_unquote_copy (char *dst, const char *src, size_t n);
  143. void wordsplit_c_quote_copy (char *dst, const char *src, int quote_hex);
  144. void wordsplit_perror (struct wordsplit *ws);
  145. const char *wordsplit_strerror (struct wordsplit *ws);
  146. #endif