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
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* ap_expr_scan.l, based on ssl_expr_scan.l from mod_ssl
*/
/* _________________________________________________________________
**
** Expression Scanner
** _________________________________________________________________
*/
%pointer
%option batch
%option never-interactive
%option nodefault
%option noyywrap
%option reentrant
%option bison-bridge
%option warn
%option noinput nounput noyy_top_state
%option stack
%x str
%x var
%x vararg
%x regex regex_flags
%{
#include "util_expr_private.h"
#include "util_expr_parse.h"
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
{ \
if ((result = MIN(max_size, yyextra->inputbuf \
+ yyextra->inputlen \
- yyextra->inputptr)) <= 0) \
{ \
result = YY_NULL; \
} \
else { \
memcpy(buf, yyextra->inputptr, result); \
yyextra->inputptr += result; \
} \
}
#define YY_EXTRA_TYPE ap_expr_parse_ctx_t*
#define PERROR(msg) do { yyextra->error2 = msg ; return T_ERROR; } while (0)
#define str_ptr (yyextra->scan_ptr)
#define str_buf (yyextra->scan_buf)
#define str_del (yyextra->scan_del)
#define STR_APPEND(c) do { \
*str_ptr++ = (c); \
if (str_ptr >= str_buf + sizeof(str_buf)) \
PERROR("String too long"); \
} while (0)
%}
%%
char regex_buf[MAX_STRING_LEN];
char *regex_ptr = NULL;
char regex_del = '\0';
%{
/*
* Set initial state for string expressions
*/
if (yyextra->at_start) {
yyextra->at_start = 0;
if (yyextra->flags & AP_EXPR_FLAG_STRING_RESULT) {
BEGIN(str);
return T_EXPR_STRING;
}
else {
return T_EXPR_BOOL;
}
}
%}
/*
* Whitespaces
*/
[ \t\n]+ {
/* NOP */
}
/*
* strings ("..." and '...')
*/
["'] {
str_ptr = str_buf;
str_del = yytext[0];
BEGIN(str);
return T_STR_BEGIN;
}
<str>["'] {
if (yytext[0] == str_del) {
if (YY_START == var) {
PERROR("Unterminated variable in string");
}
else if (str_ptr == str_buf) {
BEGIN(INITIAL);
return T_STR_END;
}
else {
/* return what we have so far and scan delimiter again */
*str_ptr = '\0';
yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
yyless(0);
str_ptr = str_buf;
return T_STRING;
}
}
else {
STR_APPEND(yytext[0]);
}
}
<str,var,vararg>\n {
PERROR("Unterminated string or variable");
}
<var,vararg><<EOF>> {
PERROR("Unterminated string or variable");
}
<str><<EOF>> {
if (!(yyextra->flags & AP_EXPR_FLAG_STRING_RESULT)) {
PERROR("Unterminated string or variable");
}
else {
*str_ptr = '\0';
yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
str_ptr = str_buf;
BEGIN(INITIAL);
return T_STRING;
}
}
<str,vararg>\\[0-7]{1,3} {
int result;
(void)sscanf(yytext+1, "%o", &result);
if (result > 0xff) {
PERROR("Escape sequence out of bound");
}
else {
STR_APPEND(result);
}
}
<str,vararg>\\[0-9]+ {
PERROR("Bad escape sequence");
}
<str,vararg>\\n { STR_APPEND('\n'); }
<str,vararg>\\r { STR_APPEND('\r'); }
<str,vararg>\\t { STR_APPEND('\t'); }
<str,vararg>\\b { STR_APPEND('\b'); }
<str,vararg>\\f { STR_APPEND('\f'); }
<str,vararg>\\(.|\n) { STR_APPEND(yytext[1]); }
/* regexp backref inside string/arg */
<str,vararg>[$][0-9] {
if (str_ptr != str_buf) {
/* return what we have so far and scan '$x' again */
*str_ptr = '\0';
yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
str_ptr = str_buf;
yyless(0);
return T_STRING;
}
else {
yylval->num = yytext[1] - '0';
return T_REGEX_BACKREF;
}
}
<str,vararg>[^\\\n"'%}$]+ {
char *cp = yytext;
while (*cp != '\0') {
STR_APPEND(*cp);
cp++;
}
}
/* variable inside string/arg */
<str,vararg>%\{ {
if (str_ptr != str_buf) {
/* return what we have so far and scan '%{' again */
*str_ptr = '\0';
yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
yyless(0);
str_ptr = str_buf;
return T_STRING;
}
else {
yy_push_state(var, yyscanner);
return T_VAR_BEGIN;
}
}
<vararg>[%$] {
STR_APPEND(yytext[0]);
}
<str>[%}$] {
STR_APPEND(yytext[0]);
}
%\{ {
yy_push_state(var, yyscanner);
return T_VAR_BEGIN;
}
[$][0-9] {
yylval->num = yytext[1] - '0';
return T_REGEX_BACKREF;
}
/*
* fixed name variable expansion %{XXX} and function call in %{func:arg} syntax
*/
<var>[a-zA-Z][a-zA-Z0-9_]* {
yylval->cpVal = apr_pstrdup(yyextra->pool, yytext);
return T_ID;
}
<var>\} {
yy_pop_state(yyscanner);
return T_VAR_END;
}
<var>: {
BEGIN(vararg);
return yytext[0];
}
<var>.|\n {
char *msg = apr_psprintf(yyextra->pool,
"Invalid character in variable name '%c'", yytext[0]);
PERROR(msg);
}
<vararg>\} {
if (str_ptr != str_buf) {
/* return what we have so far and scan '}' again */
*str_ptr = '\0';
yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
str_ptr = str_buf;
yyless(0);
return T_STRING;
}
else {
yy_pop_state(yyscanner);
return T_VAR_END;
}
}
/*
* Regular Expression
*/
"m"[/#$%^,;:_\?\|\^\-\!\.\'\"] {
regex_del = yytext[1];
regex_ptr = regex_buf;
BEGIN(regex);
}
"/" {
regex_del = yytext[0];
regex_ptr = regex_buf;
BEGIN(regex);
}
<regex>.|\n {
if (yytext[0] == regex_del) {
*regex_ptr = '\0';
BEGIN(regex_flags);
}
else {
*regex_ptr++ = yytext[0];
if (regex_ptr >= regex_buf + sizeof(regex_buf))
PERROR("Regexp too long");
}
}
<regex_flags>i {
yylval->cpVal = apr_pstrdup(yyextra->pool, regex_buf);
BEGIN(INITIAL);
return T_REGEX_I;
}
<regex_flags>.|\n {
yylval->cpVal = apr_pstrdup(yyextra->pool, regex_buf);
yyless(0);
BEGIN(INITIAL);
return T_REGEX;
}
<regex_flags><<EOF>> {
yylval->cpVal = apr_pstrdup(yyextra->pool, regex_buf);
BEGIN(INITIAL);
return T_REGEX;
}
/*
* Operators
*/
==? { return T_OP_STR_EQ; }
"!=" { return T_OP_STR_NE; }
"<" { return T_OP_STR_LT; }
"<=" { return T_OP_STR_LE; }
">" { return T_OP_STR_GT; }
">=" { return T_OP_STR_GE; }
"=~" { return T_OP_REG; }
"!~" { return T_OP_NRE; }
"and" { return T_OP_AND; }
"&&" { return T_OP_AND; }
"or" { return T_OP_OR; }
"||" { return T_OP_OR; }
"not" { return T_OP_NOT; }
"!" { return T_OP_NOT; }
"." { return T_OP_CONCAT; }
"-in" { return T_OP_IN; }
"-eq" { return T_OP_EQ; }
"-ne" { return T_OP_NE; }
"-ge" { return T_OP_GE; }
"-le" { return T_OP_LE; }
"-gt" { return T_OP_GT; }
"-lt" { return T_OP_LT; }
/* for compatibility with ssl_expr */
"lt" { return T_OP_LT; }
"le" { return T_OP_LE; }
"gt" { return T_OP_GT; }
"ge" { return T_OP_GE; }
"ne" { return T_OP_NE; }
"eq" { return T_OP_EQ; }
"in" { return T_OP_IN; }
"-"[a-zA-Z_] {
yylval->cpVal = apr_pstrdup(yyextra->pool, yytext + 1);
return T_OP_UNARY;
}
"-"[a-zA-Z_][a-zA-Z_0-9]+ {
yylval->cpVal = apr_pstrdup(yyextra->pool, yytext + 1);
return T_OP_BINARY;
}
/*
* Specials
*/
"true" { return T_TRUE; }
"false" { return T_FALSE; }
/*
* Digits
*/
-?[0-9]+ {
yylval->cpVal = apr_pstrdup(yyextra->pool, yytext);
return T_DIGIT;
}
/*
* Identifiers
*/
[a-zA-Z][a-zA-Z0-9_]* {
yylval->cpVal = apr_pstrdup(yyextra->pool, yytext);
return T_ID;
}
/*
* These are parts of the grammar and are returned as is
*/
[(){},:] {
return yytext[0];
}
/*
* Anything else is an error
*/
.|\n {
char *msg = apr_psprintf(yyextra->pool, "Parse error near '%c'", yytext[0]);
PERROR(msg);
}
%%
|