-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyserv.c
214 lines (194 loc) · 5.23 KB
/
keyserv.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
########################################################################
# This file is part of the minicom communications package for WRAMP.
#
# Copyright 1991-1995 Miquel van Smoorenburg.
# Copyright (C) 2019 The University of Waikato, Hamilton, New Zealand.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
*/
#include "port.h"
#include "remote.h"
/* Emulation modes */
#define EVT100 1
#define EANSI 2
/* Pipe file descriptors */
#define from_remote 3
#define to_remote 4
/* Set modes to normal */
int keypadmode = NORMAL;
int cursormode = NORMAL;
char *_tptr = (char *)NULL; /* Pointer to termcap information */
int mode = EVT100; /* Emulation mode selector */
int parent; /* Process ID of minicom */
jmp_buf mainloop; /* To jump to after a 'HELLO' signal */
char **escseq; /* Translation table to use */
static int argument; /* Argument to 'HELLO' command */
static int esc_char = 1; /* Escape character (initially ^A) */
static int bs_code = 8; /* Code that backspace key sends */
extern int pendingkeys; /* From wkeys.c */
char *st_vtesc[] = {
"", "\033OP", "\033OQ", "\033OR", "\033OS", "", "", "", "", "", "",
"\033[H", "", "\033[A", "\033[D", "\033[C", "\033[B", "\033[K", "",
"", "\177" };
char *app_vtesc[] = {
"", "\033OP", "\033OQ", "\033OR", "\033OS", "", "", "", "", "", "",
"\033[H", "", "\033OA", "\033OD", "\033OC", "\033OB", "\033[K", "",
"", "\177" };
char *ansiesc[] = {
"", "\033OP", "\033OQ", "\033OR", "\033OS", "\033OT", "\033OU", "\033OV",
"\033OW", "\033OX", "\033OY", "\033[H", "\033[V", "\033[A", "\033[D",
"\033[C", "\033[B", "\033[Y", "\033[U", "0", "\177" };
/*
* We got a signal. This means that there is some information for us.
* Read it, and jump to the main loop.
*/
/*ARGSUSED*/
void handler(dummy)
int dummy;
{
unsigned char buf[8];
int n;
signal(HELLO, handler);
n = read(from_remote, buf, 8);
if (n <= 0) {
n = 2;
buf[0] = 0;
}
if (n % 2 == 1) {
n++;
read(from_remote, buf + n, 1);
}
argument = buf[n-1];
longjmp(mainloop, (int)buf[n - 2]);
}
/*
* Send a string to the modem
*/
void sendstr(s)
char *s;
{
write(1, s, strlen(s));
}
/*
* Main program of keyserv.
*/
int main(argc, argv)
int argc;
char **argv;
{
int c;
char ch;
int f, fun;
int stopped = 0;
parent = atoi(argv[1]);
/* Initialize signal handlers */
/* signal(SIGHUP, SIG_IGN); */
signal(SIGQUIT, SIG_IGN);
signal(SIGINT, SIG_IGN);
#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
#endif
signal(HELLO, handler);
/* Set up escape sequence table */
escseq = st_vtesc;
/* Cbreak, no echo (remote itself sets to raw if needed) */
(void) setcbreak(1);
if ((fun = setjmp(mainloop)) != 0) {
switch(fun) {
/* We come here after remote has told us something */
case KVT100: /* VT100 keyboard */
mode = EVT100;
escseq = st_vtesc;
break;
case KANSI: /* ANSI keyboard */
mode = EANSI;
escseq = ansiesc;
break;
case KKPST: /* Keypad in standard mode, not used */
keypadmode = NORMAL;
break;
case KKPAPP: /* Keypad in applications mode, not used */
keypadmode = APPL;
break;
case KCURST: /* Standard cursor keys */
cursormode = NORMAL;
if (mode == EVT100)
escseq = st_vtesc;
break;
case KCURAPP: /* cursor keys in applications mode */
cursormode = APPL;
if (mode == EVT100)
escseq = app_vtesc;
break;
case KSTOP: /* Sleep until further notice */
stopped = 1;
break;
case KSIGIO: /* Wait for keypress and tell parent */
kill(parent, ACK);
f = read(0, &ch, 1);
if (f == 1) {
write(to_remote, &ch, 1);
(void) kill(parent, HELLO);
}
break;
case KSTART: /* Restart when stopped */
stopped = 0;
break;
case KSETBS: /* Set code that BS key sends */
bs_code = argument;
break;
case KSETESC: /* Set escape character */
esc_char = argument;
break;
default:
break;
}
if (fun != KSIGIO) {
kill(parent, ACK);
}
}
/* Wait if stopped */
if (stopped) pause();
/* Main loop: read keyboard, send to modem */
while(1) {
c = wxgetch();
if (c > 256 && c < 256 + NUM_KEYS) {
sendstr(escseq[c - 256]);
}
if (c < 256) {
if (c == K_ERA) c = bs_code;
ch = c;
/* Test for escape characters */
if (c == esc_char || (esc_char == 128 && c > 128)) {
/* If we typed too fast, and the escape sequence
* was not that of a function key, the next key
* is already in the buffer.
*/
if (c == esc_char && pendingkeys > 0) {
ch = wxgetch();
}
write(to_remote, &ch, 1);
(void) kill(parent, HELLO);
} else {
write(1, &ch, 1);
}
}
}
/*NOTREACHED*/
return(0);
}