-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtj1check.c
188 lines (175 loc) · 5.75 KB
/
vtj1check.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* vtj1check.c - Check to see if the current terminal is a VTJ-1
* Probably not as portable as I'd like, but compiles and seems to run ok
* under both Linux and NetBSD 7.
*
* Copyright (c) 2017 Jeremy Dilatush
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JEREMY DILATUSH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JEREMY DILATUSH OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
static int timedout_getchar(int i);
/* vtj1check() - Check to see if the terminal (accessed via input file
* descriptor 'i' and output file pointer 'o') is a VTJ1. Returns 1 if so,
* 0 if not or in case of error.
*/
int
vtj1check(int i, int o)
{
const char *query = "\033[0;86c"; /* query string */
const char *xresp1 = "=86;84;74;"; /* part of expected response */
int p, rv, l, ch, ok_so_far = 1;
struct termios tios, tios2;
int tios_valid = 0;
/* alter terminal state into non-canonical mode */
memset(&tios, 0, sizeof(tios));
if (tcgetattr(i, &tios) >= 0) tios_valid = 1;
if (tios_valid) {
tios2 = tios;
tios2.c_lflag &= ~ICANON;
tios2.c_cc[VMIN] = 1;
tios2.c_cc[VTIME] = 2;
tios2.c_lflag &= ~ECHO;
tcsetattr(i, TCSANOW, &tios2);
}
/* send the query string to the terminal */
l = strlen(query);
for (p = 0; p < l; ) {
rv = write(o, query + p, l - p);
if (rv < 0) {
if (errno == EAGAIN || errno == EINTR) {
/* transient error, try again */
continue;
}
ok_so_far = 0;
goto vtj1check_done;
} else if (rv == 0) {
ok_so_far = 0;
goto vtj1check_done;
}
p += rv;
}
/* Get a response, but don't wait forever. Complicated by the need
* to recognize a valid response that isn't the one we expected.
* See also ECMA-48 section 5.4.
*/
/* read CSI: 27 91 or 155 */
ch = timedout_getchar(i);
if (ch < 0) {
ok_so_far = 0;
goto vtj1check_done;
}
if (ch == 27) {
ch = timedout_getchar(i);
if (ch != 91) {
ok_so_far = 0;
goto vtj1check_done;
}
} else if (ch != 155) {
ok_so_far = 0;
goto vtj1check_done;
}
/* process one or more 'P' bytes (48-63); in our case this should start
* with xresp1; 'p' counts position therein, and 'ok_so_far' keeps
* track of whether we've seen anything very wrong.
*/
p = 0;
for (;;) {
ch = timedout_getchar(i);
if (ch < 0) {
ok_so_far = 0;
goto vtj1check_done;
}
if (ch < 48 || ch >= 64) break;
if (xresp1[p] && xresp1[p] != ch) ok_so_far = 0;
if (xresp1[p]) p++;
}
/* process zero or more 'I' bytes (32-47); not expecting any */
for (;;) {
if (ch < 32 || ch >= 48) break;
ok_so_far = 0;
ch = timedout_getchar(i);
if (ch < 0) {
goto vtj1check_done;
}
}
/* process the 'F' byte (64-126); ours should be 'c' (99) */
if (ch != 99) {
ok_so_far = 0;
goto vtj1check_done;
}
vtj1check_done:
if (tios_valid) {
tios2 = tios;
tcsetattr(i, TCSANOW, &tios2);
}
return(ok_so_far);
}
/* timedout_getchar() - read a character but don't wait forever */
static int
timedout_getchar(int i)
{
struct timeval tv;
fd_set rfds;
int rv;
unsigned char ch;
for (;;) {
/* The use of select() here may be unnecessary with the termios
* settings made in vtj1check().
*/
FD_ZERO(&rfds);
FD_SET(i, &rfds);
tv.tv_usec = 250000; /* time to wait without anything happening */
tv.tv_sec = 0;
rv = select(i + 1, &rfds, NULL, NULL, &tv);
if (rv < 0) {
if (errno == EINTR) {
continue; /* try again */
} else {
return(-1); /* error */
}
} else if (rv == 0) {
return(-1); /* timed out */
} else {
rv = read(i, &ch, sizeof(ch));
if (rv < 0) {
if (errno == EINTR || errno == EAGAIN) {
continue; /* try again */
} else {
return(-1); /* error */
}
} else if (rv == 0) {
return(-1); /* no more bytes to get */
} else {
return(ch); /* got it! */
}
}
/* shouldn't be reached, but try to avoid an infinite loop */
return(-1);
}
}