Restore change unintentionally killed during merge.
[glibc.git] / glibc-compat / nss_files / files-parse.c
blob49c08153c978c91935dc91382ca3a594161b1434
1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996, 1997, 1998 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 Also see files-XXX.c. */
38 #define CONCAT(a,b) CONCAT1(a,b)
39 #define CONCAT1(a,b) a##b
41 #ifndef STRUCTURE
42 # define STRUCTURE ENTNAME
43 #endif
46 struct parser_data
48 #ifdef ENTDATA
49 struct ENTDATA entdata;
50 # define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
51 #else
52 # define ENTDATA_DECL(data)
53 #endif
54 char linebuffer[0];
57 #ifdef ENTDATA
58 /* The function can't be exported, because the entdata structure
59 is defined only in files-foo.c. */
60 # define parser_stclass static
61 #else
62 /* Export the line parser function so it can be used in nss_db. */
63 # define parser_stclass /* Global */
64 # define parse_line CONCAT(_nss_files_parse_,ENTNAME)
65 #endif
68 #ifdef EXTERN_PARSER
70 /* The parser is defined in a different module. */
71 extern int parse_line (char *line, struct STRUCTURE *result,
72 struct parser_data *data, size_t datalen);
74 # define LINE_PARSER(EOLSET, BODY) /* Do nothing */
76 #else
78 /* Define a line parsing function. */
80 # define LINE_PARSER(EOLSET, BODY) \
81 parser_stclass int \
82 parse_line (char *line, struct STRUCTURE *result, \
83 struct parser_data *data, size_t datalen) \
84 { \
85 ENTDATA_DECL (data) \
86 char *p = strpbrk (line, EOLSET "\n"); \
87 if (p != NULL) \
88 *p = '\0'; \
89 BODY; \
90 TRAILING_LIST_PARSER; \
91 return 1; \
95 # define STRING_FIELD(variable, terminator_p, swallow) \
96 { \
97 variable = line; \
98 while (*line != '\0' && !terminator_p (*line)) \
99 ++line; \
100 if (*line != '\0') \
102 *line = '\0'; \
103 do \
104 ++line; \
105 while (swallow && terminator_p (*line)); \
109 # define INT_FIELD(variable, terminator_p, swallow, base, convert) \
111 char *endp; \
112 variable = convert (strtoul (line, &endp, base)); \
113 if (endp == line) \
114 return 0; \
115 else if (terminator_p (*endp)) \
116 do \
117 ++endp; \
118 while (swallow && terminator_p (*endp)); \
119 else if (*endp != '\0') \
120 return 0; \
121 line = endp; \
124 # define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
126 char *endp; \
127 if (*line == '\0') \
128 /* We expect some more input, so don't allow the string to end here. */ \
129 return 0; \
130 variable = convert (strtoul (line, &endp, base)); \
131 if (endp == line) \
132 variable = default; \
133 if (terminator_p (*endp)) \
134 do \
135 ++endp; \
136 while (swallow && terminator_p (*endp)); \
137 else if (*endp != '\0') \
138 return 0; \
139 line = endp; \
142 # define ISCOLON(c) ((c) == ':')
145 # ifndef TRAILING_LIST_MEMBER
146 # define TRAILING_LIST_PARSER /* Nothing to do. */
147 # else
149 # define TRAILING_LIST_PARSER \
151 char **list = parse_list (line, data, datalen); \
152 if (list) \
153 result->TRAILING_LIST_MEMBER = list; \
154 else \
155 return -1; /* -1 indicates we ran out of space. */ \
158 static inline char **
159 __attribute ((always_inline))
160 parse_list (char *line, struct parser_data *data, size_t datalen)
162 char *eol, **list, **p;
164 if (line >= data->linebuffer && line < (char *) data + datalen)
165 /* Find the end of the line buffer, we will use the space in DATA after
166 it for storing the vector of pointers. */
167 eol = strchr (line, '\0') + 1;
168 else
169 /* LINE does not point within DATA->linebuffer, so that space is
170 not being used for scratch space right now. We can use all of
171 it for the pointer vector storage. */
172 eol = data->linebuffer;
173 /* Adjust the pointer so it is aligned for storing pointers. */
174 eol += __alignof__ (char *) - 1;
175 eol -= (eol - (char *) 0) % __alignof__ (char *);
176 /* We will start the storage here for the vector of pointers. */
177 list = (char **) eol;
179 p = list;
180 while (1)
182 char *elt;
184 if ((size_t) ((char *) &p[1] - (char *) data) > datalen)
186 /* We cannot fit another pointer in the buffer. */
187 __set_errno (ERANGE);
188 return NULL;
190 if (*line == '\0')
191 break;
193 /* Skip leading white space. This might not be portable but useful. */
194 while (isspace (*line))
195 ++line;
197 elt = line;
198 while (1)
200 if (*line == '\0' || TRAILING_LIST_SEPARATOR_P (*line))
202 /* End of the next entry. */
203 if (line > elt)
204 /* We really found some data. */
205 *p++ = elt;
207 /* Terminate string if necessary. */
208 if (*line != '\0')
209 *line++ = '\0';
210 break;
212 ++line;
215 *p = NULL;
217 return list;
220 # endif /* TRAILING_LIST_MEMBER */
221 #endif /* EXTERN_PARSER */
224 #define LOOKUP_NAME(nameelt, aliaselt) \
226 char **ap; \
227 if (! strcmp (name, result->nameelt)) \
228 break; \
229 for (ap = result->aliaselt; *ap; ++ap) \
230 if (! strcmp (name, *ap)) \
231 break; \
232 if (*ap) \
233 break; \
236 #define LOOKUP_NAME_CASE(nameelt, aliaselt) \
238 char **ap; \
239 if (! __strcasecmp (name, result->nameelt)) \
240 break; \
241 for (ap = result->aliaselt; *ap; ++ap) \
242 if (! __strcasecmp (name, *ap)) \
243 break; \
244 if (*ap) \
245 break; \
249 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
250 #ifndef GENERIC
251 # define GENERIC "files-XXX.c"
252 #endif