ReadKey like Function in C

Steve Summit scs at adam.pika.mit.edu
Sun Aug 13 06:51:58 AEST 1989


In article <3705 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
>How about if someone begs with the ANSI committee to have its
>implementation as a standard function required for compliance?

In article <10712 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>Not only is it too late, but I think it was proposed and justly was
>awarded a "VE" stock response code, meaning:  The Standard must
>accommodate a variety of environments.

Doug is correct, but the astonishing frequency with which this
question arises (I think it's been asked on comp.lang.c three
times in the past week alone) indicates that the opportunity is
ripe for some kind of informal, community-"standard" solution.
(Or did someone try this already?)

One difficulty is whether the interface should be a "getkey"
routine or a set of mode-setting functions.  A straightforward
getkey implementation on Unix would suffer at least three system
call overheads per character read.  Mode setting, though hewing
to the Unix ioctl tradition, might be distasteful to MS-DOS
aficionadoes who appreciate the utility of getch() (and, on a
related note, kbhit()).  (The presence of these two simple, oft-
needed routines might be said to be one thing which DOS does
right, although it's achieved with the introduction of a bizarre
dichotomy between "Console I/O" and stdio.)

A useful approach (combining the worst of both approaches) would
accommodate mode setting _and_ a separate getkey routine:

	setkeymode(CHARATATIME);
	/* repeated calls to */ getkey();
	setkeymode(CANONICAL);

On Unix, setkeymode would play the usual games with ioctls
(V7, SYSV, or POSIX flavors), and getkey would map to getchar.
On MS-DOS, setkeymode would be a no-op and getkey would map to
getch.  On VMS, either approach could be used (there's a per-read
QIO function modifier to disable canonical processing, as well as
mode-setting QIO's.)  On other systems the scheme might be
unimplementable, but that's what I mean by an informal, community
"standard," as opposed to a mandatory part of the pANS.

If there's interest, I could post versions for the three systems
mentioned above (I'd only be able to test the V7/BSD Unix
variants), but first we have to have a long debate about what the
routines should be called...

                                            Steve Summit
                                            scs at adam.pika.mit.edu

P.S. It occurs to me that, whenever this question is asked,
     someone points out that the curses package provides a semi-
     standard means of doing character-at-a-time input, which
     could be used independently of curses' screen management
     features.  I suppose the sequence would be crmode();
     getch(); nocrmode();.  ("getch" -- what a coincidence.)



More information about the Comp.std.c mailing list