1 /* $NetBSD: sun_map_tok.l,v 1.5 2015/01/17 17:46:31 christos Exp $ */
2
3 %{
4 /*
5 * Copyright (c) 1997-2014 Erez Zadok
6 * Copyright (c) 2005 Daniel P. Ottavio
7 * Copyright (c) 1990 Jan-Simon Pendry
8 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
9 * Copyright (c) 1990 The Regents of the University of California.
10 * All rights reserved.
11 *
12 * This code is derived from software contributed to Berkeley by
13 * Jan-Simon Pendry at Imperial College, London.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *
40 * File: am-utils/amd/sun_map_tok.l
41 *
42 */
43
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif /* HAVE_CONFIG_H */
47 /*
48 * Some systems include a definition for the macro ECHO in <sys/ioctl.h>,
49 * and their (bad) version of lex defines it too at the very beginning of
50 * the generated lex.yy.c file (before it can be easily undefined),
51 * resulting in a conflict. So undefine it here before needed.
52 * Luckily, it does not appear that this macro is actually used in the rest
53 * of the generated lex.yy.c file.
54 */
55 #ifdef ECHO
56 # undef ECHO
57 #endif /* ECHO */
58 #include <am_defs.h>
59 #include <amd.h>
60 #include <sun_map_parse.h>
61 /* and once again undefine this, just in case */
62 #ifdef ECHO
63 # undef ECHO
64 #endif /* ECHO */
65
66 /*
67 * There are some things that need to be defined only if using GNU flex.
68 * These must not be defined if using standard lex
69 */
70 #ifdef FLEX_SCANNER
71 # ifndef ECHO
72 # define ECHO __IGNORE(fwrite( yytext, yyleng, 1, yyout ))
73 # endif /* not ECHO */
74 #endif /* FLEX_SCANNER */
75
76 int yylex(void);
77 int sun_map_error(const char *);
78
79 /*
80 * We need to configure lex to parse from a string
81 * instead of a file. Each version of lex has it's
82 * own way of doing this (sigh).
83 */
84
85 /* assign the buffer to parse */
86 void sun_map_tok_setbuff(const char* buff);
87
88 /* buffer that contains the string to parse */
89 const char *sun_map_tok_buff = NULL;
90
91 #ifdef FLEX_SCANNER
92 /*
93 * The flex scanner uses the YY_INPUT to parse the input.
94 * We need to redefine it so that it can parse strings.
95 * In addition to the above string buffer we need to have
96 * a position pointer and a end pointer.
97 */
98
99 /* current position of the buffer */
100 const char *sun_map_tok_pos = NULL;
101
102 /* size of the buffer */
103 const char *sun_map_tok_end = NULL;
104
105 /* copies the current position + maxsize into buff */
106 int sun_map_input(char *buff, int maxsize);
107
108 # undef YY_INPUT
109 # define YY_INPUT(buff,result,maxsize) (result = sun_map_input(buff,maxsize))
110
111 #else
112 /*
113 * If this is not Flex than fall back to an AT&T style lex.
114 * We can parse strings by redefining input and unput.
115 */
116 #undef input
117 #undef unput
118 #define input() (*(char *)sun_map_tok_buff++)
119 #define unput(c) (*(char *)--sun_map_tok_buff = c)
120
121 #endif /* FLEX_SCANNER */
122
123 /*
124 * some systems such as DU-4.x have a different GNU flex in /usr/bin
125 * which automatically generates yywrap macros and symbols. So I must
126 * distinguish between them and when yywrap is actually needed.
127 */
128 #if !defined(yywrap) || defined(yylex)
129 int yywrap(void);
130 #endif /* not yywrap or yylex */
131
132 /* no need to use yywrap() */
133 #define YY_SKIP_YYWRAP
134
135 int sun_map_line = 1;
136 int sun_map_tokpos = 1;
137
138 %}
139
140 /* This option causes Solaris lex to fail. Use flex. See BUGS file */
141 /* no need to use yyunput() */
142 %option nounput
143 %option noinput
144
145 /* allocate more output slots so lex scanners don't run out of mem */
146 %o 1024
147
148 WORD_REX [A-Za-z0-9_/&\.$=]+[A-Za-z0-9_/&\.$=-]*
149 COMMENT_REX ^#.*\n
150 WSPACE_REX [ \t]*
151 NEWLINE_REX [ \t]*\n
152 CONTINUE_REX "\\"\n
153
154 %%
155
156 {WORD_REX} {
157 sun_map_tokpos += yyleng;
158 xstrlcpy((char *)sun_map_lval.strval,(const char *)yytext,sizeof(sun_map_lval.strval));
159 return WORD;
160 }
161
162 {WSPACE_REX} {
163 sun_map_tokpos += yyleng;
164 return WSPACE;
165 }
166
167 {NEWLINE_REX} {
168 sun_map_tokpos = 0;
169 sun_map_line++;
170 return NEWLINE;
171 }
172
173 {CONTINUE_REX} {
174 sun_map_tokpos = 0;
175 sun_map_line++;
176 }
177
178 {COMMENT_REX} {
179 sun_map_line++;
180 }
181
182 . {
183 return yytext[0];
184 }
185
186 %%
187
188
189 int
190 sun_map_error(const char* s)
191 {
192 return 1;
193 }
194
195 #ifdef FLEX_SCANNER
196 void
sun_map_tok_setbuff(const char * buff)197 sun_map_tok_setbuff(const char* buff)
198 {
199 sun_map_tok_end = buff + strlen(buff);
200 sun_map_tok_pos = buff;
201 sun_map_tok_buff = buff;
202 }
203
204
205 int
sun_map_input(char * buff,int maxsize)206 sun_map_input(char *buff, int maxsize)
207 {
208 int size = MIN(maxsize, (sun_map_tok_end - sun_map_tok_pos));
209 if (size > 0) {
210 memcpy(buff,sun_map_tok_pos,size);
211 sun_map_tok_pos += size;
212 }
213
214 return size;
215 }
216 #else
217 void
sun_map_tok_setbuff(const char * buff)218 sun_map_tok_setbuff(const char* buff)
219 {
220 sun_map_tok_buff = buff;
221 }
222
223 #endif /* FLEX_SCANNER */
224
225 /*
226 * some systems such as DU-4.x have a different GNU flex in /usr/bin
227 * which automatically generates yywrap macros and symbols. So I must
228 * distinguish between them and when yywrap is actually needed.
229 */
230 #if !defined(yywrap) || defined(yylex)
yywrap(void)231 int yywrap(void)
232 {
233 return 1;
234 }
235 #endif /* not yywrap or yylex */
236