From: bones_was_here Date: Sun, 31 Dec 2023 20:55:03 +0000 (+1000) Subject: cmd: fix stdin command ordering and related stuff X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=952aed9fee04278bc7f9857e3b8e8007d64894b8;p=xonotic%2Fdarkplaces.git cmd: fix stdin command ordering and related stuff Fixes bug introduced in d217d6b9ba9012bc374ac0d44d48422b7b13b755 where multiline stdin execution order was reversed. Retrieves FD of stdin stream rather than assuming it will always be 0. Signed-off-by: bones_was_here --- diff --git a/cmd.c b/cmd.c index df218d9e..1d92cadc 100644 --- a/cmd.c +++ b/cmd.c @@ -425,10 +425,20 @@ static void Cbuf_Frame_Input(void) { char *line; - // bones_was_here: prepending allows a loop such as `alias foo "bar; wait; foo"; foo` - // to be broken with an alias or unalias command - while ((line = Sys_ConsoleInput())) + if ((line = Sys_ConsoleInput())) + { + // bones_was_here: prepending allows a loop such as `alias foo "bar; wait; foo"; foo` + // to be broken with an alias or unalias command Cbuf_InsertText(cmd_local, line); + /* appending subsequent lines allows this test to pass (if pasted with proper \n): + * wait; echo a + * wait + * echo b + * echo c + */ + while ((line = Sys_ConsoleInput())) + Cbuf_AddText(cmd_local, line); + } } void Cbuf_Frame(cmd_buf_t *cbuf) diff --git a/sys_shared.c b/sys_shared.c index 286ff7e5..457abe36 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -618,8 +618,8 @@ void Sys_Printf(const char *fmt, ...) char *Sys_ConsoleInput(void) { static char text[MAX_INPUTLINE]; - static unsigned int len = 0; #ifdef WIN32 + static unsigned int len = 0; int c; // read a line out @@ -653,24 +653,12 @@ char *Sys_ConsoleInput(void) } #else fd_set fdset; - struct timeval timeout; + struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 }; + FD_ZERO(&fdset); - FD_SET(0, &fdset); // stdin - timeout.tv_sec = 0; - timeout.tv_usec = 0; - if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset)) - { - len = read (0, text, sizeof(text) - 1); - if (len >= 1) - { - // rip off the \n and terminate - // div0: WHY? console code can deal with \n just fine - // this caused problems with pasting stuff into a terminal window - // so, not ripping off the \n, but STILL keeping a NUL terminator - text[len] = 0; - return text; - } - } + FD_SET(fileno(stdin), &fdset); + if (select(1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(fileno(stdin), &fdset)) + return fgets(text, sizeof(text), stdin); #endif return NULL; } @@ -922,7 +910,7 @@ int main (int argc, char **argv) Sys_ProvideSelfFD(); // may call Con_Printf() so must be after sys.outfd is set #ifndef WIN32 - fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK); + fcntl(fileno(stdin), F_SETFL, fcntl (fileno(stdin), F_GETFL, 0) | O_NONBLOCK); #endif #ifdef __ANDROID__