1*19806Sdist /* 2*19806Sdist * Copyright (c) 1983 Regents of the University of California. 3*19806Sdist * All rights reserved. The Berkeley software License Agreement 4*19806Sdist * specifies the terms and conditions for redistribution. 5*19806Sdist */ 6*19806Sdist 713279Ssam #ifndef lint 8*19806Sdist char copyright[] = 9*19806Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10*19806Sdist All rights reserved.\n"; 11*19806Sdist #endif not lint 125136Ssam 13*19806Sdist #ifndef lint 14*19806Sdist static char sccsid[] = "@(#)tip.c 5.1 (Berkeley) 04/30/85"; 15*19806Sdist #endif not lint 16*19806Sdist 173696Sroot /* 185136Ssam * tip - UNIX link to other systems 193696Sroot * tip [-v] [-speed] system-name 205136Ssam * or 215136Ssam * cu phone-number [-s speed] [-l line] [-a acu] 223696Sroot */ 233696Sroot #include "tip.h" 243696Sroot 253696Sroot /* 263696Sroot * Baud rate mapping table 273696Sroot */ 283696Sroot int bauds[] = { 293696Sroot 0, 50, 75, 110, 134, 150, 200, 300, 600, 303696Sroot 1200, 1800, 2400, 4800, 9600, 19200, -1 313696Sroot }; 323696Sroot 334004Ssam int disc = OTTYDISC; /* tip normally runs this way */ 343696Sroot int intprompt(); 353696Sroot int timeout(); 365136Ssam int cleanup(); 375257Sshannon char *sname(); 385257Sshannon extern char *sprintf(); 393696Sroot 403696Sroot main(argc, argv) 414962Ssam char *argv[]; 423696Sroot { 433696Sroot char *system = NOSTR; 443696Sroot register int i; 455257Sshannon register char *p; 465257Sshannon char sbuf[12]; 473696Sroot 485257Sshannon if (equal(sname(argv[0]), "cu")) { 495136Ssam cumain(argc, argv); 505136Ssam cumode = 1; 515136Ssam goto cucommon; 525136Ssam } 535136Ssam 543696Sroot if (argc > 4) { 553696Sroot fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n"); 563696Sroot exit(1); 573696Sroot } 583696Sroot if (!isatty(0)) { 593696Sroot fprintf(stderr, "tip: must be interactive\n"); 603696Sroot exit(1); 613696Sroot } 625257Sshannon 635257Sshannon for (; argc > 1; argv++, argc--) { 645257Sshannon if (argv[1][0] != '-') 655257Sshannon system = argv[1]; 665257Sshannon else switch (argv[1][1]) { 675257Sshannon 685257Sshannon case 'v': 695257Sshannon vflag++; 705257Sshannon break; 715257Sshannon 725257Sshannon case '0': case '1': case '2': case '3': case '4': 735257Sshannon case '5': case '6': case '7': case '8': case '9': 745257Sshannon BR = atoi(&argv[1][1]); 755257Sshannon break; 765257Sshannon 775257Sshannon default: 785257Sshannon fprintf(stderr, "tip: %s, unknown option\n", argv[1]); 795257Sshannon break; 805257Sshannon } 815257Sshannon } 825257Sshannon 837593Sshannon if (system == NOSTR) 847593Sshannon goto notnumber; 855257Sshannon for (p = system; *p; p++) 865257Sshannon if (isalpha(*p)) 875257Sshannon goto notnumber; 885257Sshannon PN = system; /* system name is really a phone number */ 895257Sshannon system = sprintf(sbuf, "tip%d", BR); 905257Sshannon 915257Sshannon notnumber: 923696Sroot signal(SIGINT, cleanup); 933696Sroot signal(SIGQUIT, cleanup); 943696Sroot signal(SIGHUP, cleanup); 953696Sroot signal(SIGTERM, cleanup); 963898Ssam 973696Sroot if ((i = hunt(system)) == 0) { 983696Sroot printf("all ports busy\n"); 993696Sroot exit(3); 1003696Sroot } 1013696Sroot if (i == -1) { 1023696Sroot printf("link down\n"); 1034232Ssam delock(uucplock); 1043696Sroot exit(3); 1053696Sroot } 1063719Ssam setbuf(stdout, NULL); 1073696Sroot loginit(); 1083696Sroot /* 1093696Sroot * Now that we have the logfile and the ACU open 1103696Sroot * return to the real uid and gid. These things will 1113696Sroot * be closed on exit. Note that we can't run as root, 1123696Sroot * because locking mechanism on the tty and the accounting 1133696Sroot * will be bypassed. 1143696Sroot */ 11513139Sralph setgid(getgid()); 1163696Sroot setuid(getuid()); 1175257Sshannon 1183696Sroot /* 1193696Sroot * Kludge, their's no easy way to get the initialization 1203696Sroot * in the right order, so force it here 1213696Sroot */ 1223696Sroot if ((PH = getenv("PHONES")) == NOSTR) 1233696Sroot PH = "/etc/phones"; 1243696Sroot vinit(); /* init variables */ 12513430Ssam setparity("even"); /* set the parity table */ 1264004Ssam if ((i = speed(number(value(BAUDRATE)))) == NULL) { 1273696Sroot printf("tip: bad baud rate %d\n", number(value(BAUDRATE))); 1283696Sroot delock(uucplock); 1293696Sroot exit(3); 1303696Sroot } 1313898Ssam 1324004Ssam /* 1334004Ssam * Hardwired connections require the 1344004Ssam * line speed set before they make any transmissions 1354004Ssam * (this is particularly true of things like a DF03-AC) 1364004Ssam */ 1374004Ssam if (HW) 1384004Ssam ttysetup(i); 1393898Ssam if (p = connect()) { 1403898Ssam printf("\07%s\n[EOT]\n", p); 1413898Ssam delock(uucplock); 1423898Ssam exit(1); 1433898Ssam } 1444004Ssam if (!HW) 1454004Ssam ttysetup(i); 1465136Ssam cucommon: 1473696Sroot /* 1485136Ssam * From here down the code is shared with 1495136Ssam * the "cu" version of tip. 1503696Sroot */ 1514004Ssam ioctl(0, TIOCGETP, (char *)&defarg); 1524004Ssam ioctl(0, TIOCGETC, (char *)&defchars); 15312478Sroot ioctl(0, TIOCGLTC, (char *)&deflchars); 1544004Ssam ioctl(0, TIOCGETD, (char *)&odisc); 1553696Sroot arg = defarg; 1563696Sroot arg.sg_flags = ANYP | CBREAK; 1573696Sroot tchars = defchars; 1583696Sroot tchars.t_intrc = tchars.t_quitc = -1; 15912478Sroot ltchars = deflchars; 16012478Sroot ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc 16112478Sroot = ltchars.t_lnextc = -1; 1623696Sroot raw(); 1634004Ssam 1643696Sroot pipe(fildes); pipe(repdes); 1653696Sroot signal(SIGALRM, timeout); 1663898Ssam 1673898Ssam /* 1683898Ssam * Everything's set up now: 16917236Shelge * connection established (hardwired or dialup) 1703898Ssam * line conditioned (baud rate, mode, etc.) 1713898Ssam * internal data structures (variables) 1723898Ssam * so, fork one process for local side and one for remote. 1733898Ssam */ 1745136Ssam printf(cumode ? "Connected\r\n" : "\07connected\r\n"); 1753696Sroot if (pid = fork()) 1763696Sroot tipin(); 1773696Sroot else 1783696Sroot tipout(); 1793696Sroot /*NOTREACHED*/ 1803696Sroot } 1813696Sroot 1823696Sroot cleanup() 1833696Sroot { 18413279Ssam 1853696Sroot delock(uucplock); 1863898Ssam if (odisc) 1873898Ssam ioctl(0, TIOCSETD, (char *)&odisc); 1883696Sroot exit(0); 1893696Sroot } 1903696Sroot 1913696Sroot /* 1923696Sroot * put the controlling keyboard into raw mode 1933696Sroot */ 1943696Sroot raw() 1953696Sroot { 19613279Ssam 1973696Sroot ioctl(0, TIOCSETP, &arg); 1983696Sroot ioctl(0, TIOCSETC, &tchars); 19912478Sroot ioctl(0, TIOCSLTC, <chars); 2004004Ssam ioctl(0, TIOCSETD, (char *)&disc); 2013696Sroot } 2023696Sroot 2033696Sroot 2043696Sroot /* 2053696Sroot * return keyboard to normal mode 2063696Sroot */ 2073696Sroot unraw() 2083696Sroot { 20913279Ssam 2104004Ssam ioctl(0, TIOCSETD, (char *)&odisc); 2114004Ssam ioctl(0, TIOCSETP, (char *)&defarg); 2124004Ssam ioctl(0, TIOCSETC, (char *)&defchars); 21312478Sroot ioctl(0, TIOCSLTC, (char *)&deflchars); 2143696Sroot } 2153696Sroot 21613279Ssam static jmp_buf promptbuf; 21713279Ssam 2183696Sroot /* 2193696Sroot * Print string ``s'', then read a string 2203696Sroot * in from the terminal. Handles signals & allows use of 2213696Sroot * normal erase and kill characters. 2223696Sroot */ 2233696Sroot prompt(s, p) 2243696Sroot char *s; 2253696Sroot register char *p; 2263696Sroot { 2273696Sroot register char *b = p; 22813279Ssam int (*oint)(), (*oquit)(); 2293696Sroot 2303696Sroot stoprompt = 0; 23113279Ssam oint = signal(SIGINT, intprompt); 23213279Ssam oint = signal(SIGQUIT, SIG_IGN); 2333696Sroot unraw(); 2343696Sroot printf("%s", s); 23513279Ssam if (setjmp(promptbuf) == 0) 23613279Ssam while ((*p = getchar()) != EOF && *p != '\n') 23713279Ssam p++; 2383696Sroot *p = '\0'; 23913279Ssam 2403696Sroot raw(); 24113279Ssam signal(SIGINT, oint); 24213279Ssam signal(SIGQUIT, oint); 24313279Ssam return (stoprompt || p == b); 2443696Sroot } 2453696Sroot 2463696Sroot /* 2473696Sroot * Interrupt service routine during prompting 2483696Sroot */ 2493696Sroot intprompt() 2503696Sroot { 25113279Ssam 2523696Sroot signal(SIGINT, SIG_IGN); 2533696Sroot stoprompt = 1; 2543696Sroot printf("\r\n"); 25513279Ssam longjmp(promptbuf, 1); 2563696Sroot } 2573696Sroot 2583696Sroot /* 2593696Sroot * ****TIPIN TIPIN**** 2603696Sroot */ 2613696Sroot tipin() 2623696Sroot { 2633696Sroot char gch, bol = 1; 2643696Sroot 2653796Ssam /* 2663796Ssam * Kinda klugey here... 2673796Ssam * check for scripting being turned on from the .tiprc file, 2683796Ssam * but be careful about just using setscript(), as we may 2693796Ssam * send a SIGEMT before tipout has a chance to set up catching 2703796Ssam * it; so wait a second, then setscript() 2713796Ssam */ 2723796Ssam if (boolean(value(SCRIPT))) { 2733796Ssam sleep(1); 2743796Ssam setscript(); 2753796Ssam } 2763796Ssam 2773696Sroot while (1) { 2783696Sroot gch = getchar()&0177; 2793696Sroot if ((gch == character(value(ESCAPE))) && bol) { 2803696Sroot if (!(gch = escape())) 2813696Sroot continue; 2825136Ssam } else if (!cumode && gch == character(value(RAISECHAR))) { 2833696Sroot boolean(value(RAISE)) = !boolean(value(RAISE)); 2843696Sroot continue; 2853696Sroot } else if (gch == '\r') { 2863696Sroot bol = 1; 28713139Sralph pwrite(FD, &gch, 1); 28813139Sralph if (boolean(value(HALFDUPLEX))) 28913139Sralph printf("\r\n"); 2903696Sroot continue; 2915136Ssam } else if (!cumode && gch == character(value(FORCE))) 2923696Sroot gch = getchar()&0177; 2933696Sroot bol = any(gch, value(EOL)); 2943696Sroot if (boolean(value(RAISE)) && islower(gch)) 29513139Sralph gch = toupper(gch); 29613139Sralph pwrite(FD, &gch, 1); 29713139Sralph if (boolean(value(HALFDUPLEX))) 29813139Sralph printf("%c", gch); 2993696Sroot } 3003696Sroot } 3013696Sroot 3023696Sroot /* 3033696Sroot * Escape handler -- 3043696Sroot * called on recognition of ``escapec'' at the beginning of a line 3053696Sroot */ 3063696Sroot escape() 3073696Sroot { 3083696Sroot register char gch; 3093696Sroot register esctable_t *p; 3103696Sroot char c = character(value(ESCAPE)); 3113696Sroot extern esctable_t etable[]; 3123696Sroot 3133696Sroot gch = (getchar()&0177); 3143696Sroot for (p = etable; p->e_char; p++) 3153696Sroot if (p->e_char == gch) { 3163696Sroot if ((p->e_flags&PRIV) && getuid()) 3173696Sroot continue; 3183696Sroot printf("%s", ctrl(c)); 3193696Sroot (*p->e_func)(gch); 32013279Ssam return (0); 3213696Sroot } 3224146Ssam /* ESCAPE ESCAPE forces ESCAPE */ 3234146Ssam if (c != gch) 32413139Sralph pwrite(FD, &c, 1); 32513279Ssam return (gch); 3263696Sroot } 3273696Sroot 3283696Sroot speed(n) 32913279Ssam int n; 3303696Sroot { 3313696Sroot register int *p; 3323696Sroot 3333696Sroot for (p = bauds; *p != -1; p++) 3343696Sroot if (*p == n) 33513279Ssam return (p - bauds); 33613279Ssam return (NULL); 3373696Sroot } 3383696Sroot 3393696Sroot any(c, p) 3403696Sroot register char c, *p; 3413696Sroot { 34213279Ssam while (p && *p) 3433696Sroot if (*p++ == c) 34413279Ssam return (1); 34513279Ssam return (0); 3463696Sroot } 3473696Sroot 3483696Sroot size(s) 3493696Sroot register char *s; 3503696Sroot { 35113279Ssam register int i = 0; 3523696Sroot 35313279Ssam while (s && *s++) 35413279Ssam i++; 35513279Ssam return (i); 3563696Sroot } 3573696Sroot 3583696Sroot char * 3593696Sroot interp(s) 3603696Sroot register char *s; 3613696Sroot { 3623696Sroot static char buf[256]; 3633696Sroot register char *p = buf, c, *q; 3643696Sroot 3653696Sroot while (c = *s++) { 3663696Sroot for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++) 3673696Sroot if (*q++ == c) { 3683696Sroot *p++ = '\\'; *p++ = *q; 3693696Sroot goto next; 3703696Sroot } 3713696Sroot if (c < 040) { 3723696Sroot *p++ = '^'; *p++ = c + 'A'-1; 3733696Sroot } else if (c == 0177) { 3743696Sroot *p++ = '^'; *p++ = '?'; 3753696Sroot } else 3763696Sroot *p++ = c; 3773696Sroot next: 3783696Sroot ; 3793696Sroot } 3803696Sroot *p = '\0'; 38113279Ssam return (buf); 3823696Sroot } 3833696Sroot 3843696Sroot char * 3853696Sroot ctrl(c) 3863696Sroot char c; 3873696Sroot { 3883696Sroot static char s[3]; 3893696Sroot 3903696Sroot if (c < 040 || c == 0177) { 3913696Sroot s[0] = '^'; 3923696Sroot s[1] = c == 0177 ? '?' : c+'A'-1; 3933696Sroot s[2] = '\0'; 3943696Sroot } else { 3953696Sroot s[0] = c; 3963696Sroot s[1] = '\0'; 3973696Sroot } 39813279Ssam return (s); 3993696Sroot } 4003696Sroot 4013696Sroot /* 4023696Sroot * Help command 4033696Sroot */ 4043696Sroot help(c) 4053696Sroot char c; 4063696Sroot { 4073696Sroot register esctable_t *p; 4083696Sroot extern esctable_t etable[]; 4093696Sroot 4103696Sroot printf("%c\r\n", c); 4113696Sroot for (p = etable; p->e_char; p++) { 4123696Sroot if ((p->e_flags&PRIV) && getuid()) 4133696Sroot continue; 4143696Sroot printf("%2s", ctrl(character(value(ESCAPE)))); 4153696Sroot printf("%-2s %c %s\r\n", ctrl(p->e_char), 4163696Sroot p->e_flags&EXP ? '*': ' ', p->e_help); 4173696Sroot } 4183696Sroot } 4194004Ssam 4204004Ssam /* 4214004Ssam * Set up the "remote" tty's state 4224004Ssam */ 4234004Ssam ttysetup(speed) 42413279Ssam int speed; 4254004Ssam { 4264004Ssam unsigned bits = LDECCTQ; 4274004Ssam 4284004Ssam arg.sg_ispeed = arg.sg_ospeed = speed; 42913279Ssam arg.sg_flags = RAW; 43013139Sralph if (boolean(value(TAND))) 43113279Ssam arg.sg_flags |= TANDEM; 4324004Ssam ioctl(FD, TIOCSETP, (char *)&arg); 4334004Ssam ioctl(FD, TIOCLBIS, (char *)&bits); 4344004Ssam } 4355257Sshannon 4365257Sshannon /* 4375257Sshannon * Return "simple" name from a file name, 4385257Sshannon * strip leading directories. 4395257Sshannon */ 4405257Sshannon char * 4415257Sshannon sname(s) 4425257Sshannon register char *s; 4435257Sshannon { 4445257Sshannon register char *p = s; 4455257Sshannon 4465257Sshannon while (*s) 4475257Sshannon if (*s++ == '/') 4485257Sshannon p = s; 4495257Sshannon return (p); 4505257Sshannon } 45113139Sralph 45213139Sralph static char partab[0200]; 45313139Sralph 45413139Sralph /* 45513279Ssam * Do a write to the remote machine with the correct parity. 45613279Ssam * We are doing 8 bit wide output, so we just generate a character 45713139Sralph * with the right parity and output it. 45813139Sralph */ 45913139Sralph pwrite(fd, buf, n) 46013139Sralph int fd; 46113139Sralph char *buf; 46213139Sralph register int n; 46313139Sralph { 46413139Sralph register int i; 46513279Ssam register char *bp; 46615188Ssam extern int errno; 46713139Sralph 46813279Ssam bp = buf; 46913279Ssam for (i = 0; i < n; i++) { 47013139Sralph *bp = partab[(*bp) & 0177]; 47113279Ssam bp++; 47213139Sralph } 47315188Ssam if (write(fd, buf, n) < 0) { 47415188Ssam if (errno == EIO) 47515188Ssam abort("Lost carrier."); 47615188Ssam /* this is questionable */ 47715188Ssam perror("write"); 47815188Ssam } 47913139Sralph } 48013139Sralph 48113139Sralph /* 48213279Ssam * Build a parity table with appropriate high-order bit. 48313139Sralph */ 48413430Ssam setparity(defparity) 48513430Ssam char *defparity; 48613139Sralph { 48713279Ssam register int i; 48813139Sralph char *parity; 48913279Ssam extern char evenpartab[]; 49013139Sralph 49113139Sralph if (value(PARITY) == NOSTR) 49213430Ssam value(PARITY) = defparity; 49313139Sralph parity = value(PARITY); 49413139Sralph for (i = 0; i < 0200; i++) 49513279Ssam partab[i] = evenpartab[i]; 49613279Ssam if (equal(parity, "even")) 49713279Ssam return; 49813139Sralph if (equal(parity, "odd")) { 49913139Sralph for (i = 0; i < 0200; i++) 50013139Sralph partab[i] ^= 0200; /* reverse bit 7 */ 50113279Ssam return; 50213139Sralph } 50313279Ssam if (equal(parity, "none") || equal(parity, "zero")) { 50413139Sralph for (i = 0; i < 0200; i++) 50513139Sralph partab[i] &= ~0200; /* turn off bit 7 */ 50613279Ssam return; 50713139Sralph } 50813279Ssam if (equal(parity, "one")) { 50913139Sralph for (i = 0; i < 0200; i++) 51013139Sralph partab[i] |= 0200; /* turn on bit 7 */ 51113279Ssam return; 51213139Sralph } 51313279Ssam fprintf(stderr, "%s: unknown parity value\n", PA); 51413279Ssam fflush(stderr); 51513139Sralph } 516