shithub: mc

Download patch

ref: 5f5b3c226cdea602baa23cf346ec2f3f367d81ee
parent: 3036bad34f204e9348610ef5194ba7b29933aaf7
parent: 20bb032dc16ab0a0a20e9a5705738c797ac4b3da
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Apr 12 21:26:24 EDT 2015

Merge ../libbio

--- /dev/null
+++ b/libbio/Makefile
@@ -1,0 +1,11 @@
+MYRLIB=bio
+MYRSRC= \
+	bio.myr \
+	puti.myr \
+	geti.myr
+
+include config.mk
+include mk/myr.mk
+
+check: all
+	make -C test check
--- /dev/null
+++ b/libbio/bio.myr
@@ -1,0 +1,500 @@
+use std
+
+pkg bio =
+	type mode = int
+	const Rd	: mode = 1
+	const Wr	: mode = 2
+	const Rw	: mode = 1 | 2
+
+	type file = struct
+		/* backing fd */
+		fd	: std.fd
+		mode	: mode
+
+		/* read buffer */
+		rbuf	: byte[:]
+		rstart	: std.size
+		rend	: std.size
+
+		/* write buffer */
+		wbuf	: byte[:]
+		wend	: std.size
+	;;
+
+	/* creation */
+	const mkfile	: (fd : std.fd, mode : mode	-> file#)
+	const open	: (path : byte[:], mode : mode	-> std.option(file#))
+	const dial	: (srv	: byte[:], mode : mode	-> std.option(file#))
+	const create	: (path : byte[:], mode : mode, perm : int	-> std.option(file#))
+	const close	: (f : file# -> bool)
+	const free	: (f : file# -> void)
+
+	/* basic i/o. Returns sub-buffer when applicable. */
+	const write	: (f : file#, src : byte[:]	-> std.size)
+	const read	: (f : file#, dst : byte[:]	-> std.option(byte[:]))
+	const flush	: (f : file# -> bool)
+
+	/* seeking */
+
+	/* single unit operations */
+	const putb	: (f : file#, b : byte	-> std.size)
+	const putc	: (f : file#, c : char	-> std.size)
+	const getb	: (f : file# -> std.option(byte))
+	const getc	: (f : file# -> std.option(char))
+
+	/* peeking */
+	const peekb	: (f : file# -> std.option(byte))
+	const peekc	: (f : file# -> std.option(char))
+
+	/* delimited read; returns freshly allocated buffer. */
+	const readln	: (f : file#	-> std.option(byte[:]))
+	const readto	: (f : file#, delim : byte[:]	-> std.option(byte[:]))
+	const skipto	: (f : file#, delim : byte[:]	-> bool)
+
+	/* formatted i/o */
+	const put	: (f : file#, fmt : byte[:], args : ... -> std.size)
+
+	/* pkg funcs */
+	pkglocal const ensureread	: (f : file#, n : std.size -> bool)
+	pkglocal const ensurewrite	: (f : file#, n : std.size -> bool)
+;;
+
+const Bufsz = 16*std.KiB
+const Small = 512
+
+/* Creates a file from an fd, opened in the given mode. */
+const mkfile = {fd, mode
+	var f
+
+	f = std.alloc()
+
+	f.fd = fd
+	f.mode = mode
+	if mode & Rd != 0
+		f.rbuf = std.slalloc(Bufsz)
+		f.rstart = 0
+		f.rend = 0
+	;;
+	if mode & Wr != 0
+		f.wbuf = std.slalloc(Bufsz)
+		f.wend = 0
+	;;
+	-> 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, perm)
+}
+
+/* dial the server, and open a file using the returned fd */
+const dial = {srv, mode
+	match std.dial(srv)
+	| `std.Ok sock:	-> `std.Some mkfile(sock, mode)
+	| `std.Fail _:	-> `std.None
+	;;
+}
+
+/* map from the bio modes to the unix open modes */
+const sysmode = {mode
+	match mode
+	| Rd:	-> std.Ordonly
+	| Wr:	-> std.Owronly
+	| Rw:	-> std.Ordwr
+	| _:	std.fatal(1, "bio: bad file mode")
+	;;
+	-> 0
+}
+
+/* open the file, and return it */
+const sysopen = {path, mode, openmode, perm
+	var fd
+
+	fd = std.openmode(path, openmode, perm castto(int64))
+	if fd < 0
+		-> `std.None
+	else
+		-> `std.Some mkfile(fd, mode)
+	;;
+}
+
+/* closes a file, flushing it to the output fd */
+const close = {f
+	var fd
+
+	fd = f.fd
+	free(f)
+	-> std.close(fd) == 0
+}
+
+const free = {f
+	flush(f)
+	if f.mode & Rd != 0
+		std.slfree(f.rbuf)
+	;;
+
+	if f.mode & Wr != 0
+		std.slfree(f.wbuf)
+	;;
+	std.free(f)
+}
+
+/* 
+writes to as much from `src` as possible to a file,
+returning the number of bytes written.
+*/
+const write = {f, src
+	std.assert(f.mode & Wr != 0, "File is not in write mode")
+	/*
+	Tack small writes onto the buffer end. Big ones
+	flush the buffer and then go right to kernel.
+	*/
+	if src.len < (f.wbuf.len - f.wend)
+		std.slcp(f.wbuf[f.wend:f.wend+src.len], src)
+		f.wend += src.len
+		-> src.len
+	else
+		flush(f)
+		-> writebuf(f.fd, src)
+	;;
+}
+
+/*
+reads as much into 'dst' as possible, up to the size of 'dst',
+returning the number of bytes read.
+*/
+const read = {f, dst
+	var n
+	var d
+	var count
+
+	std.assert(f.mode & Rd != 0, "File is not in read mode")
+	/* 
+	 * small reads should try to fill, so we don't have to make a
+	 * syscall for every read
+	 */
+	if dst.len < Small
+		fill(f, f.rbuf.len - f.rend)
+	;;
+	/* Read as much as we can from the buffer */
+	count = std.min(dst.len, f.rend - f.rstart)
+	std.slcp(dst[:count], f.rbuf[f.rstart:f.rstart+count])
+	f.rstart += count
+
+	/* if we drained the buffer, reset it */
+	if f.rstart == f.rend
+		f.rstart = 0
+		f.rend = 0
+	;;
+
+	/* Read the rest directly from the fd */
+	d = dst[count:]
+	while dst.len > 0
+		n = std.read(f.fd, d)
+		if n <= 0
+			goto readdone
+		;;
+		count += n
+		d = d[n:]
+	;;
+:readdone
+	if count > 0
+		-> `std.Some dst[:count]
+	else
+		-> `std.None
+	;;
+}
+
+/* flushes f out to the backing fd */
+const flush = {f
+	var ret
+
+	ret = true
+	if f.mode & Wr != 0
+		ret = (writebuf(f.fd, f.wbuf[:f.wend]) == f.wend)
+		f.wend = 0
+	;;
+	-> ret
+}
+
+/* writes a single byte to the output stream */
+const putb = {f, b
+	ensurewrite(f, 1)
+	f.wbuf[f.wend++] = b
+	-> 1
+}
+
+/* writes a single character to the output stream, encoded in utf8 */
+const putc = {f, c
+	var sz
+	
+	sz = std.charlen(c)
+	ensurewrite(f, sz)
+	std.encode(f.wbuf[f.wend:], c)
+	f.wend += sz
+	-> sz
+}
+
+/* reads a single byte from the input stream */
+const getb = {f
+	if ensureread(f, 1)
+		-> `std.Some f.rbuf[f.rstart++]
+	;;
+	-> `std.None
+}
+
+/* reads a single character from the input stream, encoded in utf8 */
+const getc = {f
+	var c
+
+	if ensurecodepoint(f)
+		c = std.decode(f.rbuf[f.rstart:f.rend])
+		f.rstart += std.charlen(c)
+		-> `std.Some c
+	;;
+	-> `std.None
+}
+
+/* ensures we have enough to read a single codepoint in the buffer */
+const ensurecodepoint = {f
+	var b
+	var len
+
+	if !ensureread(f, 1)
+		-> false
+	;;
+	b = f.rbuf[f.rstart]
+	if b & 0x80 == 0	/* 0b0xxx_xxxx */
+		len = 1
+	elif b & 0xe0 == 0xc0	/* 0b110x_xxxx */
+		len = 2
+	elif b & 0xf0 == 0xe0 	/* 0b1110_xxxx */
+		len = 3
+	elif b & 0xf8 == 0xf0 	/* 0b1111_0xxx */
+		len = 4
+	else
+		len = 1		/* invalid unicode char */
+	;;
+	-> ensureread(f, len)
+}
+
+/*
+  writes a single integer-like value to the output stream, in
+  little endian format
+*/
+generic putle = {f, v : @a::(numeric,integral)
+	var i
+
+	for i = 0; i < sizeof(@a); i++
+		putb(f, (v & 0xff) castto(byte))
+		v >>= 8
+	;;
+	-> sizeof(@a)
+}
+
+/*
+  writes a single integer-like value to the output stream, in
+  big endian format
+*/
+generic putbe = {f, v : @a::(numeric,integral)
+	var i
+
+	for i = sizeof(@a); i != 0; i--
+		putb(f, ((v >> ((i-1)*8)) & 0xff) castto(byte))
+	;;
+	-> sizeof(@a)
+}
+
+
+/* peeks a single byte from an input stream */
+const peekb = {f
+	if !ensureread(f, 1)
+		-> `std.None
+	else
+		-> `std.Some f.rbuf[f.rstart]
+	;;
+}
+
+/* peeks a single character from a utf8 encoded input stream */
+const peekc = {f
+	if !ensurecodepoint(f)
+		-> `std.None
+	else
+		-> `std.Some std.decode(f.rbuf[f.rstart:f.rend])
+	;;
+}
+
+/*
+  reads up to a single character delimiter. drops the delimiter
+  from the input stream. EOF always counts as a delimiter.
+  
+  Eg, with the input "foo,bar\n"
+
+  	bio.readto(f, ',')	-> "foo"
+  	bio.readto(f, ',')	-> "bar\n"
+*/
+const readto = {f, delim
+	-> readdelim(f, delim, false)
+}
+
+/* same as readto, but drops the read data. */
+const skipto = {f, delim
+	match readdelim(f, delim, true)
+	| `std.Some ret:	-> true
+	| `std.None:	-> false
+	;;
+}
+
+/* Same as readto, but the delimiter is always a '\n' */
+const readln = {f
+	-> readto(f, "\n")
+}
+
+const readdelim = {f, delim, drop
+	var ret
+	var i, j
+
+	ret = [][:]
+	while true
+		if !ensureread(f, delim.len)
+			if !drop
+				ret = readinto(f, ret, f.rend - f.rstart)
+			;;
+			if ret.len > 0
+				-> `std.Some ret
+			else
+				-> `std.None
+			;;
+		;;
+		for i = f.rstart; i < f.rend; i++
+			if f.rbuf[i] == delim[0]
+				for j = 0; j < delim.len; j++
+					if f.rbuf[i + j] != delim[j]
+						goto nextiterread
+					;;
+				;;
+				if !drop
+					ret = readinto(f, ret, i - f.rstart)
+				;;
+				f.rstart += delim.len
+				-> `std.Some ret
+			;;
+:nextiterread
+		;;
+		if !drop
+			ret = readinto(f, ret, f.rend - f.rstart)
+		;;
+	;;
+	std.die("unreachable")
+}
+
+/*
+Same as std.put, but buffered. Returns the number of bytes written.
+
+FIXME: depends on std.fmt() having a flush buffer API. Until then,
+we're stuck with a small static buffer.
+*/
+const put = {f, fmt, args
+	var buf : byte[2048]
+	var sl
+
+	sl = std.bfmt(buf[:], fmt, std.vastart(&args))
+	-> write(f, sl)
+}
+
+/* 
+reads n bytes from the read buffer onto the heap-allocated slice
+provided.
+*/
+const readinto = {f, buf, n
+	var ret
+
+	std.assert(f.rstart + n <= f.rend, "Reading too much from buffer")
+	ret = std.sljoin(buf, f.rbuf[f.rstart:f.rstart + n])
+	f.rstart += n
+	-> ret
+}
+
+/* makes sure we can bufferedly write at least n bytes */
+const ensurewrite = {f, n
+	std.assert(n < f.wbuf.len, "ensured write capacity > buffer size")
+	if n > f.wbuf.len - f.wend
+		-> flush(f)
+	;;
+	-> true
+}
+
+/*
+makes sure we have at least n bytes buffered. returns true if we succeed
+in buffering n bytes, false if we fail.
+*/
+const ensureread = {f, n
+	var held
+	var cap
+
+	std.assert(n < f.rbuf.len, "ensured read capacity > buffer size")
+	held = f.rend - f.rstart
+	if n > held
+		/* if we need to shift the slice down to the start, do it */
+		cap = f.rend - f.rstart
+		if n > (cap + held)
+			std.slcp(f.rbuf[:cap], f.rbuf[f.rstart:f.rend])
+			f.rstart = 0
+			f.rend = cap
+		;;
+		-> fill(f, n) > n
+	else
+		-> true
+	;;
+}
+
+/* blats a buffer to an fd */
+const writebuf = {fd, src
+	var n
+	var count
+
+	count = 0
+	while src.len != 0
+		n = std.write(fd, src)
+		if n <= 0
+			goto writedone
+		;;
+		count += n
+		src = src[n:]
+	;;
+:writedone
+	-> count
+}
+
+
+
+/*
+Reads as many bytes as possible from the file into
+the read buffer.
+*/
+const fill = {f, min
+	var n
+	var count
+
+	count = 0
+	while count < min
+		n = std.read(f.fd, f.rbuf[f.rend:])
+		if n <= 0
+			goto filldone
+		;;
+		count += n
+		f.rend += n
+	;;
+:filldone
+	-> count
+}
+
+
--- /dev/null
+++ b/libbio/bldfile
@@ -1,0 +1,5 @@
+lib bio = 
+	bio.myr
+	geti.myr
+	puti.myr
+;;
--- /dev/null
+++ b/libbio/configure
@@ -1,0 +1,52 @@
+#!/bin/sh
+
+prefix="/usr/local"
+
+for i in `seq 300`; do
+    echo "Lots of output to emulate automake... ok"
+    echo "Testing for things you'll never use... fail"
+    echo "Satisfying the fortran77 lobby... ok"
+    echo "Burning CPU time checking for the bloody obvious... ok"
+done
+echo "Automake emulated successfully"
+
+INST_ROOT='/usr/local'
+
+for arg in $*; do
+    shift 1
+    case $arg in
+        "--prefix" | "-p")
+            prefix=shift $*
+            ;;
+        --prefix=*)
+            prefix=`echo $arg | sed 's/^--prefix=//g'`
+            ;;
+        "--help" | "-h")
+            echo "Usage:"
+            echo "      --prefix | -p: The prefix to install to"
+            break;
+            ;;
+        *) echo "Unrecognized argument $arg";;
+    esac
+done
+
+OS=`uname`
+
+echo export INST_ROOT=$prefix > config.mk
+case $OS in
+    *Linux*)
+        echo 'export SYS=linux' >> config.mk
+        ;;
+    *Darwin*)
+        echo 'export SYS=osx' >> config.mk
+        ;;
+    *)
+        echo 'Unknown architecture.'
+        ;;
+esac
+
+cat << EOF
+    Building with:
+        prefix=$prefix
+EOF
+
--- /dev/null
+++ b/libbio/geti.myr
@@ -1,0 +1,63 @@
+use std
+
+use "bio.use"
+
+pkg bio =
+	/* unsigned big endian */
+	generic getbe8	: (f : file# -> std.option(@a::(numeric,integral)))
+	generic getbe16	: (f : file# -> std.option(@a::(numeric,integral)))
+	generic getbe32	: (f : file# -> std.option(@a::(numeric,integral)))
+	generic getbe64	: (f : file# -> std.option(@a::(numeric,integral)))
+
+	/* signed big endian */
+	generic getle8	: (f : file# -> std.option(@a::(numeric,integral)))
+	generic getle16	: (f : file# -> std.option(@a::(numeric,integral)))
+	generic getle32	: (f : file# -> std.option(@a::(numeric,integral)))
+	generic getle64	: (f : file# -> std.option(@a::(numeric,integral)))
+;;
+
+/*
+  reads a single integer-like value to the output stream, in
+  little endian format
+*/
+generic getle = {f, n -> std.option(@a::(numeric,integral))
+	var v, i
+
+	v = 0
+	if !ensureread(f, n)
+		-> `std.None
+	;;
+	for i = 0; i < n; i++
+		v |= (f.rbuf[f.rstart++] castto(uint64)) << (8*(i castto(uint64)))
+	;;
+	-> `std.Some v castto(@a::(numeric,integral))
+}
+
+/*
+  reads a single integer-like value to the output stream, in
+  big endian format
+*/
+generic getbe = {f, n -> std.option(@a::(numeric,integral))
+	var v, i
+
+	v = 0
+	if !ensureread(f,n)
+		-> `std.None
+	;;
+	for i = 0; i < n; i++
+		v <<= 8
+		v |= (f.rbuf[f.rstart++] castto(uint64))
+	;;
+	-> `std.Some v castto(@a::(numeric,integral))
+}
+
+generic getbe8  = {f; -> getbe(f, 1)}
+generic getbe16 = {f; -> getbe(f, 2)}
+generic getbe32 = {f; -> getbe(f, 4)}
+generic getbe64 = {f; -> getbe(f, 8)}
+
+generic getle8  = {f; -> getle(f, 1)}
+generic getle16 = {f; -> getle(f, 2)}
+generic getle32 = {f; -> getle(f, 4)}
+generic getle64 = {f; -> getle(f, 8)}
+
--- /dev/null
+++ b/libbio/mk/myr.mk
@@ -1,0 +1,97 @@
+ifneq ($(MYRLIB),)
+    _LIBNAME=lib$(MYRLIB).a
+endif
+
+all: subdirs $(_LIBNAME) $(MYRBIN) 
+
+subdirs:
+	@for i in $(SUB); do (\
+	    cd $$i && \
+	    $(MAKE) || \
+	    exit 1 \
+	) || exit 1; done
+
+subdirs-clean:
+	@for i in $(SUB); do (\
+	    cd $$i && \
+	    $(MAKE) clean|| \
+	    exit 1 \
+	); done
+
+subdirs-install:
+	@for i in $(SUB); do (\
+	    cd $$i && \
+	    $(MAKE) install|| \
+	    exit 1 \
+	); done
+
+subdirs-uninstall:
+	@for i in $(SUB); do (\
+	    cd $$i && \
+	    $(MAKE) uninstall|| \
+	    exit 1 \
+	); done
+
+$(_LIBNAME): $(MYRSRC) $(ASMSRC)
+	myrbuild -l $(MYRLIB) $^
+
+$(MYRBIN): $(MYRSRC) $(ASMSRC)
+	myrbuild -b $(MYRBIN) $^
+
+OBJ=$(MYRSRC:.myr=.o) $(ASMSRC:.s=.o)
+USE=$(MYRSRC:.myr=.use) $(MYRLIB)
+.PHONY: clean
+clean: subdirs-clean
+	rm -f $(OBJ)
+	rm -f $(USE)
+	@if [ ! -z "$(MYRLIB)" ]; then \
+	    echo rm -f $(MYRLIB); \
+	    rm -f $(MYRLIB); \
+	    echo rm -f lib$(MYRLIB).a; \
+	    rm -f lib$(MYRLIB).a; \
+	fi
+	@if [ ! -z "$(MYRBIN)" ]; then \
+	    echo rm -f $(MYRBIN); \
+	    rm -f $(MYRBIN); \
+	    echo rm -f lib$(MYRBIN).a; \
+	    rm -f lib$(MYRBIN).a; \
+	fi
+
+install: subdirs-install $(MYRBIN) $(_LIBNAME) $(MAN)
+	@if [ ! -z "$(MYRBIN)" ]; then \
+	    echo install $(MYRBIN) $(abspath $(DESTDIR)/$(INST_ROOT)/bin); \
+	    mkdir -p $(abspath $(DESTDIR)/$(INST_ROOT)/bin); \
+	    install $(MYRBIN) $(abspath $(DESTDIR)/$(INST_ROOT)/bin); \
+	fi
+	@if [ ! -z "$(_LIBNAME)" ]; then \
+		echo install -m 644 $(_LIBNAME) $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr); \
+		echo install -m 644 $(MYRLIB) $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr); \
+		mkdir -p $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr); \
+		install -m 644 $(_LIBNAME) $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr); \
+		install -m 644 $(MYRLIB) $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr); \
+	fi
+	@for i in $(MAN); do \
+	    MANSECT=$$(echo $$i | awk -F. '{print $$NF}'); \
+	    echo mkdir -p $(abspath $(DESTDIR)/$(INST_ROOT)/share/man/man$$MANSECT); \
+	    echo install -m 644 $(MAN) $(abspath $(DESTDIR)/$(INST_ROOT)/share/man/man$${MANSECT}); \
+	    mkdir -p $(abspath $(DESTDIR)/$(INST_ROOT)/share/man/man$$MANSECT); \
+	    install -m 644 $(MAN) $(abspath $(DESTDIR)/$(INST_ROOT)/share/man/man$${MANSECT}); \
+	done \
+
+uninstall: subdirs-uninstall
+	@for i in $(MYRBIN); do \
+	    echo rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/bin/$$i); \
+	    rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/bin/$$i); \
+	done
+	@for i in $(_LIBNAME) $(MYRLIB); do \
+	    echo rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr/$$i); \
+	    rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr/$$i); \
+	done
+	@for i in $(MAN); do \
+	    MANSECT=$$(echo $$i | awk -F. '{print $$NF}'); \
+	    echo rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/share/man/man$${MANSECT}/$$i); \
+	    rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/share/man/man$${MANSECT}/$$i); \
+	done
+
+config.mk:
+	./configure
--- /dev/null
+++ b/libbio/puti.myr
@@ -1,0 +1,62 @@
+use std
+
+use "bio.use"
+
+pkg bio =
+	/* unsigned big endian */
+	generic putbe8	: (f : file#, v : @a::(numeric,integral) -> std.size)
+	generic putbe16	: (f : file#, v : @a::(numeric,integral) -> std.size)
+	generic putbe32	: (f : file#, v : @a::(numeric,integral) -> std.size)
+	generic putbe64	: (f : file#, v : @a::(numeric,integral) -> std.size)
+
+	/* unsigned little endian */
+	generic putle8	: (f : file#, v : @a::(numeric,integral) -> std.size)
+	generic putle16	: (f : file#, v : @a::(numeric,integral) -> std.size)
+	generic putle32	: (f : file#, v : @a::(numeric,integral) -> std.size)
+	generic putle64	: (f : file#, v : @a::(numeric,integral) -> std.size)
+;;
+
+generic putbe8  = {f, v; -> putbe(f, v castto(uint64), 1)}
+generic putbe16 = {f, v; -> putbe(f, v castto(uint64), 2)}
+generic putbe32 = {f, v; -> putbe(f, v castto(uint64), 4)}
+generic putbe64 = {f, v; -> putbe(f, v castto(uint64), 8)}
+
+generic putle8  = {f, v; -> putle(f, v castto(uint64), 1)}
+generic putle16 = {f, v; -> putle(f, v castto(uint64), 2)}
+generic putle32 = {f, v; -> putle(f, v castto(uint64), 4)}
+generic putle64 = {f, v; -> putle(f, v castto(uint64), 8)}
+
+const putle = {f, v, n
+	var buf : byte[8]
+
+	if !ensurewrite(f, n)
+		-> 0
+	;;
+	buf[0] = (v >> 0) & 0xff castto(byte)
+	buf[1] = (v >> 8) & 0xff castto(byte)
+	buf[2] = (v >> 16) & 0xff castto(byte)
+	buf[3] = (v >> 24) & 0xff castto(byte)
+	buf[4] = (v >> 32) & 0xff castto(byte)
+	buf[5] = (v >> 40) & 0xff castto(byte)
+	buf[6] = (v >> 48) & 0xff castto(byte)
+	buf[7] = (v >> 56) & 0xff castto(byte)
+	write(f, buf[:n])
+}
+
+const putbe = {f, v, n
+	var buf : byte[8]
+
+	if !ensurewrite(f, n)
+		-> 0
+	;;
+	buf[0] = (v >> 56) & 0xff castto(byte)
+	buf[1] = (v >> 48) & 0xff castto(byte)
+	buf[2] = (v >> 40) & 0xff castto(byte)
+	buf[3] = (v >> 32) & 0xff castto(byte)
+	buf[4] = (v >> 24) & 0xff castto(byte)
+	buf[5] = (v >> 16) & 0xff castto(byte)
+	buf[6] = (v >> 8) & 0xff castto(byte)
+	buf[7] = (v >> 0) & 0xff castto(byte)
+	write(f, buf[8-n:])
+}
+
--- /dev/null
+++ b/libbio/test/Makefile
@@ -1,0 +1,20 @@
+# don't build anything for 'all'
+all: 
+	$(MAKE) -C ..
+
+check:
+	./runtest.sh
+
+.PHONY: %
+%:
+	./runtest.sh $@
+
+.PHONY: clean
+clean:
+	rm -f testmatch.use testmatch.o
+	@for i in `awk '/^[A-Z]/{print $$2}' tests`; do \
+	    echo rm -f $$i; \
+	    rm -f $$i; \
+	done
+
+install:
--- /dev/null
+++ b/libbio/test/bio-create.myr
@@ -1,0 +1,13 @@
+use std
+use bio
+
+const main = {
+	var f
+
+	std.mkdir("tmpout", 0o755);
+	match bio.create("tmpout/test-create", bio.Wr, 0o644)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Failed to open file\n")
+	;;
+	bio.close(f)
+}
--- /dev/null
+++ b/libbio/test/bio-delim.myr
@@ -1,0 +1,70 @@
+use std
+use bio
+
+const main = {
+	var f
+	var d
+
+	match bio.open("data/lines", bio.Rd)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file\n")
+	;;
+
+	/* read first line */
+	d = readln(f)
+	std.write(1, d)
+	std.write(1, "\n")
+	std.slfree(d)
+
+	/* read second line, should not include \n */
+	d = readln(f)
+	std.write(1, d)
+	std.write(1, "\n")
+	std.slfree(d)
+
+	/* read to ';' */
+	d = readto(f, ";")
+	std.write(1, d)
+	std.write(1, "\n")
+	std.slfree(d)
+
+	/* read to ';'  again */
+	d = readto(f, ";")
+	std.write(1, d)
+	std.write(1, "\n")
+	std.slfree(d)
+
+	/* '--'  this time */
+	d = readto(f, "--")
+	std.write(1, d)
+	std.write(1, "\n")
+	std.slfree(d)
+
+	/* and without the terminator, we should get the remaining text */
+	d = readto(f, "not-there")
+	std.write(1, d)
+	std.write(1, "\n")
+	std.slfree(d)
+
+	/* and now, eof */
+	d = readln(f)
+	d = readto(f, "actually, eof")
+	
+	bio.close(f)
+}
+
+const readln = {f
+	match bio.readln(f)
+	| `std.Some d:	-> d
+	| `std.None:	std.put("eof\n")
+			-> [][:]
+	;;
+}
+
+const readto = {f, delim
+	match bio.readto(f, delim)
+	| `std.Some d:	-> d
+	| `std.None:	std.put("eof\n")
+			-> [][:]
+	;;
+}
--- /dev/null
+++ b/libbio/test/bio-endianrd.myr
@@ -1,0 +1,57 @@
+use std
+use bio
+
+generic try = {opt : std.option(@a::(integral,numeric))-> @a::(integral,numeric)
+	match opt
+	| `std.Some val:	-> val
+	| `std.None:	std.fatal(1, "read failed")
+	;;
+}
+const main = {
+	var b : byte
+	var w : uint16
+	var l : uint32
+	var q : uint64
+	var f
+
+	/* use the expected write data as read data */
+	match bio.open("data/bio-endianwr-expected", bio.Rd)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file")
+	;;
+	
+	/* byte */
+	/*
+	/* FIXME: compiler bug. multiplication on byte
+	   values is currently broken. */
+	b = 0xaa
+	std.assert(try(bio.getle8(f)) == b, "le byte broken\n")
+	std.assert(try(bio.getbe8(f)) == b, "be byte broken\n")
+	*/
+
+	/* word */
+	w = 0xaabb
+	std.assert(try(bio.getle16(f)) == w, "le word broken\n")
+	std.assert(try(bio.getbe16(f)) == w, "be word broken\n")
+
+	/* long */
+	l = 0xaabbccdd
+	std.assert(try(bio.getle32(f)) == l, "le long broken\n")
+	std.assert(try(bio.getbe32(f)) == l, "be long broken\n")
+
+	/* quad */
+	q = 0x11223344aabbccdd castto(uint64)
+	std.assert(try(bio.getle64(f)) == q, "le quad broken\n")
+	std.assert(try(bio.getbe64(f)) == q, "be quad broken\n")
+
+	/* end of file */
+	match bio.getle64(f)
+	| `std.None:
+	| `std.Some v:	std.die("read past end of file\n")
+	;;
+
+	bio.close(f);
+
+	std.put("success: all reads matched\n")
+}
+
--- /dev/null
+++ b/libbio/test/bio-endianwr.myr
@@ -1,0 +1,42 @@
+use std
+use bio
+
+const main = {
+	var b : byte
+	var w : uint16
+	var l : uint32
+	var q : uint64
+	var f
+
+	match bio.create("tmpout/test-endianwr", bio.Wr, 0o644)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file")
+	;;
+	
+	/* byte */
+	/*
+	/* FIXME: compiler bug. multiplication on byte
+	   values is currently broken. */
+	b = 0xaa
+	bio.putle(f, b)
+	bio.putbe(f, b)
+	*/
+
+	/* word */
+	w = 0xaabb
+	bio.putle16(f, w)
+	bio.putbe16(f, w)
+
+	/* long */
+	l = 0xaabbccdd
+	bio.putle32(f, l)
+	bio.putbe32(f, l)
+
+	/* quad */
+	q = 0x11223344aabbccdd castto(uint64)
+	bio.putle64(f, q)
+	bio.putbe64(f, q)
+
+	/* and test for flush on close */
+	bio.close(f);
+}
--- /dev/null
+++ b/libbio/test/bio-peek.myr
@@ -1,0 +1,45 @@
+use std
+use bio
+
+const main = {
+	var f
+	/* Must be bigger than a bio buffer (ie, > 64k) */
+	var buf : byte[64*1024]
+
+	match bio.open("data/datafile", bio.Rd)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file")
+	;;
+	
+	std.assert(peekb(f) == 0x30, "wrong byte value read from datafile")
+	std.assert(peekc(f) == '0', "wrong char value read from datafile")
+
+	bio.read(f, buf[:4])	/* skip ahead 4 bytes */
+	std.assert(peekb(f) == 0x34, "wrong byte value read from datafile")
+	std.assert(peekc(f) == '4', "wrong char value read from datafile")
+
+	bio.read(f, buf[:])	/* skip ahead 64k */
+	std.assert(peekb(f) == 0x30, "wrong byte value read from datafile")
+	std.assert(peekc(f) == '0', "wrong char value read from datafile")
+
+	bio.close(f);
+	std.put("Succeded peeeking values\n")
+}
+
+const peekc = {f
+	match bio.peekc(f)
+	| `std.Some c:	-> c
+	| `std.None:
+		std.put("eof")
+		-> -1
+	;;
+}
+
+const peekb = {f
+	match bio.peekb(f)
+	| `std.Some b:	-> b
+	| `std.None:
+		std.put("eof")
+		-> -1
+	;;
+}
--- /dev/null
+++ b/libbio/test/bio-read.myr
@@ -1,0 +1,46 @@
+use std
+use bio
+
+const main = {
+	var f
+	/* Must be bigger than a bio buffer (ie, > 64k) */
+	var buf : byte[64*1024]
+	var b
+
+	match bio.open("data/datafile", bio.Rd)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file")
+	;;
+	
+	/* read a 4 byte chunk j*/
+	b = r(f, buf[:4])
+	std.write(1, b)
+	std.write(1, "\n")
+
+	/* read the next 32 bytes */
+	b = r(f, buf[:32])
+	std.write(1, b)
+	std.write(1, "\n")
+
+	/* read a 64k chunk */
+	b = r(f, buf[:])
+	std.write(1, b)
+	std.write(1, "\n")
+
+	/* read to EOF */
+	b = r(f, buf[:])
+	std.write(1, b)
+	std.write(1, "\n")
+
+	/* and fail */
+	b = r(f, buf[:])
+
+	bio.close(f)
+}
+
+const r = {f, buf
+	match bio.read(f, buf)
+	| `std.Some b:	-> b
+	| `std.None:	std.put("eof\n")
+	;;
+}
--- /dev/null
+++ b/libbio/test/bio-unitwr.myr
@@ -1,0 +1,14 @@
+use std
+use bio
+
+const main = {
+	var f
+	match bio.create("tmpout/test-unitwr", bio.Wr, 0o644)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file")
+	;;
+	bio.putb(f, 42)
+	bio.putc(f, 'ה')
+	bio.putb(f, 0xa)
+	bio.close(f);
+}
--- /dev/null
+++ b/libbio/test/bio-write.myr
@@ -1,0 +1,33 @@
+use std
+use bio
+
+const main = {
+	var i
+	var f
+	/* Must be bigger than a bio buffer (ie, > 64k) */
+	var buf : byte[64*1024]
+
+	match bio.create("tmpout/test-write", bio.Wr, 0o644)
+	| `std.Some bio:	f = bio
+	| `std.None:	std.fatal(1, "Unable to open data file")
+	;;
+
+	/* write a 5 byte chunk */
+	bio.write(f, "test\n")
+
+	/* again */
+	bio.write(f, "test\n")
+
+	/* write a 64k chunk */
+	for i = 0; i < 64*1024; i++
+		buf[i] = 0x31
+	;;
+	bio.write(f, buf[:])
+
+	/* final message after a big burst */
+	bio.write(f, "goodbye\n")
+	bio.flush(f)
+
+	/* and test for flush on close */
+	bio.close(f);
+}
--- /dev/null
+++ b/libbio/test/data/bio-delim-expected
@@ -1,0 +1,10 @@
+first line
+second line
+data with
+semicolons
+
+and
+no-terminator
+
+eof
+eof
--- /dev/null
+++ b/libbio/test/data/bio-endianrd-expected
@@ -1,0 +1,1 @@
+success: all reads matched
--- /dev/null
+++ b/libbio/test/data/bio-endianwr-expected
@@ -1,0 +1,1 @@
+�����̻������̻�D3""3D���
\ No newline at end of file
--- /dev/null
+++ b/libbio/test/data/bio-peek-expected
@@ -1,0 +1,1 @@
+Succeded peeeking values
--- /dev/null
+++ b/libbio/test/data/bio-read-expected
@@ -1,0 +1,6 @@
+0123
+45678901234567890123456789012345
+678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
\ No newline at end of file
+234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
\ No newline at end of file
+
+eof
--- /dev/null
+++ b/libbio/test/data/bio-unitwr-expected
@@ -1,0 +1,1 @@
+*ה
--- /dev/null
+++ b/libbio/test/data/bio-write-expected
@@ -1,0 +1,3 @@
+test
+test
+111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
\ No newline at end of file
--- /dev/null
+++ b/libbio/test/data/datafile
@@ -1,0 +1,1 @@
+012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
\ No newline at end of file
--- /dev/null
+++ b/libbio/test/data/lines
@@ -1,0 +1,4 @@
+first line
+second line
+data with;semicolons;
+and--no-terminator
--- /dev/null
+++ b/libbio/test/runtest.sh
@@ -1,0 +1,124 @@
+#!/bin/bash
+NFAILURES=0
+NPASSES=0
+
+function build {
+    rm -f $1 $1.o $1.s $1.use
+    myrbuild $FLAGS -b $1 $1.myr $EXTRA_SRC
+}
+
+function pass {
+    PASSED="$PASSED $1"
+    NPASSED=$[$NPASSED + 1]
+}
+
+function fail {
+    echo "FAIL: $1"
+    FAILED="$FAILED $1"
+    NFAILED=$[$NFAILED + 1]
+}
+
+function expectstatus {
+    ./$1 $3
+    if [ $? -eq $2 ]; then
+        pass $1
+        return
+    else
+        fail $1
+    fi
+}
+
+function expectprint {
+    if [ "`./$1 $3`" != "$2" ]; then
+        fail $1
+    else
+        pass $1
+    fi
+}
+
+
+function expectcompare {
+    if [ x"" !=  x"$TMPDIR" ]; then 
+        t=$TMPDIR/myrtest-$1-$RANDOM
+    else
+        t=/tmp/myrtest-$1-$RANDOM
+    fi
+    ./$1 $3 > $t
+    if cmp $t data/$1-expected; then
+        pass $1
+    else
+        fail $1
+    fi
+    rm -f $t
+}
+
+function expectfcompare {
+    ./$1 $3
+    if cmp data/$1-expected $2; then
+        pass $1
+    else
+        fail $1
+    fi
+}
+
+function shouldskip {
+  if [ -z $ARGS ]; then
+      return 1
+  fi
+
+  for i in $ARGS; do
+      if [ $i = $1 ]; then
+          return 1
+      fi
+  done
+  return 0
+}
+
+
+# Should build and run
+function B {
+    if shouldskip $1; then
+        return
+    fi
+
+    test="$1"; shift
+    type="$1"; shift
+    res="$1"; shift
+    if [ $# > 0 ]; then
+        args="$1"; shift
+    fi
+    build $test
+    case $type in
+    "E")  expectstatus "$test" "$res" "$input";;
+    "P")  expectprint "$test" "$res" "$input";;
+    "C")  expectcompare "$test" "$res" "$input";;
+    "F")  expectfcompare "$test" "$res" "$args";;
+    esac
+}
+
+# Should fail
+function F {
+    if shouldskip $1; then
+        return
+    fi
+    (build $1) > /dev/null
+    if [ $? -eq '1' ]; then
+        pass $1
+    else
+        fail $1
+    fi
+}
+
+# Should generate a usefile
+function U {
+    return
+}
+
+source tests
+
+echo "PASSED ($NPASSED): $PASSED"
+if [ -z "$NFAILED" ]; then
+    echo "SUCCESS"
+else
+    echo "FAILURES ($NFAILED): $FAILED"
+fi
--- /dev/null
+++ b/libbio/test/tests
@@ -1,0 +1,31 @@
+FLAGS=-I../
+mkdir -p tmpout
+# Format:
+# [B|F] testname [E|P] result
+#    [B|F]: Compiler outcome.
+#	B: Expect that this test will build.
+#	F: Expect that this test will not build.
+#    testname: Test case
+#	The test that will run. We will try to
+#	compile 'testname.myr' to 'testname',
+#	and then execute it, verifying the result
+#    [E|P|C]: Result type
+#	E tells us that the result is an exit status
+#	P tells us that the result is on stdout,
+#         and should be compared to the value on the
+#         line
+#	C tells us that the result is on stdout,
+#         and should be compared to the contents of
+#         the file passed on the line.
+#    result: Result value
+#	What we compare with. This should be self-
+#	evident.
+B bio-create    F	tmpout/test-create
+B bio-read	C
+B bio-write	F	tmpout/test-write
+B bio-delim	C
+B bio-endianwr	F	tmpout/test-endianwr
+B bio-endianrd	C
+B bio-unitwr	F	tmpout/test-unitwr
+B bio-peek	C
+#B bio-fmt	C