"Raw" and "Non-blocking" terminal mode in Linux

"Raw" and "Non-blocking" terminal mode in Linux

Post by AQ » Tue, 18 Nov 1997 04:00:00



I am porting my non-gui text editor to Linux.

On Sun Solaris, SGI IRIX, Sequent Unix the following code allows me
to put a terminal into "raw" mode, then set or unset non-blocking
termial reads at will.

On Linux this code does not work - the terminal wont go into non-blocking
read mode.
I have studied the man pages for ioctl, termio/termios, terminfo,
term and cannot find anything to give me a clue as to what I'm doing
wrong.

I have looked at example code (nvi), and it uses the termios struct in
place of termio struct, and
   tcsetattr(0,(struct termios *) &new_term))
in place of ioctl(), with everything alse the same, but this fails in a
different way, appearing to be cooking Ctrl-C (etc) when I have told it
not to.

If there is anyone who has written/ported code to do this on Linux I
would be most gratefull for any assistance.

#include <term.h>

static struct termio old_term;
static struct termio new_term;

/* put the terminal into "raw" mode with blocking terminals reads */
void set_raw()
{
new_term.c_iflag = new_term.c_iflag & ~IXON & ~BRKINT;
new_term.c_lflag = new_term.c_lflag & ~ECHO & ~ICANON & ~ISIG;
new_term.c_cc[4] = 1;
new_term.c_cc[5] = 0;

if (ioctl(0,TCSETA,(struct termio *) &new_term))
  fprintf(stderr,"\nUnable to setup terminal - ioctl() failed.\n");

Quote:}

/* setup non-blocking terminals reads */
void non_block_kb(int delay)
{
if (kb_blocking)
  {
  new_term.c_cc[4] = 0;
  new_term.c_cc[5] = delay;
  if (ioctl(0,TCSETA,(struct termio *) &new_term))
    fprintf(stderr,"Unable to setup non-blocking kb input: ioctl() failed.");
  }

Quote:}

Many thanks, Mark.

 
 
 

"Raw" and "Non-blocking" terminal mode in Linux

Post by Andrew Giert » Tue, 18 Nov 1997 04:00:00


 AQ> I am porting my non-gui text editor to Linux.
 AQ> On Sun Solaris, SGI IRIX, Sequent Unix the following code allows me
 AQ> to put a terminal into "raw" mode, then set or unset non-blocking
 AQ> termial reads at will.

 AQ> On Linux this code does not work - the terminal wont go into
 AQ> non-blocking read mode.
 AQ> I have studied the man pages for ioctl, termio/termios, terminfo,
 AQ> term and cannot find anything to give me a clue as to what I'm doing
 AQ> wrong.

Firstly, you should always use termios in preference to termio when
possible. As you don't give your code for the termios version I can't
comment on why it didn't work, other than the fact that this:

 AQ>    tcsetattr(0,(struct termios *) &new_term))

is blatantly wrong (should be tcsetattr(0, TCSANOW, &new_term))

 AQ> #include <term.h>

 AQ> static struct termio old_term;
 AQ> static struct termio new_term;

 AQ> /* put the terminal into "raw" mode with blocking terminals reads */
 AQ> void set_raw()
 AQ> {
 AQ> new_term.c_iflag = new_term.c_iflag & ~IXON & ~BRKINT;
 AQ> new_term.c_lflag = new_term.c_lflag & ~ECHO & ~ICANON & ~ISIG;
 AQ> new_term.c_cc[4] = 1;
 AQ> new_term.c_cc[5] = 0;

Never hardcode constants. You have no guarantee that MIN and TIME have
these values.

Examples using termios can be found in the FAQ.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>