Repository navigation
Expand file tree
/
Copy pathterminal.c
More file actions
123 lines (93 loc) · 2.25 KB
/
Copy pathterminal.c
File metadata and controls
123 lines (93 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* terminal.c - ввод/вывод терминала */
#include "stack.h"
#include <termio.h>
static struct termio tty_mode; /* save tty mode here */
static int previous_key; /* save key */
/*
* Put a terminal device into RAW mode with ECHO off.
* Before doing so we first save the terminal's current mode,
* assuming the caller will call the tty_reset() function
* when it's done with raw mode.
*/
int tty_raw()
{
struct termio temp_mode;
if(ioctl(0, TCGETA, (char *)&temp_mode) < 0)
return(-1);
tty_mode = temp_mode; /* save for restoring later */
temp_mode.c_iflag = 0; /* turn off all input control */
temp_mode.c_oflag &= ~OPOST; /* disable output post-processing */
temp_mode.c_lflag &= ~(ISIG | ICANON | ECHO | XCASE);
/* disable signal generation */
/* disable canonical input */
/* disable echo */
/* disable upper/lower output */
temp_mode.c_cflag &= ~(CSIZE | PARENB);
/* clear char size, disable parity */
temp_mode.c_cflag |= CS8; /* 8-bit chars */
temp_mode.c_cc[VMIN] = 0; /* min #chars to satisfy read */
temp_mode.c_cc[VTIME] = 0; /* 10'ths of seconds between chars */
if(ioctl(0, TCSETA, (char *)&temp_mode) < 0)
return(-1);
previous_key = -1;
return 0;
}
/*
* Restore a terminal's mode to whatewer it was on the most
* recent call to the tty_raw() function above.
*/
int tty_reset()
{
if(ioctl(0, TCSETA, (char *)&tty_mode) < 0)
return(-1);
return 0;
}
int getchr() /* Get a char without waiting */
{
unsigned char xchr[1];
if(read(0, xchr, 1) != 1)
return(-1);
return xchr[0];
}
void key() /* (key) ->c */
{
int chr;
if(previous_key != -1)
{
PushSingle(previous_key);
previous_key = -1;
return;
}
while((chr = getchr()) == -1);
PushSingle(chr);
}
void qkey() /* ?key ->f */
{
int chr;
if(previous_key != -1)
{
PushSingle(-1); /* true */
return;
}
if((chr = getchr()) != -1)
{
previous_key = chr; /* save char for KEY operation */
PushSingle(-1); /* true */
return;
}
PushSingle(0); /* false */
}
void emit() /* (emit) c-> */
{
SINGLE a;
a = PopSingle();
fputc(a, stdout);
}
void cr() /* (cr) -> */
{
fputc('\n', stdout);
}
void fflushstdout() /* fflush -> */
{
fflush(stdout);
}