shithub: Nail

Download patch

ref: d2ddeb025f374eef4234007c19bd5f7bcc2d8118
parent: 0cd0d013de0f13501b5247c70a8619bfb57c97f7
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Nov 4 18:22:22 EST 2020

clear win fully on close, don't double-send events, tweak shutdown code.

--- a/comp.c
+++ b/comp.c
@@ -114,9 +114,11 @@
 		case 'X':
 			if((nf = tokenize(ev.text, f, nelem(f))) == 0)
 				continue;
-			for(p = compfn; p->name != nil; p++)
-				if(strcmp(p->name, f[0]) == 0)
+			for(p = compfn; p->fn != nil; p++)
+				if(strcmp(p->name, f[0]) == 0){
 					p->fn(c, &f[1], nf - 1);
+					break;
+				}
 			if(p->name == nil && !(ev.flags & 0x2))
 				winreturn(&mbox, &ev);
 			break;
--- a/mail.h
+++ b/mail.h
@@ -169,8 +169,8 @@
 Mesg	*mblookupid(char*);
 Mesg	*mesglookup(char*, char*);
 Mesg	*mesgload(char*);
+Mesg	*mesgopen(char*, char*);
 int	mesgmatch(Mesg*, char*, char*);
-void	mesgopen(char*, char*);
 void	mesgclear(Mesg*);
 void	mesgfree(Mesg*);
 void	mesgpath2name(char*, int, char*);
--- a/mbox.c
+++ b/mbox.c
@@ -5,6 +5,7 @@
 #include <plumb.h>
 #include <ctype.h>
 #include <regexp.h>
+#include <pool.h>
 
 #include "mail.h"
 
@@ -72,6 +73,7 @@
 {
 	Event *ev;
 
+	threadsetname("mbevent");
 	while(1){
 		ev = emalloc(sizeof(Event));
 		if(winevent(&mbox, ev) == -1)
@@ -78,7 +80,6 @@
 			break;
 		sendp(mbox.event, ev);
 	}
-	closeioproc(mbox.io);
 	sendp(mbox.event, nil);
 	threadexits(nil);
 }
@@ -370,6 +371,7 @@
 	return m;
 error:
 	fprint(2, "load failed: %r\n");
+	abort();
 	mesgfree(m);
 	return nil;
 }
@@ -536,7 +538,6 @@
 			continue;
 		}
 		ln = mesglineno(m, nil);
-		fprint(2, "remove %s@%d,%d\n", m->name, ln+1, ln+1+m->nsub);
 		fprint(mbox.addr, "%d,%d", ln+1, ln+1+m->nsub);
 		write(mbox.data, "", 0);
 		if(m->flags & Ftodel)
@@ -615,27 +616,11 @@
 static void
 mbquit(char **, int)
 {
-	Event *e;
-	Ioproc *io;
-
 	if(mbox.nopen > 0){
 		fprint(2, "Del: %d open messages\n", mbox.nopen);
 		return;
 	}
-
-	/*
-	 * Do a bit of a dance to ensure that we don't
-	 * try to read from the ioproc after we close
-	 * it while exiting.
-	 */
-	io = mbox.io;
-	mbox.io = nil;
-
-	winclose(&mbox);
-	iointerrupt(io);
-	while((e = recvp(mbox.event)) != nil)	
-		free(e);
-	closeioproc(io);		
+	fprint(mbox.ctl, "del\n");
 	threadexitsall(nil);
 }
 
@@ -651,7 +636,7 @@
 
 	digest = plumblookup(pm->attr, "digest");
 	action = plumblookup(pm->attr, "mailtype");
