fix .sp bug, add -P option and change -F directory handling.
[unix-history] / usr / src / usr.bin / tip / tip.c
index 621dd9e..f328b80 100644 (file)
@@ -1,4 +1,6 @@
-/*     tip.c   4.12    82/07/29        */
+#ifndef lint
+static char sccsid[] = "@(#)tip.c      4.16 (Berkeley) %G%";
+#endif
 
 /*
  * tip - UNIX link to other systems
 
 /*
  * tip - UNIX link to other systems
@@ -16,10 +18,7 @@ int bauds[] = {
        1200, 1800, 2400, 4800, 9600, 19200, -1
 };
 
        1200, 1800, 2400, 4800, 9600, 19200, -1
 };
 
-#ifdef VMUNIX
 int    disc = OTTYDISC;                /* tip normally runs this way */
 int    disc = OTTYDISC;                /* tip normally runs this way */
-#endif
-
 int    intprompt();
 int    timeout();
 int    cleanup();
 int    intprompt();
 int    timeout();
 int    cleanup();
@@ -101,8 +100,8 @@ notnumber:
         *  because locking mechanism on the tty and the accounting
         *  will be bypassed.
         */
         *  because locking mechanism on the tty and the accounting
         *  will be bypassed.
         */
-       setuid(getuid());
        setgid(getgid());
        setgid(getgid());
