diff --git a/src/core/wee-command.c b/src/core/wee-command.c index b37c37539..325e24753 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -6545,8 +6545,7 @@ COMMAND_CALLBACK(version) COMMAND_CALLBACK(wait) { - char *pos, *str_number, *error; - long number, factor, delay; + long delay; /* make C compiler happy */ (void) pointer; @@ -6554,46 +6553,7 @@ COMMAND_CALLBACK(wait) COMMAND_MIN_ARGS(3, ""); - pos = argv[1]; - while (pos[0] && isdigit ((unsigned char)pos[0])) - { - pos++; - } - - /* default is seconds (1000 milliseconds) */ - factor = 1000; - - if ((pos != argv[1]) && pos[0]) - { - str_number = string_strndup (argv[1], pos - argv[1]); - if (strcmp (pos, "ms") == 0) - factor = 1; - else if (strcmp (pos, "s") == 0) - factor = 1000; - else if (strcmp (pos, "m") == 0) - factor = 1000 * 60; - else if (strcmp (pos, "h") == 0) - factor = 1000 * 60 * 60; - else - COMMAND_ERROR; - } - else - str_number = strdup (argv[1]); - - if (!str_number) - COMMAND_ERROR; - - error = NULL; - number = strtol (str_number, &error, 10); - if (!error || error[0]) - { - free (str_number); - COMMAND_ERROR; - } - free (str_number); - - delay = number * factor; - + delay = util_parse_delay (argv[1], 1000); if (delay < 1) COMMAND_ERROR; diff --git a/src/core/wee-util.c b/src/core/wee-util.c index 48c470ea3..cd4892a7c 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -338,6 +338,78 @@ util_get_time_string (const time_t *date) return text_time; } +/* + * Parses a string with a delay and optional unit, returns the delay in + * milliseconds. + * + * The delay is a number followed by a unit which can be: + * - "ms": milliseconds + * - "s": seconds + * - "m": minutes + * - "h": hours + * + * The default factor sets the default unit: + * - 1: milliseconds + * - 1000: seconds + * - 60000: minutes + * - 3600000: hours + * + * Returns the delay in milliseconds, -1 if error. + */ + +long +util_parse_delay (const char *string_delay, long default_factor) +{ + const char *pos; + char *str_number, *error; + long factor, delay; + + if (!string_delay || !string_delay[0] || (default_factor < 1)) + return -1; + + factor = default_factor; + + pos = string_delay; + while (pos[0] && isdigit ((unsigned char)pos[0])) + { + pos++; + } + + if ((pos > string_delay) && pos[0]) + { + str_number = string_strndup (string_delay, pos - string_delay); + if (strcmp (pos, "ms") == 0) + factor = 1; + else if (strcmp (pos, "s") == 0) + factor = 1000; + else if (strcmp (pos, "m") == 0) + factor = 1000 * 60; + else if (strcmp (pos, "h") == 0) + factor = 1000 * 60 * 60; + else + return -1; + } + else + { + str_number = strdup (string_delay); + } + + if (!str_number) + return -1; + + error = NULL; + delay = strtol (str_number, &error, 10); + if (!error || error[0] || (delay < 0)) + { + free (str_number); + return -1; + } + + free (str_number); + + return delay * factor; +} + /* * Gets a signal number with a name; only some commonly used signal names are * supported here (see declaration of util_signals[]). @@ -909,7 +981,7 @@ util_version_number (const char *version) } /* - * Return uptime as number of days, hours, minutes, seconds. + * Returns uptime as number of days, hours, minutes, seconds. */ void diff --git a/src/core/wee-util.h b/src/core/wee-util.h index ec0920bbf..223017f8a 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -47,6 +47,10 @@ extern void util_timeval_add (struct timeval *tv, long long interval); /* time */ extern const char *util_get_time_string (const time_t *date); +/* delay */ + +extern long util_parse_delay (const char *string_delay, long default_factor); + /* signal */ extern int util_signal_search (const char *name); extern void util_catch_signal (int signum, void (*handler)(int)); diff --git a/tests/unit/core/test-core-util.cpp b/tests/unit/core/test-core-util.cpp index 86627a3fe..0d29a832d 100644 --- a/tests/unit/core/test-core-util.cpp +++ b/tests/unit/core/test-core-util.cpp @@ -93,6 +93,55 @@ TEST(CoreUtil, GetTimeString) STRCMP_EQUAL("Sat, 01 Jan 2000 00:00:00", str_date); } +/* + * Tests functions: + * util_parse_delay + */ + +TEST(CoreUtil, ParseDelay) +{ + /* error: no string */ + LONGS_EQUAL(-1, util_parse_delay (NULL, -1)); + LONGS_EQUAL(-1, util_parse_delay (NULL, 0)); + LONGS_EQUAL(-1, util_parse_delay (NULL, 1)); + LONGS_EQUAL(-1, util_parse_delay ("", -1)); + LONGS_EQUAL(-1, util_parse_delay ("", 0)); + LONGS_EQUAL(-1, util_parse_delay ("", 1)); + + /* error: bad default_factor */ + LONGS_EQUAL(-1, util_parse_delay ("abcd", -1)); + LONGS_EQUAL(-1, util_parse_delay ("abcd", 0)); + LONGS_EQUAL(-1, util_parse_delay ("123", -1)); + LONGS_EQUAL(-1, util_parse_delay ("123", 0)); + + /* error: bad unit */ + LONGS_EQUAL(-1, util_parse_delay ("123a", 1)); + LONGS_EQUAL(-1, util_parse_delay ("123ss", 1)); + LONGS_EQUAL(-1, util_parse_delay ("123mss", 1)); + + /* error: bad number */ + LONGS_EQUAL(-1, util_parse_delay ("abcd", 1)); + + /* tests with delay == 0 */ + LONGS_EQUAL(0, util_parse_delay ("0", 1)); + LONGS_EQUAL(0, util_parse_delay ("0s", 1)); + LONGS_EQUAL(0, util_parse_delay ("0m", 1)); + LONGS_EQUAL(0, util_parse_delay ("0h", 1)); + + /* tests with delay == 123, default_factor = 1 */ + LONGS_EQUAL(123, util_parse_delay ("123", 1)); + LONGS_EQUAL(123, util_parse_delay ("123", 1)); + LONGS_EQUAL(123 * 1000, util_parse_delay ("123s", 1)); + LONGS_EQUAL(123 * 1000 * 60, util_parse_delay ("123m", 1)); + LONGS_EQUAL(123 * 1000 * 60 * 60, util_parse_delay ("123h", 1)); + + /* tests with delay == 123, default_factor = 1000 */ + LONGS_EQUAL(123 * 1000, util_parse_delay ("123", 1000)); + LONGS_EQUAL(123 * 1000, util_parse_delay ("123s", 1000)); + LONGS_EQUAL(123 * 1000 * 60, util_parse_delay ("123m", 1000)); + LONGS_EQUAL(123 * 1000 * 60 * 60, util_parse_delay ("123h", 1000)); +} + /* * Tests functions: * util_signal_search