0

I was trying to look into the termios structure in C. I wanted to know how the value to the several flags are assigned in the terminal. I could not understand how the c_cc array in the structure gets its value assigned to its various indexes. Is it through the input we give in the terminal. Or is there some other way the values are assigned to this variable.?

#include <termios.h>
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;
};

tcflag_t and speed_t are unsigned int, cc_t is unsigned char.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • *termios*? Would you have the code of interest to show? (not all! just the piece your question is about). – Déjà vu Dec 05 '17 at 06:50
  • @nalinkanoongo can you also show where & how it been assigned ? – ntshetty Dec 05 '17 at 06:56
  • See the POSIX specification of the [``](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/termios.h.html) header — _The `` header shall define the following symbolic constants for use as subscripts for the array `c_cc`:_ followed by a table of names such as `VEOF`, `VERASE`, `VINTR`, etc. The initial setup is done by the `login` program or thereabouts (possibly `getty`, possibly by a different process these days); subsequently, the values are manipulated by `stty` from the command line, and by programs such as `vim` or `emacs` (or Bash) when appropriate. – Jonathan Leffler Dec 05 '17 at 07:11
  • You use the [`tcgetattr()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetattr.html) to retrieve the terminal settings associated with the terminal associated with a specific descriptor, and [`tcsetattr()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcsetattr.html) to alter the values. – Jonathan Leffler Dec 05 '17 at 09:10
  • @JonathanLeffler Hi, I went through the link but as i see this link does tell me how the actual values of the structure variables are set. It provides me the possible values of the all the structure variables. Say for example i press "ENTER" on my terminal does this set some flags? If so how? That was my question – nalin kanoongo Dec 05 '17 at 11:15
  • There's an [XY Problem](http://mywiki.wooledge.org/XyProblem) here. The `termios` structures are used to control the behaviour of a terminal. They don't do I/O per se. These functions are not intended to be used to detect whether the Enter key is pressed; that is a job for `read()` and its friends. The `termios` attributes control whether you have to wait until Enter is pressed before any characters are sent to the program, or do eccentric things like map Enter key so it generates an interrupt signal. – Jonathan Leffler Dec 05 '17 at 15:16
  • @JonathanLeffler Exactly that was what i wanted to know. Now where can i find the signals generated when i press Enter or say ^S(Ctrl + S). And how are mapped in the function. Are the Ascii values used in the core code? – nalin kanoongo Dec 06 '17 at 06:46
  • I'm sorry, but you need to find a book on Unix (Linux) system calls and study the chapter(s) on terminal handling. The subject is impossibly broad for an SO question. No signal is generated at the user level when you press Enter or control-S; they're interpreted by the line discipline of the terminal driver (under normal circumstances). I'm not clear which function you think might be mapping them. ASCII values are mostly immaterial; there's a character code which is set, which is often ASCII, and usually a single byte (so UTF-8 presents problems). But you need to read about it elsewhere. – Jonathan Leffler Dec 06 '17 at 06:52
  • I suggest any of these 3 books (1 is enough to start with; you can get the others later): W Richard Stevens, Stephen A Rago [Advanced Programming in the Unix Environment, 3rd Edn](http://smile.amazon.com/Advanced-Programming-UNIX-Environment-Edition/dp/0321637739) — Marc J Rochkind [Advanced Unix Programming, 2nd Edn](http://smile.amazon.com/Advanced-UNIX-Programming-2nd-Edition/dp/0131411543) — Michael Kerrisk [The Linux Programming Interface: A Linux and Unix System Programming Handbook](http://smile.amazon.com/The-Linux-Programming-Interface-Handbook/dp/1593272200). – Jonathan Leffler Dec 06 '17 at 06:53
  • You could also look at [Canonical vs non-canonical terminal input](https://stackoverflow.com/questions/358342/) for some information. – Jonathan Leffler Dec 06 '17 at 06:56
  • @JonathanLeffler Thank You for help. I will go through the books for sure – nalin kanoongo Dec 06 '17 at 07:02

0 Answers0