Send message size inside messages through pipes

This commit is contained in:
Quentin Rameau 2018-10-09 12:27:59 +02:00
parent 2b71a22755
commit 02541c3146
3 changed files with 39 additions and 26 deletions

View File

@ -1,3 +1,3 @@
#define MSGBUFSZ 32 #define MSGBUFSZ 8
void die(char *, ...); void die(char *, ...);

View File

@ -41,59 +41,64 @@ newpage(WebKitWebPage *page)
static void static void
msgsurf(Page *p, const char *s) msgsurf(Page *p, const char *s)
{ {
char msg[MSGBUFSZ]; static char msg[MSGBUFSZ];
size_t sln = strlen(s);
int ret; int ret;
msg[0] = p ? p->id : 0; if ((ret = snprintf(msg, sizeof(msg), "%c%c%s",
ret = snprintf(&msg[1], sizeof(msg) - 1, "%s", s); 2 + sln, p ? p->id : 0, s))
if (ret >= sizeof(msg)) { >= sizeof(msg)) {
fprintf(stderr, "webext: message too long: %d\n", ret); fprintf(stderr, "webext: message too long: %d\n", ret);
return; return;
} }
if (pipeout) { if (pipeout && write(pipeout, msg, sizeof(msg)) < 0)
if (write(pipeout, msg, sizeof(msg)) < 0) fprintf(stderr, "webext: error sending: %.*s\n", ret-2, msg+2);
fprintf(stderr, "webext: error sending: %s\n", msg);
}
} }
static gboolean static gboolean
readpipe(GIOChannel *s, GIOCondition c, gpointer unused) readpipe(GIOChannel *s, GIOCondition c, gpointer unused)
{ {
char msg[MSGBUFSZ]; static char msg[MSGBUFSZ], msgsz;
gsize msgsz;
WebKitDOMDOMWindow *view; WebKitDOMDOMWindow *view;
GError *gerr = NULL; GError *gerr = NULL;
glong wh, ww; glong wh, ww;
Page *p; Page *p;
if (g_io_channel_read_chars(s, msg, LENGTH(msg), &msgsz, &gerr) != if (g_io_channel_read_chars(s, msg, LENGTH(msg), NULL, &gerr) !=
G_IO_STATUS_NORMAL) { G_IO_STATUS_NORMAL) {
fprintf(stderr, "webext: error reading pipe: %s\n", fprintf(stderr, "webext: error reading pipe: %s\n",
gerr->message); gerr->message);
g_error_free(gerr); g_error_free(gerr);
return TRUE; return TRUE;
} }
msg[msgsz] = '\0'; if ((msgsz = msg[0]) < 3) {
fprintf(stderr, "webext: message too short: %d\n", msgsz);
return TRUE;
}
for (p = pages; p; p = p->next) { for (p = pages; p; p = p->next) {
if (p->id == msg[0]) if (p->id == msg[1])
break; break;
} }
if (!p || !(view = webkit_dom_document_get_default_view( if (!p || !(view = webkit_dom_document_get_default_view(
webkit_web_page_get_dom_document(p->webpage)))) webkit_web_page_get_dom_document(p->webpage))))
return TRUE; return TRUE;
switch (msg[1]) { switch (msg[2]) {
case 'h': case 'h':
if (msgsz != 4)
return TRUE;
ww = webkit_dom_dom_window_get_inner_width(view); ww = webkit_dom_dom_window_get_inner_width(view);
webkit_dom_dom_window_scroll_by(view, webkit_dom_dom_window_scroll_by(view,
(ww / 100) * msg[2], 0); (ww / 100) * msg[3], 0);
break; break;
case 'v': case 'v':
if (msgsz != 4)
return TRUE;
wh = webkit_dom_dom_window_get_inner_height(view); wh = webkit_dom_dom_window_get_inner_height(view);
webkit_dom_dom_window_scroll_by(view, webkit_dom_dom_window_scroll_by(view,
0, (wh / 100) * msg[2]); 0, (wh / 100) * msg[3]);
break; break;
} }

26
surf.c
View File

@ -1209,20 +1209,22 @@ newview(Client *c, WebKitWebView *rv)
static gboolean static gboolean
readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused) readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
{ {
char msg[MSGBUFSZ]; static char msg[MSGBUFSZ], msgsz;
gsize msgsz;
GError *gerr = NULL; 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) { G_IO_STATUS_NORMAL) {
fprintf(stderr, "surf: error reading pipe: %s\n", fprintf(stderr, "surf: error reading pipe: %s\n",
gerr->message); gerr->message);
g_error_free(gerr); g_error_free(gerr);
return TRUE; 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': case 'i':
close(pipein[1]); close(pipein[1]);
close(pipeout[0]); close(pipeout[0]);
@ -1843,12 +1845,18 @@ zoom(Client *c, const Arg *a)
static void static void
msgext(Client *c, char type, const Arg *a) 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 ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
if (write(pipeout[1], msg, sizeof(msg)) < 0) 4, c->pageid, type, a->i))
fprintf(stderr, "surf: error sending: %s\n", msg); >= 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 void