chiark / gitweb /
wip
[userv.git] / lexer.l.m4
1 dnl  userv - lexer.l.m4
2 dnl  lexer, passed through m4 with defs from langauge.i4
3 /*
4  *   Copyright (C)1996-1997,1999 Ian Jackson
5  *  
6  *   This is free software; you can redistribute it and/or modify it
7  *   under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *  
11  *   This program is distributed in the hope that it will be useful, but
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *   General Public License for more details.
15  *  
16  *   You should have received a copy of the GNU General Public License
17  *   along with userv; if not, write to the Free Software
18  *   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 %{
22 include(language.i4)
23
24 #include <syslog.h>
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <fnmatch.h>
34 #include <limits.h>
35 #include <dirent.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <time.h>
39 #include <errno.h>
40
41 #include "config.h"
42 #include "common.h"
43 #include "daemon.h"
44 #include "lib.h"
45 #include "both.h"
46 #include "tokens.h"
47
48 #define HYPHEN '-'
49
50 typedef int directive_fnt(int dtoken);
51 static directive_fnt df_reject, df_execute, df_executefrompath;
52 static directive_fnt df_executefromdirectory, df_executebuiltin;
53 static directive_fnt df_errorstostderr, df_errorstosyslog, df_errorstofile;
54 static directive_fnt dfg_fdwant, dfg_setflag;
55 static directive_fnt df_reset, df_cd, df_userrcfile, df_include;
56 static directive_fnt df_includelookup, df_includedirectory;
57 static directive_fnt df_message, df_error, df_quit, df_eof;
58 static directive_fnt df_if, df_catchquit, df_errorspush;
59 static directive_fnt dfi_includeuserrcfile, dfi_includeclientconfig;
60 /* directive functions return:
61  *  0 for success having scanned up to and including end of line but not beyond,
62  *  or tokv_error or tokv_quit.
63  * They expect to parse the whitespace before their parameters (if any).
64  */
65
66 typedef int parmcondition_fnt(int ctoken, char *const *parmvalues, int *rtrue);
67 static parmcondition_fnt pcf_glob, pcf_range, pcf_grep;
68 /* all conditional functions return tokv_error for failure or 0 for success
69  *  at parsing and testing, in which case *rtrue is set to 0 or 1.
70  *  On success they have scanned up to and including the condition's
71  *  terminating newline; the pcf_... functions expect to parse the whitespace
72  *  between the parameter name and the condition's arguments.
73  * Otherwise they return tokv_error.
74  * The parameter-based conditionals take a list of parameter values
75  * as obtained from the parameter functions and pa_parameter,
76  * and do _not_ free it.
77  */
78
79 typedef int parameter_fnt(int ptoken, char ***rvalues);
80 static parameter_fnt pf_service;
81 static parameter_fnt pf_callinguser, pf_serviceuser;
82 static parameter_fnt pf_callinggroup, pf_servicegroup;
83 static parameter_fnt pf_callingusershell, pf_serviceusershell;
84 /* Parameter functions return tokv_error or 0 for success at parsing
85  * and determining the value, in which case *rvalues is made to be
86  * a mallocd null-terminated array of pointers to mallocd strings.
87  * freeparm can be used to free such an array.
88  */
89
90 typedef int builtinserviceparse_fnt(char ***rnewargs);
91 static builtinserviceparse_fnt bispa_none, bispa_parameter;
92 /* These parse the arguments to a builtin service, including the
93  * newline at the end of the line.  *rnewargs will initially be
94  * null, indicating that no arguments are to be set; the function
95  * may store a mallocd array of mallocd strings in it,
96  * containing the arguments it wishes to have set (null-pointer
97  * terminated).
98  */
99
100 static int yylex(void);
101 /* Returns a token (which may be an eof or error exception) */
102
103 static directive_fnt *lr_dir;
104 static parmcondition_fnt *lr_parmcond;
105 static builtinserviceparse_fnt *lr_bispa;
106 static builtinserviceexec_fnt *lr_bisexec;
107 static parameter_fnt *lr_parameter;
108 static int lr_loglevel, lr_logfacility, lr_min, lr_max, *lr_flag;
109 static int lr_flagval, lr_controlend;
110 static int lr_fdwant_readwrite; /* -1=never, 0=opt, 1=always */
111
112 /* Forward declarations of things used in lexer and parser */
113
114 struct parser_state {
115   int lineno, reportlineno, notedreferer, isinternal;
116   const char *filename;
117   struct stat filestab;
118   YY_BUFFER_STATE ybuf;
119   struct parser_state *upstate;
120 };
121
122 static struct parser_state *cstate;
123
124 struct error_handling {
125   int handling; /* One of the error handling modes tokt_ehandlemode */
126   int logfacility, loglevel;
127   int filekeep; /* File is in use by higher-level errors-push, leave it open */
128   FILE *file;
129   char *filename;
130 };
131
132 static struct error_handling eh = { tokv_word_errorstostderr, 0,0,0,0,0 };
133
134 static int dequote(char *inplace);
135 static void countnewlines(void);
136
137 #define YY_NO_UNPUT
138
139 %}
140
141 %option noyywrap
142
143 %%
144
145 dnl simple words
146 undivert(3)
147 changequote({*,*})
148 {*
149 [0-9]{1,8}              {
150                           char *ep;
151                           lr_min=lr_max= (int)strtoul(yytext,&ep,10);
152                           assert(!*ep);
153                           return tokv_ordinal;
154                         }
155 [0-9]{1,8}-[0-9]{1,8}   {
156                           char *ep;
157                           lr_min= (int)strtoul(yytext,&ep,10);
158                           assert(*ep == HYPHEN);
159                           ep++;  assert(*ep);
160                           lr_max= (int)strtoul(ep,&ep,10);
161                           assert(!*ep);
162                           if (lr_max < lr_min)
163                             return parseerrprint("fd range has min > max");
164                           return tokv_fdrange;
165                         }
166 [0-9]{1,8}-             {
167                           char *ep;
168                           lr_min= (int)strtoul(yytext,&ep,10);
169                           assert(*ep == HYPHEN);
170                           ep++;  assert(!*ep);
171                           lr_max=-1;
172                           return tokv_fdstoend;
173                         }
174 ([\ \t]*\\[\ \t]*\n[\ \t]*)+    countnewlines(); return tokv_lwsp;
175 [\ \t]+                         return tokv_lwsp;
176 [\ \t]*\n               cstate->lineno++; return tokv_newline;
177 [\ \t]*\#[^\n]*\n       cstate->lineno++; return tokv_newline;
178 [\ \t]*\#[^\n]*         return parseerrprint("missing newline at eof after comment");
179 \"([^\\\"\n]|\\[a-z]|\\[0-9]{3}|\\x[0-9A-Fa-f]{2}|\\[[:punct:]]|\\[ \t]*\n)*\" {
180                           countnewlines();
181                           return dequote(yytext);
182                         }
183 [^\ \t\n\\\"]+          return tokv_barestring;
184 <<EOF>>                 return tokv_eof;
185 \"                      return parseerrprint("misquoted or unterminated string");
186 \\                      return parseerrprint("unexpected backslash");
187 .                       abort(); /* expect lex warning "rule cannot be matched" */
188 *}
189 changequote(`,')
190 %%
191
192 const char *const builtinservicehelpstrings[]= {
193 undivert(5)dnl
194    0
195 };
196 `
197 #include "parser.c"
198 '
199 divert(-1)
200 undivert