aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libn.h35
-rw-r--r--sys/libn/bufio.c33
-rw-r--r--sys/libn/coro.c16
-rw-r--r--sys/libn/io.c71
4 files changed, 142 insertions, 13 deletions
diff --git a/include/libn.h b/include/libn.h
index aad359e..64e2e70 100644
--- a/include/libn.h
+++ b/include/libn.h
@@ -11,9 +11,9 @@
#include <stdio.h>
#include <unistd.h>
+#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <fcntl.h>
// ----------------------------------------------------------------------------
// Dynamic array.
@@ -50,15 +50,16 @@ void _bufpop(void*, int, vlong);
// -----------------------------------------------------------------------------
// Co-routines
-typedef struct coro coro;
+typedef struct Coro Coro;
-coro* coro·new(uintptr stk, uintptr (*func)(coro*, uintptr));
-uintptr coro·yield(coro *c, uintptr arg);
-error coro·free(coro *c);
+Coro* coro·new(uintptr stk, uintptr (*func)(Coro*, uintptr));
+uintptr coro·yield(Coro *c, uintptr arg);
+error coro·free(Coro *c);
// -----------------------------------------------------------------------------
// Strings
+// TODO(nnoll): Move here?
#include ".include/str.h"
// -----------------------------------------------------------------------------
@@ -67,6 +68,30 @@ error coro·free(coro *c);
#include ".include/map.h"
// -----------------------------------------------------------------------------
+// I/O
+
+typedef FILE Stream;
+enum SeekPos
+{
+ seek·CUR = SEEK_CUR,
+ seek·SET = SEEK_SET,
+ seek·END = SEEK_END
+};
+
+Stream *io·open(byte *name, byte *mode);
+error io·close(Stream *s);
+byte io·getbyte(Stream *s);
+vlong io·read(Stream *s, int sz, int n, void *buf);
+int io·readln(Stream *s, int n, byte* buf);
+error io·putbyte(Stream *s, byte c);
+int io·putstring(Stream *s, string str);
+vlong io·write(Stream *s, int sz, int n, void *buf);
+int io·seek(Stream *s, long off, enum SeekPos origin);
+
+// -----------------------------------------------------------------------------
+// Buffered I/O
+
+// -----------------------------------------------------------------------------
// Error handling functions.
void errorf(const byte* fmt, ...);
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);
+}