DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Terminal device control

TTY in canonical mode

In canonical mode, characters are read from the device and processed before being returned. This processing translates kill and erase characters. Characters are not returned until a new line (NL), end of file (EOF), or end of line (EOL) is read, which means that characters are returned a line at a time. Canonical mode is usually associated with terminals.

An important factor to consider when using canonical mode is what to do when reading from a TTY device for which characters are not available. If the O_NDELAY flag has been set for the TTY, then such reads return a 0, indicating that no characters are available. Otherwise, reads will not return until a character is available. If a program can perform other processing when characters are not available from a TTY, then the O_NDELAY flag should be set for the TTY. This might require programs to be more complicated, but the complication are offset by an increase in efficiency.

The following function opens a TTY device for reading or writing (line 12), places it in canonical mode (line 23), and sets the O_NDELAY option so that reads are not blocked when characters are not available (line 12).

 1  #include <fcntl.h>
 2  #include <termio.h>
 3
 4  extern struct termio old_term;
 5
 6  setup1(TTY)
 7  char *TTY;
 8  {
 9        int fid;
10        struct termio new_term;
11
12        if ((fid = open(TTY, O_RDWR|O_NDELAY)) == -1)
13        {
14                printf("open failed.\n");
15                exit(1);
16        }
17                else if (ioctl(fid, TCGETA, &old_term) == -1)
18                     {
19                        printf("ioctl get failed.\n");
20                        exit(1);
21                     }
22        new_term = old_term;
23        new_term.c_lflag |= ICANON;
24        if (ioctl(fid, TCSETA, &new_term) == -1)
25        {
26                printf("ioctl set failed.\n");
27                exit(1);
28        }
29        return fid;
30  }

Improving TTY performance - canonical mode


Next topic: TTY in raw mode
Previous topic: Improving terminal I/O performance

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004