Blame manual/examples/termios.c

Packit Service 82fcde
/* Noncanonical Mode Example
Packit Service 82fcde
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit Service 82fcde
Packit Service 82fcde
   This program is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU General Public License
Packit Service 82fcde
   as published by the Free Software Foundation; either version 2
Packit Service 82fcde
   of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   This program is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 82fcde
   GNU General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU General Public License
Packit Service 82fcde
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit Service 82fcde
*/
Packit Service 82fcde
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <termios.h>
Packit Service 82fcde
Packit Service 82fcde
/* Use this variable to remember original terminal attributes. */
Packit Service 82fcde
Packit Service 82fcde
struct termios saved_attributes;
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
reset_input_mode (void)
Packit Service 82fcde
{
Packit Service 82fcde
  tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
set_input_mode (void)
Packit Service 82fcde
{
Packit Service 82fcde
  struct termios tattr;
Packit Service 82fcde
  char *name;
Packit Service 82fcde
Packit Service 82fcde
  /* Make sure stdin is a terminal. */
Packit Service 82fcde
  if (!isatty (STDIN_FILENO))
Packit Service 82fcde
    {
Packit Service 82fcde
      fprintf (stderr, "Not a terminal.\n");
Packit Service 82fcde
      exit (EXIT_FAILURE);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* Save the terminal attributes so we can restore them later. */
Packit Service 82fcde
  tcgetattr (STDIN_FILENO, &saved_attributes);
Packit Service 82fcde
  atexit (reset_input_mode);
Packit Service 82fcde
Packit Service 82fcde
/*@group*/
Packit Service 82fcde
  /* Set the funny terminal modes. */
Packit Service 82fcde
  tcgetattr (STDIN_FILENO, &tattr);
Packit Service 82fcde
  tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO.  */
Packit Service 82fcde
  tattr.c_cc[VMIN] = 1;
Packit Service 82fcde
  tattr.c_cc[VTIME] = 0;
Packit Service 82fcde
  tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
Packit Service 82fcde
}
Packit Service 82fcde
/*@end group*/
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
main (void)
Packit Service 82fcde
{
Packit Service 82fcde
  char c;
Packit Service 82fcde
Packit Service 82fcde
  set_input_mode ();
Packit Service 82fcde
Packit Service 82fcde
  while (1)
Packit Service 82fcde
    {
Packit Service 82fcde
      read (STDIN_FILENO, &c, 1);
Packit Service 82fcde
      if (c == '\004')		/* @kbd{C-d} */
Packit Service 82fcde
	break;
Packit Service 82fcde
      else
Packit Service 82fcde
	putchar (c);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  return EXIT_SUCCESS;
Packit Service 82fcde
}