math: Fix test-fenv.c feupdateenv tests
[glibc.git] / nss / nss_action_parse.c
blob109735a3aa3b7ae46a00b72f12a28846667f111a
1 /* Parse a service line from nsswitch.conf.
2 Copyright (c) 1996-2024 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, see
17 <https://www.gnu.org/licenses/>. */
19 #include <nsswitch.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <stdbool.h>
25 /* Staging area during parsing. */
26 #define DYNARRAY_STRUCT action_list
27 #define DYNARRAY_ELEMENT struct nss_action
28 #define DYNARRAY_PREFIX action_list_
29 #include <malloc/dynarray-skeleton.c>
31 /* Skip whitespace in line[]. */
32 #define SKIP_WS() \
33 while (line[0] != '\0' && isspace (line[0])) \
34 ++line;
36 /* Read the source names:
37 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
39 static bool
40 nss_action_parse (const char *line, struct action_list *result)
42 while (1)
44 SKIP_WS ();
45 if (line[0] == '\0')
46 /* No more sources specified. */
47 return true;
49 /* Read <source> identifier. */
50 const char *name = line;
51 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
52 ++line;
53 if (name == line)
54 return true;
56 struct nss_action new_service
57 = { .module = __nss_module_allocate (name, line - name), };
58 if (new_service.module == NULL)
60 /* Memory allocation error. */
61 action_list_mark_failed (result);
62 return false;
64 nss_action_set_all (&new_service, NSS_ACTION_CONTINUE);
65 nss_action_set (&new_service, NSS_STATUS_SUCCESS, NSS_ACTION_RETURN);
66 nss_action_set (&new_service, NSS_STATUS_RETURN, NSS_ACTION_RETURN);
68 SKIP_WS ();
70 if (line[0] == '[')
72 /* Read criterions. */
74 /* Skip the '['. */
75 ++line;
76 SKIP_WS ();
80 int not;
81 enum nss_status status;
82 lookup_actions action;
84 /* Grok ! before name to mean all statuses but that one. */
85 not = line[0] == '!';
86 if (not)
87 ++line;
89 /* Read status name. */
90 name = line;
91 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
92 && line[0] != ']')
93 ++line;
95 /* Compare with known statuses. */
96 if (line - name == 7)
98 if (__strncasecmp (name, "SUCCESS", 7) == 0)
99 status = NSS_STATUS_SUCCESS;
100 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
101 status = NSS_STATUS_UNAVAIL;
102 else
103 return false;
105 else if (line - name == 8)
107 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
108 status = NSS_STATUS_NOTFOUND;
109 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
110 status = NSS_STATUS_TRYAGAIN;
111 else
112 return false;
114 else
115 return false;
117 SKIP_WS ();
118 if (line[0] != '=')
119 return false;
121 /* Skip the '='. */
122 ++line;
123 SKIP_WS ();
124 name = line;
125 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
126 && line[0] != ']')
127 ++line;
129 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
130 action = NSS_ACTION_RETURN;
131 else if (line - name == 8
132 && __strncasecmp (name, "CONTINUE", 8) == 0)
133 action = NSS_ACTION_CONTINUE;
134 else if (line - name == 5
135 && __strncasecmp (name, "MERGE", 5) == 0)
136 action = NSS_ACTION_MERGE;
137 else
138 return false;
140 if (not)
142 /* Save the current action setting for this status,
143 set them all to the given action, and reset this one. */
144 const lookup_actions save
145 = nss_action_get (&new_service, status);
146 nss_action_set_all (&new_service, action);
147 nss_action_set (&new_service, status, save);
149 else
150 nss_action_set (&new_service, status, action);
152 SKIP_WS ();
154 while (line[0] != ']');
156 /* Skip the ']'. */
157 ++line;
160 action_list_add (result, new_service);
164 nss_action_list
165 __nss_action_parse (const char *line)
167 struct action_list list;
168 action_list_init (&list);
169 if (nss_action_parse (line, &list))
171 size_t size;
172 struct nss_action null_service
173 = { .module = NULL, };
175 action_list_add (&list, null_service);
176 size = action_list_size (&list);
177 return __nss_action_allocate (action_list_begin (&list), size);
179 else if (action_list_has_failed (&list))
181 /* Memory allocation error. */
182 __set_errno (ENOMEM);
183 return NULL;
185 else
187 /* Parse error. */
188 __set_errno (EINVAL);
189 return NULL;