Entering raw mode

Entering Raw Mode in C

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    struct termios orig_termios;
    tcgetattr(STDIN_FILENO, &orig_termios);
    struct termios raw = orig_termios;
    cfmakeraw(&raw);
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);

    char c;
    while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q');

    tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
    return 0;
}
  1. Include Necessary Libraries:
  2. Include the necessary libraries for handling terminal input and output in C.

  3. Main Function:

  4. Define the main function to contain the program logic.

  5. Retrieve Original Terminal Attributes:

  6. Retrieve the original terminal attributes using tcgetattr and store them in the orig_termios struct.

  7. Create Raw Terminal Attributes:

  8. Create a copy of the original terminal attributes and modify them to create raw mode using cfmakeraw.

  9. Set Raw Terminal Attributes:

  10. Set the raw terminal attributes using tcsetattr with the TCSAFLUSH option to apply the changes immediately.

  11. Read Input in Raw Mode:

  12. Read input character by character in a loop using read until the character 'q' is entered.

  13. Restore Original Terminal Attributes:

  14. Restore the original terminal attributes using tcsetattr to revert the terminal to its original mode.

  15. Return:

  16. Return 0 to indicate successful execution.