]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_files/files-parse.c
tests: replace system by xsystem
[thirdparty/glibc.git] / nss / nss_files / files-parse.c
CommitLineData
5f0e6fc7 1/* Common code for file-based database parsers in nss_files module.
6d7e8eda 2 Copyright (C) 1996-2023 Free Software Foundation, Inc.
2303f5fd
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
2303f5fd
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
2303f5fd 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
5f0e6fc7
RM
18
19#include <ctype.h>
20#include <errno.h>
21#include <string.h>
22#include <stdlib.h>
e054f494 23#include <stdint.h>
e9b23409 24#include <nss_files.h>
5f0e6fc7 25
adc6ff7f
RM
26/* These symbols are defined by the including source file:
27
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").
31
32 ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
c3e2f19b 33 things pointed to by the resultant `struct STRUCTURE'.
adc6ff7f
RM
34
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
36
71d3bda9
UD
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
40
adc6ff7f 41 Also see files-XXX.c. */
5f0e6fc7 42
71d3bda9
UD
43#ifndef EXTRA_ARGS
44# define EXTRA_ARGS
45# define EXTRA_ARGS_DECL
46# define EXTRA_ARGS_VALUE
47#endif
48
5f0e6fc7
RM
49#define CONCAT(a,b) CONCAT1(a,b)
50#define CONCAT1(a,b) a##b
51
52#ifndef STRUCTURE
26761c28 53# define STRUCTURE ENTNAME
5f0e6fc7
RM
54#endif
55
56
57struct parser_data
58 {
3776d592
RM
59#ifdef ENTDATA
60 struct ENTDATA entdata;
26761c28 61# define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
3776d592 62#else
26761c28 63# define ENTDATA_DECL(data)
3776d592 64#endif
5f0e6fc7
RM
65 char linebuffer[0];
66 };
67
3776d592
RM
68#ifdef ENTDATA
69/* The function can't be exported, because the entdata structure
70 is defined only in files-foo.c. */
25337753 71# define parser_stclass static
5656e294 72# define nss_files_parse_hidden_def(name)
3776d592
RM
73#else
74/* Export the line parser function so it can be used in nss_db. */
26761c28
UD
75# define parser_stclass /* Global */
76# define parse_line CONCAT(_nss_files_parse_,ENTNAME)
6212bb67 77# define nss_files_parse_hidden_def(name) libc_hidden_def (name)
3776d592
RM
78#endif
79
96bda0ea
RM
80
81#ifdef EXTERN_PARSER
82
83/* The parser is defined in a different module. */
9980bf0b 84extern int parse_line (char *line, void *result,
71d3bda9
UD
85 struct parser_data *data, size_t datalen, int *errnop
86 EXTRA_ARGS_DECL);
96bda0ea 87
26761c28 88# define LINE_PARSER(EOLSET, BODY) /* Do nothing */
96bda0ea
RM
89
90#else
91
92/* Define a line parsing function. */
93
26761c28 94# define LINE_PARSER(EOLSET, BODY) \
3776d592 95parser_stclass int \
9980bf0b 96parse_line (char *line, void *generic_result, \
71d3bda9
UD
97 struct parser_data *data, size_t datalen, int *errnop \
98 EXTRA_ARGS_DECL) \
5f0e6fc7 99{ \
9980bf0b 100 struct STRUCTURE *result = generic_result; \
ffee1316 101 ENTDATA_DECL (data) \
829fea46 102 BUFFER_PREPARE \
ffee1316 103 char *p = strpbrk (line, EOLSET "\n"); \
26761c28 104 if (p != NULL) \
ffee1316 105 *p = '\0'; \
5f0e6fc7
RM
106 BODY; \
107 TRAILING_LIST_PARSER; \
108 return 1; \
5656e294
RM
109} \
110nss_files_parse_hidden_def (parse_line)
5f0e6fc7
RM
111
112
26761c28 113# define STRING_FIELD(variable, terminator_p, swallow) \
5f0e6fc7
RM
114 { \
115 variable = line; \
adc6ff7f 116 while (*line != '\0' && !terminator_p (*line)) \
5f0e6fc7 117 ++line; \
adc6ff7f
RM
118 if (*line != '\0') \
119 { \
120 *line = '\0'; \
121 do \
122 ++line; \
123 while (swallow && terminator_p (*line)); \
124 } \
5f0e6fc7
RM
125 }
126
829fea46
UD
127# define STRING_LIST(variable, terminator_c) \
128 { \
129 char **list = parse_list (&line, buf_start, buf_end, terminator_c, \
130 errnop); \
131 if (list) \
132 variable = list; \
133 else \
134 return -1; /* -1 indicates we ran out of space. */ \
135 \
136 /* Determine the new end of the buffer. */ \
137 while (*list != NULL) \
138 ++list; \
139 buf_start = (char *) (list + 1); \
140 }
141
1e545d01
UD
142/* Helper function. */
143static inline uint32_t
144__attribute__ ((always_inline))
145strtou32 (const char *nptr, char **endptr, int base)
146{
147 unsigned long int val = strtoul (nptr, endptr, base);
148
149 /* Match the 32-bit behavior on 64-bit platforms. */
150 if (sizeof (long int) > 4 && val > 0xffffffff)
151 val = 0xffffffff;
152
153 return val;
154}
155
26761c28 156# define INT_FIELD(variable, terminator_p, swallow, base, convert) \
5f0e6fc7
RM
157 { \
158 char *endp; \
1e545d01 159 variable = convert (strtou32 (line, &endp, base)); \
5f0e6fc7
RM
160 if (endp == line) \
161 return 0; \
162 else if (terminator_p (*endp)) \
163 do \
164 ++endp; \
165 while (swallow && terminator_p (*endp)); \
166 else if (*endp != '\0') \
167 return 0; \
168 line = endp; \
169 }
170
26761c28 171# define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
569c558c
UD
172 { \
173 char *endp; \
174 if (*line == '\0') \
175 /* We expect some more input, so don't allow the string to end here. */ \
176 return 0; \
1e545d01 177 variable = convert (strtou32 (line, &endp, base)); \
569c558c
UD
178 if (endp == line) \
179 variable = default; \
180 if (terminator_p (*endp)) \
181 do \
182 ++endp; \
183 while (swallow && terminator_p (*endp)); \
184 else if (*endp != '\0') \
185 return 0; \
186 line = endp; \
187 }
188
26761c28 189# define ISCOLON(c) ((c) == ':')
5f0e6fc7
RM
190
191
26761c28 192# ifndef TRAILING_LIST_MEMBER
829fea46 193# define BUFFER_PREPARE /* Nothing to do. */
26761c28
UD
194# define TRAILING_LIST_PARSER /* Nothing to do. */
195# else
5f0e6fc7 196
829fea46
UD
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; \
204 else \
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; \
209
210# define TRAILING_LIST_PARSER \
5f0e6fc7 211{ \
829fea46
UD
212 if (buf_start == NULL) \
213 { \
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; \
218 else \
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; \
223 } \
224 \
225 char **list = parse_list (&line, buf_start, buf_end, '\0', errnop); \
5f0e6fc7
RM
226 if (list) \
227 result->TRAILING_LIST_MEMBER = list; \
c3e2f19b 228 else \
22d57dd3 229 return -1; /* -1 indicates we ran out of space. */ \
5f0e6fc7
RM
230}
231
232static inline char **
9c7ff11a 233__attribute ((always_inline))
829fea46
UD
234parse_list (char **linep, char *eol, char *buf_end, int terminator_c,
235 int *errnop)
5f0e6fc7 236{
829fea46
UD
237 char *line = *linep;
238 char **list, **p;
239
5f0e6fc7 240 /* Adjust the pointer so it is aligned for storing pointers. */
3776d592 241 eol += __alignof__ (char *) - 1;
cc4d6614 242 eol -= ((uintptr_t) eol) % __alignof__ (char *);
5f0e6fc7
RM
243 /* We will start the storage here for the vector of pointers. */
244 list = (char **) eol;
245
246 p = list;
247 while (1)
248 {
829fea46 249 if ((char *) (p + 2) > buf_end)
5f0e6fc7
RM
250 {
251 /* We cannot fit another pointer in the buffer. */
d71b808a 252 *errnop = ERANGE;
5f0e6fc7
RM
253 return NULL;
254 }
829fea46 255
5f0e6fc7
RM
256 if (*line == '\0')
257 break;
829fea46
UD
258 if (*line == terminator_c)
259 {
260 ++line;
261 break;
262 }
5f0e6fc7 263
22d57dd3
UD
264 /* Skip leading white space. This might not be portable but useful. */
265 while (isspace (*line))
266 ++line;
267
829fea46 268 char *elt = line;
5f0e6fc7
RM
269 while (1)
270 {
829fea46
UD
271 if (*line == '\0' || *line == terminator_c
272 || TRAILING_LIST_SEPARATOR_P (*line))
5f0e6fc7 273 {
22d57dd3 274 /* End of the next entry. */
5f0e6fc7 275 if (line > elt)
22d57dd3 276 /* We really found some data. */
5f0e6fc7 277 *p++ = elt;
22d57dd3
UD
278
279 /* Terminate string if necessary. */
280 if (*line != '\0')
829fea46
UD
281 {
282 char endc = *line;
283 *line++ = '\0';
284 if (endc == terminator_c)
285 goto out;
286 }
5f0e6fc7
RM
287 break;
288 }
22d57dd3 289 ++line;
5f0e6fc7
RM
290 }
291 }
829fea46 292 out:
5f0e6fc7 293 *p = NULL;
829fea46 294 *linep = line;
5f0e6fc7
RM
295
296 return list;
297}
298
26761c28 299# endif /* TRAILING_LIST_MEMBER */
96bda0ea
RM
300#endif /* EXTERN_PARSER */
301
302
5f0e6fc7
RM
303#define LOOKUP_NAME(nameelt, aliaselt) \
304{ \
305 char **ap; \
306 if (! strcmp (name, result->nameelt)) \
307 break; \
308 for (ap = result->aliaselt; *ap; ++ap) \
309 if (! strcmp (name, *ap)) \
310 break; \
311 if (*ap) \
312 break; \
313}
314
74015205
UD
315#define LOOKUP_NAME_CASE(nameelt, aliaselt) \
316{ \
317 char **ap; \
14ea22e9 318 if (! __strcasecmp (name, result->nameelt)) \
74015205
UD
319 break; \
320 for (ap = result->aliaselt; *ap; ++ap) \
3b9754f4 321 if (! __strcasecmp (name, *ap)) \
74015205
UD
322 break; \
323 if (*ap) \
324 break; \
325}
326
96bda0ea
RM
327
328/* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
329#ifndef GENERIC
26761c28 330# define GENERIC "files-XXX.c"
96bda0ea 331#endif