]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - keys.c
cbuf: catch runaway command loops, respects -norunaway cmdline arg
[xonotic/darkplaces.git] / keys.c
diff --git a/keys.c b/keys.c
index d830e827abbecab806f3bab29991aef6c47f5f59..9ff4853311313e4104819f30b990c603370b3f18 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -694,7 +694,7 @@ Interactive line editing and console scrollback
 ====================
 */
 
-int chat_mode; // 0 for say, 1 for say_team, -1 for command
+signed char chat_mode; // 0 for say, 1 for say_team, -1 for command
 char chat_buffer[MAX_INPUTLINE];
 int chat_bufferpos = 0;
 
@@ -1351,6 +1351,7 @@ the K_* names are matched up.
 int
 Key_StringToKeynum (const char *str)
 {
+       Uchar ch;
        const keyname_t  *kn;
 
        if (!str || !str[0])
@@ -1362,7 +1363,11 @@ Key_StringToKeynum (const char *str)
                if (!strcasecmp (str, kn->name))
                        return kn->keynum;
        }
-       return -1;
+
+       // non-ascii keys are Unicode codepoints, so give the character if it's valid;
+       // error message have more than one character, don't allow it
+       ch = u8_getnchar(str, &str, 3);
+       return (ch == 0 || *str != 0) ? -1 : (int)ch;
 }
 
 /*
@@ -1387,13 +1392,9 @@ Key_KeynumToString (int keynum, char *tinystr, size_t tinystrlength)
                        return kn->name;
 
        // if it is printable, output it as a single character
-       if (keynum > 32 && keynum < 256)
+       if (keynum > 32)
        {
-               if (tinystrlength >= 2)
-               {
-                       tinystr[0] = keynum;
-                       tinystr[1] = 0;
-               }
+               u8_fromchar(keynum, tinystr, tinystrlength);
                return tinystr;
        }
 
@@ -1586,7 +1587,7 @@ static void
 Key_PrintBindList(int j)
 {
        char bindbuf[MAX_INPUTLINE];
-       char tinystr[2];
+       char tinystr[TINYSTR_LEN];
        const char *p;
        int i;
 
@@ -1597,9 +1598,9 @@ Key_PrintBindList(int j)
                {
                        Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false);
                        if (j == 0)
-                               Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
+                               Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
                        else
-                               Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
+                               Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
                }
        }
 }
@@ -1679,7 +1680,7 @@ Key_WriteBindings (qfile_t *f)
 {
        int         i, j;
        char bindbuf[MAX_INPUTLINE];
-       char tinystr[2];
+       char tinystr[TINYSTR_LEN];
        const char *p;
 
        // Override default binds
@@ -1694,9 +1695,9 @@ Key_WriteBindings (qfile_t *f)
                        {
                                Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); // don't need to escape $ because cvars are not expanded inside bind
                                if (j == 0)
-                                       FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
+                                       FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
                                else
-                                       FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
+                                       FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
                        }
                }
        }
@@ -1832,7 +1833,7 @@ void Key_EventQueue_Unblock(void)
 void
 Key_Event (int key, int ascii, qbool down)
 {
-       cmd_state_t *cmd = &cmd_client;
+       cmd_state_t *cmd = cmd_local;
        const char *bind;
        qbool q;
        keydest_t keydest = key_dest;
@@ -1910,7 +1911,7 @@ Key_Event (int key, int ascii, qbool down)
                {
                        if(down)
                        {
-                               Con_ToggleConsole_f(&cmd_client);
+                               Con_ToggleConsole_f(cmd_local);
                                tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
                        }
                        return;
@@ -1929,7 +1930,7 @@ Key_Event (int key, int ascii, qbool down)
 #endif
                                        }
                                        else
-                                               Con_ToggleConsole_f(&cmd_client);
+                                               Con_ToggleConsole_f(cmd_local);
                                }
                                break;
 
@@ -1971,14 +1972,12 @@ Key_Event (int key, int ascii, qbool down)
                        {
                                // button commands add keynum as a parm
                                if (bind[0] == '+')
-                                       Cbuf_AddText (cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
+                                       Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
                                else
-                               {
-                                       Cbuf_AddText (cmd, bind);
-                                       Cbuf_AddText (cmd, "\n");
-                               }
-                       } else if(bind[0] == '+' && !down && keydown[key] == 0)
-                               Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
+                                       Cbuf_InsertText(cmd, bind);
+                       }
+                       else if(bind[0] == '+' && !down && keydown[key] == 0)
+                               Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
                }
                return;
        }
@@ -1993,7 +1992,7 @@ Key_Event (int key, int ascii, qbool down)
                // (special exemption for german keyboard layouts)
                if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && (con_closeontoggleconsole.integer >= ((ascii != STRING_COLOR_TAG) ? 2 : 3) || key_linepos == 1))
                {
-                       Con_ToggleConsole_f(&cmd_client);
+                       Con_ToggleConsole_f(cmd_local);
                        return;
                }
 
@@ -2009,7 +2008,7 @@ Key_Event (int key, int ascii, qbool down)
        {
                if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
                {
-                       Cbuf_AddText(cmd, "toggleconsole\n");  // Deferred to next frame so we're not sending the text event to the console.
+                       Cbuf_InsertText(cmd, "toggleconsole\n");  // Deferred to next frame so we're not sending the text event to the console.
                        tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
                        return;
                }
@@ -2052,14 +2051,12 @@ Key_Event (int key, int ascii, qbool down)
                                {
                                        // button commands add keynum as a parm
                                        if (bind[0] == '+')
-                                               Cbuf_AddText (cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
+                                               Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
                                        else
-                                       {
-                                               Cbuf_AddText (cmd, bind);
-                                               Cbuf_AddText (cmd, "\n");
-                                       }
-                               } else if(bind[0] == '+' && !down && keydown[key] == 0)
-                                       Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
+                                               Cbuf_InsertText(cmd, bind);
+                               }
+                               else if(bind[0] == '+' && !down && keydown[key] == 0)
+                                       Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
                        }
                        break;
                default: