GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/config/gram.y Lines: 0 19 0.0 %
Date: 2017-11-13 Branches: 0 12 0.0 %

Line Branch Exec Source
1
%{
2
/*	$OpenBSD: gram.y,v 1.24 2015/01/16 06:40:16 deraadt Exp $	*/
3
/*	$NetBSD: gram.y,v 1.14 1997/02/02 21:12:32 thorpej Exp $	*/
4
5
/*
6
 * Copyright (c) 1992, 1993
7
 *	The Regents of the University of California.  All rights reserved.
8
 *
9
 * This software was developed by the Computer Systems Engineering group
10
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11
 * contributed to Berkeley.
12
 *
13
 * All advertising materials mentioning features or use of this software
14
 * must display the following acknowledgement:
15
 *	This product includes software developed by the University of
16
 *	California, Lawrence Berkeley Laboratories.
17
 *
18
 * Redistribution and use in source and binary forms, with or without
19
 * modification, are permitted provided that the following conditions
20
 * are met:
21
 * 1. Redistributions of source code must retain the above copyright
22
 *    notice, this list of conditions and the following disclaimer.
23
 * 2. Redistributions in binary form must reproduce the above copyright
24
 *    notice, this list of conditions and the following disclaimer in the
25
 *    documentation and/or other materials provided with the distribution.
26
 * 3. Neither the name of the University nor the names of its contributors
27
 *    may be used to endorse or promote products derived from this software
28
 *    without specific prior written permission.
29
 *
30
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
 * SUCH DAMAGE.
41
 *
42
 *	from: @(#)gram.y	8.1 (Berkeley) 6/6/93
43
 */
44
45
#include <sys/param.h>	/* NODEV */
46
#include <sys/types.h>
47
#include <ctype.h>
48
#include <stdio.h>
49
#include <stdlib.h>
50
#include <limits.h>
51
#include <string.h>
52
#include <errno.h>
53
#include "config.h"
54
#include "sem.h"
55
56
#define	FORMAT(n) ((n) > -10 && (n) < 10 ? "%d" : "0x%x")
57
58
#define	stop(s)	error(s), exit(1)
59
60
int	include(const char *, int);
61
void	yyerror(const char *);
62
int	yylex(void);
63
64
static	struct	config conf;	/* at most one active at a time */
65
66
/* the following is used to recover nvlist space after errors */
67
static	struct	nvlist *alloc[1000];
68
static	int	adepth;
69
#define	new0(n,s,p,i,x)	(alloc[adepth++] = newnv(n, s, p, i, x))
70
#define	new_n(n)	new0(n, NULL, NULL, 0, NULL)
71
#define	new_nx(n, x)	new0(n, NULL, NULL, 0, x)
72
#define	new_ns(n, s)	new0(n, s, NULL, 0, NULL)
73
#define	new_si(s, i)	new0(NULL, s, NULL, i, NULL)
74
#define	new_nsi(n,s,i)	new0(n, s, NULL, i, NULL)
75
#define	new_np(n, p)	new0(n, NULL, p, 0, NULL)
76
#define	new_s(s)	new0(NULL, s, NULL, 0, NULL)
77
#define	new_p(p)	new0(NULL, NULL, p, 0, NULL)
78
#define	new_px(p, x)	new0(NULL, NULL, p, 0, x)
79
80
#define	fx_atom(s)	new0(s, NULL, NULL, FX_ATOM, NULL)
81
#define	fx_not(e)	new0(NULL, NULL, NULL, FX_NOT, e)
82
#define	fx_and(e1, e2)	new0(NULL, NULL, e1, FX_AND, e2)
83
#define	fx_or(e1, e2)	new0(NULL, NULL, e1, FX_OR, e2)
84
85
static	void	cleanup(void);
86
static	void	setmachine(const char *, const char *);
87
static	void	check_maxpart(void);
88
89
%}
90
91
%union {
92
	struct	attr *attr;
93
	struct	devbase *devb;
94
	struct	deva *deva;
95
	struct	nvlist *list;
96
	const char *str;
97
	int	val;
98
}
99
100
%token	AND AT ATTACH BUILD COMPILE_WITH CONFIG DEFINE DEFOPT
101
%token	DEVICE DISABLE
102
%token	DUMPS ENDFILE XFILE XOBJECT FLAGS INCLUDE XMACHINE MAJOR MAKEOPTIONS
103
%token	MAXUSERS MAXPARTITIONS MINOR ON OPTIONS PSEUDO_DEVICE ROOT SOURCE SWAP
104
%token	WITH NEEDS_COUNT NEEDS_FLAG RMOPTIONS ENABLE
105
%token	<val> NUMBER
106
%token	<str> PATHNAME WORD EMPTY
107
108
%left '|'
109
%left '&'
110
111
%type	<list>	pathnames
112
%type	<list>	fopts fexpr fatom
113
%type	<val>	fflgs fflag oflgs oflag
114
%type	<str>	rule
115
%type	<attr>	attr
116
%type	<devb>	devbase
117
%type	<deva>	devattach_opt
118
%type	<val>	disable
119
%type	<list>	atlist interface_opt
120
%type	<str>	atname
121
%type	<list>	loclist_opt loclist locdef
122
%type	<str>	locdefault
123
%type	<list>	attrs_opt attrs
124
%type	<list>	locators locator
125
%type	<list>	swapdev_list dev_spec
126
%type	<str>	device_instance
127
%type	<str>	attachment
128
%type	<str>	value
129
%type	<val>	major_minor signed_number npseudo
130
%type	<val>	flags_opt
131
132
%%
133
134
/*
135
 * A configuration consists of a machine type, followed by the machine
136
 * definition files (via the include() mechanism), followed by the
137
 * configuration specification(s) proper.  In effect, this is two
138
 * separate grammars, with some shared terminals and nonterminals.
139
 * Note that we do not have sufficient keywords to enforce any order
140
 * between elements of "topthings" without introducing shift/reduce
141
 * conflicts.  Instead, check order requirements in the C code.
142
 */
