chiark / gitweb /
7183d075c27d9226a22cc072324e4dfb65cede54
[become] / src / lexer.l
1 /* -*-c-*-
2  *
3  * $Id: lexer.l,v 1.5 2003/10/12 00:29:10 mdw Exp $
4  *
5  * Lexical analyser for `become.conf' files
6  *
7  * (c) 1998 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.5  2003/10/12 00:29:10  mdw
33  * Missed an old include...
34  *
35  * Revision 1.4  1999/05/04 16:17:26  mdw
36  * Change to header file name for parser.  See log for `parse.h' for
37  * details.
38  *
39  * Revision 1.3  1998/01/12  16:46:07  mdw
40  * Fix copyright date.
41  *
42  * Revision 1.2  1997/08/04 10:24:23  mdw
43  * Sources placed under CVS control.
44  *
45  * Revision 1.1  1997/07/21  13:47:48  mdw
46  * Initial revision
47  *
48  */
49
50 /*----- Declarations section ----------------------------------------------*/
51
52 /* --- Header files --- */
53
54 %{
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include "become.h"
60 #include "lexer.h"
61 #include "parse.h"
62 %}
63
64 /* --- Start conditions --- */
65
66 %x s_KEYWORD
67 %x s_NORMAL
68 %x s_STRING
69
70 /* --- A handy static buffer --- */
71
72  static char lex__buff[4096];
73  static char *lex__ptr;
74
75 /* --- Line number --- */
76
77  int lex_line;
78
79 %%
80
81  /*---- Main scanner definition -------------------------------------------*/
82
83  /* --- Comments --- */
84
85 <INITIAL,s_KEYWORD,s_NORMAL>{
86   "#".*\n                       lex_line++;
87
88  /* --- Whitespace --- */
89
90   [ \t]                         /* munch */
91   \n                            lex_line++;
92 }
93
94  /* --- Keywords --- */
95
96 <INITIAL,s_KEYWORD>{
97   user                          BEGIN(s_NORMAL); return (USER);
98   command                       BEGIN(s_NORMAL); return (COMMAND);
99   host                          BEGIN(s_NORMAL); return (HOST);
100   allow                         BEGIN(s_NORMAL); return (ALLOW);
101   port                          BEGIN(s_NORMAL); return (PORT);
102   keyfile                       BEGIN(s_NORMAL); return (KEYFILE);
103   .                             BEGIN(s_NORMAL); return (BADTOKEN);
104 }
105  /* --- Other sorts of tokens --- */
106
107 <s_NORMAL>{
108   [0-9]*                        yylval.i = atoi(yytext); return (INT);
109   [a-zA-Z_][a-zA-Z_0-9]*        yylval.s = yytext; return (WORD);
110   \"                            BEGIN(s_STRING); lex__ptr = lex__buff;
111   ";"                           BEGIN(s_KEYWORD); return (';');
112   "->"                          return (ARROW);
113   .                             return (yytext[0]);
114 }
115
116  /* --- Strings and things --- *
117   *
118   * Be a little careful about buffer overflows here.
119   */
120
121 <s_STRING>{
122   \\.                           {
123                                   if (lex__ptr >
124                                       lex__buff + sizeof(lex__buff) - 8) {
125                                     moan("string too long at line %i",
126                                          lex_line);
127                                     *lex__ptr++ = 0;
128                                     yylval.s = lex__buff;
129                                     BEGIN(s_NORMAL);
130                                     return (STRING);
131                                   }
132                                   *lex__ptr++ = yytext[1];
133                                 }
134   \"                            {
135                                   *lex__ptr++ = 0;
136                                   yylval.s = lex__buff;
137                                   BEGIN(s_NORMAL);
138                                   return (STRING);
139                                 }
140   \n                            |
141   <<EOF>>                       {
142                                   moan("missing `\"', inserted at line %i",
143                                        lex_line);
144                                   lex_line++;
145                                   *lex__ptr++ = 0;
146                                   yylval.s = lex__buff;
147                                   BEGIN(s_NORMAL);
148                                   return (STRING);
149                                 }
150   .                             {
151                                   if (lex__ptr >
152                                       lex__buff + sizeof(lex__buff) - 8) {
153                                     moan("string too long at line %i",
154                                          lex_line);
155                                     *lex__ptr++ = 0;
156                                     yylval.s = lex__buff;
157                                     BEGIN(s_NORMAL);
158                                     return (STRING);
159                                   }
160                                   *lex__ptr++ = yytext[0];
161                                 }
162 }
163
164 %%
165
166 /*----- Support routines --------------------------------------------------*/
167
168 /* --- @lexer_scan@ --- *
169  *
170  * Arguments:   @FILE *fp@ = pointer to a stream object to scan
171  *
172  * Returns:     ---
173  *
174  * Use:         Initialises the scanner ready to parse from the given
175  *              stream.
176  */
177
178 void lexer_scan(FILE *fp)
179 {
180   yyin = fp;
181   lex_line = 1;
182   BEGIN(INITIAL);
183 }
184
185 /*----- That's all, folks -------------------------------------------------*/