shithub: orca

Download patch

ref: 7b18a6644afefa5457c36b6e860c45953c7952a7
parent: 823cd957881a2b145faf53365bef958ec6ac192f
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Feb 13 10:16:15 EST 2020

plan9: don't block on channel send (sim and kproc), sanitize alt array indices

--- a/plan9.c
+++ b/plan9.c
@@ -15,6 +15,13 @@
 	Txtoff = 16,
 	Coloff = 2,
 	Msgredraw = 0,
+
+	Chanchar = 0,
+	Chankey,
+	Chanmouse,
+	Chanresize,
+	Chanredraw,
+	Numchan,
 };
 
 typedef struct Key Key;
@@ -186,7 +193,7 @@
 		processnew = nanosec();
 		process(&events);
 		tick++;
-		sendul(drawchan, Msgredraw);
+		nbsendul(drawchan, Msgredraw);
 
 		oldn = start;
 		end = start + 1;
@@ -380,9 +387,9 @@
 	if (insert) {
 		selset(key);
 	} else {
+		field.buffer[curx + field.width*cury] = key;
 		if (curx < field.width-1)
 			curx++;
-		field.buffer[curx + field.width*cury] = key;
 	}
 }
 
@@ -462,7 +469,7 @@
 		switch (buf[0]) {
 		case 'c':
 			if (chartorune(&r, buf+1) > 0 && r != Runeerror)
-				send(cchan, &r);
+				nbsend(cchan, &r);
 			/* no break */
 		default:
 			continue;
@@ -474,7 +481,7 @@
 				if (utfrune(buf2+1, r) == nil) {
 					key.down = 1;
 					key.rune = r;
-					send(kchan, &key);
+					nbsend(kchan, &key);
 				}
 			}
 			break;
@@ -486,7 +493,7 @@
 				if(utfrune(buf+1, r) == nil) {
 					key.down = 0;
 					key.rune = r;
-					send(kchan, &key);
+					nbsend(kchan, &key);
 				}
 			}
 			break;
@@ -505,12 +512,12 @@
 	char tmp[256];
 	Channel *kchan;
 	int oldw, oldh, w, h, n, shiftdown;
-	Alt a[] = {
-		{ nil, &m, CHANRCV },
-		{ nil, nil, CHANRCV },
-		{ nil, &key, CHANRCV },
-		{ nil, nil, CHANRCV },
-		{ nil, &key.rune, CHANRCV },
+	Alt a[Numchan+1] = {
+		[Chanchar] = { nil, &key.rune, CHANRCV },
+		[Chankey] = { nil, &key, CHANRCV },
+		[Chanmouse] = { nil, &m, CHANRCV },
+		[Chanresize] = { nil, nil, CHANRCV },
+		[Chanredraw] = { nil, nil, CHANRCV },
 		{ nil, nil, CHANEND },
 	};
 
@@ -531,11 +538,11 @@
 	kctl.file = "/dev/null";
 	kctl.consfd = kctl.pid = kctl.ctlfd = -1;
 
-	a[0].c = mctl->c;
-	a[1].c = mctl->resizec;
-	a[2].c = kchan;
-	a[3].c = chancreate(sizeof(ulong), 20);
-	a[4].c = cchan;
+	a[Chanchar].c = cchan;
+	a[Chankey].c = kchan;
+	a[Chanmouse].c = mctl->c;
+	a[Chanresize].c = mctl->resizec;
+	a[Chanredraw].c = chancreate(sizeof(ulong), 1);
 
 	curbg = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, setalpha(DYellow, 128));
 	charw = stringwidth(font, "X");
@@ -551,7 +558,7 @@
 	mbuf_reusable_ensure_size(&mbuf, h, w);
 	oevent_list_init(&events);
 
-	proccreate(orcathread, a[3].c, mainstacksize);
+	proccreate(orcathread, a[Chanredraw].c, mainstacksize);
 	shiftdown = 0;
 	altdown = 0;
 
@@ -561,7 +568,7 @@
 		oldh = h = field.height;
 
 		switch (alt(a)) {
-		case 0: /* mouse */
+		case Chanmouse: /* mouse */
 			switch (m.buttons & 7) {
 			case 1:
 				select();
@@ -586,11 +593,11 @@
 			}
 			break;
 
-		case 1: /* resize */
+		case Chanresize: /* resize */
 			getwindow(display, Refnone);
 			break;
 
-		case 2: /* key up/down */
+		case Chankey: /* key up/down */
 			switch (key.rune) {
 			case Kshift:
 				shiftdown = key.down;
@@ -601,10 +608,10 @@
 			}
 			break;
 
-		case 3: /* redraw */
+		case Chanredraw: /* redraw */
 			break;
 
-		case 4: /* key */
+		case Chanchar: /* key */
 			switch (key.rune) {
 			case 0x0b: /* C-k */
 			case Kup:
@@ -662,6 +669,13 @@
 				goto end;
 			case 0x12: /* C-r */
 				tick = 0;
+				break;
+			case 0x13: /* C-s */
+				tmp[0] = 0;
+				if (filename[0])
+					fieldsave(filename);
+				else if (enter("file path:", tmp, sizeof(tmp), mctl, &kctl, nil) > 0 && fieldsave(tmp) == 0)
+					strncpy(filename, tmp, sizeof(filename));
 				break;
 			case 0x18: /* C-x */
 				selcopy();