shithub: lola

Download patch

ref: d525a8fd26307afc029ae75f7adfed741d5ee36f
parent: 8e41616af560b69d176f9b3d4a5380e3220e8915
author: aap <aap@papnet.eu>
date: Mon Jan 30 06:49:30 EST 2023

cosmetics

--- a/fs.c
+++ b/fs.c
@@ -636,6 +636,9 @@
 	data = r->ifcall.data;
 	r->ofcall.count = count;
 
+	/* custom emalloc9p allows us this */
+	data[count] = '\0';
+
 	if(w == nil || w->deleted){
 		respond(r, Edeleted);
 		return;
@@ -723,9 +726,7 @@
 			respond(r, "non-zero offset writing label");
 			return;
 		}
-		w->label = realloc(w->label, count+1);
-		memmove(w->label, data, count);
-		w->label[count] = 0;
+		wsetlabel(w, data);
 		break;
 
 	case Qsnarf:
@@ -765,12 +766,7 @@
 		break;
 
 	case Qwctl:
-		/* sucks that we have no space for '\0' */
-		p = emalloc(count+1);
-		memmove(p, data, count);
-		p[count] = '\0';
-		respond(r, writewctl(w, p));
-		free(p);
+		respond(r, writewctl(w, data));
 		return;
 
 	case Qtap:
@@ -865,22 +861,6 @@
 	nil
 };
 
-void
-post(char *name, int srvfd)
-{
-	char buf[80];
-	int fd;
-
-	snprint(buf, sizeof buf, "/srv/%s", name);
-	fd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600);
-	if(fd < 0)
-		panic(buf);
-	if(fprint(fd, "%d", srvfd) < 0)
-		panic("post");
-	putenv("wsys", buf);
-	/* leave fd open */
-}
-
 static Ioproc *io9p;
 
 /* copy & paste from /sys/src/libc/9sys/read9pmsg.c
@@ -911,6 +891,27 @@
 	if(m < len)
 		return 0;
 	return BIT32SZ+m;
+}
+
+/* +1 so we can always zero-terminate a write buffer */
+void *emalloc9p(ulong sz) { return emalloc(sz+1); }
+void *erealloc9p(void *v, ulong sz) { return erealloc(v, sz+1); }
+char *estrdup9p(char *s) { return estrdup(s); }
+
+void
+post(char *name, int srvfd)
+{
+	char buf[80];
+	int fd;
+
+	snprint(buf, sizeof buf, "/srv/%s", name);
+	fd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600);
+	if(fd < 0)
+		panic(buf);
+	if(fprint(fd, "%d", srvfd) < 0)
+		panic("post");
+	putenv("wsys", buf);
+	/* leave fd open */
 }
 
 /*
--- a/inc.h
+++ b/inc.h
@@ -77,6 +77,7 @@
 void xpaste(Text *x);
 void xsend(Text *x);
 int xplumb(Text *w, char *dir, int maxsize);
+void freescrtemps(void);
 
 enum
 {
@@ -232,9 +233,9 @@
 Window *wpointto(Point pt);
 void wsetcursor(Window *w);
 void wsetlabel(Window *w, char *label);
+void wsetname(Window *w);
+void wsetpid(Window *w, int pid, int dolabel);
 void wmove(Window *w, Point pos);
-void wrmove(Window *w, Point delta);
-void wrmovescreen(Point delta);
 void wraise(Window *w);
 void wlower(Window *w);
 void wfocus(Window *w);
@@ -243,9 +244,6 @@
 void wsethold(Window *w, int hold);
 void wmovemouse(Window *w, Point pt);
 void wtype(Window *w, Rune r);
-void wsetname(Window *w);
-void wsetpid(Window *w, int pid, int dolabel);
-void winshell(void *args);
 int wincmd(Window *w, int pid, char *dir, char **argv);
 
 char *writewctl(Window *w, char *data);
--- a/main.c
+++ b/main.c
@@ -566,6 +566,7 @@
 			sysfatal("resize failed: %r");
 		nr = screen->clipr;
 
+		freescrtemps();
 		freescreen(wscreen);
 		wscreen = allocscreen(screen, background, 0);
 		draw(screen, screen->r, background, nil, ZP);
@@ -848,8 +849,8 @@
 threadmain(int, char *[])
 {
 	char buf[256];
-//rfork(RFENVG);
-//newwindow("-dx 1280 -dy 800");
+rfork(RFENVG);
+newwindow("-dx 1280 -dy 800");
 
 	if(getwd(buf, sizeof(buf)) == nil)
 		startdir = estrdup(".");
--- a/text.c
+++ b/text.c
@@ -275,7 +275,6 @@
 	return scrtmp;
 }
 
-/*
 void
 freescrtemps(void)
 {
@@ -284,7 +283,6 @@
 		scrtmp = nil;
 	}
 }
-*/
 
 static Rectangle
 scrpos(Rectangle r, uint p0, uint p1, uint tot)
