From: dansmith@Autodesk.COM (Daniel Smith)
Newsgroups: comp.unix.questions
Subject: Re: How do you program Single Char Input on a Sun w. 4.1.1?
Keywords: getchar, getc, termio, fcntl
Date: 16 Apr 92 17:38:21 GMT
Organization: Autodesk Inc., Sausalito CA, USA

In <1992Apr14.220811.15336@oakhill.sps.mot.com> wcl@oakhill.sps.mot.com (Wayne Long) writes:

> I'm writing a C program that requires single character
> input without waiting for the '\0' character.

> How does one properly use ioctl, fcntl, termios.h, or filio.h
> to read a single character from stdin w/o waiting for a '\0'
> character?


> Has anyone effectively solved this basic programming need on
> a Sun workstation?

	Yes, over and over.  see the FAQ for this group.

	Also, for real live code to work from, you get my grabchars
package from comp.sources.misc archives, or from me directly.  I'll
include the man page for grabchars below.

				Daniel




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.1        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, and currently only applies to a BSD
          compiled version.

     -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, expand it  to
          'a'.

     grabchars -d gumby



Sun Release 4.1        Last change: LOCAL                       2






GRABCHARS(1)             USER COMMANDS               GRABCHARS(1)



          If the first character typed is a RETURN, expand it  to
          "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 (dansmith@autodesk.com)













Sun Release 4.1        Last change: LOCAL                       3



-- 
 dansmith@autodesk.com       daniel@world.std.com       dansmith@well.sf.ca.us
        Daniel Smith, Autodesk, Sausalito, California, (415) 332-2344 x 2580
              Disclaimer: written by a highly caffeinated mammal
 $|=1;for("..."," hacker"," perl"," a","just"){print $l=$_.$l,"\015";sleep 1;}