|
Unix Terminals
An Introduction |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
xterm)/dev or /dev/pts
tty in each to get its namels (you should know
what will happen)ls > other
where other is the name of the other terminal
(e.g., /dev/pts/1)man man (so that the
output requires more than one "screen"
and you need to provide input to move between "screens")read() only returns when a line-feed character is
encounteredint tcsetattr(int fd, int options, const struct termios *attr);
fd
|
The open file descriptor for the terminal |
options
|
TCSANOW to make changes now; TCSADRAIN to wait until after all output is transmitted; TCSAFLUSH to both wait and discard input not received |
attr
|
The attributes to set |
| Return | 0 on success; -1 on error |
termios Structure
struct termios {
tcflag_t c_iflag; // Input flags
tcflag_t c_oflag; // Output flags
tcflag_t c_cflag; // Control flags
tcflag_t c_lflag; // Local modes
cc_t c_line; // Do not use
cc_t c_cc[NCCS}; // Special characters
speed_t c_ispeed; // Do not use
speed_t c_ospeed; // Do not use
}
c_iflag
ICRNL
|
Map CR to LF (i.e., newline) on input |
IGNCR
|
Ignore CR on input |
c_lflag
ECHO
|
Echo input characters |
ICANON
|
Canonical model input |
IEXTEN
|
Enable extended processing of input characters |
ISIG
|
Enable signal generating characters |
read() Return in Noncanonical Mode?VMIN is 0, VTIME is 0VMIN is > 0, VTIME is 0VMIN is 0, VTIME is > 0VMIN is > 0, VTIME is > 0#include <stdio.h>
#include <unistd.h>
#define LENGTH 40
int
main(void)
{
char line[LENGTH+1];
int n;
printf("%s\n", "Enter a short or long line...");
// Note: The line-feed character is not disgarded
n = read(STDIN_FILENO, line, LENGTH);
// Make it a null-terminated string
line[n] = '\0';
printf("Line Length: |%d|\n", n);
printf("Line Contents: |%s|\n", line);
}
#include <termios.h>
#include <unistd.h>
int
main(void)
{
char key;
struct termios newterm, oldterm;
tcgetattr(STDIN_FILENO, &oldterm); // Remember the current mode
newterm = oldterm;
newterm.c_lflag &= ~ICANON; // Not canonical
newterm.c_lflag |= ECHO; // Echo
newterm.c_lflag |= ISIG; // Enable QUIT, INTR, SUSP
newterm.c_lflag &= ~ICRNL; // Don't map CR to LF/NL
newterm.c_cc[VMIN] = 1; // Character-at-a-time
newterm.c_cc[VTIME] = 0; // Do not blocking
tcsetattr(STDIN_FILENO, TCSANOW, &newterm);
write(STDOUT_FILENO, "Continue (Y/N)? ", 16);
read(STDIN_FILENO, &key, 1);
if (key == 'Y')
{
write(STDOUT_FILENO, "\nContinuing\n", 12);
}
else
{
write(STDOUT_FILENO, "\nStopping \n", 12);
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldterm);
}