+       setuid(getuid());
 
        /*
         * Kludge, their's no easy way to get the initialization
 
        /*
         * Kludge, their's no easy way to get the initialization
@@ -111,6 +110,7 @@ notnumber:
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
+       setparity("even");                      /* set the parity table */
        if ((i = speed(number(value(BAUDRATE)))) == NULL) {
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
                delock(uucplock);
        if ((i = speed(number(value(BAUDRATE)))) == NULL) {
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
                delock(uucplock);
@@ -138,13 +138,15 @@ cucommon:
         */
        ioctl(0, TIOCGETP, (char *)&defarg);
        ioctl(0, TIOCGETC, (char *)&defchars);
         */
        ioctl(0, TIOCGETP, (char *)&defarg);
        ioctl(0, TIOCGETC, (char *)&defchars);
-#ifdef VMUNIX
+       ioctl(0, TIOCGLTC, (char *)&deflchars);
        ioctl(0, TIOCGETD, (char *)&odisc);
        ioctl(0, TIOCGETD, (char *)&odisc);
-#endif
        arg = defarg;
        arg.sg_flags = ANYP | CBREAK;
        tchars = defchars;
        tchars.t_intrc = tchars.t_quitc = -1;
        arg = defarg;
        arg.sg_flags = ANYP | CBREAK;
        tchars = defchars;
        tchars.t_intrc = tchars.t_quitc = -1;
+       ltchars = deflchars;
+       ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
+               = ltchars.t_lnextc = -1;
        raw();
 
        pipe(fildes); pipe(repdes);
        raw();
 
        pipe(fildes); pipe(repdes);
@@ -167,11 +169,10 @@ cucommon:
 
 cleanup()
 {
 
 cleanup()
 {
+
        delock(uucplock);
        delock(uucplock);
-#ifdef VMUNIX
        if (odisc)
                ioctl(0, TIOCSETD, (char *)&odisc);
        if (odisc)
                ioctl(0, TIOCSETD, (char *)&odisc);
-#endif
        exit(0);
 }
 
        exit(0);
 }
 
@@ -180,11 +181,11 @@ cleanup()
  */
 raw()
 {
  */
 raw()
 {
+
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
-#ifdef VMUNIX
+       ioctl(0, TIOCSLTC, &ltchars);
        ioctl(0, TIOCSETD, (char *)&disc);
        ioctl(0, TIOCSETD, (char *)&disc);
-#endif
 }
 
 
 }
 
 
@@ -193,13 +194,15 @@ raw()
  */
 unraw()
 {
  */
 unraw()
 {
-#ifdef VMUNIX
+
        ioctl(0, TIOCSETD, (char *)&odisc);
        ioctl(0, TIOCSETD, (char *)&odisc);
-#endif
        ioctl(0, TIOCSETP, (char *)&defarg);
        ioctl(0, TIOCSETC, (char *)&defchars);
        ioctl(0, TIOCSETP, (char *)&defarg);
        ioctl(0, TIOCSETC, (char *)&defchars);
+       ioctl(0, TIOCSLTC, (char *)&deflchars);
 }
 
 }
 
+static jmp_buf promptbuf;
+
 /*
  * Print string ``s'', then read a string
  *  in from the terminal.  Handles signals & allows use of
 /*
  * Print string ``s'', then read a string
  *  in from the terminal.  Handles signals & allows use of
@@ -210,23 +213,22 @@ prompt(s, p)
        register char *p;
 {
        register char *b = p;
        register char *p;
 {
        register char *b = p;
+       int (*oint)(), (*oquit)();
 
        stoprompt = 0;
 
        stoprompt = 0;
-       signal(SIGINT, intprompt);
-       signal(SIGQUIT, SIG_IGN);
+       oint = signal(SIGINT, intprompt);
+       oint = signal(SIGQUIT, SIG_IGN);
        unraw();
        printf("%s", s);
        unraw();
        printf("%s", s);
-       while ((*p = getchar()) != EOF && *p != '\n') {
-               if (stoprompt)
-                       goto pbreak;
-               p++;
-       }
+       if (setjmp(promptbuf) == 0)
+               while ((*p = getchar()) != EOF && *p != '\n')
+                       p++;
        *p = '\0';
        *p = '\0';
-pbreak:
+
        raw();
        raw();
-       signal(SIGINT, SIG_DFL);
-       signal(SIGQUIT,SIG_DFL);
-       return(stoprompt || p == b);
+       signal(SIGINT, oint);
+       signal(SIGQUIT, oint);
+       return (stoprompt || p == b);
 }
 
 /*
 }
 
 /*
@@ -234,9 +236,11 @@ pbreak:
  */
 intprompt()
 {
  */
 intprompt()
 {
+
        signal(SIGINT, SIG_IGN);
        stoprompt = 1;
        printf("\r\n");
        signal(SIGINT, SIG_IGN);
        stoprompt = 1;
        printf("\r\n");
+       longjmp(promptbuf, 1);
 }
 
 /*
 }
 
 /*
@@ -268,14 +272,18 @@ tipin()
                        continue;
                } else if (gch == '\r') {
                        bol = 1;
                        continue;
                } else if (gch == '\r') {
                        bol = 1;
-                       write(FD, &gch, 1);
+                       pwrite(FD, &gch, 1);
+                       if (boolean(value(HALFDUPLEX)))
+                               printf("\r\n");
                        continue;
                } else if (!cumode && gch == character(value(FORCE)))
                        gch = getchar()&0177;
                bol = any(gch, value(EOL));
                if (boolean(value(RAISE)) && islower(gch))
                        continue;
                } else if (!cumode && gch == character(value(FORCE)))
                        gch = getchar()&0177;
                bol = any(gch, value(EOL));
                if (boolean(value(RAISE)) && islower(gch))
-                       toupper(gch);
-               write(FD, &gch, 1);
+                       gch = toupper(gch);
+               pwrite(FD, &gch, 1);
+               if (boolean(value(HALFDUPLEX)))
+                       printf("%c", gch);
        }
 }
 
        }
 }
 
@@ -297,41 +305,42 @@ escape()
                                continue;
                        printf("%s", ctrl(c));
                        (*p->e_func)(gch);
                                continue;
                        printf("%s", ctrl(c));
                        (*p->e_func)(gch);
-                       return(0);
+                       return (0);
                }
        /* ESCAPE ESCAPE forces ESCAPE */
        if (c != gch)
                }
        /* ESCAPE ESCAPE forces ESCAPE */
        if (c != gch)
-               write(FD, &c, 1);
-       return(gch);
+               pwrite(FD, &c, 1);
+       return (gch);
 }
 
 speed(n)
 }
 
 speed(n)
