xref: /netbsd/lib/libc/net/nsparser.y (revision 645b10c9)
1 %{
2 /*	$NetBSD: nsparser.y,v 1.3 1999/01/25 00:16:18 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.3 1999/01/25 00:16:18 lukem Exp $");
43 #endif /* LIBC_SCCS and not lint */
44 
45 #include <err.h>
46 #define _NS_PRIVATE
47 #include <nsswitch.h>
48 #include <stdio.h>
49 #include <string.h>
50 
51 
52 static	void	_nsaddsrctomap __P((const char *));
53 
54 static	ns_dbt		curdbt;
55 static	ns_src		cursrc;
56 %}
57 
58 %union {
59 	char *str;
60 	int   mapval;
61 }
62 
63 %token	NL
64 %token	SUCCESS UNAVAIL NOTFOUND TRYAGAIN
65 %token	RETURN CONTINUE
66 %token	<str> STRING
67 
68 %type	<mapval> Status Action
69 
70 %%
71 
72 File
73 	:	/* empty */
74 	| Lines
75 	;
76 
77 Lines
78 	: Entry
79 	| Lines Entry
80 	;
81 
82 Entry
83 	: NL
84 	| Database ':' NL
85 	| Database ':' Srclist NL
86 		{
87 			_nsdbtput(&curdbt);
88 		}
89 	;
90 
91 Database
92 	: STRING
93 		{
94 			curdbt.name = yylval.str;
95 			curdbt.srclist = NULL;
96 			curdbt.srclistsize = 0;
97 		}
98 	;
99 
100 Srclist
101 	: Item
102 	| Srclist Item
103 	;
104 
105 Item
106 	: STRING
107 		{
108 			cursrc.flags = NS_SUCCESS;
109 			_nsaddsrctomap($1);
110 		}
111 	| STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']'
112 		{
113 			_nsaddsrctomap($1);
114 		}
115 	;
116 
117 Criteria
118 	: Criterion
119 	| Criteria Criterion
120 	;
121 
122 Criterion
123 	: Status '=' Action
124 		{
125 			if ($3)		/* if action == RETURN set RETURN bit */
126 				cursrc.flags |= $1;
127 			else		/* else unset it */
128 				cursrc.flags &= ~$1;
129 		}
130 	;
131 
132 Status
133 	: SUCCESS	{ $$ = NS_SUCCESS; }
134 	| UNAVAIL	{ $$ = NS_UNAVAIL; }
135 	| NOTFOUND	{ $$ = NS_NOTFOUND; }
136 	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
137 	;
138 
139 Action
140 	: RETURN	{ $$ = 1L; }
141 	| CONTINUE	{ $$ = 0L; }
142 	;
143 
144 %%
145 
146 static void
147 _nsaddsrctomap(elem)
148 	const char *elem;
149 {
150 	int		i, lineno;
151 	extern int	_nsyylineno;
152 	extern char *	_nsyytext;
153 
154 	lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
155 	if (curdbt.srclistsize > 0) {
156 		if ((strcasecmp(elem, NSSRC_COMPAT) == 0) ||
157 		    (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) {
158 				/* XXX: syslog the following */
159 			warnx("%s line %d: 'compat' used with other sources",
160 			    _PATH_NS_CONF, lineno);
161 			return;
162 		}
163 	}
164 	for (i = 0; i < curdbt.srclistsize; i++) {
165 		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
166 				/* XXX: syslog the following */
167 			warnx("%s line %d: duplicate source '%s'",
168 			    _PATH_NS_CONF, lineno, elem);
169 			return;
170 		}
171 	}
172 	cursrc.name = elem;
173 	_nsdbtaddsrc(&curdbt, &cursrc);
174 }
175