-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlex.l
539 lines (467 loc) · 11.9 KB
/
lex.l
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
%{
/* +-------------------------------------------------------------------+ */
/* | Copyright 1991, David Koblas. | */
/* | Permission to use, copy, modify, and distribute this software | */
/* | and its documentation for any purpose and without fee is hereby | */
/* | granted, provided that the above copyright notice appear in all | */
/* | copies and that both that copyright notice and this permission | */
/* | notice appear in supporting documentation. This software is | */
/* | provided "as is" without express or implied warranty. | */
/* +-------------------------------------------------------------------+ */
#include "defs.h"
#include <syslog.h>
#ifndef NDEBUG
#define _msg(x)
#else
#define _msg(x) msg x
static void msg(const char *format, ...);
#endif
static char *expandvars(const char *instr);
static void preprocerror(const char *str);
static void preproc(const char *str);
static void addvar(const char *str);
static void addquotedarg(int state, cmd_t * cmd, const char *instr);
static void addarg(int state, cmd_t * cmd, const char *instr);
static cmd_t *alloccmd(const char *name);
static void freecmd(cmd_t * cmd);
static cmd_t *newcmd(const char *name);
#ifdef NUNUSED
static int cmpopts(const char *a, const char *b);
static void sortopts(cmd_t * cmd);
#endif
int yyline = 1;
#define YY_NO_INPUT
%}
WS [ \t]*
NWS [^ \n\t;]+
%s ARGS
%a 2700
%o 3700
%option nounput
%%
int state = 0;
cmd_t *cmd = NULL;
#[^\n]* ;
^%(((if|elsif)[ \t]+.*)|(else|endif)) { preproc(yytext); }
^%.* { preprocerror(yytext); }
\n { yyline++; BEGIN 0; }
^[A-Z_][A-Z_0-9]*=[^\n\r]* { addvar(yytext); }
^[^ \n\t]+ { cmd = newcmd(yytext);
state = strcmp(yytext, "DEFAULT") == 0 ? 1 : 0;
BEGIN ARGS; }
^{WS} BEGIN ARGS;
<ARGS>";" state++;
<ARGS>([^ \n\t'"]*'(\\.|[^'])*')+ addquotedarg(state, cmd, yytext);
<ARGS>([^ \n\t'"]*\"(\\.|[^\"])*\")+ addquotedarg(state, cmd, yytext);
<ARGS>{NWS} addarg(state, cmd, yytext);
<ARGS>{WS} ;
%%
/* ' for emacs */
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#ifdef NDEBUG
static void
msg(const char *format, ...)
{
va_list ap;
char *s;
va_start(ap);
s = va_arg(ap, char *);
fprintf(stderr, "line %d: ", yyline);
/* Flawfinder: ignore (vfprintf) */
vfprintf(stderr, s, ap);
fputc('\n', stderr);
va_end(ap);
}
#endif
static char *
expandvars(const char *instr)
{
char *str = (char *)malloc(VAR_EXPAND_LEN);
size_t i = 0;
if (str == NULL)
fatal(1, "Unable to allocate variable expansion buffer");
while (*instr)
if (isupper((int)*instr) || *instr == '_') {
const char *mark = instr;
var_t *var;
while (*instr &&
(isupper((int)*instr) || isdigit((int)*instr) ||
*instr == '_'))
++instr;
for (var = Variables; var != 0; var = var->next) {
/* Flawfinder: ignore (strlen) */
size_t l = strlen(var->name);
if ((size_t) (instr - mark) > l)
l = (size_t) (instr - mark);
if (!strncmp(mark, var->name, l)) {
str[i] = 0;
/* Flawfinder: fix (strcat) */
strlcat(str, var->value, VAR_EXPAND_LEN);
/* Flawfinder: ignore (strlen) */
i += strlen(var->value);
break;
}
}
if (!var) {
instr = mark + 1;
str[i++] = *mark;
}
} else
str[i++] = *instr++;
str[i] = 0;
return str;
}
static void
preprocerror(const char *str)
{
fprintf(stderr, "Invalid preprocessor command '%s'\n", str);
exit(1);
}
static void
preproc(const char *str)
{
if (!strncmp(str, "%if", 3)) {
} else if (!strncmp(str, "%elseif", 7)) {
} else if (!strcmp(str, "%else")) {
} else if (!strcmp(str, "%endif")) {
} else
preprocerror(str);
}
static void
addvar(const char *str)
{
/* Flawfinder: ignore (char) */
/* cppcheck-suppress variableScope */
char name[VAR_NAME_LEN], value[VAR_EXPAND_LEN];
const char *eq = strchr(str, '=');
if (eq && str - eq < VAR_NAME_LEN) {
size_t i, o, len;
var_t *var;
/* Flawfinder: fix (strncpy) */
strlcpy(name, str, MIN((size_t) (eq - str + 1), sizeof(name)));
for (o = 0, i = (size_t) (eq - str + 1);
o < VAR_EXPAND_LEN - 1 && str[i]; ++i)
if (str[i] == '\\') {
switch (str[++i]) {
case 'n':
value[o++] = '\n';
break;
case 'r':
value[o++] = '\r';
break;
case 't':
value[o++] = '\t';
break;
case 'a':
value[o++] = '\a';
break;
case 'b':
value[o++] = '\b';
break;
default:
value[o++] = str[i];
break;
}
} else if (str[i] == '"')
break;
else
value[o++] = str[i];
value[o++] = 0;
if ((var = (var_t *) malloc(sizeof(var_t))) == NULL)
fatal(1, "Unable to allocate var_t");
/* Flawfinder: fix (strlen) */
len = strlen(name) + 1;
if ((var->name = malloc(len)) == NULL) /* expected-warning */
fatal(1, "Unable to allocate variable name");
/* Flawfinder: fix (strcpy) */
strlcpy(var->name, name, len);
var->value = expandvars(value);
var->next = 0;
if (Variables) {
var_t *v;
for (v = Variables; v->next; v = v->next) ;
v->next = var;
} else
Variables = var;
} else
fatal(1, "Invalid alias");
}
static void
addquotedarg(int state, cmd_t * cmd, const char *instr)
{
/* Flawfinder: ignore (char) */
char buffer[MAXSTRLEN];
size_t i, o, q;
/* Flawfinder: ignore (strlen) */
if (strlen(instr) + 2 > MAXSTRLEN) {
fatal(1, "Quoted argument too long\n");
exit(1);
}
for (o = 0; !strchr("'\"", instr[o]); ++o)
buffer[o] = instr[o];
q = o;
for (i = o + 1; instr[i] && instr[i] != instr[q]; ++i, ++o) {
if (instr[i] == '\\') {
int c = instr[++i];
if (strchr("'\"", c)) {
buffer[o] = (char)c;
} else {
buffer[o++] = '\\';
buffer[o] = (char)c;
}
} else
buffer[o] = instr[i];
}
buffer[o] = 0;
addarg(state, cmd, buffer);
}
static void
addarg(int state, cmd_t * cmd, const char *instr)
{
char *str = expandvars(instr);
if (state == 0) {
_msg(("cmd='%s' add arg '%s'", cmd->name, str));
if (cmd->margs == cmd->nargs) {
cmd->margs += cmd->margs;
cmd->args = (char **)realloc(cmd->args,
sizeof(char *) * cmd->margs);
if (cmd->args == NULL)
fatal(1, "Unable to groupw args");
}
cmd->args[cmd->nargs++] = savestr(str);
} else if (state == 1) {
_msg(("cmd='%s' add opt '%s'", cmd->name, str));
if (cmd->mopts == cmd->nopts) {
cmd->mopts += cmd->mopts;
cmd->opts = (char **)realloc(cmd->opts,
sizeof(char *) * cmd->mopts);
if (cmd->opts == NULL)
fatal(1, "Unable to groupw opts");
}
cmd->opts[cmd->nopts++] = savestr(str);
} else {
fatal(1, "bad state (%d) received\n", state);
}
free(str);
}
char *
savestr(const char *str)
{
/* Flawfinder: ignore (strlen) */
size_t len = strlen(str) + 1;
char *s = (char *)malloc(len);
if (s == NULL)
fatal(1, "No string space");
/* Flawfinder: fix (strcpy) */
strlcpy(s, str, len);
return s;
}
static cmd_t *
alloccmd(const char *name)
{
cmd_t *cmd = (cmd_t *) malloc(sizeof(cmd_t));
if (cmd == NULL)
fatal(1, "Unable to alloc space for new command");
cmd->name = savestr(name);
cmd->nargs = 0;
cmd->margs = 16;
cmd->nopts = 0;
cmd->mopts = 16;
cmd->args = (char **)malloc(sizeof(char *) * cmd->margs);
cmd->opts = (char **)malloc(sizeof(char *) * cmd->mopts);
if (cmd->args == NULL || cmd->opts == NULL)
fatal(1, "Unable to alloc args/opts");
return cmd;
}
static void
freecmd(cmd_t * cmd)
{
if (cmd == NULL)
return;
free(cmd->name);
free(cmd->args);
free(cmd->opts);
free(cmd);
}
static cmd_t *
newcmd(const char *name)
{
cmd_t *cmd = alloccmd(name);
cmd->next = First;
First = cmd;
return cmd;
}
int
ReadFile(const char *file)
{
struct stat statbuf;
FILE *fd;
if ((stat(file, &statbuf) < 0))
return 0;
if ((statbuf.st_uid != 0) || /* Owned by root */
((statbuf.st_mode & 0077) != 0)) { /* SD - no perm */
logger(LOG_ERR, "Permission problems on %s", file);
return 0;
}
/* Flawfinder: ignore (fopen) race condition */
if ((fd = fopen(file, "r")) == NULL)
return 0;
yyin = fd;
yylex();
return 1;
}
int
CountArgs(cmd_t * cmd)
{
size_t i;
/* NOLINTNEXTLINE(runtime/int) */
long val, max = 0;
int wild = 0;
/* Flawfinder: ignore (char) */
/* cppcheck-suppress variableScope */
char *cp, *np, str[MAXSTRLEN];
for (i = 0; i < cmd->nargs; i++) {
np = cmd->args[i];
while ((cp = strchr(np, '$')) != NULL) {
if ((cp != cmd->args[i]) && (*(cp - 1) == '\\'))
np = cp + 1;
else {
if (*(cp + 1) == '*') {
wild = 1;
++cp;
np = cp;
continue;
}
cp++;
np = cp;
while (isdigit((int)*cp))
cp++;
if ((cp - np) == 0)
continue;
/* Flawfinder: fix (strncpy) */
strlcpy(str, np, MIN((size_t) (cp - np + 1), sizeof(str)));
/* Flawfinder: fix (atoi -> strtolong) */
val = strtolong(str, 10);
if (val > max)
max = val;
}
}
}
if (wild)
return (int)-max;
return (int)max;
}
#ifdef NUNUSED
static int
cmpopts(const char *a, const char *b)
{
char *cp_a, *cp_b;
/* NOLINTNEXTLINE(runtime/int) */
long val_a, val_b;
/* Flawfinder: ignore (char) */
char str_a[MAXSTRLEN], str_b[MAXSTRLEN];
if (*a != '$' && *b != '$')
return 0;
if (*a == '$' && *b != '$')
return -1;
if (*a != '$' && *b == '$')
return 1;
cp_a = ++a;
cp_b = ++b;
while ((*cp_a != '\0') && (*cp_a != '='))
if (!isdigit((int)*cp_a))
break;
while ((*cp_b != '\0') && (*cp_b != '='))
if (!isdigit((int)*cp_b))
break;
if (*cp_a != '=' && *cp_b != '=')
return 0;
if (*cp_a == '=' && *cp_b != '=')
return -1;
if (*cp_a != '=' && *cp_b == '=')
return 1;
/* flawfinder: fix (strncpy) */
strlcpy(str_a, a, MIN((size_t) (cp_a - a + 1), sizeof(str_a)));
/* flawfinder: fix (atoi -> strtolong) */
val_a = strtolong(str_a, 10);
/* flawfinder: fix (strncpy) */
strlcpy(str_b, b, MIN((size_t) (cp_b - a + 1), sizeof(str_b)));
/* Flawfinder: fix (atoi -> strtolong) */
val_b = strtolong(str_b, 10);
if (val_a < val_b)
return -1;
if (val_a > val_b)
return 1;
return 0;
}
static void
sortopts(cmd_t * cmd)
{
qsort(cmd->opts, cmd->nopts, sizeof(char *),
(int(*)(const void *, const void *))cmpopts);
}
#endif
/* Build a new command but don't merge it into the global list */
cmd_t *
BuildSingle(cmd_t * def, cmd_t * cmd)
{
cmd_t *new = alloccmd(cmd->name ? cmd->name : "");
/* Flawfinder: ignore (char) */
char defname[MAXSTRLEN], optname[MAXSTRLEN], *cp;
size_t i, j;
/* cppcheck-suppress nullPointer */
if (cmd == NULL) {
freecmd(new);
return def;
}
if (def == NULL) {
freecmd(new);
return cmd;
}
for (i = 0; i < cmd->nargs; i++)
addarg(0, new, cmd->args[i]);
for (i = 0; i < def->nopts; i++) {
int skipped = 0;
if ((cp = strchr(def->opts[i], '=')) == NULL)
/* Flawfinder: fix (strcpy) */
strlcpy(defname, def->opts[i], sizeof(defname));
else {
size_t l = (size_t) (cp - def->opts[i]);
/* Flawfinder: fix (strncpy) */
strlcpy(defname, def->opts[i], MIN(l + 1, sizeof(defname)));
}
for (j = 0; j < cmd->nopts; j++) {
if ((cp = strchr(cmd->opts[j], '=')) == NULL)
/* Flawfinder: fix (strcpy) */
strlcpy(optname, cmd->opts[j], sizeof(optname));
else {
size_t l = (size_t) (cp - cmd->opts[j]);
/* Flawfinder: fix (strncpy) */
strlcpy(optname, cmd->opts[j], MIN(l + 1, sizeof(optname)));
}
if (strcmp(defname, optname) == 0) {
skipped = 1;
break;
}
}
if (skipped)
continue;
if (def->opts[i][0] != '\0')
addarg(1, new, def->opts[i]);
}
for (j = 0; j < cmd->nopts; j++)
addarg(1, new, cmd->opts[j]);
/* sortopts(new); */
return new;
}
/* Build a new command *and* merge it with the global command list */
cmd_t *
Build(cmd_t * def, cmd_t * cmd)
{
cmd_t *new = BuildSingle(def, cmd);
new->next = First;
First = new;
return new;
}