CSC112 Spring 2016 Examples
/*************************************************************************\1* Copyright (C) Michael Kerrisk, 2015. *2* *3* This program is free software. You may use, modify, and redistribute it *4* under the terms of the GNU Lesser General Public License as published *5* by the Free Software Foundation, either version 3 or (at your option) *6* any later version. This program is distributed without any warranty. *7* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *8\*************************************************************************/9/*10* Revision: $Revision: 1.1 $11* Change Log12* $Log: tty_functions.cpp,v $13* Revision 1.1 2016/03/17 16:01:38 pngwen14* Initial revision15*16*/1718/* This file contains an additional function written by Robert Lowe19[email protected] */2021/* Listing 62-3 */2223/* tty_functions.c2425Implement ttySetCbreak() and ttySetRaw().26*/27#include <termios.h>28#include <unistd.h>29#include <sys/ioctl.h>30#include "tty_functions.h" /* Declares functions defined here */3132/* Place terminal referred to by 'fd' in cbreak mode (noncanonical mode33with echoing turned off). This function assumes that the terminal is34currently in cooked mode (i.e., we shouldn't call it if the terminal35is currently in raw mode, since it does not undo all of the changes36made by the ttySetRaw() function below). Return 0 on success, or -137on error. If 'prevTermios' is non-NULL, then use the buffer to which38it points to return the previous terminal settings. */3940int41ttySetCbreak(int fd, struct termios *prevTermios)42{43struct termios t;4445if (tcgetattr(fd, &t) == -1)46return -1;4748if (prevTermios != NULL)49*prevTermios = t;5051t.c_lflag &= ~(ICANON | ECHO);52t.c_lflag |= ISIG;5354t.c_iflag &= ~ICRNL;5556t.c_cc[VMIN] = 1; /* Character-at-a-time input */57t.c_cc[VTIME] = 0; /* with blocking */5859if (tcsetattr(fd, TCSAFLUSH, &t) == -1)60return -1;6162return 0;63}6465/* Place terminal referred to by 'fd' in raw mode (noncanonical mode66with all input and output processing disabled). Return 0 on success,67or -1 on error. If 'prevTermios' is non-NULL, then use the buffer to68which it points to return the previous terminal settings. */6970int71ttySetRaw(int fd, struct termios *prevTermios)72{73struct termios t;7475if (tcgetattr(fd, &t) == -1)76return -1;7778if (prevTermios != NULL)79*prevTermios = t;8081t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);82/* Noncanonical mode, disable signals, extended83input processing, and echoing */8485t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |86INPCK | ISTRIP | IXON | PARMRK);87/* Disable special handling of CR, NL, and BREAK.88No 8th-bit stripping or parity error handling.89Disable START/STOP output flow control. */9091t.c_oflag &= ~OPOST; /* Disable all output processing */9293t.c_cc[VMIN] = 1; /* Character-at-a-time input */94t.c_cc[VTIME] = 0; /* with blocking */9596if (tcsetattr(fd, TCSAFLUSH, &t) == -1)97return -1;9899return 0;100}101102103ttySize104ttyGetSize(int fd) {105struct winsize ws;106ttySize size;107108/* try to retrieve the size */109if(ioctl(fd, TIOCGWINSZ, &ws) == -1) {110size.rows = -1;111size.cols = -1;112return size;113}114115/* get the size and return */116size.rows = ws.ws_row;117size.cols = ws.ws_col;118119return size;120}121122123