This patch adds functions to get int, uint, double value from string
with error checking and range specification.
These functions don't allow overflow and outside of the range values.
In addition, it adds parser_result cases "MALFORMED" and "OUT_OF_RANGE" to make
reason of parse error more clear.
Signed-off-by: Ken ICHIKAWA <***@jp.fujitsu.com>
---
config.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
config.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 104 insertions(+), 6 deletions(-)
diff --git a/config.c b/config.c
index 734bd36..cc0e1bc 100644
--- a/config.c
+++ b/config.c
@@ -17,8 +17,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include <errno.h>
#include "config.h"
#include "ether.h"
#include "print.h"
@@ -30,12 +32,6 @@ enum config_section {
UNKNOWN_SECTION,
};
-enum parser_result {
- PARSED_OK,
- NOT_PARSED,
- BAD_VALUE,
-};
-
static enum parser_result parse_section_line(char *s, enum config_section *section)
{
if (!strcasecmp(s, "[global]")) {
@@ -512,6 +508,14 @@ int config_read(char *name, struct config *cfg)
fprintf(stderr, "%s is a bad value for option %s at line %d\n",
value, option, line_num);
goto parse_error;
+ case MALFORMED:
+ fprintf(stderr, "%s is a malformed value for option %s at line %d\n",
+ value, option, line_num);
+ goto parse_error;
+ case OUT_OF_RANGE:
+ fprintf(stderr, "%s is an out of range value for option %s at line %d\n",
+ value, option, line_num);
+ goto parse_error;
}
break;
@@ -562,3 +566,45 @@ int config_create_interface(char *name, struct config *cfg)
return i;
}
+
+enum parser_result get_ranged_int(const char *str_val, int *result, int min, int max)
+{
+ long parsed_val;
+ char *endptr = NULL;
+ errno = 0;
+ parsed_val = strtol(str_val, &endptr, 0);
+ if (*endptr != '\0' || endptr == str_val)
+ return MALFORMED;
+ if (errno == ERANGE || parsed_val < min || parsed_val > max)
+ return OUT_OF_RANGE;
+ *result = parsed_val;
+ return PARSED_OK;
+}
+
+enum parser_result get_ranged_uint(const char *str_val, unsigned int *result, unsigned int min, unsigned int max)
+{
+ unsigned long parsed_val;
+ char *endptr = NULL;
+ errno = 0;
+ parsed_val = strtoul(str_val, &endptr, 0);
+ if (*endptr != '\0' || endptr == str_val)
+ return MALFORMED;
+ if (errno == ERANGE || parsed_val < min || parsed_val > max)
+ return OUT_OF_RANGE;
+ *result = parsed_val;
+ return PARSED_OK;
+}
+
+enum parser_result get_ranged_double(const char *str_val, double *result, double min, double max)
+{
+ double parsed_val;
+ char *endptr = NULL;
+ errno = 0;
+ parsed_val = strtod(str_val, &endptr);
+ if (*endptr != '\0' || endptr == str_val)
+ return MALFORMED;
+ if (errno == ERANGE || parsed_val < min || parsed_val > max)
+ return OUT_OF_RANGE;
+ *result = parsed_val;
+ return PARSED_OK;
+}
diff --git a/config.h b/config.h
index 901b50a..02eaeb1 100644
--- a/config.h
+++ b/config.h
@@ -46,6 +46,14 @@ struct interface {
#define CFG_IGNORE_USE_SYSLOG (1 << 5)
#define CFG_IGNORE_VERBOSE (1 << 6)
+enum parser_result {
+ PARSED_OK,
+ NOT_PARSED,
+ BAD_VALUE,
+ MALFORMED,
+ OUT_OF_RANGE,
+};
+
struct config {
/* configuration override */
int cfg_ignore;
@@ -82,4 +90,48 @@ struct config {
int config_read(char *name, struct config *cfg);
int config_create_interface(char *name, struct config *cfg);
+/**
+ * Get an integer value from string with error checking and range
+ * specification.
+ *
+ * @param str_val String which contains an integer value.
+ * @param result Parsed value is stored in here.
+ * @param min Lower limit. Return OUT_OF_RANGE if parsed value
+ * is less than min.
+ * @param max Upper Limit. Return OUT_OF_RANGE if parsed value
+ * is bigger than max.
+ * @return PARSED_OK on success, MALFORMED if str_val is malformed,
+ * OUT_OF_RANGE if str_val is out of range.
+ */
+enum parser_result get_ranged_int(const char *str_val, int *result, int min, int max);
+
+/**
+ * Get an unsigned integer value from string with error checking and range
+ * specification.
+ *
+ * @param str_val String which contains an unsigned integer value.
+ * @param result Parsed value is stored in here.
+ * @param min Lower limit. Return OUT_OF_RANGE if parsed value
+ * is less than min.
+ * @param max Upper Limit. Return OUT_OF_RANGE if parsed value
+ * is bigger than max.
+ * @return PARSED_OK on success, MALFORMED if str_val is malformed,
+ * OUT_OF_RANGE if str_val is out of range.
+ */
+enum parser_result get_ranged_uint(const char *str_val, unsigned int *result, unsigned int min, unsigned int max);
+
+/**
+ * Get a double value from string with error checking and range
+ * specification.
+ *
+ * @param str_val String which contains a double value.
+ * @param result Parsed value is stored in here.
+ * @param min Lower limit. Return OUT_OF_RANGE if parsed value
+ * is less than min.
+ * @param max Upper Limit. Return OUT_OF_RANGE if parsed value
+ * is bigger than max.
+ * @return PARSED_OK on success, MALFORMED if str_val is malformed,
+ * OUT_OF_RANGE if str_val is out of range.
+ */
+enum parser_result get_ranged_double(const char *str_val, double *result, double min, double max);
#endif
--
1.7.1