1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996-2000, 2003, 2004, 2009 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 /* These symbols are defined by the including source file:
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").
31 ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
32 things pointed to by the resultant `struct STRUCTURE'.
34 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
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
40 Also see files-XXX.c. */
44 # define EXTRA_ARGS_DECL
45 # define EXTRA_ARGS_VALUE
48 #define CONCAT(a,b) CONCAT1(a,b)
49 #define CONCAT1(a,b) a##b
52 # define STRUCTURE ENTNAME
59 struct ENTDATA entdata
;
60 # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
62 # define ENTDATA_DECL(data)
68 /* The function can't be exported, because the entdata structure
69 is defined only in files-foo.c. */
70 # define parser_stclass static
71 # define nss_files_parse_hidden_def(name)
73 /* Export the line parser function so it can be used in nss_db. */
74 # define parser_stclass /* Global */
75 # define parse_line CONCAT(_nss_files_parse_,ENTNAME)
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)
81 # define nss_files_parse_hidden_def(name) libnss_files_hidden_def (name)
88 /* The parser is defined in a different module. */
89 extern int parse_line (char *line
, struct STRUCTURE
*result
,
90 struct parser_data
*data
, size_t datalen
, int *errnop
93 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
97 /* Define a line parsing function. */
99 # define LINE_PARSER(EOLSET, BODY) \
101 parse_line (char *line, struct STRUCTURE *result, \
102 struct parser_data *data, size_t datalen, int *errnop \
105 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 /* Helper function. */
131 static inline uint32_t
132 __attribute__ ((always_inline
))
133 strtou32 (const char *nptr
, char **endptr
, int base
)
135 unsigned long int val
= strtoul (nptr
, endptr
, base
);
137 /* Match the 32-bit behavior on 64-bit platforms. */
138 if (sizeof (long int) > 4 && val
> 0xffffffff)
144 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
147 variable = convert (strtou32 (line, &endp, base)); \
150 else if (terminator_p (*endp)) \
153 while (swallow && terminator_p (*endp)); \
154 else if (*endp != '\0') \
159 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
163 /* We expect some more input, so don't allow the string to end here. */ \
165 variable = convert (strtou32 (line, &endp, base)); \
167 variable = default; \
168 if (terminator_p (*endp)) \
171 while (swallow && terminator_p (*endp)); \
172 else if (*endp != '\0') \
177 # define ISCOLON(c) ((c) == ':')
180 # ifndef TRAILING_LIST_MEMBER
181 # define TRAILING_LIST_PARSER /* Nothing to do. */
184 # define TRAILING_LIST_PARSER \
186 char **list = parse_list (line, data, datalen, errnop); \
188 result->TRAILING_LIST_MEMBER = list; \
190 return -1; /* -1 indicates we ran out of space. */ \
193 static inline char **
194 __attribute ((always_inline
))
195 parse_list (char *line
, struct parser_data
*data
, size_t datalen
, int *errnop
)
197 char *eol
, **list
, **p
;
199 if (line
>= data
->linebuffer
&& line
< (char *) data
+ datalen
)
200 /* Find the end of the line buffer, we will use the space in DATA after
201 it for storing the vector of pointers. */
202 eol
= strchr (line
, '\0') + 1;
204 /* LINE does not point within DATA->linebuffer, so that space is
205 not being used for scratch space right now. We can use all of
206 it for the pointer vector storage. */
207 eol
= data
->linebuffer
;
208 /* Adjust the pointer so it is aligned for storing pointers. */
209 eol
+= __alignof__ (char *) - 1;
210 eol
-= (eol
- (char *) 0) % __alignof__ (char *);
211 /* We will start the storage here for the vector of pointers. */
212 list
= (char **) eol
;
219 if ((size_t) ((char *) &p
[1] - (char *) data
) > datalen
)
221 /* We cannot fit another pointer in the buffer. */
228 /* Skip leading white space. This might not be portable but useful. */
229 while (isspace (*line
))
235 if (*line
== '\0' || TRAILING_LIST_SEPARATOR_P (*line
))
237 /* End of the next entry. */
239 /* We really found some data. */
242 /* Terminate string if necessary. */
255 # endif /* TRAILING_LIST_MEMBER */
256 #endif /* EXTERN_PARSER */
259 #define LOOKUP_NAME(nameelt, aliaselt) \
262 if (! strcmp (name, result->nameelt)) \
264 for (ap = result->aliaselt; *ap; ++ap) \
265 if (! strcmp (name, *ap)) \
271 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
274 if (! __strcasecmp (name, result->nameelt)) \
276 for (ap = result->aliaselt; *ap; ++ap) \
277 if (! __strcasecmp (name, *ap)) \
284 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
286 # define GENERIC "files-XXX.c"