xref: /original-bsd/usr.sbin/config/lang.l (revision f1656be1)
1 %{
2 
3 /*
4  * Copyright (c) 1980 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that this notice is preserved and that due credit is given
9  * to the University of California at Berkeley. The name of the University
10  * may not be used to endorse or promote products derived from this
11  * software without specific prior written permission. This software
12  * is provided ``as is'' without express or implied warranty.
13  *
14  *	@(#)lang.l	5.4 (Berkeley) 05/21/88
15  */
16 
17 #include <ctype.h>
18 #include "y.tab.h"
19 #include "config.h"
20 
21 #define tprintf if (do_trace) printf
22 
23 /*
24  * Key word table
25  */
26 
27 struct kt {
28 	char *kt_name;
29 	int kt_val;
30 } key_words[] = {
31 	{ "and",	AND },
32 	{ "args",	ARGS },
33 	{ "at",		AT },
34 	{ "config",	CONFIG },
35 	{ "controller",	CONTROLLER },
36 	{ "cpu",	CPU },
37 	{ "csr",	CSR },
38 	{ "device",	DEVICE },
39 	{ "disk",	DISK },
40 	{ "drive",	DRIVE },
41 	{ "dst",	DST },
42 	{ "dumps",	DUMPS },
43 	{ "flags",	FLAGS },
44 	{ "hz",		HZ },
45 	{ "ident",	IDENT },
46 	{ "machine",	MACHINE },
47 	{ "major",	MAJOR },
48 	{ "makeoptions", MAKEOPTIONS },
49 	{ "master",	MASTER },
50 	{ "maxusers",	MAXUSERS },
51 	{ "minor",	MINOR },
52 	{ "nexus",	NEXUS },
53 	{ "on",		ON },
54 	{ "options",	OPTIONS },
55 	{ "priority",	PRIORITY },
56 	{ "pseudo-device",PSEUDO_DEVICE },
57 	{ "root",	ROOT },
58 	{ "size",	SIZE },
59 	{ "slave",	SLAVE },
60 	{ "swap",	SWAP },
61 	{ "tape",	DEVICE },
62 	{ "timezone",	TIMEZONE },
63 	{ "trace",	TRACE },
64 	{ "vector",	VECTOR },
65 	{ 0, 0 },
66 };
67 %}
68 WORD	[A-Za-z_][-A-Za-z_]*
69 %%
70 {WORD}		{
71 			int i;
72 
73 			if ((i = kw_lookup(yytext)) == -1)
74 			{
75 				yylval.str = yytext;
76 				tprintf("id(%s) ", yytext);
77 				return ID;
78 			}
79 			tprintf("(%s) ", yytext);
80 			return i;
81 		}
82 \"[^"]+\"	{
83 			yytext[strlen(yytext)-1] = '\0';
84 			yylval.str = yytext + 1;
85 			return ID;
86 		}
87 0[0-7]*		{
88 			yylval.val = octal(yytext);
89 			tprintf("#O:%o ", yylval.val);
90 			return NUMBER;
91 		}
92 0x[0-9a-fA-F]+	{
93 			yylval.val = hex(yytext);
94 			tprintf("#X:%x ", yylval.val);
95 			return NUMBER;
96 		}
97 [1-9][0-9]*	{
98 			yylval.val = atoi(yytext);
99 			tprintf("#D:%d ", yylval.val);
100 			return NUMBER;
101 		}
102 [0-9]"."[0-9]*	{
103 			double atof();
104 			yylval.val = (int) (60 * atof(yytext) + 0.5);
105 			return FPNUMBER;
106 		}
107 "-"		{
108 			return MINUS;
109 		}
110 "?"		{
111 			yylval.val = -1;
112 			tprintf("? ");
113 			return NUMBER;
114 		}
115 \n/[ \t]	{
116 			yyline++;
117 			tprintf("\n... ");
118 		}
119 \n		{
120 			yyline++;
121 			tprintf("\n");
122 			return SEMICOLON;
123 		}
124 #.*		{	/* Ignored (comment) */;	}
125 [ \t]*		{	/* Ignored (white space) */;	}
126 ";"		{	return SEMICOLON;		}
127 ","		{	return COMMA;			}
128 "="		{	return EQUALS;			}
129 "@"		{	return AT;			}
130 .		{	return yytext[0];		}
131 
132 %%
133 /*
134  * kw_lookup
135  *	Look up a string in the keyword table.  Returns a -1 if the
136  *	string is not a keyword otherwise it returns the keyword number
137  */
138 
139 kw_lookup(word)
140 register char *word;
141 {
142 	register struct kt *kp;
143 
144 	for (kp = key_words; kp->kt_name != 0; kp++)
145 		if (eq(word, kp->kt_name))
146 			return kp->kt_val;
147 	return -1;
148 }
149 
150 /*
151  * Number conversion routines
152  */
153 
154 octal(str)
155 char *str;
156 {
157 	int num;
158 
159 	(void) sscanf(str, "%o", &num);
160 	return num;
161 }
162 
163 hex(str)
164 char *str;
165 {
166 	int num;
167 
168 	(void) sscanf(str+2, "%x", &num);
169 	return num;
170 }
171