summaryrefslogtreecommitdiff
path: root/lib/bio
diff options
context:
space:
mode:
authorOri Bernstein <ori@markovcorp.com>2018-01-03 14:28:27 -0800
committerOri Bernstein <ori@markovcorp.com>2018-01-03 14:36:37 -0800
commit9ecaaf6877841fe168695140328c198607565d7d (patch)
treef2de5f7f04a87de633a735b065a9d4cb3b17814e /lib/bio
parent7937358e3e356a62b5c52b67bc1f1daf32924a02 (diff)
downloadmc-9ecaaf6877841fe168695140328c198607565d7d.tar.gz
Split out backends from abstract bio types.
Diffstat (limited to 'lib/bio')
-rw-r--r--lib/bio/bio.myr92
-rw-r--r--lib/bio/bld.sub4
-rw-r--r--lib/bio/fd.myr60
-rw-r--r--lib/bio/geti.myr1
-rw-r--r--lib/bio/iter.myr1
-rw-r--r--lib/bio/mem.myr2
-rw-r--r--lib/bio/puti.myr1
-rw-r--r--lib/bio/types.myr42
8 files changed, 112 insertions, 91 deletions
diff --git a/lib/bio/bio.myr b/lib/bio/bio.myr
index 437cde6..4db8ec9 100644
--- a/lib/bio/bio.myr
+++ b/lib/bio/bio.myr
@@ -1,50 +1,9 @@
use std
+use "types"
pkg bio =
- type mode = int
- const Rd : mode = 1
- const Wr : mode = 2
- const Rw : mode = 1 | 2
-
- type file = struct
- /* backing io ops */
- mode : mode
- lasterr : std.errno
-
- read : (buf : byte[:] -> std.result(std.size, std.errno))
- write : (buf : byte[:] -> std.result(std.size, std.errno))
- seek : (idx : std.off -> std.result(std.off, std.errno))
- close : (-> bool)
-
- /* read buffer */
- rbuf : byte[:]
- rstart : std.size
- rend : std.size
-
- /* write buffer */
- wbuf : byte[:]
- wend : std.size
- ;;
-
- type vtab = struct
- read : (buf : byte[:] -> std.result(std.size, std.errno))
- write : (buf : byte[:] -> std.result(std.size, std.errno))
- seek : (idx : std.off -> std.result(std.off, std.errno))
- close : (-> bool)
- ;;
-
- type err = union
- `Eof
- `Eio
- `Ebadf
- ;;
-
/* creation */
- const mkfile : (fd : std.fd, mode : mode -> file#)
const mk : (mode : mode, vt : vtab -> file#)
- const open : (path : byte[:], mode : mode -> std.result(file#, byte[:]))
- const dial : (srv : byte[:], mode : mode -> std.result(file#, byte[:]))
- const create : (path : byte[:], mode : mode, perm : int -> std.result(file#, byte[:]))
const close : (f : file# -> bool)
const free : (f : file# -> void)
@@ -85,15 +44,6 @@ const Bufsz = 16*std.KiB
const Small = 512
/* Creates a file from an fd, opened in the given mode. */
-const mkfile = {fd, mode
- -> mk(mode, [
- .read = {buf; -> std.read(fd, buf)},
- .write = {buf; -> std.write(fd, buf)},
- .seek = {off; -> std.seek(fd, off, std.Seekset)},
- .close = {; -> std.close(fd) >= 0},
- ])
-}
-
const mk = {mode, vt
var f
@@ -118,46 +68,6 @@ const mk = {mode, vt
-> f
}
-/* Opens a file with mode provided. */
-const open = {path, mode
- -> sysopen(path, mode, sysmode(mode), 0o777)
-}
-
-/*
- Creates a file for the provided path, with opened in
- the requested mode, with the requested permissions
-*/
-const create = {path, mode, perm
- -> sysopen(path, mode, sysmode(mode) | std.Ocreat | std.Otrunc, perm)
-}
-
-/* dial the server, and open a file using the returned fd */
-const dial = {srv, mode
- match std.dial(srv)
- | `std.Ok sock: -> `std.Ok mkfile(sock, mode)
- | `std.Err m: -> `std.Err m
- ;;
-}
-
-/* map from the bio modes to the unix open modes */
-const sysmode = {mode
- match mode
- | Rd: -> std.Oread
- | Wr: -> std.Owrite
- | Rw: -> std.Ordwr
- | _: std.fatal("bio: bad file mode")
- ;;
- -> 0
-}
-
-/* open the file, and return it */
-const sysopen = {path, mode, openmode, perm
- match std.openmode(path, openmode, (perm : int64))
- | `std.Ok fd: -> `std.Ok mkfile(fd, mode)
- | `std.Err e: -> `std.Err "could not open fd"
- ;;
-}
-
/* closes a file, flushing it to the output fd */
const close = {f
var ok
diff --git a/lib/bio/bld.sub b/lib/bio/bld.sub
index 95eeca2..b03a9a6 100644
--- a/lib/bio/bld.sub
+++ b/lib/bio/bld.sub
@@ -3,6 +3,10 @@ lib bio =
geti.myr
puti.myr
iter.myr
+ types.myr
+
+ # backends
+ fd.myr
mem.myr
lib ../std:std
diff --git a/lib/bio/fd.myr b/lib/bio/fd.myr
new file mode 100644
index 0000000..aef4d2e
--- /dev/null
+++ b/lib/bio/fd.myr
@@ -0,0 +1,60 @@
+use std
+
+use "bio"
+use "types"
+
+pkg bio =
+ const mkfile : (fd : std.fd, mode : mode -> file#)
+ const open : (path : byte[:], mode : mode -> std.result(file#, byte[:]))
+ const dial : (srv : byte[:], mode : mode -> std.result(file#, byte[:]))
+ const create : (path : byte[:], mode : mode, perm : int -> std.result(file#, byte[:]))
+;;
+
+const mkfile = {fd, mode
+ -> mk(mode, [
+ .read = {buf; -> std.read(fd, buf)},
+ .write = {buf; -> std.write(fd, buf)},
+ .seek = {off; -> std.seek(fd, off, std.Seekset)},
+ .close = {; -> std.close(fd) >= 0},
+ ])
+}
+
+/* Opens a file with mode provided. */
+const open = {path, mode
+ -> sysopen(path, mode, sysmode(mode), 0o777)
+}
+
+/*
+ Creates a file for the provided path, with opened in
+ the requested mode, with the requested permissions
+*/
+const create = {path, mode, perm
+ -> sysopen(path, mode, sysmode(mode) | std.Ocreat | std.Otrunc, perm)
+}
+
+/* dial the server, and open a file using the returned fd */
+const dial = {srv, mode
+ match std.dial(srv)
+ | `std.Ok sock: -> `std.Ok mkfile(sock, mode)
+ | `std.Err m: -> `std.Err m
+ ;;
+}
+
+/* map from the bio modes to the unix open modes */
+const sysmode = {mode
+ match mode
+ | Rd: -> std.Oread
+ | Wr: -> std.Owrite
+ | Rw: -> std.Ordwr
+ | _: std.fatal("bio: bad file mode")
+ ;;
+ -> 0
+}
+
+/* open the file, and return it */
+const sysopen = {path, mode, openmode, perm
+ match std.openmode(path, openmode, (perm : int64))
+ | `std.Ok fd: -> `std.Ok mkfile(fd, mode)
+ | `std.Err e: -> `std.Err "could not open fd"
+ ;;
+}
diff --git a/lib/bio/geti.myr b/lib/bio/geti.myr
index c7ae74e..ee9a631 100644
--- a/lib/bio/geti.myr
+++ b/lib/bio/geti.myr
@@ -1,6 +1,7 @@
use std
use "bio"
+use "types"
pkg bio =
/* unsigned big endian */
diff --git a/lib/bio/iter.myr b/lib/bio/iter.myr
index 55379ed..923a15a 100644
--- a/lib/bio/iter.myr
+++ b/lib/bio/iter.myr
@@ -1,5 +1,6 @@
use std
use "bio"
+use "types"
pkg bio =
type lineiter = file#
diff --git a/lib/bio/mem.myr b/lib/bio/mem.myr
index bebae73..58a875c 100644
--- a/lib/bio/mem.myr
+++ b/lib/bio/mem.myr
@@ -1,5 +1,7 @@
use std
+
use "bio"
+use "types"
pkg bio =
const mkmem : (buf : byte[:] -> file#)
diff --git a/lib/bio/puti.myr b/lib/bio/puti.myr
index 056f6a4..2f07414 100644
--- a/lib/bio/puti.myr
+++ b/lib/bio/puti.myr
@@ -1,6 +1,7 @@
use std
use "bio"
+use "types"
pkg bio =
/* unsigned big endian */
diff --git a/lib/bio/types.myr b/lib/bio/types.myr
new file mode 100644
index 0000000..7b99d66
--- /dev/null
+++ b/lib/bio/types.myr
@@ -0,0 +1,42 @@
+use std
+
+pkg bio =
+ type mode = int
+ const Rd : mode = 1
+ const Wr : mode = 2
+ const Rw : mode = 1 | 2
+
+ type file = struct
+ /* backing io ops */
+ mode : mode
+ lasterr : std.errno
+
+ read : (buf : byte[:] -> std.result(std.size, std.errno))
+ write : (buf : byte[:] -> std.result(std.size, std.errno))
+ seek : (idx : std.off -> std.result(std.off, std.errno))
+ close : (-> bool)
+
+ /* read buffer */
+ rbuf : byte[:]
+ rstart : std.size
+ rend : std.size
+
+ /* write buffer */
+ wbuf : byte[:]
+ wend : std.size
+ ;;
+
+ type vtab = struct
+ read : (buf : byte[:] -> std.result(std.size, std.errno))
+ write : (buf : byte[:] -> std.result(std.size, std.errno))
+ seek : (idx : std.off -> std.result(std.off, std.errno))
+ close : (-> bool)
+ ;;
+
+ type err = union
+ `Eof
+ `Eio
+ `Ebadf
+ ;;
+;;
+