wsp.c
5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mailutils/wordsplit.h>
#include <mailutils/kwd.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/cstr.h>
extern char **environ;
struct mu_kwd bool_keytab[] = {
{ "append", MU_WRDSF_APPEND },
/*{ "reuse", MU_WRDSF_REUSE },*/
{ "undef", MU_WRDSF_UNDEF },
{ "novar", MU_WRDSF_NOVAR },
{ "ws", MU_WRDSF_WS },
{ "quote", MU_WRDSF_QUOTE },
{ "squeeze_delims", MU_WRDSF_SQUEEZE_DELIMS },
{ "return_delims", MU_WRDSF_RETURN_DELIMS },
{ "sed", MU_WRDSF_SED_EXPR },
{ "debug", MU_WRDSF_SHOWDBG },
{ "nosplit", MU_WRDSF_NOSPLIT },
{ "keepundef", MU_WRDSF_KEEPUNDEF },
{ "warnundef", MU_WRDSF_WARNUNDEF },
{ "cescapes", MU_WRDSF_CESCAPES },
{ "default", MU_WRDSF_DEFFLAGS },
{ NULL, 0 }
};
struct mu_kwd string_keytab[] = {
{ "delim", MU_WRDSF_DELIM },
{ "comment", MU_WRDSF_COMMENT },
{ NULL, 0 }
};
static void
help ()
{
size_t i;
printf ("usage: wsp [options]\n");
printf ("options are:\n");
printf (" [-]trimnl\n");
printf (" [-]plaintext\n");
putchar ('\n');
for (i = 0; bool_keytab[i].name; i++)
printf (" [-]%s\n", bool_keytab[i].name);
putchar ('\n');
for (i = 0; string_keytab[i].name; i++)
{
printf (" -%s\n", bool_keytab[i].name);
printf (" %s ARG\n", bool_keytab[i].name);
}
putchar ('\n');
printf (" -dooffs\n");
printf (" dooffs COUNT ARGS...\n");
exit (0);
}
void
print_qword (const char *word, int plaintext)
{
static char *qbuf = NULL;
static size_t qlen = 0;
int quote;
size_t size = mu_wordsplit_c_quoted_length (word, 0, "e);
if (plaintext)
{
printf ("%s", word);
return;
}
if (*word == 0)
quote = 1;
if (size >= qlen)
{
qlen = size + 1;
qbuf = realloc (qbuf, qlen);
if (!qbuf)
{
mu_error ("not enough memory");
abort ();
}
}
mu_wordsplit_c_quote_copy (qbuf, word, 0);
qbuf[size] = 0;
if (quote)
printf ("\"%s\"", qbuf);
else
printf ("%s", qbuf);
}
int
main (int argc, char **argv)
{
char buf[1024];
int i, offarg = 0;
int trimnl_option = 0;
int plaintext_option = 0;
int wsflags = (MU_WRDSF_DEFFLAGS & ~MU_WRDSF_NOVAR) |
MU_WRDSF_ENOMEMABRT |
MU_WRDSF_ENV | MU_WRDSF_SHOWERR;
struct mu_wordsplit ws;
for (i = 1; i < argc; i++)
{
char *opt = argv[i];
int negate;
int flag;
if (opt[0] == '-')
{
negate = 1;
opt++;
}
else if (opt[0] == '+')
{
negate = 0;
opt++;
}
else
negate = 0;
if (strcmp (opt, "h") == 0 ||
strcmp (opt, "help") == 0 ||
strcmp (opt, "-help") == 0)
{
help ();
}
if (strcmp (opt, "trimnl") == 0)
{
trimnl_option = !negate;
continue;
}
if (strcmp (opt, "plaintext") == 0)
{
plaintext_option = !negate;
continue;
}
if (mu_kwd_xlat_name (bool_keytab, opt, &flag) == 0)
{
if (negate)
wsflags &= ~flag;
else
wsflags |= flag;
continue;
}
if (mu_kwd_xlat_name (string_keytab, opt, &flag) == 0)
{
if (negate)
wsflags &= ~flag;
else
{
i++;
if (i == argc)
{
mu_error ("%s missing argument", opt);
exit (1);
}
switch (flag)
{
case MU_WRDSF_DELIM:
ws.ws_delim = argv[i];
break;
case MU_WRDSF_COMMENT:
ws.ws_comment = argv[i];
break;
}
wsflags |= flag;
}
continue;
}
if (strcmp (opt, "dooffs") == 0)
{
if (negate)
wsflags &= ~MU_WRDSF_DOOFFS;
else
{
char *p;
i++;
if (i == argc)
{
mu_error ("%s missing arguments", opt);
exit (1);
}
ws.ws_offs = strtoul (argv[i], &p, 10);
if (*p)
{
mu_error ("invalid number: %s", argv[i]);
exit (1);
}
i++;
if (i + ws.ws_offs > argc)
{
mu_error ("%s: not enough arguments", opt);
exit (1);
}
offarg = i;
i += ws.ws_offs - 1;
wsflags |= MU_WRDSF_DOOFFS;
}
continue;
}
mu_error ("%s: unrecognized argument", opt);
exit (1);
}
ws.ws_env = (const char **) environ;
while (fgets (buf, sizeof (buf), stdin))
{
int rc;
size_t i;
if (trimnl_option)
mu_rtrim_cset (buf, "\n");
rc = mu_wordsplit (buf, &ws, wsflags);
if (rc)
{
if (!(wsflags & MU_WRDSF_SHOWERR))
mu_wordsplit_perror (&ws);
continue;
}
if (offarg)
{
for (i = 0; i < ws.ws_offs; i++)
ws.ws_wordv[i] = argv[offarg + i];
offarg = 0;
}
wsflags |= MU_WRDSF_REUSE;
printf ("NF: %lu", (unsigned long) ws.ws_wordc);
if (wsflags & MU_WRDSF_DOOFFS)
printf (" (%lu)", (unsigned long) ws.ws_offs);
putchar ('\n');
for (i = 0; i < ws.ws_offs; i++)
{
printf ("(%lu): ", (unsigned long) i);
print_qword (ws.ws_wordv[i], plaintext_option);
putchar ('\n');
}
for (; i < ws.ws_offs + ws.ws_wordc; i++)
{
printf ("%lu: ", (unsigned long) i);
print_qword (ws.ws_wordv[i], plaintext_option);
putchar ('\n');
}
}
return 0;
}