/* -*-c-*- * * $Id: lexer.l,v 1.7 2004/04/08 01:36:20 mdw Exp $ * * Lexical analyser for `become.conf' files * * (c) 1998 EBI */ /*----- Licensing notice --------------------------------------------------* * * This file is part of `become' * * `Become' 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 of the License, or * (at your option) any later version. * * `Become' 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 `become'; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*----- Declarations section ----------------------------------------------*/ /* --- Header files --- */ %{ #include #include #include #include #include "become.h" #include "lexer.h" #include "parse.h" %} /* --- Start conditions --- */ %x s_KEYWORD %x s_NORMAL %x s_STRING /* --- A handy static buffer --- */ static char lex__buff[4096]; static char *lex__ptr; /* --- Line number --- */ int lex_line; %% /*---- Main scanner definition -------------------------------------------*/ /* --- Comments --- */ { "#".*\n lex_line++; /* --- Whitespace --- */ [ \t] /* munch */ \n lex_line++; } /* --- Keywords --- */ { user BEGIN(s_NORMAL); return (USER); command BEGIN(s_NORMAL); return (COMMAND); host BEGIN(s_NORMAL); return (HOST); allow BEGIN(s_NORMAL); return (ALLOW); port BEGIN(s_NORMAL); return (PORT); keyfile BEGIN(s_NORMAL); return (KEYFILE); . BEGIN(s_NORMAL); return (BADTOKEN); } /* --- Other sorts of tokens --- */ { [0-9]* yylval.i = atoi(yytext); return (INT); [a-zA-Z_][a-zA-Z_0-9]* yylval.s = yytext; return (WORD); \" BEGIN(s_STRING); lex__ptr = lex__buff; ";" BEGIN(s_KEYWORD); return (';'); "->" return (ARROW); . return (yytext[0]); } /* --- Strings and things --- * * * Be a little careful about buffer overflows here. */ { \\. { if (lex__ptr > lex__buff + sizeof(lex__buff) - 8) { moan("string too long at line %i", lex_line); *lex__ptr++ = 0; yylval.s = lex__buff; BEGIN(s_NORMAL); return (STRING); } *lex__ptr++ = yytext[1]; } \" { *lex__ptr++ = 0; yylval.s = lex__buff; BEGIN(s_NORMAL); return (STRING); } \n | <> { moan("missing `\"', inserted at line %i", lex_line); lex_line++; *lex__ptr++ = 0; yylval.s = lex__buff; BEGIN(s_NORMAL); return (STRING); } . { if (lex__ptr > lex__buff + sizeof(lex__buff) - 8) { moan("string too long at line %i", lex_line); *lex__ptr++ = 0; yylval.s = lex__buff; BEGIN(s_NORMAL); return (STRING); } *lex__ptr++ = yytext[0]; } } %% /*----- Support routines --------------------------------------------------*/ /* --- @lexer_scan@ --- * * * Arguments: @FILE *fp@ = pointer to a stream object to scan * * Returns: --- * * Use: Initialises the scanner ready to parse from the given * stream. */ void lexer_scan(FILE *fp) { yyin = fp; lex_line = 1; BEGIN(INITIAL); } /*----- That's all, folks -------------------------------------------------*/