143
Configuration:
144
	topthings			/* dirspecs, include "std.arch" */
145
	machine_spec			/* "machine foo" from machine descr. */
146
	dev_defs dev_eof		/* sys/conf/files */
147
	dev_defs dev_eof		/* sys/arch/${MACHINE_ARCH}/... */
148
	dev_defs dev_eof		/* sys/arch/${MACHINE}/... */
149
					{ check_maxpart(); }
150
	specs;				/* rest of machine description */
151
152
topthings:
153
	topthings topthing |
154
	/* empty */;
155
156
topthing:
157
	SOURCE PATHNAME '\n'		{ if (!srcdir) srcdir = $2; } |
158
	BUILD  PATHNAME '\n'		{ if (!builddir) builddir = $2; } |
159
	include '\n' |
160
	'\n';
161
162
machine_spec:
163
	XMACHINE WORD '\n'		{ setmachine($2,NULL); } |
164
	XMACHINE WORD WORD '\n'		{ setmachine($2,$3); } |
165
	error { stop("cannot proceed without machine specifier"); };
166
167
dev_eof:
168
	ENDFILE				{ enddefs(); checkfiles(); };
169
170
pathnames:
171
	PATHNAME			{ $$ = new_nsi($1, NULL, 0); } |
172
	pathnames '|' PATHNAME		{ ($$ = $1)->nv_next = new_nsi($3, NULL, 0); };
173
174
/*
175
 * Various nonterminals shared between the grammars.
176
 */
177
file:
178
	XFILE pathnames fopts fflgs rule { addfile($2, $3, $4, $5); };
179
180
object:
181
	XOBJECT PATHNAME fopts oflgs	{ addobject($2, $3, $4); };
182
183
/* order of options is important, must use right recursion */
184
fopts:
185
	fexpr				{ $$ = $1; } |
186
	/* empty */			{ $$ = NULL; };
187
188
fexpr:
189
	fatom				{ $$ = $1; } |
190
	'!' fatom			{ $$ = fx_not($2); } |
191
	fexpr '&' fexpr			{ $$ = fx_and($1, $3); } |
192
	fexpr '|' fexpr			{ $$ = fx_or($1, $3); } |
193
	'(' fexpr ')'			{ $$ = $2; };
194
195
fatom:
196
	WORD				{ $$ = fx_atom($1); };
197
198
fflgs:
199
	fflgs fflag			{ $$ = $1 | $2; } |
200
	/* empty */			{ $$ = 0; };
201
202
fflag:
203
	NEEDS_COUNT			{ $$ = FI_NEEDSCOUNT; } |
204
	NEEDS_FLAG			{ $$ = FI_NEEDSFLAG; };
205
206
oflgs:
207
	oflgs oflag			{ $$ = $1 | $2; } |
208
	/* empty */			{ $$ = 0; };
209
210
oflag:
211
	NEEDS_FLAG			{ $$ = OI_NEEDSFLAG; };
