| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: lexer.l,v 1.2 1997/08/04 10:24:23 mdw Exp $ |
| 4 | * |
| 5 | * Lexical analyser for `become.conf' files |
| 6 | * |
| 7 | * (c) 1997 EBI |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of `become' |
| 13 | * |
| 14 | * `Become' is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * `Become' is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with `become'; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: lexer.l,v $ |
| 32 | * Revision 1.2 1997/08/04 10:24:23 mdw |
| 33 | * Sources placed under CVS control. |
| 34 | * |
| 35 | * Revision 1.1 1997/07/21 13:47:48 mdw |
| 36 | * Initial revision |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | /*----- Declarations section ----------------------------------------------*/ |
| 41 | |
| 42 | /* --- Header files --- */ |
| 43 | |
| 44 | %{ |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| 48 | |
| 49 | #include "become.h" |
| 50 | #include "lexer.h" |
| 51 | #include "parser.h" |
| 52 | #include "utils.h" |
| 53 | %} |
| 54 | |
| 55 | /* --- Start conditions --- */ |
| 56 | |
| 57 | %x s_KEYWORD |
| 58 | %x s_NORMAL |
| 59 | %x s_STRING |
| 60 | |
| 61 | /* --- A handy static buffer --- */ |
| 62 | |
| 63 | static char lex__buff[4096]; |
| 64 | static char *lex__ptr; |
| 65 | |
| 66 | /* --- Line number --- */ |
| 67 | |
| 68 | int lex_line; |
| 69 | |
| 70 | %% |
| 71 | |
| 72 | /*---- Main scanner definition -------------------------------------------*/ |
| 73 | |
| 74 | /* --- Comments --- */ |
| 75 | |
| 76 | <INITIAL,s_KEYWORD,s_NORMAL>{ |
| 77 | "#".*\n lex_line++; |
| 78 | |
| 79 | /* --- Whitespace --- */ |
| 80 | |
| 81 | [ \t] /* munch */ |
| 82 | \n lex_line++; |
| 83 | } |
| 84 | |
| 85 | /* --- Keywords --- */ |
| 86 | |
| 87 | <INITIAL,s_KEYWORD>{ |
| 88 | user BEGIN(s_NORMAL); return (USER); |
| 89 | command BEGIN(s_NORMAL); return (COMMAND); |
| 90 | host BEGIN(s_NORMAL); return (HOST); |
| 91 | allow BEGIN(s_NORMAL); return (ALLOW); |
| 92 | port BEGIN(s_NORMAL); return (PORT); |
| 93 | keyfile BEGIN(s_NORMAL); return (KEYFILE); |
| 94 | . BEGIN(s_NORMAL); return (BADTOKEN); |
| 95 | } |
| 96 | /* --- Other sorts of tokens --- */ |
| 97 | |
| 98 | <s_NORMAL>{ |
| 99 | [0-9]* yylval.i = atoi(yytext); return (INT); |
| 100 | [a-zA-Z_][a-zA-Z_0-9]* yylval.s = yytext; return (WORD); |
| 101 | \" BEGIN(s_STRING); lex__ptr = lex__buff; |
| 102 | ";" BEGIN(s_KEYWORD); return (';'); |
| 103 | "->" return (ARROW); |
| 104 | . return (yytext[0]); |
| 105 | } |
| 106 | |
| 107 | /* --- Strings and things --- * |
| 108 | * |
| 109 | * Be a little careful about buffer overflows here. |
| 110 | */ |
| 111 | |
| 112 | <s_STRING>{ |
| 113 | \\. { |
| 114 | if (lex__ptr > |
| 115 | lex__buff + sizeof(lex__buff) - 8) { |
| 116 | moan("string too long at line %i", |
| 117 | lex_line); |
| 118 | *lex__ptr++ = 0; |
| 119 | yylval.s = lex__buff; |
| 120 | BEGIN(s_NORMAL); |
| 121 | return (STRING); |
| 122 | } |
| 123 | *lex__ptr++ = yytext[1]; |
| 124 | } |
| 125 | \" { |
| 126 | *lex__ptr++ = 0; |
| 127 | yylval.s = lex__buff; |
| 128 | BEGIN(s_NORMAL); |
| 129 | return (STRING); |
| 130 | } |
| 131 | \n | |
| 132 | <<EOF>> { |
| 133 | moan("missing `\"', inserted at line %i", |
| 134 | lex_line); |
| 135 | lex_line++; |
| 136 | *lex__ptr++ = 0; |
| 137 | yylval.s = lex__buff; |
| 138 | BEGIN(s_NORMAL); |
| 139 | return (STRING); |
| 140 | } |
| 141 | . { |
| 142 | if (lex__ptr > |
| 143 | lex__buff + sizeof(lex__buff) - 8) { |
| 144 | moan("string too long at line %i", |
| 145 | lex_line); |
| 146 | *lex__ptr++ = 0; |
| 147 | yylval.s = lex__buff; |
| 148 | BEGIN(s_NORMAL); |
| 149 | return (STRING); |
| 150 | } |
| 151 | *lex__ptr++ = yytext[0]; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | %% |
| 156 | |
| 157 | /*----- Support routines --------------------------------------------------*/ |
| 158 | |
| 159 | /* --- @lexer_scan@ --- * |
| 160 | * |
| 161 | * Arguments: @FILE *fp@ = pointer to a stream object to scan |
| 162 | * |
| 163 | * Returns: --- |
| 164 | * |
| 165 | * Use: Initialises the scanner ready to parse from the given |
| 166 | * stream. |
| 167 | */ |
| 168 | |
| 169 | void lexer_scan(FILE *fp) |
| 170 | { |
| 171 | yyin = fp; |
| 172 | lex_line = 1; |
| 173 | BEGIN(INITIAL); |
| 174 | } |
| 175 | |
| 176 | /*----- That's all, folks -------------------------------------------------*/ |