1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996-2021 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)
78 /* We are defining one of the functions that actually lives in libc
79 because it is used to implement fget*ent and suchlike. */
80 # define nss_files_parse_hidden_def(name) libc_hidden_def (name)
82 # define nss_files_parse_hidden_def(name) libnss_files_hidden_def (name)
89 /* The parser is defined in a different module. */
90 extern int parse_line (char *line
, void *result
,
91 struct parser_data
*data
, size_t datalen
, int *errnop
94 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
98 /* Define a line parsing function. */
100 # define LINE_PARSER(EOLSET, BODY) \
102 parse_line (char *line, void *generic_result, \
103 struct parser_data *data, size_t datalen, int *errnop \
106 struct STRUCTURE *result = generic_result; \
107 ENTDATA_DECL (data) \
109 char *p = strpbrk (line, EOLSET "\n"); \
113 TRAILING_LIST_PARSER; \
116 nss_files_parse_hidden_def (parse_line)
119 # define STRING_FIELD(variable, terminator_p, swallow) \
122 while (*line != '\0' && !terminator_p (*line)) \
129 while (swallow && terminator_p (*line)); \
133 # define STRING_LIST(variable, terminator_c) \
135 char **list = parse_list (&line, buf_start, buf_end, terminator_c, \
140 return -1; /* -1 indicates we ran out of space. */ \
142 /* Determine the new end of the buffer. */ \
143 while (*list != NULL) \
145 buf_start = (char *) (list + 1); \
148 /* Helper function. */
149 static inline uint32_t
150 __attribute__ ((always_inline
))
151 strtou32 (const char *nptr
, char **endptr
, int base
)
153 unsigned long int val
= strtoul (nptr
, endptr
, base
);
155 /* Match the 32-bit behavior on 64-bit platforms. */
156 if (sizeof (long int) > 4 && val
> 0xffffffff)
162 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
165 variable = convert (strtou32 (line, &endp, base)); \
168 else if (terminator_p (*endp)) \
171 while (swallow && terminator_p (*endp)); \
172 else if (*endp != '\0') \
177 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
181 /* We expect some more input, so don't allow the string to end here. */ \
183 variable = convert (strtou32 (line, &endp, base)); \
185 variable = default; \
186 if (terminator_p (*endp)) \
189 while (swallow && terminator_p (*endp)); \
190 else if (*endp != '\0') \
195 # define ISCOLON(c) ((c) == ':')
198 # ifndef TRAILING_LIST_MEMBER
199 # define BUFFER_PREPARE /* Nothing to do. */
200 # define TRAILING_LIST_PARSER /* Nothing to do. */
203 # define BUFFER_PREPARE \
204 char *buf_start = NULL; \
205 char *buf_end = (char *) data + datalen; \
206 if (line >= data->linebuffer && line < buf_end) \
207 /* Find the end of the line buffer, we will use the space in \
208 DATA after it for storing the vector of pointers. */ \
209 buf_start = strchr (line, '\0') + 1; \
211 /* LINE does not point within DATA->linebuffer, so that space is \
212 not being used for scratch space right now. We can use all of \
213 it for the pointer vector storage. */ \
214 buf_start = data->linebuffer; \
216 # define TRAILING_LIST_PARSER \
218 if (buf_start == NULL) \
220 if (line >= data->linebuffer && line < buf_end) \
221 /* Find the end of the line buffer, we will use the space in \
222 DATA after it for storing the vector of pointers. */ \
223 buf_start = strchr (line, '\0') + 1; \
225 /* LINE does not point within DATA->linebuffer, so that space is \
226 not being used for scratch space right now. We can use all of \
227 it for the pointer vector storage. */ \
228 buf_start = data->linebuffer; \
231 char **list = parse_list (&line, buf_start, buf_end, '\0', errnop); \
233 result->TRAILING_LIST_MEMBER = list; \
235 return -1; /* -1 indicates we ran out of space. */ \
238 static inline char **
239 __attribute ((always_inline
))
240 parse_list (char **linep
, char *eol
, char *buf_end
, int terminator_c
,
246 /* Adjust the pointer so it is aligned for storing pointers. */
247 eol
+= __alignof__ (char *) - 1;
248 eol
-= (eol
- (char *) 0) % __alignof__ (char *);
249 /* We will start the storage here for the vector of pointers. */
250 list
= (char **) eol
;
255 if ((char *) (p
+ 2) > buf_end
)
257 /* We cannot fit another pointer in the buffer. */
264 if (*line
== terminator_c
)
270 /* Skip leading white space. This might not be portable but useful. */
271 while (isspace (*line
))
277 if (*line
== '\0' || *line
== terminator_c
278 || TRAILING_LIST_SEPARATOR_P (*line
))
280 /* End of the next entry. */
282 /* We really found some data. */
285 /* Terminate string if necessary. */
290 if (endc
== terminator_c
)
305 # endif /* TRAILING_LIST_MEMBER */
306 #endif /* EXTERN_PARSER */
309 #define LOOKUP_NAME(nameelt, aliaselt) \
312 if (! strcmp (name, result->nameelt)) \
314 for (ap = result->aliaselt; *ap; ++ap) \
315 if (! strcmp (name, *ap)) \
321 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
324 if (! __strcasecmp (name, result->nameelt)) \
326 for (ap = result->aliaselt; *ap; ++ap) \
327 if (! __strcasecmp (name, *ap)) \
334 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
336 # define GENERIC "files-XXX.c"