sync
[bitrig.git] / bin / systrace / lex.l
blob3d5511d2dcfe76be4699efd0d5d7ec624341fa0c
1 /*      $OpenBSD: lex.l,v 1.18 2006/07/02 12:34:15 sturm Exp $  */
3 /*
4  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Niels Provos.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 %x quote
35 #include <sys/types.h>
36 #include <sys/tree.h>
38 #include <sys/time.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <err.h>
43 #include <stdarg.h>
44 #include <string.h>
46 #include "intercept.h"
47 #include "systrace.h"
48 #include "y.tab.h"
50 extern int yyerror(const char *fmt, ...);
51 int yylex(void);
53 char *mystring;
54 int myoff;
56 char quotestr[8192];
57 int quoteescape;
59 #define YY_INPUT(buf,result,max_size) \
60 { \
61         int len = strlen(mystring + myoff); \
62         if (max_size < len) \
63                 len = max_size; \
64         if (len == 0) \
65                 result = YY_NULL; \
66         else { \
67                 memcpy(buf, mystring + myoff, len); \
68                 myoff += len; \
69                 result = len; \
70         } \
75 ask             { return ASK; }
76 deny            { return DENY; }
77 permit          { return PERMIT; }
78 and             { return AND; }
79 or              { return OR; }
80 not             { return NOT; }
81 match           { return MATCH; }
82 then            { return THEN; }
83 eq              { return EQ; }
84 neq             { return NEQ; }
85 sub             { return SUB; }
86 nsub            { return NSUB; }
87 inpath          { return INPATH; }
88 re              { return RE; }
89 log             { return LOG; }
90 true            { return TRUE; }
91 "->"            { return THEN; }
92 \(              { return LBRACE; }
93 \)              { return RBRACE; }
94 if              { return IF; }
95 user            { return USER; }
96 group           { return GROUP; }
97 as              { return AS; }
98 ":"             { return COLON; }
99 ","             { return COMMA; }
100 "="             { return EQUAL; }
101 "!="            { return NEQUAL; }
102 "<"             { return LESSER; }
103 ">"             { return GREATER; }
104 [\_\$A-Za-z][\.\(\)\/A-Za-z_\-0-9]*\$? { yylval.string = strdup(yytext); return STRING; }
105 [0-9]+          { yylval.number = atoi(yytext); return NUMBER; }
106 \"              { BEGIN(quote);
107                   *quotestr = '\0';
108                   quoteescape = 0;
109                 }
110 <quote>[^\\\"]+ {
111                   if (quoteescape) {
112                           switch (yytext[0]) {
113                           case 'n':
114                                   yytext[0] = '\n';
115                                   break;
116                           case 'r':
117                                   yytext[0] = '\r';
118                                   break;
119                           default:
120                                   break;
121                           }
122                   }
123                   strlcat(quotestr, yytext, sizeof(quotestr));
124                   quoteescape = 0;
125                 }
126 <quote>\\       {
127                   if (!quoteescape)
128                      quoteescape = 1;
129                   else {
130                      strlcat(quotestr, "\\", sizeof(quotestr));
131                      quoteescape = 0;
132                   }
133                 }
134 <quote>\"       {
135                   if (!quoteescape) {
136                         BEGIN(INITIAL);
137                         yylval.string = strdup(quotestr);
138                         return CMDSTRING;
139                   } else {
140                         strlcat(quotestr, "\"", sizeof(quotestr));
141                         quoteescape = 0;
142                   }
143                 }
144 \[              { return LSQBRACE; }
145 \]              { return RSQBRACE; }
146 \               { ; }
147 \n              { ; }
148 \t              { ; }
149 "#".*\n         { ; }
150 .               { yyerror("illegal token"); }
152 #ifndef yywrap
153 int yywrap() { return 1; }
154 #endif