+       int n;
 {
        register int *p;
 
        for (p = bauds; *p != -1;  p++)
                if (*p == n)
 {
        register int *p;
 
        for (p = bauds; *p != -1;  p++)
                if (*p == n)
-                       return(p-bauds);
-       return(NULL);
+                       return (p - bauds);
+       return (NULL);
 }
 
 any(c, p)
        register char c, *p;
 {
 }
 
 any(c, p)
        register char c, *p;
 {
-       if (p)
-       while (*p)
+       while (p && *p)
                if (*p++ == c)
                if (*p++ == c)
-                       return(1);
-       return(0);
+                       return (1);
+       return (0);
 }
 
 size(s)
        register char   *s;
 {
 }
 
 size(s)
        register char   *s;
 {
-       register int    i = 0;
+       register int i = 0;
 
 
-       while (*s++) i++;
-       return(i);
+       while (s && *s++)
+               i++;
+       return (i);
 }
 
 char *
 }
 
 char *
@@ -357,7 +366,7 @@ interp(s)
                ;
        }
        *p = '\0';
                ;
        }
        *p = '\0';
-       return(buf);
+       return (buf);
 }
 
 char *
 }
 
 char *
@@ -374,7 +383,7 @@ ctrl(c)
                s[0] = c;
                s[1] = '\0';
        }
                s[0] = c;
                s[1] = '\0';
        }
-       return(s);
+       return (s);
 }
 
 /*
 }
 
 /*
@@ -400,17 +409,16 @@ help(c)
  * Set up the "remote" tty's state
  */
 ttysetup(speed)
  * Set up the "remote" tty's state
  */
 ttysetup(speed)
+       int speed;
 {
 {
-#ifdef VMUNIX
        unsigned bits = LDECCTQ;
        unsigned bits = LDECCTQ;
-#endif
 
        arg.sg_ispeed = arg.sg_ospeed = speed;
 
        arg.sg_ispeed = arg.sg_ospeed = speed;
-       arg.sg_flags = TANDEM|RAW;
+       arg.sg_flags = RAW;
+       if (boolean(value(TAND)))
+               arg.sg_flags |= TANDEM;
        ioctl(FD, TIOCSETP, (char *)&arg);
        ioctl(FD, TIOCSETP, (char *)&arg);
-#ifdef VMUNIX
        ioctl(FD, TIOCLBIS, (char *)&bits);
        ioctl(FD, TIOCLBIS, (char *)&bits);
-#endif
 }
 
 /*
 }
 
 /*
@@ -428,3 +436,62 @@ sname(s)
                        p = s;
        return (p);
 }
                        p = s;
        return (p);
 }
+
+static char partab[0200];
+
+/*
+ * Do a write to the remote machine with the correct parity.
+ * We are doing 8 bit wide output, so we just generate a character
+ * with the right parity and output it.
+ */
+pwrite(fd, buf, n)
+       int fd;
+       char *buf;
+       register int n;
+{
+       register int i;
+       register char *bp;
+
+       bp = buf;
+       for (i = 0; i < n; i++) {
+               *bp = partab[(*bp) & 0177];
+               bp++;
+       }
+       write(fd, buf, n);
+}
+
+/*
+ * Build a parity table with appropriate high-order bit.
+ */
+setparity(defparity)
+       char *defparity;
+{
+       register int i;
+       char *parity;
+       extern char evenpartab[];
+
+       if (value(PARITY) == NOSTR)
+               value(PARITY) = defparity;
+       parity = value(PARITY);
+       for (i = 0; i < 0200; i++)
+               partab[i] = evenpartab[i];
+       if (equal(parity, "even"))
+               return;
+       if (equal(parity, "odd")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] ^= 0200;      /* reverse bit 7 */
+               return;
+       }
+       if (equal(parity, "none") || equal(parity, "zero")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] &= ~0200;     /* turn off bit 7 */
+               return;
+       }
+       if (equal(parity, "one")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] |= 0200;      /* turn on bit 7 */
+               return;
+       }
+       fprintf(stderr, "%s: unknown parity value\n", PA);
+       fflush(stderr);
+}