DEV Community

Tanishq Singla
Tanishq Singla

Posted on

Termios

If you love working with applications related to terminals, that depend on how you take the input, you may have came across the library termios.h. The purpose of the blog is to cover a small part of termios library for which I spent a good amount of time.

Termios

The termios functions describe a general terminal interface that is provided to control asynchronous communications ports.
The above is the description of termios as per the man page.
What termios lets you do is basically to set certain behaviors of terminal, sometimes for some terminal application you want don't want the default features of terminal so that your application can do what it does perfectly and the keyboard shortcuts or the input output processing don't ruin it.

Here are a bunch of things that you should know about termios.

struct termios

The struct termios acts as an interface between you and the terminal, termios struct has various functions and flags that let's us change how the terminal behave, some of which, we are going to discuss below.

tcgetattr() and tcsetattr()

These two functions are used to do what termios is meant to do. The tcgetattr() gets the current behavior of the terminal and tcsetattr() sets them.

Flags

Termios struct has four types of flags, these flags are basically bitflags.

  • Input flags
  • Output flags
  • Local flags
  • control flags As the name suggests input flags are flags that lets you control the inputs, similarly output flags lets you do the same for terminal outputs. To access input output flags we use c_iflag and c_oflag values in the termios struct Local flags controls the broad scale behavior, these flags are used to control various terminal functions. For local flags c_lflag value is used. Control flags controls the hardware interaction with the terminal, so most of them are pretty much irrelevant in the modern day. And finally for control flags c_cflags are used.

ECHO

Echo is a local flag, this flag is responsible for printing the output to the terminal. One famous example of this flag turned off is when you're typing passwords in a bash terminal, while typing the letters don't show on the terminal,that's because this flag is tuned off.
If you're wondering what are bitflags, well bitflags are usually unsigned 32-bit integers whose bits are taken as flags and turning the bits off or on you change different flags.

ICANON

ICANON flag is a local flag, the I stands for input here, that's because it deals with the mode of input, since mode of input is a core feature of a terminal this is a local flag. This flag allows you to set between canonical mode or raw mode.
In canonical mode the terminal takes input line by line, where as in raw mode it takes input byte by byte, the bash terminal by default is in canonical mode, you can notice that by creating a simple console application that waits for a user input and then exits itself, in that program you will only be able to exit the program after you press enter.

That's it for the blog, more content will be added, I am publishing this blog early so that if someone is finding the content above won't have problem finding it.

Top comments (0)