[comp.lang.fortran] INKEY routine for UNIX fortran

wang@math.ufl.edu (02/05/91)

I saw someone posted an article asking for a routine which reads a
keystroke without a carriage return for MS-fortran. I am asking a
similar routine for UNIX fortran. The routine I want is much like a
BASIC's "INKEY" routine which returns the ASCII code (an integer) of a
character input from the keyboard without a carriage return. It's
shame that such an important routine does not come with any of FORTRAN
compiler....

By the way, does a similar routine in C exist? 

khb@chiba.Eng.Sun.COM (Keith Bierman fpgroup) (02/05/91)

In article <1991Feb4.195630.6570@math.ufl.edu> wang@math.ufl.edu writes:

   I saw someone posted an article asking for a routine which reads a
   keystroke without a carriage return for MS-fortran. I am asking a
   similar routine for UNIX fortran. The routine I want is much like a
   BASIC's "INKEY" routine which returns the ASCII code (an integer) of a
   character input from the keyboard without a carriage return. It's
   shame that such an important routine does not come with any of FORTRAN
   compiler....

   By the way, does a similar routine in C exist? 

This might prove of use.

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	bork.f
#	bork2.c
# This archive created: Thu Aug  2 11:11:25 1990
# By:	chiba (Sun MegaSystems)
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'bork.f'
then
	echo shar: "will not over-write existing file 'bork.f'"
else
cat << \SHAR_EOF > 'bork.f'
      program bork
      integer getc_noecho
      character*1 chr/" "/,EOL
      character*32 password/" "/

      EOL=char(10)

      write(6,'("enter password: ",$)') 
      call flush(6)
      do i = 1, 32
         istat=getc_noecho(chr)
         if (istat .le. 0 .or. chr .eq. EOL) goto 999
         password(i:i)=chr
      end do
 999  continue
      print*," end of run: look ma, no hands .. password is: ",password
      end
SHAR_EOF
fi
if test -f 'bork2.c'
then
	echo shar: "will not over-write existing file 'bork2.c'"
else
cat << \SHAR_EOF > 'bork2.c'
/* emulate getc, but don't echo */

#include <termios.h>

getc_noecho_(buf)
char buf[];

{

	struct termios old, new;
	int rv;
	int size;
	int fd;

	size=1; fd=0; /* hardcoded for 1 byte, stdin */

	if (ioctl(fd, TCGETS, &old) == -1) return -1;
	new = old;
	new.c_lflag &= ~ECHO;
	if (ioctl(fd, TCSETS, &new) == -1) return -1;
	rv = read (fd, buf, size);
	if (ioctl(fd, TCSETS, &old) == -1) return -1;
	return rv;
}
SHAR_EOF
fi
exit 0
#	End of shell archive
--
----------------------------------------------------------------
Keith H. Bierman    kbierman@Eng.Sun.COM | khb@chiba.Eng.Sun.COM
SMI 2550 Garcia 12-33			 | (415 336 2648)   
    Mountain View, CA 94043

jerry@heyman.austin.ibm.com (Jerry Heyman) (02/07/91)

In article <1991Feb4.195630.6570@math.ufl.edu> wang@math.ufl.edu writes:
>I saw someone posted an article asking for a routine which reads a
>keystroke without a carriage return for MS-fortran. I am asking a
>similar routine for UNIX fortran. The routine I want is much like a
>BASIC's "INKEY" routine which returns the ASCII code (an integer) of a
>character input from the keyboard without a carriage return. It's
>shame that such an important routine does not come with any of FORTRAN
>compiler....

The best way to handle that in Unix is to make a call to the 'stty ()' function
that changes the mode of the terminal input.  By default most (if not all)
terminal input is buffered and is known as 'cooked'.  What you want is to 
change the terminals actions to 'raw' - ie when a keystroke is made, you want
to see it - NOT wait for a carriage return to be pressed.

This is callable from C as I have several programs that make use of this, and
I would assume that the same function is available to the Fortran compiler as
a direct call to the operating system.

Word of caution, once you turn on 'raw', all keyboard i/o will be in that mode
until you reset it to 'cooked'.

jerry

-- 
Jerry Heyman                     IBM T-R: jerry@heyman.austin.ibm.com
AWD Tools Development            VNET   : HEYMAN at AUSVMQ
AWD Austin                       T/L    : 793-3962
*** All opinions expressed are exactly that - my opinions and NOT IBM's

jeffe@eniac.seas.upenn.edu (George Jefferson ) (02/11/91)

this is the code I use 
(works on several unix machines)

character*1 c
integer system,getc


i=system('stty -icanon time 0 min 1 -echo)
i=getc(c)
i=system('stty icanon echo')


not very pretty, but it works.
getc returns when the number of characters determined by min
have been entered (1 in this case)

--
-george            george@mech.seas.upenn.edu