nsparser.y revision 1.9
1%{
2/*	$NetBSD: nsparser.y,v 1.9 2004/01/25 16:38:15 lukem 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.9 2004/01/25 16:38:15 lukem Exp $");
43#endif /* LIBC_SCCS and not lint */
44
45#include "namespace.h"
46
47#include <assert.h>
48#define _NS_PRIVATE
49#include <nsswitch.h>
50#include <stdio.h>
51#include <string.h>
52#include <syslog.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				syslog(LOG_WARNING,
98				    "libc nsdispatch: %s line %d: %s",
99				    _PATH_NS_CONF, lineno,
100				    "error adding entry");
101		}
102	| error NL
103		{
104			yyerrok;
105		}
106	;
107
108Database
109	: STRING
110		{
111			curdbt.name = yylval.str;
112			curdbt.srclist = NULL;
113			curdbt.srclistsize = 0;
114		}
115	;
116
117Srclist
118	: Item
119	| Srclist Item
120	;
121
122Item
123	: STRING
124		{
125			cursrc.flags = NS_SUCCESS;
126			_nsaddsrctomap($1);
127		}
128	| STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']'
129		{
130			_nsaddsrctomap($1);
131		}
132	;
133
134Criteria
135	: Criterion
136	| Criteria Criterion
137	;
138
139Criterion
140	: Status '=' Action
141		{
142			if ($3)		/* if action == RETURN set RETURN bit */
143				cursrc.flags |= $1;
144			else		/* else unset it */
145				cursrc.flags &= ~$1;
146		}
147	;
148
149Status
150	: SUCCESS	{ $$ = NS_SUCCESS; }
151	| UNAVAIL	{ $$ = NS_UNAVAIL; }
152	| NOTFOUND	{ $$ = NS_NOTFOUND; }
153	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
154	;
155
156Action
157	: RETURN	{ $$ = 1L; }
158	| CONTINUE	{ $$ = 0L; }
159	;
160
161%%
162
163static void
164_nsaddsrctomap(elem)
165	const char *elem;
166{
167	int		i, lineno;
168
169	_DIAGASSERT(elem != NULL);
170
171	lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
172	if (curdbt.srclistsize > 0) {
173		if ((strcasecmp(elem, NSSRC_COMPAT) == 0) ||
174		    (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) {
175			syslog(LOG_WARNING,
176			    "libc nsdispatch: %s line %d: %s",
177			    _PATH_NS_CONF, lineno,
178			    "'compat' used with other sources");
179			return;
180		}
181	}
182	for (i = 0; i < curdbt.srclistsize; i++) {
183		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
184			syslog(LOG_WARNING,
185			    "libc nsdispatch: %s line %d: %s '%s'",
186			    _PATH_NS_CONF, lineno,
187			    "duplicate source", elem);
188			return;
189		}
190	}
191	cursrc.name = elem;
192	if (_nsdbtaddsrc(&curdbt, &cursrc) == -1) {
193		syslog(LOG_WARNING,
194		    "libc nsdispatch: %s line %d: %s '%s'",
195		    _PATH_NS_CONF, lineno,
196		    "error adding", elem);
197	}
198}
199