src/configfile.c: cf_util_get_port_number: Gracefully handle number arguments as...
authorFlorian Forster <octo@huhu.verplant.org>
Tue, 25 Jan 2011 06:42:53 +0000 (07:42 +0100)
committerFlorian Forster <octo@huhu.verplant.org>
Tue, 25 Jan 2011 06:42:56 +0000 (07:42 +0100)
Why force the user into using strings when it's not strictly
necessary..?

src/configfile.c
src/configfile.h

index 33a7c20..54d418b 100644 (file)
@@ -1058,19 +1058,40 @@ int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
        return (0);
 } /* }}} int cf_util_get_flag */
 
-/* Assures that the config option is a string. The string is then converted to
- * a port number using `service_name_to_port_number' and returned. Returns the
- * port number in the range [1-65535] or less than zero upon failure. */
+/* Assures that the config option is a string or a number if the correct range
+ * of 1-65535. The string is then converted to a port number using
+ * `service_name_to_port_number' and returned.
+ * Returns the port number in the range [1-65535] or less than zero upon
+ * failure. */
 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
 {
-       if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+       int tmp;
+
+       if ((ci->values_num != 1)
+                       || ((ci->values[0].type != OCONFIG_TYPE_STRING)
+                               && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
        {
-               ERROR ("cf_util_get_port_number: The %s option requires "
+               ERROR ("cf_util_get_port_number: The \"%s\" option requires "
                                "exactly one string argument.", ci->key);
                return (-1);
        }
 
-       return (service_name_to_port_number (ci->values[0].value.string));
+       if (ci->values[0].type == OCONFIG_TYPE_STRING)
+               return (service_name_to_port_number (ci->values[0].value.string));
+
+       assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
+       tmp = (int) (ci->values[0].value.number + 0.5);
+       if ((tmp < 1) || (tmp > 65535))
+       {
+               ERROR ("cf_util_get_port_number: The \"%s\" option requires "
+                               "a service name or a port number. The number "
+                               "you specified, %i, is not in the valid "
+                               "range of 1-65535.",
+                               ci->key, tmp);
+               return (-1);
+       }
+
+       return (tmp);
 } /* }}} int cf_util_get_port_number */
 
 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
index 65b1efc..c381028 100644 (file)
@@ -109,9 +109,11 @@ int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool);
 int cf_util_get_flag (const oconfig_item_t *ci,
                unsigned int *ret_value, unsigned int flag);
 
-/* Assures that the config option is a string. The string is then converted to
- * a port number using `service_name_to_port_number' and returned. Returns the
- * port number in the range [1-65535] or less than zero upon failure. */
+/* Assures that the config option is a string or a number if the correct range
+ * of 1-65535. The string is then converted to a port number using
+ * `service_name_to_port_number' and returned.
+ * Returns the port number in the range [1-65535] or less than zero upon
+ * failure. */
 int cf_util_get_port_number (const oconfig_item_t *ci);
 
 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value);