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