1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996-2022 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 <https://www.gnu.org/licenses/>. */
24 #include <nss_files.h>
26 /* These symbols are defined by the including source file:
28 ENTNAME -- database name of the structure and functions (hostent, pwent).
29 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
30 DATABASE -- string of the database file's name ("hosts", "passwd").
32 ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
33 things pointed to by the resultant `struct STRUCTURE'.
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
37 EXTRA_ARGS -- defined iff extra parameters must be passed to the parser
38 EXTRA_ARGS_DECL -- declaration for these extra parameters
39 EXTRA_ARGS_VALUE -- values to be passed for these parameters
41 Also see files-XXX.c. */
45 # define EXTRA_ARGS_DECL
46 # define EXTRA_ARGS_VALUE
49 #define CONCAT(a,b) CONCAT1(a,b)
50 #define CONCAT1(a,b) a##b
53 # define STRUCTURE ENTNAME
60 struct ENTDATA entdata
;
61 # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
63 # define ENTDATA_DECL(data)
69 /* The function can't be exported, because the entdata structure
70 is defined only in files-foo.c. */
71 # define parser_stclass static
72 # define nss_files_parse_hidden_def(name)
74 /* Export the line parser function so it can be used in nss_db. */
75 # define parser_stclass /* Global */
76 # define parse_line CONCAT(_nss_files_parse_,ENTNAME)
77 # define nss_files_parse_hidden_def(name) libc_hidden_def (name)
83 /* The parser is defined in a different module. */
84 extern int parse_line (char *line
, void *result
,
85 struct parser_data
*data
, size_t datalen
, int *errnop
88 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
92 /* Define a line parsing function. */
94 # define LINE_PARSER(EOLSET, BODY) \
96 parse_line (char *line, void *generic_result, \
97 struct parser_data *data, size_t datalen, int *errnop \
100 struct STRUCTURE *result = generic_result; \
101 ENTDATA_DECL (data) \
103 char *p = strpbrk (line, EOLSET "\n"); \
107 TRAILING_LIST_PARSER; \
110 nss_files_parse_hidden_def (parse_line)
113 # define STRING_FIELD(variable, terminator_p, swallow) \
116 while (*line != '\0' && !terminator_p (*line)) \
123 while (swallow && terminator_p (*line)); \
127 # define STRING_LIST(variable, terminator_c) \
129 char **list = parse_list (&line, buf_start, buf_end, terminator_c, \
134 return -1; /* -1 indicates we ran out of space. */ \
136 /* Determine the new end of the buffer. */ \
137 while (*list != NULL) \
139 buf_start = (char *) (list + 1); \
142 /* Helper function. */
143 static inline uint32_t
144 __attribute__ ((always_inline
))
145 strtou32 (const char *nptr
, char **endptr
, int base
)
147 unsigned long int val
= strtoul (nptr
, endptr
, base
);
149 /* Match the 32-bit behavior on 64-bit platforms. */
150 if (sizeof (long int) > 4 && val
> 0xffffffff)
156 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
159 variable = convert (strtou32 (line, &endp, base)); \
162 else if (terminator_p (*endp)) \
165 while (swallow && terminator_p (*endp)); \
166 else if (*endp != '\0') \
171 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
175 /* We expect some more input, so don't allow the string to end here. */ \
177 variable = convert (strtou32 (line, &endp, base)); \
179 variable = default; \
180 if (terminator_p (*endp)) \
183 while (swallow && terminator_p (*endp)); \
184 else if (*endp != '\0') \
189 # define ISCOLON(c) ((c) == ':')
192 # ifndef TRAILING_LIST_MEMBER
193 # define BUFFER_PREPARE /* Nothing to do. */
194 # define TRAILING_LIST_PARSER /* Nothing to do. */
197 # define BUFFER_PREPARE \
198 char *buf_start = NULL; \
199 char *buf_end = (char *) data + datalen; \
200 if (line >= data->linebuffer && line < buf_end) \
201 /* Find the end of the line buffer, we will use the space in \
202 DATA after it for storing the vector of pointers. */ \
203 buf_start = strchr (line, '\0') + 1; \
205 /* LINE does not point within DATA->linebuffer, so that space is \
206 not being used for scratch space right now. We can use all of \
207 it for the pointer vector storage. */ \
208 buf_start = data->linebuffer; \
210 # define TRAILING_LIST_PARSER \
212 if (buf_start == NULL) \
214 if (line >= data->linebuffer && line < buf_end) \
215 /* Find the end of the line buffer, we will use the space in \
216 DATA after it for storing the vector of pointers. */ \
217 buf_start = strchr (line, '\0') + 1; \
219 /* LINE does not point within DATA->linebuffer, so that space is \
220 not being used for scratch space right now. We can use all of \
221 it for the pointer vector storage. */ \
222 buf_start = data->linebuffer; \
225 char **list = parse_list (&line, buf_start, buf_end, '\0', errnop); \
227 result->TRAILING_LIST_MEMBER = list; \
229 return -1; /* -1 indicates we ran out of space. */ \
232 static inline char **
233 __attribute ((always_inline
))
234 parse_list (char **linep
, char *eol
, char *buf_end
, int terminator_c
,
240 /* Adjust the pointer so it is aligned for storing pointers. */
241 eol
+= __alignof__ (char *) - 1;
242 eol
-= (eol
- (char *) 0) % __alignof__ (char *);
243 /* We will start the storage here for the vector of pointers. */
244 list
= (char **) eol
;
249 if ((char *) (p
+ 2) > buf_end
)
251 /* We cannot fit another pointer in the buffer. */
258 if (*line
== terminator_c
)
264 /* Skip leading white space. This might not be portable but useful. */
265 while (isspace (*line
))
271 if (*line
== '\0' || *line
== terminator_c
272 || TRAILING_LIST_SEPARATOR_P (*line
))
274 /* End of the next entry. */
276 /* We really found some data. */
279 /* Terminate string if necessary. */
284 if (endc
== terminator_c
)
299 # endif /* TRAILING_LIST_MEMBER */
300 #endif /* EXTERN_PARSER */
303 #define LOOKUP_NAME(nameelt, aliaselt) \
306 if (! strcmp (name, result->nameelt)) \
308 for (ap = result->aliaselt; *ap; ++ap) \
309 if (! strcmp (name, *ap)) \
315 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
318 if (! __strcasecmp (name, result->nameelt)) \
320 for (ap = result->aliaselt; *ap; ++ap) \
321 if (! __strcasecmp (name, *ap)) \
328 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
330 # define GENERIC "files-XXX.c"