Updated flex/bison parser to take advantage of current versions of these tools.

This commit is contained in:
Peter Simons 2009-07-19 22:27:10 +02:00
parent 398afbbc5c
commit e4af1eb7fe
3 changed files with 88 additions and 77 deletions

View File

@ -1,23 +1,20 @@
%{
/* /*
$Source$ * Copyright (c) 1995-2006 Peter Simons <simons@cryp.to>
$Revision$ * Copyright (c) 2000-2001 Cable & Wireless GmbH
* Copyright (c) 1999-2000 CyberSolutions GmbH
Copyright (C) 2000 by CyberSolutions GmbH, Germany. *
* This program is free software; you can redistribute it and/or
This file is part of Petidomo. * modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
Petidomo is free software; you can redistribute it and/or modify * the License, or (at your option) any later version.
it under the terms of the GNU General Public License as published by *
the Free Software Foundation; either version 2, or (at your option) * This program is distributed in the hope that it will be
any later version. * useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Petidomo is distributed in the hope that it will be useful, but * PURPOSE. See the GNU General Public License for more details.
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/ */
%{
/* Definitions we need in the parser. */ /* Definitions we need in the parser. */
#include <stdio.h> #include <stdio.h>
@ -29,8 +26,8 @@
#include "libtext/text.h" #include "libtext/text.h"
#include "petidomo.h" #include "petidomo.h"
static int yyerror(char *); static int acl_error(char *);
static int yylex(void); static int acl_lex(void);
static int domatch(int, int, char *); static int domatch(int, int, char *);
static int dofilter(const char *); static int dofilter(const char *);
@ -39,10 +36,13 @@ int operation, g_rc;
char * g_parameter = NULL; char * g_parameter = NULL;
struct Mail * g_MailStruct; struct Mail * g_MailStruct;
#include "acl-scan.c" // Forward declarations for the scanner generated from acl-scanner.l.
extern char *acl_text;
extern FILE *acl_in;
#define YYERROR_VERBOSE #define YYERROR_VERBOSE
%} %}
%name-prefix="acl_"
%token TOK_IF TOK_EQUAL TOK_FROM TOK_SUBJECT %token TOK_IF TOK_EQUAL TOK_FROM TOK_SUBJECT
%token TOK_ENVELOPE TOK_HEADER TOK_BODY TOK_AND TOK_OR TOK_NOT %token TOK_ENVELOPE TOK_HEADER TOK_BODY TOK_AND TOK_OR TOK_NOT
%token TOK_THEN TOK_MATCH TOK_STRING TOK_DROP TOK_PASS TOK_APPROVE %token TOK_THEN TOK_MATCH TOK_STRING TOK_DROP TOK_PASS TOK_APPROVE
@ -66,19 +66,19 @@ statmt: ';'
; ;
exp: qualifier TOK_EQUAL TOK_STRING { exp: qualifier TOK_EQUAL TOK_STRING {
g_rc = domatch($1, TOK_EQUAL, yytext); g_rc = domatch($1, TOK_EQUAL, acl_text);
if (g_rc == -1) if (g_rc == -1)
YYABORT; YYABORT;
$$ = g_rc; $$ = g_rc;
} }
| qualifier TOK_MATCH TOK_STRING { | qualifier TOK_MATCH TOK_STRING {
g_rc = domatch($1, TOK_MATCH, yytext); g_rc = domatch($1, TOK_MATCH, acl_text);
if (g_rc == -1) if (g_rc == -1)
YYABORT; YYABORT;
$$ = g_rc; $$ = g_rc;
} }
| TOK_STRING { | TOK_STRING {
g_rc = dofilter(yytext); g_rc = dofilter(acl_text);
if (g_rc == -1) if (g_rc == -1)
YYABORT; YYABORT;
$$ = g_rc; $$ = g_rc;
@ -104,7 +104,7 @@ action: TOK_PASS { $$ = ACL_PASS; }
$$ = ACL_REJECTWITH; $$ = ACL_REJECTWITH;
if (g_parameter != NULL) if (g_parameter != NULL)
free(g_parameter); free(g_parameter);
g_parameter = strdup(yytext); g_parameter = strdup(acl_text);
if (g_parameter == NULL) if (g_parameter == NULL)
YYABORT; YYABORT;
} }
@ -112,7 +112,7 @@ action: TOK_PASS { $$ = ACL_PASS; }
$$ = ACL_REDIRECT; $$ = ACL_REDIRECT;
if (g_parameter != NULL) if (g_parameter != NULL)
free(g_parameter); free(g_parameter);
g_parameter = strdup(yytext); g_parameter = strdup(acl_text);
if (g_parameter == NULL) if (g_parameter == NULL)
YYABORT; YYABORT;
} }
@ -120,7 +120,7 @@ action: TOK_PASS { $$ = ACL_PASS; }
$$ = ACL_FORWARD; $$ = ACL_FORWARD;
if (g_parameter != NULL) if (g_parameter != NULL)
free(g_parameter); free(g_parameter);
g_parameter = strdup(yytext); g_parameter = strdup(acl_text);
if (g_parameter == NULL) if (g_parameter == NULL)
YYABORT; YYABORT;
} }
@ -128,7 +128,7 @@ action: TOK_PASS { $$ = ACL_PASS; }
$$ = ACL_FILTER; $$ = ACL_FILTER;
if (g_parameter != NULL) if (g_parameter != NULL)
free(g_parameter); free(g_parameter);
g_parameter = strdup(yytext); g_parameter = strdup(acl_text);
if (g_parameter == NULL) if (g_parameter == NULL)
YYABORT; YYABORT;
} }
@ -136,14 +136,8 @@ action: TOK_PASS { $$ = ACL_PASS; }
%% %%
/***** internal routines *****/ /***** internal routines *****/
int
yywrap(void)
{
return 1;
}
static int static int
yyerror(char * string) acl_error(char * string)
{ {
syslog(LOG_ERR, "Syntax error in line %u: %s\n", lineno, string); syslog(LOG_ERR, "Syntax error in line %u: %s\n", lineno, string);
return 0; return 0;
@ -238,6 +232,8 @@ int checkACL(struct Mail * MailStruct,
char ** parameter_ptr, char ** parameter_ptr,
acl_type_t type) acl_type_t type)
{ {
extern void acl_reset_lexer();
const struct PD_Config * MasterConfig; const struct PD_Config * MasterConfig;
const struct List_Config * ListConfig; const struct List_Config * ListConfig;
int rc; int rc;
@ -252,13 +248,13 @@ int checkACL(struct Mail * MailStruct,
/* Set up the lex scanner. */ /* Set up the lex scanner. */
BEGIN(INITIAL); acl_reset_lexer();
lineno = 1; operation = ACL_NONE; lineno = 1; operation = ACL_NONE;
/* First check the mail against the master acl file. */ /* First check the mail against the master acl file. */
yyin = fopen((type == ACL_PRE ? MasterConfig->acl_file_pre : MasterConfig->acl_file_post), "r"); acl_in = fopen((type == ACL_PRE ? MasterConfig->acl_file_pre : MasterConfig->acl_file_post), "r");
if (yyin == NULL) { if (acl_in == NULL) {
switch(errno) { switch(errno) {
case ENOENT: case ENOENT:
/* no master acl file */ /* no master acl file */
@ -275,9 +271,9 @@ int checkACL(struct Mail * MailStruct,
/* Parse the acl file. */ /* Parse the acl file. */
rc = yyparse(); rc = yyparse();
if (yyin != NULL) { if (acl_in != NULL) {
fclose(yyin); fclose(acl_in);
yyin = NULL; acl_in = NULL;
} }
if (rc != 0) { if (rc != 0) {
syslog(LOG_ERR, "Parsing \"%s\" file returned with an error.", syslog(LOG_ERR, "Parsing \"%s\" file returned with an error.",
@ -301,12 +297,12 @@ check_local_acl_file:
/* Set up the lex scanner. */ /* Set up the lex scanner. */
BEGIN(INITIAL); acl_reset_lexer();
lineno = 1; operation = ACL_NONE; lineno = 1; operation = ACL_NONE;
ListConfig = getListConfig(listname); ListConfig = getListConfig(listname);
yyin = fopen((type == ACL_PRE ? ListConfig->acl_file_pre : ListConfig->acl_file_post), "r"); acl_in = fopen((type == ACL_PRE ? ListConfig->acl_file_pre : ListConfig->acl_file_post), "r");
if (yyin == NULL) { if (acl_in == NULL) {
switch(errno) { switch(errno) {
case ENOENT: case ENOENT:
/* no list acl file */ /* no list acl file */
@ -320,8 +316,8 @@ check_local_acl_file:
} }
rc = yyparse(); rc = yyparse();
fclose(yyin); fclose(acl_in);
yyin = NULL; acl_in = NULL;
if (rc != 0) { if (rc != 0) {
syslog(LOG_ERR, "Parsing \"%s\" file returned with an error.", syslog(LOG_ERR, "Parsing \"%s\" file returned with an error.",
(type == ACL_PRE ? ListConfig->acl_file_pre : ListConfig->acl_file_post)); (type == ACL_PRE ? ListConfig->acl_file_pre : ListConfig->acl_file_post));

View File

@ -1,30 +1,38 @@
%{
/* /*
$Source$ * Copyright (c) 1995-2006 Peter Simons <simons@cryp.to>
$Revision$ * Copyright (c) 2000-2001 Cable & Wireless GmbH
* Copyright (c) 1999-2000 CyberSolutions GmbH
Copyright (C) 2000 by CyberSolutions GmbH, Germany. *
* This program is free software; you can redistribute it and/or
This file is part of Petidomo. * modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
Petidomo is free software; you can redistribute it and/or modify * the License, or (at your option) any later version.
it under the terms of the GNU General Public License as published by *
the Free Software Foundation; either version 2, or (at your option) * This program is distributed in the hope that it will be
any later version. * useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Petidomo is distributed in the hope that it will be useful, but * PURPOSE. See the GNU General Public License for more details.
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/ */
%{
#include <ctype.h> #include <ctype.h>
#include "acl-scan.h" #include "acl-parser.h"
%} %}
%option 8bit
%option batch
%option case-insensitive
%option nodefault
%option nounput
%option outfile="lex.yy.c"
%option prefix="acl_"
%option warn
%option noyywrap
%% %%
^[ \t]*#.*$ /* ignore comments */ ^[ \t]*#.*$ /* ignore comments */
[ \t] /* ignore whitespace */ [ \t] /* ignore whitespace */
\n lineno++; \n yylineno++;
if return TOK_IF; if return TOK_IF;
= return TOK_EQUAL; = return TOK_EQUAL;
@ -54,5 +62,10 @@ reject return TOK_REJECT;
rejectwith return TOK_REJECTWITH; rejectwith return TOK_REJECTWITH;
filter return TOK_FILTER; filter return TOK_FILTER;
. { yylval = yytext[0]; return yylval; } /* literal */ . { acl_lval = acl_text[0]; return acl_lval; } /* literal */
%% %%
void acl_reset_lexer()
{
BEGIN(INITIAL);
}

View File

@ -23,8 +23,7 @@
#endif #endif
#include "rfc822.h" #include "rfc822.h"
#include "address_scan.h" #include "address.h"
#define yylval rfc822_lval
extern int yylval; extern int yylval;
@ -37,6 +36,16 @@ int rfc822_address_buffer_pos;
} }
%} %}
%x quoted escaped escaped2 %x quoted escaped escaped2
%option 8bit
%option batch
%option case-insensitive
%option nounput
%option outfile="lex.yy.c"
%option prefix="rfc822_"
%option warn
%option noyywrap
%% %%
([^@\.\(\) \t<>\":,\\]*\\)/(.[@\.:]) { BEGIN(escaped2); yymore(); } ([^@\.\(\) \t<>\":,\\]*\\)/(.[@\.:]) { BEGIN(escaped2); yymore(); }
([^@\.\(\) \t<>\":,\\]*\\)/(.) { BEGIN(escaped2); yymore(); } ([^@\.\(\) \t<>\":,\\]*\\)/(.) { BEGIN(escaped2); yymore(); }
@ -57,10 +66,3 @@ int rfc822_address_buffer_pos;
<quoted>\" { BEGIN(INITIAL); return TOK_ATOM; } <quoted>\" { BEGIN(INITIAL); return TOK_ATOM; }
<quoted><<EOF>> { BEGIN(INITIAL); return TOK_ILLEGAL; } <quoted><<EOF>> { BEGIN(INITIAL); return TOK_ILLEGAL; }
%% %%
/* Internal routines. */
int
yywrap(void)
{
return 1;
}