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/* This file contains an additional function written by Robert Lowe10[email protected] */1112/* Listing 62-3 */1314/* tty_functions.c1516Implement ttySetCbreak() and ttySetRaw().17*/18#include <termios.h>19#include <unistd.h>20#include <sys/ioctl.h>21#include "tty_functions.h" /* Declares functions defined here */2223/* Place terminal referred to by 'fd' in cbreak mode (noncanonical mode24with echoing turned off). This function assumes that the terminal is25currently in cooked mode (i.e., we shouldn't call it if the terminal26is currently in raw mode, since it does not undo all of the changes27made by the ttySetRaw() function below). Return 0 on success, or -128on error. If 'prevTermios' is non-NULL, then use the buffer to which29it points to return the previous terminal settings. */3031int32ttySetCbreak(int fd, struct termios *prevTermios)33{34struct termios t;3536if (tcgetattr(fd, &t) == -1)37return -1;3839if (prevTermios != NULL)40*prevTermios = t;4142t.c_lflag &= ~(ICANON | ECHO);43t.c_lflag |= ISIG;4445t.c_iflag &= ~ICRNL;4647t.c_cc[VMIN] = 1; /* Character-at-a-time input */48t.c_cc[VTIME] = 0; /* with blocking */4950if (tcsetattr(fd, TCSAFLUSH, &t) == -1)51return -1;5253return 0;54}5556/* Place terminal referred to by 'fd' in raw mode (noncanonical mode57with all input and output processing disabled). Return 0 on success,58or -1 on error. If 'prevTermios' is non-NULL, then use the buffer to59which it points to return the previous terminal settings. */6061int62ttySetRaw(int fd, struct termios *prevTermios)63{64struct termios t;6566if (tcgetattr(fd, &t) == -1)67return -1;6869if (prevTermios != NULL)70*prevTermios = t;7172t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);73/* Noncanonical mode, disable signals, extended74input processing, and echoing */7576t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |77INPCK | ISTRIP | IXON | PARMRK);78/* Disable special handling of CR, NL, and BREAK.79No 8th-bit stripping or parity error handling.80Disable START/STOP output flow control. */8182t.c_oflag &= ~OPOST; /* Disable all output processing */8384t.c_cc[VMIN] = 1; /* Character-at-a-time input */85t.c_cc[VTIME] = 0; /* with blocking */8687if (tcsetattr(fd, TCSAFLUSH, &t) == -1)88return -1;8990return 0;91}929394ttySize95ttyGetSize(int fd) {96struct winsize ws;97ttySize size;9899/* try to retrieve the size */100if(ioctl(fd, TIOCGWINSZ, &ws) == -1) {101size.rows = -1;102size.cols = -1;103return size;104}105106/* get the size and return */107size.rows = ws.ws_row;108size.cols = ws.ws_col;109110return size;111}112113114