Call xsetenv() in main process instead of child
[st.git] / st.c
diff --git a/st.c b/st.c
index 1e4196e..668b312 100644 (file)
--- a/st.c
+++ b/st.c
@@ -60,7 +60,7 @@ char *argv0;
 #define ISDELIM(u)             (utf8strchr(worddelimiters, u) != NULL)
 
 /* constants */
-#define ISO14755CMD            "dmenu -w %lu -p codepoint: </dev/null"
+#define ISO14755CMD            "dmenu -w \"$WINDOWID\" -p codepoint: </dev/null"
 
 enum cursor_movement {
        CURSOR_SAVE,
@@ -198,6 +198,8 @@ static char utf8encodebyte(Rune, size_t);
 static char *utf8strchr(char *s, Rune u);
 static size_t utf8validate(Rune *, size_t);
 
+static char *base64dec(const char *);
+
 static ssize_t xwrite(int, const char *, size_t);
 static void *xrealloc(void *, size_t);
 
@@ -369,6 +371,55 @@ utf8validate(Rune *u, size_t i)
        return i;
 }
 
+static const char base64_digits[] = {
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0,
+       63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, -1, 0, 0, 0, 0, 1,
+       2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
+       22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34,
+       35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+char
+base64dec_getc(const char **src)
+{
+       while (**src && !isprint(**src)) (*src)++;
+       return *((*src)++);
+}
+
+char *
+base64dec(const char *src)
+{
+       size_t in_len = strlen(src);
+       char *result, *dst;
+
+       if (in_len % 4)
+               in_len += 4 - (in_len % 4);
+       result = dst = xmalloc(in_len / 4 * 3 + 1);
+       while (*src) {
+               int a = base64_digits[(unsigned char) base64dec_getc(&src)];
+               int b = base64_digits[(unsigned char) base64dec_getc(&src)];
+               int c = base64_digits[(unsigned char) base64dec_getc(&src)];
+               int d = base64_digits[(unsigned char) base64dec_getc(&src)];
+
+               *dst++ = (a << 2) | ((b & 0x30) >> 4);
+               if (c == -1)
+                       break;
+               *dst++ = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
+               if (d == -1)
+                       break;
+               *dst++ = ((c & 0x03) << 6) | d;
+       }
+       *dst = '\0';
+       return result;
+}
+
 void
 selinit(void)
 {
@@ -655,7 +706,6 @@ execsh(void)
        setenv("SHELL", sh, 1);
        setenv("HOME", pw->pw_dir, 1);
        setenv("TERM", termname, 1);
-       xsetenv();
 
        signal(SIGCHLD, SIG_DFL);
        signal(SIGHUP, SIG_DFL);
@@ -1820,6 +1870,19 @@ strhandle(void)
                        if (narg > 1)
                                xsettitle(strescseq.args[1]);
                        return;
+               case 52:
+                       if (narg > 2) {
+                               char *dec;
+
+                               dec = base64dec(strescseq.args[2]);
+                               if (dec) {
+                                       xsetsel(dec, CurrentTime);
+                                       clipcopy(NULL);
+                               } else {
+                                       fprintf(stderr, "erresc: invalid base64\n");
+                               }
+                       }
+                       return;
                case 4: /* color set */
                        if (narg < 3)
                                break;
@@ -1929,14 +1992,11 @@ tprinter(char *s, size_t len)
 void
 iso14755(const Arg *arg)
 {
-       unsigned long id = xwinid();
-       char cmd[sizeof(ISO14755CMD) + NUMMAXLEN(id)];
        FILE *p;
        char *us, *e, codepoint[9], uc[UTF_SIZ];
        unsigned long utf32;
 
-       snprintf(cmd, sizeof(cmd), ISO14755CMD, id);
-       if (!(p = popen(cmd, "r")))
+       if (!(p = popen(ISO14755CMD, "r")))
                return;
 
        us = fgets(codepoint, sizeof(codepoint), p);
@@ -2480,7 +2540,7 @@ tresize(int col, int row)
        }
 
        /* allocate any new rows */
-       for (/* i == minrow */; i < row; i++) {
+       for (/* i = minrow */; i < row; i++) {
                term.line[i] = xmalloc(col * sizeof(Glyph));
                term.alt[i] = xmalloc(col * sizeof(Glyph));
        }