POSIX flags

Terminal in Linux

By default, the terminal starts in cooked mode or canonical mode. In this mode the terminal waits for the user to press the Enter key before sending the input to the program.

Another mode is raw mode. In this mode the terminal sends the input to the program as soon as the user presses a key. This mode is useful for programs that need to read the input character by character.

termios.h

termios.h is a library that provides access to POSIX terminal internal. It handles different modes such as input and output.

It uses termios structure.

struct termios {
	tcflag_t c_iflag;
	tcflag_t c_oflag;
	tcflag_t c_cflag;
	tcflag_t c_lflag;
	cc_t  c_cc[NCCS];
	speed_t c_ispeed;
	speed_t c_ospeed;
};
  1. c_iflag: input mode flags

  2. c_oflag: output mode flags

  3. c_cflag: control mode flags

  4. c_lflag: local mode flags

  5. c_cc: control characters

  6. c_ispeed: input baud rate

  7. c_ospeed: output baud rate

Functions

  1. tcgetattr(file_descriptor, &termios): gets the parameters associated with the terminal

  2. tcsetattr(file_descriptor, optional_actions, &termios): sets the parameters associated with the terminal

  3. tcdrain(file_descriptor): waits until all output written to the file descriptor has been transmitted

  4. tcflush(file_descriptor, queue_selector): discards data written to the file descriptor but not transmitted or data received but not read

  5. tcflow(file_descriptor, action): suspends or restarts transmission or reception of data

  6. cfgetispeed(&termios): gets the input baud rate stored in the termios structure

  7. cfgetospeed(&termios): gets the output baud rate stored in the termios structure

  8. cfsetispeed(&termios, baud_rate): sets the input baud rate stored in the termios structure

  9. cfsetospeed(&termios, baud_rate): sets the output baud rate stored in the termios structure

sys/ioctl.h

ioctl is a system call that manipulates the underlying device parameters of special files. It is used to control the terminal.

Special sequences

  1. \e[2J: clears the screen

  2. \e[%a;%b: moves the cursor to the position specified by a and b, where a is the row and b is the column

  3. \e[?25l: hides the cursor

  4. \e[?25h: shows the cursor

  5. \e[1m: sets the text to bold

  6. \e[0m: sets the text to normal

Arrow, page up, page down, home, end keys

Above mentioned keys are sent as a sequence of characters. First two characters are \e[ and the third character is one of the following:

  1. A: up arrow

  2. B: down arrow

  3. C: right arrow

  4. D: left arrow

  5. H: home

  6. F: end

  7. 5: page up

  8. 6: page down