wordsplit.h 5.5 KB

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