1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * ap_expr_scan.l, based on ssl_expr_scan.l from mod_ssl
21 /* _________________________________________________________________
24 ** _________________________________________________________________
29 %option never-interactive
35 %option noinput nounput noyy_top_state
43 #include "util_expr_private.h"
44 #include "util_expr_parse.h"
47 #define YY_INPUT(buf,result,max_size) \
49 if ((result = MIN(max_size, yyextra->inputbuf \
51 - yyextra->inputptr)) <= 0) \
56 memcpy(buf, yyextra->inputptr, result); \
57 yyextra->inputptr += result; \
61 #define YY_EXTRA_TYPE ap_expr_parse_ctx_t*
63 #define PERROR(msg) do { yyextra->error2 = msg ; return T_ERROR; } while (0)
65 #define str_ptr (yyextra->scan_ptr)
66 #define str_buf (yyextra->scan_buf)
67 #define str_del (yyextra->scan_del)
69 #define STR_APPEND(c) do { \
71 if (str_ptr >= str_buf + sizeof(str_buf)) \
72 PERROR("String too long"); \
80 char regex_buf[MAX_STRING_LEN];
81 char *regex_ptr = NULL;
82 char regex_del = '\0';
86 * Set initial state for string expressions
88 if (yyextra->at_start) {
89 yyextra->at_start = 0;
90 if (yyextra->flags & AP_EXPR_FLAG_STRING_RESULT) {
108 * strings ("..." and '...')
117 if (yytext[0] == str_del) {
118 if (YY_START == var) {
119 PERROR("Unterminated variable in string");
121 else if (str_ptr == str_buf) {
126 /* return what we have so far and scan delimiter again */
128 yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
135 STR_APPEND(yytext[0]);
139 PERROR("Unterminated string or variable");
141 <var,vararg><<EOF>> {
142 PERROR("Unterminated string or variable");
145 if (!(yyextra->flags & AP_EXPR_FLAG_STRING_RESULT)) {
146 PERROR("Unterminated string or variable");
150 yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
157 <str,vararg>\\[0-7]{1,3} {
160 (void)sscanf(yytext+1, "%o", &result);
162 PERROR("Escape sequence out of bound");
168 <str,vararg>\\[0-9]+ {
169 PERROR("Bad escape sequence");
171 <str,vararg>\\n { STR_APPEND('\n'); }
172 <str,vararg>\\r { STR_APPEND('\r'); }
173 <str,vararg>\\t { STR_APPEND('\t'); }
174 <str,vararg>\\b { STR_APPEND('\b'); }
175 <str,vararg>\\f { STR_APPEND('\f'); }
176 <str,vararg>\\(.|\n) { STR_APPEND(yytext[1]); }
178 /* regexp backref inside string/arg */
179 <str,vararg>[$][0-9] {
180 if (str_ptr != str_buf) {
181 /* return what we have so far and scan '$x' again */
183 yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
189 yylval->num = yytext[1] - '0';
190 return T_REGEX_BACKREF;
194 <str,vararg>[^\\\n"'%}$]+ {
196 while (*cp != '\0') {
202 /* variable inside string/arg */
204 if (str_ptr != str_buf) {
205 /* return what we have so far and scan '%{' again */
207 yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
213 yy_push_state(var, yyscanner);
219 STR_APPEND(yytext[0]);
223 STR_APPEND(yytext[0]);
227 yy_push_state(var, yyscanner);
232 yylval->num = yytext[1] - '0';
233 return T_REGEX_BACKREF;
237 * fixed name variable expansion %{XXX} and function call in %{func:arg} syntax
239 <var>[a-zA-Z][a-zA-Z0-9_]* {
240 yylval->cpVal = apr_pstrdup(yyextra->pool, yytext);
245 yy_pop_state(yyscanner);
255 char *msg = apr_psprintf(yyextra->pool,
256 "Invalid character in variable name '%c'", yytext[0]);
261 if (str_ptr != str_buf) {
262 /* return what we have so far and scan '}' again */
264 yylval->cpVal = apr_pstrdup(yyextra->pool, str_buf);
270 yy_pop_state(yyscanner);
278 "m"[/#$%^,;:_\?\|\^\-\!\.\'\"] {
279 regex_del = yytext[1];
280 regex_ptr = regex_buf;
284 regex_del = yytext[0];
285 regex_ptr = regex_buf;
289 if (yytext[0] == regex_del) {
294 *regex_ptr++ = yytext[0];
295 if (regex_ptr >= regex_buf + sizeof(regex_buf))
296 PERROR("Regexp too long");
300 yylval->cpVal = apr_pstrdup(yyextra->pool, regex_buf);
305 yylval->cpVal = apr_pstrdup(yyextra->pool, regex_buf);
310 <regex_flags><<EOF>> {
311 yylval->cpVal = apr_pstrdup(yyextra->pool, regex_buf);
319 ==? { return T_OP_STR_EQ; }
320 "!=" { return T_OP_STR_NE; }
321 "<" { return T_OP_STR_LT; }
322 "<=" { return T_OP_STR_LE; }
323 ">" { return T_OP_STR_GT; }
324 ">=" { return T_OP_STR_GE; }
325 "=~" { return T_OP_REG; }
326 "!~" { return T_OP_NRE; }
327 "and" { return T_OP_AND; }
328 "&&" { return T_OP_AND; }
329 "or" { return T_OP_OR; }
330 "||" { return T_OP_OR; }
331 "not" { return T_OP_NOT; }
332 "!" { return T_OP_NOT; }
333 "." { return T_OP_CONCAT; }
334 "-in" { return T_OP_IN; }
335 "-eq" { return T_OP_EQ; }
336 "-ne" { return T_OP_NE; }
337 "-ge" { return T_OP_GE; }
338 "-le" { return T_OP_LE; }
339 "-gt" { return T_OP_GT; }
340 "-lt" { return T_OP_LT; }
342 /* for compatibility with ssl_expr */
343 "lt" { return T_OP_LT; }
344 "le" { return T_OP_LE; }
345 "gt" { return T_OP_GT; }
346 "ge" { return T_OP_GE; }
347 "ne" { return T_OP_NE; }
348 "eq" { return T_OP_EQ; }
349 "in" { return T_OP_IN; }
352 yylval->cpVal = apr_pstrdup(yyextra->pool, yytext + 1);
356 "-"[a-zA-Z_][a-zA-Z_0-9]+ {
357 yylval->cpVal = apr_pstrdup(yyextra->pool, yytext + 1);
364 "true" { return T_TRUE; }
365 "false" { return T_FALSE; }
371 yylval->cpVal = apr_pstrdup(yyextra->pool, yytext);
378 [a-zA-Z][a-zA-Z0-9_]* {
379 yylval->cpVal = apr_pstrdup(yyextra->pool, yytext);
384 * These are parts of the grammar and are returned as is
391 * Anything else is an error
394 char *msg = apr_psprintf(yyextra->pool, "Parse error near '%c'", yytext[0]);