]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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.
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.
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/>. */
24 /* These symbols are defined by the including source file:
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").
30 ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
31 things pointed to by the resultant `struct STRUCTURE'.
33 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
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
39 Also see files-XXX.c. */
43 # define EXTRA_ARGS_DECL
44 # define EXTRA_ARGS_VALUE
47 #define CONCAT(a,b) CONCAT1(a,b)
48 #define CONCAT1(a,b) a##b
51 # define STRUCTURE ENTNAME
58 struct ENTDATA entdata
;
59 # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
61 # define ENTDATA_DECL(data)
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)
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)
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)
80 # define nss_files_parse_hidden_def(name) libnss_files_hidden_def (name)
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
92 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
96 /* Define a line parsing function. */
98 # define LINE_PARSER(EOLSET, BODY) \
100 parse_line (char *line, struct STRUCTURE *result, \
101 struct parser_data *data, size_t datalen, int *errnop \
104 ENTDATA_DECL (data) \
106 char *p = strpbrk (line, EOLSET "\n"); \
110 TRAILING_LIST_PARSER; \
113 nss_files_parse_hidden_def (parse_line)
116 # define STRING_FIELD(variable, terminator_p, swallow) \
119 while (*line != '\0' && !terminator_p (*line)) \
126 while (swallow && terminator_p (*line)); \
130 # define STRING_LIST(variable, terminator_c) \
132 char **list = parse_list (&line, buf_start, buf_end, terminator_c, \
137 return -1; /* -1 indicates we ran out of space. */ \
139 /* Determine the new end of the buffer. */ \
140 while (*list != NULL) \
142 buf_start = (char *) (list + 1); \
145 /* Helper function. */
146 static inline uint32_t
147 __attribute__ ((always_inline
))
148 strtou32 (const char *nptr
, char **endptr
, int base
)
150 unsigned long int val
= strtoul (nptr
, endptr
, base
);
152 /* Match the 32-bit behavior on 64-bit platforms. */
153 if (sizeof (long int) > 4 && val
> 0xffffffff)
159 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
162 variable = convert (strtou32 (line, &endp, base)); \
165 else if (terminator_p (*endp)) \
168 while (swallow && terminator_p (*endp)); \
169 else if (*endp != '\0') \
174 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
178 /* We expect some more input, so don't allow the string to end here. */ \
180 variable = convert (strtou32 (line, &endp, base)); \
182 variable = default; \
183 if (terminator_p (*endp)) \
186 while (swallow && terminator_p (*endp)); \
187 else if (*endp != '\0') \
192 # define ISCOLON(c) ((c) == ':')
195 # ifndef TRAILING_LIST_MEMBER
196 # define BUFFER_PREPARE /* Nothing to do. */
197 # define TRAILING_LIST_PARSER /* Nothing to do. */
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; \
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; \
213 # define TRAILING_LIST_PARSER \
215 if (buf_start == NULL) \
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; \
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; \
228 char **list = parse_list (&line, buf_start, buf_end, '\0', errnop); \
230 result->TRAILING_LIST_MEMBER = list; \
232 return -1; /* -1 indicates we ran out of space. */ \
235 static inline char **
236 __attribute ((always_inline
))
237 parse_list (char **linep
, char *eol
, char *buf_end
, int terminator_c
,
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
;
252 if ((char *) (p
+ 2) > buf_end
)
254 /* We cannot fit another pointer in the buffer. */
261 if (*line
== terminator_c
)
267 /* Skip leading white space. This might not be portable but useful. */
268 while (isspace (*line
))
274 if (*line
== '\0' || *line
== terminator_c
275 || TRAILING_LIST_SEPARATOR_P (*line
))
277 /* End of the next entry. */
279 /* We really found some data. */
282 /* Terminate string if necessary. */
287 if (endc
== terminator_c
)
302 # endif /* TRAILING_LIST_MEMBER */
303 #endif /* EXTERN_PARSER */
306 #define LOOKUP_NAME(nameelt, aliaselt) \
309 if (! strcmp (name, result->nameelt)) \
311 for (ap = result->aliaselt; *ap; ++ap) \
312 if (! strcmp (name, *ap)) \
318 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
321 if (! __strcasecmp (name, result->nameelt)) \
323 for (ap = result->aliaselt; *ap; ++ap) \
324 if (! __strcasecmp (name, *ap)) \
331 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
333 # define GENERIC "files-XXX.c"