-//	fprint(2, "changing message %s, %s %s\n", action, pm->data, digest);
+	fprint(2, "changing message %s, %s %s\n", action, pm->data, digest);
 	if(strcmp(action, "new") == 0){
 		m = load(pm->data, digest, 1);
 		add = 1;
@@ -667,7 +652,12 @@
 static void
 viewmesg(Plumbmsg *pm)
 {
-	mesgopen(pm->data, plumblookup(pm->attr, "digest"));
+	Mesg *m;
+	m = mesgopen(pm->data, plumblookup(pm->attr, "digest"));
+	if(m != nil){
+		fprint(mbox.addr, "%d", mesglineno(m, nil));
+		fprint(mbox.ctl, "dot=addr\n");
+	}
 }
 
 Fn mboxfn[] = {
@@ -693,6 +683,8 @@
 
 	if(ev->action != 'M')
 		return;
+	poolcheck(mainmem);
+	print("event %c %s\n", ev->type, ev->text);
 	switch(ev->type){
 	case 'l':
 	case 'L':
@@ -708,18 +700,19 @@
 	case 'X':
 		if((nf = tokenize(ev->text, f, nelem(f))) == 0)
 			return;
-		for(p = mboxfn; p->name != nil; p++)
-			if(strcmp(p->name, f[0]) == 0 && p->fn != nil)
+		for(p = mboxfn; p->fn != nil; p++)
+			if(strcmp(p->name, f[0]) == 0 && p->fn != nil){
 				p->fn(&f[1], nf - 1);
+				break;
+			}
 		if(p->fn == nil)
 			winreturn(&mbox, ev);
 		break;
-	break;
 	}
 }
 
 static void
-mbmain(void *)
+mbmain(void)
 {
 	Event *ev;
 	Plumbmsg *pm;
@@ -784,9 +777,9 @@
 	plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
 	plumbseemailfd = plumbopen("seemail", OREAD|OCEXEC);
 	plumbshowmailfd = plumbopen("showmail", OREAD|OCEXEC);
-	mbox.event = chancreate(sizeof(Event*), 1);
-	mbox.see = chancreate(sizeof(Plumbmsg*), 1);
-	mbox.show = chancreate(sizeof(Plumbmsg*), 1);
+	mbox.event = chancreate(sizeof(Event*), 0);
+	mbox.see = chancreate(sizeof(Plumbmsg*), 0);
+	mbox.show = chancreate(sizeof(Plumbmsg*), 0);
 
 	addrpat = regcomp("[^ \t]*@[^ \t]*\\.[^ \t]*");
 	mesgpat = regcomp("(\\(deleted\\)-)?[0-9]+/.*");
@@ -797,8 +790,10 @@
 	if(argc == 1)
 		mailbox = argv[0];
 	mbload();
-
-	threadcreate(mbmain, nil, Stack);
+	poolcheck(mainmem);
+	mainmem->flags |= POOL_PARANOIA;
 	proccreate(plumbsee, nil, Stack);
 	proccreate(plumbshow, nil, Stack);
+	threadsetname("mbox %s", mbox.path);
+	mbmain();
 }
--- a/mesg.c
+++ b/mesg.c
@@ -88,8 +88,11 @@
 
 	m = emalloc(sizeof(Mesg));
 	m->name = estrjoin(name, "/", nil);
-	if((info = rslurp(m, "info", &ninfo)) == nil)
+	if((info = rslurp(m, "info", &ninfo)) == nil){
+		free(m->name);
+		free(m);
 		return nil;
+	}
 
 	p = info;
 	m->from = line(p, &p, 0);
@@ -370,8 +373,8 @@
 		case 'x':
 		case 'X':
 			if((nf = tokenize(ev.text, f, nelem(f))) == 0)
-				continue;
-			for(p = mesgfn; p->name != nil; p++){
+				continue;//
+			for(p = mesgfn; p->fn != nil; p++){
 				if(strcmp(p->name, f[0]) == 0 && p->fn != nil){
 					p->fn(m, &f[1], nf - 1);
 					break;
@@ -426,7 +429,7 @@
 	return nil;
 }
 
-void
+Mesg*
 mesgopen(char *name, char *digest)
 {
 	Mesg *m;
@@ -435,7 +438,8 @@
 
 	m = mesglookup(name, digest);
 	if(m == nil || (m->flags & Fopen))
-		return;
+		return nil;
+
 	assert(!(m->flags & Fdummy));
 	m->flags |= Fopen;
 	if(m->flags & Funseen){
@@ -449,4 +453,5 @@
 		free(path);
 	}
 	threadcreate(mesgmain, m, Stack);
+	return m;
 }
--- a/win.c
+++ b/win.c
@@ -128,6 +128,7 @@
 {
 	char buf[12];
 
+	w->io = ioproc();
 	w->ctl = open("/mnt/wsys/new/ctl", ORDWR|OCEXEC);
 	if(w->ctl < 0)
 		sysfatal("winopen: %r");
@@ -137,8 +138,6 @@
 		sysfatal("write ctl: %r");
 	if(fprint(w->ctl, "noscroll\n") == -1)
 		sysfatal("write ctl: %r");
-	if((w->io = ioproc()) == nil)
-		sysfatal("ioproc alloc: %r");
 	w->id = atoi(buf);
 	w->event = bwinopen(w, "event", OREAD);
 	w->revent = winopen(w, "event", OWRITE);
@@ -160,6 +159,11 @@
 		closeioproc(w->io);
 	if(w->ctl != -1)
 		close(w->ctl);
+	w->ctl = -1;
+	w->data = -1;
+	w->addr = -1;
+	w->evetn = nil;
+	w->io = nil;
 }
 
 void