xref: /haiku/src/system/libnetwork/netresolv/net/nsparser.y (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 %{
2 /*	$NetBSD: nsparser.y,v 1.12 2012/03/20 17:44:18 matt 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: nsparser.y,v 1.12 2012/03/20 17:44:18 matt Exp $");
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <assert.h>
39 #define _NS_PRIVATE
40 #include <nsswitch.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <syslog.h>
44 
45 int yylex();
46 void yyerror(const char *s);
47 
48 static	void	_nsaddsrctomap(const char *);
49 
50 static	ns_dbt		curdbt;
51 static	ns_src		cursrc;
52 
53 extern char *	_nsyytext;
54 extern int _nsyylineno;
55 %}
56 
57 %name-prefix "_nsyy"
58 
59 %union {
60 	char *str;
61 	int   mapval;
62 }
63 
64 %token	NL
65 %token	SUCCESS UNAVAIL NOTFOUND TRYAGAIN
66 %token	RETURN CONTINUE
67 %token	<str> STRING
68 
69 %type	<mapval> Status Action
70 
71 %%
72 
73 File
74 	:	/* empty */
75 	| Lines
76 	;
77 
78 Lines
79 	: Entry
80 	| Lines Entry
81 	;
82 
83 Entry
84 	: NL
85 	| Database ':' NL
86 	| Database ':' Srclist NL
87 		{
88 			int lineno;
89 
90 			lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
91 			if (_nsdbtput(&curdbt) == -1)
92 				syslog(LOG_WARNING,
93 				    "libc nsdispatch: %s line %d: %s",
94 				    _PATH_NS_CONF, lineno,
95 				    "error adding entry");
96 		}
97 	| error NL
98 		{
99 			yyerrok;
100 		}
101 	;
102 
103 Database
104 	: STRING
105 		{
106 			curdbt.name = yylval.str;
107 			curdbt.srclist = NULL;
108 			curdbt.srclistsize = 0;
109 		}
110 	;
111 
112 Srclist
113 	: Item
114 	| Srclist Item
115 	;
116 
117 Item
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 
129 Criteria
130 	: Criterion
131 	| Criteria Criterion
132 	;
133 
134 Criterion
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 
144 Status
145 	: SUCCESS	{ $$ = NS_SUCCESS; }
146 	| UNAVAIL	{ $$ = NS_UNAVAIL; }
147 	| NOTFOUND	{ $$ = NS_NOTFOUND; }
148 	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
149 	;
150 
151 Action
152 	: RETURN	{ $$ = 1L; }
153 	| CONTINUE	{ $$ = 0L; }
154 	;
155 
156 %%
157 
158 static void
159 _nsaddsrctomap(const char *elem)
160 {
161 	unsigned int	i;
162 	int		lineno;
163 
164 	assert(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 			syslog(LOG_WARNING,
171 			    "libc nsdispatch: %s line %d: %s",
172 			    _PATH_NS_CONF, lineno,
173 			    "'compat' used with other sources");
174 			return;
175 		}
176 	}
177 	for (i = 0; i < curdbt.srclistsize; i++) {
178 		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
179 			syslog(LOG_WARNING,
180 			    "libc nsdispatch: %s line %d: %s '%s'",
181 			    _PATH_NS_CONF, lineno,
182 			    "duplicate source", elem);
183 			return;
184 		}
185 	}
186 	cursrc.name = elem;
187 	if (_nsdbtaddsrc(&curdbt, &cursrc) == -1) {
188 		syslog(LOG_WARNING,
189 		    "libc nsdispatch: %s line %d: %s '%s'",
190 		    _PATH_NS_CONF, lineno,
191 		    "error adding", elem);
192 	}
193 }
194