1 /* $NetBSD: conf_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) 1989 Jan-Simon Pendry 7 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 8 * Copyright (c) 1989 The Regents of the University of California. 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * Jan-Simon Pendry at Imperial College, London. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * 39 * File: am-utils/amd/conf_tok.l 40 * 41 */ 42 43 /* 44 * Lexical analyzer for AMD configuration parser. 45 */ 46 47 #ifdef HAVE_CONFIG_H 48 # include <config.h> 49 #endif /* HAVE_CONFIG_H */ 50 /* 51 * Some systems include a definition for the macro ECHO in <sys/ioctl.h>, 52 * and their (bad) version of lex defines it too at the very beginning of 53 * the generated lex.yy.c file (before it can be easily undefined), 54 * resulting in a conflict. So undefine it here before needed. 55 * Luckily, it does not appear that this macro is actually used in the rest 56 * of the generated lex.yy.c file. 57 */ 58 #ifdef ECHO 59 # undef ECHO 60 #endif /* ECHO */ 61 #include <am_defs.h> 62 #include <amd.h> 63 #include <conf_parse.h> 64 /* and once again undefine this, just in case */ 65 #ifdef ECHO 66 # undef ECHO 67 #endif /* ECHO */ 68 69 /* 70 * There are some things that need to be defined only if using GNU flex. 71 * These must not be defined if using standard lex 72 */ 73 #ifdef FLEX_SCANNER 74 # ifndef ECHO 75 # define ECHO __IGNORE(fwrite( yytext, yyleng, 1, yyout )) 76 # endif /* not ECHO */ 77 #endif /* FLEX_SCANNER */ 78 79 int ayylineno = 0; 80 81 int yylex(void); 82 /* 83 * some systems such as DU-4.x have a different GNU flex in /usr/bin 84 * which automatically generates yywrap macros and symbols. So I must 85 * distinguish between them and when yywrap is actually needed. 86 */ 87 #if !defined(yywrap) || defined(yylex) 88 int yywrap(void); 89 #endif /* not yywrap or yylex */ 90 91 #define TOK_DEBUG 0 92 93 #if TOK_DEBUG 94 # define dprintf(f,s) fprintf(stderr, (f), ayylineno, (s)) 95 # define amu_return(v) 96 #else /* not TOK_DEBUG */ 97 # define dprintf(f,s) 98 # define amu_return(v) return((v)) 99 #endif /* not TOK_DEBUG */ 100 101 /* no need to use yywrap() */ 102 #define YY_SKIP_YYWRAP 103 104 %} 105 106 /* This option causes Solaris lex to fail. Use flex. See BUGS file */ 107 /* no need to use yyunput() */ 108 %option nounput 109 %option noinput 110 111 /* allocate more output slots so lex scanners don't run out of mem */ 112 %o 1024 113 114 DIGIT [0-9] 115 ALPHA [A-Za-z] 116 ALPHANUM [A-Za-z0-9] 117 SYMBOL [A-Za-z0-9_-] 118 PATH [A-Za-z0-9_-/] 119 NONWSCHAR [^ \t\n\[\]=] 120 NONWSEQCHAR [^ \t\n\[\]] 121 NONNL [^\n] 122 NONQUOTE [^\"] 123 124 %% 125 126 \n { 127 ayylineno++; 128 amu_return(NEWLINE); 129 } 130 131 \[ { 132 dprintf("%8d: Left bracket \"%s\"\n", yytext); 133 conf_lval.strtype = xstrdup(yytext); 134 amu_return(LEFT_BRACKET); 135 } 136 137 \] { 138 dprintf("%8d: Right bracket \"%s\"\n", yytext); 139 conf_lval.strtype = xstrdup(yytext); 140 amu_return(RIGHT_BRACKET); 141 } 142 143 = { 144 dprintf("%8d: Equal \"%s\"\n", yytext); 145 conf_lval.strtype = xstrdup(yytext); 146 amu_return(EQUAL); 147 } 148 149 [ \t]* { 150 dprintf("%8d: Whitespace \"%s\"\n", yytext); 151 } 152 "#"[^\n]*\n { 153 /* a comment line includes the terminating \n */ 154 ayylineno++; 155 yytext[strlen((char *)yytext)-1] = '\0'; 156 dprintf("%8d: Comment \"%s\"\n", yytext); 157 } 158 159 {NONWSCHAR}{NONWSCHAR}* { 160 dprintf("%8d: Non-WS string \"%s\"\n", yytext); 161 conf_lval.strtype = xstrdup(yytext); 162 amu_return(NONWS_STRING); 163 } 164 165 \"{NONQUOTE}{NONQUOTE}*\" { 166 dprintf("%8d: QUOTED-Non-WS-EQ string \"%s\"\n", yytext); 167 /* must strip quotes */ 168 yytext[strlen((char *)yytext)-1] = '\0'; 169 conf_lval.strtype = xstrdup(&yytext[1]); 170 amu_return(QUOTED_NONWSEQ_STRING); 171 } 172 173 {NONWSEQCHAR}{NONWSEQCHAR}* { 174 dprintf("%8d: Non-WS-EQ string \"%s\"\n", yytext); 175 conf_lval.strtype = xstrdup(yytext); 176 amu_return(NONWSEQ_STRING); 177 } 178 179 %% 180 181 /* 182 * some systems such as DU-4.x have a different GNU flex in /usr/bin 183 * which automatically generates yywrap macros and symbols. So I must 184 * distinguish between them and when yywrap is actually needed. 185 */ 186 #if !defined(yywrap) || defined(yylex) 187 int yywrap(void) 188 { 189 return 1; 190 } 191 #endif /* not yywrap or yylex */ 192