37
37
#include "upstream.h"
38
38
#include "connect-ports.h"
39
39
#include "basicauth.h"
40
+ #include "conf-tokens.h"
40
41
41
42
/*
42
43
* The configuration directives are defined in the structure below. Each
@@ -98,10 +99,13 @@ typedef int (*CONFFILE_HANDLER) (struct config_s *, const char *,
98
99
* List all the handling functions. These are defined later, but they need
99
100
* to be in-scope before the big structure below.
100
101
*/
101
- static HANDLE_FUNC (handle_nop )
102
+ static HANDLE_FUNC (handle_disabled_feature )
102
103
{
103
- return 0 ;
104
- } /* do nothing function */
104
+ fprintf (stderr , "ERROR: accessing feature that was disabled at compiletime on line %lu\n" ,
105
+ lineno );
106
+
107
+ return -1 ;
108
+ }
105
109
106
110
static HANDLE_FUNC (handle_allow );
107
111
static HANDLE_FUNC (handle_basicauth );
@@ -161,7 +165,7 @@ static void config_free_regex (void);
161
165
* do not follow the pattern above. This macro is for convenience
162
166
* only.
163
167
*/
164
- #define STDCONF (d , re , func ) { BEGIN "(" #d " )" WS re END, func, NULL }
168
+ #define STDCONF (d , re , func ) [CD_ ## d] = { BEGIN "()" WS re END, func, NULL }
165
169
166
170
/*
167
171
* Holds the regular expression used to match the configuration directive,
@@ -174,14 +178,6 @@ struct {
174
178
CONFFILE_HANDLER handler ;
175
179
regex_t * cre ;
176
180
} directives [] = {
177
- /* comments */
178
- {
179
- BEGIN "#" , handle_nop , NULL
180
- },
181
- /* blank lines */
182
- {
183
- "^[[:space:]]+$" , handle_nop , NULL
184
- },
185
181
/* string arguments */
186
182
STDCONF (logfile , STR , handle_logfile ),
187
183
STDCONF (pidfile , STR , handle_pidfile ),
@@ -326,20 +322,25 @@ void free_config (struct config_s *conf)
326
322
}
327
323
328
324
/*
325
+ * Initializes Config parser. Currently this means:
329
326
* Compiles the regular expressions used by the configuration file. This
330
327
* routine MUST be called before trying to parse the configuration file.
331
328
*
332
329
* Returns 0 on success; negative upon failure.
333
330
*/
334
331
int
335
- config_compile_regex (void )
332
+ config_init (void )
336
333
{
337
334
unsigned int i , r ;
338
335
339
336
for (i = 0 ; i != ndirectives ; ++ i ) {
340
- assert (directives [i ].handler );
341
337
assert (!directives [i ].cre );
342
338
339
+ if (!directives [i ].handler ) {
340
+ directives [i ].handler = handle_disabled_feature ;
341
+ continue ;
342
+ }
343
+
343
344
directives [i ].cre = (regex_t * ) safemalloc (sizeof (regex_t ));
344
345
if (!directives [i ].cre )
345
346
return -1 ;
@@ -383,20 +384,17 @@ config_free_regex (void)
383
384
* a negative number is returned.
384
385
*/
385
386
static int check_match (struct config_s * conf , const char * line ,
386
- unsigned long lineno )
387
+ unsigned long lineno , enum config_directive cd )
387
388
{
388
389
regmatch_t match [RE_MAX_MATCHES ];
389
- unsigned int i ;
390
-
391
- assert (ndirectives > 0 );
390
+ unsigned int i = cd ;
392
391
393
- for (i = 0 ; i != ndirectives ; ++ i ) {
394
- assert (directives [i ].cre );
395
- if (!regexec
396
- (directives [i ].cre , line , RE_MAX_MATCHES , match , 0 ))
397
- return (* directives [i ].handler ) (conf , line , lineno , match );
398
- }
392
+ if (!directives [i ].cre )
393
+ return (* directives [i ].handler ) (conf , line , lineno , match );
399
394
395
+ if (!regexec
396
+ (directives [i ].cre , line , RE_MAX_MATCHES , match , 0 ))
397
+ return (* directives [i ].handler ) (conf , line , lineno , match );
400
398
return -1 ;
401
399
}
402
400
@@ -405,15 +403,25 @@ static int check_match (struct config_s *conf, const char *line,
405
403
*/
406
404
static int config_parse (struct config_s * conf , FILE * f )
407
405
{
408
- char buffer [LINE_MAX ];
406
+ char buffer [LINE_MAX ], * p , * q , c ;
407
+ const struct config_directive_entry * e ;
409
408
unsigned long lineno = 1 ;
410
409
411
- while (fgets (buffer , sizeof (buffer ), f )) {
412
- if (check_match (conf , buffer , lineno )) {
413
- printf ("Syntax error on line %ld\n" , lineno );
410
+ for (;fgets (buffer , sizeof (buffer ), f );++ lineno ) {
411
+ if (buffer [0 ] == '#' ) continue ;
412
+ p = buffer ;
413
+ while (isspace (* p ))p ++ ;
414
+ if (!* p ) continue ;
415
+ q = p ;
416
+ while (!isspace (* q ))q ++ ;
417
+ c = * q ;
418
+ * q = 0 ;
419
+ e = config_directive_find (p , strlen (p ));
420
+ * q = c ;
421
+ if (!e || e -> value == CD_NIL || check_match (conf , q , lineno , e -> value )) {
422
+ fprintf (stderr , "ERROR: Syntax error on line %lu\n" , lineno );
414
423
return 1 ;
415
424
}
416
- ++ lineno ;
417
425
}
418
426
return 0 ;
419
427
}
0 commit comments