summaryrefslogtreecommitdiff
path: root/chapter25/src/main/smp/sys/io.smp
diff options
context:
space:
mode:
Diffstat (limited to 'chapter25/src/main/smp/sys/io.smp')
-rw-r--r--chapter25/src/main/smp/sys/io.smp32
1 files changed, 32 insertions, 0 deletions
diff --git a/chapter25/src/main/smp/sys/io.smp b/chapter25/src/main/smp/sys/io.smp
new file mode 100644
index 00000000..b94e89fa
--- /dev/null
+++ b/chapter25/src/main/smp/sys/io.smp
@@ -0,0 +1,32 @@
+// -*- mode: java; -*-
+
+// Simple I/O Library
+
+// Write byte array to stdout
+val p = { u8[~] str ->
+ i64 ptr = str; // cast array base to i64
+ return libc.write(1,ptr,str#);
+};
+
+val p_noInline = { u8[~] str ->
+ i64 ptr = str; // cast array base to i64
+ return libc.write(1,ptr,str#);
+};
+
+// Return stdin as a u8[].
+val stdin = { ->
+ var buf = new u8[8*1024];
+ i64 ptr = buf;
+ int len = 0;
+ while( true ) {
+ int rez = libc.read(0,ptr,buf#);
+ if( rez < 0 ) libc._exit(libc.errno);
+ len += rez;
+ if( rez < buf# )
+ break;
+ libc._exit(-2); // Need to implement for more than 8K input
+ }
+ // Compress
+ var dst = new u8[len];
+ return Ary.copy(dst,0,buf,0,len);
+};