diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2023-08-25 11:23:41 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2023-08-25 11:23:41 +0200 |
commit | 015ff084e31a4202513d202c7a2362a4d6c59f1b (patch) | |
tree | 320427b743f6ceb525f71ce67cf3b4c17f3a246f /src | |
parent | 11943ebaaf98d70be17bcdf2579ee7461fa789a6 (diff) | |
download | weechat-015ff084e31a4202513d202c7a2362a4d6c59f1b.zip |
core: add microseconds precision in function util_parse_delay
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-command.c | 19 | ||||
-rw-r--r-- | src/core/wee-util.c | 42 | ||||
-rw-r--r-- | src/core/wee-util.h | 3 |
3 files changed, 37 insertions, 27 deletions
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 <delay>[<unit>]] <count> <command>"), - 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_("<number>[<unit>] <command>"), - 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); |