added some hooks for pdx
[unix-history] / usr / src / usr.bin / tip / tip.c
index 5f95728..ece27c6 100644 (file)
@@ -1,50 +1,11 @@
-/*     tip.c   4.3     81/05/18        */
+/*     tip.c   4.11    81/12/16        */
+
 /*
 /*
- * 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
+ * or
+ *  cu phone-number [-s speed] [-l line] [-a acu]
  */
  */
-
 #include "tip.h"
 
 /*
 #include "tip.h"
 
 /*
@@ -55,19 +16,29 @@ 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 */
+#endif
+
 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;
-#ifdef VMUNIX
-       int disc;
-#endif
-       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");
@@ -77,18 +48,46 @@ 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;
+               }
+       }
+
+       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(SIGTERM, cleanup);
        signal(SIGINT, cleanup);
        signal(SIGQUIT, cleanup);
        signal(SIGHUP, cleanup);
        signal(SIGTERM, cleanup);
+
        if ((i = hunt(system)) == 0) {
                printf("all ports busy\n");
                exit(3);
        }
        if (i == -1) {
                printf("link down\n");
        if ((i = hunt(system)) == 0) {
                printf("all ports busy\n");
                exit(3);
        }
        if (i == -1) {
                printf("link down\n");
+               delock(uucplock);
                exit(3);
        }
        setbuf(stdout, NULL);
                exit(3);
        }
        setbuf(stdout, NULL);
@@ -102,9 +101,7 @@ char *argv[];
         */
        setuid(getuid());
        setgid(getgid());
         */
        setuid(getuid());
        setgid(getgid());
-       for (i = 1; i < argc-1; i++)
-               if (equal(argv[i], "-v"))
-                       vflag++;
+
        /*
         * 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
@@ -112,51 +109,53 @@ 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]);
-               }
-       if ((arg.sg_ispeed = speed(number(value(BAUDRATE)))) == NULL) {
+       if ((i = speed(number(value(BAUDRATE)))) == NULL) {
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
                delock(uucplock);
                exit(3);
        }
                printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
                delock(uucplock);
                exit(3);
        }
-       arg.sg_ospeed = arg.sg_ispeed;
+
        /*
        /*
-        * NOTE that remote side runs in TANDEM mode,
-        *  if the host doesn't honor X-ON/X-OFF with default
-        *  start/stop chars, the remote description must be
-        *  extended and tchars will have to be set up here.
-        * If the host doesn't honor TANDEM mode, then watch
-        *  out, as you'll get garbage.
+        * Hardwired connections require the
+        *  line speed set before they make any transmissions
+        *  (this is particularly true of things like a DF03-AC)
         */
         */
-       arg.sg_flags = RAW | TANDEM;
-       ioctl(FD, TIOCSETP, &arg);
-
-       ioctl(0, TIOCGETP, &defarg);    /* store initial status */
-       ioctl(0, TIOCGETC, &defchars);
-       arg = defarg;
-       arg.sg_flags = ANYP | CBREAK;
-       tchars = defchars;
-       tchars.t_intrc = tchars.t_quitc = -1;
-
+       if (HW)
+               ttysetup(i);
        if (p = connect()) {
                printf("\07%s\n[EOT]\n", p);
                delock(uucplock);
                exit(1);
        }
        if (p = connect()) {
                printf("\07%s\n[EOT]\n", p);
                delock(uucplock);
                exit(1);
        }
-       write(1, "\07connected\n", 11);
-       raw();
+       if (!HW)
+               ttysetup(i);
+cucommon:
+       /*
+        * From here down the code is shared with
+        * the "cu" version of tip.
+        */
+       ioctl(0, TIOCGETP, (char *)&defarg);
+       ioctl(0, TIOCGETC, (char *)&defchars);
 #ifdef VMUNIX
        ioctl(0, TIOCGETD, (char *)&odisc);
 #ifdef VMUNIX
        ioctl(0, TIOCGETD, (char *)&odisc);
-       disc = OTTYDISC;
-       ioctl(0, TIOCSETD, (char *)&disc);
 #endif
 #endif
+       arg = defarg;
+       arg.sg_flags = ANYP | CBREAK;
+       tchars = defchars;
+       tchars.t_intrc = tchars.t_quitc = -1;
+       raw();
+
        pipe(fildes); pipe(repdes);
        signal(SIGALRM, timeout);
        pipe(fildes); pipe(repdes);
        signal(SIGALRM, timeout);
+
+       /*
+        * Everything's set up now:
+        *      connection established (hardwired or diaulup)
+        *      line conditioned (baud rate, mode, etc.)
+        *      internal data structures (variables)
+        * so, fork one process for local side and one for remote.
+        */
+       printf(cumode ? "Connected\r\n" : "\07connected\r\n");
        if (pid = fork())
                tipin();
        else
        if (pid = fork())
                tipin();
        else
@@ -164,10 +163,13 @@ char *argv[];
        /*NOTREACHED*/
 }
 
        /*NOTREACHED*/
 }
 
-static
 cleanup()
 {
        delock(uucplock);
 cleanup()
 {
        delock(uucplock);
+#ifdef VMUNIX
+       if (odisc)
+               ioctl(0, TIOCSETD, (char *)&odisc);
+#endif
        exit(0);
 }
 
        exit(0);
 }
 
@@ -178,6 +180,9 @@ raw()
 {
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
 {
        ioctl(0, TIOCSETP, &arg);
        ioctl(0, TIOCSETC, &tchars);
+#ifdef VMUNIX
+       ioctl(0, TIOCSETD, (char *)&disc);
+#endif
 }
 
 
 }
 
 
@@ -186,8 +191,11 @@ raw()
  */
 unraw()
 {
  */
 unraw()
 {
-       ioctl(0, TIOCSETP, &defarg);
-       ioctl(0, TIOCSETC, &defchars);
+#ifdef VMUNIX
+       ioctl(0, TIOCSETD, (char *)&odisc);
+#endif
+       ioctl(0, TIOCSETP, (char *)&defarg);
+       ioctl(0, TIOCSETC, (char *)&defchars);
 }
 
 /*
 }
 
 /*
@@ -253,18 +261,15 @@ 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;
                        write(FD, &gch, 1);
                        continue;
                        continue;
                } else if (gch == '\r') {
                        bol = 1;
                        write(FD, &gch, 1);
                        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))
                        toupper(gch);
                bol = any(gch, value(EOL));
                if (boolean(value(RAISE)) && islower(gch))
                        toupper(gch);
@@ -292,8 +297,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)
+               write(FD, &c, 1);
        return(gch);
 }
 
        return(gch);
 }
 
@@ -386,3 +392,36 @@ help(c)
                        p->e_flags&EXP ? '*': ' ', p->e_help);
        }
 }
                        p->e_flags&EXP ? '*': ' ', p->e_help);
        }
 }
+
+/*
+ * Set up the "remote" tty's state
+ */
+ttysetup(speed)
+{
+#ifdef VMUNIX
+       unsigned bits = LDECCTQ;
+#endif
+
+       arg.sg_ispeed = arg.sg_ospeed = speed;
+       arg.sg_flags = TANDEM|RAW;
+       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);
+}