MEDIUM: mcli: make the prompt mode configurable between i/p

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.
This commit is contained in:
Willy Tarreau 2025-04-28 18:51:47 +02:00
parent e5c255c4e5
commit c347cb73fa
2 changed files with 44 additions and 4 deletions

View 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.

View File

@ -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;
s->pcli_flags ^= PCLI_F_TIMED;
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);