]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/config/config.y
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / config / config.y
1 %union {
2         char    *str;
3         int     val;
4         struct  file_list *file;
5 }
6
7 %token  ARCH
8 %token  COMMA
9 %token  CONFIG
10 %token  CPU
11 %token  NOCPU
12 %token  DEVICE
13 %token  NODEVICE
14 %token  ENV
15 %token  EQUALS
16 %token  PLUSEQUALS
17 %token  HINTS
18 %token  IDENT
19 %token  MAXUSERS
20 %token  PROFILE
21 %token  OPTIONS
22 %token  NOOPTION
23 %token  MAKEOPTIONS
24 %token  NOMAKEOPTION 
25 %token  SEMICOLON
26 %token  INCLUDE
27 %token  FILES
28
29 %token  <str>   ID
30 %token  <val>   NUMBER
31
32 %type   <str>   Save_id
33 %type   <str>   Opt_value
34 %type   <str>   Dev
35 %token  <str>   PATH
36
37 %{
38
39 /*
40  * Copyright (c) 1988, 1993
41  *      The Regents of the University of California.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *      This product includes software developed by the University of
54  *      California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *      @(#)config.y    8.1 (Berkeley) 6/6/93
72  * $FreeBSD$
73  */
74
75 #include <assert.h>
76 #include <ctype.h>
77 #include <err.h>
78 #include <stdio.h>
79 #include <string.h>
80
81 #include "config.h"
82
83 struct  device_head dtab;
84 char    *ident;
85 char    *env;
86 int     envmode;
87 int     hintmode;
88 int     yyline;
89 const   char *yyfile;
90 struct  file_list_head ftab;
91 struct  files_name_head fntab;
92 char    errbuf[80];
93 int     maxusers;
94
95 #define ns(s)   strdup(s)
96 int include(const char *, int);
97 void yyerror(const char *s);
98 int yywrap(void);
99
100 static char *
101 devopt(char *dev)
102 {
103         char *ret = malloc(strlen(dev) + 5);
104         
105         sprintf(ret, "DEV_%s", dev);
106         raisestr(ret);
107         return ret;
108 }
109
110 %}
111 %%
112 Configuration:
113         Many_specs
114                 ;
115
116 Many_specs:
117         Many_specs Spec
118                 |
119         /* lambda */
120                 ;
121
122 Spec:
123         Device_spec SEMICOLON
124                 |
125         Config_spec SEMICOLON
126                 |
127         INCLUDE PATH SEMICOLON
128               = {
129                 if (incignore == 0)
130                         include($2, 0);
131                 };
132                 |
133         INCLUDE ID SEMICOLON
134               = {
135                   if (incignore == 0)
136                         include($2, 0);
137                 };
138                 |
139         FILES ID SEMICOLON
140               = { newfile($2); };
141                 |
142         SEMICOLON
143                 |
144         error SEMICOLON
145                 ;
146
147 Config_spec:
148         ARCH Save_id
149             = {
150                 if (machinename != NULL && !eq($2, machinename))
151                     errx(1, "%s:%d: only one machine directive is allowed",
152                         yyfile, yyline);
153                 machinename = $2;
154                 machinearch = $2;
155               } |
156         ARCH Save_id Save_id
157             = {
158                 if (machinename != NULL &&
159                     !(eq($2, machinename) && eq($3, machinearch)))
160                     errx(1, "%s:%d: only one machine directive is allowed",
161                         yyfile, yyline);
162                 machinename = $2;
163                 machinearch = $3;
164               } |
165         CPU Save_id
166               = {
167                 struct cputype *cp =
168                     (struct cputype *)calloc(1, sizeof (struct cputype));
169                 cp->cpu_name = $2;
170                 SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
171               } |
172         NOCPU Save_id
173               = {
174                 struct cputype *cp, *cp2;
175                 SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
176                         if (eq(cp->cpu_name, $2)) {
177                                 SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
178                                 free(cp);
179                         }
180                 }
181               } |
182         OPTIONS Opt_list
183                 |
184         NOOPTION Save_id
185               = { rmopt_schedule(&opt, $2); } |
186         MAKEOPTIONS Mkopt_list
187                 |
188         NOMAKEOPTION Save_id
189               = { rmopt_schedule(&mkopt, $2); } |
190         IDENT ID
191               = { ident = $2; } |
192         System_spec
193                 |
194         MAXUSERS NUMBER
195               = { maxusers = $2; } |
196         PROFILE NUMBER
197               = { profiling = $2; } |
198         ENV ID
199               = {
200                 env = $2;
201                 envmode = 1;
202                 } |
203         HINTS ID
204               = {
205                 struct hint *hint;
206
207                 hint = (struct hint *)calloc(1, sizeof (struct hint));
208                 hint->hint_name = $2;
209                 STAILQ_INSERT_TAIL(&hints, hint, hint_next);
210                 hintmode = 1;
211                 }
212
213 System_spec:
214         CONFIG System_id System_parameter_list
215           = { errx(1, "%s:%d: root/dump/swap specifications obsolete",
216               yyfile, yyline);}
217           |
218         CONFIG System_id
219           ;
220
221 System_id:
222         Save_id
223               = { newopt(&mkopt, ns("KERNEL"), $1, 0); };
224
225 System_parameter_list:
226           System_parameter_list ID
227         | ID
228         ;
229
230 Opt_list:
231         Opt_list COMMA Option
232                 |
233         Option
234                 ;
235
236 Option:
237         Save_id
238               = {
239                 newopt(&opt, $1, NULL, 0);
240                 if (strchr($1, '=') != NULL)
241                         errx(1, "%s:%d: The `=' in options should not be "
242                             "quoted", yyfile, yyline);
243               } |
244         Save_id EQUALS Opt_value
245               = {
246                 newopt(&opt, $1, $3, 0);
247               } ;
248
249 Opt_value:
250         ID
251                 = { $$ = $1; } |
252         NUMBER
253                 = {
254                         char buf[80];
255
256                         (void) snprintf(buf, sizeof(buf), "%d", $1);
257                         $$ = ns(buf);
258                 } ;
259
260 Save_id:
261         ID
262               = { $$ = $1; }
263         ;
264
265 Mkopt_list:
266         Mkopt_list COMMA Mkoption
267                 |
268         Mkoption
269                 ;
270
271 Mkoption:
272         Save_id
273               = { newopt(&mkopt, $1, ns(""), 0); } |
274         Save_id EQUALS Opt_value
275               = { newopt(&mkopt, $1, $3, 0); } |
276         Save_id PLUSEQUALS Opt_value
277               = { newopt(&mkopt, $1, $3, 1); } ;
278
279 Dev:
280         ID
281               = { $$ = $1; }
282         ;
283
284 Device_spec:
285         DEVICE Dev_list
286                 |
287         NODEVICE NoDev_list
288                 ;
289
290 Dev_list:
291         Dev_list COMMA Device
292                 |
293         Device
294                 ;
295
296 NoDev_list:
297         NoDev_list COMMA NoDevice
298                 |
299         NoDevice
300                 ;
301
302 Device:
303         Dev
304               = {
305                 newopt(&opt, devopt($1), ns("1"), 0);
306                 /* and the device part */
307                 newdev($1);
308                 }
309
310 NoDevice:
311         Dev
312               = {
313                 char *s = devopt($1);
314
315                 rmopt_schedule(&opt, s);
316                 free(s);
317                 /* and the device part */
318                 rmdev_schedule(&dtab, $1);
319                 } ;
320
321 %%
322
323 void
324 yyerror(const char *s)
325 {
326
327         errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
328 }
329
330 int
331 yywrap(void)
332 {
333         if (found_defaults) {
334                 if (freopen(PREFIX, "r", stdin) == NULL)
335                         err(2, "%s", PREFIX);           
336                 yyfile = PREFIX;
337                 yyline = 0;
338                 found_defaults = 0;
339                 return 0;
340         }
341         return 1;
342 }
343
344 /*
345  * Add a new file to the list of files.
346  */
347 static void
348 newfile(char *name)
349 {
350         struct files_name *nl;
351         
352         nl = (struct files_name *) calloc(1, sizeof *nl);
353         nl->f_name = name;
354         STAILQ_INSERT_TAIL(&fntab, nl, f_next);
355 }
356         
357 /*
358  * Find a device in the list of devices.
359  */
360 static struct device *
361 finddev(struct device_head *dlist, char *name)
362 {
363         struct device *dp;
364
365         STAILQ_FOREACH(dp, dlist, d_next)
366                 if (eq(dp->d_name, name))
367                         return (dp);
368
369         return (NULL);
370 }
371         
372 /*
373  * Add a device to the list of devices.
374  */
375 static void
376 newdev(char *name)
377 {
378         struct device *np;
379
380         if (finddev(&dtab, name)) {
381                 printf("WARNING: duplicate device `%s' encountered.\n", name);
382                 return;
383         }
384
385         np = (struct device *) calloc(1, sizeof *np);
386         np->d_name = name;
387         STAILQ_INSERT_TAIL(&dtab, np, d_next);
388 }
389
390 /*
391  * Schedule a device to removal.
392  */
393 static void
394 rmdev_schedule(struct device_head *dh, char *name)
395 {
396         struct device *dp;
397
398         dp = finddev(dh, name);
399         if (dp != NULL) {
400                 STAILQ_REMOVE(dh, dp, device, d_next);
401                 free(dp->d_name);
402                 free(dp);
403         }
404 }
405
406 /*
407  * Find an option in the list of options.
408  */
409 static struct opt *
410 findopt(struct opt_head *list, char *name)
411 {
412         struct opt *op;
413
414         SLIST_FOREACH(op, list, op_next)
415                 if (eq(op->op_name, name))
416                         return (op);
417
418         return (NULL);
419 }
420
421 /*
422  * Add an option to the list of options.
423  */
424 static void
425 newopt(struct opt_head *list, char *name, char *value, int append)
426 {
427         struct opt *op, *op2;
428
429         /*
430          * Ignore inclusions listed explicitly for configuration files.
431          */
432         if (eq(name, OPT_AUTOGEN)) {
433                 incignore = 1;
434                 return;
435         }
436
437         op2 = findopt(list, name);
438         if (op2 != NULL && !append) {
439                 printf("WARNING: duplicate option `%s' encountered.\n", name);
440                 return;
441         }
442
443         op = (struct opt *)calloc(1, sizeof (struct opt));
444         op->op_name = name;
445         op->op_ownfile = 0;
446         op->op_value = value;
447         if (op2 != NULL) {
448                 while (SLIST_NEXT(op2, op_append) != NULL)
449                         op2 = SLIST_NEXT(op2, op_append);
450                 SLIST_NEXT(op2, op_append) = op;
451         } else
452                 SLIST_INSERT_HEAD(list, op, op_next);
453 }
454
455 /*
456  * Remove an option from the list of options.
457  */
458 static void
459 rmopt_schedule(struct opt_head *list, char *name)
460 {
461         struct opt *op;
462
463         op = findopt(list, name);
464         if (op != NULL) {
465                 SLIST_REMOVE(list, op, opt, op_next);
466                 free(op->op_name);
467                 free(op);
468         }
469 }