changes for local info, parity, vadic auto dialers
[unix-history] / usr / src / usr.bin / tip / tip.c
index 08690fe..4e168f2 100644 (file)
@@ -1,55 +1,15 @@
-/*     tip.c   4.6     81/07/13        */
+/*     tip.c   4.14    83/06/15        */
+
 /*
 /*
- * tip - Unix link to other systems
+ * tip - UNIX link to other systems
  *  tip [-v] [-speed] system-name
  *  tip [-v] [-speed] system-name
- *
- * Uses remote file for system descriptions.
- * Current commands (escapes):
- *
- *     ~!      fork a shell on the local machine
- *     ~c      change working directory on local machine
- *     ~^D     exit tip
- *     ~<      fetch file from remote system
- *     ~>      send file to remote system
- *     ~t      take a file from a remote UNIX (uses cat & echo)
- *     ~p      send a file to a remote UNIX (uses cat)
- *     ~|      fetch file from remote system and pipe it to
- *              a local process
- *     ~%      fork and wait for a program which inherits file
- *              descriptors 3 & 4 attached to the remote machine
- *              (optional by CONNECT define)
- *     ~s      set or show variable
- *     ~?      give a help summary
- *
- * Samuel J. Leffler   1-18-81
- *
- * sjl                 2-11-81
- * add auto-dial stuff for the BIZCOMP
- *
- * sjl                 2-14-81
- * cleaned up auto-dialer stuff and added variables
- *
- * sjl                 2-19-81
- * handle quit and interrupt during calls
- *
- * sjl                 3-8-81
- * made to pass lint
- *
- * sjl                 4-11-81
- * mods to handle both FIOCAPACITY and FIONREAD in biz.c
- *
- * sjl                 4-17-81
- * added take and put, made piping stuff work
- * honor uucp locks
- * rewrite remote file stuff for DN-11 like acu's and just to clean
- *   it up
- *
- * sjl                 6-16-81
- * real working setup for DN-11's
+ * or
+ *  cu phone-number [-s speed] [-l line] [-a acu]
  */
  */
-
 #include "tip.h"
 
 #include "tip.h"
 
+static char *sccsid = "@(#)tip.c       4.14 %G%";
+
 /*
  * Baud rate mapping table
  */
 /*
  * Baud rate mapping table
  */
@@ -64,14 +24,23 @@ int disc = OTTYDISC;                /* tip normally runs this way */
 
 int    intprompt();
 int    timeout();
 
 int    intprompt();
 int    timeout();
-static int cleanup();
+int    cleanup();
+char   *sname();
+extern char *sprintf();
 
 main(argc, argv)
 
 main(argc, argv)
-char *argv[];
+       char *argv[];
 {
        char *system = NOSTR;
        register int i;
 {
        char *system = NOSTR;
        register int i;
-       char *p;
+       register char *p;
+       char sbuf[12];
+
+       if (equal(sname(argv[0]), "cu")) {
+               cumain(argc, argv);
+               cumode = 1;
+               goto cucommon;
+       }
 
        if (argc > 4) {
                fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
 
        if (argc > 4) {
                fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
@@ -81,8 +50,36 @@ char *argv[];
                fprintf(stderr, "tip: must be interactive\n");
                exit(1);
        }
                fprintf(stderr, "tip: must be interactive\n");
                exit(1);
        }
-       if (argc > 1 && argv[argc-1][0] != '-')
-               system = argv[argc-1];          /* always last item */
+
+       for (; argc > 1; argv++, argc--) {
+               if (argv[1][0] != '-')
+                       system = argv[1];
+               else switch (argv[1][1]) {
+
+               case 'v':
+                       vflag++;
+                       break;
+
+               case '0': case '1': case '2': case '3': case '4':
+               case '5': case '6': case '7': case '8': case '9':
+                       BR = atoi(&argv[1][1]);
+                       break;
+
+               default:
+                       fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
+                       break;
+               }
+       }
+
+       if (system == NOSTR)
+               goto notnumber;
+       for (p = system; *p; p++)
+               if (isalpha(*p))
+                       goto notnumber;
+       PN = system;            /* system name is really a phone number */
+       system = sprintf(sbuf, "tip%d", BR);
+
+notnumber:
        signal(SIGINT, cleanup);
        signal(SIGQUIT, cleanup);
        signal(SIGHUP, cleanup);
        signal(SIGINT, cleanup);
        signal(SIGQUIT, cleanup);
        signal(SIGHUP, cleanup);
@@ -94,6 +91,7 @@ char *argv[];
        }
        if (i == -1) {
                printf("link down\n");
        }
        if (i == -1) {
                printf("link down\n");
+               delock(uucplock);
                exit(3);
        }
        setbuf(stdout, NULL);
                exit(3);
        }
        setbuf(stdout, NULL);
