1/* $NetBSD: token.l,v 1.1 2010/05/07 17:41:58 degroote Exp $ */ 2 3/*- 4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 30%{ 31#include <sys/cdefs.h> 32 33#ifndef lint 34__RCSID("$NetBSD: token.l,v 1.1 2010/05/07 17:41:58 degroote Exp $"); 35#endif 36 37#include <stdlib.h> 38#include <limits.h> 39#include <inttypes.h> 40#include <string.h> 41#include <errno.h> 42 43#include <net/if.h> 44#include <netinet/in.h> 45#include <net/pfvar.h> 46 47#include "parse.h" 48#include "parser.h" 49 50%} 51 52%option nounput 53%option noinput 54 55%% 56 57state { return STATE;} 58on { return ON;} 59out { return OUT;} 60in { return IN;} 61proto { return PROTO;} 62from { return FROM;} 63to { return TO;} 64using { return USING;} 65id { return ID;} 66cid { return CID;} 67expire { return EXPIRE;} 68timeout { return TIMEOUT;} 69src { return SRC;} 70dst { return DST;} 71seq { return SEQ;} 72max_win { return MAX_WIN;} 73wscale { return WSCALE;} 74mss { return MSS;} 75no-scrub { return NOSCRUB;} 76scrub { return SCRUB;} 77flags { return FLAGS;} 78ttl { return TTL;} 79mode { return MODE;} 80[0-9]+ { char *ep; 81 errno = 0; 82 yylval.num = strtoumax(yytext, &ep, 10); 83 if (errno == ERANGE && yylval.num == UINTMAX_MAX) 84 yyfatal("Number out of range"); 85 return NUMBER; 86 } 87 88[A-Za-z0-9:\[][A-Za-z0-9\[\]_:%\.-]* { yylval.str = strdup(yytext); 89 if (yylval.str == NULL) 90 yyfatal("Not enough memory"); 91 return STRING; 92 } 93 94 95\n { lineno ++; } 96 97%% 98 99 100void 101yyfatal(const char *s) 102{ 103 yyerror(s); 104 exit(EXIT_FAILURE); 105} 106 107void 108yyerror(const char *s) 109{ 110 printf("line %d: %s at [%s]\n", lineno, s, yytext); 111} 112 113 114int 115parse(FILE *fp, struct pfioc_states* s) 116{ 117 yyin = fp; 118 119 lineno = 1; 120 121 states = s; 122 allocated = 0; 123 memset(s, 0, sizeof(*s)); 124 125 if (yyparse()) { 126 printf("parse failed, line %d.\n", lineno); 127 return(-1); 128 } 129 130 return(0); 131} 132