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