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