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;
};
c_iflag
: input mode flagsc_oflag
: output mode flagsc_cflag
: control mode flagsc_lflag
: local mode flagsc_cc
: control charactersc_ispeed
: input baud ratec_ospeed
: output baud rate
Functions
tcgetattr(file_descriptor, &termios)
: gets the parameters associated with the terminaltcsetattr(file_descriptor, optional_actions, &termios)
: sets the parameters associated with the terminaltcdrain(file_descriptor)
: waits until all output written to the file descriptor has been transmittedtcflush(file_descriptor, queue_selector)
: discards data written to the file descriptor but not transmitted or data received but not readtcflow(file_descriptor, action)
: suspends or restarts transmission or reception of datacfgetispeed(&termios)
: gets the input baud rate stored in the termios structurecfgetospeed(&termios)
: gets the output baud rate stored in the termios structurecfsetispeed(&termios, baud_rate)
: sets the input baud rate stored in the termios structurecfsetospeed(&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
\e[2J
: clears the screen\e[%a;%b
: moves the cursor to the position specified bya
andb
, wherea
is the row andb
is the column\e[?25l
: hides the cursor\e[?25h
: shows the cursor\e[1m
: sets the text to bold\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:
A
: up arrowB
: down arrowC
: right arrowD
: left arrowH
: homeF
: end5
: page up6
: page down