From 7922e8ea342194f27757d278efef1ba349cf30bb Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lab Date: Tue, 25 Nov 2025 16:52:23 +0100 Subject: [PATCH] Allow exiting a watch loop when a command returns the specified code --- src/builtins.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/builtins.c b/src/builtins.c index ddd7fa1..b7fe2e7 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -115,11 +115,13 @@ static int slash_builtin_watch(struct slash *slash) unsigned int interval = 1000; unsigned int count = 0; + int exit = 0xFFFFFF; optparse_t * parser = optparse_new("watch", ""); optparse_add_help(parser); optparse_add_unsigned(parser, 'n', "interval", "NUM", 0, &interval, "interval in milliseconds (default = )"); optparse_add_unsigned(parser, 'c', "count", "NUM", 0, &count, "number of times to repeat (default = infinite)"); + optparse_add_unsigned(parser, 'e', "exit", "NUM", 0, &exit, "stop watch when command returns the given exit code (default = ignore exit code completely)"); int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1); if (argi < 0) { @@ -137,7 +139,7 @@ static int slash_builtin_watch(struct slash *slash) } printf("Executing \"%s\" each %u ms - press to stop\n", line, interval); - + int cmd_exit_code; while(1) { /* Make another copy, since slash_exec will modify this */ @@ -149,7 +151,12 @@ static int slash_builtin_watch(struct slash *slash) clock_gettime(CLOCK_MONOTONIC, &time_before); /* Execute command */ - slash_execute(slash, cmd_exec); + cmd_exit_code = slash_execute(slash, cmd_exec); + if(exit != 0xFFFFFF) { + if(exit == cmd_exit_code) { + break; + } + } clock_gettime(CLOCK_MONOTONIC, &time_after);