Send message size inside messages through pipes
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 25940f9..2b54e3c 100644 (file)
--- a/surf.c
+++ b/surf.c
 #include <glib.h>
 
 #include "arg.h"
+#include "common.h"
 
 #define LENGTH(x)               (sizeof(x) / sizeof(x[0]))
 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
-#define MSGBUFSZ 32
 
 enum { AtomFind, AtomGo, AtomUri, AtomLast };
 
@@ -142,7 +142,6 @@ typedef struct {
 
 /* Surf */
 static void usage(void);
-static void die(const char *errstr, ...);
 static void setup(void);
 static void sigchld(int unused);
 static void sighup(int unused);
@@ -310,17 +309,6 @@ usage(void)
            "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
 }
 
-void
-die(const char *errstr, ...)
-{
-       va_list ap;
-
-       va_start(ap, errstr);
-       vfprintf(stderr, errstr, ap);
-       va_end(ap);
-       exit(1);
-}
-
 void
 setup(void)
 {
@@ -1221,20 +1209,22 @@ newview(Client *c, WebKitWebView *rv)
 static gboolean
 readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
 {
-       char msg[MSGBUFSZ];
-       gsize msgsz;
+       static char msg[MSGBUFSZ], msgsz;
        GError *gerr = NULL;
 
-       if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) !=
+       if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
            G_IO_STATUS_NORMAL) {
                fprintf(stderr, "surf: error reading pipe: %s\n",
                        gerr->message);
                g_error_free(gerr);
                return TRUE;
        }
-       msg[msgsz] = '\0';
+       if ((msgsz = msg[0]) < 3) {
+               fprintf(stderr, "surf: message too short: %d\n", msgsz);
+               return TRUE;
+       }
 
-       switch (msg[1]) {
+       switch (msg[2]) {
        case 'i':
                close(pipein[1]);
                close(pipeout[0]);
@@ -1855,12 +1845,18 @@ zoom(Client *c, const Arg *a)
 static void
 msgext(Client *c, char type, const Arg *a)
 {
-       char msg[MSGBUFSZ] = { c->pageid, type, a->i, '\0' };
+       static char msg[MSGBUFSZ];
+       int ret;
 
-       if (pipeout[1]) {
-               if (write(pipeout[1], msg, sizeof(msg)) < 0)
-                       fprintf(stderr, "surf: error sending: %s\n", msg);
+       if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
+                           4, c->pageid, type, a->i))
+           >= sizeof(msg)) {
+               fprintf(stderr, "surf: message too long: %d\n", ret);
+               return;
        }
+
+       if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
+               fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
 }
 
 void