cfg_lexer.l
8.76 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
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
%{
/* cfg_lexer.l -- default lexer for Mailutils configuration files
Copyright (C) 2007, 2008, 2009 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <mailutils/cctype.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/debug.h>
#include <mailutils/argcv.h>
#include <mailutils/alloc.h>
#include <mailutils/nls.h>
#include <mailutils/cfg.h>
#include <mailutils/list.h>
#include "cfg_parser.h"
void _mu_line_begin (void);
void _mu_line_add (char *text, size_t len);
char *_mu_line_finish (void);
extern void mu_cfg_set_debug (void);
static void
mu_cfg_set_lex_debug (void)
{
yy_flex_debug = mu_debug_check_level (mu_cfg_get_debug (),
MU_DEBUG_TRACE2);
}
static void _mu_line_add_unescape_last (char *text, size_t len);
static void multiline_begin (char *p);
static char *multiline_strip_tabs (char *text);
static void multiline_add (char *s);
static char *multiline_finish (void);
static char *multiline_delimiter;
static size_t multiline_delimiter_len;
static int multiline_unescape; /* Unescape here-document contents */
static int (*char_to_strip)(char); /* Strip matching characters of each
here-document line */
static int isemptystr(int off);
static mu_opool_t pool;
%}
%x COMMENT ML STR
WS [ \t\f][ \t\f]*
ID [a-zA-Z_][a-zA-Z_0-9-]+
P [1-9][0-9]*
%%
/* C-style comments */
"/*" BEGIN(COMMENT);
<COMMENT>[^*\n]* /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
<COMMENT>\n ++mu_cfg_locus.line;
<COMMENT>"*"+"/" BEGIN (INITIAL);
/* End-of-line comments */
#debug=.*\n {
mu_log_level_t lev;
mu_debug_t dbg = mu_cfg_get_debug ();
if (mu_debug_level_from_string (yytext + 7, &lev, dbg) == 0)
{
mu_debug_set_level (dbg, lev);
mu_cfg_set_debug ();
mu_cfg_set_lex_debug ();
}
}
#.*\n { mu_cfg_locus.line++; }
#.* /* end-of-file comment */;
"//".*\n { mu_cfg_locus.line++; }
"//".* /* end-of-file comment */;
/* Identifiers */
<INITIAL>{ID} {
_mu_line_begin ();
_mu_line_add (yytext, yyleng);
yylval.string = _mu_line_finish ();
return MU_TOK_IDENT; }
/* Strings */
[a-zA-Z0-9_\./:\*=-]+ { _mu_line_begin ();
_mu_line_add (yytext, yyleng);
yylval.string = _mu_line_finish ();
return MU_TOK_STRING; }
/* Quoted strings */
\"[^\\"\n]*\" { _mu_line_begin ();
_mu_line_add (yytext + 1, yyleng - 2);
yylval.string = _mu_line_finish ();
return MU_TOK_QSTRING; }
\"[^\\"\n]*\\. |
\"[^\\"\n]*\\\n { BEGIN (STR);
_mu_line_begin ();
_mu_line_add_unescape_last (yytext + 1, yyleng - 1); }
<STR>[^\\"\n]*\\. |
<STR>\"[^\\"\n]*\\\n { _mu_line_add_unescape_last (yytext, yyleng); }
<STR>[^\\"\n]*\" { BEGIN (INITIAL);
if (yyleng > 1)
_mu_line_add (yytext, yyleng - 1);
yylval.string = _mu_line_finish ();
return MU_TOK_QSTRING; }
/* Multiline strings */
"<<"(-" "?)?\\?{ID}[ \t]*#.*\n |
"<<"(-" "?)?\\?{ID}[ \t]*"//".*\n |
"<<"(-" "?)?\\?{ID}[ \t]*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*#.*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*\n {
BEGIN (ML);
multiline_begin (yytext+2);
mu_cfg_locus.line++;
}
<ML>.*\n { char *p = multiline_strip_tabs (yytext);
if (!strncmp (p, multiline_delimiter, multiline_delimiter_len)
&& isemptystr (p + multiline_delimiter_len - yytext))
{
free (multiline_delimiter);
multiline_delimiter = NULL;
BEGIN (INITIAL);
yylval.string = multiline_finish ();
return MU_TOK_MSTRING;
}
mu_cfg_locus.line++;
multiline_add (p); }
{WS} ;
/* Other tokens */
\n { mu_cfg_locus.line++; }
[,;{}()] return yytext[0];
. { if (mu_isprint (yytext[0]))
mu_cfg_parse_error (_("stray character %c"), yytext[0]);
else
mu_cfg_parse_error (_("stray character \\%03o"),
(unsigned char) yytext[0]);
}
%%
int
yywrap ()
{
return 1;
}
static void
unescape_to_line (int c)
{
if (c != '\n')
{
char t = mu_argcv_unquote_char (c);
if (t == c && t != '\\' && t != '\"')
mu_cfg_parse_error (_("unknown escape sequence '\\%c'"), c);
mu_opool_append_char (pool, t);
}
}
void
_mu_line_add (char *text, size_t len)
{
mu_opool_append (pool, text, len);
}
void
_mu_line_add_unescape_last (char *text, size_t len)
{
mu_opool_append (pool, text, len - 2);
unescape_to_line (text[len - 1]);
}
void
_mu_line_begin ()
{
if (!pool)
mu_opool_create (&pool, 1);
else
mu_opool_clear (pool);
}
char *
_mu_line_finish ()
{
mu_opool_append_char (pool, 0);
return mu_opool_finish (pool, NULL);
}
static int
is_tab (char c)
{
return c == '\t';
}
static int
is_ws (char c)
{
return c == '\t' || c == ' ';
}
static int
isemptystr (int off)
{
for (; yytext[off] && mu_isspace (yytext[off]); off++)
;
if (yytext[off] == ';')
{
int i;
for (i = off + 1; yytext[i]; i++)
if (!mu_isspace (yytext[i]))
return 0;
yyless (off);
return 1;
}
return yytext[off] == 0;
}
static void
multiline_begin (char *p)
{
if (*p == '-')
{
if (*++p == ' ')
{
char_to_strip = is_ws;
p++;
}
else
char_to_strip = is_tab;
}
else
char_to_strip = NULL;
if (*p == '\\')
{
p++;
multiline_unescape = 0;
}
else if (*p == '"')
{
char *q;
p++;
multiline_unescape = 0;
q = strchr (p, '"');
multiline_delimiter_len = q - p;
}
else
{
multiline_delimiter_len = strcspn (p, " \t");
multiline_unescape = 1;
}
/* Remove trailing newline */
multiline_delimiter_len--;
multiline_delimiter = mu_alloc (multiline_delimiter_len + 1);
memcpy (multiline_delimiter, p, multiline_delimiter_len);
multiline_delimiter[multiline_delimiter_len] = 0;
_mu_line_begin ();
}
static char *
multiline_strip_tabs (char *text)
{
if (char_to_strip)
for (; *text && char_to_strip (*text); text++)
;
return text;
}
static void
multiline_add (char *s)
{
if (multiline_unescape)
{
for (; *s; s++)
{
if (*s == '\\')
{
unescape_to_line (s[1]);
++s;
}
else
_mu_line_add (s, 1);
}
}
else
_mu_line_add (s, strlen (s));
}
static char *
multiline_finish ()
{
return _mu_line_finish ();
}
int
mu_cfg_parse_file (mu_cfg_tree_t **return_tree, const char *file, int flags)
{
struct stat st;
FILE *fp;
int rc;
if (stat (file, &st))
{
if (errno != ENOENT)
mu_error (_("cannot stat `%s': %s"), file, mu_strerror (errno));
return ENOENT;
}
fp = fopen (file, "r");
if (!fp)
{
mu_error (_("cannot open config file `%s': %s"), file,
mu_strerror (errno));
return errno;
}
if (flags & MU_PARSE_CONFIG_VERBOSE)
mu_diag_output (MU_DIAG_INFO, _("parsing file `%s'"), file);
mu_cfg_set_lex_debug ();
/* Parse configuration */
mu_cfg_locus.file = (char*) file;
mu_cfg_locus.line = 1;
yyrestart (fp);
rc = mu_cfg_parse (return_tree);
fclose (fp);
if (flags & MU_PARSE_CONFIG_VERBOSE)
mu_diag_output (MU_DIAG_INFO, _("finished parsing file `%s'"), file);
return rc == 0 ? 0 : MU_ERR_FAILURE;
}
/* FIXME: Deprecated interface */
int
mu_get_config (const char *file, const char *progname,
struct mu_cfg_param *progparam, int flags, void *target_ptr)
{
mu_cfg_tree_t *parse_tree;
int rc = mu_cfg_parse_file (&parse_tree, file, flags);
if (rc == 0)
rc = mu_cfg_tree_reduce (parse_tree, progname, progparam, flags,
target_ptr);
mu_cfg_destroy_tree (&parse_tree);
return rc == 0 ? 0 : MU_ERR_FAILURE;
}
mu_opool_t
mu_cfg_lexer_pool ()
{
mu_opool_t p = pool;
pool = NULL;
return p;
}