-- Copyright 1993-1998, by the Cecil Project -- Department of Computer Science and Engineering, University of Washington -- See the LICENSE file for license information. ---------- -- a couple user-interface-type methods related to stdin/stdout ---------- --DOC The `ask' method prints out a prompt on `stdout' and returns the --DOC result typed in on `stdin'. The `ask_yes_no' method returns true --DOC iff the first character of the response begins with Y or y. method ask(prompt@:string):string { print(prompt); let var s:string := ""; loop_exit(&(exit:&():none){ let buf_len:int := 80; let buffer:m_vstring := new_m_vstring(buf_len); let nread:int := read_line(stdin(), buffer, buf_len, &(code:int){ -- just stop reading, don't fail (failing can cause infinite -- output at the Cecil> prompt!) nonfatal_unix_error(code, "reading "); eval(exit); }); if(nread = 0, exit); if(buffer!nread.pred = '\n', { -- don't include newline in answer s := s || view_subrange(buffer, 0, nread - 2); eval(exit); }); s := s || view_subrange(buffer, 0, nread.pred); }); s } method ask_yes_no(prompt@:string):bool { let s:string := ask(prompt); s.non_empty & { s.first.to_lower_case = 'y' } }