@@ -770,6 +768,8 @@
 	uint q0, q1;
 	int nb;
 
+	xsnarf(x);
+	xcut(x);
 	switch(r){
 	case CTRL('H'):	/* erase character */
 	case CTRL('W'):	/* erase word */
@@ -789,7 +789,6 @@
 		}
 		break;
 	default:
-		xdelete(x, x->q0, x->q1);
 		xinsert(x, &r, 1, x->q0);
 		xshow(x, x->q0);
 		break;
@@ -943,7 +942,6 @@
 		plumbfree(m);
 		return 0;	/* too large for 9P */
 	}
-//	m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
 	m->data = smprint("%.*S", p1-p0, w->r+p0);
 	m->ndata = strlen(m->data);
 	if(plumbsend(fd, m) < 0){
--- a/wind.c
+++ b/wind.c
@@ -94,7 +94,6 @@
 	wcalcrects(w);
 	draw(w->img, w->img->r, colors[BACK], nil, ZP);
 	xinit(&w->text, w->textr, w->scrollr, font, w->img, colors);
-	wdecor(w);
 }
 
 static int id = 1;
@@ -108,11 +107,12 @@
 	incref(w);
 	w->id = id++;
 	w->notefd = -1;
-	w->label = estrdup("<unnamed>");
+	wsetlabel(w, "<unnamed>");
 	w->dir = estrdup(startdir);
 	w->hidden = hidden;
 	w->scrolling = scrolling;
 	wsetsize(w, r);
+	wdecor(w);
 	wlistpushfront(w);
 	// TMP - make dynamic
 	windows[nwindows++] = w;
@@ -230,31 +230,67 @@
 void
 wsetcursor(Window *w)
 {
-	if(w != cursorwin)
-		return;
+	if(w == cursorwin)
+		setcursornormal(w->holdmode ? &whitearrow : w->cursorp);
+}
 
-	if(w->holdmode)
-		setcursornormal(&whitearrow);
-	else
-		setcursornormal(w->cursorp);
+void
+wsetlabel(Window *w, char *label)
+{
+	free(w->label);
+	w->label = estrdup(label);
 }
 
 void
-wrepaint(Window *w)
+wsetname(Window *w)
 {
-	wsetcolors(w);
-	if(!w->mouseopen)
-		xredraw(&w->text);
-	wdecor(w);
+	int i, n;
+	char err[ERRMAX];
+	
+	n = snprint(w->name, sizeof(w->name)-2, "%s.%d.%d", w->noborder ? "noborder" : "window", w->id, w->namecount++);
+	for(i='A'; i<='Z'; i++){
+		if(nameimage(w->img, w->name, 1) > 0)
+			return;
+		errstr(err, sizeof err);
+		if(strcmp(err, "image name in use") != 0)
+			break;
+		w->name[n] = i;
+		w->name[n+1] = 0;
+	}
+	w->name[0] = 0;
+	fprint(2, "lola: setname failed: %s\n", err);
 }
 
 void
-wsetlabel(Window *w, char *label)
+wsetpid(Window *w, int pid, int dolabel)
 {
-	free(w->label);
-	w->label = estrdup(label);
+	char buf[32];
+	int ofd;
+
+	ofd = w->notefd;
+	if(pid <= 0)
+		w->notefd = -1;
+	else{
+		if(dolabel){
+			snprint(buf, sizeof(buf), "rc %lud", (ulong)pid);
+			wsetlabel(w, buf);
+		}
+		snprint(buf, sizeof(buf), "/proc/%lud/notepg", (ulong)pid);
+		w->notefd = open(buf, OWRITE|OCEXEC);
+	}
+	if(ofd >= 0)
+		close(ofd);
 }
 
