import "io" import "heap" import "string" import "keyboard" import "stringstream" manifest { io_putback = 0, io_thestring = 1, io_length = 2, io_position = 3, io_blocknum = 4, io_readch = 5, io_unreadch = 6, io_writech = 7, // new 1 io_close = 8, // new 1 sizeof_io = 9 } let illegal_operation(v) be // new 1 { out("\nillegal operation performed\n"); finish } let kbd_int_readch(v) be { let answer; test v ! io_putback = 0 then answer := inch() else answer := v ! io_putback; v ! io_putback := 0; resultis answer } let kbd_int_unreadch(v, c) be { v ! io_putback := c } let kbd_int_writech(v, c) be // new 1 { outch(c) } let str_int_readch(v) be { let answer; if v ! io_putback <> 0 then { answer := v ! io_putback; v ! io_putback := 0; resultis answer } if v ! io_position >= v ! io_length then resultis '\n'; answer := string_getchar(v ! io_thestring, v ! io_position); v ! io_position +:= 1; resultis answer } let str_int_unreadch(v, c) be { v ! io_putback := c } let donothing_close(v) be // new 1 { freevec(v); resultis true } let ofile_int_writech(v, c) be // new 1 { if v ! io_position >= 512 then resultis false; byte v ! io_position of v ! io_thestring := c; v ! io_position +:= 1; resultis true } let ofile_int_close(v) be // new 1 { let r = devctl(dc$discwrite, 1, v ! io_blocknum, v ! io_thestring); freevec(v ! io_thestring); freevec(v); if r < 0 then resultis false; resultis true } let ifile_int_readch(v) be // new 2 { let c = v ! io_putback; if c <> 0 then { v ! io_putback := 0; resultis c } if v ! io_position >= 512 then resultis -1; c := byte v ! io_position of v ! io_thestring; v ! io_position +:= 1; resultis c } let ifile_int_unreadch(v, c) be // new 2 { v ! io_putback := c } let readch(v) be { resultis (v ! io_readch)(v) } let unreadch(v, c) be { resultis (v ! io_unreadch)(v, c) } let readint(v) be { let n = 0, neg = false, c = readch(v); while c <= ' ' do c := readch(v); test c = '-' then { neg := true; c := readch(v) } else if c = '+' then c := readch(v); while c >= '0' /\ c <= '9' do { n := n * 10 + c - '0'; c := readch(v) } unreadch(v, c); if neg then n := - n; resultis n } let readstring(v) be { let s = new_string(), c = readch(v); while c <= ' ' /\ c <> '\n' do c := readch(v); while c > ' ' do { string_addchar(s, c); c := readch(v) } resultis s } let readline(v) be { let s = new_string(), c = readch(v); while c <> '\n' /\ c <> 0 do { string_addchar(s, c); c := readch(v) } resultis s } let writech(v, c) be // new 1 { (v ! io_writech)(v, c) } let writestring(v, s) be // new 1 { for i = 0 to string_length(s) - 1 do writech(v, string_getchar(s, i)) } let write_pos_int(v, n) be // new 1 { if n > 9 then write_pos_int(v, n / 10); writech(v, n rem 10 + '0') } let writebcplstring(v, s) be // new 1 { let i = 0; while true do { let c = byte i of s; if c = 0 then return; writech(v, c); i +:= 1 } } let writeint(v, n) be // new 1 { if n = 0x80000000 then { writestring(v, "-2147483648"); return } if n < 0 then { writech(v, '-'); n := - n } write_pos_int(v, n) } let close(v) be // new 1 { (v ! io_close)(v) } let start_kbd() be { let v = newvec(sizeof_io); v ! io_putback := 0; v ! io_thestring := 0; v ! io_length := 0; v ! io_position := 0; v ! io_blocknum := 0; v ! io_readch := kbd_int_readch; v ! io_unreadch := kbd_int_unreadch; v ! io_writech := kbd_int_writech; v ! io_close := donothing_close; resultis v } let start_str(s) be { let v = newvec(sizeof_io); v ! io_putback := 0; v ! io_thestring := s; v ! io_length := string_length(s); v ! io_position := 0; v ! io_blocknum := 0; v ! io_readch := str_int_readch; v ! io_unreadch := str_int_unreadch; v ! io_writech := illegal_operation; v ! io_close := donothing_close; resultis v } let start_ofile(bn) be // new 1 { let v = newvec(sizeof_io); v ! io_putback := 0; v ! io_thestring := newvec(128); $memory_zero(v ! io_thestring, 128); v ! io_length := 0; v ! io_position := 0; v ! io_blocknum := bn; v ! io_readch := illegal_operation; v ! io_unreadch := illegal_operation; v ! io_writech := ofile_int_writech; v ! io_close := ofile_int_close; resultis v } let start_ifile(bn) be // new 2 { let v = newvec(sizeof_io), r; v ! io_putback := 0; v ! io_thestring := newvec(128); v ! io_length := 0; v ! io_position := 0; v ! io_blocknum := bn; v ! io_readch := ifile_int_readch; v ! io_unreadch := ifile_int_unreadch; v ! io_writech := illegal_operation; v ! io_close := donothing_close; r := devctl(dc$discread, 1, bn, v ! io_thestring); if r < 0 then { (v ! io_close)(v); resultis nil } resultis v } let write_file() be // new 1 { let f = start_ofile(3); for i = 1 to 12 do { writeint(f, i); writebcplstring(f, " * 9 = "); writeint(f, i * 9); writech(f, '\n') } close(f) } let read_file() be // new 2 { let f = start_ifile(3), k = start_kbd(); while true do { let s = readline(f); if string_length(s) = 0 then break; writestring(k, s); writech(k, '\n') } close(f) } let start() be { init(); write_file() }