aboutsummaryrefslogtreecommitdiff
path: root/src/wordsplit.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2019-07-10 19:50:55 +0300
committerSergey Poznyakoff <gray@gnu.org>2019-07-10 19:50:55 +0300
commit2834b9911ab3154fe979debe766eed8db6ed8aad (patch)
treefb6cbf758ec0c15713781cc03333ec59bad26966 /src/wordsplit.c
parentffe7c11031760922c8265b62eb1b6c83a8647f73 (diff)
downloadcflow-2834b9911ab3154fe979debe766eed8db6ed8aad.tar.gz
cflow-2834b9911ab3154fe979debe766eed8db6ed8aad.tar.bz2
Use wordsplit from submodule
Diffstat (limited to 'src/wordsplit.c')
-rw-r--r--src/wordsplit.c2546
1 files changed, 0 insertions, 2546 deletions
diff --git a/src/wordsplit.c b/src/wordsplit.c
deleted file mode 100644
index bad59b1..0000000
--- a/src/wordsplit.c
+++ /dev/null
@@ -1,2546 +0,0 @@
1/* wordsplit - a word splitter
2 Copyright (C) 2009-2018 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#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
21#include <errno.h>
22#include <ctype.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdio.h>
27#include <stdarg.h>
28#include <pwd.h>
29#include <glob.h>
30
31#if ENABLE_NLS
32# include <gettext.h>
33#else
34# define gettext(msgid) msgid
35#endif
36#define _(msgid) gettext (msgid)
37#define N_(msgid) msgid
38
39#include <wordsplit.h>
40
41#define ISWS(c) ((c)==' '||(c)=='\t'||(c)=='\n')
42#define ISDELIM(ws,c) \
43 (strchr ((ws)->ws_delim, (c)) != NULL)
44#define ISPUNCT(c) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",(c))!=NULL)
45#define ISUPPER(c) ('A' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'Z')
46#define ISLOWER(c) ('a' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'z')
47#define ISALPHA(c) (ISUPPER(c) || ISLOWER(c))
48#define ISDIGIT(c) ('0' <= ((unsigned) (c)) && ((unsigned) (c)) <= '9')
49#define ISXDIGIT(c) (strchr("abcdefABCDEF", c)!=NULL)
50#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
51#define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127)
52
53#define ISVARBEG(c) (ISALPHA(c) || c == '_')
54#define ISVARCHR(c) (ISALNUM(c) || c == '_')
55
56#define WSP_RETURN_DELIMS(wsp) \
57 ((wsp)->ws_flags & WRDSF_RETURN_DELIMS || ((wsp)->ws_options & WRDSO_MAXWORDS))
58
59#define ALLOC_INIT 128
60#define ALLOC_INCR 128
61
62static void
63_wsplt_alloc_die (struct wordsplit *wsp)
64{
65 wsp->ws_error ("%s", _("memory exhausted"));
66 abort ();
67}
68
69static void
70_wsplt_error (const char *fmt, ...)
71{
72 va_list ap;
73
74 va_start (ap, fmt);
75 vfprintf (stderr, fmt, ap);
76 va_end (ap);
77 fputc ('\n', stderr);
78}
79
80static void wordsplit_free_nodes (struct wordsplit *);
81
82static int
83_wsplt_seterr (struct wordsplit *wsp, int ec)
84{
85 wsp->ws_errno = ec;
86 if (wsp->ws_flags & WRDSF_SHOWERR)
87 wordsplit_perror (wsp);
88 return ec;
89}
90
91static int
92_wsplt_nomem (struct wordsplit *wsp)
93{
94 errno = ENOMEM;
95 wsp->ws_errno = WRDSE_NOSPACE;
96 if (wsp->ws_flags & WRDSF_ENOMEMABRT)
97 wsp->ws_alloc_die (wsp);
98 if (wsp->ws_flags & WRDSF_SHOWERR)
99 wordsplit_perror (wsp);
100 if (!(wsp->ws_flags & WRDSF_REUSE))
101 wordsplit_free (wsp);
102 wordsplit_free_nodes (wsp);
103 return wsp->ws_errno;
104}
105
106static int wordsplit_run (const char *command, size_t length,
107 struct wordsplit *wsp,
108 int flags, int lvl);
109
110static int wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
111 int flags);
112static int wordsplit_process_list (struct wordsplit *wsp, size_t start);
113static int wordsplit_finish (struct wordsplit *wsp);
114
115static int
116_wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss,
117 char const *str, int len,
118 int flags, int finalize)
119{
120 int rc;
121
122 wss->ws_delim = wsp->ws_delim;
123 wss->ws_debug = wsp->ws_debug;
124 wss->ws_error = wsp->ws_error;
125 wss->ws_alloc_die = wsp->ws_alloc_die;
126
127 if (!(flags & WRDSF_NOVAR))
128 {
129 wss->ws_env = wsp->ws_env;
130 wss->ws_getvar = wsp->ws_getvar;
131 flags |= wsp->ws_flags & (WRDSF_ENV | WRDSF_ENV_KV | WRDSF_GETVAR);
132 }
133 if (!(flags & WRDSF_NOCMD))
134 {
135 wss->ws_command = wsp->ws_command;
136 }
137
138 if ((flags & (WRDSF_NOVAR|WRDSF_NOCMD)) != (WRDSF_NOVAR|WRDSF_NOCMD))
139 {
140 wss->ws_closure = wsp->ws_closure;
141 flags |= wsp->ws_flags & WRDSF_CLOSURE;
142 }
143
144 wss->ws_options = wsp->ws_options;
145
146 flags |= WRDSF_DELIM
147 | WRDSF_ALLOC_DIE
148 | WRDSF_ERROR
149 | WRDSF_DEBUG
150 | (wsp->ws_flags & (WRDSF_SHOWDBG | WRDSF_SHOWERR | WRDSF_OPTIONS));
151
152 rc = wordsplit_init (wss, str, len, flags);
153 if (rc)
154 return rc;
155 wss->ws_lvl = wsp->ws_lvl + 1;
156 rc = wordsplit_process_list (wss, 0);
157 if (rc)
158 {
159 wordsplit_free_nodes (wss);
160 return rc;
161 }
162 if (finalize)
163 {
164 rc = wordsplit_finish (wss);
165 wordsplit_free_nodes (wss);
166 }
167 return rc;
168}
169
170static void
171_wsplt_seterr_sub (struct wordsplit *wsp, struct wordsplit *wss)
172{
173 if (wsp->ws_errno == WRDSE_USERERR)
174 free (wsp->ws_usererr);
175 wsp->ws_errno = wss->ws_errno;
176 if (wss->ws_errno == WRDSE_USERERR)
177 {
178 wsp->ws_usererr = wss->ws_usererr;
179 wss->ws_errno = WRDSE_EOF;
180 wss->ws_usererr = NULL;
181 }
182}
183
184static void
185wordsplit_init0 (struct wordsplit *wsp)
186{
187 if (wsp->ws_flags & WRDSF_REUSE)
188 {
189 if (!(wsp->ws_flags & WRDSF_APPEND))
190 wordsplit_free_words (wsp);
191 wordsplit_clearerr (wsp);
192 }
193 else
194 {
195 wsp->ws_wordv = NULL;
196 wsp->ws_wordc = 0;
197 wsp->ws_wordn = 0;
198 }
199
200 wsp->ws_errno = 0;
201}
202
203char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";
204
205static int
206wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
207 int flags)
208{
209 wsp->ws_flags = flags;
210
211 if (!(wsp->ws_flags & WRDSF_ALLOC_DIE))
212 wsp->ws_alloc_die = _wsplt_alloc_die;
213 if (!(wsp->ws_flags & WRDSF_ERROR))
214 wsp->ws_error = _wsplt_error;
215
216 if (!(wsp->ws_flags & WRDSF_NOVAR))
217 {
218 /* These will be initialized on first variable assignment */
219 wsp->ws_envidx = wsp->ws_envsiz = 0;
220 wsp->ws_envbuf = NULL;
221 }
222
223 if (!(wsp->ws_flags & WRDSF_NOCMD))
224 {
225 if (!wsp->ws_command)
226 {
227 _wsplt_seterr (wsp, WRDSE_USAGE);
228 errno = EINVAL;
229 return wsp->ws_errno;
230 }
231 }
232
233 if (wsp->ws_flags & WRDSF_SHOWDBG)
234 {
235 if (!(wsp->ws_flags & WRDSF_DEBUG))
236 {
237 if (wsp->ws_flags & WRDSF_ERROR)
238 wsp->ws_debug = wsp->ws_error;
239 else if (wsp->ws_flags & WRDSF_SHOWERR)
240 wsp->ws_debug = _wsplt_error;
241 else
242 wsp->ws_flags &= ~WRDSF_SHOWDBG;
243 }
244 }