shithub: libacme

Download patch

ref: 78e5064ffdce3b4b2b5ea42ce0b45c75cbce6bf3
parent: 521670692b07cbadbf6a88964839d76531ba9afd
author: james palmer <foura@biobuf.link>
date: Wed Jun 9 05:30:00 EDT 2021

clean up. remove print fns, use fprint instead

--- a/acme.h
+++ b/acme.h
@@ -13,9 +13,9 @@
 	int ctlfd;
 	int addrfd;
 	int datafd;
+	int eventfd;
+	int bodyfd;
 	
-	Channel *eventc;
-	
 	void *aux;
 	
 	AWin *next;
@@ -33,24 +33,16 @@
 	char text[AEventSz + 1];
 };
 
-int			awinfsopen(AWin *, char *, int);
-Biobuf *	awinfsbopen(AWin *, char *, int);
-
-
+int		awinfsopen(AWin *, char *, int);
 AWin *	awincreate(void);
 void	awinclose(AWin *);
 void	awincloseall(void);
 
-int		awinctl(AWin *, char *, ...);
-void	awinclear(AWin *);
-int		awincat(AWin *, int fd);
-int		awinprint(AWin *, char *, ...);
-int		awinerror(AWin *, char *, ...);
-int		awinfatal(AWin *, char *, ...);
-void	awinaddtag(AWin *, char *);
-void	awinsettag(AWin *, char *);
+int		awinload(AWin *, char *);
+int		awinput(AWin *, char *);
 
-Channel *	aeventlisten(AWin *);
-void		aeventstop(AWin *);
-int			aeventnext(Biobuf *bio, AEvent *);
-void		aeventsend(AWin *, AEvent *);
+int		awinaddtag(AWin *, char *);
+int		awinsettag(AWin *, char *);
+
+int		aeventnext(AWin *, AEvent *);
+int		aeventsend(AWin *, AEvent *);
--- a/event.c
+++ b/event.c
@@ -6,14 +6,25 @@
 #include "acme.h"
 
 static int
