1 /* $NetBSD: sel-lex.l,v 1.1.1.3 2014/04/24 12:45:42 pettai 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 HAVE_CONFIG_H 40 #include <config.h> 41 #endif 42 43 #undef ECHO 44 45 #include <stdio.h> 46 #include <string.h> 47 #include <stdarg.h> 48 #include <stdlib.h> 49 #include "sel.h" 50 #include "sel-gram.h" 51 unsigned lineno = 1; 52 53 static char * handle_string(void); 54 static int lex_input(char *, int); 55 static int lex_classic_input(void); 56 57 struct hx_expr_input _hx509_expr_input; 58 59 #ifndef YY_NULL 60 #define YY_NULL 0 61 #endif 62 63 #define YY_NO_UNPUT 1 64 65 #undef YY_INPUT 66 #define YY_INPUT(buf,res,maxsize) (res = lex_input(buf, maxsize)) 67 68 #undef ECHO 69 70 %} 71 %% 72 73 TRUE { return kw_TRUE; } 74 FALSE { return kw_FALSE; } 75 AND { return kw_AND; } 76 OR { return kw_OR; } 77 IN { return kw_IN; } 78 TAILMATCH { return kw_TAILMATCH; } 79 80 [A-Za-z][-A-Za-z0-9_]* { 81 yylval.string = strdup ((const char *)yytext); 82 return IDENTIFIER; 83 } 84 "\"" { yylval.string = handle_string(); return STRING; } 85 \n { ++lineno; } 86 [,.!={}()%] { return *yytext; } 87 [ \t] ; 88 %% 89 90 static char * 91 handle_string(void) 92 { 93 char x[1024]; 94 int i = 0; 95 int c; 96 int quote = 0; 97 while((c = input()) != EOF){ 98 if(quote) { 99 x[i++] = '\\'; 100 x[i++] = c; 101 quote = 0; 102 continue; 103 } 104 if(c == '\n'){ 105 _hx509_sel_yyerror("unterminated string"); 106 lineno++; 107 break; 108 } 109 if(c == '\\'){ 110 quote++; 111 continue; 112 } 113 if(c == '\"') 114 break; 115 x[i++] = c; 116 } 117 x[i] = '\0'; 118 return strdup(x); 119 } 120 121 int 122 yywrap () 123 { 124 return 1; 125 } 126 127 static int 128 lex_input(char *buf, int max_size) 129 { 130 int n; 131 132 n = _hx509_expr_input.length - _hx509_expr_input.offset; 133 if (max_size < n) 134 n = max_size; 135 if (n <= 0) 136 return YY_NULL; 137 138 memcpy(buf, _hx509_expr_input.buf + _hx509_expr_input.offset, n); 139 _hx509_expr_input.offset += n; 140 141 return n; 142 } 143