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