@@ -105,11 +103,9 @@ char *argv[];
         *  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());
-       for (i = 1; i < argc-1; i++)
-               if (equal(argv[i], "-v"))
-                       vflag++;
+       setuid(getuid());
+
        /*
         * Kludge, their's no easy way to get the initialization
         *   in the right order, so force it here
        /*
         * Kludge, their's no easy way to get the initialization
         *   in the right order, so force it here
@@ -117,13 +113,7 @@ char *argv[];
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
        if ((PH = getenv("PHONES")) == NOSTR)
                PH = "/etc/phones";
        vinit();                                /* init variables */
-       for (i = 1; i < argc-1; i++)
-               if (argv[i][0] == '-' && argv[i][1] != 'v') {
-                       if (isnum(argv[i][1]))
-                               number(value(BAUDRATE)) = atoi(&argv[i][1]);
-                       else
-                               printf("%s: unknown option\n", argv[i]);
-               }
+       setparity();                            /* 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);
@@ -144,12 +134,14 @@ char *argv[];
        }
        if (!HW)
                ttysetup(i);
        }
        if (!HW)
                ttysetup(i);
-
+cucommon:
        /*
        /*
-        * Set up local tty state
+        * From here down the code is shared with
+        * the "cu" version of tip.
         */
        ioctl(0, TIOCGETP, (char *)&defarg);
        ioctl(0, TIOCGETC, (char *)&defchars);
         */
        ioctl(0, TIOCGETP, (char *)&defarg);
        ioctl(0, TIOCGETC, (char *)&defchars);
+       ioctl(0, TIOCGLTC, (char *)&deflchars);
 #ifdef VMUNIX
        ioctl(0, TIOCGETD, (char *)&odisc);
 #endif
 #ifdef VMUNIX
        ioctl(0, TIOCGETD, (char *)&odisc);
 #endif
@@ -157,6 +149,9 @@ char *argv[];
        arg.sg_flags = ANYP | CBREAK;
        tchars = defchars;
        tchars.t_intrc = tchars.t_quitc = -1;
        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);
@@ -169,7 +164,7 @@ char *argv[];
         *      internal data structures (variables)
         * so, fork one process for local side and one for remote.
         */
         *      internal data structures (variables)
         * so, fork one process for local side and one for remote.
         */
-       write(1, "\07connected\r\n", 12);
+       printf(cumode ? "Connected\r\n" : "\07connected\r\n");
        if (pid = fork())
                tipin();
        else
        if (pid = fork())
                tipin();
        else
@@ -177,7 +172,6 @@ char *argv[];
        /*NOTREACHED*/
 }
 
        /*NOTREACHED*/
 }
 
