aboutsummaryrefslogtreecommitdiff
path: root/sys/libn
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-21 10:40:03 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-21 10:40:03 -0700
commit4fbf50ddfeeb0e9d99d271e31dd35d545fb42ab6 (patch)
tree2a9664a6c9d8aa57102bbe40a306fddf3d86ff2b /sys/libn
parent82885d35ea1b52982ebcf3a58e9d328cfa4e933a (diff)
feat: added wrapper for io
Diffstat (limited to 'sys/libn')
-rw-r--r--sys/libn/bufio.c33
-rw-r--r--sys/libn/coro.c16
-rw-r--r--sys/libn/io.c71
3 files changed, 112 insertions, 8 deletions
diff --git a/sys/libn/bufio.c b/sys/libn/bufio.c
new file mode 100644
index 0000000..6b15760
--- /dev/null
+++ b/sys/libn/bufio.c
@@ -0,0 +1,33 @@
+#include <u.h>
+#include <libn.h>
+
+enum
+{
+ BUF·size = 8 * 2048,
+ BUF·ungets = 8,
+
+ BUF·bad = -2,
+ BUF·eof = -1,
+
+ BUF·inactive = 0,
+ BUF·rdractive,
+ BUF·wtractive,
+
+ BUF·END,
+} bmode;
+
+typedef struct Buffer
+{
+ uint8 state;
+ vlong off;
+ vlong size;
+
+ byte *bbuf, *ebuf;
+ byte b[BUF·size + BUF·ungets];
+} Buffer;
+
+struct bufio·Stream
+{
+ Stream *s;
+ Buffer buf;
+};
diff --git a/sys/libn/coro.c b/sys/libn/coro.c
index 2e50164..2ad629b 100644
--- a/sys/libn/coro.c
+++ b/sys/libn/coro.c
@@ -4,8 +4,8 @@
// -----------------------------------------------------------------------
// Assembly routines
-extern void _newcoro(coro *co, uintptr (*func)(coro*, uintptr), void *stk);
-extern uintptr _coroyield(coro *co, uintptr arg);
+extern void _newcoro(Coro *co, uintptr (*func)(Coro*, uintptr), void *stk);
+extern uintptr _coroyield(Coro *co, uintptr arg);
// -----------------------------------------------------------------------
// Globals
@@ -16,7 +16,7 @@ extern uintptr _coroyield(coro *co, uintptr arg);
// C interface
/* Co-routine context */
-struct coro
+struct Coro
{
void* sp;
void* bp;
@@ -24,14 +24,14 @@ struct coro
void* user;
};
-coro*
-coro·new(uintptr stk, uintptr (*func)(coro*, uintptr))
+Coro*
+coro·new(uintptr stk, uintptr (*func)(Coro*, uintptr))
{
if (!func) return nil;
if (stk == 0) stk = 8192;
byte *block = malloc(stk);
- coro *co = (coro*)&block[stk - sizeof(coro)];
+ Coro *co = (Coro*)&block[stk - sizeof(Coro)];
co->bp = block;
co->size = stk;
@@ -40,7 +40,7 @@ coro·new(uintptr stk, uintptr (*func)(coro*, uintptr))
}
error
-coro·free(coro *co)
+coro·free(Coro *co)
{
enum
{
@@ -60,7 +60,7 @@ coro·free(coro *co)
}
uintptr
-coro·yield(coro *c, uintptr arg)
+coro·yield(Coro *c, uintptr arg)
{
return _coroyield(c, arg);
}
diff --git a/sys/libn/io.c b/sys/libn/io.c
new file mode 100644
index 0000000..789bc7b
--- /dev/null
+++ b/sys/libn/io.c
@@ -0,0 +1,71 @@
+#include <u.h>
+#include <libn.h>
+
+// -----------------------------------------------------------------------
+// Open/Close
+
+Stream*
+io·open(byte *name, byte *mode)
+{
+ return fopen(name, mode);
+}
+
+error
+io·close(Stream *s)
+{
+ return fclose(s);
+}
+
+// -----------------------------------------------------------------------
+// Reading
+
+byte
+io·getbyte(Stream *s)
+{
+ return fgetc(s);
+}
+
+vlong
+io·read(Stream *s, int sz, int n, void *buf)
+{
+ return fread(buf, sz, n, s);
+}
+
+int
+io·readln(Stream *s, int n, byte* buf)
+{
+ byte* b;
+ b = fgets(buf, n+1, s);
+
+ return strlen(buf);
+}
+
+// -----------------------------------------------------------------------
+// Writing
+
+error
+io·putbyte(Stream *s, byte c)
+{
+ return fputc(c, s);
+}
+
+int
+io·putstring(Stream *s, string str)
+{
+ return fputs(str, s);
+}
+
+vlong
+io·write(Stream *s, int sz, int n, void *buf)
+{
+ return fwrite(buf, sz, n, s);
+}
+
+// -----------------------------------------------------------------------
+// Seek
+
+int
+io·seek(Stream *s, long off, enum SeekPos origin)
+{
+ return fseek(s, off, origin);
+}