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