-aeventgetnum(Biobuf *bio)
+getc(int fd)
 {
-	int c, n;
+	char buf[1];
 	
+	if(read(fd, buf, sizeof(buf)) < 0)
+		return buf[0];
+	return -1;
+}
+
+static int
+getnum(int fd)
+{
+	int c, n;
+
 	n = 0;
-	while('0' <= (c = Bgetc(bio)) && c <= '9')
+	while('0' <= (c = getc(fd)) && c <= '9')
 		n = n*10+(c-'0');
-	if(c != ' ') {
+	
+	if(c != ' '){
 		werrstr("event number syntax: %c", c);
 		return -1;
 	}
@@ -21,105 +32,59 @@
 	return n;
 }
 
+static long;
+getrune(int fd)
+{
+	Rune rune;
+	char buf[UTFmax];
+	
+	if(read(fd, buf, sizeof(buf)))
+		return chartorune(&rune, buf);
+		
+	return -1;
+}
+
 static int
-aeventgetdata(Biobuf *bio, AEvent *ev)
+getdata(int fd, AEvent *e)
 {
-	int i, runes, len;
-	Rune r;
+	int i, n, o;
+	long r;
 	
-	len = 0;
-	runes = aeventgetnum(bio);
-	for(i = 0; i < runes; i++) {
-		if((r = Bgetrune(bio)) == -1)
+	o = 0;
+	n = getnum(fd);
+	for(i = 0; i < n; i++) {
+		if((r = getrune(fd)) < 0)
 			break;
-		len += runetochar(ev->text + len, &r);
+		o += runetochar(e->text + o, (Rune*)&r);
 	}
 	
-	ev->text[len] = '\0';
-	return len;
+	e->text[o] = 0;
+	return o;
 }
 
 int
-aeventnext(Biobuf *bio, AEvent *ev)
+aeventnext(AWin *w, AEvent *e)
 {
 	int flags;
 	
 	flags = 0;
-	
-Again:
-	ev->origin = Bgetc(bio);
-	ev->type = Bgetc(bio);
-	ev->q0 = aeventgetnum(bio);
-	ev->q1 = aeventgetnum(bio);
-	ev->flags = aeventgetnum(bio);
-	ev->ntext = aeventgetdata(bio, ev);
-	if(Bgetc(bio) != '\n') {
-		werrstr("unterminated mesage");
+loop:
+	e->origin = getc(w->eventfd);
+	e->type = getc(w->eventfd);
+	e->q0 = getnum(w->eventfd);
+	e->q1 = getnum(w->eventfd);
+	e->ntext = getdata(w->eventfd, e);
+	if(getc(w->eventfd) != '\n') {
+		werrstr("unterminated message");
 		return -1;
 	}
 	
-	if(ev->flags & 0x2) {
-		ev->p = ev->q0;
-		flags = ev->flags;
-		goto Again;
+	if(e->flags & 0x2) {
+		e->p = e->q0;
+		flags = e->flags;
+		goto loop;
 	}
 	
-	ev->flags |= flags;
-	return ev->origin;
-}
-
-static void
-aeventproc(void *aux)
-{
-	AWin *w;
-	Channel *c;
-	Biobuf *bio;
-	AEvent ev;
-	
-	w = aux;
-	c = w->eventc;
-	bio = awinfsbopen(w, "event", OREAD);
-	
-	while(aeventnext(bio, &ev)) {
-		if(send(c, &ev) < 0) {
-			Bterm(bio);
-			exits(nil);
-		}
-	}
-}
-
-Channel *
-aeventlisten(AWin *w)
-{
-	if(w->eventc)
-		return w->eventc;
-		
-	w->eventc = chancreate(sizeof(AEvent), 0);
-	proccreate(aeventproc, w, 4096);
-	
-	return w->eventc;
-}
-
-void
-aeventstop(AWin *w)
-{
-	if(w->eventc) {
-		chanclose(w->eventc);
-		w->eventc = nil;
-	}
-}
-
-void
-aeventsend(AWin *w, AEvent *ev)
-{
-	int fd;
-	
-	fd = awinfsopen(w, "event", OWRITE);
-	
-	if(ev->flags & 0x2)
-		fprint(fd, "%c%c%d %d\n", ev->origin, ev->type, ev->p, ev->p);
-	else
-		fprint(fd, "%c%c%d %d\n", ev->origin, ev->type, ev->q0, ev->q1);
-		
-	close(fd);
+	e->flags |= flags;
+	return e->origin;
 }
--- a/window.c
+++ b/window.c
@@ -1,7 +1,6 @@
 #include <u.h>
 #include <libc.h>
 #include <thread.h>
-#include <bio.h>
 
 #include "acme.h"
 
@@ -20,19 +19,6 @@
 	return fd;
 }
 
-Biobuf *
-awinfsbopen(AWin *w, char *file, int mode)
-{
-	char buf[128];
-	Biobuf *fd;
-	
-	snprint(buf, sizeof(buf), "/mnt/wsys/%d/%s", w->id, file);
-	if((fd = Bopen(buf, mode)) == nil)
-		sysfatal("bopen %s: %r", file);
-		
-	return fd;
-}
-
 AWin *
 awincreate(void)
 {
@@ -47,8 +33,10 @@
 		sysfatal("read ctl: %r");
 	
 	w->id = atoi(buf);
-	w->addrfd = -1;
-	w->datafd = -1;
+	w->addrfd = awinfsopen(w, "addr", ORDWR);
+	w->datafd = awinfsopen(w, "data", ORDWR);
+	w->eventfd = awinfsopen(w, "events", ORDWR);
+	w->bodyfd = awinfsopen(w, "body", OWRITE);
 
 	w->next = awins;
 	awins = w;
@@ -87,8 +75,11 @@
 	while(temp) {
 		fprint(temp->ctlfd, "delete");
 		
+		close(temp->ctlfd);
 		close(temp->addrfd);
 		close(temp->datafd);
+		close(temp->eventfd);
+		close(temp->bodyfd);
 		
 		awins = temp->next;
 		free(temp);
@@ -98,46 +89,52 @@
 }
 
 int
-awinctl(AWin *w, char *fmt, ...)
+awinload(AWin *w, char *file)
 {
-	va_list args;
-	int ret;
+	int fd;
+	char buf[8192];
+	long n;
 	
-	va_start(args, fmt);
-	ret = vfprint(w->ctlfd, fmt, args);
-	va_end(args);
-	
-	return ret;
-}
-
-void
-awinclear(AWin *w)
-{
-	w->addrfd = awinfsopen(w, "addr", OWRITE);
-	w->datafd = awinfsopen(w, "data", OWRITE);
-	
+	fd = open(file, OREAD);
+	if(fd < 0) {
+		werrstr("awinload open failed: %r");
+		return -1;
+	}
+		
 	fprint(w->addrfd, ",");
-	write(w->datafd, "", 0);
+	while((n = read(w->datafd, buf, sizeof(buf))) > 0)
+		write(fd, buf, n);
+		
+	if(n < 0) {
+		werrstr("awinload read failed: %r");
+		close(fd);
+		return -1;
+	}
 	
-	close(w->datafd);
-	close(w->addrfd);
+	return 0;
 }
 
 int
-awincat(AWin *w, int fd)
+awinsave(AWin *w, char *file)
 {
-	int body;
-	char *buf;
+	int fd;
+	char buf[8192];
 	long n;
 	
-	buf = malloc(8*1024);
-	body = awinfsopen(w, "body", OWRITE);
+	fd = open(file, OWRITE);
+	if(fd < 0) {
+		werrstr("awinload open failed: %r");
+		return -1;
+	}
 		
-	while((n=read(fd, buf, 8*1024)) > 0) {
-		if(write(body, buf, n) != n) {
-			awinerror(w, "awincat write: %r");
-			return -1;
-		}
+	fprint(w->addrfd, ",");
+	while((n = read(fd, buf, sizeof(buf))) > 0)
+		write(w->datafd, buf, n);
+		
+	if(n < 0) {
+		werrstr("awinload read failed: %r");
+		close(fd);
+		return -1;
 	}
 	
 	return 0;
@@ -144,74 +141,26 @@
 }
 
 int
-awinprint(AWin *w, char *fmt, ...)
+awinaddtag(AWin *w, char *tag)
 {
-	int fd;
-	int ret;
-	va_list args;
+	int fd, ret;
 	
-	fd = awinfsopen(w, "body", OWRITE);
-	
-	va_start(args, fmt);
-	ret = vfprint(fd, fmt, args);
-	va_end(args);
-	
+	fd = awinfsopen(w, "tag", OWRITE);
+	ret = fprint(fd, " %s", tag);
 	close(fd);
-	return ret;
-}
-
-int
-awinerror(AWin *w, char *fmt, ...)
-{
-	int fd;
-	int ret;
-	va_list args;
 	
-	fd = awinfsopen(w, "errors", OWRITE);
-	
-	va_start(args, fmt);
-	ret = vfprint(fd, fmt, args);
-	va_end(args);
-	
-	close(fd);
 	return ret;
 }
 
 int
-awinfatal(AWin *w, char *fmt, ...)
-{
-	int fd;
-	int ret;
-	va_list args;
-	
-	fd = awinfsopen(w, "body", OWRITE);
-	
-	va_start(args, fmt);
-	ret = vfprint(fd, fmt, args);
-	va_end(args);
-	
-	awinclose(w);
-	close(fd);
-	return ret;
-}
-
-void
-awinaddtag(AWin *w, char *tag)
-{
-	int fd;
-	
-	fd = awinfsopen(w, "tag", OWRITE);
-	fprint(fd, "%s ", tag);
-	close(fd);
-}
-
-void
 awinsettag(AWin *w, char *tag)
 {
-	int fd;
+	int fd, ret;
 	
-	awinctl(w, "cleartag");
 	fd = awinfsopen(w, "tag", OWRITE);
-	fprint(fd, "%s", tag);
+	fprint(w->ctlfd, "cleartag");
+	ret = fprint(fd, "%s", tag);
 	close(fd);
+	
+	return ret;
 }