+void
+wrepaint(Window *w)
+{
+	wsetcolors(w);
+	wdecor(w);
+	if(!w->mouseopen)
+		xredraw(&w->text);
+}
+
 /* restore window order after reshaping has disturbed it */
 void
 worder(void)
@@ -299,28 +335,6 @@
 }
 
 void
-wrmove(Window *w, Point delta)
-{
-	wmove(w, addpt(w->img->r.min, delta));
-}
-
-/* currently UNUSED */
-void
-wrmovescreen(Point delta)
-{
-	Point pos;
-	Window *w;
-
-	for(w = bottomwin; w; w = w->higher){
-		pos = addpt(w->img->r.min, delta);
-		originwindow(w->img, pos, pos);
-		wcalcrects(w);
-		xsetrects(&w->text, w->textr, w->scrollr);
-	}
-	flushimage(display, 1);
-}
-
-void
 wraise(Window *w)
 {
 	wlistremove(w);
@@ -411,10 +425,9 @@
 void
 wmovemouse(Window *w, Point pt)
 {
-	if(w != focused || wpointto(mctl->xy) != w)
-		return;
 	// TODO? rio also checks menuing and such
-	moveto(mctl, pt);
+	if(w == focused && wpointto(mctl->xy) == w)
+		moveto(mctl, pt);
 }
 
 /*
@@ -585,7 +598,6 @@
 			return;
 		case Kdown:
 			xscrolln(x, shiftdown ? 1 : nlines/3);
-//			xtickupdn(x, 1);
 			return;
 		case Kpgdown:
 			xscrolln(x, nlines*2/3);
@@ -596,7 +608,6 @@
 			return;
 		case Kup:
 			xscrolln(x, -(shiftdown ? 1 : nlines/3));
-//			xtickupdn(x, -1);
 			return;
 		case Kpgup:
 			xscrolln(x, -nlines*2/3);
@@ -784,7 +795,7 @@
 	alts[NALT].op = CHANEND;
 
 	for(;;){
-		if(w->deleted){					// TODO? rio checks image here
+		if(w->deleted){
 			alts[Agone].op = CHANSND;
 			alts[AConsWrite].op = CHANNOP;
 			alts[AConsRead].op = CHANNOP;
@@ -958,51 +969,9 @@
 	}
 }
 
-void
-wsetname(Window *w)
+static void
+shellproc(void *args)
 {
-	int i, n;
-	char err[ERRMAX];
-	
-	n = snprint(w->name, sizeof(w->name)-2, "%s.%d.%d", w->noborder ? "noborder" : "window", w->id, w->namecount++);
-	for(i='A'; i<='Z'; i++){
-		if(nameimage(w->img, w->name, 1) > 0)
-			return;
-		errstr(err, sizeof err);
-		if(strcmp(err, "image name in use") != 0)
-			break;
-		w->name[n] = i;
-		w->name[n+1] = 0;
-	}
-	w->name[0] = 0;
-	fprint(2, "lola: setname failed: %s\n", err);
-}
-
-void
-wsetpid(Window *w, int pid, int dolabel)
-{
-	char buf[32];
-	int ofd;
-
-	ofd = w->notefd;
-	if(pid <= 0)
-		w->notefd = -1;
-	else {
-		if(dolabel){
-			snprint(buf, sizeof(buf), "rc %lud", (ulong)pid);
-			free(w->label);
-			w->label = estrdup(buf);
-		}
-		snprint(buf, sizeof(buf), "/proc/%lud/notepg", (ulong)pid);
-		w->notefd = open(buf, OWRITE|OCEXEC);
-	}
-	if(ofd >= 0)
-		close(ofd);
-}
-
-void
-winshell(void *args)
-{
 	Window *w;
 	Channel *pidc;
 	void **arg;
@@ -1057,7 +1026,7 @@
 		args[2] = "/bin/rc";
 		args[3] = argv;
 		args[4] = dir;
-		proccreate(winshell, args, mainstacksize);
+		proccreate(shellproc, args, mainstacksize);
 		pid = recvul(cpid);
 		chanfree(cpid);
 		if(pid == 0){