]> 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 1d320ec7c14ea866d9e499701e23ccfbfeb9ba86..9ff4853311313e4104819f30b990c603370b3f18 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -1364,9 +1364,10 @@ Key_StringToKeynum (const char *str)
                        return kn->keynum;
        }
 
-       // non-ascii keys are Unicode codepoints, so give the character
+       // 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 ? -1 : (int)ch;
+       return (ch == 0 || *str != 0) ? -1 : (int)ch;
 }
 
 /*
@@ -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);
                }
        }
 }
@@ -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);
                        }
                }
        }
@@ -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;
        }
@@ -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: