1 /* $NetBSD: sun_map_tok.l,v 1.3 2009/03/20 20:30:52 christos 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 149 /* allocate more output slots so lex scanners don't run out of mem */ 150 %o 1024 151 152 WORD_REX [A-Za-z0-9_/&\.$=]+[A-Za-z0-9_/&\.$=-]* 153 COMMENT_REX ^#.*\n 154 WSPACE_REX [ \t]* 155 NEWLINE_REX [ \t]*\n 156 CONTINUE_REX "\\"\n 157 158 %% 159 160 {WORD_REX} { 161 sun_map_tokpos += yyleng; 162 xstrlcpy((char *)yylval.strval,(const char *)yytext,sizeof(yylval.strval)); 163 return WORD; 164 } 165 166 {WSPACE_REX} { 167 sun_map_tokpos += yyleng; 168 return WSPACE; 169 } 170 171 {NEWLINE_REX} { 172 sun_map_tokpos = 0; 173 sun_map_line++; 174 return NEWLINE; 175 } 176 177 {CONTINUE_REX} { 178 sun_map_tokpos = 0; 179 sun_map_line++; 180 } 181 182 {COMMENT_REX} { 183 sun_map_line++; 184 } 185 186 . { 187 return yytext[0]; 188 } 189 190 %% 191 192 193 int 194 yyerror(const char* s) 195 { 196 return 1; 197 } 198 199 #ifdef FLEX_SCANNER 200 void 201 sun_map_tok_setbuff(const char* buff) 202 { 203 sun_map_tok_end = buff + strlen(buff); 204 sun_map_tok_pos = buff; 205 sun_map_tok_buff = buff; 206 } 207 208 209 int 210 sun_map_input(char *buff, int maxsize) 211 { 212 int size = MIN(maxsize, (sun_map_tok_end - sun_map_tok_pos)); 213 if (size > 0) { 214 memcpy(buff,sun_map_tok_pos,size); 215 sun_map_tok_pos += size; 216 } 217 218 return size; 219 } 220 #else 221 void 222 sun_map_tok_setbuff(const char* buff) 223 { 224 sun_map_tok_buff = buff; 225 } 226 227 #endif /* FLEX_SCANNER */ 228 229 /* 230 * some systems such as DU-4.x have a different GNU flex in /usr/bin 231 * which automatically generates yywrap macros and symbols. So I must 232 * distinguish between them and when yywrap is actually needed. 233 */ 234 #if !defined(yywrap) || defined(yylex) 235 int yywrap(void) 236 { 237 return 1; 238 } 239 #endif /* not yywrap or yylex */ 240