style(9) cleanup:
[dragonfly.git] / usr.sbin / config / config.y
1 %union {
2         char    *str;
3         int     val;
4         struct  file_list *file;
5 }
6
7 %token  ANY
8 %token  ARCH
9 %token  AT
10 %token  BUS
11 %token  COMMA
12 %token  CONFIG
13 %token  CONFLICTS
14 %token  CONTROLLER
15 %token  CPU
16 %token  DEVICE
17 %token  DISABLE
18 %token  DISK
19 %token  DRIVE
20 %token  DRQ
21 %token  EQUALS
22 %token  FLAGS
23 %token  IDENT
24 %token  IOMEM
25 %token  IOSIZ
26 %token  IRQ
27 %token  MAXUSERS
28 %token  MINUS
29 %token  NEXUS
30 %token  OPTIONS
31 %token  MAKEOPTIONS
32 %token  PORT
33 %token  PSEUDO_DEVICE
34 %token  SEMICOLON
35 %token  TAPE
36 %token  TARGET
37 %token  TTY
38 %token  UNIT
39 %token  VECTOR
40
41 %token  <str>   ID
42 %token  <val>   NUMBER
43 %token  <val>   FPNUMBER
44
45 %type   <str>   Save_id
46 %type   <str>   Opt_value
47 %type   <str>   Dev
48 %type   <str>   device_name
49
50 %{
51
52 /*
53  * Copyright (c) 1988, 1993
54  *      The Regents of the University of California.  All rights reserved.
55  *
56  * Redistribution and use in source and binary forms, with or without
57  * modification, are permitted provided that the following conditions
58  * are met:
59  * 1. Redistributions of source code must retain the above copyright
60  *    notice, this list of conditions and the following disclaimer.
61  * 2. Redistributions in binary form must reproduce the above copyright
62  *    notice, this list of conditions and the following disclaimer in the
63  *    documentation and/or other materials provided with the distribution.
64  * 3. All advertising materials mentioning features or use of this software
65  *    must display the following acknowledgement:
66  *      This product includes software developed by the University of
67  *      California, Berkeley and its contributors.
68  * 4. Neither the name of the University nor the names of its contributors
69  *    may be used to endorse or promote products derived from this software
70  *    without specific prior written permission.
71  *
72  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
73  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
76  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82  * SUCH DAMAGE.
83  *
84  *      @(#)config.y    8.1 (Berkeley) 6/6/93
85  * $FreeBSD: src/usr.sbin/config/config.y,v 1.42.2.1 2001/01/23 00:09:32 peter Exp $
86  * $DragonFly: src/usr.sbin/config/config.y,v 1.7 2004/03/04 20:44:49 eirikn Exp $
87  */
88
89 #include <ctype.h>
90 #include <err.h>
91 #include <stdio.h>
92 #include <string.h>
93
94 #include "config.h"
95
96 static struct   device cur;
97 static struct   device *curp = 0;
98
99 struct  device *dtab;
100 char    *ident;
101 int     yyline;
102 struct  file_list *ftab;
103 char    errbuf[80];
104 int     maxusers;
105
106 #define ns(s)   strdup(s)
107
108 static int connect(char *, int);
109 static void yyerror(char *s);
110
111
112 %}
113 %%
114 Configuration:
115         Many_specs
116                 ;
117
118 Many_specs:
119         Many_specs Spec
120                 |
121         /* lambda */
122                 ;
123
124 Spec:
125         Device_spec SEMICOLON
126               = { newdev(&cur); } |
127         Config_spec SEMICOLON
128                 |
129         SEMICOLON
130                 |
131         error SEMICOLON
132                 ;
133
134 Config_spec:
135         ARCH Save_id
136             = {
137                 if (machinename != NULL) {
138                     errx(1, "%d: only one machine directive is allowed",
139                         yyline);
140                 }
141                 machinename = $2;
142               } |
143         CPU Save_id
144               = {
145                 struct cputype *cp;
146
147                 cp = (struct cputype *)malloc(sizeof(struct cputype));
148                 memset(cp, 0, sizeof(*cp));
149                 cp->cpu_name = $2;
150                 cp->cpu_next = cputype;
151                 cputype = cp;
152               } |
153         OPTIONS Opt_list
154                 |
155         MAKEOPTIONS Mkopt_list
156                 |
157         IDENT ID
158               = { ident = $2; } |
159         System_spec
160                 |
161         MAXUSERS NUMBER
162               = { maxusers = $2; };
163
164 System_spec:
165         CONFIG System_id System_parameter_list
166           = { errx(1,"line %d: root/dump/swap specifications obsolete", yyline);}
167           |
168         CONFIG System_id
169           ;
170
171 System_id:
172         Save_id
173               = {
174                 struct opt *op;
175
176                 op = (struct opt *)malloc(sizeof(struct opt));
177                 memset(op, 0, sizeof(*op));
178                 op->op_name = ns("KERNEL");
179                 op->op_ownfile = 0;
180                 op->op_next = mkopt;
181                 op->op_value = $1;
182                 op->op_line = yyline + 1;
183                 mkopt = op;
184               };
185
186 System_parameter_list:
187           System_parameter_list ID
188         | ID
189         ;
190
191 device_name:
192           Save_id
193                 = { $$ = $1; }
194         | Save_id NUMBER
195                 = {
196                         char buf[80];
197
198                         (void)snprintf(buf, sizeof(buf), "%s%d", $1, $2);
199                         $$ = ns(buf); free($1);
200                 }
201         | Save_id NUMBER ID
202                 = {
203                         char buf[80];
204
205                         (void)snprintf(buf, sizeof(buf), "%s%d%s", $1, $2, $3);
206                         $$ = ns(buf); free($1);
207                 }
208         | Save_id NUMBER ID NUMBER
209                 = {
210                         char buf[80];
211
212                         (void)snprintf(buf, sizeof(buf), "%s%d%s%d",
213                              $1, $2, $3, $4);
214                         $$ = ns(buf); free($1);
215                 }
216         | Save_id NUMBER ID NUMBER ID
217                 = {
218                         char buf[80];
219
220                         (void)snprintf(buf, sizeof(buf), "%s%d%s%d%s",
221                              $1, $2, $3, $4, $5);
222                         $$ = ns(buf); free($1);
223                 }
224         ;
225
226 Opt_list:
227         Opt_list COMMA Option
228                 |
229         Option
230                 ;
231
232 Option:
233         Save_id
234               = {
235                 struct opt *op;
236                 char *s;
237                 
238                 op = (struct opt *)malloc(sizeof(struct opt));
239                 memset(op, 0, sizeof(*op));
240                 op->op_name = $1;
241                 op->op_next = opt;
242                 op->op_value = 0;
243                 /*
244                  * op->op_line is 1-based; yyline is 0-based but is now 1
245                  * larger than when `Save_id' was lexed.
246                  */
247                 op->op_line = yyline;
248                 opt = op;
249                 if ((s = strchr(op->op_name, '=')))
250                         errx(1, "line %d: The `=' in options should not be quoted", yyline);
251               } |
252         Save_id EQUALS Opt_value
253               = {
254                 struct opt *op;
255
256                 op = (struct opt *)malloc(sizeof(struct opt));
257                 memset(op, 0, sizeof(*op));
258                 op->op_name = $1;
259                 op->op_next = opt;
260                 op->op_value = $3;
261                 op->op_line = yyline + 1;
262                 opt = op;
263               } ;
264
265 Opt_value:
266         ID
267                 = { $$ = $1; } |
268         NUMBER
269                 = {
270                         char buf[80];
271
272                         (void)snprintf(buf, sizeof(buf), "%d", $1);
273                         $$ = ns(buf);
274                 } ;
275
276 Save_id:
277         ID
278               = { $$ = $1; }
279         ;
280
281 Mkopt_list:
282         Mkopt_list COMMA Mkoption
283                 |
284         Mkoption
285                 ;
286
287 Mkoption:
288         Save_id EQUALS Opt_value
289               = {
290                 struct opt *op;
291
292                 op = (struct opt *)malloc(sizeof(struct opt));
293                 memset(op, 0, sizeof(*op));
294                 op->op_name = $1;
295                 op->op_ownfile = 0;     /* for now */
296                 op->op_next = mkopt;
297                 op->op_value = $3;
298                 op->op_line = yyline + 1;
299                 mkopt = op;
300               } ;
301
302 Dev:
303         ID
304               = { $$ = $1; }
305         ;
306
307 Device_spec:
308         DEVICE Dev_spec
309               = { cur.d_type = DEVICE; } |
310         DISK Dev_spec
311               = {
312                 errx(1, "line %d: Obsolete keyword 'disk' found - use 'device'", yyline);
313                 } |
314         TAPE Dev_spec
315               = {
316                 errx(1, "line %d: Obsolete keyword 'tape' found - use 'device'", yyline);
317                 } |
318         CONTROLLER Dev_spec
319               = {
320                 errx(1, "line %d: Obsolete keyword 'controller' found - use 'device'", yyline);
321                 } |
322         PSEUDO_DEVICE Init_dev Dev
323               = {
324                 cur.d_name = $3;
325                 cur.d_type = PSEUDO_DEVICE;
326                 } |
327         PSEUDO_DEVICE Init_dev Dev NUMBER
328               = {
329                 cur.d_name = $3;
330                 cur.d_type = PSEUDO_DEVICE;
331                 cur.d_count = $4;
332                 } ;
333
334 Dev_spec:
335         Init_dev Dev
336               = {
337                 cur.d_name = $2;
338                 cur.d_unit = UNKNOWN;
339                 } |
340         Init_dev Dev NUMBER Dev_info
341               = {
342                 cur.d_name = $2;
343                 cur.d_unit = $3;
344                 };
345
346 Init_dev:
347         /* lambda */
348               = { init_dev(&cur); };
349
350 Dev_info:
351         Con_info Info_list
352                 |
353         /* lambda */
354                 ;
355
356 Con_info:
357         AT Dev NUMBER
358               = {
359                 connect($2, $3);
360                 cur.d_conn = $2;
361                 cur.d_connunit = $3;
362                 } |
363         AT NEXUS NUMBER
364               = {
365                 cur.d_conn = "nexus";
366                 cur.d_connunit = 0;
367                 };
368     
369 Info_list:
370         Info_list Info
371                 |
372         /* lambda */
373                 ;
374
375 Info:
376         BUS NUMBER      /* device scbus1 at ahc0 bus 1 - twin channel */
377               = { cur.d_bus = $2; } |
378         TARGET NUMBER
379               = { cur.d_target = $2; } |
380         UNIT NUMBER
381               = { cur.d_lun = $2; } |
382         DRIVE NUMBER
383               = { cur.d_drive = $2; } |
384         IRQ NUMBER
385               = { cur.d_irq = $2; } |
386         DRQ NUMBER
387               = { cur.d_drq = $2; } |
388         IOMEM NUMBER
389               = { cur.d_maddr = $2; } |
390         IOSIZ NUMBER
391               = { cur.d_msize = $2; } |
392         PORT device_name
393               = { cur.d_port = $2; } |
394         PORT NUMBER
395               = { cur.d_portn = $2; } |
396         FLAGS NUMBER
397               = { cur.d_flags = $2; } |
398         DISABLE 
399               = { cur.d_disabled = 1; } |
400         CONFLICTS
401               = {
402                 errx(1, "line %d: Obsolete keyword 'conflicts' found", yyline);
403                 };
404
405 %%
406
407 static void
408 yyerror(char *s)
409 {
410
411         errx(1, "line %d: %s", yyline + 1, s);
412 }
413
414 /*
415  * add a device to the list of devices
416  */
417 static void
418 newdev(struct device *dp)
419 {
420         struct device *np, *xp;
421
422         if (dp->d_unit >= 0) {
423                 for (xp = dtab; xp != NULL; xp = xp->d_next) {
424                         if ((xp->d_unit == dp->d_unit) &&
425                             eq(xp->d_name, dp->d_name)) {
426                                 errx(1, "line %d: already seen device %s%d",
427                                     yyline, xp->d_name, xp->d_unit);
428                         }
429                 }
430         }
431         np = (struct device *)malloc(sizeof(*np));
432         memset(np, 0, sizeof(*np));
433         *np = *dp;
434         np->d_next = NULL;
435         if (curp == NULL)
436                 dtab = np;
437         else
438                 curp->d_next = np;
439         curp = np;
440 }
441
442
443 /*
444  * find the pointer to connect to the given device and number.
445  * returns 0 if no such device and prints an error message
446  */
447 static int
448 connect(char *dev, int num)
449 {
450         struct device *dp;
451
452         if (num == QUES) {
453                 for (dp = dtab; dp != NULL; dp = dp->d_next)
454                         if (eq(dp->d_name, dev))
455                                 break;
456                 if (dp == NULL) {
457                         (void)snprintf(errbuf, sizeof(errbuf),
458                             "no %s's to wildcard", dev);
459                         yyerror(errbuf);
460                         return(0);
461                 }
462                 return(1);
463         }
464         for (dp = dtab; dp != NULL; dp = dp->d_next) {
465                 if ((num != dp->d_unit) || !eq(dev, dp->d_name))
466                         continue;
467                 if (dp->d_type != DEVICE) {
468                         (void)snprintf(errbuf, sizeof(errbuf), 
469                             "%s connected to non-device", dev);
470                         yyerror(errbuf);
471                         return(0);
472                 }
473                 return(1);
474         }
475         (void)snprintf(errbuf, sizeof(errbuf), "%s %d not defined", dev, num);
476         yyerror(errbuf);
477         return(0);
478 }
479
480 void
481 init_dev(struct device *dp)
482 {
483
484         dp->d_name = "OHNO!!!";
485         dp->d_type = DEVICE;
486         dp->d_conn = 0;
487         dp->d_disabled = 0;
488         dp->d_flags = 0;
489         dp->d_bus = dp->d_lun = dp->d_target = dp->d_drive = dp->d_unit = \
490                 dp->d_count = UNKNOWN;
491         dp->d_port = NULL;
492         dp->d_portn = -1;
493         dp->d_irq = -1;
494         dp->d_drq = -1;
495         dp->d_maddr = 0;
496         dp->d_msize = 0;
497 }