1 /* $NetBSD: sel-lex.l,v 1.5 2023/06/19 21:41:44 christos Exp $ */
2
3 %{
4 /*
5 * Copyright (c) 2004 - 2017 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of the Institute nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 /* Id */
38
39 #ifdef __GNUC__
40 #pragma GCC diagnostic ignored "-Wunused-function"
41 #endif
42
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #undef ECHO
49
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #include "sel.h"
55 #include "sel-gram.h"
56 unsigned lineno = 1;
57
58 static char * handle_string(void);
59 static int lex_input(char *, int);
60
61 struct hx_expr_input _hx509_expr_input;
62
63 #ifndef YY_NULL
64 #define YY_NULL 0
65 #endif
66
67 #define YY_NO_UNPUT 1
68
69 #undef YY_INPUT
70 #define YY_INPUT(buf,res,maxsize) (res = lex_input(buf, maxsize))
71
72 #undef ECHO
73
74 %}
75
76 %option noyywrap
77 %%
78
79 TRUE { return kw_TRUE; }
80 FALSE { return kw_FALSE; }
81 AND { return kw_AND; }
82 OR { return kw_OR; }
83 IN { return kw_IN; }
84 TAILMATCH { return kw_TAILMATCH; }
85
86 [A-Za-z][-A-Za-z0-9_]* {
87 _hx509_sel_yylval.string = strdup ((const char *)_hx509_sel_yytext);
88 return IDENTIFIER;
89 }
90 "\"" { _hx509_sel_yylval.string = handle_string(); return STRING; }
91 \n { ++lineno; }
92 [,.!={}()%] { return *_hx509_sel_yytext; }
93 [ \t] ;
94 %%
95
96 static char *
97 handle_string(void)
98 {
99 char x[1024];
100 int i = 0;
101 int c;
102 int quote = 0;
103 while((c = input()) != EOF){
104 if(quote) {
105 x[i++] = '\\';
106 x[i++] = c;
107 quote = 0;
108 continue;
109 }
110 if(c == '\n'){
111 _hx509_sel_yyerror("unterminated string");
112 lineno++;
113 break;
114 }
115 if(c == '\\'){
116 quote++;
117 continue;
118 }
119 if(c == '\"')
120 break;
121 x[i++] = c;
122 }
123 x[i] = '\0';
124 return strdup(x);
125 }
126
127 static int
lex_input(char * buf,int max_size)128 lex_input(char *buf, int max_size)
129 {
130 int n;
131
132 n = _hx509_expr_input.length - _hx509_expr_input.offset;
133 if (max_size < n)
134 n = max_size;
135 if (n <= 0)
136 return YY_NULL;
137
138 memcpy(buf, _hx509_expr_input.buf + _hx509_expr_input.offset, n);
139 _hx509_expr_input.offset += n;
140
141 return n;
142 }
143