212
213
rule:
214
	COMPILE_WITH WORD		{ $$ = $2; } |
215
	/* empty */			{ $$ = NULL; };
216
217
include:
218
	INCLUDE WORD			{ include($2, 0); };
219
220
221
/*
222
 * The machine definitions grammar.
223
 */
224
dev_defs:
225
	dev_defs dev_def |
226
	/* empty */;
227
228
dev_def:
229
	one_def '\n'			{ adepth = 0; } |
230
	'\n' |
231
	error '\n'			{ cleanup(); };
232
233
one_def:
234
	file |
235
	object |
236
	include |
237
	DEFINE WORD interface_opt	{ (void)defattr($2, $3); } |
238
	DEFOPT WORD			{ defoption($2); } |
239
	DEVICE devbase interface_opt attrs_opt
240
					{ defdev($2, 0, $3, $4); } |
241
	ATTACH devbase AT atlist devattach_opt attrs_opt
242
					{ defdevattach($5, $2, $4, $6); } |
243
	MAXUSERS NUMBER NUMBER NUMBER	{ setdefmaxusers($2, $3, $4); } |
244
	MAXPARTITIONS NUMBER		{ maxpartitions = $2; } |
245
	PSEUDO_DEVICE devbase attrs_opt { defdev($2,1,NULL,$3); } |
246
	MAJOR '{' majorlist '}';
247
248
disable:
249
	DISABLE				{ $$ = 1; } |
250
	/* empty */			{ $$ = 0; };
251
252
atlist:
253
	atlist ',' atname		{ $$ = new_nx($3, $1); } |
254
	atname				{ $$ = new_n($1); };
255
256
atname:
257
	WORD				{ $$ = $1; } |
258
	ROOT				{ $$ = NULL; };
259
260
devbase:
261
	WORD				{ $$ = getdevbase((char *)$1); };
262
263
devattach_opt:
264
	WITH WORD			{ $$ = getdevattach($2); } |
265
	/* empty */			{ $$ = NULL; };
266
267
interface_opt:
268
	'{' loclist_opt '}'		{ $$ = new_nx("", $2); } |
269
	/* empty */			{ $$ = NULL; };
270
271
loclist_opt:
272
	loclist				{ $$ = $1; } |
273
	/* empty */			{ $$ = NULL; };
274
275
/* loclist order matters, must use right recursion */
276
loclist:
277
	locdef ',' loclist		{ ($$ = $1)->nv_next = $3; } |
278
	locdef				{ $$ = $1; };
279
280
/* "[ WORD locdefault ]" syntax may be unnecessary... */
281
locdef:
282
	WORD locdefault			{ $$ = new_nsi($1, $2, 0); } |
283
	WORD				{ $$ = new_nsi($1, NULL, 0); } |
284
	'[' WORD locdefault ']'		{ $$ = new_nsi($2, $3, 1); };
285
286
locdefault:
287
	'=' value			{ $$ = $2; };
288
289
value:
290
	WORD				{ $$ = $1; } |
291
	EMPTY				{ $$ = $1; } |
292
	signed_number			{
293
						char bf[40];
294
295
						(void)snprintf(bf, sizeof bf,
296
						    FORMAT($1), $1);
297
						$$ = intern(bf);
298
					};
299
300
signed_number:
301
	NUMBER				{ $$ = $1; } |
302
	'-' NUMBER			{ $$ = -$2; };
303
304
attrs_opt:
305
	':' attrs			{ $$ = $2; } |
306
	/* empty */			{ $$ = NULL; };
307
308
attrs:
309
	attrs ',' attr			{ $$ = new_px($3, $1); } |
310
	attr				{ $$ = new_p($1); };
311
312
attr:
313
	WORD				{ $$ = getattr($1); };
314
315
majorlist:
316
	majorlist ',' majordef |
317
	majordef;
318
319
majordef:
320
	devbase '=' NUMBER		{ setmajor($1, $3); };
321
322
323
324
/*
325
 * The configuration grammar.
326
 */
327
specs:
328
	specs spec |
329
	/* empty */;
330
331
spec:
332
	config_spec '\n'		{ adepth = 0; } |
333
	'\n' |
334
	error '\n'			{ cleanup(); };
335
336
config_spec:
337
	file |
338
	object |
339
	include |
340
	OPTIONS opt_list |
341
	RMOPTIONS ropt_list |
342
	MAKEOPTIONS mkopt_list |
343
	MAXUSERS NUMBER			{ setmaxusers($2); } |
