title: Building a text editor. Reading character from terminal in raw mode
body: <p>I'm following the tutorial <a href="https://viewsourcecode.org/snaptoken/kilo/03.rawInputAndOutput.html" rel="nofollow noreferrer">Build Your Own Text Editor</a> on how to program your own text editor.</p>
  <p>I am currently at the low-level keypress reading and mapping it to editor operations(Section: Refactoring keyboard input). But <strong>when I implement the same</strong> in my code and run it, <strong>my terminal just freezes up</strong> and I've to restart to get back to normal. I'm using Parrot OS(if that matters). Here is the relevant section of my source code:</p>
  <pre><code>char editorReadKey() {
      int nread;
      char c;
      while ((nread = read(STDIN_FILENO, &amp;c, 1) != 1)) {
          if (nread == -1 &amp;&amp; errno != EAGAIN)
              die(&quot;read&quot;);
          /*if (iscntrl(c))
              printf(&quot;%d\r\n&quot;, c);
          else
              printf(&quot;%d('%c')\r\n&quot;, c, c);
          */
      }
      return c;
  
  }
  void editorProcessKeypress() {
      char c = editorReadKey();
      switch (c) {
          case CTRL_KEY('q') : exit(0);
                               break;
      }
  }
  int main(void) {
      enableRawMode();
      while (1) {
              editorProcessKeypress();
      }
      return 0;
    
  </code></pre>
  <p>The functions <strong>enableRawMode</strong> and <strong>disableRawMode</strong> aren't different and more or less are the exact implementation of the tutorial.</p>
  <pre><code>void disableRawMode() {
      if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &amp;orig_termios) == -1)
          die(&quot;tcsetattr&quot;);
  }
  void enableRawMode() {
      if(tcgetattr(STDIN_FILENO, &amp;orig_termios) == -1)
          die(&quot;tcgetattr&quot;);
      atexit(disableRawMode);
      struct termios raw = orig_termios;
      tcgetattr(STDIN_FILENO, &amp;raw);
      //turns off echoing the command on the screen
      //turn off cannonical mode
      raw.c_iflag &amp;= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
      raw.c_oflag &amp;= ~(OPOST);
      raw.c_cflag |= (CS8);
      raw.c_lflag &amp;= ~(ECHO | ICANON | IEXTEN | ISIG);
      raw.c_cc[VMIN] = 0;
      raw.c_cc[VTIME] = 1;
      if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &amp;raw) == -1)
          die(&quot;tcsetattr&quot;);
  }
  </code></pre>
  <p>Any reasons as to why that might be happening? Or am I doing something wrong?</p>
  <p><strong>EDIT:</strong> Added the return value to <code>editorReadKey()</code> which I had  somehow missed putting it here.</p>