]>
Commit | Line | Data |
---|---|---|
5f0e6fc7 | 1 | /* Common code for file-based database parsers in nss_files module. |
c3e2f19b | 2 | Copyright (C) 1996-2000,2003,2004,2009,2010 Free Software Foundation, Inc. |
2303f5fd UD |
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 | |
41bdb6e2 AJ |
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. | |
2303f5fd UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
2303f5fd | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
5f0e6fc7 RM |
18 | |
19 | #include <ctype.h> | |
20 | #include <errno.h> | |
21 | #include <string.h> | |
22 | #include <stdlib.h> | |
23 | ||
adc6ff7f RM |
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 | |
c3e2f19b | 31 | things pointed to by the resultant `struct STRUCTURE'. |
adc6ff7f RM |
32 | |
33 | NEED_H_ERRNO - defined iff an arg `int *herrnop' is used. | |
34 | ||
71d3bda9 UD |
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 | ||
adc6ff7f | 39 | Also see files-XXX.c. */ |
5f0e6fc7 | 40 | |
71d3bda9 UD |
41 | #ifndef EXTRA_ARGS |
42 | # define EXTRA_ARGS | |
43 | # define EXTRA_ARGS_DECL | |
44 | # define EXTRA_ARGS_VALUE | |
45 | #endif | |
46 | ||
5f0e6fc7 RM |
47 | #define CONCAT(a,b) CONCAT1(a,b) |
48 | #define CONCAT1(a,b) a##b | |
49 | ||
50 | #ifndef STRUCTURE | |
26761c28 | 51 | # define STRUCTURE ENTNAME |
5f0e6fc7 RM |
52 | #endif |
53 | ||
54 | ||
55 | struct parser_data | |
56 | { | |
3776d592 RM |
57 | #ifdef ENTDATA |
58 | struct ENTDATA entdata; | |
26761c28 | 59 | # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata; |
3776d592 | 60 | #else |
26761c28 | 61 | # define ENTDATA_DECL(data) |
3776d592 | 62 | #endif |
5f0e6fc7 RM |
63 | char linebuffer[0]; |
64 | }; | |
65 | ||
3776d592 RM |
66 | #ifdef ENTDATA |
67 | /* The function can't be exported, because the entdata structure | |
68 | is defined only in files-foo.c. */ | |
25337753 | 69 | # define parser_stclass static |
5656e294 | 70 | # define nss_files_parse_hidden_def(name) |
3776d592 RM |
71 | #else |
72 | /* Export the line parser function so it can be used in nss_db. */ | |
26761c28 UD |
73 | # define parser_stclass /* Global */ |
74 | # define parse_line CONCAT(_nss_files_parse_,ENTNAME) | |
4463a2ee RM |
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 | |
3776d592 RM |
82 | #endif |
83 | ||
96bda0ea RM |
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, | |
71d3bda9 UD |
89 | struct parser_data *data, size_t datalen, int *errnop |
90 | EXTRA_ARGS_DECL); | |
96bda0ea | 91 | |
26761c28 | 92 | # define LINE_PARSER(EOLSET, BODY) /* Do nothing */ |
96bda0ea RM |
93 | |
94 | #else | |
95 | ||
96 | /* Define a line parsing function. */ | |
97 | ||
26761c28 | 98 | # define LINE_PARSER(EOLSET, BODY) \ |
3776d592 | 99 | parser_stclass int \ |
5f0e6fc7 | 100 | parse_line (char *line, struct STRUCTURE *result, \ |
71d3bda9 UD |
101 | struct parser_data *data, size_t datalen, int *errnop \ |
102 | EXTRA_ARGS_DECL) \ | |
5f0e6fc7 | 103 | { \ |
ffee1316 | 104 | ENTDATA_DECL (data) \ |
829fea46 | 105 | BUFFER_PREPARE \ |
ffee1316 | 106 | char *p = strpbrk (line, EOLSET "\n"); \ |
26761c28 | 107 | if (p != NULL) \ |
ffee1316 | 108 | *p = '\0'; \ |
5f0e6fc7 RM |
109 | BODY; \ |
110 | TRAILING_LIST_PARSER; \ | |
111 | return 1; \ | |
5656e294 RM |
112 | } \ |
113 | nss_files_parse_hidden_def (parse_line) | |
5f0e6fc7 RM |
114 | |
115 | ||
26761c28 | 116 | # define STRING_FIELD(variable, terminator_p, swallow) \ |
5f0e6fc7 RM |
117 | { \ |
118 | variable = line; \ | |
adc6ff7f | 119 | while (*line != '\0' && !terminator_p (*line)) \ |
5f0e6fc7 | 120 | ++line; \ |
adc6ff7f RM |
121 | if (*line != '\0') \ |
122 | { \ | |
123 | *line = '\0'; \ | |
124 | do \ | |
125 | ++line; \ | |
126 | while (swallow && terminator_p (*line)); \ | |
127 | } \ | |
5f0e6fc7 RM |
128 | } |
129 | ||
829fea46 UD |
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 | ||
1e545d01 UD |
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 | ||
26761c28 | 159 | # define INT_FIELD(variable, terminator_p, swallow, base, convert) \ |
5f0e6fc7 RM |
160 | { \ |
161 | char *endp; \ | |
1e545d01 | 162 | variable = convert (strtou32 (line, &endp, base)); \ |
5f0e6fc7 RM |
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 | ||
26761c28 | 174 | # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \ |
569c558c UD |
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; \ | |
1e545d01 | 180 | variable = convert (strtou32 (line, &endp, base)); \ |
569c558c UD |
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 | ||
26761c28 | 192 | # define ISCOLON(c) ((c) == ':') |
5f0e6fc7 RM |
193 | |
194 | ||
26761c28 | 195 | # ifndef TRAILING_LIST_MEMBER |
829fea46 | 196 | # define BUFFER_PREPARE /* Nothing to do. */ |
26761c28 UD |
197 | # define TRAILING_LIST_PARSER /* Nothing to do. */ |
198 | # else | |
5f0e6fc7 | 199 | |
829fea46 UD |
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 \ | |
5f0e6fc7 | 214 | { \ |
829fea46 UD |
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); \ | |
5f0e6fc7 RM |
229 | if (list) \ |
230 | result->TRAILING_LIST_MEMBER = list; \ | |
c3e2f19b | 231 | else \ |
22d57dd3 | 232 | return -1; /* -1 indicates we ran out of space. */ \ |
5f0e6fc7 RM |
233 | } |
234 | ||
235 | static inline char ** | |
9c7ff11a | 236 | __attribute ((always_inline)) |
829fea46 UD |
237 | parse_list (char **linep, char *eol, char *buf_end, int terminator_c, |
238 | int *errnop) | |
5f0e6fc7 | 239 | { |
829fea46 UD |
240 | char *line = *linep; |
241 | char **list, **p; | |
242 | ||
5f0e6fc7 | 243 | /* Adjust the pointer so it is aligned for storing pointers. */ |
3776d592 RM |
244 | eol += __alignof__ (char *) - 1; |
245 | eol -= (eol - (char *) 0) % __alignof__ (char *); | |
5f0e6fc7 RM |
246 | /* We will start the storage here for the vector of pointers. */ |
247 | list = (char **) eol; | |
248 | ||
249 | p = list; | |
250 | while (1) | |
251 | { | |
829fea46 | 252 | if ((char *) (p + 2) > buf_end) |
5f0e6fc7 RM |
253 | { |
254 | /* We cannot fit another pointer in the buffer. */ | |
d71b808a | 255 | *errnop = ERANGE; |
5f0e6fc7 RM |
256 | return NULL; |
257 | } | |
829fea46 | 258 | |
5f0e6fc7 RM |
259 | if (*line == '\0') |
260 | break; | |
829fea46 UD |
261 | if (*line == terminator_c) |
262 | { | |
263 | ++line; | |
264 | break; | |
265 | } | |
5f0e6fc7 | 266 | |
22d57dd3 UD |
267 | /* Skip leading white space. This might not be portable but useful. */ |
268 | while (isspace (*line)) | |
269 | ++line; | |
270 | ||
829fea46 | 271 | char *elt = line; |
5f0e6fc7 RM |
272 | while (1) |
273 | { | |
829fea46 UD |
274 | if (*line == '\0' || *line == terminator_c |
275 | || TRAILING_LIST_SEPARATOR_P (*line)) | |
5f0e6fc7 | 276 | { |
22d57dd3 | 277 | /* End of the next entry. */ |
5f0e6fc7 | 278 | if (line > elt) |
22d57dd3 | 279 | /* We really found some data. */ |
5f0e6fc7 | 280 | *p++ = elt; |
22d57dd3 UD |
281 | |
282 | /* Terminate string if necessary. */ | |
283 | if (*line != '\0') | |
829fea46 UD |
284 | { |
285 | char endc = *line; | |
286 | *line++ = '\0'; | |
287 | if (endc == terminator_c) | |
288 | goto out; | |
289 | } | |
5f0e6fc7 RM |
290 | break; |
291 | } | |
22d57dd3 | 292 | ++line; |
5f0e6fc7 RM |
293 | } |
294 | } | |
829fea46 | 295 | out: |
5f0e6fc7 | 296 | *p = NULL; |
829fea46 | 297 | *linep = line; |
5f0e6fc7 RM |
298 | |
299 | return list; | |
300 | } | |
301 | ||
26761c28 | 302 | # endif /* TRAILING_LIST_MEMBER */ |
96bda0ea RM |
303 | #endif /* EXTERN_PARSER */ |
304 | ||
305 | ||
5f0e6fc7 RM |
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 | ||
74015205 UD |
318 | #define LOOKUP_NAME_CASE(nameelt, aliaselt) \ |
319 | { \ | |
320 | char **ap; \ | |
14ea22e9 | 321 | if (! __strcasecmp (name, result->nameelt)) \ |
74015205 UD |
322 | break; \ |
323 | for (ap = result->aliaselt; *ap; ++ap) \ | |
3b9754f4 | 324 | if (! __strcasecmp (name, *ap)) \ |
74015205 UD |
325 | break; \ |
326 | if (*ap) \ | |
327 | break; \ | |
328 | } | |
329 | ||
96bda0ea RM |
330 | |
331 | /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */ | |
332 | #ifndef GENERIC | |
26761c28 | 333 | # define GENERIC "files-XXX.c" |
96bda0ea | 334 | #endif |