daniel@island.COM (Dan Smith) (06/17/91)
In <652@elroy> davidk@dsinet (David Karr) writes: > I could write it myself, but if it already exists, I won't bother. > I want a program which I can give approximately the following arguments to: > 1. A prompt string (like "Enter the document type") > 2. A string indicating the list of possible one-letter answers. > 3. A string indicating the default if they just press space or return. > (This could be encapsulated in the second argument.) I wrote grabchars, which is a package for obtaining one or more keystrokes directly from the user. You may get 1.95 from me or from comp.sources.misc archives worldwide. I'll include the man page: Daniel p.s. I'm getting ready to do 2.0, which will handle function/arrow keys, and also allow for keyboard polling (whether there is a key to read or not), among other things. GRABCHARS(1) USER COMMANDS GRABCHARS(1) NAME grabchars - get keystrokes directly from user SYNOPSIS grabchars [options] DESCRIPTION Grabchars gets characters from the user as they are typed in, without having to wait for the return key to be pressed. Among other things, this allows shell scripts to be written with highly interactive menus. By default, grabchars will obtain one character from stdin, echo that character to stdout, and return with a status of one; meaning one character read. -b Both stdout and stderr are used for output. This is useful for setting a variable in a shell script and echoing a keystroke to the screen at the same time. -c<valid characters> Only characters in <valid characters> are accepted. Regular expressions such as [a-z] may be used to specify ranges. All other characters are ignored. -d<char(s)> Default char or string to output if the user hits RETURN or lets grabchars timeout. The status that is returned is the same as if the user had typed in the character or string, so this option may be used with the -s (silent) flag. -e Output goes to stderr rather than stdout. -f Flush any previous input. By default, grabchars will see any characters present in stdin, which allows for some typeahead in shell scripts. -h Help/usage screen. -p<prompt> Sets up a prompt for the user. See EXAMPLES. -q<prompt> Sets up a prompt for the user, except it is printed to stderr rather than stdout. -r The RETURN key exits. Use this with the -n option to allow for variable numbers of characters to be typed in. -n<number> Sun Release 4.0 Last change: LOCAL 1 GRABCHARS(1) USER COMMANDS GRABCHARS(1) Number of characters to read. By default, grabchars looks for one character. -s Silent. Do not output anything. Just return a status. -t<seconds> Time to allow the user to respond. By default, the user can take as long as he or she wants to. The timeout option allows you to write shell scripts where you can offer some assistance if it's obvious that the user might be stuck. -E Erase/kill processing is done. You have use of the keys (usually DELETE and ^U or ^X) that you would nor- mally have from the shell for deleting characters. This is useful with the -n option, where many charac- ters are being typed in. This code hasn't been thoroughly tested. -L Map characters to lower case. -L Map characters to upper case. Note that if -U and -L are both specified on the command line, the last one that occurs will win out. This is because it doesn't make sense to try to do both at once. EXAMPLES grabchars gets one keystroke grabchars -caeiou get one of the vowels grabchars -c i get the letter 'i' grabchars '-penter a letter ' print the prompt "enter a letter " grabchars '-qenter a letter ' print the prompt ('q' for question) "enter a letter " through stderr. grabchars -n4 get four characters. grabchars -d a If the first character typed is a RETURN, pretend it was an 'a'. grabchars -d gumby If the first character typed is a RETURN, pretend that Sun Release 4.0 Last change: LOCAL 2 GRABCHARS(1) USER COMMANDS GRABCHARS(1) the user typed in "gumby". grabchars -r The RETURN key will exit grabchars. You would use this with -n option, so that variable numbers of characters may be entered. grabchars -n 4 -r -t 10 Accept up to four characters, or exit when RETURN is hit, or exit when 10 seconds have elapsed. grabchars -t2 timeout after two seconds. grabchars -d gumby -t2 If the first character typed is a RETURN, or if two seconds have gone by, pretend that the user typed in "gumby". grabchars -n3 -p 'initials: ' print a prompt and grab three characters. grabchars -c 0123456789 -n2 -t10 get two numbers with a ten second timeout. note that arguments like "-n4" or "-n 4" are handled the same way SEE ALSO csh(1) and sh(1) for syntax of csh and sh scripts, respec- tively. See "The Unix Csh Field Guide", by Gail and Paul Anderson (Prentice Hall), for an excellent tour of csh and good examples of writing csh scripts. DIAGNOSTICS Grabchars returns -2 if it times out, or -1 if it gives a usage statement. Otherwise, it returns the number of char- acters successfully read. AUTHOR Daniel Smith (daniel@island.com, daniel@world.std.com) -- daniel@island.com Daniel Smith, Island Graphics, (415) 491 0765 x 250(w) daniel@world.std.com 4000 CivicCenterDrive SanRafael MarinCounty CA 94903 dansmith@well.sf.ca.us Fax: 491 0402 Disclaimer: Hey, I wrote it, not IG! falling/yes I'm falling/and she keeps calling/me back again - IJSaF, Beatles