summaryrefslogtreecommitdiff
path: root/src/main/smp/sys/io.smp
blob: b94e89fab4c5762d36471d677f5d94ea985af563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
};