summaryrefslogtreecommitdiff
path: root/sys/src/libbio
diff options
context:
space:
mode:
authoraiju <devnull@localhost>2017-05-04 17:42:12 +0000
committeraiju <devnull@localhost>2017-05-04 17:42:12 +0000
commitf681cf835af1e5c9e016e5245c24165b029a5e38 (patch)
tree43453c10a803a5e64b47dc3474e603715cd76bcc /sys/src/libbio
parent414d29e98f8d5242cc68161e61b66b7171e96634 (diff)
bio: add support for custom I/O handler via Biofn
Diffstat (limited to 'sys/src/libbio')
-rw-r--r--sys/src/libbio/bflush.c2
-rw-r--r--sys/src/libbio/bgetc.c2
-rw-r--r--sys/src/libbio/binit.c26
-rw-r--r--sys/src/libbio/brdline.c2
-rw-r--r--sys/src/libbio/brdstr.c2
-rw-r--r--sys/src/libbio/bread.c2
-rw-r--r--sys/src/libbio/bwrite.c2
7 files changed, 32 insertions, 6 deletions
diff --git a/sys/src/libbio/bflush.c b/sys/src/libbio/bflush.c
index 44b0d59a9..5a0a5f034 100644
--- a/sys/src/libbio/bflush.c
+++ b/sys/src/libbio/bflush.c
@@ -12,7 +12,7 @@ Bflush(Biobufhdr *bp)
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 --git a/sys/src/libbio/bgetc.c b/sys/src/libbio/bgetc.c
index 0cabc0716..c8f716368 100644
--- a/sys/src/libbio/bgetc.c
+++ b/sys/src/libbio/bgetc.c
@@ -24,7 +24,7 @@ loop:
* 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 --git a/sys/src/libbio/binit.c b/sys/src/libbio/binit.c
index 9afae5b34..13fd0f4ce 100644
--- a/sys/src/libbio/binit.c
+++ b/sys/src/libbio/binit.c
@@ -50,6 +50,18 @@ install(Biobufhdr *bp)
}
}
+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 @@ Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size)
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 @@ Bterm(Biobufhdr *bp)
/* 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 --git a/sys/src/libbio/brdline.c b/sys/src/libbio/brdline.c
index 4168d1e10..0a3512daa 100644
--- a/sys/src/libbio/brdline.c
+++ b/sys/src/libbio/brdline.c
@@ -46,7 +46,7 @@ Brdline(Biobufhdr *bp, int delim)
*/
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 --git a/sys/src/libbio/brdstr.c b/sys/src/libbio/brdstr.c
index 5e27c033d..ec163db7f 100644
--- a/sys/src/libbio/brdstr.c
+++ b/sys/src/libbio/brdstr.c
@@ -69,7 +69,7 @@ Brdstr(Biobufhdr *bp, int delim, int nulldelim)
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 --git a/sys/src/libbio/bread.c b/sys/src/libbio/bread.c
index 8e57d8e07..8ad61960c 100644
--- a/sys/src/libbio/bread.c
+++ b/sys/src/libbio/bread.c
@@ -20,7 +20,7 @@ Bread(Biobufhdr *bp, void *ap, long count)
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 --git a/sys/src/libbio/bwrite.c b/sys/src/libbio/bwrite.c
index 03f46779b..373038c53 100644
--- a/sys/src/libbio/bwrite.c
+++ b/sys/src/libbio/bwrite.c
@@ -21,7 +21,7 @@ Bwrite(Biobufhdr *bp, void *ap, long count)
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) {