| @@ -1,582 +1,646 @@ | | | @@ -1,582 +1,646 @@ |
1 | /* $NetBSD: tip.c,v 1.62 2019/02/28 17:41:27 gson Exp $ */ | | 1 | /* $NetBSD: tip.c,v 1.63 2020/04/23 00:35:14 joerg Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1983, 1993 | | 4 | * Copyright (c) 1983, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the University nor the names of its contributors | | 15 | * 3. Neither the name of the University nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this software | | 16 | * may be used to endorse or promote products derived from this software |
17 | * without specific prior written permission. | | 17 | * without specific prior written permission. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | | 29 | * SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | #include <ctype.h> | | 33 | #include <ctype.h> |
34 | #include <libgen.h> | | 34 | #include <libgen.h> |
35 | | | 35 | |
36 | #ifndef lint | | 36 | #ifndef lint |
37 | __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ | | 37 | __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ |
38 | The Regents of the University of California. All rights reserved."); | | 38 | The Regents of the University of California. All rights reserved."); |
39 | #endif /* not lint */ | | 39 | #endif /* not lint */ |
40 | | | 40 | |
41 | #ifndef lint | | 41 | #ifndef lint |
42 | #if 0 | | 42 | #if 0 |
43 | static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93"; | | 43 | static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93"; |
44 | #endif | | 44 | #endif |
45 | __RCSID("$NetBSD: tip.c,v 1.62 2019/02/28 17:41:27 gson Exp $"); | | 45 | __RCSID("$NetBSD: tip.c,v 1.63 2020/04/23 00:35:14 joerg Exp $"); |
46 | #endif /* not lint */ | | 46 | #endif /* not lint */ |
47 | | | 47 | |
48 | /* | | 48 | /* |
49 | * tip - UNIX link to other systems | | 49 | * tip - UNIX link to other systems |
50 | * tip [-v] [-speed] system-name | | 50 | * tip [-v] [-speed] system-name |
51 | * or | | 51 | * or |
52 | * cu [options] [phone-number|"dir"] | | 52 | * cu [options] [phone-number|"dir"] |
53 | */ | | 53 | */ |
54 | #include "tip.h" | | 54 | #include "tip.h" |
55 | #include "pathnames.h" | | 55 | #include "pathnames.h" |
56 | | | 56 | |
| | | 57 | struct termios term; |
| | | 58 | struct termios defterm; |
| | | 59 | struct termios defchars; |
| | | 60 | |
| | | 61 | FILE *fscript; |
| | | 62 | |
| | | 63 | int attndes[2]; |
| | | 64 | int fildes[2]; |
| | | 65 | int repdes[2]; |
| | | 66 | int FD; |
| | | 67 | #ifndef __lint__ /* not used by hayes.c, but used by some other dialers */ |
| | | 68 | int AC; |
| | | 69 | #endif /*__lint__*/ |
| | | 70 | int sfd; |
| | | 71 | int pid; |
| | | 72 | uid_t uid, euid; |
| | | 73 | gid_t gid, egid; |
| | | 74 | int stop; |
| | | 75 | int quit; |
| | | 76 | int stoprompt; |
| | | 77 | int timedout; |
| | | 78 | int cumode; |
| | | 79 | int bits8; |
| | | 80 | #define STRIP_PAR (bits8 ? 0377 : 0177) |
| | | 81 | |
| | | 82 | char fname[80]; |
| | | 83 | char copyname[80]; |
| | | 84 | char ccc; |
| | | 85 | |
| | | 86 | int odisc; |
| | | 87 | int vflag; |
| | | 88 | |
| | | 89 | char *DV; |
| | | 90 | char *EL; |
| | | 91 | char *CM; |
| | | 92 | char *IE; |
| | | 93 | char *OE; |
| | | 94 | char *CU; |
| | | 95 | char *AT; |
| | | 96 | char *PN; |
| | | 97 | char *DI; |
| | | 98 | char *PA; |
| | | 99 | |
| | | 100 | char *PH; |
| | | 101 | char *RM; |
| | | 102 | char *HO; |
| | | 103 | |
| | | 104 | long BR; |
| | | 105 | long FS; |
| | | 106 | |
| | | 107 | long DU; |
| | | 108 | long HW; |
| | | 109 | char *ES; |
| | | 110 | char *EX; |
| | | 111 | char *FO; |
| | | 112 | char *RC; |
| | | 113 | char *RE; |
| | | 114 | char *PR; |
| | | 115 | long DL; |
| | | 116 | long CL; |
| | | 117 | long ET; |
| | | 118 | long HD; |
| | | 119 | char DC; |
| | | 120 | |
57 | __dead static void tipusage(void); | | 121 | __dead static void tipusage(void); |
58 | | | 122 | |
59 | int escape(void); | | 123 | int escape(void); |
60 | __dead static void intprompt(int); | | 124 | __dead static void intprompt(int); |
61 | __dead static void tipin(void); | | 125 | __dead static void tipin(void); |
62 | | | 126 | |
63 | char PNbuf[256]; /* This limits the size of a number */ | | 127 | char PNbuf[256]; /* This limits the size of a number */ |
64 | | | 128 | |
65 | static char path_phones[] = _PATH_PHONES; | | 129 | static char path_phones[] = _PATH_PHONES; |
66 | | | 130 | |
67 | int | | 131 | int |
68 | main(int argc, char *argv[]) | | 132 | main(int argc, char *argv[]) |
69 | { | | 133 | { |
70 | char *System = NULL; | | 134 | char *System = NULL; |
71 | int c, i; | | 135 | int c, i; |
72 | char *p; | | 136 | char *p; |
73 | const char *q; | | 137 | const char *q; |
74 | char sbuf[12]; | | 138 | char sbuf[12]; |
75 | int cmdlineBR; | | 139 | int cmdlineBR; |
76 | int fcarg; | | 140 | int fcarg; |
77 | | | 141 | |
78 | setprogname(argv[0]); | | 142 | setprogname(argv[0]); |
79 | gid = getgid(); | | 143 | gid = getgid(); |
80 | egid = getegid(); | | 144 | egid = getegid(); |
81 | uid = getuid(); | | 145 | uid = getuid(); |
82 | euid = geteuid(); | | 146 | euid = geteuid(); |
83 | if (strcmp(getprogname(), "cu") == 0) { | | 147 | if (strcmp(getprogname(), "cu") == 0) { |
84 | cumode = 1; | | 148 | cumode = 1; |
85 | cumain(argc, argv); | | 149 | cumain(argc, argv); |
86 | goto cucommon; | | 150 | goto cucommon; |
87 | } | | 151 | } |
88 | | | 152 | |
89 | if (argc > 4) | | 153 | if (argc > 4) |
90 | tipusage(); | | 154 | tipusage(); |
91 | | | 155 | |
92 | if (!isatty(0)) | | 156 | if (!isatty(0)) |
93 | errx(EXIT_FAILURE, "must be interactive"); | | 157 | errx(EXIT_FAILURE, "must be interactive"); |
94 | | | 158 | |
95 | cmdlineBR = 0; | | 159 | cmdlineBR = 0; |
96 | while((c = getopt(argc, argv, "v0123456789")) != -1) { | | 160 | while((c = getopt(argc, argv, "v0123456789")) != -1) { |
97 | switch(c) { | | 161 | switch(c) { |
98 | | | 162 | |
99 | case 'v': | | 163 | case 'v': |
100 | vflag++; | | 164 | vflag++; |
101 | break; | | 165 | break; |
102 | | | 166 | |
103 | case '0': case '1': case '2': case '3': case '4': | | 167 | case '0': case '1': case '2': case '3': case '4': |
104 | case '5': case '6': case '7': case '8': case '9': | | 168 | case '5': case '6': case '7': case '8': case '9': |
105 | cmdlineBR = cmdlineBR * 10 + (c - '0'); | | 169 | cmdlineBR = cmdlineBR * 10 + (c - '0'); |
106 | BR = cmdlineBR; | | 170 | BR = cmdlineBR; |
107 | break; | | 171 | break; |
108 | | | 172 | |
109 | default: | | 173 | default: |
110 | warnx("%s, unknown option", argv[1]); | | 174 | warnx("%s, unknown option", argv[1]); |
111 | break; | | 175 | break; |
112 | } | | 176 | } |
113 | } | | 177 | } |
114 | | | 178 | |
115 | argc -= optind; | | 179 | argc -= optind; |
116 | argv += optind; | | 180 | argv += optind; |
117 | | | 181 | |
118 | if (argc != 1) | | 182 | if (argc != 1) |
119 | tipusage(); | | 183 | tipusage(); |
120 | else | | 184 | else |
121 | System = argv[0]; | | 185 | System = argv[0]; |
122 | | | 186 | |
123 | | | 187 | |
124 | if (System == NULL) | | 188 | if (System == NULL) |
125 | goto notnumber; | | 189 | goto notnumber; |
126 | if (isalpha((unsigned char)*System)) | | 190 | if (isalpha((unsigned char)*System)) |
127 | goto notnumber; | | 191 | goto notnumber; |
128 | /* | | 192 | /* |
129 | * System name is really a phone number... | | 193 | * System name is really a phone number... |
130 | * Copy the number then stomp on the original (in case the number | | 194 | * Copy the number then stomp on the original (in case the number |
131 | * is private, we don't want 'ps' or 'w' to find it). | | 195 | * is private, we don't want 'ps' or 'w' to find it). |
132 | */ | | 196 | */ |
133 | if (strlen(System) > sizeof PNbuf - 1) { | | 197 | if (strlen(System) > sizeof PNbuf - 1) { |
134 | errx(1, "phone number too long (max = %d bytes)", | | 198 | errx(1, "phone number too long (max = %d bytes)", |
135 | (int)sizeof(PNbuf) - 1); | | 199 | (int)sizeof(PNbuf) - 1); |
136 | } | | 200 | } |
137 | (void)strlcpy(PNbuf, System, sizeof(PNbuf)); | | 201 | (void)strlcpy(PNbuf, System, sizeof(PNbuf)); |
138 | for (p = System; *p; p++) | | 202 | for (p = System; *p; p++) |
139 | *p = '\0'; | | 203 | *p = '\0'; |
140 | PN = PNbuf; | | 204 | PN = PNbuf; |
141 | (void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR); | | 205 | (void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR); |
142 | System = sbuf; | | 206 | System = sbuf; |
143 | | | 207 | |
144 | notnumber: | | 208 | notnumber: |
145 | (void)signal(SIGINT, cleanup); | | 209 | (void)signal(SIGINT, cleanup); |
146 | (void)signal(SIGQUIT, cleanup); | | 210 | (void)signal(SIGQUIT, cleanup); |
147 | (void)signal(SIGHUP, cleanup); | | 211 | (void)signal(SIGHUP, cleanup); |
148 | (void)signal(SIGTERM, cleanup); | | 212 | (void)signal(SIGTERM, cleanup); |
149 | | | 213 | |
150 | if ((i = hunt(System)) == 0) { | | 214 | if ((i = hunt(System)) == 0) { |
151 | errx(3, "all ports busy"); | | 215 | errx(3, "all ports busy"); |
152 | } | | 216 | } |
153 | if (i == -1) { | | 217 | if (i == -1) { |
154 | errx(3, "link down"); | | 218 | errx(3, "link down"); |
155 | } | | 219 | } |
156 | setbuf(stdout, NULL); | | 220 | setbuf(stdout, NULL); |
157 | | | 221 | |
158 | /* | | 222 | /* |
159 | * Kludge, their's no easy way to get the initialization | | 223 | * Kludge, their's no easy way to get the initialization |
160 | * in the right order, so force it here | | 224 | * in the right order, so force it here |
161 | */ | | 225 | */ |
162 | if ((PH = getenv("PHONES")) == NULL) | | 226 | if ((PH = getenv("PHONES")) == NULL) |
163 | PH = path_phones; | | 227 | PH = path_phones; |
164 | vinit(); /* init variables */ | | 228 | vinit(); /* init variables */ |
165 | setparity("none"); /* set the parity table */ | | 229 | setparity("none"); /* set the parity table */ |
166 | | | 230 | |
167 | /* | | 231 | /* |
168 | * Hardwired connections require the | | 232 | * Hardwired connections require the |
169 | * line speed set before they make any transmissions | | 233 | * line speed set before they make any transmissions |
170 | * (this is particularly true of things like a DF03-AC) | | 234 | * (this is particularly true of things like a DF03-AC) |
171 | */ | | 235 | */ |
172 | if (HW) { | | 236 | if (HW) { |
173 | if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) { | | 237 | if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) { |
174 | errx(3, "bad baud rate %d", | | 238 | errx(3, "bad baud rate %d", |
175 | (int)number(value(BAUDRATE))); | | 239 | (int)number(value(BAUDRATE))); |
176 | } | | 240 | } |
177 | } | | 241 | } |
178 | if ((q = tip_connect()) != NULL) { | | 242 | if ((q = tip_connect()) != NULL) { |
179 | errx(1, "\07%s\n[EOT]", q); | | 243 | errx(1, "\07%s\n[EOT]", q); |
180 | } | | 244 | } |
181 | if (!HW) { | | 245 | if (!HW) { |
182 | if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) { | | 246 | if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) { |
183 | errx(3, "bad baud rate %d", | | 247 | errx(3, "bad baud rate %d", |
184 | (int)number(value(BAUDRATE))); | | 248 | (int)number(value(BAUDRATE))); |
185 | } | | 249 | } |
186 | } | | 250 | } |
187 | | | 251 | |
188 | | | 252 | |
189 | cucommon: | | 253 | cucommon: |
190 | /* | | 254 | /* |
191 | * From here down the code is shared with | | 255 | * From here down the code is shared with |
192 | * the "cu" version of tip. | | 256 | * the "cu" version of tip. |
193 | */ | | 257 | */ |
194 | | | 258 | |
195 | /* | | 259 | /* |
196 | * Direct connections with no carrier require using O_NONBLOCK on | | 260 | * Direct connections with no carrier require using O_NONBLOCK on |
197 | * open, but we don't want to keep O_NONBLOCK after open because it | | 261 | * open, but we don't want to keep O_NONBLOCK after open because it |
198 | * will cause busy waits. | | 262 | * will cause busy waits. |
199 | */ | | 263 | */ |
200 | if (DC && | | 264 | if (DC && |
201 | ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 || | | 265 | ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 || |
202 | fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) { | | 266 | fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) { |
203 | err(1, "can't clear O_NONBLOCK"); | | 267 | err(1, "can't clear O_NONBLOCK"); |
204 | } | | 268 | } |
205 | | | 269 | |
206 | (void)tcgetattr(0, &defterm); | | 270 | (void)tcgetattr(0, &defterm); |
207 | term = defterm; | | 271 | term = defterm; |
208 | term.c_lflag &= ~(ICANON|IEXTEN|ECHO); | | 272 | term.c_lflag &= ~(ICANON|IEXTEN|ECHO); |
209 | term.c_iflag &= ~(INPCK|ICRNL); | | 273 | term.c_iflag &= ~(INPCK|ICRNL); |
210 | term.c_oflag &= ~OPOST; | | 274 | term.c_oflag &= ~OPOST; |
211 | term.c_cc[VMIN] = 1; | | 275 | term.c_cc[VMIN] = 1; |
212 | term.c_cc[VTIME] = 0; | | 276 | term.c_cc[VTIME] = 0; |
213 | defchars = term; | | 277 | defchars = term; |
214 | term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] = | | 278 | term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] = |
215 | term.c_cc[VDSUSP] = term.c_cc[VDISCARD] = | | 279 | term.c_cc[VDSUSP] = term.c_cc[VDISCARD] = |
216 | term.c_cc[VLNEXT] = _POSIX_VDISABLE; | | 280 | term.c_cc[VLNEXT] = _POSIX_VDISABLE; |
217 | raw(); | | 281 | raw(); |
218 | | | 282 | |
219 | (void)pipe(attndes); | | 283 | (void)pipe(attndes); |
220 | (void)pipe(fildes); | | 284 | (void)pipe(fildes); |
221 | (void)pipe(repdes); | | 285 | (void)pipe(repdes); |
222 | (void)signal(SIGALRM, alrmtimeout); | | 286 | (void)signal(SIGALRM, alrmtimeout); |
223 | | | 287 | |
224 | /* | | 288 | /* |
225 | * Everything's set up now: | | 289 | * Everything's set up now: |
226 | * connection established (hardwired or dialup) | | 290 | * connection established (hardwired or dialup) |
227 | * line conditioned (baud rate, mode, etc.) | | 291 | * line conditioned (baud rate, mode, etc.) |
228 | * internal data structures (variables) | | 292 | * internal data structures (variables) |
229 | * so, fork one process for local side and one for remote. | | 293 | * so, fork one process for local side and one for remote. |
230 | */ | | 294 | */ |
231 | (void)printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n"); | | 295 | (void)printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n"); |
232 | switch (pid = fork()) { | | 296 | switch (pid = fork()) { |
233 | default: | | 297 | default: |
234 | tipin(); | | 298 | tipin(); |
235 | break; | | 299 | break; |
236 | case 0: | | 300 | case 0: |
237 | tipout(); | | 301 | tipout(); |
238 | break; | | 302 | break; |
239 | case -1: | | 303 | case -1: |
240 | err(1, "can't fork"); | | 304 | err(1, "can't fork"); |
241 | } | | 305 | } |
242 | /*NOTREACHED*/ | | 306 | /*NOTREACHED*/ |
243 | exit(0); /* XXX: pacify gcc */ | | 307 | exit(0); /* XXX: pacify gcc */ |
244 | } | | 308 | } |
245 | | | 309 | |
246 | void | | 310 | void |
247 | tipusage(void) | | 311 | tipusage(void) |
248 | { | | 312 | { |
249 | (void)fprintf(stderr, "usage: %s [-v] [-speed] system-name\n", | | 313 | (void)fprintf(stderr, "usage: %s [-v] [-speed] system-name\n", |
250 | getprogname()); | | 314 | getprogname()); |
251 | (void)fprintf(stderr, " %s [-v] [-speed] phone-number\n", | | 315 | (void)fprintf(stderr, " %s [-v] [-speed] phone-number\n", |
252 | getprogname()); | | 316 | getprogname()); |
253 | exit(1); | | 317 | exit(1); |
254 | } | | 318 | } |
255 | | | 319 | |
256 | void | | 320 | void |
257 | /*ARGSUSED*/ | | 321 | /*ARGSUSED*/ |
258 | cleanup(int dummy __unused) | | 322 | cleanup(int dummy __unused) |
259 | { | | 323 | { |
260 | | | 324 | |
261 | if (odisc) | | 325 | if (odisc) |
262 | (void)ioctl(0, TIOCSETD, &odisc); | | 326 | (void)ioctl(0, TIOCSETD, &odisc); |
263 | _exit(0); | | 327 | _exit(0); |
264 | } | | 328 | } |
265 | | | 329 | |
266 | /* | | 330 | /* |
267 | * put the controlling keyboard into raw mode | | 331 | * put the controlling keyboard into raw mode |
268 | */ | | 332 | */ |
269 | void | | 333 | void |
270 | raw(void) | | 334 | raw(void) |
271 | { | | 335 | { |
272 | | | 336 | |
273 | (void)tcsetattr(0, TCSADRAIN, &term); | | 337 | (void)tcsetattr(0, TCSADRAIN, &term); |
274 | } | | 338 | } |
275 | | | 339 | |
276 | | | 340 | |
277 | /* | | 341 | /* |
278 | * return keyboard to normal mode | | 342 | * return keyboard to normal mode |
279 | */ | | 343 | */ |
280 | void | | 344 | void |
281 | unraw(void) | | 345 | unraw(void) |
282 | { | | 346 | { |
283 | | | 347 | |
284 | (void)tcsetattr(0, TCSADRAIN, &defterm); | | 348 | (void)tcsetattr(0, TCSADRAIN, &defterm); |
285 | } | | 349 | } |
286 | | | 350 | |
287 | static jmp_buf promptbuf; | | 351 | static jmp_buf promptbuf; |
288 | | | 352 | |
289 | /* | | 353 | /* |
290 | * Print string ``s'', then read a string | | 354 | * Print string ``s'', then read a string |
291 | * in from the terminal. Handles signals & allows use of | | 355 | * in from the terminal. Handles signals & allows use of |
292 | * normal erase and kill characters. | | 356 | * normal erase and kill characters. |
293 | */ | | 357 | */ |
294 | int | | 358 | int |
295 | prompt(const char *s, char *volatile p, size_t l) | | 359 | prompt(const char *s, char *volatile p, size_t l) |
296 | { | | 360 | { |
297 | int c; | | 361 | int c; |
298 | char *b = p; | | 362 | char *b = p; |
299 | sig_t oint, oquit; | | 363 | sig_t oint, oquit; |
300 | | | 364 | |
301 | stoprompt = 0; | | 365 | stoprompt = 0; |
302 | oint = signal(SIGINT, intprompt); | | 366 | oint = signal(SIGINT, intprompt); |
303 | oquit = signal(SIGQUIT, SIG_IGN); | | 367 | oquit = signal(SIGQUIT, SIG_IGN); |
304 | unraw(); | | 368 | unraw(); |
305 | (void)printf("%s", s); | | 369 | (void)printf("%s", s); |
306 | if (setjmp(promptbuf) == 0) | | 370 | if (setjmp(promptbuf) == 0) |
307 | while ((c = getchar()) != EOF && (*p = c) != '\n' && | | 371 | while ((c = getchar()) != EOF && (*p = c) != '\n' && |
308 | b + l > p) | | 372 | b + l > p) |
309 | p++; | | 373 | p++; |
310 | *p = '\0'; | | 374 | *p = '\0'; |
311 | | | 375 | |
312 | raw(); | | 376 | raw(); |
313 | (void)signal(SIGINT, oint); | | 377 | (void)signal(SIGINT, oint); |
314 | (void)signal(SIGQUIT, oquit); | | 378 | (void)signal(SIGQUIT, oquit); |
315 | return (stoprompt || p == b); | | 379 | return (stoprompt || p == b); |
316 | } | | 380 | } |
317 | | | 381 | |
318 | /* | | 382 | /* |
319 | * Interrupt service routine during prompting | | 383 | * Interrupt service routine during prompting |
320 | */ | | 384 | */ |
321 | static void | | 385 | static void |
322 | /*ARGSUSED*/ | | 386 | /*ARGSUSED*/ |
323 | intprompt(int dummy __unused) | | 387 | intprompt(int dummy __unused) |
324 | { | | 388 | { |
325 | | | 389 | |
326 | (void)signal(SIGINT, SIG_IGN); | | 390 | (void)signal(SIGINT, SIG_IGN); |
327 | stoprompt = 1; | | 391 | stoprompt = 1; |
328 | (void)printf("\r\n"); | | 392 | (void)printf("\r\n"); |
329 | longjmp(promptbuf, 1); | | 393 | longjmp(promptbuf, 1); |
330 | } | | 394 | } |
331 | | | 395 | |
332 | /* | | 396 | /* |
333 | * getchar() wrapper that checks for EOF on the local end. | | 397 | * getchar() wrapper that checks for EOF on the local end. |
334 | */ | | 398 | */ |
335 | static int | | 399 | static int |
336 | xgetchar(void) | | 400 | xgetchar(void) |
337 | { | | 401 | { |
338 | int c = getchar(); | | 402 | int c = getchar(); |
339 | if (__predict_false(c == EOF)) { | | 403 | if (__predict_false(c == EOF)) { |
340 | cleanup(SIGHUP); | | 404 | cleanup(SIGHUP); |
341 | /* NOTREACHED */ | | 405 | /* NOTREACHED */ |
342 | } | | 406 | } |
343 | | | 407 | |
344 | return c & STRIP_PAR; | | 408 | return c & STRIP_PAR; |
345 | } | | 409 | } |
346 | | | 410 | |
347 | | | 411 | |
348 | /* | | 412 | /* |
349 | * ****TIPIN TIPIN**** | | 413 | * ****TIPIN TIPIN**** |
350 | */ | | 414 | */ |
351 | static void | | 415 | static void |
352 | tipin(void) | | 416 | tipin(void) |
353 | { | | 417 | { |
354 | char gch, bol = 1; | | 418 | char gch, bol = 1; |
355 | | | 419 | |
356 | /* | | 420 | /* |
357 | * Kinda klugey here... | | 421 | * Kinda klugey here... |
358 | * check for scripting being turned on from the .tiprc file, | | 422 | * check for scripting being turned on from the .tiprc file, |
359 | * but be careful about just using setscript(), as we may | | 423 | * but be careful about just using setscript(), as we may |
360 | * send a SIGEMT before tipout has a chance to set up catching | | 424 | * send a SIGEMT before tipout has a chance to set up catching |
361 | * it; so wait a second, then setscript() | | 425 | * it; so wait a second, then setscript() |
362 | */ | | 426 | */ |
363 | if (boolean(value(SCRIPT))) { | | 427 | if (boolean(value(SCRIPT))) { |
364 | (void)sleep(1); | | 428 | (void)sleep(1); |
365 | setscript(); | | 429 | setscript(); |
366 | } | | 430 | } |
367 | | | 431 | |
368 | for (;;) { | | 432 | for (;;) { |
369 | gch = xgetchar(); | | 433 | gch = xgetchar(); |
370 | if ((gch == character(value(ESCAPE))) && bol) { | | 434 | if ((gch == character(value(ESCAPE))) && bol) { |
371 | if (!(gch = escape())) | | 435 | if (!(gch = escape())) |
372 | continue; | | 436 | continue; |
373 | } else if (!cumode && | | 437 | } else if (!cumode && |
374 | gch && gch == character(value(RAISECHAR))) { | | 438 | gch && gch == character(value(RAISECHAR))) { |
375 | setboolean(value(RAISE), !boolean(value(RAISE))); | | 439 | setboolean(value(RAISE), !boolean(value(RAISE))); |
376 | continue; | | 440 | continue; |
377 | } else if (gch == '\r' || gch == '\n') { | | 441 | } else if (gch == '\r' || gch == '\n') { |
378 | bol = 1; | | 442 | bol = 1; |
379 | xpwrite(FD, &gch, 1); | | 443 | xpwrite(FD, &gch, 1); |
380 | if (boolean(value(HALFDUPLEX))) | | 444 | if (boolean(value(HALFDUPLEX))) |
381 | (void)printf("%s\n", gch == '\r' ? "\r" : ""); | | 445 | (void)printf("%s\n", gch == '\r' ? "\r" : ""); |
382 | continue; | | 446 | continue; |
383 | } else if (!cumode && gch && gch == character(value(FORCE))) | | 447 | } else if (!cumode && gch && gch == character(value(FORCE))) |
384 | gch = xgetchar(); | | 448 | gch = xgetchar(); |
385 | bol = any(gch, value(EOL)); | | 449 | bol = any(gch, value(EOL)); |
386 | if (boolean(value(RAISE)) && islower((unsigned char)gch)) | | 450 | if (boolean(value(RAISE)) && islower((unsigned char)gch)) |
387 | gch = toupper((unsigned char)gch); | | 451 | gch = toupper((unsigned char)gch); |
388 | xpwrite(FD, &gch, 1); | | 452 | xpwrite(FD, &gch, 1); |
389 | if (boolean(value(HALFDUPLEX))) | | 453 | if (boolean(value(HALFDUPLEX))) |
390 | (void)printf("%c", gch); | | 454 | (void)printf("%c", gch); |
391 | } | | 455 | } |
392 | } | | 456 | } |
393 | | | 457 | |
394 | /* | | 458 | /* |
395 | * Escape handler -- | | 459 | * Escape handler -- |
396 | * called on recognition of ``escapec'' at the beginning of a line | | 460 | * called on recognition of ``escapec'' at the beginning of a line |
397 | */ | | 461 | */ |
398 | int | | 462 | int |
399 | escape(void) | | 463 | escape(void) |
400 | { | | 464 | { |
401 | char gch; | | 465 | char gch; |
402 | esctable_t *p; | | 466 | esctable_t *p; |
403 | char c = character(value(ESCAPE)); | | 467 | char c = character(value(ESCAPE)); |
404 | | | 468 | |
405 | gch = xgetchar(); | | 469 | gch = xgetchar(); |
406 | for (p = etable; p->e_char; p++) | | 470 | for (p = etable; p->e_char; p++) |
407 | if (p->e_char == gch) { | | 471 | if (p->e_char == gch) { |
408 | if ((p->e_flags&PRIV) && uid) | | 472 | if ((p->e_flags&PRIV) && uid) |
409 | continue; | | 473 | continue; |
410 | (void)printf("%s", ctrl(c)); | | 474 | (void)printf("%s", ctrl(c)); |
411 | (*p->e_func)(gch); | | 475 | (*p->e_func)(gch); |
412 | return (0); | | 476 | return (0); |
413 | } | | 477 | } |
414 | /* ESCAPE ESCAPE forces ESCAPE */ | | 478 | /* ESCAPE ESCAPE forces ESCAPE */ |
415 | if (c != gch) | | 479 | if (c != gch) |
416 | xpwrite(FD, &c, 1); | | 480 | xpwrite(FD, &c, 1); |
417 | return (gch); | | 481 | return (gch); |
418 | } | | 482 | } |
419 | | | 483 | |
420 | int | | 484 | int |
421 | any(char c, const char *p) | | 485 | any(char c, const char *p) |
422 | { | | 486 | { |
423 | | | 487 | |
424 | while (p && *p) | | 488 | while (p && *p) |
425 | if (*p++ == c) | | 489 | if (*p++ == c) |
426 | return (1); | | 490 | return (1); |
427 | return (0); | | 491 | return (0); |
428 | } | | 492 | } |
429 | | | 493 | |
430 | char * | | 494 | char * |
431 | interp(const char *s) | | 495 | interp(const char *s) |
432 | { | | 496 | { |
433 | static char buf[256]; | | 497 | static char buf[256]; |
434 | char *p = buf, c; | | 498 | char *p = buf, c; |
435 | const char *q; | | 499 | const char *q; |
436 | | | 500 | |
437 | while ((c = *s++) != 0 && buf + sizeof buf - p > 2) { | | 501 | while ((c = *s++) != 0 && buf + sizeof buf - p > 2) { |
438 | for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) | | 502 | for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) |
439 | if (*q++ == c) { | | 503 | if (*q++ == c) { |
440 | *p++ = '\\'; *p++ = *q; | | 504 | *p++ = '\\'; *p++ = *q; |
441 | goto next; | | 505 | goto next; |
442 | } | | 506 | } |
443 | if (c < 040) { | | 507 | if (c < 040) { |
444 | *p++ = '^'; *p++ = c + 'A'-1; | | 508 | *p++ = '^'; *p++ = c + 'A'-1; |
445 | } else if (c == 0177) { | | 509 | } else if (c == 0177) { |
446 | *p++ = '^'; *p++ = '?'; | | 510 | *p++ = '^'; *p++ = '?'; |
447 | } else | | 511 | } else |
448 | *p++ = c; | | 512 | *p++ = c; |
449 | next: | | 513 | next: |
450 | ; | | 514 | ; |
451 | } | | 515 | } |
452 | *p = '\0'; | | 516 | *p = '\0'; |
453 | return (buf); | | 517 | return (buf); |
454 | } | | 518 | } |
455 | | | 519 | |
456 | char * | | 520 | char * |
457 | ctrl(char c) | | 521 | ctrl(char c) |
458 | { | | 522 | { |
459 | static char s[3]; | | 523 | static char s[3]; |
460 | | | 524 | |
461 | if (c < 040 || c == 0177) { | | 525 | if (c < 040 || c == 0177) { |
462 | s[0] = '^'; | | 526 | s[0] = '^'; |
463 | s[1] = c == 0177 ? '?' : c+'A'-1; | | 527 | s[1] = c == 0177 ? '?' : c+'A'-1; |
464 | s[2] = '\0'; | | 528 | s[2] = '\0'; |
465 | } else { | | 529 | } else { |
466 | s[0] = c; | | 530 | s[0] = c; |
467 | s[1] = '\0'; | | 531 | s[1] = '\0'; |
468 | } | | 532 | } |
469 | return (s); | | 533 | return (s); |
470 | } | | 534 | } |
471 | | | 535 | |
472 | /* | | 536 | /* |
473 | * Help command | | 537 | * Help command |
474 | */ | | 538 | */ |
475 | void | | 539 | void |
476 | help(char c) | | 540 | help(char c) |
477 | { | | 541 | { |
478 | esctable_t *p; | | 542 | esctable_t *p; |
479 | | | 543 | |
480 | (void)printf("%c\r\n", c); | | 544 | (void)printf("%c\r\n", c); |
481 | for (p = etable; p->e_char; p++) { | | 545 | for (p = etable; p->e_char; p++) { |
482 | if ((p->e_flags&PRIV) && uid) | | 546 | if ((p->e_flags&PRIV) && uid) |
483 | continue; | | 547 | continue; |
484 | (void)printf("%2s", ctrl(character(value(ESCAPE)))); | | 548 | (void)printf("%2s", ctrl(character(value(ESCAPE)))); |
485 | (void)printf("%-2s %c %s\r\n", ctrl(p->e_char), | | 549 | (void)printf("%-2s %c %s\r\n", ctrl(p->e_char), |
486 | p->e_flags&EXP ? '*': ' ', p->e_help); | | 550 | p->e_flags&EXP ? '*': ' ', p->e_help); |
487 | } | | 551 | } |
488 | } | | 552 | } |
489 | | | 553 | |
490 | /* | | 554 | /* |
491 | * Set up the "remote" tty's state | | 555 | * Set up the "remote" tty's state |
492 | */ | | 556 | */ |
493 | int | | 557 | int |
494 | ttysetup(speed_t spd) | | 558 | ttysetup(speed_t spd) |
495 | { | | 559 | { |
496 | struct termios cntrl; | | 560 | struct termios cntrl; |
497 | | | 561 | |
498 | (void)tcgetattr(FD, &cntrl); | | 562 | (void)tcgetattr(FD, &cntrl); |
499 | (void)cfsetospeed(&cntrl, spd); | | 563 | (void)cfsetospeed(&cntrl, spd); |
500 | (void)cfsetispeed(&cntrl, spd); | | 564 | (void)cfsetispeed(&cntrl, spd); |
501 | cntrl.c_cflag &= ~(CSIZE|PARENB); | | 565 | cntrl.c_cflag &= ~(CSIZE|PARENB); |
502 | cntrl.c_cflag |= CS8; | | 566 | cntrl.c_cflag |= CS8; |
503 | if (DC) | | 567 | if (DC) |
504 | cntrl.c_cflag |= CLOCAL; | | 568 | cntrl.c_cflag |= CLOCAL; |
505 | if (boolean(value(HARDWAREFLOW))) | | 569 | if (boolean(value(HARDWAREFLOW))) |
506 | cntrl.c_cflag |= CRTSCTS; | | 570 | cntrl.c_cflag |= CRTSCTS; |
507 | cntrl.c_iflag &= ~(ISTRIP|ICRNL); | | 571 | cntrl.c_iflag &= ~(ISTRIP|ICRNL); |
508 | cntrl.c_oflag &= ~OPOST; | | 572 | cntrl.c_oflag &= ~OPOST; |
509 | cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO); | | 573 | cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO); |
510 | cntrl.c_cc[VMIN] = 1; | | 574 | cntrl.c_cc[VMIN] = 1; |
511 | cntrl.c_cc[VTIME] = 0; | | 575 | cntrl.c_cc[VTIME] = 0; |
512 | if (boolean(value(TAND))) | | 576 | if (boolean(value(TAND))) |
513 | cntrl.c_iflag |= IXOFF|IXON; | | 577 | cntrl.c_iflag |= IXOFF|IXON; |
514 | else | | 578 | else |
515 | cntrl.c_iflag &= ~(IXOFF|IXON); | | 579 | cntrl.c_iflag &= ~(IXOFF|IXON); |
516 | return tcsetattr(FD, TCSAFLUSH, &cntrl); | | 580 | return tcsetattr(FD, TCSAFLUSH, &cntrl); |
517 | } | | 581 | } |
518 | | | 582 | |
519 | static char partab[0200]; | | 583 | static char partab[0200]; |
520 | | | 584 | |
521 | /* | | 585 | /* |
522 | * Do a write to the remote machine with the correct parity. | | 586 | * Do a write to the remote machine with the correct parity. |
523 | * We are doing 8 bit wide output, so we just generate a character | | 587 | * We are doing 8 bit wide output, so we just generate a character |
524 | * with the right parity and output it. | | 588 | * with the right parity and output it. |
525 | */ | | 589 | */ |
526 | void | | 590 | void |
527 | xpwrite(int fd, char *buf, size_t n) | | 591 | xpwrite(int fd, char *buf, size_t n) |
528 | { | | 592 | { |
529 | size_t i; | | 593 | size_t i; |
530 | char *bp; | | 594 | char *bp; |
531 | | | 595 | |
532 | bp = buf; | | 596 | bp = buf; |
533 | if (bits8 == 0) | | 597 | if (bits8 == 0) |
534 | for (i = 0; i < n; i++) { | | 598 | for (i = 0; i < n; i++) { |
535 | *bp = partab[(*bp) & 0177]; | | 599 | *bp = partab[(*bp) & 0177]; |
536 | bp++; | | 600 | bp++; |
537 | } | | 601 | } |
538 | if (write(fd, buf, n) < 0) { | | 602 | if (write(fd, buf, n) < 0) { |
539 | if (errno == EIO) | | 603 | if (errno == EIO) |
540 | tipabort("Lost carrier."); | | 604 | tipabort("Lost carrier."); |
541 | /* this is questionable */ | | 605 | /* this is questionable */ |
542 | warn("write"); | | 606 | warn("write"); |
543 | } | | 607 | } |
544 | } | | 608 | } |
545 | | | 609 | |
546 | /* | | 610 | /* |
547 | * Build a parity table with appropriate high-order bit. | | 611 | * Build a parity table with appropriate high-order bit. |
548 | */ | | 612 | */ |
549 | void | | 613 | void |
550 | setparity(const char *defparity) | | 614 | setparity(const char *defparity) |
551 | { | | 615 | { |
552 | int i, flip, clr, set; | | 616 | int i, flip, clr, set; |
553 | const char *parity; | | 617 | const char *parity; |
554 | static char *curpar; | | 618 | static char *curpar; |
555 | | | 619 | |
556 | if (value(PARITY) == NULL || ((char *)value(PARITY))[0] == '\0') { | | 620 | if (value(PARITY) == NULL || ((char *)value(PARITY))[0] == '\0') { |
557 | if (curpar != NULL) | | 621 | if (curpar != NULL) |
558 | free(curpar); | | 622 | free(curpar); |
559 | value(PARITY) = curpar = strdup(defparity); | | 623 | value(PARITY) = curpar = strdup(defparity); |
560 | } | | 624 | } |
561 | parity = value(PARITY); | | 625 | parity = value(PARITY); |
562 | if (strcmp(parity, "none") == 0) { | | 626 | if (strcmp(parity, "none") == 0) { |
563 | bits8 = 1; | | 627 | bits8 = 1; |
564 | return; | | 628 | return; |
565 | } | | 629 | } |
566 | bits8 = 0; | | 630 | bits8 = 0; |
567 | flip = 0; | | 631 | flip = 0; |
568 | clr = 0377; | | 632 | clr = 0377; |
569 | set = 0; | | 633 | set = 0; |
570 | if (strcmp(parity, "odd") == 0) | | 634 | if (strcmp(parity, "odd") == 0) |
571 | flip = 0200; /* reverse bit 7 */ | | 635 | flip = 0200; /* reverse bit 7 */ |
572 | else if (strcmp(parity, "zero") == 0) | | 636 | else if (strcmp(parity, "zero") == 0) |
573 | clr = 0177; /* turn off bit 7 */ | | 637 | clr = 0177; /* turn off bit 7 */ |
574 | else if (strcmp(parity, "one") == 0) | | 638 | else if (strcmp(parity, "one") == 0) |
575 | set = 0200; /* turn on bit 7 */ | | 639 | set = 0200; /* turn on bit 7 */ |
576 | else if (strcmp(parity, "even") != 0) { | | 640 | else if (strcmp(parity, "even") != 0) { |
577 | (void)fprintf(stderr, "%s: unknown parity value\r\n", parity); | | 641 | (void)fprintf(stderr, "%s: unknown parity value\r\n", parity); |
578 | (void)fflush(stderr); | | 642 | (void)fflush(stderr); |
579 | } | | 643 | } |
580 | for (i = 0; i < 0200; i++) | | 644 | for (i = 0; i < 0200; i++) |
581 | partab[i] = ((evenpartab[i] ^ flip) | set) & clr; | | 645 | partab[i] = ((evenpartab[i] ^ flip) | set) & clr; |
582 | } | | 646 | } |