]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: mcli: make the prompt mode configurable between i/p
authorWilly Tarreau <w@1wt.eu>
Mon, 28 Apr 2025 16:51:47 +0000 (18:51 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 28 Apr 2025 18:21:06 +0000 (20:21 +0200)
Support the same syntax in master mode as in worker mode in order to
configure the prompt. The only thing is that for now the master doesn't
have a non-interactive mode and it doesn't seem necessary to implement
it, so we only support the interactive and prompt modes. However the code
was written in a way that makes it easy to change this later if desired.

doc/management.txt
src/cli.c

index 3ceea6b5f734d3d118b9a707a546d713f9d9061c..52d3c61c4c715b718c1ebd8eee2d991223cb44cd 100644 (file)
@@ -2406,6 +2406,7 @@ prompt [help | n | i | p | timed]*
 
   The prompt mode is more suited to human users, the interactive mode to
   advanced scripts, and the non-interactive mode (default) to basic scripts.
+  Note that the non-interactive mode is not available for the master socket.
 
 quit
   Close the connection when in interactive mode.
index 964ad8cf27c05a7fabcd37990d8b1e844938fb1b..954087140b73d4cae2e23b56ef2befb9d64624a6 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -2865,12 +2865,51 @@ int pcli_find_and_exec_kw(struct stream *s, char **args, int argl, char **errmsg
                        *next_pid = target_pid;
                return 1;
        } else if (strcmp("prompt", args[0]) == 0) {
-               if (argl >= 2 && strcmp(args[1], "timed") == 0) {
-                       s->pcli_flags |= PCLI_F_PROMPT;
+               int mode = 0;  // 0=default, 1="n" (not in master), 2="i", 3="p"
+               int timed = 0; // non-zero = "timed" present
+               int arg;
+
+               const char *usage =
+                       "Usage: prompt [help | i | p | timed]*\n"
+                       "Changes the default prompt and interactive mode. Available options:\n"
+                       "  help    display this help\n"
+                       "  i       set to interactive mode only (no prompt, multi-command)\n"
+                       "  p       set to prompt+interactive mode (prompt + multi-command)\n"
+                       "  timed   toggle displaying the current date in the prompt\n"
+                       "Without any argument, toggles between i<->p.\n"
+                       ;
+
+               for (arg = 1; arg < argl; arg++) {
+                       if (strcmp(args[arg], "i") == 0)
+                               mode = 2;
+                       else if (strcmp(args[arg], "p") == 0)
+                               mode = 3;
+                       else if (strcmp(args[arg], "timed") == 0)
+                               timed = 1;
+                       else {
+                               memprintf(errmsg, "%s", usage);
+                               return -1;
+                       }
+               }
+
+               /* In timed mode, the default is to enable prompt+inter and toggle timed.
+                * In other mode, the default is to toggle prompt+inter (n/i->p, p->n).
+                */
+               if (timed) {
                        s->pcli_flags ^= PCLI_F_TIMED;
+                       mode = mode ? mode : 3; // p by default
                }
+
+               if (mode) {
+                       /* force mode (i,p) */
+                       s->pcli_flags &= ~PCLI_F_PROMPT;
+                       s->pcli_flags |= (mode >= 3) ? PCLI_F_PROMPT : 0;
+               }
+               else if (~s->pcli_flags & PCLI_F_PROMPT)
+                       s->pcli_flags |= PCLI_F_PROMPT; // p
                else
-                       s->pcli_flags ^= PCLI_F_PROMPT;
+                       s->pcli_flags &= ~PCLI_F_PROMPT; // i
+
                return argl; /* return the number of elements in the array */
        } else if (strcmp("quit", args[0]) == 0) {
                sc_schedule_abort(s->scf);