nsparser.y revision 1.7
1%{
2/*	$NetBSD: nsparser.y,v 1.7 2000/12/20 20:50:46 christos Exp $	*/
3
4/*-
5 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Luke Mewburn.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *        This product includes software developed by the NetBSD
22 *        Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41#if defined(LIBC_SCCS) && !defined(lint)
42__RCSID("$NetBSD: nsparser.y,v 1.7 2000/12/20 20:50:46 christos Exp $");
43#endif /* LIBC_SCCS and not lint */
44
45#include "namespace.h"
46
47#include <assert.h>
48#include <err.h>
49#define _NS_PRIVATE
50#include <nsswitch.h>
51#include <stdio.h>
52#include <string.h>
53
54
55static	void	_nsaddsrctomap __P((const char *));
56
57static	ns_dbt		curdbt;
58static	ns_src		cursrc;
59
60extern char *	_nsyytext;
61extern int _nsyylineno;
62%}
63
64%union {
65	char *str;
66	int   mapval;
67}
68
69%token	NL
70%token	SUCCESS UNAVAIL NOTFOUND TRYAGAIN
71%token	RETURN CONTINUE
72%token	<str> STRING
73
74%type	<mapval> Status Action
75
76%%
77
78File
79	:	/* empty */
80	| Lines
81	;
82
83Lines
84	: Entry
85	| Lines Entry
86	;
87
88Entry
89	: NL
90	| Database ':' NL
91	| Database ':' Srclist NL
92		{
93			int lineno;
94
95			lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
96			if (_nsdbtput(&curdbt) == -1)
97					/* XXX: syslog the following */
98				warn("%s line %d: error adding entry",
99				    _PATH_NS_CONF, lineno);
100		}
101	;
102
103Database
104	: STRING
105		{
106			curdbt.name = yylval.str;
107			curdbt.srclist = NULL;
108			curdbt.srclistsize = 0;
109		}
110	;
111
112Srclist
113	: Item
114	| Srclist Item
115	;
116
117Item
118	: STRING
119		{
120			cursrc.flags = NS_SUCCESS;
121			_nsaddsrctomap($1);
122		}
123	| STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']'
124		{
125			_nsaddsrctomap($1);
126		}
127	;
128
129Criteria
130	: Criterion
131	| Criteria Criterion
132	;
133
134Criterion
135	: Status '=' Action
136		{
137			if ($3)		/* if action == RETURN set RETURN bit */
138				cursrc.flags |= $1;
139			else		/* else unset it */
140				cursrc.flags &= ~$1;
141		}
142	;
143
144Status
145	: SUCCESS	{ $$ = NS_SUCCESS; }
146	| UNAVAIL	{ $$ = NS_UNAVAIL; }
147	| NOTFOUND	{ $$ = NS_NOTFOUND; }
148	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
149	;
150
151Action
152	: RETURN	{ $$ = 1L; }
153	| CONTINUE	{ $$ = 0L; }
154	;
155
156%%
157
158static void
159_nsaddsrctomap(elem)
160	const char *elem;
161{
162	int		i, lineno;
163
164	_DIAGASSERT(elem != NULL);
165
166	lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
167	if (curdbt.srclistsize > 0) {
168		if ((strcasecmp(elem, NSSRC_COMPAT) == 0) ||
169		    (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) {
170				/* XXX: syslog the following */
171			warnx("%s line %d: 'compat' used with other sources",
172			    _PATH_NS_CONF, lineno);
173			return;
174		}
175	}
176	for (i = 0; i < curdbt.srclistsize; i++) {
177		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
178				/* XXX: syslog the following */
179			warnx("%s line %d: duplicate source '%s'",
180			    _PATH_NS_CONF, lineno, elem);
181			return;
182		}
183	}
184	cursrc.name = elem;
185	if (_nsdbtaddsrc(&curdbt, &cursrc) == -1) {
186			/* XXX: syslog the following */
187		warn("%s line %d: error adding '%s'",
188		    _PATH_NS_CONF, lineno, elem);
189	}
190}
191