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	ENVVAR
16%token	EQUALS
17%token	PLUSEQUALS
18%token	HINTS
19%token	IDENT
20%token	MAXUSERS
21%token	PROFILE
22%token	OPTIONS
23%token	NOOPTION
24%token	MAKEOPTIONS
25%token	NOMAKEOPTION
26%token	SEMICOLON
27%token	INCLUDE
28%token	FILES
29
30%token	<str>	ENVLINE
31%token	<str>	ID
32%token	<val>	NUMBER
33
34%type	<str>	Save_id
35%type	<str>	Opt_value
36%type	<str>	Dev
37%token	<str>	PATH
38
39%{
40
41/*-
42 * SPDX-License-Identifier: BSD-3-Clause
43 *
44 * Copyright (c) 1988, 1993
45 *	The Regents of the University of California.  All rights reserved.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 *    notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 *    notice, this list of conditions and the following disclaimer in the
54 *    documentation and/or other materials provided with the distribution.
55 * 3. 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
83struct	device_head dtab;
84char	*ident;
85char	*env;
86int	yyline;
87const	char *yyfile;
88struct  file_list_head ftab;
89struct  files_name_head fntab;
90char	errbuf[80];
91int	maxusers;
92
93#define ns(s)	strdup(s)
94int include(const char *, int);
95void yyerror(const char *s);
96int yywrap(void);
97
98static void newdev(char *name);
99static void newfile(char *name);
100static void newenvvar(char *name, bool is_file);
101static void rmdev_schedule(struct device_head *dh, char *name);
102static void newopt(struct opt_head *list, char *name, char *value, int append, int dupe);
103static void rmopt_schedule(struct opt_head *list, char *name);
104
105static char *
106devopt(char *dev)
107{
108	char *ret = malloc(strlen(dev) + 5);
109
110	sprintf(ret, "DEV_%s", dev);
111	raisestr(ret);
112	return ret;
113}
114
115%}
116%%
117Configuration:
118	Many_specs
119		;
120
121Many_specs:
122	Many_specs Spec
123		|
124	/* lambda */
125		;
126
127Spec:
128	Device_spec SEMICOLON
129		|
130	Config_spec SEMICOLON
131		|
132	INCLUDE PATH SEMICOLON {
133		if (incignore == 0)
134			include($2, 0);
135		};
136		|
137	INCLUDE ID SEMICOLON {
138	          if (incignore == 0)
139		  	include($2, 0);
140		};
141		|
142	FILES ID SEMICOLON { newfile($2); };
143	        |
144	SEMICOLON
145		|
146	error SEMICOLON
147		;
148
149Config_spec:
150	ARCH Save_id {
151		if (machinename != NULL && !eq($2, machinename))
152		    errx(1, "%s:%d: only one machine directive is allowed",
153			yyfile, yyline);
154		machinename = $2;
155		machinearch = $2;
156	      } |
157	ARCH Save_id Save_id {
158		/*
159		 * Allow the machinearch to change with a second machine directive,
160		 * but still enforce no changes to the machinename.
161		 */
162		if (machinename != NULL && !eq($2, machinename))
163		    errx(1, "%s:%d: only one machine directive is allowed",
164			yyfile, yyline);
165		machinename = $2;
166		machinearch = $3;
167	      } |
168	CPU Save_id {
169		struct cputype *cp =
170		    (struct cputype *)calloc(1, sizeof (struct cputype));
171		if (cp == NULL)
172			err(EXIT_FAILURE, "calloc");
173		cp->cpu_name = $2;
174		SLIST_INSERT_HEAD(&cputype, cp, cpu_next);
175	      } |
176	NOCPU Save_id {
177		struct cputype *cp, *cp2;
178		SLIST_FOREACH_SAFE(cp, &cputype, cpu_next, cp2) {
179			if (eq(cp->cpu_name, $2)) {
180				SLIST_REMOVE(&cputype, cp, cputype, cpu_next);
181				free(cp);
182			}
183		}
184	      } |
185	OPTIONS Opt_list
186		|
187	NOOPTION NoOpt_list |
188	MAKEOPTIONS Mkopt_list
189		|
190	NOMAKEOPTION Save_id { rmopt_schedule(&mkopt, $2); } |
191	IDENT ID { ident = $2; } |
192	System_spec
193		|
194	MAXUSERS NUMBER { maxusers = $2; } |
195	PROFILE NUMBER { profiling = $2; } |
196	ENV ID { newenvvar($2, true); } |
197	ENVVAR ENVLINE { newenvvar($2, false); } |
198	HINTS ID {
199		struct hint *hint;
200
201		hint = (struct hint *)calloc(1, sizeof (struct hint));
202		if (hint == NULL)
203			err(EXIT_FAILURE, "calloc");
204		hint->hint_name = $2;
205		STAILQ_INSERT_HEAD(&hints, hint, hint_next);
206	        }
207
208System_spec:
209	CONFIG System_id System_parameter_list {
210		errx(1, "%s:%d: root/dump/swap specifications obsolete",
211		      yyfile, yyline);
212		}
213	  |
214	CONFIG System_id
215	  ;
216
217System_id:
218	Save_id { newopt(&mkopt, ns("KERNEL"), $1, 0, 0); };
219
220System_parameter_list:
221	  System_parameter_list ID
222	| ID
223	;
224
225Opt_list:
226	Opt_list COMMA Option
227		|
228	Option
229		;
230
231NoOpt_list:
232	NoOpt_list COMMA NoOption
233	  	|
234	NoOption
235		;
236Option:
237	Save_id {
238		newopt(&opt, $1, NULL, 0, 1);
239		if (strchr($1, '=') != NULL)
240			errx(1, "%s:%d: The `=' in options should not be "
241			    "quoted", yyfile, yyline);
242	      } |
243	Save_id EQUALS Opt_value {
244		newopt(&opt, $1, $3, 0, 1);
245	      } ;
246
247NoOption:
248	Save_id {
249		rmopt_schedule(&opt, $1);
250		};
251
252Opt_value:
253	ID { $$ = $1; } |
254	NUMBER {
255			char buf[80];
256
257			(void) snprintf(buf, sizeof(buf), "%d", $1);
258			$$ = ns(buf);
259		} ;
260
261Save_id:
262	ID { $$ = $1; }
263	;
264
265Mkopt_list:
266	Mkopt_list COMMA Mkoption
267		|
268	Mkoption
269		;
270
271Mkoption:
272	Save_id { newopt(&mkopt, $1, ns(""), 0, 0); } |
273	Save_id EQUALS { newopt(&mkopt, $1, ns(""), 0, 0); } |
274	Save_id EQUALS Opt_value { newopt(&mkopt, $1, $3, 0, 0); } |
275	Save_id PLUSEQUALS Opt_value { newopt(&mkopt, $1, $3, 1, 0); } ;
276
277Dev:
278	ID { $$ = $1; }
279	;
280
281Device_spec:
282	DEVICE Dev_list
283		|
284	NODEVICE NoDev_list
285		;
286
287Dev_list:
288	Dev_list COMMA Device
289		|
290	Device
291		;
292
293NoDev_list:
294	NoDev_list COMMA NoDevice
295		|
296	NoDevice
297		;
298
299Device:
300	Dev {
301		newopt(&opt, devopt($1), ns("1"), 0, 0);
302		/* and the device part */
303		newdev($1);
304		}
305
306NoDevice:
307	Dev {
308		char *s = devopt($1);
309
310		rmopt_schedule(&opt, s);
311		free(s);
312		/* and the device part */
313		rmdev_schedule(&dtab, $1);
314		} ;
315
316%%
317
318void
319yyerror(const char *s)
320{
321
322	errx(1, "%s:%d: %s", yyfile, yyline + 1, s);
323}
324
325int
326yywrap(void)
327{
328	if (found_defaults) {
329		if (freopen(PREFIX, "r", stdin) == NULL)
330			err(2, "%s", PREFIX);
331		yyfile = PREFIX;
332		yyline = 0;
333		found_defaults = 0;
334		return 0;
335	}
336	return 1;
337}
338
339/*
340 * Add a new file to the list of files.
341 */
342static void
343newfile(char *name)
344{
345	struct files_name *nl;
346
347	nl = (struct files_name *) calloc(1, sizeof *nl);
348	if (nl == NULL)
349		err(EXIT_FAILURE, "calloc");
350	nl->f_name = name;
351	STAILQ_INSERT_TAIL(&fntab, nl, f_next);
352}
353
354static void
355newenvvar(char *name, bool is_file)
356{
357	struct envvar *envvar;
358
359	envvar = (struct envvar *)calloc(1, sizeof (struct envvar));
360	if (envvar == NULL)
361		err(EXIT_FAILURE, "calloc");
362	envvar->env_str = name;
363	envvar->env_is_file = is_file;
364	STAILQ_INSERT_HEAD(&envvars, envvar, envvar_next);
365}
366
367/*
368 * Find a device in the list of devices.
369 */
370static struct device *
371finddev(struct device_head *dlist, char *name)
372{
373	struct device *dp;
374
375	STAILQ_FOREACH(dp, dlist, d_next)
376		if (eq(dp->d_name, name))
377			return (dp);
378
379	return (NULL);
380}
381
382/*
383 * Add a device to the list of devices.
384 */
385static void
386newdev(char *name)
387{
388	struct device *np, *dp;
389
390	if ((dp = finddev(&dtab, name)) != NULL) {
391		if (strcmp(dp->yyfile, yyfile) == 0)
392			fprintf(stderr,
393			    "WARNING: duplicate device `%s' encountered in %s\n",
394			    name, yyfile);
395		return;
396	}
397
398	np = (struct device *) calloc(1, sizeof *np);
399	if (np == NULL)
400		err(EXIT_FAILURE, "calloc");
401	np->d_name = name;
402	np->yyfile = strdup(yyfile);
403	STAILQ_INSERT_TAIL(&dtab, np, d_next);
404}
405
406/*
407 * Schedule a device to removal.
408 */
409static void
410rmdev_schedule(struct device_head *dh, char *name)
411{
412	struct device *dp;
413
414	dp = finddev(dh, name);
415	if (dp != NULL) {
416		STAILQ_REMOVE(dh, dp, device, d_next);
417		free(dp->yyfile);
418		free(dp->d_name);
419		free(dp);
420	}
421}
422
423/*
424 * Find an option in the list of options.
425 */
426static struct opt *
427findopt(struct opt_head *list, char *name)
428{
429	struct opt *op;
430
431	SLIST_FOREACH(op, list, op_next)
432		if (eq(op->op_name, name))
433			return (op);
434
435	return (NULL);
436}
437
438/*
439 * Add an option to the list of options.
440 */
441static void
442newopt(struct opt_head *list, char *name, char *value, int append, int dupe)
443{
444	struct opt *op, *op2;
445
446	/*
447	 * Ignore inclusions listed explicitly for configuration files.
448	 */
449	if (eq(name, OPT_AUTOGEN)) {
450		incignore = 1;
451		return;
452	}
453
454	op2 = findopt(list, name);
455	if (op2 != NULL && !append && !dupe) {
456		if (strcmp(op2->yyfile, yyfile) == 0)
457			fprintf(stderr,
458			    "WARNING: duplicate option `%s' encountered.\n", name);
459		return;
460	}
461
462	op = (struct opt *)calloc(1, sizeof (struct opt));
463	if (op == NULL)
464		err(EXIT_FAILURE, "calloc");
465	op->op_name = name;
466	op->op_ownfile = 0;
467	op->op_value = value;
468	op->yyfile = strdup(yyfile);
469	if (op2 != NULL) {
470		if (append) {
471			while (SLIST_NEXT(op2, op_append) != NULL)
472				op2 = SLIST_NEXT(op2, op_append);
473			SLIST_NEXT(op2, op_append) = op;
474		} else {
475			while (SLIST_NEXT(op2, op_next) != NULL)
476				op2 = SLIST_NEXT(op2, op_next);
477			SLIST_NEXT(op2, op_next) = op;
478		}
479	} else
480		SLIST_INSERT_HEAD(list, op, op_next);
481}
482
483/*
484 * Remove an option from the list of options.
485 */
486static void
487rmopt_schedule(struct opt_head *list, char *name)
488{
489	struct opt *op;
490
491	while ((op = findopt(list, name)) != NULL) {
492		SLIST_REMOVE(list, op, opt, op_next);
493		free(op->yyfile);
494		free(op->op_name);
495		free(op);
496	}
497}
498