chiark / gitweb /
manual/Makefile.am: Install the HTML version of the manual correctly.
[become] / src / lexer.l
1 /* -*-c-*-
2  *
3  * $Id: lexer.l,v 1.7 2004/04/08 01:36:20 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 /*----- Declarations section ----------------------------------------------*/
30
31 /* --- Header files --- */
32
33 %{
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <mLib/report.h>
39
40 #include "become.h"
41 #include "lexer.h"
42 #include "parse.h"
43 %}
44
45 /* --- Start conditions --- */
46
47 %x s_KEYWORD
48 %x s_NORMAL
49 %x s_STRING
50
51 /* --- A handy static buffer --- */
52
53  static char lex__buff[4096];
54  static char *lex__ptr;
55
56 /* --- Line number --- */
57
58  int lex_line;
59
60 %%
61
62  /*---- Main scanner definition -------------------------------------------*/
63
64  /* --- Comments --- */
65
66 <INITIAL,s_KEYWORD,s_NORMAL>{
67   "#".*\n                       lex_line++;
68
69  /* --- Whitespace --- */
70
71   [ \t]                         /* munch */
72   \n                            lex_line++;
73 }
74
75  /* --- Keywords --- */
76
77 <INITIAL,s_KEYWORD>{
78   user                          BEGIN(s_NORMAL); return (USER);
79   command                       BEGIN(s_NORMAL); return (COMMAND);
80   host                          BEGIN(s_NORMAL); return (HOST);
81   allow                         BEGIN(s_NORMAL); return (ALLOW);
82   port                          BEGIN(s_NORMAL); return (PORT);
83   keyfile                       BEGIN(s_NORMAL); return (KEYFILE);
84   .                             BEGIN(s_NORMAL); return (BADTOKEN);
85 }
86  /* --- Other sorts of tokens --- */
87
88 <s_NORMAL>{
89   [0-9]*                        yylval.i = atoi(yytext); return (INT);
90   [a-zA-Z_][a-zA-Z_0-9]*        yylval.s = yytext; return (WORD);
91   \"                            BEGIN(s_STRING); lex__ptr = lex__buff;
92   ";"                           BEGIN(s_KEYWORD); return (';');
93   "->"                          return (ARROW);
94   .                             return (yytext[0]);
95 }
96
97  /* --- Strings and things --- *
98   *
99   * Be a little careful about buffer overflows here.
100   */
101
102 <s_STRING>{
103   \\.                           {
104                                   if (lex__ptr >
105                                       lex__buff + sizeof(lex__buff) - 8) {
106                                     moan("string too long at line %i",
107                                          lex_line);
108                                     *lex__ptr++ = 0;
109                                     yylval.s = lex__buff;
110                                     BEGIN(s_NORMAL);
111                                     return (STRING);
112                                   }
113                                   *lex__ptr++ = yytext[1];
114                                 }
115   \"                            {
116                                   *lex__ptr++ = 0;
117                                   yylval.s = lex__buff;
118                                   BEGIN(s_NORMAL);
119                                   return (STRING);
120                                 }
121   \n                            |
122   <<EOF>>                       {
123                                   moan("missing `\"', inserted at line %i",
124                                        lex_line);
125                                   lex_line++;
126                                   *lex__ptr++ = 0;
127                                   yylval.s = lex__buff;
128                                   BEGIN(s_NORMAL);
129                                   return (STRING);
130                                 }
131   .                             {
132                                   if (lex__ptr >
133                                       lex__buff + sizeof(lex__buff) - 8) {
134                                     moan("string too long at line %i",
135                                          lex_line);
136                                     *lex__ptr++ = 0;
137                                     yylval.s = lex__buff;
138                                     BEGIN(s_NORMAL);
139                                     return (STRING);
140                                   }
141                                   *lex__ptr++ = yytext[0];
142                                 }
143 }
144
145 %%
146
147 /*----- Support routines --------------------------------------------------*/
148
149 /* --- @lexer_scan@ --- *
150  *
151  * Arguments:   @FILE *fp@ = pointer to a stream object to scan
152  *
153  * Returns:     ---
154  *
155  * Use:         Initialises the scanner ready to parse from the given
156  *              stream.
157  */
158
159 void lexer_scan(FILE *fp)
160 {
161   yyin = fp;
162   lex_line = 1;
163   BEGIN(INITIAL);
164 }
165
166 /*----- That's all, folks -------------------------------------------------*/