17c478bd9Sstevel@tonic-gate /*
2*de81e71eSTim Marsland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
58d489c7aSmuffin
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate */
117c478bd9Sstevel@tonic-gate
127c478bd9Sstevel@tonic-gate /*
137c478bd9Sstevel@tonic-gate * tip - UNIX link to other systems
147c478bd9Sstevel@tonic-gate * tip [-v] [-speed] system-name
157c478bd9Sstevel@tonic-gate * or
167c478bd9Sstevel@tonic-gate * cu phone-number [-s speed] [-l line] [-a acu]
177c478bd9Sstevel@tonic-gate */
187c478bd9Sstevel@tonic-gate #include "tip.h"
197c478bd9Sstevel@tonic-gate #include <sys/wait.h>
207c478bd9Sstevel@tonic-gate #include <locale.h>
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Baud rate mapping table
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate int bauds[] = {
267c478bd9Sstevel@tonic-gate 0, 50, 75, 110, 134, 150, 200, 300, 600,
277c478bd9Sstevel@tonic-gate 1200, 1800, 2400, 4800, 9600, 19200, 38400,
28*de81e71eSTim Marsland 57600, 76800, 115200, 153600, 230400, 307200,
29*de81e71eSTim Marsland 460800, 921600, -1
307c478bd9Sstevel@tonic-gate };
317c478bd9Sstevel@tonic-gate
328d489c7aSmuffin extern void tipout(void) __NORETURN;
338d489c7aSmuffin extern void timeout(void);
348d489c7aSmuffin extern esctable_t etable[];
358d489c7aSmuffin extern unsigned char evenpartab[];
368d489c7aSmuffin
378d489c7aSmuffin void intprompt(void);
388d489c7aSmuffin void deadkid(void);
398d489c7aSmuffin void cleanup(void);
408d489c7aSmuffin void tipin(void) __NORETURN;
418d489c7aSmuffin unsigned char escape(void);
428d489c7aSmuffin char *sname(char *);
437c478bd9Sstevel@tonic-gate char PNbuf[256]; /* This limits the size of a number */
447c478bd9Sstevel@tonic-gate int noparity = 0;
457c478bd9Sstevel@tonic-gate
468d489c7aSmuffin int
main(int argc,char * argv[])478d489c7aSmuffin main(int argc, char *argv[])
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate char *system = NOSTR;
508d489c7aSmuffin int i;
518d489c7aSmuffin char *p;
5294e1761eSsn199410 char sbuf[15];
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate gid = getgid();
557c478bd9Sstevel@tonic-gate egid = getegid();
567c478bd9Sstevel@tonic-gate uid = getuid();
577c478bd9Sstevel@tonic-gate euid = geteuid();
587c478bd9Sstevel@tonic-gate if (equal(sname(argv[0]), "cu")) {
597c478bd9Sstevel@tonic-gate cumode = 1;
607c478bd9Sstevel@tonic-gate cumain(argc, argv);
617c478bd9Sstevel@tonic-gate goto cucommon;
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate if (argc > 4) {
658d489c7aSmuffin (void) fprintf(stderr,
668d489c7aSmuffin "usage: tip [-v] [-speed] [system-name]\n");
678d489c7aSmuffin return (1);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate if (!isatty(0)) {
708d489c7aSmuffin (void) fprintf(stderr, "tip: must be interactive\n");
718d489c7aSmuffin return (1);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate for (; argc > 1; argv++, argc--) {
757c478bd9Sstevel@tonic-gate if (argv[1][0] != '-')
767c478bd9Sstevel@tonic-gate system = argv[1];
777c478bd9Sstevel@tonic-gate else switch (argv[1][1]) {
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate case 'v':
807c478bd9Sstevel@tonic-gate vflag++;
817c478bd9Sstevel@tonic-gate break;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate case '0': case '1': case '2': case '3': case '4':
847c478bd9Sstevel@tonic-gate case '5': case '6': case '7': case '8': case '9':
857c478bd9Sstevel@tonic-gate BR = atoi(&argv[1][1]);
867c478bd9Sstevel@tonic-gate break;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate default:
898d489c7aSmuffin (void) fprintf(stderr, "tip: %s, unknown option\n",
908d489c7aSmuffin argv[1]);
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate (void) setlocale(LC_CTYPE, "");
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate if (system == NOSTR)
987c478bd9Sstevel@tonic-gate goto notnumber;
997c478bd9Sstevel@tonic-gate for (p = system; *p; p++)
1007c478bd9Sstevel@tonic-gate if (isalpha(*p))
1017c478bd9Sstevel@tonic-gate goto notnumber;
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * System name is really a phone number...
1047c478bd9Sstevel@tonic-gate * Copy the number then stomp on the original (in case the number
1057c478bd9Sstevel@tonic-gate * is private, we don't want 'ps' or 'w' to find it).
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate if (strlen(system) > sizeof (PNbuf) - 1) {
1088d489c7aSmuffin (void) fprintf(stderr,
1098d489c7aSmuffin "tip: phone number too long (max = %d bytes)\n",
1107c478bd9Sstevel@tonic-gate sizeof (PNbuf) - 1);
1118d489c7aSmuffin return (1);
1127c478bd9Sstevel@tonic-gate }
1138d489c7aSmuffin (void) strncpy(PNbuf, system, sizeof (PNbuf) - 1);
1147c478bd9Sstevel@tonic-gate for (p = system; *p; p++)
1157c478bd9Sstevel@tonic-gate *p = '\0';
1167c478bd9Sstevel@tonic-gate PN = PNbuf;
11794e1761eSsn199410 (void) snprintf(sbuf, sizeof (sbuf), "tip%d", BR);
1187c478bd9Sstevel@tonic-gate system = sbuf;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate notnumber:
1218d489c7aSmuffin (void) signal(SIGINT, (sig_handler_t)cleanup);
1228d489c7aSmuffin (void) signal(SIGQUIT, (sig_handler_t)cleanup);
1238d489c7aSmuffin (void) signal(SIGHUP, (sig_handler_t)cleanup);
1248d489c7aSmuffin (void) signal(SIGTERM, (sig_handler_t)cleanup);
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate if ((i = hunt(system)) == 0) {
1278d489c7aSmuffin (void) printf("all ports busy\n");
1288d489c7aSmuffin return (3);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate if (i == -1) {
1318d489c7aSmuffin (void) printf("link down\n");
1327c478bd9Sstevel@tonic-gate delock(uucplock);
1338d489c7aSmuffin return (3);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate setbuf(stdout, NULL);
1367c478bd9Sstevel@tonic-gate loginit();
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Now that we have the logfile and the ACU open
1407c478bd9Sstevel@tonic-gate * return to the real uid and gid. These things will
1417c478bd9Sstevel@tonic-gate * be closed on exit. The saved-setuid uid and gid
1427c478bd9Sstevel@tonic-gate * allows us to get the original setuid permissions back
1437c478bd9Sstevel@tonic-gate * for removing the uucp lock.
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate userperm();
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * Kludge, there's no easy way to get the initialization
1497c478bd9Sstevel@tonic-gate * in the right order, so force it here.
1507c478bd9Sstevel@tonic-gate * Do the open here, before we change back to real uid.
1517c478bd9Sstevel@tonic-gate * We will check whether the open succeeded later, when
1527c478bd9Sstevel@tonic-gate * (and if) we actually go to use the file.
1537c478bd9Sstevel@tonic-gate */
1547c478bd9Sstevel@tonic-gate if ((PH = getenv("PHONES")) == NOSTR) {
1557c478bd9Sstevel@tonic-gate myperm();
1567c478bd9Sstevel@tonic-gate PH = "/etc/phones";
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate phfd = fopen(PH, "r");
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate userperm();
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate vinit(); /* init variables */
1637c478bd9Sstevel@tonic-gate setparity("none"); /* set the parity table */
1647c478bd9Sstevel@tonic-gate if ((i = speed(number(value(BAUDRATE)))) == NULL) {
1658d489c7aSmuffin (void) printf("tip: bad baud rate %d\n",
1668d489c7aSmuffin number(value(BAUDRATE)));
1677c478bd9Sstevel@tonic-gate myperm();
1687c478bd9Sstevel@tonic-gate delock(uucplock);
1698d489c7aSmuffin return (3);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate * Hardwired connections require the
1757c478bd9Sstevel@tonic-gate * line speed set before they make any transmissions
1767c478bd9Sstevel@tonic-gate * (this is particularly true of things like a DF03-AC)
1777c478bd9Sstevel@tonic-gate */
1787c478bd9Sstevel@tonic-gate if (HW)
1797c478bd9Sstevel@tonic-gate ttysetup(i);
1807c478bd9Sstevel@tonic-gate if (p = connect()) {
1818d489c7aSmuffin (void) printf("\07%s\n[EOT]\n", p);
1827c478bd9Sstevel@tonic-gate myperm();
1837c478bd9Sstevel@tonic-gate delock(uucplock);
1848d489c7aSmuffin return (1);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * Always setup the tty again here in case hardware flow
1897c478bd9Sstevel@tonic-gate * control was selected, which can only be set after the
1907c478bd9Sstevel@tonic-gate * connection is made, or in case this is not a hardwired
1917c478bd9Sstevel@tonic-gate * modem (rare these days) that likewise can only be setup
1927c478bd9Sstevel@tonic-gate * after the connection is made.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate ttysetup(i);
1957c478bd9Sstevel@tonic-gate cucommon:
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * From here down the code is shared with
1987c478bd9Sstevel@tonic-gate * the "cu" version of tip.
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate
2018d489c7aSmuffin (void) ioctl(0, TCGETS, (char *)&defarg);
2027c478bd9Sstevel@tonic-gate arg = defarg;
2037c478bd9Sstevel@tonic-gate /* turn off input processing */
2047c478bd9Sstevel@tonic-gate arg.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
2057c478bd9Sstevel@tonic-gate arg.c_cc[VMIN] = 1;
2067c478bd9Sstevel@tonic-gate arg.c_cc[VTIME] = 0;
2077c478bd9Sstevel@tonic-gate arg.c_iflag &= ~(INPCK|IXON|IXOFF|ICRNL);
2087c478bd9Sstevel@tonic-gate arg.c_oflag = 0; /* turn off all output processing */
2097c478bd9Sstevel@tonic-gate /* handle tandem mode in case was set in remote file */
2107c478bd9Sstevel@tonic-gate if (boolean(value(TAND)))
2117c478bd9Sstevel@tonic-gate tandem("on");
2127c478bd9Sstevel@tonic-gate else
2137c478bd9Sstevel@tonic-gate tandem("off");
2147c478bd9Sstevel@tonic-gate raw();
2157c478bd9Sstevel@tonic-gate
2168d489c7aSmuffin (void) pipe(fildes); (void) pipe(repdes);
2178d489c7aSmuffin (void) signal(SIGALRM, (sig_handler_t)timeout);
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate * Everything's set up now:
2217c478bd9Sstevel@tonic-gate * connection established (hardwired or dialup)
2227c478bd9Sstevel@tonic-gate * line conditioned (baud rate, mode, etc.)
2237c478bd9Sstevel@tonic-gate * internal data structures (variables)
2247c478bd9Sstevel@tonic-gate * so, fork one process for local side and one for remote.
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate if (CM != NOSTR) {
2278d489c7aSmuffin (void) sleep(2); /* let line settle */
2288d489c7aSmuffin parwrite(FD, (unsigned char *)CM, strlen(CM));
2297c478bd9Sstevel@tonic-gate }
2308d489c7aSmuffin (void) printf(cumode ? "Connected\r\n" : "\07connected\r\n");
2318d489c7aSmuffin (void) signal(SIGCHLD, (sig_handler_t)deadkid);
2327c478bd9Sstevel@tonic-gate if (pid = fork())
2337c478bd9Sstevel@tonic-gate tipin();
2347c478bd9Sstevel@tonic-gate else
2357c478bd9Sstevel@tonic-gate tipout();
2367c478bd9Sstevel@tonic-gate /*NOTREACHED*/
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate void
deadkid(void)2408d489c7aSmuffin deadkid(void)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate if (pid >= 0 && waitpid(pid, NULL, WNOHANG) == pid)
2448d489c7aSmuffin tip_abort("Connection Closed");
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate void
cleanup(void)2488d489c7aSmuffin cleanup(void)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate if (uid != getuid()) {
2527c478bd9Sstevel@tonic-gate myperm();
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate delock(uucplock);
2557c478bd9Sstevel@tonic-gate exit(0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * put the controlling keyboard into raw mode
2607c478bd9Sstevel@tonic-gate */
2618d489c7aSmuffin void
raw(void)2628d489c7aSmuffin raw(void)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate
2658d489c7aSmuffin (void) ioctl(0, TCSETSF, (char *)&arg);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * return keyboard to normal mode
2717c478bd9Sstevel@tonic-gate */
2728d489c7aSmuffin void
unraw(void)2738d489c7aSmuffin unraw(void)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate
2768d489c7aSmuffin (void) ioctl(0, TCSETSF, (char *)&defarg);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate * switch to using invoking user's permissions
2817c478bd9Sstevel@tonic-gate */
2828d489c7aSmuffin void
userperm(void)2838d489c7aSmuffin userperm(void)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate
2868d489c7aSmuffin (void) setegid(gid);
2878d489c7aSmuffin (void) seteuid(uid);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate * switch to using my special (setuid) permissions
2927c478bd9Sstevel@tonic-gate */
2938d489c7aSmuffin void
myperm(void)2948d489c7aSmuffin myperm(void)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate
2978d489c7aSmuffin (void) setegid(egid);
2988d489c7aSmuffin (void) seteuid(euid);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate static sigjmp_buf promptbuf;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate * Print string ``s'', then read a string
3057c478bd9Sstevel@tonic-gate * in from the terminal. Handles signals & allows use of
3067c478bd9Sstevel@tonic-gate * normal erase and kill characters.
3077c478bd9Sstevel@tonic-gate */
3088d489c7aSmuffin int
prompt(char * s,char * p,size_t len)3098d489c7aSmuffin prompt(char *s, char *p, size_t len)
3107c478bd9Sstevel@tonic-gate {
3118d489c7aSmuffin char *b = p;
3128d489c7aSmuffin int c;
3138d489c7aSmuffin sig_handler_t ointr, oquit;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate stoprompt = 0;
3168d489c7aSmuffin ointr = signal(SIGINT, (sig_handler_t)intprompt);
3177c478bd9Sstevel@tonic-gate oquit = signal(SIGQUIT, SIG_IGN);
3187c478bd9Sstevel@tonic-gate unraw();
3198d489c7aSmuffin (void) printf("%s", s);
3207c478bd9Sstevel@tonic-gate if (sigsetjmp(promptbuf, 1) == 0)
3217c478bd9Sstevel@tonic-gate while (p < b + len - 1 &&
3227c478bd9Sstevel@tonic-gate ((c = getchar()) != EOF) && (c != '\n'))
3237c478bd9Sstevel@tonic-gate *p++ = c;
3247c478bd9Sstevel@tonic-gate *p = '\0';
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate raw();
3278d489c7aSmuffin (void) signal(SIGINT, ointr);
3288d489c7aSmuffin (void) signal(SIGQUIT, oquit);
3297c478bd9Sstevel@tonic-gate return (stoprompt || p == b);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate * Interrupt service routine during prompting
3347c478bd9Sstevel@tonic-gate */
3357c478bd9Sstevel@tonic-gate void
intprompt(void)3368d489c7aSmuffin intprompt(void)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate
3398d489c7aSmuffin (void) signal(SIGINT, SIG_IGN);
3408d489c7aSmuffin (void) signal(SIGQUIT, SIG_IGN);
3417c478bd9Sstevel@tonic-gate stoprompt = 1;
3428d489c7aSmuffin (void) printf("\r\n");
3437c478bd9Sstevel@tonic-gate siglongjmp(promptbuf, 1);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * ****TIPIN TIPIN****
3487c478bd9Sstevel@tonic-gate */
3498d489c7aSmuffin void
tipin(void)3508d489c7aSmuffin tipin(void)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate unsigned char gch, c;
3537c478bd9Sstevel@tonic-gate int bol = 1;
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate * Kinda klugey here...
3577c478bd9Sstevel@tonic-gate * check for scripting being turned on from the .tiprc file,
3587c478bd9Sstevel@tonic-gate * but be careful about just using setscript(), as we may
3597c478bd9Sstevel@tonic-gate * send a SIGEMT before tipout has a chance to set up catching
3607c478bd9Sstevel@tonic-gate * it; so wait a second, then setscript()
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate if (boolean(value(SCRIPT))) {
3638d489c7aSmuffin (void) sleep(1);
3647c478bd9Sstevel@tonic-gate setscript();
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate for (;;) {
3687c478bd9Sstevel@tonic-gate gch = getchar()&0377;
3697c478bd9Sstevel@tonic-gate if ((gch == character(value(ESCAPE))) && bol) {
3707c478bd9Sstevel@tonic-gate if (!(gch = escape()))
3717c478bd9Sstevel@tonic-gate continue;
3727c478bd9Sstevel@tonic-gate } else if (!cumode && gch == character(value(RAISECHAR))) {
3737c478bd9Sstevel@tonic-gate boolean(value(RAISE)) = !boolean(value(RAISE));
3747c478bd9Sstevel@tonic-gate continue;
3757c478bd9Sstevel@tonic-gate } else if (gch == '\r') {
3767c478bd9Sstevel@tonic-gate bol = 1;
3777c478bd9Sstevel@tonic-gate parwrite(FD, &gch, 1);
3787c478bd9Sstevel@tonic-gate if (boolean(value(HALFDUPLEX)))
3798d489c7aSmuffin (void) printf("\r\n");
3807c478bd9Sstevel@tonic-gate continue;
3817c478bd9Sstevel@tonic-gate } else if (!cumode && gch == character(value(FORCE)))
3827c478bd9Sstevel@tonic-gate gch = getchar()&0377;
3837c478bd9Sstevel@tonic-gate bol = any(gch, value(EOL));
3847c478bd9Sstevel@tonic-gate if (boolean(value(RAISE)) && islower(gch))
3857c478bd9Sstevel@tonic-gate gch = toupper(gch);
3867c478bd9Sstevel@tonic-gate c = gch;
3877c478bd9Sstevel@tonic-gate parwrite(FD, &gch, 1);
3887c478bd9Sstevel@tonic-gate if (boolean(value(HALFDUPLEX)))
3898d489c7aSmuffin (void) putchar(c);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * Escape handler --
3957c478bd9Sstevel@tonic-gate * called on recognition of ``escapec'' at the beginning of a line
3967c478bd9Sstevel@tonic-gate */
3978d489c7aSmuffin unsigned char
escape(void)3988d489c7aSmuffin escape(void)
3997c478bd9Sstevel@tonic-gate {
4008d489c7aSmuffin unsigned char gch;
4018d489c7aSmuffin esctable_t *p;
4027c478bd9Sstevel@tonic-gate char c = character(value(ESCAPE));
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate gch = (getchar()&0377);
4057c478bd9Sstevel@tonic-gate for (p = etable; p->e_char; p++)
4067c478bd9Sstevel@tonic-gate if (p->e_char == gch) {
4077c478bd9Sstevel@tonic-gate if ((p->e_flags&PRIV) && uid)
4087c478bd9Sstevel@tonic-gate continue;
4098d489c7aSmuffin (void) printf("%s", ctrl(c));
4107c478bd9Sstevel@tonic-gate (*p->e_func)(gch);
4117c478bd9Sstevel@tonic-gate return (0);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate /* ESCAPE ESCAPE forces ESCAPE */
4147c478bd9Sstevel@tonic-gate if (c != gch)
4158d489c7aSmuffin parwrite(FD, (unsigned char *)&c, 1);
4167c478bd9Sstevel@tonic-gate return (gch);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate
4198d489c7aSmuffin int
speed(int n)4208d489c7aSmuffin speed(int n)
4217c478bd9Sstevel@tonic-gate {
4228d489c7aSmuffin int *p;
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate for (p = bauds; *p != -1; p++)
4257c478bd9Sstevel@tonic-gate if (*p == n)
4267c478bd9Sstevel@tonic-gate return (p - bauds);
4278d489c7aSmuffin return (0);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate
4308d489c7aSmuffin int
any(char c,char * p)4318d489c7aSmuffin any(char c, char *p)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate while (p && *p)
4347c478bd9Sstevel@tonic-gate if (*p++ == c)
4357c478bd9Sstevel@tonic-gate return (1);
4367c478bd9Sstevel@tonic-gate return (0);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate char *
interp(char * s)4408d489c7aSmuffin interp(char *s)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate static char buf[256];
4438d489c7aSmuffin char *p = buf, c, *q;
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate while (c = *s++) {
4467c478bd9Sstevel@tonic-gate for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
4477c478bd9Sstevel@tonic-gate if (*q++ == c) {
4487c478bd9Sstevel@tonic-gate *p++ = '\\'; *p++ = *q;
4497c478bd9Sstevel@tonic-gate goto next;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate if (c < 040) {
4527c478bd9Sstevel@tonic-gate *p++ = '^'; *p++ = c + 'A'-1;
4537c478bd9Sstevel@tonic-gate } else if (c == 0177) {
4547c478bd9Sstevel@tonic-gate *p++ = '^'; *p++ = '?';
4557c478bd9Sstevel@tonic-gate } else
4567c478bd9Sstevel@tonic-gate *p++ = c;
4577c478bd9Sstevel@tonic-gate next:
4587c478bd9Sstevel@tonic-gate ;
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate *p = '\0';
4617c478bd9Sstevel@tonic-gate return (buf);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate char *
ctrl(char c)4658d489c7aSmuffin ctrl(char c)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate static char s[3];
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate if (c < 040 || c == 0177) {
4707c478bd9Sstevel@tonic-gate s[0] = '^';
4717c478bd9Sstevel@tonic-gate s[1] = c == 0177 ? '?' : c+'A'-1;
4727c478bd9Sstevel@tonic-gate s[2] = '\0';
4737c478bd9Sstevel@tonic-gate } else {
4747c478bd9Sstevel@tonic-gate s[0] = c;
4757c478bd9Sstevel@tonic-gate s[1] = '\0';
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate return (s);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate * Help command
4827c478bd9Sstevel@tonic-gate */
4838d489c7aSmuffin void
help(int c)4848d489c7aSmuffin help(int c)
4857c478bd9Sstevel@tonic-gate {
4868d489c7aSmuffin esctable_t *p;
4877c478bd9Sstevel@tonic-gate
4888d489c7aSmuffin (void) printf("%c\r\n", c);
4897c478bd9Sstevel@tonic-gate for (p = etable; p->e_char; p++) {
4907c478bd9Sstevel@tonic-gate if ((p->e_flags&PRIV) && uid)
4917c478bd9Sstevel@tonic-gate continue;
4928d489c7aSmuffin (void) printf("%2s", ctrl(character(value(ESCAPE))));
4938d489c7aSmuffin (void) printf("%-2s %c %s\r\n", ctrl(p->e_char),
4947c478bd9Sstevel@tonic-gate p->e_flags&EXP ? '*': ' ', p->e_help);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate * Set up the "remote" tty's state
5007c478bd9Sstevel@tonic-gate */
5018d489c7aSmuffin void
ttysetup(int speed)5028d489c7aSmuffin ttysetup(int speed)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate struct termios buf;
5057c478bd9Sstevel@tonic-gate char *loc;
5067c478bd9Sstevel@tonic-gate
5078d489c7aSmuffin (void) ioctl(FD, TCGETS, (char *)&buf);
5087c478bd9Sstevel@tonic-gate buf.c_cflag &= (CREAD|HUPCL|CLOCAL|CRTSCTS|CRTSXOFF);
5097c478bd9Sstevel@tonic-gate buf.c_cflag |= CS8;
5108d489c7aSmuffin (void) cfsetospeed(&buf, speed);
5117c478bd9Sstevel@tonic-gate if (boolean(value(HARDWAREFLOW))) {
5127c478bd9Sstevel@tonic-gate int i = TIOCM_CAR;
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate * Only set hardware flow control if carrier is up,
5167c478bd9Sstevel@tonic-gate * because some devices require both CD and RTS to
5177c478bd9Sstevel@tonic-gate * be up before sending.
5187c478bd9Sstevel@tonic-gate */
5198d489c7aSmuffin (void) ioctl(FD, TIOCMGET, &i);
5207c478bd9Sstevel@tonic-gate if (i & TIOCM_CAR)
5217c478bd9Sstevel@tonic-gate buf.c_cflag |= (CRTSCTS|CRTSXOFF);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * Careful to only penalize the 8-bit users here on the
5267c478bd9Sstevel@tonic-gate * incoming tty port. The default 7-bit users will
5277c478bd9Sstevel@tonic-gate * still get the parity bit from the other side's login
5287c478bd9Sstevel@tonic-gate * process (which happens to be the default for sun tip
5297c478bd9Sstevel@tonic-gate * configurations).
5307c478bd9Sstevel@tonic-gate */
5317c478bd9Sstevel@tonic-gate loc = setlocale(LC_CTYPE, NULL);
5327c478bd9Sstevel@tonic-gate if (noparity && loc != 0 && strcmp(loc, "C") != 0)
5337c478bd9Sstevel@tonic-gate buf.c_iflag = 0;
5347c478bd9Sstevel@tonic-gate else
5357c478bd9Sstevel@tonic-gate buf.c_iflag = ISTRIP;
5367c478bd9Sstevel@tonic-gate buf.c_oflag = 0;
5377c478bd9Sstevel@tonic-gate buf.c_lflag = 0;
5387c478bd9Sstevel@tonic-gate buf.c_cc[VMIN] = 1;
5397c478bd9Sstevel@tonic-gate buf.c_cc[VTIME] = 0;
5408d489c7aSmuffin (void) ioctl(FD, TCSETSF, (char *)&buf);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate * Return "simple" name from a file name,
5457c478bd9Sstevel@tonic-gate * strip leading directories.
5467c478bd9Sstevel@tonic-gate */
5477c478bd9Sstevel@tonic-gate char *
sname(char * s)5488d489c7aSmuffin sname(char *s)
5497c478bd9Sstevel@tonic-gate {
5508d489c7aSmuffin char *p = s;
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate while (*s)
5537c478bd9Sstevel@tonic-gate if (*s++ == '/')
5547c478bd9Sstevel@tonic-gate p = s;
5557c478bd9Sstevel@tonic-gate return (p);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate static char partab[0400];
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate * Do a write to the remote machine with the correct parity.
5627c478bd9Sstevel@tonic-gate * We are doing 8 bit wide output, so we just generate a character
5637c478bd9Sstevel@tonic-gate * with the right parity and output it.
5647c478bd9Sstevel@tonic-gate */
5658d489c7aSmuffin void
parwrite(int fd,unsigned char * buf,int n)5668d489c7aSmuffin parwrite(int fd, unsigned char *buf, int n)
5677c478bd9Sstevel@tonic-gate {
5688d489c7aSmuffin int i;
5698d489c7aSmuffin unsigned char *bp;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate bp = buf;
5727c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
5737c478bd9Sstevel@tonic-gate *bp = partab[(*bp)&0377];
5747c478bd9Sstevel@tonic-gate bp++;
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate if (write(fd, buf, n) < 0) {
5777c478bd9Sstevel@tonic-gate if (errno == EIO || errno == ENXIO)
5788d489c7aSmuffin tip_abort("Lost carrier.");
5797c478bd9Sstevel@tonic-gate /* this is questionable */
5807c478bd9Sstevel@tonic-gate perror("write");
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate * Build a parity table with appropriate high-order bit.
5867c478bd9Sstevel@tonic-gate */
5878d489c7aSmuffin void
setparity(char * defparity)5888d489c7aSmuffin setparity(char *defparity)
5897c478bd9Sstevel@tonic-gate {
5908d489c7aSmuffin int i;
5917c478bd9Sstevel@tonic-gate char *parity;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate if (value(PARITY) == NOSTR)
5947c478bd9Sstevel@tonic-gate value(PARITY) = defparity;
5957c478bd9Sstevel@tonic-gate parity = value(PARITY);
5967c478bd9Sstevel@tonic-gate for (i = 0; i < 0400; i++)
5977c478bd9Sstevel@tonic-gate partab[i] = evenpartab[i];
5988d489c7aSmuffin if (equal(parity, "even")) {
5998d489c7aSmuffin /* EMPTY */
6008d489c7aSmuffin } else if (equal(parity, "odd")) {
6017c478bd9Sstevel@tonic-gate for (i = 0; i < 0400; i++)
6027c478bd9Sstevel@tonic-gate partab[i] ^= 0200; /* reverse bit 7 */
6037c478bd9Sstevel@tonic-gate } else if (equal(parity, "none")) {
6047c478bd9Sstevel@tonic-gate /* Do nothing so we can pass thru 8-bit chars */
6057c478bd9Sstevel@tonic-gate noparity = 1;
6067c478bd9Sstevel@tonic-gate for (i = 0; i < 0400; i++)
6077c478bd9Sstevel@tonic-gate partab[i] = i;
6087c478bd9Sstevel@tonic-gate } else if (equal(parity, "zero")) {
6097c478bd9Sstevel@tonic-gate for (i = 0; i < 0400; i++)
6107c478bd9Sstevel@tonic-gate partab[i] &= ~0200; /* turn off bit 7 */
6117c478bd9Sstevel@tonic-gate } else if (equal(parity, "one")) {
6127c478bd9Sstevel@tonic-gate for (i = 0; i < 0400; i++)
6137c478bd9Sstevel@tonic-gate partab[i] |= 0200; /* turn on bit 7 */
6147c478bd9Sstevel@tonic-gate } else {
6158d489c7aSmuffin (void) fprintf(stderr, "%s: unknown parity value\n", PA);
6168d489c7aSmuffin (void) fflush(stderr);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate }
619