-static
 cleanup()
 {
        delock(uucplock);
 cleanup()
 {
        delock(uucplock);
@@ -195,6 +189,7 @@ raw()
 {
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
 {
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
+       ioctl(0, TIOCSLTC, &ltchars);
 #ifdef VMUNIX
        ioctl(0, TIOCSETD, (char *)&disc);
 #endif
 #ifdef VMUNIX
        ioctl(0, TIOCSETD, (char *)&disc);
 #endif
@@ -211,6 +206,7 @@ unraw()
 #endif
        ioctl(0, TIOCSETP, (char *)&defarg);
        ioctl(0, TIOCSETC, (char *)&defchars);
 #endif
        ioctl(0, TIOCSETP, (char *)&defarg);
        ioctl(0, TIOCSETC, (char *)&defchars);
+       ioctl(0, TIOCSLTC, (char *)&deflchars);
 }
 
 /*
 }
 
 /*
@@ -276,22 +272,23 @@ tipin()
                if ((gch == character(value(ESCAPE))) && bol) {
                        if (!(gch = escape()))
                                continue;
                if ((gch == character(value(ESCAPE))) && bol) {
                        if (!(gch = escape()))
                                continue;
-               } else if (gch == character(value(RAISECHAR))) {
+               } else if (!cumode && gch == character(value(RAISECHAR))) {
                        boolean(value(RAISE)) = !boolean(value(RAISE));
                        boolean(value(RAISE)) = !boolean(value(RAISE));
-                       printf("%s", ctrl(character(value(RAISECHAR))));
                        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;
                        continue;
-               } else if (gch == character(value(FORCE))) {
-                       printf("%s", ctrl(character(value(FORCE))));
+               } else if (!cumode && gch == character(value(FORCE)))
                        gch = getchar()&0177;
                        gch = getchar()&0177;
-               }
                bol = any(gch, value(EOL));
                if (boolean(value(RAISE)) && islower(gch))
                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);
        }
 }
 
        }
 }
 
@@ -315,8 +312,9 @@ escape()
                        (*p->e_func)(gch);
                        return(0);
                }
                        (*p->e_func)(gch);
                        return(0);
                }
-
-       write(FD, &c, 1);
+       /* ESCAPE ESCAPE forces ESCAPE */
+       if (c != gch)
+               pwrite(FD, &c, 1);
        return(gch);
 }
 
        return(gch);
 }
 
@@ -333,6 +331,7 @@ speed(n)
 any(c, p)
        register char c, *p;
 {
 any(c, p)
        register char c, *p;
 {
+       if (p)
        while (*p)
                if (*p++ == c)
                        return(1);
        while (*p)
                if (*p++ == c)
                        return(1);
@@ -413,7 +412,6 @@ help(c)
 /*
  * Set up the "remote" tty's state
  */
 /*
  * Set up the "remote" tty's state
  */
-static
 ttysetup(speed)
 {
 #ifdef VMUNIX
 ttysetup(speed)
 {
 #ifdef VMUNIX
@@ -421,9 +419,88 @@ ttysetup(speed)
 #endif
 
        arg.sg_ispeed = arg.sg_ospeed = speed;
 #endif
 
        arg.sg_ispeed = arg.sg_ospeed = speed;
-       arg.sg_flags = TANDEM|RAW;
+       if (boolean(value(TAND)))
+               arg.sg_flags = TANDEM|RAW;
+       else
+               arg.sg_flags = RAW;
        ioctl(FD, TIOCSETP, (char *)&arg);
 #ifdef VMUNIX
        ioctl(FD, TIOCLBIS, (char *)&bits);
 #endif
 }
        ioctl(FD, TIOCSETP, (char *)&arg);
 #ifdef VMUNIX
        ioctl(FD, TIOCLBIS, (char *)&bits);
 #endif
 }
+
+/*
+ * Return "simple" name from a file name,
+ * strip leading directories.
+ */
+char *
+sname(s)
+       register char *s;
+{
+       register char *p = s;
+
+       while (*s)
+               if (*s++ == '/')
+                       p = s;
+       return (p);
+}
+
+extern char chartab[];
+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 = buf;
+
+       for (i = 0, bp = buf; i < n; i++, bp++) {
+               *bp = partab[(*bp) & 0177];
+       }
+       write(fd, buf, n);
+}
+
+/*
+ * build a parity table with the right high-order bit
+ * copy an even-parity table and doctor it
+ */
+setparity()
+{
+       int i;
+       char *parity;
+
+       if (value(PARITY) == NOSTR)
+               value(PARITY) = "even";
+
+       parity = value(PARITY);
+
+       for (i = 0; i < 0200; i++)
+               partab[i] = chartab[i];
+
+       if (equal(parity, "odd")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] ^= 0200;      /* reverse bit 7 */
+       }
+       else if (equal(parity, "none") || equal(parity, "zero")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] &= ~0200;     /* turn off bit 7 */
+       }
+       else if (equal(parity, "one")) {
+               for (i = 0; i < 0200; i++)
+                       partab[i] |= 0200;      /* turn on bit 7 */
+       }
+       else if (equal(parity, "even")) {
+               /* table is already even parity */
+       }
+       else {
+               fprintf(stderr, "parity value %s unknown\n", PA);
+               fflush(stderr);
+       }
+}