344
	CONFIG conf sysparam_list	{ addconf(&conf); } |
345
	PSEUDO_DEVICE WORD npseudo disable { addpseudo($2, $3, $4); } |
346
	device_instance AT attachment ENABLE { enabledev($1, $3); } |
347
	device_instance AT attachment disable locators flags_opt
348
					{ adddev($1, $3, $5, $6, $4); };
349
350
mkopt_list:
351
	mkopt_list ',' mkoption |
352
	mkoption;
353
354
mkoption:
355
	WORD '=' value			{ addmkoption($1, $3); }
356
357
opt_list:
358
	opt_list ',' option |
359
	option;
360
361
ropt_list:
362
	ropt_list ',' WORD { removeoption($3); } |
363
	WORD { removeoption($1); };
364
365
option:
366
	WORD				{ addoption($1, NULL); } |
367
	WORD '=' value			{ addoption($1, $3); };
368
369
conf:
370
	WORD				{ conf.cf_name = $1;
371
					    conf.cf_lineno = currentline();
372
					    conf.cf_root = NULL;
373
					    conf.cf_swap = NULL;
374
					    conf.cf_dump = NULL; };
375
376
sysparam_list:
377
	sysparam_list sysparam |
378
	sysparam;
379
380
sysparam:
381
	ROOT on_opt dev_spec	 { setconf(&conf.cf_root, "root", $3); } |
382
	SWAP on_opt swapdev_list { setconf(&conf.cf_swap, "swap", $3); } |
383
	DUMPS on_opt dev_spec	 { setconf(&conf.cf_dump, "dumps", $3); };
384
385
swapdev_list:
386
	dev_spec AND swapdev_list	{ ($$ = $1)->nv_next = $3; } |
387
	dev_spec			{ $$ = $1; };
388
389
dev_spec:
390
	WORD				{ $$ = new_si($1, NODEV); } |
391
	major_minor			{ $$ = new_si(NULL, $1); };
392
393
major_minor:
394
	MAJOR NUMBER MINOR NUMBER	{ $$ = makedev($2, $4); };
395
396
on_opt:
397
	ON | /* empty */;
398
399
npseudo:
400
	NUMBER				{ $$ = $1; } |
401
	/* empty */			{ $$ = 1; };
402
403
device_instance:
404
	WORD '*'			{ $$ = starref($1); } |
405
	WORD				{ $$ = $1; };
406
407
attachment:
408
	ROOT				{ $$ = NULL; } |
409
	WORD '?'			{ $$ = wildref($1); } |
410
	WORD				{ $$ = $1; };
411
412
locators:
413
	locators locator		{ ($$ = $2)->nv_next = $1; } |
414
	/* empty */			{ $$ = NULL; };
415
416
locator:
417
	WORD value			{ $$ = new_ns($1, $2); } |
418
	WORD '?'			{ $$ = new_ns($1, NULL); };
419
420
flags_opt:
421
	FLAGS NUMBER			{ $$ = $2; } |
422
	/* empty */			{ $$ = 0; };
423
424
%%
425
426
void
427
yyerror(const char *s)
428
{
429
430
	error("%s", s);
431
}
432
433
/*
434
 * Cleanup procedure after syntax error: release any nvlists
435
 * allocated during parsing the current line.
436
 */
437
static void
438
cleanup(void)
439
{
440
	struct nvlist **np;
441
	int i;
442
443
	for (np = alloc, i = adepth; --i >= 0; np++)
444
		nvfree(*np);
445
	adepth = 0;
446
}
447
448
static void
449
setmachine(const char *mch, const char *mcharch)
450
{
451
	char buf[PATH_MAX];
452
453
	machine = mch;
454
	machinearch = mcharch;
455
456
	(void)snprintf(buf, sizeof buf, "arch/%s/conf/files.%s", machine, machine);
457
	if (include(buf, ENDFILE) != 0)
458
		exit(1);
459
460
	if (machinearch != NULL)
461
		(void)snprintf(buf, sizeof buf, "arch/%s/conf/files.%s",
462
		    machinearch, machinearch);
463
	else
464
		strlcpy(buf, _PATH_DEVNULL, sizeof buf);
465
	if (include(buf, ENDFILE) != 0)
466
		exit(1);
467
468
	if (include("conf/files", ENDFILE) != 0)
469
		exit(1);
470
}
471
472
static void
473
check_maxpart(void)
474
{
475
	if (maxpartitions <= 0) {
476
		stop("cannot proceed without maxpartitions specifier");
477
	}
478
}