From b4be6128321fce59f201f7d8fd40b840fa5e9042 Mon Sep 17 00:00:00 2001 From: George Koehler Date: Fri, 4 May 2018 18:06:51 -0400 Subject: [PATCH] Change readstring() to use buffered input. Change from `uread(0, c, 1)` to `read(c)`, so input goes through libpc's buffer. If input is a tty in Unix, this reduces the number of read(2) system calls from one per character to one per line. This change will become necessary in CP/M when I enable the line editor. --- examples/hilo.p | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/examples/hilo.p b/examples/hilo.p index be953a09e..161ff8049 100644 --- a/examples/hilo.p +++ b/examples/hilo.p @@ -9,7 +9,6 @@ program hilo(input, output); type string = packed array [0..255] of char; - charstar = packed array [0..0] of char; var playing : Boolean; @@ -30,47 +29,25 @@ function random(range : integer) : integer; random := seed mod range; end; -{ Pascal doesn't provide string input, so we interface to the read() syscall - and do it manually. But... we can't interface to read() directly because - that conflicts with a Pascal keyword. Luckily there's a private function - uread() in the ACK Pascal library that we can use instead. } - -function uread(fd : integer; var buffer : charstar; count : integer) : integer; - extern; +{ Pascal doesn't provide string input, so we read characters until the + end of line and put them in a string. } -function readchar : char; - var - c : charstar; - dummy : integer; - - begin - c[0] := chr(0); - dummy := uread(0, c, 1); - readchar := c[0]; - end; - procedure readstring(var buffer : string; var length : integer); var - finished : Boolean; c : char; begin write('> '); length := 0; - finished := FALSE; - seed := 0; - while not finished do + repeat begin - c := readchar; - if (ord(c) = 10) then - finished := true - else - begin - buffer[length] := c; - length := length + 1; - end - end; + read(c); + buffer[length] := c; + length := length + 1; + end + until eoln; + readln; { discard end of line } end; procedure getname;