st

fork of suckless's simple terminal
Index Commits Files Refs README LICENSE
commit 4e6915a16b75c1e79142e15a9b23e761140d4e9b
parent dab8326e6428a5db6b7a6e08f40fa1e5ac77bc6f
Author: Aurélien Aptel <aurelien.aptel@gmail.com>
Date:   Mon,  7 Sep 2009 21:19:29 +0200

Fixed possible segfault by reverting to the key struct (again).

Diffstat:
Mconfig.h | 21++++++++++-----------
Mst.c | 21+++++++++++++++++++--
2 files changed, 29 insertions(+), 13 deletions(-)
diff --git a/config.h b/config.h
@@ -24,16 +24,15 @@ static const char *colorname[] = {
 #define DefaultCS 1
 #define BellCol   DefaultFG
 
-
 /* special keys */
-static const char *key[] = {
-    [XK_Delete] = "\033[3~", 
-    [XK_Home]   = "\033[1~",
-    [XK_End]    = "\033[4~",
-    [XK_Prior]  = "\033[5~",
-    [XK_Next]   = "\033[6~",
-    [XK_Left]   = "\033[D",
-    [XK_Right]  = "\033[C",
-    [XK_Up]     = "\033[A",
-    [XK_Down]   = "\033[B",
+static Key key[] = {
+    { XK_Delete, "\033[3~" },
+    { XK_Home,   "\033[1~" },
+    { XK_End,    "\033[4~" },
+    { XK_Prior,  "\033[5~" },
+    { XK_Next,   "\033[6~" },
+    { XK_Left,   "\033[D" },
+    { XK_Right,  "\033[C" },
+    { XK_Up,     "\033[A" },
+    { XK_Down,   "\033[B" },
 };
diff --git a/st.c b/st.c
@@ -93,6 +93,11 @@ typedef struct {
     int cw; /* char width  */
 } XWindow; 
 
+typedef struct {
+    KeySym k;
+    char s[ESCSIZ];
+} Key;
+
 #include "config.h"
 
 /* Drawing Context */
@@ -146,6 +151,7 @@ static void xinit(void);
 static void xscroll(void);
 
 static void expose(XEvent *);
+static char * kmap(KeySym);
 static void kpress(XEvent *);
 static void resize(XEvent *);
 
@@ -1005,11 +1011,21 @@ expose(XEvent *ev) {
     draw(SCredraw);
 }
 
+char *
+kmap(KeySym k) {
+    int i;
+    for(i = 0; i < LEN(key); i++)
+        if(key[i].k == k)
+            return (char*)key[i].s;
+    return NULL;
+}
+
 void
 kpress(XEvent *ev) {
     XKeyEvent *e = &ev->xkey;
     KeySym ksym;
     char buf[32];
+    char *customkey;
     int len;
     int meta;
     int shift;
@@ -1017,8 +1033,9 @@ kpress(XEvent *ev) {
     meta  = e->state & Mod1Mask;
     shift = e->state & ShiftMask;
     len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
-    if(key[ksym])
-        ttywrite(key[ksym], strlen(key[ksym]));
+
+    if(customkey = kmap(ksym))
+        ttywrite(customkey, strlen(customkey));
     else if(len > 0) {
         buf[sizeof(buf)-1] = '\0';
         if(meta && len == 1)