Initial import from FreeBSD RELENG_4:
[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  */
87
88 #include <ctype.h>
89 #include <err.h>
90 #include <stdio.h>
91 #include <string.h>
92
93 #include "config.h"
94
95 static struct   device cur;
96 static struct   device *curp = 0;
97
98 struct  device *dtab;
99 char    *ident;
100 int     yyline;
101 struct  file_list *ftab;
102 char    errbuf[80];
103 int     maxusers;
104
105 #define ns(s)   strdup(s)
106
107 static int connect __P((char *, int));
108 static void yyerror __P((char *s));
109
110
111 %}
112 %%
113 Configuration:
114         Many_specs
115                 ;
116
117 Many_specs:
118         Many_specs Spec
119                 |
120         /* lambda */
121                 ;
122
123 Spec:
124         Device_spec SEMICOLON
125               = { newdev(&cur); } |
126         Config_spec SEMICOLON
127                 |
128         SEMICOLON
129                 |
130         error SEMICOLON
131                 ;
132
133 Config_spec:
134         ARCH Save_id
135             = {
136                 if (!strcmp($2, "i386")) {
137                         machine = MACHINE_I386;
138                         machinename = "i386";
139                 } else if (!strcmp($2, "pc98")) {
140                         machine = MACHINE_PC98;
141                         machinename = "pc98";
142                 } else if (!strcmp($2, "alpha")) {
143                         machine = MACHINE_ALPHA;
144                         machinename = "alpha";
145                 } else
146                         yyerror("Unknown machine type");
147               } |
148         CPU Save_id
149               = {
150                 struct cputype *cp =
151                     (struct cputype *)malloc(sizeof (struct cputype));
152                 memset(cp, 0, sizeof(*cp));
153                 cp->cpu_name = $2;
154                 cp->cpu_next = cputype;
155                 cputype = cp;
156               } |
157         OPTIONS Opt_list
158                 |
159         MAKEOPTIONS Mkopt_list
160                 |
161         IDENT ID
162               = { ident = $2; } |
163         System_spec
164                 |
165         MAXUSERS NUMBER
166               = { maxusers = $2; };
167
168 System_spec:
169         CONFIG System_id System_parameter_list
170           = { errx(1,"line %d: root/dump/swap specifications obsolete", yyline);}
171           |
172         CONFIG System_id
173           ;
174
175 System_id:
176         Save_id
177               = {
178                 struct opt *op = (struct opt *)malloc(sizeof (struct opt));
179                 memset(op, 0, sizeof(*op));
180                 op->op_name = ns("KERNEL");
181                 op->op_ownfile = 0;
182                 op->op_next = mkopt;
183                 op->op_value = $1;
184                 op->op_line = yyline + 1;
185                 mkopt = op;
186               };
187
188 System_parameter_list:
189           System_parameter_list ID
190         | ID
191         ;
192
193 device_name:
194           Save_id
195                 = { $$ = $1; }
196         | Save_id NUMBER
197                 = {
198                         char buf[80];
199
200                         (void) snprintf(buf, sizeof(buf), "%s%d", $1, $2);
201                         $$ = ns(buf); free($1);
202                 }
203         | Save_id NUMBER ID
204                 = {
205                         char buf[80];
206
207                         (void) snprintf(buf, sizeof(buf), "%s%d%s", $1, $2, $3);
208                         $$ = ns(buf); free($1);
209                 }
210         | Save_id NUMBER ID NUMBER
211                 = {
212                         char buf[80];
213
214                         (void) snprintf(buf, sizeof(buf), "%s%d%s%d",
215                              $1, $2, $3, $4);
216                         $$ = ns(buf); free($1);
217                 }
218         | Save_id NUMBER ID NUMBER ID
219                 = {
220                         char buf[80];
221
222                         (void) snprintf(buf, sizeof(buf), "%s%d%s%d%s",
223                              $1, $2, $3, $4, $5);
224                         $$ = ns(buf); free($1);
225                 }
226         ;
227
228 Opt_list:
229         Opt_list COMMA Option
230                 |
231         Option
232                 ;
233
234 Option:
235         Save_id
236               = {
237                 struct opt *op = (struct opt *)malloc(sizeof (struct opt));
238                 char *s;
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 = (struct opt *)malloc(sizeof (struct opt));
255                 memset(op, 0, sizeof(*op));
256                 op->op_name = $1;
257                 op->op_next = opt;
258                 op->op_value = $3;
259                 op->op_line = yyline + 1;
260                 opt = op;
261               } ;
262
263 Opt_value:
264         ID
265                 = { $$ = $1; } |
266         NUMBER
267                 = {
268                         char buf[80];
269
270                         (void) snprintf(buf, sizeof(buf), "%d", $1);
271                         $$ = ns(buf);
272                 } ;
273
274 Save_id:
275         ID
276               = { $$ = $1; }
277         ;
278
279 Mkopt_list:
280         Mkopt_list COMMA Mkoption
281                 |
282         Mkoption
283                 ;
284
285 Mkoption:
286         Save_id EQUALS Opt_value
287               = {
288                 struct opt *op = (struct opt *)malloc(sizeof (struct opt));
289                 memset(op, 0, sizeof(*op));
290                 op->op_name = $1;
291                 op->op_ownfile = 0;     /* for now */
292                 op->op_next = mkopt;
293                 op->op_value = $3;
294                 op->op_line = yyline + 1;
295                 mkopt = op;
296               } ;
297
298 Dev:
299         ID
300               = { $$ = $1; }
301         ;
302
303 Device_spec:
304         DEVICE Dev_spec
305               = { cur.d_type = DEVICE; } |
306         DISK Dev_spec
307               = {
308                 errx(1, "line %d: Obsolete keyword 'disk' found - use 'device'", yyline);
309                 } |
310         TAPE Dev_spec
311               = {
312                 errx(1, "line %d: Obsolete keyword 'tape' found - use 'device'", yyline);
313                 } |
314         CONTROLLER Dev_spec
315               = {
316                 errx(1, "line %d: Obsolete keyword 'controller' found - use 'device'", yyline);
317                 } |
318         PSEUDO_DEVICE Init_dev Dev
319               = {
320                 cur.d_name = $3;
321                 cur.d_type = PSEUDO_DEVICE;
322                 } |
323         PSEUDO_DEVICE Init_dev Dev NUMBER
324               = {
325                 cur.d_name = $3;
326                 cur.d_type = PSEUDO_DEVICE;
327                 cur.d_count = $4;
328                 } ;
329
330 Dev_spec:
331         Init_dev Dev
332               = {
333                 cur.d_name = $2;
334                 cur.d_unit = UNKNOWN;
335                 } |
336         Init_dev Dev NUMBER Dev_info
337               = {
338                 cur.d_name = $2;
339                 cur.d_unit = $3;
340                 };
341
342 Init_dev:
343         /* lambda */
344               = { init_dev(&cur); };
345
346 Dev_info:
347         Con_info Info_list
348                 |
349         /* lambda */
350                 ;
351
352 Con_info:
353         AT Dev NUMBER
354               = {
355                 connect($2, $3);
356                 cur.d_conn = $2;
357                 cur.d_connunit = $3;
358                 } |
359         AT NEXUS NUMBER
360               = {
361                 cur.d_conn = "nexus";
362                 cur.d_connunit = 0;
363                 };
364     
365 Info_list:
366         Info_list Info
367                 |
368         /* lambda */
369                 ;
370
371 Info:
372         BUS NUMBER      /* device scbus1 at ahc0 bus 1 - twin channel */
373               = { cur.d_bus = $2; } |
374         TARGET NUMBER
375               = { cur.d_target = $2; } |
376         UNIT NUMBER
377               = { cur.d_lun = $2; } |
378         DRIVE NUMBER
379               = { cur.d_drive = $2; } |
380         IRQ NUMBER
381               = { cur.d_irq = $2; } |
382         DRQ NUMBER
383               = { cur.d_drq = $2; } |
384         IOMEM NUMBER
385               = { cur.d_maddr = $2; } |
386         IOSIZ NUMBER
387               = { cur.d_msize = $2; } |
388         PORT device_name
389               = { cur.d_port = $2; } |
390         PORT NUMBER
391               = { cur.d_portn = $2; } |
392         FLAGS NUMBER
393               = { cur.d_flags = $2; } |
394         DISABLE 
395               = { cur.d_disabled = 1; } |
396         CONFLICTS
397               = {
398                 errx(1, "line %d: Obsolete keyword 'conflicts' found", yyline);
399                 };
400
401 %%
402
403 static void
404 yyerror(s)
405         char *s;
406 {
407
408         errx(1, "line %d: %s", yyline + 1, s);
409 }
410
411 /*
412  * add a device to the list of devices
413  */
414 static void
415 newdev(dp)
416         register struct device *dp;
417 {
418         register struct device *np, *xp;
419
420         if (dp->d_unit >= 0) {
421                 for (xp = dtab; xp != 0; xp = xp->d_next) {
422                         if ((xp->d_unit == dp->d_unit) &&
423                             eq(xp->d_name, dp->d_name)) {
424                                 errx(1, "line %d: already seen device %s%d",
425                                     yyline, xp->d_name, xp->d_unit);
426                         }
427                 }
428         }
429         np = (struct device *) malloc(sizeof *np);
430         memset(np, 0, sizeof(*np));
431         *np = *dp;
432         np->d_next = 0;
433         if (curp == 0)
434                 dtab = np;
435         else
436                 curp->d_next = np;
437         curp = np;
438 }
439
440
441 /*
442  * find the pointer to connect to the given device and number.
443  * returns 0 if no such device and prints an error message
444  */
445 static int
446 connect(dev, num)
447         register char *dev;
448         register int num;
449 {
450         register struct device *dp;
451
452         if (num == QUES) {
453                 for (dp = dtab; dp != 0; dp = dp->d_next)
454                         if (eq(dp->d_name, dev))
455                                 break;
456                 if (dp == 0) {
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 != 0; 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(dp)
482         register struct device *dp;
483 {
484
485         dp->d_name = "OHNO!!!";
486         dp->d_type = DEVICE;
487         dp->d_conn = 0;
488         dp->d_disabled = 0;
489         dp->d_flags = 0;
490         dp->d_bus = dp->d_lun = dp->d_target = dp->d_drive = dp->d_unit = \
491                 dp->d_count = UNKNOWN;
492         dp->d_port = (char *)0;
493         dp->d_portn = -1;
494         dp->d_irq = -1;
495         dp->d_drq = -1;
496         dp->d_maddr = 0;
497         dp->d_msize = 0;
498 }