cbreak mode without curses?

cbreak mode without curses?

Post by Dwight A. Schwar » Sat, 23 Jul 1994 04:21:39



Hi netters,
        I would like to be able to implement something analogous to cbreak
mode without going through all the extra stuff of doing curses throughout
my whole program.  What I want is something that has the same effect
as the following:

cbreak();               /* cbreak mode for module display_on_tek */
ch = getch();           /* get a character into ch */
nocbreak();             /* ending cbreak mode */

without all the added stuff about linking to -lcurses and #including
<curses.h>, etc.

        It seems that there should be a way to use termio calls to change
(in the parlance of the termio(7) man page on my SGI/IRIX machine) the "input
processing mode" from the "canonical" to the "non-canonical" style.  Canonical
Mode Input processing is the following (from the IRIX 4.x termio(7) man page):

     Normally, terminal input is processed in units of lines.  A line is
     delimited by a new-line (ASCII LF) character, an end-of-file (ASCII EOT)
     character, or an end-of-line character.  This means that a program
     attempting to read will be suspended until an entire line has been typed.
     Also, no matter how many characters are requested in the read call, at
     most one line will be returned.  It is not, however, necessary to read a
     whole line at once; any number of characters may be requested in a read,

     During input, erase and kill processing is normally done.  The erase
     character erases the last character typed, except that it will not erase
     beyond the beginning of the line.  The kill character kills (deletes) the
     entire input line, and optionally outputs a new-line character.  Both
     these characters operate on a key-stroke basis, independently of any
     backspacing or tabbing that may have been done.  The erase and kill
     characters may be changed.

Non-canonical Mode Input Processing (from the same termio man page) means:

     ". . . input characters are not assembled into lines, and erase and kill
     processing does not occur.  The MIN and TIME values are used to determine
     how to process the characters received."

This would allow a single command, analogous to:

ch = getch();   /* get a character into ch */

to sleep until one, only one, and any one character was typed at the keyboard.
Then it would return that character in the variable ch to the program.

        Any ideas, advice, flames ( :) ) and whatever else will be appreciated
greatly.

Dwight

----
Dwight A. Schwartz
University of Illinois at Urbana-Champaign
Department of Chemistry
Illinois EPR Research Center
217-333-3776

----

 
 
 

cbreak mode without curses?

Post by Dennis Tayl » Sat, 23 Jul 1994 09:58:09


read up on ioctl(S).

should this be a FAQ? There's a lot of this question on the net...

 
 
 

cbreak mode without curses?

Post by Davor Slamn » Sun, 24 Jul 1994 00:25:13



Quote:>    I would like to be able to implement something analogous to cbreak
> mode without going through all the extra stuff of doing curses throughout
> my whole program.

    I keep mailing this code to people, but the demand is too big :-)

Slama

/*
        chin.c   v. 1.1
        Davor Slamnig,  6/94

        Single character keyboard input on UNIX in three modes:

        wait    - getchar() blocks until a character is read.
        nowait  - getchar() returns immediately, -1 is returned.
          if there are no characters in queue.
        timeout - getchar() blocks until a character is read,
                  or a specified time limit is exceeded.

        See termio(7)
*/

#include <stdio.h>
#include <termio.h>

#define V_TIMEOUT       10      /* 1 second */

struct termio Savevid;
char Str[80];

void vidsave(vmin, vtime)
{
        if(ioctl(0, TCGETA, &Savevid) == -1){
                perror("vidsave failed");
                return;
        }

Quote:}

void vidsetup(vmin, vtime)
int vmin, vtime;
{
        static struct termio newvid; /* must be static or global */

        if(ioctl(0, TCGETA, &newvid) == -1){
                perror("TCGETA failed");
                return;
        }

        newvid.c_lflag = (newvid.c_lflag & ~ICANON & ~ECHO);
        newvid.c_cc[VMIN] = vmin;
        newvid.c_cc[VTIME] = vtime;

        if(ioctl(0, TCSETA, &newvid) == -1){
                perror("vidsetup failed");
                return;
        }      

Quote:}

void vidreset()
{
        if(ioctl(0, TCSETA, &Savevid) == -1){
                perror("vidreset failed");
                return;
        }      

Quote:}

int main()
{
        int c;

        vidsave();

        while( 1 ){
                printf("\n\tW - wait\n\tN - nowait\n\tT - timeout\
\n\tQ - quit \n\t<Space> - exit test loop\n>");
                gets(Str);

                switch(Str[0]){
                case 'W':
                case 'w':
                        vidsetup(1, 0);         /* wait */
                        break;
                case 'N':
                case 'n':
                        vidsetup(0, 0);         /* nowait */
                        break;
                case 'T':
                case 't':
                        vidsetup(0, V_TIMEOUT); /* timeout */
                        break;
                case 'Q':
                case 'q':
                        return;
                default:
                        continue;
                }

                printf("\n\rTest...\n\r");
                while((c = getchar()) != ' ')
                        printf("%c %d\n\r", c, c);

                vidreset();
        }
    return 1;

Quote:}

/*************** End of code ********************/
 
 
 

cbreak mode without curses?

Post by Paul Stephen Bori » Tue, 26 Jul 1994 21:19:22



Quote:>This would allow a single command, analogous to:
>ch = getch();       /* get a character into ch */
>to sleep until one, only one, and any one character was typed at the keyboard.

I would set MIN = 1 and TIME = 0 so that you have no timer at all on read
and read will return after each character.

Quote:>Dwight

-Paul

--
----------------------------------------------------------------------------