Update.
[glibc.git] / nss / nss_files / files-parse.c
blob513057f7d46200330d4fc08647d35b6a56a974d5
1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <ctype.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
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. */
42 #ifndef EXTRA_ARGS
43 # define EXTRA_ARGS
44 # define EXTRA_ARGS_DECL
45 # define EXTRA_ARGS_VALUE
46 #endif
48 #define CONCAT(a,b) CONCAT1(a,b)
49 #define CONCAT1(a,b) a##b
51 #ifndef STRUCTURE
52 # define STRUCTURE ENTNAME
53 #endif
56 struct parser_data
58 #ifdef ENTDATA
59 struct ENTDATA entdata;
60 # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
61 #else
62 # define ENTDATA_DECL(data)
63 #endif
64 char linebuffer[0];
67 #ifdef ENTDATA
68 /* The function can't be exported, because the entdata structure
69 is defined only in files-foo.c. */
70 # define parser_stclass static inline
71 #else
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)
75 #endif
78 #ifdef EXTERN_PARSER
80 /* The parser is defined in a different module. */
81 extern int parse_line (char *line, struct STRUCTURE *result,
82 struct parser_data *data, size_t datalen, int *errnop
83 EXTRA_ARGS_DECL);
85 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
87 #else
89 /* Define a line parsing function. */
91 # define LINE_PARSER(EOLSET, BODY) \
92 parser_stclass int \
93 parse_line (char *line, struct STRUCTURE *result, \
94 struct parser_data *data, size_t datalen, int *errnop \
95 EXTRA_ARGS_DECL) \
96 { \
97 ENTDATA_DECL (data) \
98 char *p = strpbrk (line, EOLSET "\n"); \
99 if (p != NULL) \
100 *p = '\0'; \
101 BODY; \
102 TRAILING_LIST_PARSER; \
103 return 1; \
107 # define STRING_FIELD(variable, terminator_p, swallow) \
109 variable = line; \
110 while (*line != '\0' && !terminator_p (*line)) \
111 ++line; \
112 if (*line != '\0') \
114 *line = '\0'; \
115 do \
116 ++line; \
117 while (swallow && terminator_p (*line)); \
121 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
123 char *endp; \
124 variable = convert (strtoul (line, &endp, base)); \
125 if (endp == line) \
126 return 0; \
127 else if (terminator_p (*endp)) \
128 do \
129 ++endp; \
130 while (swallow && terminator_p (*endp)); \
131 else if (*endp != '\0') \
132 return 0; \
133 line = endp; \
136 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
138 char *endp; \
139 if (*line == '\0') \
140 /* We expect some more input, so don't allow the string to end here. */ \
141 return 0; \
142 variable = convert (strtoul (line, &endp, base)); \
143 if (endp == line) \
144 variable = default; \
145 if (terminator_p (*endp)) \
146 do \
147 ++endp; \
148 while (swallow && terminator_p (*endp)); \
149 else if (*endp != '\0') \
150 return 0; \
151 line = endp; \
154 # define ISCOLON(c) ((c) == ':')
157 # ifndef TRAILING_LIST_MEMBER
158 # define TRAILING_LIST_PARSER /* Nothing to do. */
159 # else
161 # define TRAILING_LIST_PARSER \
163 char **list = parse_list (line, data, datalen, errnop); \
164 if (list) \
165 result->TRAILING_LIST_MEMBER = list; \
166 else \
167 return -1; /* -1 indicates we ran out of space. */ \
170 static inline char **
171 parse_list (char *line, struct parser_data *data, size_t datalen, int *errnop)
173 char *eol, **list, **p;
175 if (line >= data->linebuffer && line < (char *) data + datalen)
176 /* Find the end of the line buffer, we will use the space in DATA after
177 it for storing the vector of pointers. */
178 eol = strchr (line, '\0') + 1;
179 else
180 /* LINE does not point within DATA->linebuffer, so that space is
181 not being used for scratch space right now. We can use all of
182 it for the pointer vector storage. */
183 eol = data->linebuffer;
184 /* Adjust the pointer so it is aligned for storing pointers. */
185 eol += __alignof__ (char *) - 1;
186 eol -= (eol - (char *) 0) % __alignof__ (char *);
187 /* We will start the storage here for the vector of pointers. */
188 list = (char **) eol;
190 p = list;
191 while (1)
193 char *elt;
195 if ((size_t) ((char *) &p[1] - (char *) data) > datalen)
197 /* We cannot fit another pointer in the buffer. */
198 *errnop = ERANGE;
199 return NULL;
201 if (*line == '\0')
202 break;
204 /* Skip leading white space. This might not be portable but useful. */
205 while (isspace (*line))
206 ++line;
208 elt = line;
209 while (1)
211 if (*line == '\0' || TRAILING_LIST_SEPARATOR_P (*line))
213 /* End of the next entry. */
214 if (line > elt)
215 /* We really found some data. */
216 *p++ = elt;
218 /* Terminate string if necessary. */
219 if (*line != '\0')
220 *line++ = '\0';
221 break;
223 ++line;
226 *p = NULL;
228 return list;
231 # endif /* TRAILING_LIST_MEMBER */
232 #endif /* EXTERN_PARSER */
235 #define LOOKUP_NAME(nameelt, aliaselt) \
237 char **ap; \
238 if (! strcmp (name, result->nameelt)) \
239 break; \
240 for (ap = result->aliaselt; *ap; ++ap) \
241 if (! strcmp (name, *ap)) \
242 break; \
243 if (*ap) \
244 break; \
247 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
249 char **ap; \
250 if (! __strcasecmp (name, result->nameelt)) \
251 break; \
252 for (ap = result->aliaselt; *ap; ++ap) \
253 if (! __strcasecmp (name, *ap)) \
254 break; \
255 if (*ap) \
256 break; \
260 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
261 #ifndef GENERIC
262 # define GENERIC "files-XXX.c"
263 #endif