8e32f68c20e4860696e34e024fc75cf809b9482e
[dragonfly.git] / usr.sbin / config / lang.l
1 %{
2 /*-
3  * Copyright (c) 1980, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)lang.l      8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/config/lang.l,v 1.27 1999/11/09 07:20:22 peter Exp $
36  * $DragonFly: src/usr.sbin/config/lang.l,v 1.10 2005/01/12 00:26:03 cpressey Exp $
37  */
38
39 #include <ctype.h>
40 #include <string.h>
41 #include "y.tab.h"
42 #include "config.h"
43
44 #define YY_NO_UNPUT
45
46 /*
47  * Key word table
48  */
49
50 struct kt {
51         const char *kt_name;
52         int kt_val;
53 } key_words[] = {
54         { "at",         AT },
55         { "bus",        BUS },
56         { "conflicts",  CONFLICTS },
57         { "config",     CONFIG },
58         { "controller", CONTROLLER },
59         { "cpu",        CPU },
60         { "device",     DEVICE },
61         { "disable",    DISABLE },
62         { "disk",       DISK },
63         { "drive",      DRIVE },
64         { "drq",        DRQ },
65         { "flags",      FLAGS },
66         { "ident",      IDENT },
67         { "iomem",      IOMEM },
68         { "iosiz",      IOSIZ },
69         { "irq",        IRQ },
70         { "machine",    ARCH }, /* MACHINE is defined in /sys/param.h */
71         { "makeoptions", MAKEOPTIONS },
72         { "maxusers",   MAXUSERS },
73         { "nexus",      NEXUS },
74         { "option",     OPTIONS },
75         { "options",    OPTIONS },
76         { "port",       PORT },
77         { "pseudo-device",PSEUDO_DEVICE },
78         { "tape",       TAPE },
79         { "target",     TARGET },
80         { "unit",       UNIT },
81         { 0, 0 },
82 };
83
84
85 int kw_lookup(char *);
86 int octal(char *);
87 int hex(char *);
88
89 %}
90 WORD    [A-Za-z_][-A-Za-z_]*
91 ID      [A-Za-z_][-A-Za-z_0-9]*
92 %START NONUM TOEOL
93 %%
94 <NONUM>{WORD}   {
95                         int i;
96
97                         BEGIN 0;
98                         if ((i = kw_lookup(yytext)) == -1)
99                         {
100                                 yylval.str = strdup(yytext);
101                                 return(ID);
102                         }
103                         return(i);
104                 }
105 <INITIAL>{WORD}/[0-9]* {
106                         int i;
107
108                         if ((i = kw_lookup(yytext)) == -1)
109                                 REJECT;
110                         if (i == CONTROLLER || i == DEVICE || i == DISK || i == TAPE ||
111                             i == PSEUDO_DEVICE || i == AT)
112                                 BEGIN NONUM;
113                         return(i);
114                 }
115 <INITIAL>{ID}   {
116                         BEGIN 0;
117                         yylval.str = strdup(yytext);
118                         return(ID);
119                 }
120 \\\"[^"]+\\\"   {
121                         BEGIN 0;
122                         yytext[yyleng-2] = '"';
123                         yytext[yyleng-1] = '\0';
124                         yylval.str = strdup(yytext + 1);
125                         return(ID);
126                 }
127 \"[^"]+\"       {
128                         BEGIN 0;
129                         yytext[yyleng-1] = '\0';
130                         yylval.str = strdup(yytext + 1);
131                         return(ID);
132                 }
133 <TOEOL>[^# \t\n]*       {
134                         BEGIN 0;
135                         yylval.str = strdup(yytext);
136                         return(ID);
137                 }
138 0[0-7]*         {
139                         yylval.val = octal(yytext);
140                         return(NUMBER);
141                 }
142 0x[0-9a-fA-F]+  {
143                         yylval.val = hex(yytext);
144                         return(NUMBER);
145                 }
146 -?[1-9][0-9]*   {
147                         yylval.val = atoi(yytext);
148                         return(NUMBER);
149                 }
150 [0-9]"."[0-9]*  {
151                         yylval.val = (int)(60 * atof(yytext) + 0.5);
152                         return(FPNUMBER);
153                 }
154 "?"             {
155                         yylval.val = -1;
156                         return(NUMBER);
157                 }
158 \n/[ \t]        {
159                         yyline++;
160                 }
161 \n              {
162                         yyline++;
163                         return(SEMICOLON);
164                 }
165 #.*             {       /* Ignored (comment) */;        }
166 [ \t\f]*        {       /* Ignored (white space) */;    }
167 ";"             {       return(SEMICOLON);              }
168 ","             {       return(COMMA);                  }
169 "="             {       BEGIN TOEOL; return(EQUALS);    }
170 "@"             {       return(AT);                     }
171 .               {       return(yytext[0]);              }
172
173 %%
174 /*
175  * kw_lookup
176  *      Look up a string in the keyword table.  Returns a -1 if the
177  *      string is not a keyword otherwise it returns the keyword number
178  */
179
180 int
181 kw_lookup(char *word)
182 {
183         struct kt *kp;
184
185         for (kp = key_words; kp->kt_name != NULL; kp++)
186                 if (!strcmp(word, kp->kt_name))
187                         return(kp->kt_val);
188         return(-1);
189 }
190
191 /*
192  * Number conversion routines
193  */
194
195 int
196 octal(char *str)
197 {
198         int num;
199
200         sscanf(str, "%o", &num);
201         return(num);
202 }
203
204 int
205 hex(char *str)
206 {
207         int num;
208
209         sscanf(str + 2, "%x", &num);
210         return(num);
211 }