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