1 /**
2  * @file
3  * Helper functions to get config values
4  *
5  * @authors
6  * Copyright (C) 2020 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /**
24  * @page config_helpers Helper functions to get config values
25  *
26  * Helper functions to get config values
27  */
28 
29 #include "config.h"
30 #include <assert.h>
31 #include <limits.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include "mutt/lib.h"
36 #include "helpers.h"
37 #include "quad.h"
38 #include "set.h"
39 #include "subset.h"
40 #include "types.h"
41 
42 /**
43  * cs_subset_address - Get an Address config item by name
44  * @param sub   Config Subset
45  * @param name  Name of config item
46  * @retval ptr  Address
47  * @retval NULL Empty address
48  */
cs_subset_address(const struct ConfigSubset * sub,const char * name)49 const struct Address *cs_subset_address(const struct ConfigSubset *sub, const char *name)
50 {
51   assert(sub && name);
52 
53   struct HashElem *he = cs_subset_create_inheritance(sub, name);
54   assert(he);
55 
56 #ifndef NDEBUG
57   struct HashElem *he_base = cs_get_base(he);
58   assert(DTYPE(he_base->type) == DT_ADDRESS);
59 #endif
60 
61   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
62   assert(value != INT_MIN);
63 
64   return (const struct Address *) value;
65 }
66 
67 /**
68  * cs_subset_bool - Get a boolean config item by name
69  * @param sub   Config Subset
70  * @param name  Name of config item
71  * @retval bool  Boolean value
72  */
cs_subset_bool(const struct ConfigSubset * sub,const char * name)73 bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
74 {
75   assert(sub && name);
76 
77   struct HashElem *he = cs_subset_create_inheritance(sub, name);
78   assert(he);
79 
80 #ifndef NDEBUG
81   struct HashElem *he_base = cs_get_base(he);
82   assert(DTYPE(he_base->type) == DT_BOOL);
83 #endif
84 
85   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
86   assert(value != INT_MIN);
87 
88   return (bool) value;
89 }
90 
91 /**
92  * cs_subset_enum - Get a enumeration config item by name
93  * @param sub   Config Subset
94  * @param name  Name of config item
95  * @retval num  Enumeration
96  */
cs_subset_enum(const struct ConfigSubset * sub,const char * name)97 unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
98 {
99   assert(sub && name);
100 
101   struct HashElem *he = cs_subset_create_inheritance(sub, name);
102   assert(he);
103 
104 #ifndef NDEBUG
105   struct HashElem *he_base = cs_get_base(he);
106   assert(DTYPE(he_base->type) == DT_ENUM);
107 #endif
108 
109   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
110   assert(value != INT_MIN);
111 
112   return (unsigned char) value;
113 }
114 
115 /**
116  * cs_subset_long - Get a long config item by name
117  * @param sub   Config Subset
118  * @param name  Name of config item
119  * @retval num Long value
120  */
cs_subset_long(const struct ConfigSubset * sub,const char * name)121 long cs_subset_long(const struct ConfigSubset *sub, const char *name)
122 {
123   assert(sub && name);
124 
125   struct HashElem *he = cs_subset_create_inheritance(sub, name);
126   assert(he);
127 
128 #ifndef NDEBUG
129   struct HashElem *he_base = cs_get_base(he);
130   assert(DTYPE(he_base->type) == DT_LONG);
131 #endif
132 
133   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
134   assert(value != INT_MIN);
135 
136   return (long) value;
137 }
138 
139 /**
140  * cs_subset_mbtable - Get a Multibyte table config item by name
141  * @param sub   Config Subset
142  * @param name  Name of config item
143  * @retval ptr Multibyte table
144  */
cs_subset_mbtable(const struct ConfigSubset * sub,const char * name)145 struct MbTable *cs_subset_mbtable(const struct ConfigSubset *sub, const char *name)
146 {
147   assert(sub && name);
148 
149   struct HashElem *he = cs_subset_create_inheritance(sub, name);
150   assert(he);
151 
152 #ifndef NDEBUG
153   struct HashElem *he_base = cs_get_base(he);
154   assert(DTYPE(he_base->type) == DT_MBTABLE);
155 #endif
156 
157   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
158   assert(value != INT_MIN);
159 
160   return (struct MbTable *) value;
161 }
162 
163 /**
164  * cs_subset_number - Get a number config item by name
165  * @param sub   Config Subset
166  * @param name  Name of config item
167  * @retval num Number
168  */
cs_subset_number(const struct ConfigSubset * sub,const char * name)169 short cs_subset_number(const struct ConfigSubset *sub, const char *name)
170 {
171   assert(sub && name);
172 
173   struct HashElem *he = cs_subset_create_inheritance(sub, name);
174   assert(he);
175 
176 #ifndef NDEBUG
177   struct HashElem *he_base = cs_get_base(he);
178   assert(DTYPE(he_base->type) == DT_NUMBER);
179 #endif
180 
181   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
182   assert(value != INT_MIN);
183 
184   return (short) value;
185 }
186 
187 /**
188  * cs_subset_path - Get a path config item by name
189  * @param sub   Config Subset
190  * @param name  Name of config item
191  * @retval ptr  Path
192  * @retval NULL Empty path
193  */
cs_subset_path(const struct ConfigSubset * sub,const char * name)194 const char *cs_subset_path(const struct ConfigSubset *sub, const char *name)
195 {
196   assert(sub && name);
197 
198   struct HashElem *he = cs_subset_create_inheritance(sub, name);
199   assert(he);
200 
201 #ifndef NDEBUG
202   struct HashElem *he_base = cs_get_base(he);
203   assert(DTYPE(he_base->type) == DT_PATH);
204 #endif
205 
206   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
207   assert(value != INT_MIN);
208 
209   return (const char *) value;
210 }
211 
212 /**
213  * cs_subset_quad - Get a quad-value config item by name
214  * @param sub   Config Subset
215  * @param name  Name of config item
216  * @retval num Quad-value
217  */
cs_subset_quad(const struct ConfigSubset * sub,const char * name)218 enum QuadOption cs_subset_quad(const struct ConfigSubset *sub, const char *name)
219 {
220   assert(sub && name);
221 
222   struct HashElem *he = cs_subset_create_inheritance(sub, name);
223   assert(he);
224 
225 #ifndef NDEBUG
226   struct HashElem *he_base = cs_get_base(he);
227   assert(DTYPE(he_base->type) == DT_QUAD);
228 #endif
229 
230   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
231   assert(value != INT_MIN);
232 
233   return (enum QuadOption) value;
234 }
235 
236 /**
237  * cs_subset_regex - Get a regex config item by name
238  * @param sub   Config Subset
239  * @param name  Name of config item
240  * @retval ptr  Regex
241  * @retval NULL Empty regex
242  */
cs_subset_regex(const struct ConfigSubset * sub,const char * name)243 const struct Regex *cs_subset_regex(const struct ConfigSubset *sub, const char *name)
244 {
245   assert(sub && name);
246 
247   struct HashElem *he = cs_subset_create_inheritance(sub, name);
248   assert(he);
249 
250 #ifndef NDEBUG
251   struct HashElem *he_base = cs_get_base(he);
252   assert(DTYPE(he_base->type) == DT_REGEX);
253 #endif
254 
255   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
256   assert(value != INT_MIN);
257 
258   return (const struct Regex *) value;
259 }
260 
261 /**
262  * cs_subset_slist - Get a string-list config item by name
263  * @param sub   Config Subset
264  * @param name  Name of config item
265  * @retval ptr  String list
266  * @retval NULL Empty string list
267  */
cs_subset_slist(const struct ConfigSubset * sub,const char * name)268 const struct Slist *cs_subset_slist(const struct ConfigSubset *sub, const char *name)
269 {
270   assert(sub && name);
271 
272   struct HashElem *he = cs_subset_create_inheritance(sub, name);
273   assert(he);
274 
275 #ifndef NDEBUG
276   struct HashElem *he_base = cs_get_base(he);
277   assert(DTYPE(he_base->type) == DT_SLIST);
278 #endif
279 
280   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
281   assert(value != INT_MIN);
282 
283   return (const struct Slist *) value;
284 }
285 
286 /**
287  * cs_subset_sort - Get a sort config item by name
288  * @param sub   Config Subset
289  * @param name  Name of config item
290  * @retval num Sort
291  */
cs_subset_sort(const struct ConfigSubset * sub,const char * name)292 short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
293 {
294   assert(sub && name);
295 
296   struct HashElem *he = cs_subset_create_inheritance(sub, name);
297   assert(he);
298 
299 #ifndef NDEBUG
300   struct HashElem *he_base = cs_get_base(he);
301   assert(DTYPE(he_base->type) == DT_SORT);
302 #endif
303 
304   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
305   assert(value != INT_MIN);
306 
307   return (short) value;
308 }
309 
310 /**
311  * cs_subset_string - Get a string config item by name
312  * @param sub   Config Subset
313  * @param name  Name of config item
314  * @retval ptr  String
315  * @retval NULL Empty string
316  */
cs_subset_string(const struct ConfigSubset * sub,const char * name)317 const char *cs_subset_string(const struct ConfigSubset *sub, const char *name)
318 {
319   assert(sub && name);
320 
321   struct HashElem *he = cs_subset_create_inheritance(sub, name);
322   assert(he);
323 
324 #ifndef NDEBUG
325   struct HashElem *he_base = cs_get_base(he);
326   assert(DTYPE(he_base->type) == DT_STRING);
327 #endif
328 
329   intptr_t value = cs_subset_he_native_get(sub, he, NULL);
330   assert(value != INT_MIN);
331 
332   return (const char *) value;
333 }
334