]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
cmd: fix stdin command ordering and related stuff
authorbones_was_here <bones_was_here@xonotic.au>
Sun, 31 Dec 2023 20:55:03 +0000 (06:55 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Wed, 3 Jan 2024 00:36:23 +0000 (10:36 +1000)
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 <bones_was_here@xonotic.au>
cmd.c
sys_shared.c

diff --git a/cmd.c b/cmd.c
index df218d9e0fecae0f5bf031b6c7e3d546c083bc11..1d92cadcc7e59595db5a66cd950b6756e1459d01 100644 (file)
--- 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)
index 286ff7e5a80f1e13df408a3d3e470ea3b9ded7e1..457abe36a9a26e1776b2158698b3a014662daad3 100644 (file)
@@ -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__