Add missing files
[kopensolaris-gnu/glibc.git] / nss / nss_files / files-parse.c
1 /* Common code for file-based database parsers in nss_files module.
2 Copyright (C) 1996 Free Software Foundation, Inc.
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
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.
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
13 Library General Public License for more details.
14
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.  */
19
20 #include <ctype.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 /* These symbols are defined by the including source file:
26
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").
30
31    ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
32               things pointed to by the resultant `struct STRUCTURE'.
33
34    NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
35
36    Also see files-XXX.c.  */
37
38 #define CONCAT(a,b) CONCAT1(a,b)
39 #define CONCAT1(a,b) a##b
40
41 #ifndef STRUCTURE
42 #define STRUCTURE ENTNAME
43 #endif
44
45
46 struct parser_data
47   {
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];
55   };
56
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 inline
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
66
67
68 #ifdef EXTERN_PARSER
69
70 /* The parser is defined in a different module.  */
71 extern int parse_line (char *line, struct STRUCTURE *result,
72                        struct parser_data *data, int datalen);
73
74 #define LINE_PARSER(EOLSET, BODY) /* Do nothing */
75
76 #else
77
78 /* Define a line parsing function.  */
79
80 #define LINE_PARSER(EOLSET, BODY)                                             \
81 parser_stclass int                                                            \
82 parse_line (char *line, struct STRUCTURE *result,                             \
83             struct parser_data *data, int datalen)                            \
84 {                                                                             \
85   ENTDATA_DECL (data)                                                         \
86   char *p = strpbrk (line, EOLSET "\n");                                      \
87   if (p)                                                                      \
88     *p = '\0';                                                                \
89   BODY;                                                                       \
90   TRAILING_LIST_PARSER;                                                       \
91   return 1;                                                                   \
92 }
93
94
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')                                                        \
101       {                                                                       \
102         *line = '\0';                                                         \
103         do                                                                    \
104           ++line;                                                             \
105         while (swallow && terminator_p (*line));                              \
106       }                                                                       \
107   }
108
109 #define INT_FIELD(variable, terminator_p, swallow, base, convert)             \
110   {                                                                           \
111     char *endp;                                                               \
112     variable = convert (strtol (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;                                                              \
122   }
123
124 #define ISCOLON(c) ((c) == ':')
125
126
127 #ifndef TRAILING_LIST_MEMBER
128 #define TRAILING_LIST_PARSER /* Nothing to do.  */
129 #else
130
131 #define TRAILING_LIST_PARSER                                                  \
132 {                                                                             \
133   char **list = parse_list (line, data, datalen);                             \
134   if (list)                                                                   \
135     result->TRAILING_LIST_MEMBER = list;                                      \
136   else                                                                        \
137     return 0;                                                                 \
138 }
139
140 static inline char **
141 parse_list (char *line, struct parser_data *data, int datalen)
142 {
143   char *eol, **list, **p;
144
145   if (line >= data->linebuffer && line < (char *) data + datalen)
146     /* Find the end of the line buffer, we will use the space in DATA after
147        it for storing the vector of pointers.  */
148     eol = strchr (line, '\0') + 1;
149   else
150     /* LINE does not point within DATA->linebuffer, so that space is
151        not being used for scratch space right now.  We can use all of
152        it for the pointer vector storage.  */
153     eol = data->linebuffer;
154   /* Adjust the pointer so it is aligned for storing pointers.  */
155   eol += __alignof__ (char *) - 1;
156   eol -= (eol - (char *) 0) % __alignof__ (char *);
157   /* We will start the storage here for the vector of pointers.  */
158   list = (char **) eol;
159
160   p = list;
161   while (1)
162     {
163       char *elt;
164
165       if ((char *) &p[1] - (char *) data > datalen)
166         {
167           /* We cannot fit another pointer in the buffer.  */
168           errno = ERANGE;
169           return NULL;
170         }
171       if (*line == '\0')
172         break;
173
174       elt = line;
175       while (1)
176         {
177           if (TRAILING_LIST_SEPARATOR_P (*line))
178             {
179               *p++ = elt;
180               *line = '\0';
181               do
182                 ++line;
183               while (TRAILING_LIST_SEPARATOR_P (*line));
184               elt = line;
185             }
186           else if (*line == '\0' || *line == '\n')
187             {
188               /* End of the line.  */
189               if (line > elt)
190                 /* Last element.  */
191                 *p++ = elt;
192               *line = '\0';
193               break;
194             }
195           else
196             ++line;
197         }
198     }
199   *p = NULL;
200
201   return list;
202 }
203
204 #endif  /* EXTERN_PARSER */
205
206
207 #define LOOKUP_NAME(nameelt, aliaselt)                                        \
208 {                                                                             \
209   char **ap;                                                                  \
210   if (! strcmp (name, result->nameelt))                                       \
211     break;                                                                    \
212   for (ap = result->aliaselt; *ap; ++ap)                                      \
213     if (! strcmp (name, *ap))                                                 \
214       break;                                                                  \
215   if (*ap)                                                                    \
216     break;                                                                    \
217 }
218
219 #endif
220
221 /* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead.  */
222 #ifndef GENERIC
223 #define GENERIC "files-XXX.c"
224 #endif