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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* Copyright (C) 2016 Andre Noll <maan@tuebingen.mpg.de>
*
* Licensed under the LGPL v3, see https://www.gnu.org/licenses/lgpl-3.0.html
*/
/* We don't want symbols to clash with those of other flex users. */
%option prefix="lls_yy"
/* Generate a scanner which is safe to use in multithreaded programs. */
%option reentrant
/* To maintain state in a reentrant way. */
%option extra-type="struct lls_yy_config_file_extra *"
%option stack
%option never-interactive
%option yylineno
%x SC_ARG
%s SC_SCANNING
IDENTIFIER [a-zA-Z]+[a-zA-Z0-9_-]*
EQUALS [[:blank:]]*=[[:blank:]]*
OPTION [a-zA-Z]+[a-zA-Z0-9_-]*
%{
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include <inttypes.h>
#include "lopsub-internal.h"
#include "lopsub.h"
struct lls_yy_config_file_extra {
int argc;
char **argv;
const char *subcmd;
};
static int add_option(yyscan_t yyscanner);
static int parse_arg(char **result, yyscan_t yyscanner);
static int yywrap(yyscan_t yyscanner) {return 1;}
%}
%%
/* skip comments and whitespace */
^[[:blank:]]*#.*\n ;
[[:blank:]]|\n+ ;
<INITIAL,SC_SCANNING>\[[[:blank:]]*{IDENTIFIER}[[:blank:]]*\][[:blank:]]*\n {
int i, j;
const char *subcmd = yyget_extra(yyscanner)->subcmd;
assert(yytext[0] == '[');
if (!subcmd)
return 0;
for (i = 1; i < yyleng; i++)
if (!isspace(yytext[i]))
break;
for (j = i; j < yyleng; j++)
if (yytext[j] == ']' || isspace(yytext[j]))
break;
assert(j < yyleng);
yytext[j] = '\0';
if (strcmp(yytext + i, subcmd))
BEGIN(INITIAL);
else
BEGIN(SC_SCANNING);
}
<SC_SCANNING>{OPTION}[[:blank:]]*\n add_option(yyscanner);
<SC_SCANNING>{OPTION}({EQUALS}|[[:blank:]]+) {
int ret = add_option(yyscanner);
if (ret < 0)
return ret;
BEGIN(SC_ARG);
}
<SC_ARG>.*\n {
struct lls_yy_config_file_extra *extra = yyget_extra(yyscanner);
const char *opt = extra->argv[extra->argc - 1];
char *arg, *result;
size_t opt_len = strlen(opt), arg_len;
int ret = parse_arg(&arg, yyscanner);
if (ret < 0)
return ret;
arg_len = ret;
result = malloc(opt_len + arg_len + 2);
if (!result)
return -E_LLS_NOMEM;
strcpy(result, opt);
result[opt_len] = '=';
strcpy(result + opt_len + 1, arg);
free(arg);
result[opt_len + arg_len + 1] = '\0';
free(extra->argv[extra->argc - 1]);
extra->argv[extra->argc - 1] = result;
BEGIN(SC_SCANNING);
}
<INITIAL>.*\n {}
/* This rule runs iff none of the above patterns matched */
. {return -1;}
%%
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
static int expand_result(yyscan_t yyscanner)
{
struct lls_yy_config_file_extra *extra = yyget_extra(yyscanner);
int nargc = extra->argc + 1;
char **nrargv = realloc(extra->argv, (nargc + 1) * sizeof(char *));
if (!nrargv)
return -E_LLS_NOMEM;
extra->argc = nargc;
extra->argv = nrargv;
extra->argv[extra->argc] = NULL;
return 1;
}
static int add_option(yyscan_t yyscanner)
{
struct lls_yy_config_file_extra *extra = yyget_extra(yyscanner);
int ret;
unsigned n;
int len = yyget_leng(yyscanner);
char *narg, *text = yyget_text(yyscanner);
for (n = 0; n < len; n++)
if (!isalnum(text[n]) && !(text[n] == '-'))
break;
assert(n > 0);
ret = expand_result(yyscanner);
if (ret < 0)
return ret;
narg = malloc(n + 2 + 1);
if (!narg)
return -E_LLS_NOMEM;
narg[0] = narg[1] = '-';
memcpy(narg + 2, text, n);
narg[n + 2] = '\0';
extra->argv[extra->argc - 1] = narg;
return 1;
}
static int parse_arg(char **result, yyscan_t yyscanner)
{
bool backslash = false, quote = false;
const char *in;
char *out;
int ret;
int len = yyget_leng(yyscanner);
char *text = yyget_text(yyscanner);
*result = malloc(len + 1);
if (!*result)
return -E_LLS_NOMEM;
for (in = text, out = *result; *in; in++) {
if (*in == '\\') {
if (!backslash) {
backslash = true;
continue;
}
} else if (*in == 'n' || *in == 't') {
if (backslash) { /* \n or \t */
*out++ = (*in == 'n')? '\n' : '\t';
backslash = false;
continue;
}
} else if (*in == '"') {
if (!backslash) {
quote = !quote;
continue;
}
} else if (isspace(*in)) {
if (!backslash && !quote)
break;
}
/* copy the character */
*out++ = *in;
backslash = false;
}
ret = -E_LLS_TRAILING_BACKSLASH;
if (backslash)
goto fail;
ret = -E_LLS_UNMATCHED_QUOTE;
if (quote)
goto fail;
/* look at first non-space character */
for (; *in; in++) {
if (isspace(*in))
continue;
if (*in == '#')
break;
ret = -E_LLS_TRAILING_GARBAGE;
goto fail;
}
/* success */
*out = '\0';
return out - *result;
fail:
assert(ret < 0);
free(*result);
*result = NULL;
return ret;
}
void lls_free_argv(char **argv)
{
int i;
if (!argv)
return;
for (i = 0; argv[i]; i++)
free(argv[i]);
free(argv);
}
int lls_convert_config(const char *buf, size_t nbytes, const char *subcmd,
char ***result, char **errctx)
{
int ret;
YY_BUFFER_STATE yybs;
yyscan_t yyscanner;
struct lls_yy_config_file_extra extra;
*result = NULL;
if (errctx)
*errctx = NULL;
extra.argc = 1;
extra.argv = malloc((extra.argc + 1) * sizeof(char *));
if (!extra.argv)
return -E_LLS_NOMEM;
extra.argv[0] = strdup(__FUNCTION__);
if (!extra.argv[0]) {
free(extra.argv);
return -E_LLS_NOMEM;
}
extra.argv[1] = NULL;
extra.subcmd = subcmd;
ret = yylex_init_extra(&extra, &yyscanner);
if (ret != 0) {
ret = -E_LLS_NOMEM;
goto free_argv;
}
yy_push_state(subcmd? INITIAL : SC_SCANNING, yyscanner);
yybs = yy_scan_bytes(buf, nbytes, yyscanner);
if (!yybs) {
ret = -E_LLS_YY_SCAN;
goto destroy;
}
ret = yylex(yyscanner);
if (ret < 0) {
ret = -E_LLS_YY_LEX;
if (errctx) {
*errctx = malloc(100);
if (*errctx)
sprintf(*errctx, "yylex error");
}
}
yy_delete_buffer(yybs, yyscanner);
destroy:
yylex_destroy(yyscanner);
free_argv:
if (ret < 0)
lls_free_argv(extra.argv);
else {
*result = extra.argv;
ret = extra.argc;
}
return ret;
}
#if 0
/* flex -o t.c config_file.l && cc -o tcf t.c */
int main(void)
{
char buf[100 * 1024];
int ret, len, i, argc;
char **argv;
ret = read(STDIN_FILENO, buf, sizeof(buf));
if (ret <= 0)
exit(EXIT_FAILURE);
len = ret;
ret = lls_convert_config(buf, len, NULL, &argv, NULL);
if (ret < 0)
exit(EXIT_FAILURE);
argc = ret;
for (i = 0; i < argc; i++)
printf("argv[%d]: %s\n", i, argv[i]);
return EXIT_SUCCESS;
}
#endif
|