SYSV version of \"grabchars.c\".
Leo de Wit
leo at philmds.UUCP
Wed May 10 21:33:33 AEST 1989
In article <1201 at aplcen.apl.jhu.edu> bink at aplcen.apl.jhu.edu (Greg Ubben) writes:
|In article <19462 at adm.BRL.MIL> mark at ria-emh2.army.mil (Mark D. McKamey) writes:
|> I have recently recieved a copy of Mr. Dan Smith's "grabchars" program
|> which allows you to get one or more keystrokes from the user, without
|> requiring them to hit RETURN within a shell script. The problem I have is
|> that the program was written for BSD UNIX. Has anyone ported said program
|> to SYSV UNIX? If so, would you please send me the SYSV patches. Thank You.
|
| I wrote a System V script to allow single-keystroke input too, and came
|up with a more general solution (not requiring the "grabchars" program).
|I first used the stty command to allow reading single character responses.
|Something like "stty -icanon min '^a'" but I don't have AFSVM handy, so
|this is probably wrong. Be sure to use stty -g to save the current modes
|in a variable first, and set up a trap to restore them. Then the only way I
|found to actually read one character without a newline was to use a "head"
|program I wrote (before I gnu about knew). The code looked something like:
|
| case `head -1c` in
| [Yy])
| ...
| [Nn])
| ...
|
|I would expect that GNU head could be used the same way. Multi-sequence
|keys would fool it (such as function keys) -- does grabchars handle this?
|Is there a better way yet (in shell)?
This one will not handle multi-sequence keys, but is probably more portable
(you don't need a -c flag to head):
First setup the terminal to allow single character reads (like you did,
or somthing like 'stty raw' or 'stty cbreak' in a BSD environment). Then
dd bs=1 count=1 if=/dev/tty 2>/dev/null
will read a single character from the keyboard (and yes, restore the tty
settings afterward).
Leo.
More information about the Comp.unix.questions
mailing list