qprompt
version 2
May, 1994
by Alex Kent ... alex@nmt.edu


For those of you who aren't super shell programming buffs, but would still
like some spiffiness to your shell scripts, (peticularly for logging
in) qprompt maybe something you can find useful.

I have been using it in my .login for several months and some other
folks I have know have found it useful, so I figured maybe I would
pass it around a bit more.

What it does:
qprompt takes single character input (in raw mode) from standard in
and returns an exit code which can be used in script decision making.
It also has a timer which if no input (or the proper input) is
received then it exits with a unique code. This option can be disabled
if you want it to prompt for input indefinitely. Since it does take
single character  input, enter is not required to be hit following the
choice. (A nice option over using the shell input method)

This is a example of how to use raw mode if you don't want to mess
with curses or something similar. Feel free to hack this code up and
use it how you wish. I have compiled it on SunOS and Linux both. It
should be fairly compatible with other OSs as well-Nothing complicated.

The execution is:
	qprompt [-a] time [c1/c2 c3/c4... ....]

Time is specified in seconds, with a time of 0 prompting indefinitely.

The -a option allows other keys other then those specified
to return an exit code of 2. If the -a option is not used
then qprompt ignores invalid character inputs. If no valid
characters are given on the input line then qprompt returns
a 2 for any input. NOTE: this includes [enter] when there is
no other keys requested (i.e. when its "Hit any key to do something").
Qprompt allows for multiple characters to return the same exit
code. Equivalent characters are seperated by /. Exit codes are
the number in the command line, starting with 3.

Please mail comments, bugs, flames, etc to alex@nmt.edu.

Examples:

		echo -n "Start X: <y,N>? "
		qprompt -a 5 n/N y/Y/X/x
		set tmp=$status
		echo
		if ($tmp == 0) then
			echo "5 seconds have expired"
		else if ($tmp == 1) then
			echo "An enter received"
		else if ($tmp == 2) then
			echo "A key other then a selection received"
		else if ($tmp == 3) then
			echo "An n or N received"
		else if ($tmp == 4) then
			echo "An Y or y or X or x received"
		endif

************************************************************

echo -n "Normal(80x25)/Extended(80x50)/Wide(132x44)/Big(132x48):<N,e,w,b> "
        qprompt 2 n/N e/E w/W b/B
        set tmp=$status
        if ($tmp == 0 || $tmp == 1) then
                stty rows 24
                stty cols 80
                reset
        else if ($tmp == "3") then
                stty rows 24
                stty cols 80
                reset
        else if ($tmp == "4") then
                stty rows 49
                stty cols 80
                reset
        else if ($tmp == "5") then
                stty rows 42
                stty cols 132
                reset
        else if ($tmp == "6") then
                stty rows 48
                stty cols 132
                reset
        endif

******************************************************


  if ( { mail -e } && `tty` != "console") then
    echo -n "You have unread mail.  Do you want to read it now (Y or N) ? "
    qprompt 5 y/Y n/N
    set tmp=$status
    if ($tmp == 3) mail
  endif


*******************************************************


###########  Default Window System- set winsys to 3 for Openwin,
###########                                       4 for MIT-X,
###########                                       5 for none
set winsys=3
###########

###########  Given the window system selected above, set win_name with the 
###########  proper name of the window system
switch ($winsys)
case 3:
  set win_name=Openwin
  breaksw
case 4:
  set win_name=MIT-X
  breaksw
case 5:
  set win_name=none
  breaksw
endsw
 echo -n "Openwin/Mit-x/None <o,x,n> (default: " $win_name ")? "
  qprompt 5 o/O x/X n/N
  set tmp=$status

 if ($tmp == 0 || $tmp == 1) set tmp=$winsys

  switch ($tmp)
  case 3:
    ow
    breaksw
  case 4:
    mitx
    breaksw
  case 5:
    breaksw
  endsw

  if($tmp != 5) then

    echo -n "Automatically logging out (press any key to stop logout)"

    qprompt -a 5   # Wait five seconds and logout
    set tmp=$status 
    if($tmp == 0) then
      logout          # logout after leaving windows system
    endif  # If any other (2 to be exact) exit code then just continue
  
endif


