9front - general discussion about 9front
 help / color / mirror / Atom feed
* bio io functions
@ 2017-05-04 16:02 Julius Schmidt
  2017-05-04 16:23 ` [9front] " Benjamin Purcell
  0 siblings, 1 reply; 16+ messages in thread
From: Julius Schmidt @ 2017-05-04 16:02 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: TEXT/PLAIN, Size: 525 bytes --]

I would like to change bio to do all I/O through a user replaceable 
function. The attached patch does just that, adding a new function Biofn() 
to register such a function, which replaces read/write.

Main uses I have in mind:

1. Allow programs to flush output buffers before stalling for more input, 
which would most likely be activated with a command line flag.

2. Use bio with things that are not file descriptors.

3. Apply filters to input/outputs (arguably could also be done with 
pipes).

4. Use bio with ioprocs.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: TEXT/x-diff; name=bio.patch, Size: 4208 bytes --]

diff -r 36315eb9647c sys/include/bio.h
--- a/sys/include/bio.h	Tue May 02 04:07:21 2017 +0200
+++ b/sys/include/bio.h	Thu May 04 15:50:15 2017 +0000
@@ -33,6 +33,7 @@
 	uchar*	ebuf;		/* pointer to end of buffer */
 	uchar*	gbuf;		/* pointer to good data in buf */
 	void	(*errorf)(char *);	/* called on error if not nil */
+	int	(*iof)(Biobufhdr*, void *, long);	/* called to do i/o */
 };
 
 struct	Biobuf
@@ -74,6 +75,7 @@
 long	Bwrite(Biobufhdr*, void*, long);
 void	Blethal(Biobufhdr*, void(*)(char*));
 void	Berror(Biobufhdr*, char*, ...);
+void	Biofn(Biobufhdr*, int(*)(Biobufhdr*, void*, long));
 
 #pragma	varargck	argpos	Bprint	2
 #pragma	varargck	argpos	Berror	2
diff -r 36315eb9647c sys/src/libbio/bflush.c
--- a/sys/src/libbio/bflush.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bflush.c	Thu May 04 15:50:15 2017 +0000
@@ -12,7 +12,7 @@
 		n = bp->bsize+bp->ocount;
 		if(n == 0)
 			return 0;
-		c = write(bp->fid, bp->bbuf, n);
+		c = bp->iof(bp, bp->bbuf, n);
 		if(n == c) {
 			bp->offset += n;
 			bp->ocount = -bp->bsize;
diff -r 36315eb9647c sys/src/libbio/bgetc.c
--- a/sys/src/libbio/bgetc.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bgetc.c	Thu May 04 15:50:15 2017 +0000
@@ -24,7 +24,7 @@
 	 * buffer to allow that many ungets.
 	 */
 	memmove(bp->bbuf-Bungetsize, bp->ebuf-Bungetsize, Bungetsize);
-	i = read(bp->fid, bp->bbuf, bp->bsize);
+	i = bp->iof(bp, bp->bbuf, bp->bsize);
 	bp->gbuf = bp->bbuf;
 	if(i <= 0) {
 		bp->state = Bracteof;
diff -r 36315eb9647c sys/src/libbio/binit.c
--- a/sys/src/libbio/binit.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/binit.c	Thu May 04 15:50:15 2017 +0000
@@ -50,6 +50,18 @@
 	}
 }
 
+static int
+bioread(Biobufhdr *bp, void *v, long n)
+{
+	return read(bp->fid, v, n);
+}
+
+static int
+biowrite(Biobufhdr *bp, void *v, long n)
+{
+	return write(bp->fid, v, n);
+}
+
 int
 Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size)
 {
@@ -64,12 +76,14 @@
 	case OREAD:
 		bp->state = Bractive;
 		bp->ocount = 0;
+		bp->iof = bioread;
 		break;
 
 	case OWRITE:
 		install(bp);
 		bp->state = Bwactive;
 		bp->ocount = -size;
+		bp->iof = biowrite;
 		break;
 	}
 	bp->bbuf = p;
@@ -154,3 +168,15 @@
 	/* otherwise opened with Binit(s) */
 	return r;
 }
+
+void
+Biofn(Biobufhdr *bp, int (*f)(Biobufhdr *, void *, long))
+{
+	if(f == nil)
+		if(bp->state == Bwactive)
+			bp->iof = biowrite;
+		else
+			bp->iof = bioread;
+	else
+		bp->iof = f;
+}
diff -r 36315eb9647c sys/src/libbio/brdline.c
--- a/sys/src/libbio/brdline.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/brdline.c	Thu May 04 15:50:15 2017 +0000
@@ -46,7 +46,7 @@
 	 */
 	ip = (char*)bp->bbuf + i;
 	while(i < bp->bsize) {
-		j = read(bp->fid, ip, bp->bsize-i);
+		j = bp->iof(bp, ip, bp->bsize-i);
 		if(j < 0)
 			Berror(bp, "read error: %r");
 		if(j <= 0) {
diff -r 36315eb9647c sys/src/libbio/brdstr.c
--- a/sys/src/libbio/brdstr.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/brdstr.c	Thu May 04 15:50:15 2017 +0000
@@ -69,7 +69,7 @@
 	for(;;){
 		ip = (char*)bp->bbuf + i;
 		while(i < bp->bsize) {
-			j = read(bp->fid, ip, bp->bsize-i);
+			j = bp->iof(bp, ip, bp->bsize-i);
 			if(j < 0)
 				Berror(bp, "read error: %r");
 			if(j <= 0 && i == 0)
diff -r 36315eb9647c sys/src/libbio/bread.c
--- a/sys/src/libbio/bread.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bread.c	Thu May 04 15:50:15 2017 +0000
@@ -20,7 +20,7 @@
 		if(n == 0) {
 			if(bp->state != Bractive)
 				break;
-			i = read(bp->fid, bp->bbuf, bp->bsize);
+			i = bp->iof(bp, bp->bbuf, bp->bsize);
 			if(i <= 0) {
 				bp->state = Bracteof;
 				if(i < 0) {
diff -r 36315eb9647c sys/src/libbio/bwrite.c
--- a/sys/src/libbio/bwrite.c	Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bwrite.c	Thu May 04 15:50:15 2017 +0000
@@ -21,7 +21,7 @@
 		if(n == 0) {
 			if(bp->state != Bwactive)
 				return Beof;
-			i = write(bp->fid, bp->bbuf, bp->bsize);
+			i = bp->iof(bp, bp->bbuf, bp->bsize);
 			if(i != bp->bsize) {
 				errstr(errbuf, sizeof errbuf);
 				if(strstr(errbuf, "interrupt") == nil) {

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-05-05 15:24 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-04 16:02 bio io functions Julius Schmidt
2017-05-04 16:23 ` [9front] " Benjamin Purcell
2017-05-04 17:52   ` Giacomo Tesio
2017-05-04 20:18     ` Bruce Ellis
2017-05-04 20:21       ` Benjamin Purcell
2017-05-05  9:11       ` hiro
2017-05-05  9:14         ` Bruce Ellis
2017-05-05  9:55           ` Julius Schmidt
2017-05-05  9:56             ` Bruce Ellis
2017-05-05 10:21           ` Stanley Lieber
2017-05-05 10:29             ` Bruce Ellis
2017-05-05 10:32               ` Stanley Lieber
2017-05-05 10:33                 ` Bruce Ellis
2017-05-05 11:05                   ` Stanley Lieber
2017-05-05 12:11                   ` hiro
2017-05-05 15:24               ` Kurt H Maier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).