xref: /netbsd/lib/libc/net/nsparser.y (revision bf9ec67e)
1 %{
2 /*	$NetBSD: nsparser.y,v 1.8 2002/03/19 00:04:09 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.8 2002/03/19 00:04:09 lukem 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 
55 static	void	_nsaddsrctomap __P((const char *));
56 
57 static	ns_dbt		curdbt;
58 static	ns_src		cursrc;
59 
60 extern char *	_nsyytext;
61 extern 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 
78 File
79 	:	/* empty */
80 	| Lines
81 	;
82 
83 Lines
84 	: Entry
85 	| Lines Entry
86 	;
87 
88 Entry
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 	| error NL
102 		{
103 			yyerrok;
104 		}
105 	;
106 
107 Database
108 	: STRING
109 		{
110 			curdbt.name = yylval.str;
111 			curdbt.srclist = NULL;
112 			curdbt.srclistsize = 0;
113 		}
114 	;
115 
116 Srclist
117 	: Item
118 	| Srclist Item
119 	;
120 
121 Item
122 	: STRING
123 		{
124 			cursrc.flags = NS_SUCCESS;
125 			_nsaddsrctomap($1);
126 		}
127 	| STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']'
128 		{
129 			_nsaddsrctomap($1);
130 		}
131 	;
132 
133 Criteria
134 	: Criterion
135 	| Criteria Criterion
136 	;
137 
138 Criterion
139 	: Status '=' Action
140 		{
141 			if ($3)		/* if action == RETURN set RETURN bit */
142 				cursrc.flags |= $1;
143 			else		/* else unset it */
144 				cursrc.flags &= ~$1;
145 		}
146 	;
147 
148 Status
149 	: SUCCESS	{ $$ = NS_SUCCESS; }
150 	| UNAVAIL	{ $$ = NS_UNAVAIL; }
151 	| NOTFOUND	{ $$ = NS_NOTFOUND; }
152 	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
153 	;
154 
155 Action
156 	: RETURN	{ $$ = 1L; }
157 	| CONTINUE	{ $$ = 0L; }
158 	;
159 
160 %%
161 
162 static void
163 _nsaddsrctomap(elem)
164 	const char *elem;
165 {
166 	int		i, lineno;
167 
168 	_DIAGASSERT(elem != NULL);
169 
170 	lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
171 	if (curdbt.srclistsize > 0) {
172 		if ((strcasecmp(elem, NSSRC_COMPAT) == 0) ||
173 		    (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) {
174 				/* XXX: syslog the following */
175 			warnx("%s line %d: 'compat' used with other sources",
176 			    _PATH_NS_CONF, lineno);
177 			return;
178 		}
179 	}
180 	for (i = 0; i < curdbt.srclistsize; i++) {
181 		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
182 				/* XXX: syslog the following */
183 			warnx("%s line %d: duplicate source '%s'",
184 			    _PATH_NS_CONF, lineno, elem);
185 			return;
186 		}
187 	}
188 	cursrc.name = elem;
189 	if (_nsdbtaddsrc(&curdbt, &cursrc) == -1) {
190 			/* XXX: syslog the following */
191 		warn("%s line %d: error adding '%s'",
192 		    _PATH_NS_CONF, lineno, elem);
193 	}
194 }
195