]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_files/files-parse.c
Merge glibc-ports into ports/ directory.
[thirdparty/glibc.git] / nss / nss_files / files-parse.c
1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996-2000,2003,2004,2009,2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 /* These symbols are defined by the including source file:
25
26 ENTNAME -- database name of the structure and functions (hostent, pwent).
27 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
28 DATABASE -- string of the database file's name ("hosts", "passwd").
29
30 ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
31 things pointed to by the resultant `struct STRUCTURE'.
32
33 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
34
35 EXTRA_ARGS -- defined iff extra parameters must be passed to the parser
36 EXTRA_ARGS_DECL -- declaration for these extra parameters
37 EXTRA_ARGS_VALUE -- values to be passed for these parameters
38
39 Also see files-XXX.c. */
40
41 #ifndef EXTRA_ARGS
42 # define EXTRA_ARGS
43 # define EXTRA_ARGS_DECL
44 # define EXTRA_ARGS_VALUE
45 #endif
46
47 #define CONCAT(a,b) CONCAT1(a,b)
48 #define CONCAT1(a,b) a##b
49
50 #ifndef STRUCTURE
51 # define STRUCTURE ENTNAME
52 #endif
53
54
55 struct parser_data
56 {
57 #ifdef ENTDATA
58 struct ENTDATA entdata;
59 # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
60 #else
61 # define ENTDATA_DECL(data)
62 #endif
63 char linebuffer[0];
64 };
65
66 #ifdef ENTDATA
67 /* The function can't be exported, because the entdata structure
68 is defined only in files-foo.c. */
69 # define parser_stclass static
70 # define nss_files_parse_hidden_def(name)
71 #else
72 /* Export the line parser function so it can be used in nss_db. */
73 # define parser_stclass /* Global */
74 # define parse_line CONCAT(_nss_files_parse_,ENTNAME)
75 # ifdef IS_IN_libc
76 /* We are defining one of the functions that actually lives in libc
77 because it is used to implement fget*ent and suchlike. */
78 # define nss_files_parse_hidden_def(name) libc_hidden_def (name)
79 # else
80 # define nss_files_parse_hidden_def(name) libnss_files_hidden_def (name)
81 # endif
82 #endif
83
84
85 #ifdef EXTERN_PARSER
86
87 /* The parser is defined in a different module. */
88 extern int parse_line (char *line, struct STRUCTURE *result,
89 struct parser_data *data, size_t datalen, int *errnop
90 EXTRA_ARGS_DECL);
91
92 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
93
94 #else
95
96 /* Define a line parsing function. */
97
98 # define LINE_PARSER(EOLSET, BODY) \
99 parser_stclass int \
100 parse_line (char *line, struct STRUCTURE *result, \
101 struct parser_data *data, size_t datalen, int *errnop \
102 EXTRA_ARGS_DECL) \
103 { \
104 ENTDATA_DECL (data) \
105 BUFFER_PREPARE \
106 char *p = strpbrk (line, EOLSET "\n"); \
107 if (p != NULL) \
108 *p = '\0'; \
109 BODY; \
110 TRAILING_LIST_PARSER; \
111 return 1; \
112 } \
113 nss_files_parse_hidden_def (parse_line)
114
115
116 # define STRING_FIELD(variable, terminator_p, swallow) \
117 { \
118 variable = line; \
119 while (*line != '\0' && !terminator_p (*line)) \
120 ++line; \
121 if (*line != '\0') \
122 { \
123 *line = '\0'; \
124 do \
125 ++line; \
126 while (swallow && terminator_p (*line)); \
127 } \
128 }
129
130 # define STRING_LIST(variable, terminator_c) \
131 { \
132 char **list = parse_list (&line, buf_start, buf_end, terminator_c, \
133 errnop); \
134 if (list) \
135 variable = list; \
136 else \
137 return -1; /* -1 indicates we ran out of space. */ \
138 \
139 /* Determine the new end of the buffer. */ \
140 while (*list != NULL) \
141 ++list; \
142 buf_start = (char *) (list + 1); \
143 }
144
145 /* Helper function. */
146 static inline uint32_t
147 __attribute__ ((always_inline))
148 strtou32 (const char *nptr, char **endptr, int base)
149 {
150 unsigned long int val = strtoul (nptr, endptr, base);
151
152 /* Match the 32-bit behavior on 64-bit platforms. */
153 if (sizeof (long int) > 4 && val > 0xffffffff)
154 val = 0xffffffff;
155
156 return val;
157 }
158
159 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
160 { \
161 char *endp; \
162 variable = convert (strtou32 (line, &endp, base)); \
163 if (endp == line) \
164 return 0; \
165 else if (terminator_p (*endp)) \
166 do \
167 ++endp; \
168 while (swallow && terminator_p (*endp)); \
169 else if (*endp != '\0') \
170 return 0; \
171 line = endp; \
172 }
173
174 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
175 { \
176 char *endp; \
177 if (*line == '\0') \
178 /* We expect some more input, so don't allow the string to end here. */ \
179 return 0; \
180 variable = convert (strtou32 (line, &endp, base)); \
181 if (endp == line) \
182 variable = default; \
183 if (terminator_p (*endp)) \
184 do \
185 ++endp; \
186 while (swallow && terminator_p (*endp)); \
187 else if (*endp != '\0') \
188 return 0; \
189 line = endp; \
190 }
191
192 # define ISCOLON(c) ((c) == ':')
193
194
195 # ifndef TRAILING_LIST_MEMBER
196 # define BUFFER_PREPARE /* Nothing to do. */
197 # define TRAILING_LIST_PARSER /* Nothing to do. */
198 # else
199
200 # define BUFFER_PREPARE \
201 char *buf_start = NULL; \
202 char *buf_end = (char *) data + datalen; \
203 if (line >= data->linebuffer && line < buf_end) \
204 /* Find the end of the line buffer, we will use the space in \
205 DATA after it for storing the vector of pointers. */ \
206 buf_start = strchr (line, '\0') + 1; \
207 else \
208 /* LINE does not point within DATA->linebuffer, so that space is \
209 not being used for scratch space right now. We can use all of \
210 it for the pointer vector storage. */ \
211 buf_start = data->linebuffer; \
212
213 # define TRAILING_LIST_PARSER \
214 { \
215 if (buf_start == NULL) \
216 { \
217 if (line >= data->linebuffer && line < buf_end) \
218 /* Find the end of the line buffer, we will use the space in \
219 DATA after it for storing the vector of pointers. */ \
220 buf_start = strchr (line, '\0') + 1; \
221 else \
222 /* LINE does not point within DATA->linebuffer, so that space is \
223 not being used for scratch space right now. We can use all of \
224 it for the pointer vector storage. */ \
225 buf_start = data->linebuffer; \
226 } \
227 \
228 char **list = parse_list (&line, buf_start, buf_end, '\0', errnop); \
229 if (list) \
230 result->TRAILING_LIST_MEMBER = list; \
231 else \
232 return -1; /* -1 indicates we ran out of space. */ \
233 }
234
235 static inline char **
236 __attribute ((always_inline))
237 parse_list (char **linep, char *eol, char *buf_end, int terminator_c,
238 int *errnop)
239 {
240 char *line = *linep;
241 char **list, **p;
242
243 /* Adjust the pointer so it is aligned for storing pointers. */
244 eol += __alignof__ (char *) - 1;
245 eol -= (eol - (char *) 0) % __alignof__ (char *);
246 /* We will start the storage here for the vector of pointers. */
247 list = (char **) eol;
248
249 p = list;
250 while (1)
251 {
252 if ((char *) (p + 2) > buf_end)
253 {
254 /* We cannot fit another pointer in the buffer. */
255 *errnop = ERANGE;
256 return NULL;
257 }
258
259 if (*line == '\0')
260 break;
261 if (*line == terminator_c)
262 {
263 ++line;
264 break;
265 }
266
267 /* Skip leading white space. This might not be portable but useful. */
268 while (isspace (*line))
269 ++line;
270
271 char *elt = line;
272 while (1)
273 {
274 if (*line == '\0' || *line == terminator_c
275 || TRAILING_LIST_SEPARATOR_P (*line))
276 {
277 /* End of the next entry. */
278 if (line > elt)
279 /* We really found some data. */
280 *p++ = elt;
281
282 /* Terminate string if necessary. */
283 if (*line != '\0')
284 {
285 char endc = *line;
286 *line++ = '\0';
287 if (endc == terminator_c)
288 goto out;
289 }
290 break;
291 }
292 ++line;
293 }
294 }
295 out:
296 *p = NULL;
297 *linep = line;
298
299 return list;
300 }
301
302 # endif /* TRAILING_LIST_MEMBER */
303 #endif /* EXTERN_PARSER */
304
305
306 #define LOOKUP_NAME(nameelt, aliaselt) \
307 { \
308 char **ap; \
309 if (! strcmp (name, result->nameelt)) \
310 break; \
311 for (ap = result->aliaselt; *ap; ++ap) \
312 if (! strcmp (name, *ap)) \
313 break; \
314 if (*ap) \
315 break; \
316 }
317
318 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
319 { \
320 char **ap; \
321 if (! __strcasecmp (name, result->nameelt)) \
322 break; \
323 for (ap = result->aliaselt; *ap; ++ap) \
324 if (! __strcasecmp (name, *ap)) \
325 break; \
326 if (*ap) \
327 break; \
328 }
329
330
331 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
332 #ifndef GENERIC
333 # define GENERIC "files-XXX.c"
334 #endif