Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
/*************************************************************************\
2
* Copyright (C) Michael Kerrisk, 2015. *
3
* *
4
* This program is free software. You may use, modify, and redistribute it *
5
* under the terms of the GNU Lesser General Public License as published *
6
* by the Free Software Foundation, either version 3 or (at your option) *
7
* any later version. This program is distributed without any warranty. *
8
* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *
9
\*************************************************************************/
10
/*
11
* Revision: $Revision: 1.1 $
12
* Change Log
13
* $Log: tty_functions.cpp,v $
14
* Revision 1.1 2016/03/17 16:01:38 pngwen
15
* Initial revision
16
*
17
*/
18
19
/* This file contains an additional function written by Robert Lowe
20
[email protected] */
21
22
/* Listing 62-3 */
23
24
/* tty_functions.c
25
26
Implement ttySetCbreak() and ttySetRaw().
27
*/
28
#include <termios.h>
29
#include <unistd.h>
30
#include <sys/ioctl.h>
31
#include "tty_functions.h" /* Declares functions defined here */
32
33
/* Place terminal referred to by 'fd' in cbreak mode (noncanonical mode
34
with echoing turned off). This function assumes that the terminal is
35
currently in cooked mode (i.e., we shouldn't call it if the terminal
36
is currently in raw mode, since it does not undo all of the changes
37
made by the ttySetRaw() function below). Return 0 on success, or -1
38
on error. If 'prevTermios' is non-NULL, then use the buffer to which
39
it points to return the previous terminal settings. */
40
41
int
42
ttySetCbreak(int fd, struct termios *prevTermios)
43
{
44
struct termios t;
45
46
if (tcgetattr(fd, &t) == -1)
47
return -1;
48
49
if (prevTermios != NULL)
50
*prevTermios = t;
51
52
t.c_lflag &= ~(ICANON | ECHO);
53
t.c_lflag |= ISIG;
54
55
t.c_iflag &= ~ICRNL;
56
57
t.c_cc[VMIN] = 1; /* Character-at-a-time input */
58
t.c_cc[VTIME] = 0; /* with blocking */
59
60
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
61
return -1;
62
63
return 0;
64
}
65
66
/* Place terminal referred to by 'fd' in raw mode (noncanonical mode
67
with all input and output processing disabled). Return 0 on success,
68
or -1 on error. If 'prevTermios' is non-NULL, then use the buffer to
69
which it points to return the previous terminal settings. */
70
71
int
72
ttySetRaw(int fd, struct termios *prevTermios)
73
{
74
struct termios t;
75
76
if (tcgetattr(fd, &t) == -1)
77
return -1;
78
79
if (prevTermios != NULL)
80
*prevTermios = t;
81
82
t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
83
/* Noncanonical mode, disable signals, extended
84
input processing, and echoing */
85
86
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
87
INPCK | ISTRIP | IXON | PARMRK);
88
/* Disable special handling of CR, NL, and BREAK.
89
No 8th-bit stripping or parity error handling.
90
Disable START/STOP output flow control. */
91
92
t.c_oflag &= ~OPOST; /* Disable all output processing */
93
94
t.c_cc[VMIN] = 1; /* Character-at-a-time input */
95
t.c_cc[VTIME] = 0; /* with blocking */
96
97
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
98
return -1;
99
100
return 0;
101
}
102
103
104
ttySize
105
ttyGetSize(int fd) {
106
struct winsize ws;
107
ttySize size;
108
109
/* try to retrieve the size */
110
if(ioctl(fd, TIOCGWINSZ, &ws) == -1) {
111
size.rows = -1;
112
size.cols = -1;
113
return size;
114
}
115
116
/* get the size and return */
117
size.rows = ws.ws_row;
118
size.cols = ws.ws_col;
119
120
return size;
121
}
122
123