[comp.lang.c] getting a key from stdin in UNIX

gm@cunixd.cc.columbia.edu (Gary Mathews) (02/05/90)

	I want to be able to press a key and respond to that key without
pressing the <ENTER> key, such as getc on TurboC...

If I use the curses library the getch command works fine under cbreak mode,
but I want to use it without using an entire library for one command.

the macro for getc in stdio.h is as follows:
#define	getc(p)		--(p)->_cnt>=0? ((int)*(p)->_ptr++):_filbuf(p))

I've used variations of getc and the following, but I can't seem to get
the optimum results...

  fgets(s1, 1, stdin);
  c = fread(&buf, sizeof(char), 1, stdin);
  scanf("%c", c);
  c = read(0, buf, sizeof(char));

How do you get a key without pressing enter?
Does anyone have a solution for this?

-------------------------------------------------------------------------------
Gary Jason Mathews      | gm@cunixd.cc.columbia.edu
Columbia University     | Death is life's way of telling you you've been fired.
------------------------+ CPU time flies when you have a lot of bugs

chris@mimsy.umd.edu (Chris Torek) (02/05/90)

In article <2802@cunixc.cc.columbia.edu> gm@cunixd.cc.columbia.edu
(Gary Mathews) writes:
>	I want to be able to press a key and respond to that key without
>pressing the <ENTER> key, such as getc on TurboC...

The very subject---`getting a key from stdin in UNIX'---should tell you
that this is not a question about C.  In particular, those last two words
(`in UNIX') imply, quite correctly, that this is a question about Unix.
It should therefore not appear in this newsgroup.

The answer to the question can be found in the `frequently asked
questions' posting that appears monthly in comp.unix.questions.
Look there to find it.

This has been a public disservice announcement by the Netnews Police. :-)
(Seriously, can we keep the IBM PC and Unix specific questions out of the
C specific newsgroup?)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

mikes@rtech.UUCP (Mike Schilling) (02/07/90)

From article <22287@mimsy.umd.edu>, by chris@mimsy.umd.edu (Chris Torek):
> This has been a public disservice announcement by the Netnews Police. :-)
> (Seriously, can we keep the IBM PC and Unix specific questions out of the
> C specific newsgroup?)
And since this group is comp.lang.c, not comp.lang.c.on_unix, can we not,
while discussing portability:

1. Assume the existence of lint.
2. Answer pointer questions by discussing Unix address space.

End of harangue.  Thanks for listening.
----------------------------------------------------------------------------
"I want the right to urinate in different colors"  -- Tom Stoppard, "Travesties"
mikes@rtech.com = Mike Schilling, Ingres Corporation, Alameda, CA

tchrist@convex.COM (Tom Christiansen) (02/07/90)

In article <4665@rtech.rtech.com> mikes@rtech.UUCP (Mike Schilling) writes:
>And since this group is comp.lang.c, not comp.lang.c.on_unix, can we not,
>while discussing portability:
>
>1. Assume the existence of lint.

I strongly believe that any vendor who gives you a C compiler
without lint is ripping you off.  It's like not having any 
brakes or directions on your car.  Get on the horn and bitch.

--tom
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"

evil@arcturus.UUCP (Wade Guthrie) (02/08/90)

Gary Mathews writes:

>	I want to be able to press a key and respond to that key without
>pressing the <ENTER> key, such as getc on TurboC...

Chris Torek writes:

[...]
>It should therefore not appear in this newsgroup.

Sorry to disagree with you on this one, Chris, but what about those of us 
that can answer the question in a non-machine specific way?  

Try using curses, a public domain package that exists on many systems including 
Unix, VMS (albeit in a bass-ackwards sort of way), and MS-DOS.  By putting the 
terminal into 'raw' mode, you can do what is being asked.  In fact, I end up
using curses a lot to handle portable menus and such.

Your code should look something like:

	#include <curses.h>

	main(argc, argv)
	...
		initscr();	/* starts curses */
		...
		raw();		/* puts the input mode to raw */
		c = getch();	/* in raw mode, this just gets the char -- no
				 * newline required */
		noraw();	/* returns to cooked mode */
		...
		endwin();
	}

Or some such.  Check out the curses manual for details.


Wade Guthrie
evil@arcturus.UUCP
Rockwell International; Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)

kers@hplb.hpl.hp.com (Kers) (02/08/90)

tchrist@convex.COM (Tom Christiansen) writes:

   I strongly believe that any vendor who gives you a C compiler
   without lint is ripping you off.  It's like not having any 
   brakes or directions on your car.  Get on the horn and bitch.

*Some* C compilers are rude enough about implausible constructs to make lint
redundant. I'd much rather my compiler told me that it had kindly declared an
-extern int toggle_swicth()- for me than (a) wait for the linker to laugh, or
(b) be forced to run an extra program to perform this elementary check.

And no, I'm not advertising HP compilers, either.

Regards, Kers.

chris@mimsy.umd.edu (Chris Torek) (02/08/90)

In article <7184@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes:
>... what about those of us that can answer the question in a non-machine
>specific way?

Your way is not specific to one particular machine, or even one particular
version of (UNIX | VMS | MS-DOS | ...), but it is not C.  It merely happens
to be *coded* *in* C.

>Try using curses ....

Oddly enough, curses does not exist on Univac 1100 systems running EXEC-8
even when those systems have C compilers (some do).  Then again, perhaps
it is not so odd, since the system is not in general capable of character-
at-a-time input.  Similarly, there are a number of IBM systems that can
support ANSI X3.159 C, but cannot read invididual keystrokes.  The
hardware is not cooperative: it sends only complete lines (or screens).

There is a perfectly good newsgroup for `read character at a time from
stdin under unix', and it is not comp.lang.c.  (comp.lang.c is, however,
marginally appropriate for discussions about how character-at-a-time
input interfaces ought to appear in future standard C libraries.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

