X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=dfa%2Freadfile.c;h=6f9f6875cceddd81e7b7133823fdb49bc788c003;hp=cd3f6e4c4f9c026f0170b74aae6cb1600b115189;hb=1d5d4f08cb84516d75fcb5e6ed4199b6454cccd6;hpb=ead74d0c3b9d76204494553c61854812eb69bbc7 diff --git a/dfa/readfile.c b/dfa/readfile.c index cd3f6e4..6f9f687 100644 --- a/dfa/readfile.c +++ b/dfa/readfile.c @@ -1,14 +1,26 @@ -/* - * Copyright (C) 1994, Index Data I/S - * All rights reserved. - * Sebastian Hammer, Adam Dickmeiss - * - * $Log: readfile.c,v $ - * Revision 1.1 1994-09-26 10:16:56 adam - * First version of dfa module in alex. This version uses yacc to parse - * regular expressions. This should be hand-made instead. - * - */ +/* This file is part of the Zebra server. + Copyright (C) 2004-2013 Index Data + +Zebra is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Zebra is distributed in the hope that it will be useful, but 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. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + + +#if HAVE_CONFIG_H +#include +#endif #include #include @@ -16,7 +28,7 @@ #include #include -#include +#include #include #include "lexer.h" @@ -31,11 +43,11 @@ static int err_no; static void prep (char **s), read_defs (void), - read_rules (DFA **dfap), + read_rules (struct DFA *dfap), read_tail (void); static char - *read_line (); + *read_line (void); static void prep (char **s) { @@ -44,7 +56,7 @@ static void prep (char **s) const char *src = *s; int c; - while( (c = *src++) ) + while ((c = *src++)) *dst++ = c; *dst = '\0'; @@ -55,110 +67,109 @@ static char *read_line (void) { static char linebuf[MAXLINE+1]; ++line_no; - return fgets( linebuf, MAXLINE, inf ); + return fgets (linebuf, MAXLINE, inf); } static void read_defs (void) { const char *s; - while( (s=read_line()) ) + while ((s=read_line())) { - if( *s == '%' && s[1] == '%' ) + if (*s == '%' && s[1] == '%') return; - else if( *s == '\0' || isspace( *s ) ) - fputs( s, outf ); + else if (*s == '\0' || isspace (*s)) + fputs (s, outf); } - error( "missing rule section" ); + error ("missing rule section"); } -static void read_rules (DFA **dfap) +static void read_rules (struct DFA *dfa) { char *s; - Tnode *n; + const char *sc; int i; - DFA *dfa; - - *dfap = dfa = init_dfa(); - fputs( "\n#ifndef YY_BREAK\n#define YY_BREAK break;\n#endif\n", outf ); - fputs( "void lexact( int no )\n{\n", outf ); - fputs( "\tswitch( no )\n\t{\n", outf ); - dfa->root = NULL; - while( (s=read_line()) ) + int no = 0; + + fputs ("\n#ifndef YY_BREAK\n#define YY_BREAK break;\n#endif\n", outf); + fputs ("void lexact (int no)\n{\n", outf); + fputs ( "\tswitch (no)\n\t{\n", outf); + while ((s=read_line())) { - if( *s == '%' && s[1] == '%' ) + if (*s == '%' && s[1] == '%') break; - else if( *s == '\0' || isspace( *s ) ) + else if (*s == '\0' || isspace (*s)) /* copy rest of line to output */ - fputs( s, outf ); + fputs (s, outf); else - { + { /* preprocess regular expression */ - prep( &s ); + prep (&s); /* now parse regular expression */ - if (ccluse) - i = parse_dfa( dfa, &s, ccl_chars ); - else - i = parse_dfa( dfa, &s, thompson_chars ); - - if( dfa->rule > 1 ) - fputs( "\t\tYY_BREAK\n", outf ); - fprintf( outf, "\tcase %d:\n#line %d\n\t\t", dfa->rule, line_no ); - if( i ) + sc = s; + i = dfa_parse (dfa, &sc); + if (i) { - fprintf( stderr, "%s #%d: regular expression syntax error\n", - inf_name, line_no ); + fprintf (stderr, "%s #%d: regular expression syntax error\n", + inf_name, line_no); + assert (0); err_no++; } - else if( !dfa->root ) - dfa->root = dfa->top; else { - n = mk_Tnode(); - n->pos = OR; - n->u.p[0] = dfa->root; - n->u.p[1] = dfa->top; - dfa->root = n; + if (no) + fputs ("\t\tYY_BREAK\n", outf); + no++; + fprintf (outf, "\tcase %d:\n#line %d\n\t\t", no, line_no); } - while( *s == '\t' || *s == ' ' ) - s++; - fputs( s, outf ); + while (*sc == '\t' || *sc == ' ') + sc++; + fputs (sc, outf); } } - fputs( "\tYY_BREAK\n\t}\n}\n", outf ); - if( !dfa->root ) - error( "no regular expressions in rule section" ); + fputs ("\tYY_BREAK\n\t}\n}\n", outf); + if (!no) + error ("no regular expressions in rule section"); } static void read_tail (void) { const char *s; - while( (s=read_line()) ) - fputs( s, outf ); + while ((s=read_line())) + fputs (s, outf); } -int read_file (const char *s, DFA **dfap) +int read_file (const char *s, struct DFA *dfa) { inf_name = s; - if( !(inf=fopen( s,"r" )) ) + if (!(inf=fopen (s,"r"))) { - error( "cannot open `%s'", s ); + error ("cannot open `%s'", s); return -1; } - if( !(outf=fopen( "lex.yy.c", "w" )) ) + if (!(outf=fopen ("lex.yy.c", "w"))) { - error( "cannot open `%s'", "lex.yy.c" ); + error ("cannot open `%s'", "lex.yy.c"); return -2; } line_no = 0; err_no = 0; - read_defs(); - read_rules( dfap ); - read_tail(); + read_defs (); + read_rules (dfa); + read_tail (); - fclose( outf ); - fclose( inf ); + fclose (outf); + fclose (inf); return err_no; } +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +