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