1 /**
2  * @file
3  * Type representing a number
4  *
5  * @authors
6  * Copyright (C) 2017-2018 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_number Type: Number
25  *
26  * Config type representing a number.
27  *
28  * - Backed by `short`
29  * - Validator is passed `short`
30  * - Implementation: #CstNumber
31  */
32 
33 #include "config.h"
34 #include <stddef.h>
35 #include <limits.h>
36 #include <stdint.h>
37 #include "mutt/lib.h"
38 #include "set.h"
39 #include "types.h"
40 
41 /**
42  * number_string_set - Set a Number by string - Implements ConfigSetType::string_set() - @ingroup cfg_type_string_set
43  */
number_string_set(const struct ConfigSet * cs,void * var,struct ConfigDef * cdef,const char * value,struct Buffer * err)44 static int number_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
45                              const char *value, struct Buffer *err)
46 {
47   if (!value || (value[0] == '\0'))
48   {
49     mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
50     return CSR_ERR_INVALID | CSR_INV_TYPE;
51   }
52 
53   int num = 0;
54   if (mutt_str_atoi(value, &num) < 0)
55   {
56     mutt_buffer_printf(err, _("Invalid number: %s"), value);
57     return CSR_ERR_INVALID | CSR_INV_TYPE;
58   }
59 
60   if ((num < SHRT_MIN) || (num > SHRT_MAX))
61   {
62     mutt_buffer_printf(err, _("Number is too big: %s"), value);
63     return CSR_ERR_INVALID | CSR_INV_TYPE;
64   }
65 
66   if ((num < 0) && (cdef->type & DT_NOT_NEGATIVE))
67   {
68     mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
69     return CSR_ERR_INVALID | CSR_INV_VALIDATOR;
70   }
71 
72   if (var)
73   {
74     if (num == (*(short *) var))
75       return CSR_SUCCESS | CSR_SUC_NO_CHANGE;
76 
77     if (cdef->validator)
78     {
79       int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
80 
81       if (CSR_RESULT(rc) != CSR_SUCCESS)
82         return rc | CSR_INV_VALIDATOR;
83     }
84 
85     *(short *) var = num;
86   }
87   else
88   {
89     cdef->initial = num;
90   }
91 
92   return CSR_SUCCESS;
93 }
94 
95 /**
96  * number_string_get - Get a Number as a string - Implements ConfigSetType::string_get() - @ingroup cfg_type_string_get
97  */
number_string_get(const struct ConfigSet * cs,void * var,const struct ConfigDef * cdef,struct Buffer * result)98 static int number_string_get(const struct ConfigSet *cs, void *var,
99                              const struct ConfigDef *cdef, struct Buffer *result)
100 {
101   int value;
102 
103   if (var)
104     value = *(short *) var;
105   else
106     value = (int) cdef->initial;
107 
108   mutt_buffer_printf(result, "%d", value);
109   return CSR_SUCCESS;
110 }
111 
112 /**
113  * number_native_set - Set a Number config item by int - Implements ConfigSetType::native_set() - @ingroup cfg_type_native_set
114  */
number_native_set(const struct ConfigSet * cs,void * var,const struct ConfigDef * cdef,intptr_t value,struct Buffer * err)115 static int number_native_set(const struct ConfigSet *cs, void *var,
116                              const struct ConfigDef *cdef, intptr_t value,
117                              struct Buffer *err)
118 {
119   if ((value < SHRT_MIN) || (value > SHRT_MAX))
120   {
121     mutt_buffer_printf(err, _("Invalid number: %ld"), value);
122     return CSR_ERR_INVALID | CSR_INV_TYPE;
123   }
124 
125   if ((value < 0) && (cdef->type & DT_NOT_NEGATIVE))
126   {
127     mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
128     return CSR_ERR_INVALID | CSR_INV_VALIDATOR;
129   }
130 
131   if (value == (*(short *) var))
132     return CSR_SUCCESS | CSR_SUC_NO_CHANGE;
133 
134   if (cdef->validator)
135   {
136     int rc = cdef->validator(cs, cdef, value, err);
137 
138     if (CSR_RESULT(rc) != CSR_SUCCESS)
139       return rc | CSR_INV_VALIDATOR;
140   }
141 
142   *(short *) var = value;
143   return CSR_SUCCESS;
144 }
145 
146 /**
147  * number_native_get - Get an int from a Number config item - Implements ConfigSetType::native_get() - @ingroup cfg_type_native_get
148  */
number_native_get(const struct ConfigSet * cs,void * var,const struct ConfigDef * cdef,struct Buffer * err)149 static intptr_t number_native_get(const struct ConfigSet *cs, void *var,
150                                   const struct ConfigDef *cdef, struct Buffer *err)
151 {
152   return *(short *) var;
153 }
154 
155 /**
156  * number_string_plus_equals - Add to a Number by string - Implements ConfigSetType::string_plus_equals() - @ingroup cfg_type_string_plus_equals
157  */
number_string_plus_equals(const struct ConfigSet * cs,void * var,const struct ConfigDef * cdef,const char * value,struct Buffer * err)158 static int number_string_plus_equals(const struct ConfigSet *cs, void *var,
159                                      const struct ConfigDef *cdef,
160                                      const char *value, struct Buffer *err)
161 {
162   int num = 0;
163   if (!value || (value[0] == '\0') || (mutt_str_atoi(value, &num) < 0))
164   {
165     mutt_buffer_printf(err, _("Invalid number: %s"), NONULL(value));
166     return CSR_ERR_INVALID | CSR_INV_TYPE;
167   }
168 
169   int result = *((short *) var) + num;
170   if ((result < SHRT_MIN) || (result > SHRT_MAX))
171   {
172     mutt_buffer_printf(err, _("Number is too big: %s"), value);
173     return CSR_ERR_INVALID | CSR_INV_TYPE;
174   }
175 
176   if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
177   {
178     mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
179     return CSR_ERR_INVALID | CSR_INV_VALIDATOR;
180   }
181 
182   if (cdef->validator)
183   {
184     int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
185 
186     if (CSR_RESULT(rc) != CSR_SUCCESS)
187       return rc | CSR_INV_VALIDATOR;
188   }
189 
190   *(short *) var = result;
191   return CSR_SUCCESS;
192 }
193 
194 /**
195  * number_string_minus_equals - Subtract from a Number by string - Implements ConfigSetType::string_minus_equals() - @ingroup cfg_type_string_minus_equals
196  */
number_string_minus_equals(const struct ConfigSet * cs,void * var,const struct ConfigDef * cdef,const char * value,struct Buffer * err)197 static int number_string_minus_equals(const struct ConfigSet *cs, void *var,
198                                       const struct ConfigDef *cdef,
199                                       const char *value, struct Buffer *err)
200 {
201   int num = 0;
202   if (!value || (value[0] == '\0') || (mutt_str_atoi(value, &num) < 0))
203   {
204     mutt_buffer_printf(err, _("Invalid number: %s"), value);
205     return CSR_ERR_INVALID | CSR_INV_TYPE;
206   }
207 
208   int result = *((short *) var) - num;
209   if ((result < SHRT_MIN) || (result > SHRT_MAX))
210   {
211     mutt_buffer_printf(err, _("Number is too big: %s"), value);
212     return CSR_ERR_INVALID | CSR_INV_TYPE;
213   }
214 
215   if ((result < 0) && (cdef->type & DT_NOT_NEGATIVE))
216   {
217     mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
218     return CSR_ERR_INVALID | CSR_INV_VALIDATOR;
219   }
220 
221   if (cdef->validator)
222   {
223     int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
224 
225     if (CSR_RESULT(rc) != CSR_SUCCESS)
226       return rc | CSR_INV_VALIDATOR;
227   }
228 
229   *(short *) var = result;
230   return CSR_SUCCESS;
231 }
232 
233 /**
234  * number_reset - Reset a Number to its initial value - Implements ConfigSetType::reset() - @ingroup cfg_type_reset
235  */
number_reset(const struct ConfigSet * cs,void * var,const struct ConfigDef * cdef,struct Buffer * err)236 static int number_reset(const struct ConfigSet *cs, void *var,
237                         const struct ConfigDef *cdef, struct Buffer *err)
238 {
239   if (cdef->initial == (*(short *) var))
240     return CSR_SUCCESS | CSR_SUC_NO_CHANGE;
241 
242   if (cdef->validator)
243   {
244     int rc = cdef->validator(cs, cdef, cdef->initial, err);
245 
246     if (CSR_RESULT(rc) != CSR_SUCCESS)
247       return rc | CSR_INV_VALIDATOR;
248   }
249 
250   *(short *) var = cdef->initial;
251   return CSR_SUCCESS;
252 }
253 
254 /**
255  * CstNumber - Config type representing a number
256  */
257 const struct ConfigSetType CstNumber = {
258   DT_NUMBER,
259   "number",
260   number_string_set,
261   number_string_get,
262   number_native_set,
263   number_native_get,
264   number_string_plus_equals,
265   number_string_minus_equals,
266   number_reset,
267   NULL, // destroy
268 };
269