peter@ficc.uu.net (Peter da Silva) (02/08/90)

In article <22352@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
> >Try using curses ....

> Oddly enough, curses does not exist on Univac 1100 systems running EXEC-8
[etcetera]

I think it's quite appropriate, since curses is likely to be available to
the majority of C programmers reading this list. While it's massive overkill,
it's a broad enough solution to satisfy all by the most critical observors.

This is comp.lang.c, not comp.std.c.
-- 
 _--_|\  Peter da Silva. +1 713 274 5180. <peter@ficc.uu.net>.
/      \
\_.--._/ Xenix Support -- it's not just a job, it's an adventure!
      v  "Have you hugged your wolf today?" `-_-'

darcy@druid.uucp (D'Arcy J.M. Cain) (02/08/90)

In article <5206@convex.convex.com> tchrist@convex.COM (Tom Christiansen) writes:
>I strongly believe that any vendor who gives you a C compiler
>without lint is ripping you off.  It's like not having any 
>brakes or directions on your car.  Get on the horn and bitch.
>
No thanks.  I use Borland Turbo C with all the warnings turned on and GNU C
with the -Wall option and I'll take that over lint any day.  I actually get
useful messages about what is wrong with the program as opposed to those
crypto-facist messages that lint gives me.  I can get by for the rest of my
life without looking at another output from lint.

-- 
D'Arcy J.M. Cain (darcy@druid)     |   Thank goodness we don't get all 
D'Arcy Cain Consulting             |   the government we pay for.
West Hill, Ontario, Canada         |
(416) 281-6094                     |

john@frog.UUCP (John Woods) (02/09/90)

In article <M:L163Fxds13@ficc.uu.net>, peter@ficc.uu.net (Peter da Silva) writes:
> In article <22352@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
> > >Try using curses ....
> > Oddly enough, curses does not exist on Univac 1100 systems running EXEC-8
> I think it's quite appropriate, since curses is likely to be available to
> the majority of C programmers reading this list.

And most interestingly, the Berkeley curses implementation is available
under the new Berkeley Copyright (i.e., use it, enjoy it, thank us if you
sell it), so if EXEC-8 CAN support curses, there is no excuse for it
not to have it.
-- 
John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101
...!decvax!frog!john, john@frog.UUCP, ...!mit-eddie!jfw, jfw@eddie.mit.edu

bill@twwells.com (T. William Wells) (02/11/90)

In article <1990Feb8.130910.9083@druid.uucp> darcy@druid.UUCP (D'Arcy J.M. Cain) writes:
: In article <5206@convex.convex.com> tchrist@convex.COM (Tom Christiansen) writes:
: >I strongly believe that any vendor who gives you a C compiler
: >without lint is ripping you off.  It's like not having any
: >brakes or directions on your car.  Get on the horn and bitch.
: >
: No thanks.  I use Borland Turbo C with all the warnings turned on and GNU C
: with the -Wall option and I'll take that over lint any day.  I actually get
: useful messages about what is wrong with the program as opposed to those
: crypto-facist messages that lint gives me.  I can get by for the rest of my
: life without looking at another output from lint.

I use them both. The cost of running a "finished" program through
every checker you have before releasing it is generally negligable
compared to the cost of the errors that would otherwise appear.

Actually, any time I have a not immediately obvious bug during
development of a program, the first thing I do is run it through
gcc with all the bells and whistles on and/or lint before even
attempting to debug further. (Which I use first is mostly a
matter of chance; it depends on how my makefiles happened to be
set up that moment.)

---
Bill                    { uunet | novavax | ankh } !twwells!bill
bill@twwells.com

peter@ficc.uu.net (Peter da Silva) (02/11/90)

In article <11961@frog.UUCP> john@frog.UUCP (John Woods) writes:
> And most interestingly, the Berkeley curses implementation is available
> under the new Berkeley Copyright (i.e., use it, enjoy it, thank us if you
> sell it), so if EXEC-8 CAN support curses, there is no excuse for it
> not to have it.

I don't think it can.

I would like to note, however, that at Ferranti we *are* one of those sites
that runs Exec-1100 on the Unisys 1100 (AKA Exec-8 on the Univac). If anyone
was going to get hot under the collar about a message being inappropriate to
comp.lang.c because it suggested curses to get a single keystroke it should
be us...

(yeh, we even run TCP/IP on the beasts)
-- 
 _--_|\  Peter da Silva. +1 713 274 5180. <peter@ficc.uu.net>.
/      \
\_.--._/ Xenix Support -- it's not just a job, it's an adventure!
      v  "Have you hugged your wolf today?" `-_-'

