xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hx509/sel-lex.l (revision 7863ba460b0a05b553c754e5dbc29247dddec322)
1 /*	$NetBSD: sel-lex.l,v 1.2 2017/01/28 21:31:48 christos Exp $	*/
2 
3 %{
4 /*
5  * Copyright (c) 2004, 2008 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 
77 TRUE			{ return kw_TRUE; }
78 FALSE			{ return kw_FALSE; }
79 AND			{ return kw_AND; }
80 OR			{ return kw_OR; }
81 IN			{ return kw_IN; }
82 TAILMATCH		{ return kw_TAILMATCH; }
83 
84 [A-Za-z][-A-Za-z0-9_]*	{
85 			  yylval.string = strdup ((const char *)yytext);
86 			  return IDENTIFIER;
87 			}
88 "\""			{ yylval.string = handle_string(); return STRING; }
89 \n			{ ++lineno; }
90 [,.!={}()%]		{ return *yytext; }
91 [ \t]			;
92 %%
93 
94 static char *
95 handle_string(void)
96 {
97     char x[1024];
98     int i = 0;
99     int c;
100     int quote = 0;
101     while((c = input()) != EOF){
102 	if(quote) {
103 	    x[i++] = '\\';
104 	    x[i++] = c;
105 	    quote = 0;
106 	    continue;
107 	}
108 	if(c == '\n'){
109 	    _hx509_sel_yyerror("unterminated string");
110 	    lineno++;
111 	    break;
112 	}
113 	if(c == '\\'){
114 	    quote++;
115 	    continue;
116 	}
117 	if(c == '\"')
118 	    break;
119 	x[i++] = c;
120     }
121     x[i] = '\0';
122     return strdup(x);
123 }
124 
125 int
126 yywrap ()
127 {
128      return 1;
129 }
130 
131 static int
132 lex_input(char *buf, int max_size)
133 {
134     int n;
135 
136     n = _hx509_expr_input.length - _hx509_expr_input.offset;
137     if (max_size < n)
138         n = max_size;
139     if (n <= 0)
140 	return YY_NULL;
141 
142     memcpy(buf, _hx509_expr_input.buf + _hx509_expr_input.offset, n);
143     _hx509_expr_input.offset += n;
144 
145     return n;
146 }
147