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