dold@mitisft.Convergent.COM (Clarence Dold) (02/12/90)

in article <1990Feb11.052731.318@twwells.com>, bill@twwells.com (T. William Wells) says:
	regarding 'lint' verses compiler warning levels:
> I use them both. The cost of running a "finished" program through
> every checker you have before releasing it is generally negligable
> compared to the cost of the errors that would otherwise appear.

I also run source through 'cb' or one of the pretty-print programs.
I've encountered bugs where I wasn't looking by noticing that the indentation
appeared goofy.  My idea of 'else' doesn't always match the compilers'.

-- 
---
Clarence A Dold - dold@tsmiti.Convergent.COM            (408) 435-5293
               ...pyramid!ctnews!tsmiti!dold        FAX (408) 435-3105
               P.O.Box 6685, San Jose, CA 95150-6685         MS#10-007

meissner@osf.org (Michael Meissner) (02/13/90)

In article <11961@frog.UUCP> john@frog.UUCP (John Woods) writes:

| In article <M:L163Fxds13@ficc.uu.net>, peter@ficc.uu.net (Peter da Silva) writes:
| > In article <22352@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
| > > >Try using curses ....
| > > Oddly enough, curses does not exist on Univac 1100 systems running EXEC-8
| > I think it's quite appropriate, since curses is likely to be available to
| > the majority of C programmers reading this list.
| 
| And most interestingly, the Berkeley curses implementation is available
| under the new Berkeley Copyright (i.e., use it, enjoy it, thank us if you
| sell it), so if EXEC-8 CAN support curses, there is no excuse for it
| not to have it.

That may be, but I doubt that BSD curses would be all that ameniable
to running on EXEC-8.  BSD curses assumes 8 bit bytes and 7 bit
display.  The last time I used EXEC-8 (oh, about 15 years ago), it
used 9 bit bytes (since the Univac computers are 36 bit machines).
Also, I suspect BSD curses assumes 2's complement arithmetic, and the
Univac computers used 1's complement arithmetic (ie, -1 did not set
all 1 bits, -0 did).
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so