diff --git a/source/ilo-safer.c b/source/ilo-safer.c index 97bc5a2..fc06be7 100644 --- a/source/ilo-safer.c +++ b/source/ilo-safer.c @@ -6,10 +6,12 @@ |_|_|\___/ ilo.c (c) charles childers **************************************************************/ -#include #include -#include #include +#include +#include +#include +#include #define T ds[sp] /* Top of Data Stack */ #define N ds[sp-1] /* Next on Data Stack */ @@ -34,6 +36,17 @@ C *blocks, /* name of block file (ilo.blocks) */ I a, b, f, s, d, l; C i[1]; +struct termios ot, nt; + +V it() { + tcgetattr(STDIN_FILENO, &ot); + nt = ot; + nt.c_lflag &=(~ICANON & ~ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &nt); +} +V rt() { tcsetattr(STDIN_FILENO, TCSANOW, &ot); } + + V p(char *s) { while(*s) putchar(*s++); } V e(char *s) { p("E:"); p(s); p("\n"); exit(1); } V dso() { if ((sp + 1) > 32) { e("DSO"); } } @@ -46,13 +59,14 @@ V uli() { if (f == -1) { e("ULI"); } } V usi() { if (f == -1) { e("URI"); } } V urb() { if (f == -1) { e("URB"); } } V uwb() { if (f == -1) { e("UWB"); } } + V push(I v) { dso(); ds[sp + 1] = v; sp += 1; } I pop() { dsu(); sp -= 1; return ds[sp + 1]; } V load_image() { f = open(rom, O_RDONLY, 0666); uli(); - read(f, &m, 65536 * 4); + if (read(f, &m, 65536 * 4) < 0) { uli(); } close(f); ip = sp = rp = 0; } @@ -60,21 +74,21 @@ V load_image() { V save_image() { f = open(rom, O_WRONLY, 0666); usi(); - write(f, &m, 65536 * 4); + if (write(f, &m, 65536 * 4) < 0) { usi(); } close(f); } V block_common() { b = pop(); /* block buffer */ a = pop(); /* block number */ - lseek(f, 4096 * a, SEEK_SET); + if (lseek(f, 4096 * a, SEEK_SET) < 0) { e("LSEEK"); }; } V read_block() { f = open(blocks, O_RDONLY, 0666); urb(); block_common(); - read(f, m + b, 4096); + if (read(f, m + b, 4096) < 0) { urb(); } close(f); } @@ -82,12 +96,16 @@ V write_block() { f = open(blocks, O_WRONLY, 0666); uwb(); block_common(); - write(f, m + b, 4096); + if (write(f, m + b, 4096) < 0) { uwb(); } close(f); } V save_ip() { rso(); rp += 1; R = ip; } V symmetric() { if (b >= 0 && N < 0) { T += 1; N -= b; } } +V emit() { if (write(1, &i, 1) < 0) { exit(1); }; } +V bs() { i[0] = '\b'; emit(); + i[0] = ' '; emit(); + i[0] = '\b'; emit(); } V li() { ip += 1; push(m[ip]); } V du() { push(T); } @@ -116,13 +134,19 @@ V or() { N = T | N; sp -= 1; } V xo() { N = T ^ N; sp -= 1; } V sl() { N = N << T; sp -= 1; } V sr() { N = N >> T; sp -= 1; } -V cp() { l = pop(); d = pop(); s = T; T = -1; - while (l) { if (m[d] != m[s]) { T = 0; } - l -= 1; s += 1; d += 1; } } -V cy() { l = pop(); d = pop(); s = pop(); - while (l) { m[d] = m[s]; l -= 1; s += 1; d += 1; } } -V ioa() { i[0] = (char)pop(); write(1, &i, 1); } -V iob() { read(0, &i, 1); push(i[0]); } +V cp() { size_t l = pop() * 4; + void *d = m + pop(); + void *s = m + pop(); + push((memcmp(s, d, l) == 0) ? -1 : 0); +} +V cy() { size_t l = pop() * 4; + void *d = m + pop(); + void *s = m + pop(); + memmove(d, s, l); +} +V ioa() { i[0] = (char)pop(); emit(); } +V iob() { if (read(0, &i, 1) < 0) { rt(); exit(1); }; push(i[0]); + (i[0] == 8 || i[0] == 127) ? bs() : emit(); } V ioc() { read_block(); } V iod() { write_block(); } V ioe() { save_image(); } @@ -183,7 +207,8 @@ V add_restrictions() { #ifdef __OpenBSD__ unveil(blocks, "rw"); unveil(rom, "rw"); - pledge("stdio rpath wpath", NULL); + unveil(NULL, NULL); + pledge("stdio rpath wpath tty", NULL); #endif } @@ -191,7 +216,6 @@ I main(I argc, C **argv) { blocks = (argc > 1) ? argv[1] : "ilo.blocks"; rom = (argc > 2) ? argv[2] : "ilo.rom"; add_restrictions(); - load_image(); - execute(); + it(); load_image(); execute(); rt(); return 0; }