From 015ff084e31a4202513d202c7a2362a4d6c59f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 25 Aug 2023 11:23:41 +0200 Subject: core: add microseconds precision in function util_parse_delay --- src/core/wee-command.c | 19 ++++++++++++------- src/core/wee-util.c | 42 +++++++++++++++++++++++------------------- src/core/wee-util.h | 3 ++- 3 files changed, 37 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 54209d505..09e1b0001 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -5743,7 +5743,7 @@ command_repeat_timer_cb (const void *pointer, void *data, int remaining_calls) COMMAND_CALLBACK(repeat) { int arg_count, count, i; - long interval; + long long interval; char *error, **repeat_args; /* make C compiler happy */ @@ -5757,9 +5757,10 @@ COMMAND_CALLBACK(repeat) if ((argc >= 5) && (string_strcmp (argv[1], "-interval") == 0)) { - interval = util_parse_delay (argv[2], 1000); - if (interval < 1) + interval = util_parse_delay (argv[2], 1000000); + if (interval < 0) interval = 0; + interval /= 1000; arg_count = 3; } @@ -7256,7 +7257,7 @@ COMMAND_CALLBACK(version) COMMAND_CALLBACK(wait) { - long delay; + long long delay; /* make C compiler happy */ (void) pointer; @@ -7264,10 +7265,12 @@ COMMAND_CALLBACK(wait) COMMAND_MIN_ARGS(3, ""); - delay = util_parse_delay (argv[1], 1000); + delay = util_parse_delay (argv[1], 1000000); if (delay < 1) COMMAND_ERROR; + delay /= 1000; + if (input_data_delayed (buffer, argv_eol[2], NULL, 0, delay) != WEECHAT_RC_OK) COMMAND_ERROR; @@ -8886,8 +8889,9 @@ command_init () NULL, "repeat", N_("execute a command several times"), N_("[-interval []] "), - N_(" delay: delay between execution of commands\n" + N_(" delay: delay between execution of commands (minimum: 1 millisecond)\n" " unit: optional, values are:\n" + " us: microseconds\n" " ms: milliseconds\n" " s: seconds (default)\n" " m: minutes\n" @@ -9172,8 +9176,9 @@ command_init () NULL, "wait", N_("schedule a command execution in future"), N_("[] "), - N_(" number: amount of time to wait (integer number)\n" + N_(" number: amount of time to wait (minimum: 1 millisecond)\n" " unit: optional, values are:\n" + " us: microseconds\n" " ms: milliseconds\n" " s: seconds (default)\n" " m: minutes\n" diff --git a/src/core/wee-util.c b/src/core/wee-util.c index 8d791acfa..0e70aee4a 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -339,32 +339,34 @@ util_get_time_diff (time_t time1, time_t time2, /* * Parses a string with a delay and optional unit, returns the delay in - * milliseconds. + * microseconds. * * The delay is a number followed by a unit which can be: + * - "us": microseconds * - "ms": milliseconds * - "s": seconds * - "m": minutes * - "h": hours * * The default factor sets the default unit: - * - 1: milliseconds - * - 1000: seconds - * - 60000: minutes - * - 3600000: hours + * - 1: microseconds + * - 1000: milliseconds + * - 1000000: seconds + * - 60000000: minutes + * - 3600000000: hours * - * Returns the delay in milliseconds, -1 if error. + * Returns the delay in microseconds, -1 if error. */ -long -util_parse_delay (const char *string_delay, long default_factor) +long long +util_parse_delay (const char *string_delay, long long default_factor) { const char *pos; char *str_number, *error; - long factor, delay; + long long factor, delay; if (!string_delay || !string_delay[0] || (default_factor < 1)) - return -1; + return -1LL; factor = default_factor; @@ -377,16 +379,18 @@ util_parse_delay (const char *string_delay, long default_factor) if ((pos > string_delay) && pos[0]) { str_number = string_strndup (string_delay, pos - string_delay); - if (strcmp (pos, "ms") == 0) - factor = 1; + if (strcmp (pos, "us") == 0) + factor = 1LL; + else if (strcmp (pos, "ms") == 0) + factor = 1000LL; else if (strcmp (pos, "s") == 0) - factor = 1000; + factor = 1000LL * 1000LL; else if (strcmp (pos, "m") == 0) - factor = 1000 * 60; + factor = 1000LL * 1000LL * 60LL; else if (strcmp (pos, "h") == 0) - factor = 1000 * 60 * 60; + factor = 1000LL * 1000LL * 60LL * 60LL; else - return -1; + return -1LL; } else { @@ -394,14 +398,14 @@ util_parse_delay (const char *string_delay, long default_factor) } if (!str_number) - return -1; + return -1LL; error = NULL; - delay = strtol (str_number, &error, 10); + delay = strtoll (str_number, &error, 10); if (!error || error[0] || (delay < 0)) { free (str_number); - return -1; + return -1LL; } free (str_number); diff --git a/src/core/wee-util.h b/src/core/wee-util.h index 8811047c2..1acb9a76d 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -48,7 +48,8 @@ extern void util_get_time_diff (time_t time1, time_t time2, /* delay */ -extern long util_parse_delay (const char *string_delay, long default_factor); +extern long long util_parse_delay (const char *string_delay, + long long default_factor); /* version */ extern int util_version_number (const char *version); -- cgit v1.2.3