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
/* This file contains an additional function written by Robert Lowe
11
[email protected] */
12
13
/* Listing 62-3 */
14
15
/* tty_functions.c
16
17
Implement ttySetCbreak() and ttySetRaw().
18
*/
19
#include <termios.h>
20
#include <unistd.h>
21
#include <sys/ioctl.h>
22
#include "tty_functions.h" /* Declares functions defined here */
23
24
/* Place terminal referred to by 'fd' in cbreak mode (noncanonical mode
25
with echoing turned off). This function assumes that the terminal is
26
currently in cooked mode (i.e., we shouldn't call it if the terminal
27
is currently in raw mode, since it does not undo all of the changes
28
made by the ttySetRaw() function below). Return 0 on success, or -1
29
on error. If 'prevTermios' is non-NULL, then use the buffer to which
30
it points to return the previous terminal settings. */
31
32
int
33
ttySetCbreak(int fd, struct termios *prevTermios)
34
{
35
struct termios t;
36
37
if (tcgetattr(fd, &t) == -1)
38
return -1;
39
40
if (prevTermios != NULL)
41
*prevTermios = t;
42
43
t.c_lflag &= ~(ICANON | ECHO);
44
t.c_lflag |= ISIG;
45
46
t.c_iflag &= ~ICRNL;
47
48
t.c_cc[VMIN] = 1; /* Character-at-a-time input */
49
t.c_cc[VTIME] = 0; /* with blocking */
50
51
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
52
return -1;
53
54
return 0;
55
}
56
57
/* Place terminal referred to by 'fd' in raw mode (noncanonical mode
58
with all input and output processing disabled). Return 0 on success,
59
or -1 on error. If 'prevTermios' is non-NULL, then use the buffer to
60
which it points to return the previous terminal settings. */
61
62
int
63
ttySetRaw(int fd, struct termios *prevTermios)
64
{
65
struct termios t;
66
67
if (tcgetattr(fd, &t) == -1)
68
return -1;
69
70
if (prevTermios != NULL)
71
*prevTermios = t;
72
73
t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
74
/* Noncanonical mode, disable signals, extended
75
input processing, and echoing */
76
77
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
78
INPCK | ISTRIP | IXON | PARMRK);
79
/* Disable special handling of CR, NL, and BREAK.
80
No 8th-bit stripping or parity error handling.
81
Disable START/STOP output flow control. */
82
83
t.c_oflag &= ~OPOST; /* Disable all output processing */
84
85
t.c_cc[VMIN] = 1; /* Character-at-a-time input */
86
t.c_cc[VTIME] = 0; /* with blocking */
87
88
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
89
return -1;
90
91
return 0;
92
}
93
94
95
ttySize
96
ttyGetSize(int fd) {
97
struct winsize ws;
98
ttySize size;
99
100
/* try to retrieve the size */
101
if(ioctl(fd, TIOCGWINSZ, &ws) == -1) {
102
size.rows = -1;
103
size.cols = -1;
104
return size;
105
}
106
107
/* get the size and return */
108
size.rows = ws.ws_row;
109
size.cols = ws.ws_col;
110
111
return size;
112
}
113
114