[comp.unix.questions] SYSV version of \"grabchars.c\".

mark@ria-emh2.army.mil (Mark D. McKamey IM SA) (05/05/89)

Hello,
 
    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.

Mark D. McKamey
mark@RIA-EMH2.ARMY.MIL

dansmith@well.UUCP (Dan "Bucko" Smith) (05/06/89)

In article <19462@adm.BRL.MIL> mark@ria-emh2.army.mil (Mark D. McKamey IM SA) writes:
>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 may even port that myself this week if noone else has!  I wrote
grabchars and said at the time that I would #ifdef SYS V into it, and
then I got dragged/sidetracked into some other projects.  If anyone
has done this port, please send it to me (and Mark), so that I may add
to that for my next release.  Also, if anyone has suggestions on what to
add to it, send 'em; I'll see what I can do.

				dan
-- 
                         Dan "Bucko" Smith
     well!dansmith  unicom!daniel@pacbell.com  daniel@island.uu.net
ph: (415) 332 3278 (h), 258 2176 (w) disclaimer: Island's coffee was laced :-)
My mind likes Cyberstuff, my eyes films, my hands guitar, my feet skiing...

bink@aplcen.apl.jhu.edu (Ubben Greg) (05/06/89)

In article <19462@adm.BRL.MIL> mark@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)?

					-- Greg Ubben
					   bink@aplcen.apl.jhu.edu

dansmith@well.UUCP (Dan "Bucko" Smith) (05/09/89)

>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)?
>
>					-- Greg Ubben
>					   bink@aplcen.apl.jhu.edu


	Grabchars could be tought about function keys, that isn't
supported yet.  What it can do now is:

	timeout after a given number of seconds 
	allow only a given set of characters (using regular expressions)
		(filter for numbers only, alpha only, etc.)
	take in (up to) a given number of characters
	a default character can be returned if RETURN is hit
	map lower->upper, upper->lower case
	flush or not flush previous input (kill or don't kill typeahead)
	echo or not echo characters typed to stdout and/or stderr
		(for setting variables in shell scripts)
	etc... these all work together now.

				dan
-- 
                         Dan "Bucko" Smith
     well!dansmith  unicom!daniel@pacbell.com  daniel@island.uu.net
ph: (415) 332 3278 (h), 258 2176 (w) disclaimer: Island's coffee was laced :-)
My mind likes Cyberstuff, my eyes films, my hands guitar, my feet skiing...

leo@philmds.UUCP (Leo de Wit) (05/10/89)

In article <1201@aplcen.apl.jhu.edu> bink@aplcen.apl.jhu.edu (Greg Ubben) writes:
|In article <19462@adm.BRL.MIL> mark@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.