BSD 4_3 release
[unix-history] / usr / src / usr.bin / uucp / conn.c
index 5cac418..2fb50a2 100644 (file)
@@ -1,38 +1,49 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)conn.c     5.3 (Berkeley) 8/13/83";
+static char sccsid[] = "@(#)conn.c     5.10 (Berkeley) 1/24/86";
 #endif
 
 #endif
 
-#include "uucp.h"
 #include <signal.h>
 #include <signal.h>
+#include "uucp.h"
 #include <setjmp.h>
 #include <ctype.h>
 #include <setjmp.h>
 #include <ctype.h>
-#include <sys/types.h>
-#include <sys/time.h>
 #include <errno.h>
 #include <errno.h>
-#ifdef SYSIII
+#ifdef USG
 #include <termio.h>
 #include <fcntl.h>
 #endif
 #include <termio.h>
 #include <fcntl.h>
 #endif
-#ifndef        SYSIII
+#ifndef        USG
 #include <sgtty.h>
 #endif
 #include <sgtty.h>
 #endif
+#ifdef BSD4_2
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
 
 #define MAXC 1000
 
 extern jmp_buf Sjbuf;
 
 #define MAXC 1000
 
 extern jmp_buf Sjbuf;
-extern int errno;
+jmp_buf Cjbuf;
+extern int errno, onesys;
+extern char *sys_errlist[];
+extern char MaxGrade, DefMaxGrade;
 
 /* Parity control during login procedure */
 #define        P_ZERO  0
 #define        P_ONE   1
 #define        P_EVEN  2
 #define        P_ODD   3
 
 /* Parity control during login procedure */
 #define        P_ZERO  0
 #define        P_ONE   1
 #define        P_EVEN  2
 #define        P_ODD   3
-char   par_tab[128];   /* must be power of two */
 
 
+#define ABORT -2
+
+char   *AbortOn = NULL;
+char   par_tab[128];   /* must be power of two */
+int    linebaudrate;   /* used for the sleep test in pk1.c */
 int next_fd = -1;      /* predicted fd to close interrupted opens */
 int next_fd = -1;      /* predicted fd to close interrupted opens */
-                               /* rti!trt, courtesy unc!smb */
-/***
- *     alarmtr()  -  catch alarm routine for "expect".
+
+char *PCP = "PCP";     /* PC Pursuit device type */
+/*
+ *     catch alarm routine for "expect".
  */
 alarmtr()
 {
  */
 alarmtr()
 {
@@ -45,12 +56,39 @@ alarmtr()
        longjmp(Sjbuf, 1);
 }
 
        longjmp(Sjbuf, 1);
 }
 
-/*******
- *     conn(system)
- *     char *system;
- *
- *     conn - place a telephone call to system and
- *     login, etc.
+/* This template is for seismo to call ihnp4 
+ * the 3 lines marked ---> will be overwritten for the appropriate city
+ */
+#define PCP_BAUD       3
+#define PCP_PHONE      4
+#define PCP_CALLBACK   8
+#define PCP_CITY       10
+#define PCP_RPHONE     12
+#define NPCFIELDS      15
+
+static char *PCFlds[] = {
+       "PC-PURSUIT",
+       "Any",
+       "ACU",
+       "1200",
+       CNULL,  /* <--- **** Welcome to Telenet PC Pursuit ***** */
+       "ABORT",
+       "Good", /* Abort of Good bye! */
+       ")",    /* <--- Enter your 7-digit phone number (xxx-xxxx) */
+       CNULL,  /* ---> 528-1234 */
+       "call?",        /* <--- Which city do you wish to call? */
+       CNULL,  /* ---> CHICAGO */
+       ")",    /* <--- Enter the phone number you wish to call (xxx-xxxx) */
+       CNULL,  /* ---> 690-7171 */
+       "R)?",  /* <--- You are #1 in the queue. Do you want to wait, or Restart (Y/N/R)? */
+       "Y",
+       CNULL   /* <--- .....Good Bye! */
+};
+
+static char PCP_brand[20];
+
+/*
+ *     place a telephone call to system and login, etc.
  *
  *     return codes:
  *             CF_SYSTEM: don't know system
  *
  *     return codes:
  *             CF_SYSTEM: don't know system
@@ -60,56 +98,121 @@ alarmtr()
  *             CF_LOGIN: login/password dialog failed
  *
  *             >0  - file no.  -  connect ok
  *             CF_LOGIN: login/password dialog failed
  *
  *             >0  - file no.  -  connect ok
- *
  */
 
 int Dcf = -1;
  */
 
 int Dcf = -1;
+char *Flds[MAXC/10];
+char LineType[10];
+extern int LocalOnly;
 
 conn(system)
 char *system;
 {
 
 conn(system)
 char *system;
 {
-       int ret, nf;
-       register int fn, fnd;
-       char info[MAXC], *flds[MAXC/10];
+       int nf;
+       char info[MAXC], wkpre[NAMESIZE], file[NAMESIZE];
        register FILE *fsys;
        int fcode = 0;
 
        nf = 0;
        register FILE *fsys;
        int fcode = 0;
 
        nf = 0;
-       fnd = 0;
-
 
        fsys = fopen(SYSFILE, "r");
        ASSERT(fsys != NULL, "CAN'T OPEN", SYSFILE, 0);
 
 
        fsys = fopen(SYSFILE, "r");
        ASSERT(fsys != NULL, "CAN'T OPEN", SYSFILE, 0);
 
-       DEBUG(4, "finds %s\n", "called");
-       while((nf = finds(fsys, system, info, flds)) > 0) {
-               DEBUG(4, "getto %s\n", "called");
-               if ((fn = getto(flds)) > 0) {
-                       fnd = 1;
-                       Dcf = fn;
-                       break;
+       DEBUG(4, "finds (%s) called\n", system);
+keeplooking:
+       while((nf = finds(fsys, system, info, Flds)) > 0) {
+               strncpy(LineType, Flds[F_LINE], 10);
+               if (LocalOnly) {
+                       if (strcmp("TCP", LineType)
+                               && strcmp("DIR", LineType)
+                               && strcmp("LOCAL", LineType) ) {
+                                       fcode = CF_TIME;
+                                       continue;
+                       }
+               }
+               sprintf(wkpre, "%c.%.*s", CMDPRE, SYSNSIZE, Rmtname);
+               if (!onesys && MaxGrade != DefMaxGrade &&
+                       !iswrk(file, "chk", Spool, wkpre))  {
+                               fcode = CF_TIME;
+                               continue;
                }
                }
-               fcode = (fn == FAIL ? CF_DIAL : fn);
+               /* For GTE's PC Pursuit */
+               if (snccmp(LineType, PCP) == SAME) {
+                       FILE *dfp;
+                       int status;
+                       static struct Devices dev;
+                       dfp = fopen(DEVFILE, "r");
+                       ASSERT(dfp != NULL, "Can't open", DEVFILE, 0);
+                       while ((status=rddev(dfp, &dev)) != FAIL
+                               && strcmp(PCP, dev.D_type) != SAME)
+                                       ;
+                       fclose(dfp);
+                       if (status == FAIL)
+                               continue;
+                       if (mlock(PCP) == FAIL) {
+                               fcode = CF_NODEV;
+                               logent("DEVICE", "NO");
+                               continue;
+                       }
+                       PCFlds[PCP_BAUD] = dev.D_class;
+                       PCFlds[PCP_PHONE] = dev.D_calldev;
+                       PCFlds[PCP_CALLBACK] = dev.D_arg[D_CHAT];
+                       PCFlds[PCP_CITY] = Flds[F_CLASS];
+                       PCFlds[PCP_RPHONE] = Flds[F_PHONE];
+                       strncpy(PCP_brand, dev.D_brand, sizeof(PCP_brand));
+                       if ((fcode = getto(PCFlds)) < 0)
+                               continue;
+                       Dcf = fcode;
+                       fcode = login(NPCFIELDS, PCFlds, Dcf);
+                       clsacu(); /* Hang up, they'll call back */
+                       if (fcode != SUCCESS) {
+                               fcode = CF_DIAL;
+                               continue;
+                       }
+                       Flds[F_CLASS] = dev.D_class;
+                       Flds[F_PHONE] = dev.D_line;
+                       
+               } /* end PC Pursuit */
+               if ((fcode = getto(Flds)) > 0) 
+                       break;
        }
        }
-       fclose(fsys);
 
 
-       if (nf <= 0)
-               return(fcode ? fcode : nf);
+       if (nf <= 0) {
+               fclose(fsys);
+               return fcode ? fcode : nf;
+       }
+
+       Dcf = fcode;
 
 
-       DEBUG(4, "login %s\n", "called");
-       ret = login(nf, flds, fn);
-       if (ret < 0) {
+       if (fcode >= 0 && snccmp(LineType, PCP) == SAME) {
+               AbortOn = "Good";       /* .... Good Bye */
+               fcode = expect("****~300", Dcf);
+               if (fcode != SUCCESS) {
+                       DEBUG(4, "\nexpect timed out\n", CNULL);
+                       fcode = CF_DIAL;
+               }
+       }
+       if (fcode >= 0) {
+               DEBUG(4, "login %s\n", "called");
+               fcode = login(nf, Flds, Dcf);
+       }
+       if (fcode < 0) {
                clsacu();
                clsacu();
-               return(CF_LOGIN);
+               if (fcode == ABORT) {
+                       fcode = CF_DIAL;
+                       goto  keeplooking;
+               } else {
+                       fclose(fsys);
+                       return CF_LOGIN;
+               }
        }
        }
-       /* rti!trt:  avoid passing file to children */
-       fioclex(fn);
-       return(fn);
+       fclose(fsys);
+       fioclex(Dcf);
+       return Dcf;
 }
 
 }
 
-/***
- *     getto(flds)             connect to remote machine
- *     char *flds[];
+/*
+ *     connect to remote machine
  *
  *     return codes:
  *             >0  -  file number - ok
  *
  *     return codes:
  *             >0  -  file number - ok
@@ -121,23 +224,42 @@ register char *flds[];
 {
        register struct condev *cd;
        int nulldev(), diropn();
 {
        register struct condev *cd;
        int nulldev(), diropn();
+       char *line;
 
 
-       DEBUG(4, "call: no. %s ", flds[F_PHONE]);
+       DEBUG(4, "getto: call no. %s ", flds[F_PHONE]);
        DEBUG(4, "for sys %s\n", flds[F_NAME]);
 
        DEBUG(4, "for sys %s\n", flds[F_NAME]);
 
+       if (snccmp(flds[F_LINE], "LOCAL") == SAME)
+               line = "ACU";
+       else
+               line = flds[F_LINE];
+#ifdef DIALINOUT
+       if (snccmp(line, "ACU") != SAME)
+               reenable();
+#endif DIALINOUT
        CU_end = nulldev;
        CU_end = nulldev;
-       for (cd = condevs; cd->CU_meth != NULL; cd++) {
-               if (snccmp(cd->CU_meth, flds[F_LINE]) == SAME) {
-                       DEBUG(4, "Using %s to call\n", cd->CU_meth);
-                       return((*(cd->CU_gen))(flds));
+       if (snccmp(line, PCP) == SAME) {
+               for(cd = condevs; cd->CU_meth != NULL; cd++) {
+                       if (snccmp(PCP_brand, cd->CU_brand) == SAME) {
+                               CU_end = cd->CU_clos;
+                               return diropn(flds);
+                       }
+               }
+               logent(PCP_brand, "UNSUPPORTED ACU TYPE");
+       } else {
+               for (cd = condevs; cd->CU_meth != NULL; cd++) {
+                       if (snccmp(cd->CU_meth, line) == SAME) {
+                               DEBUG(4, "Using %s to call\n", cd->CU_meth);
+                               return (*(cd->CU_gen))(flds);
                        }
                }
                        }
                }
-       logent(flds[F_LINE], "getto: Can't find, using DIR");
-       return(diropn(flds));   /* search failed, so use direct */
+               DEBUG(1, "Can't find %s, assuming DIR\n", flds[F_LINE]);
        }
        }
+       return diropn(flds);    /* search failed, so use direct */
+}
 
 
-/***
- *     clsacu()        close call unit
+/*
+ *     close call unit
  *
  *     return codes:  none
  */
  *
  *     return codes:  none
  */
@@ -145,7 +267,24 @@ register char *flds[];
 int (*CU_end)() = nulldev;
 clsacu()
 {
 int (*CU_end)() = nulldev;
 clsacu()
 {
-       (*(CU_end))(Dcf);
+       /* make *sure* Dcf is no longer exclusive.
+        * Otherwise dual call-in/call-out modems could get stuck.
+        * Unfortunately, doing this here is not ideal, but it is the
+        * easiest place to put the call.
+        * Hopefully everyone honors the LCK protocol, of course
+        */
+#ifdef TIOCNXCL
+       if (!IsTcpIp && Dcf >= 0 && ioctl(Dcf, TIOCNXCL, STBNULL) < 0)
+               DEBUG(5, "clsacu ioctl %s\n", sys_errlist[errno]);
+#endif
+       if  (setjmp(Sjbuf))
+               logent(Rmtname, "CLOSE TIMEOUT");
+       else {
+               signal(SIGALRM, alarmtr);
+               alarm(20);
+               (*(CU_end))(Dcf);
+               alarm(0);
+       }
        if (close(Dcf) == 0) {
                DEBUG(4, "fd %d NOT CLOSED by CU_clos\n", Dcf);
                logent("clsacu", "NOT CLOSED by CU_clos");
        if (close(Dcf) == 0) {
                DEBUG(4, "fd %d NOT CLOSED by CU_clos\n", Dcf);
                logent("clsacu", "NOT CLOSED by CU_clos");
@@ -154,10 +293,8 @@ clsacu()
        CU_end = nulldev;
 }
 
        CU_end = nulldev;
 }
 
-/***
- *     exphone - expand phone number for given prefix and number
- *
- *     return code - none
+/*
+ *     expand phone number for given prefix and number
  */
 
 exphone(in, out)
  */
 
 exphone(in, out)
@@ -168,13 +305,13 @@ register char *in, *out;
        char buf[BUFSIZ];
        register char *s1;
 
        char buf[BUFSIZ];
        register char *s1;
 
-       if (!isalpha(*in)) {
+       if (!isascii(*in) || !isalpha(*in)) {
                strcpy(out, in);
                return;
        }
 
        s1=pre;
                strcpy(out, in);
                return;
        }
 
        s1=pre;
-       while (isalpha(*in))
+       while (isascii(*in) && isalpha(*in))
                *s1++ = *in++;
        *s1 = '\0';
        s1 = npart;
                *s1++ = *in++;
        *s1 = '\0';
        s1 = npart;
@@ -187,7 +324,8 @@ register char *in, *out;
                DEBUG(2, "CAN'T OPEN %s\n", DIALFILE);
        else {
                while (cfgets(buf, BUFSIZ, fn)) {
                DEBUG(2, "CAN'T OPEN %s\n", DIALFILE);
        else {
                while (cfgets(buf, BUFSIZ, fn)) {
-                       sscanf(buf, "%s%s", p, tpre);
+                       if (sscanf(buf, "%s%s", p, tpre) != 2)
+                               continue;
                        if (strcmp(p, pre) == SAME)
                                goto found;
                        tpre[0] = '\0';
                        if (strcmp(p, pre) == SAME)
                                goto found;
                        tpre[0] = '\0';
@@ -199,11 +337,10 @@ register char *in, *out;
 
        strcpy(out, tpre);
        strcat(out, npart);
 
        strcpy(out, tpre);
        strcat(out, npart);
-       return;
 }
 
 }
 
-/***
- *     rddev - read and decode a line from device file
+/*
+ *     read and decode a line from device file
  *
  *     return code - FAIL at end-of file; 0 otherwise
  */
  *
  *     return code - FAIL at end-of file; 0 otherwise
  */
@@ -212,23 +349,23 @@ rddev(fp, dev)
 register struct Devices *dev;
 FILE *fp;
 {
 register struct Devices *dev;
 FILE *fp;
 {
-       char *fdig();
-       char buf[BUFSIZ];
-       int na;
-
-       if (!cfgets(buf, BUFSIZ, fp))
-               return(FAIL);
-
-       na = sscanf(buf, "%s%s%s%s%s", dev->D_type, dev->D_line,
-         dev->D_calldev, dev->D_class, dev->D_brand);
-       ASSERT(na >= 4, "BAD DEVICE ENTRY", buf, 0);
-       if (na != 5) dev->D_brand[0] = '\0';
+       register int na;
+
+       if (!cfgets(dev->D_argbfr, sizeof(dev->D_argbfr), fp))
+               return FAIL;
+       na = getargs(dev->D_argbfr, dev->D_arg, 20);
+       ASSERT(na >= 4, "BAD DEVICE ENTRY", dev->D_argbfr, 0);
+       if (na == 4) {
+               dev->D_brand = "";
+               na++;
+       }
        dev->D_speed = atoi(fdig(dev->D_class));
        dev->D_speed = atoi(fdig(dev->D_class));
-       return(0);
+       dev->D_numargs = na;
+       return 0;
 }
 
 }
 
-/***
- *     finds(fsys, sysnam, info, flds) set system attribute vector
+/*
+ *     set system attribute vector
  *
  *     return codes:
  *             >0  -  number of arguments in vector - succeeded
  *
  *     return codes:
  *             >0  -  number of arguments in vector - succeeded
@@ -240,7 +377,6 @@ finds(fsys, sysnam, info, flds)
 char *sysnam, info[], *flds[];
 FILE *fsys;
 {
 char *sysnam, info[], *flds[];
 FILE *fsys;
 {
-       char sysn[8];
        int na;
        int fcode = 0;
 
        int na;
        int fcode = 0;
 
@@ -252,25 +388,22 @@ FILE *fsys;
         *      etc
         */
        while (cfgets(info, MAXC, fsys) != NULL) {
         *      etc
         */
        while (cfgets(info, MAXC, fsys) != NULL) {
-               na = getargs(info, flds);
-               sprintf(sysn, "%.7s", flds[F_NAME]);
-               if (strcmp(sysnam, sysn) != SAME)
+               na = getargs(info, flds, MAXC/10);
+               if (strncmp(sysnam, flds[F_NAME], MAXBASENAME) != SAME)
                        continue;
                        continue;
-               if (ifdate(flds[F_TIME]))
+               if (ifdate(flds[F_TIME]) != FAIL)
                        /*  found a good entry  */
                        /*  found a good entry  */
-                       return(na);
+                       return na;
                DEBUG(2, "Wrong time ('%s') to call\n", flds[F_TIME]);
                fcode = CF_TIME;
        }
                DEBUG(2, "Wrong time ('%s') to call\n", flds[F_TIME]);
                fcode = CF_TIME;
        }
-       return(fcode ? fcode : CF_SYSTEM);
+       return fcode ? fcode : CF_SYSTEM;
 }
 
 }
 
-/***
- *     login(nf, flds, dcr)            do login conversation
- *     char *flds[];
- *     int nf;
+/*
+ *     do login conversation
  *
  *
- *     return codes:  0  |  FAIL
+ *     return codes:  SUCCESS  |  FAIL
  */
 
 login(nf, flds, fn)
  */
 
 login(nf, flds, fn)
@@ -278,42 +411,52 @@ register char *flds[];
 int nf, fn;
 {
        register char *want, *altern;
 int nf, fn;
 {
        register char *want, *altern;
-       extern char *index();
        int k, ok;
 
        int k, ok;
 
-       ASSERT(nf > 4, "TOO FEW LOG FIELDS", "", nf);
+       ASSERT(nf > 4, "TOO FEW LOG FIELDS", CNULL, nf);
+       if (setjmp(Cjbuf))
+               return FAIL;
+       AbortOn = NULL;
        for (k = F_LOGIN; k < nf; k += 2) {
                want = flds[k];
                ok = FAIL;
        for (k = F_LOGIN; k < nf; k += 2) {
                want = flds[k];
                ok = FAIL;
-               while (ok != 0) {
+               while (ok != SUCCESS) {
                        altern = index(want, '-');
                        if (altern != NULL)
                                *altern++ = '\0';
                        altern = index(want, '-');
                        if (altern != NULL)
                                *altern++ = '\0';
-                       DEBUG(4, "wanted %s ", want);
-                       ok = expect(want, fn);
-                       DEBUG(4, "got %s\n", ok ? "?" : "that");
-                       if (ok == 0)
-                               break;
-                       if (altern == NULL) {
-                               logent("LOGIN", "FAILED");
-                               /* close *not* needed here. rti!trt */
-                               return(FAIL);
+                       if (strcmp(want, "ABORT") == 0) {
+                               AbortOn = flds[k+1];
+                               DEBUG(4, "ABORT ON: %s\n", AbortOn);
+                               goto nextfield;
                        }
                        }
-                       want = index(altern, '-');
-                       if (want != NULL)
-                               *want++ = '\0';
-                       sendthem(altern, fn);
+                       DEBUG(4, "wanted \"%s\"\n", want);
+                       ok = expect(want, fn);
+                       DEBUG(4, "got: %s\n", ok ? "?" : "that");
+                       if (ok == FAIL) {
+                               if (altern == NULL) {
+                                       logent("LOGIN", _FAILED);
+                                       return FAIL;
+                               }
+                               want = index(altern, '-');
+                               if (want != NULL)
+                                       *want++ = '\0';
+                               sendthem(altern, fn);
+                       } else
+                               if (ok == ABORT) {
+                                       logent("LOGIN ABORTED", _FAILED);
+                                       return ABORT;
+                               }
                }
                }
-               sleep(2);
+               sleep(1);
                if (k+1 < nf)
                        sendthem(flds[k+1], fn);
                if (k+1 < nf)
                        sendthem(flds[k+1], fn);
+nextfield: ;
        }
        }
-       return(0);
+       return SUCCESS;
 }
 
 
 }
 
 
-/* rti!trt: conditional table generation to support odd speeds */
-/* Suggested in n44a.139 by n44!dan (Dan Ts'o) */
+/* conditional table generation to support odd speeds */
 struct sg_spds {int sp_val, sp_name;} spds[] = {
 #ifdef B50
        {  50,   B50},
 struct sg_spds {int sp_val, sp_name;} spds[] = {
 #ifdef B50
        {  50,   B50},
@@ -361,14 +504,16 @@ struct sg_spds {int sp_val, sp_name;} spds[] = {
        {9600, B9600},
 #endif
 #ifdef B19200
        {9600, B9600},
 #endif
 #ifdef B19200
-       {19200,B19200},
+       {19200, B19200},
+#endif
+#ifdef EXTA
+       {19200, EXTA},
 #endif
        {0, 0}
 };
 
 #endif
        {0, 0}
 };
 
-/***
- *     fixline(tty, spwant)    set speed/echo/mode...
- *     int tty, spwant;
+/*
+ *     set speed/echo/mode...
  *
  *     return codes:  none
  */
  *
  *     return codes:  none
  */
@@ -376,22 +521,21 @@ struct sg_spds {int sp_val, sp_name;} spds[] = {
 fixline(tty, spwant)
 int tty, spwant;
 {
 fixline(tty, spwant)
 int tty, spwant;
 {
-#ifdef SYSIII
+#ifdef USG
        struct termio ttbuf;
        struct termio ttbuf;
-#endif
-#ifndef        SYSIII
+#else  !USG
        struct sgttyb ttbuf;
        struct sgttyb ttbuf;
-#endif
+#endif !USG
        register struct sg_spds *ps;
        int speed = -1;
        register struct sg_spds *ps;
        int speed = -1;
-       int ret;
 
        for (ps = spds; ps->sp_val; ps++)
                if (ps->sp_val == spwant)
                        speed = ps->sp_name;
 
        for (ps = spds; ps->sp_val; ps++)
                if (ps->sp_val == spwant)
                        speed = ps->sp_name;
-       ASSERT(speed >= 0, "BAD SPEED", "", speed);
-#ifdef SYSIII
-       ioctl(tty, TCGETA, &ttbuf);
+       ASSERT(speed >= 0, "BAD SPEED", CNULL, speed);
+#ifdef USG
+       if (ioctl(tty, TCGETA, &ttbuf) < 0)
+               return FAIL;
        /* ttbuf.sg_flags = (ANYP|RAW);
        ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed; */
        ttbuf.c_iflag = (ushort)0;
        /* ttbuf.sg_flags = (ANYP|RAW);
        ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed; */
        ttbuf.c_iflag = (ushort)0;
@@ -400,31 +544,30 @@ int tty, spwant;
        ttbuf.c_lflag = (ushort)0;
        ttbuf.c_cc[VMIN] = 6;
        ttbuf.c_cc[VTIME] = 1;
        ttbuf.c_lflag = (ushort)0;
        ttbuf.c_cc[VMIN] = 6;
        ttbuf.c_cc[VTIME] = 1;
-       ret = ioctl(tty, TCSETA, &ttbuf);
-#endif
-#ifndef        SYSIII
-       ioctl(tty, TIOCGETP, &ttbuf);
+       if (ioctl(tty, TCSETA, &ttbuf) < 0)
+               return FAIL;
+#else  !USG
+       if (ioctl(tty, TIOCGETP, &ttbuf) < 0)
+               return FAIL;
        ttbuf.sg_flags = (ANYP|RAW);
        ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed;
        ttbuf.sg_flags = (ANYP|RAW);
        ttbuf.sg_ispeed = ttbuf.sg_ospeed = speed;
-       ret = ioctl(tty, TIOCSETP, &ttbuf);
+       if (ioctl(tty, TIOCSETP, &ttbuf) < 0)
+               return FAIL;
 #endif
 #endif
-       ASSERT(ret >= 0, "RETURN FROM STTY", "", ret);
-#ifndef        SYSIII
-       ioctl(tty, TIOCHPCL, STBNULL);
-       ioctl(tty, TIOCEXCL, STBNULL);
+#ifndef        USG
+       if (ioctl(tty, TIOCHPCL, STBNULL) < 0)
+               return FAIL;
+       if (ioctl(tty, TIOCEXCL, STBNULL) < 0)
+               return FAIL;
 #endif
 #endif
-       return;
+       linebaudrate = spwant;
+       return SUCCESS;
 }
 
 }
 
+#define MR 100
 
 
-/* Bill Shannon recommends MR 2000, but that takes too much space on PDPs */
-/* Actually, the 'expect' algorithm should be rewritten. */
-#define MR 1000
-
-
-/***
- *     expect(str, fn) look for expected string
- *     char *str;
+/*
+ *     look for expected string
  *
  *     return codes:
  *             0  -  found
  *
  *     return codes:
  *             0  -  found
@@ -437,43 +580,72 @@ register char *str;
 int fn;
 {
        char rdvec[MR];
 int fn;
 {
        char rdvec[MR];
-       register char *rp = rdvec;
-       int kr;
+       register char *rp = rdvec, *strptr;
+       int kr, cnt_char;
        char nextch;
        char nextch;
+       int timo = MAXMSGTIME;
 
 
-       if (strcmp(str, "\"\"") == SAME)
-               return(0);
-       *rp = 0;
-       if (setjmp(Sjbuf)) {
-               return(FAIL);
+       if (*str == '\0' || strcmp(str, "\"\"") == SAME)
+               return SUCCESS;
+       /* Cleanup str, convert \0xx strings to one char  */
+       for (strptr = str; *strptr; strptr++) {
+               if (*strptr == '\\')
+                       switch(*++strptr) {
+                       case 's':
+                               DEBUG(5, "BLANK\n", CNULL);
+                               *strptr = ' ';
+                               break;
+                       default:
+                               strptr--;  /* back up to backslash */
+                               sscanf(strptr + 1,"%o", &cnt_char);
+                               DEBUG(6, "BACKSLASHED %02xH\n", cnt_char);
+                               *strptr = (char) (cnt_char);
+                               strcpy(&strptr[1], &strptr[4]);
+                       }
+       }
+
+       strptr = index(str, '~');
+       if (strptr != NULL) {
+               *strptr++ = '\0';
+               timo = atoi(strptr);
+               if (timo <= 0)
+                       timo = MAXMSGTIME;
        }
        }
+
+       if (setjmp(Sjbuf))
+               return FAIL;
        signal(SIGALRM, alarmtr);
        signal(SIGALRM, alarmtr);
-/* change MAXCHARTIME to MAXMSGTIME, outside while loop -- brl-bmd!dpk */
-       alarm(MAXMSGTIME);
+       alarm(timo);
+       *rp = 0;
        while (notin(str, rdvec)) {
        while (notin(str, rdvec)) {
+               int c;
+               if(AbortOn != NULL && !notin(AbortOn, rdvec)) {
+                       DEBUG(1, "Call aborted on '%s'\n", AbortOn);
+                       alarm(0);
+                       return ABORT;
+               }
                kr = read(fn, &nextch, 1);
                if (kr <= 0) {
                        alarm(0);
                        DEBUG(4, "lost line kr - %d\n, ", kr);
                        logent("LOGIN", "LOST LINE");
                kr = read(fn, &nextch, 1);
                if (kr <= 0) {
                        alarm(0);
                        DEBUG(4, "lost line kr - %d\n, ", kr);
                        logent("LOGIN", "LOST LINE");
-                       return(FAIL);
+                       return FAIL;
                }
                }
-               {
-               int c;
                c = nextch & 0177;
                c = nextch & 0177;
-               DEBUG(4, c >= 040 ? "%c" : "\\%03o", c);
-               }
-               if ((*rp = nextch & 0177) != '\0')
-                       rp++;
-/* Check rdvec before null termination -- cmcl2!salkind */
+               if (c == '\0')
+                       continue;
+               DEBUG(4, (isprint(c) || isspace(c)) ? "%c" : "\\%03o", c);
+               *rp++ = c;
                if (rp >= rdvec + MR) {
                if (rp >= rdvec + MR) {
-                       alarm(0);
-                       return(FAIL);
+                       register char *p;
+                       for (p = rdvec+MR/2; p < rp; p++)
+                               *(p-MR/2) = *p;
+                       rp -= MR/2;
                }
                *rp = '\0';
        }
        alarm(0);
                }
                *rp = '\0';
        }
        alarm(0);
-       return(0);
+       return SUCCESS;
 }
 
 
 }
 
 
@@ -488,23 +660,21 @@ getnextfd()
        close(next_fd = open("/", 0));
 }
 
        close(next_fd = open("/", 0));
 }
 
-/***
- *     sendthem(str, fn)       send line of login sequence
- *     char *str;
+/*
+ *     send line of login sequence
  *
  *     return codes:  none
  */
  *
  *     return codes:  none
  */
-
 sendthem(str, fn)
 register char *str;
 int fn;
 {
        register char *strptr;
        int i, n, cr = 1;
 sendthem(str, fn)
 register char *str;
 int fn;
 {
        register char *strptr;
        int i, n, cr = 1;
+       register char c;
        static int p_init = 0;
 
        static int p_init = 0;
 
-       /* Note: debugging authorized only for privileged users */
-       DEBUG(5, "send %s\n", str);
+       DEBUG(5, "send \"%s\"\n", str);
 
        if (!p_init) {
                p_init++;
 
        if (!p_init) {
                p_init++;
@@ -534,14 +704,17 @@ int fn;
                return;
        }
 
                return;
        }
 
-       /* LF, CR, and "" courtesy unc!smb */
        /* Send a '\n' */
        /* Send a '\n' */
-       if (strcmp(str, "LF") == SAME)
-               str = "\\n\\c";
+       if (strcmp(str, "LF") == SAME) {
+               p_chwrite(fn, '\n');
+               return;
+       }
 
        /* Send a '\r' */
 
        /* Send a '\r' */
-       if (strcmp(str, "CR") == SAME)
-               str = "\\r\\c";
+       if (strcmp(str, "CR") == SAME) {
+               p_chwrite(fn, '\r');
+               return;
+       }
 
        /* Set parity as needed */
        if (strcmp(str, "P_ZERO") == SAME) {
 
        /* Set parity as needed */
        if (strcmp(str, "P_ZERO") == SAME) {
@@ -562,55 +735,71 @@ int fn;
        }
 
        /* If "", just send '\r' */
        }
 
        /* If "", just send '\r' */
-       if (strcmp(str, "\"\"") != SAME)
-       for (strptr = str; *strptr; strptr++) {
-               if (*strptr == '\\') switch(*++strptr) {
-               case 's':
-                       DEBUG(5, "BLANK\n", "");
-                       *strptr = ' ';
-                       break;
-               case 'd':
-                       DEBUG(5, "DELAY\n", "");
-                       sleep(1);
-                       continue;
-               case 'r':
-                       DEBUG(5, "RETURN\n", "");
-                       *strptr = '\r';
-                       break;
-               case 'b':
-                       if (isdigit(*(strptr+1))) {
-                               i = (*++strptr - '0');
-                               if (i <= 0 || i > 10)
+       if (strcmp(str, "\"\"") == SAME) {
+               p_chwrite(fn, '\r');
+               return;
+       }
+
+       strptr = str;
+       while ((c = *strptr++) != '\0') {
+               if (c == '\\') {
+                       switch(*strptr++) {
+                       case '\0':
+                               DEBUG(5, "TRAILING BACKSLASH IGNORED\n", CNULL);
+                               --strptr;
+                               continue;
+                       case 's':
+                               DEBUG(5, "BLANK\n", CNULL);
+                               c = ' ';
+                               break;
+                       case 'd':
+                               DEBUG(5, "DELAY\n", CNULL);
+                               sleep(1);
+                               continue;
+                       case 'n':
+                               DEBUG(5, "NEW LINE\n", CNULL);
+                               c = '\n';
+                               break;
+                       case 'r':
+                               DEBUG(5, "RETURN\n", CNULL);
+                               c = '\r';
+                               break;
+                       case 'b':
+                               if (isdigit(*strptr)) {
+                                       i = (*strptr++ - '0');
+                                       if (i <= 0 || i > 10)
+                                               i = 3;
+                               } else
                                        i = 3;
                                        i = 3;
-                       } else
-                               i = 3;
-                       /* send break */
-                       genbrk(fn, i);
-                       continue;
-               case 'c':
-                       if (*(strptr+1) == '\0') {
-                       DEBUG(5, "NO CR\n", "");
-                               cr = 0;
+                               /* send break */
+                               genbrk(fn, i);
+                               if (*strptr == '\0')
+                                       cr = 0;
                                continue;
                                continue;
-                       }
-                       DEBUG(5, "NO CR - MIDDLE IGNORED\n", "");
-                       continue;
-               default:
-                       if (isdigit(strptr[1])) {
-                               i = 0;
-                               n = 0;
-                               while (isdigit(strptr[1]) && ++n <= 3)
-                                       i = i*8 + (*++strptr - '0');
-                               p_chwrite(fn, i);
+                       case 'c':
+                               if (*strptr == '\0') {
+                                       DEBUG(5, "NO CR\n", CNULL);
+                                       cr = 0;
+                               } else
+                                       DEBUG(5, "NO CR - IGNORED NOT EOL\n", CNULL);
                                continue;
                                continue;
+#define isoctal(x)     ((x >= '0') && (x <= '7'))
+                       default:
+                               if (isoctal(strptr[-1])) {
+                                       i = 0;
+                                       n = 0;
+                                       --strptr;
+                                       while (isoctal(*strptr) && ++n <= 3)
+                                               i = i * 8 + (*strptr++ - '0');
+                                       DEBUG(5, "\\%o\n", i);
+                                       p_chwrite(fn, (char)i);
+                                       continue;
+                               }
                        }
                        }
-                       DEBUG(5, "BACKSLASH\n", "");
-                       strptr--;
                }
                }
-               p_chwrite(fn, *strptr);
+               p_chwrite(fn, c);
        }
 
        }
 
-       /* '\n' changed to '\r'--a better default. rti!trt */
        if (cr)
                p_chwrite(fn, '\r');
        return;
        if (cr)
                p_chwrite(fn, '\r');
        return;
@@ -618,13 +807,13 @@ int fn;
 
 p_chwrite(fd, c)
 int fd;
 
 p_chwrite(fd, c)
 int fd;
-int c;
+char c;
 {
 {
-       char t[2];
-
-       t[0] = par_tab[c&0177];
-       t[1] = '\0';
-       ASSERT(write(fd, t, 1) == 1, "BAD WRITE", "", t[0]);
+       c = par_tab[c&0177];
+       if (write(fd, &c, 1) != 1) {
+               logent(sys_errlist[errno], "BAD WRITE");
+               longjmp(Cjbuf, 2);
+       }
 }
 
 /*
 }
 
 /*
@@ -647,150 +836,100 @@ int type;
        }
 }
 
        }
 }
 
-#define BSPEED B150
-
-/***
- *     genbrk          send a break
- *
- *     return codes;  none
- */
-
-genbrk(fn, bnulls)
-register int fn, bnulls;
-{
-       register int ret;
-#ifdef SYSIII
-       ret = ioctl(fn, TCSBRK, STBNULL);
-       DEBUG(5, "break ioctl ret %d\n", ret);
-#endif
-#ifndef        SYSIII
-#ifdef TIOCSBRK
-       ret = ioctl(fn, TIOCSBRK, STBNULL);
-       DEBUG(5, "break ioctl ret %d\n", ret);
-#ifdef TIOCCBRK
-       ret = write(fn, "\0\0\0\0\0\0\0\0\0\0\0\0", bnulls);
-       ASSERT(ret > 0, "BAD WRITE genbrk", "", ret);
-       sleep(1);
-       ret = ioctl(fn, TIOCCBRK, STBNULL);
-       DEBUG(5, "break ioctl ret %d\n", ret);
-#endif
-       DEBUG(4, "ioctl 1 second break\n", STBNULL);
-#else
-       struct sgttyb ttbuf;
-       register int sospeed;
-
-       ret = ioctl(fn, TIOCGETP, &ttbuf);
-       sospeed = ttbuf.sg_ospeed;
-       ttbuf.sg_ospeed = BSPEED;
-       ret = ioctl(fn, TIOCSETP, &ttbuf);
-       ret = write(fn, "\0\0\0\0\0\0\0\0\0\0\0\0", bnulls);
-       ASSERT(ret > 0, "BAD WRITE genbrk", "", ret);
-       ttbuf.sg_ospeed = sospeed;
-       ret = ioctl(fn, TIOCSETP, &ttbuf);
-       ret = write(fn, "@", 1);
-       ASSERT(ret > 0, "BAD WRITE genbrk", "", ret);
-       DEBUG(4, "sent BREAK nulls - %d\n", bnulls);
-#endif
-#endif
-}
-
-
-/***
- *     notin(sh, lg)   check for occurrence of substring "sh"
- *     char *sh, *lg;
+/*
+ *     check for occurrence of substring "sh"
  *
  *     return codes:
  *             0  -  found the string
  *             1  -  not in the string
  */
  *
  *     return codes:
  *             0  -  found the string
  *             1  -  not in the string
  */
-
 notin(sh, lg)
 register char *sh, *lg;
 {
        while (*lg != '\0') {
 notin(sh, lg)
 register char *sh, *lg;
 {
        while (*lg != '\0') {
-               /* Dave Martingale: permit wild cards in 'expect' */
                if (wprefix(sh, lg))
                if (wprefix(sh, lg))
-                       return(0);
+                       return 0;
                else
                        lg++;
        }
                else
                        lg++;
        }
-       return(1);
+       return 1;
 }
 
 }
 
-
-/*******
- *     ifdate(s)
- *     char *s;
- *
- *     ittvax!swatt
- *     Allow multiple date specifications separated by '|'.
- *     Calls ifadate, formerly "ifdate".
- *
- *     return codes:
- *             see ifadate
+/*
+ *     Allow multiple date specifications separated by ','.
  */
  */
-
-ifdate(s)
-char *s;
+ifdate(p)
+register char *p;
 {
 {
-       register char *p;
-       register int ret;
+       register char *np;
+       register int ret, g;
+       int rtime, i;
 
 
-       for (p = s; p && (*p == '|' ? *++p : *p); p = index(p, '|'))
-               if (ret = ifadate(p))
-                       return(ret);
-       return(0);
-}
+       /*  pick up retry time for failures  */
+       /*  global variable Retrytime is set here  */
+       if ((np = index(p, ';')) == NULL) {
+               Retrytime = RETRYTIME;
+       } else {
+               i = sscanf(np+1, "%d", &rtime);
+               if (i < 1 || rtime < 0)
+                       rtime = 5;
+               Retrytime  = rtime * 60;
+       }
 
 
+       ret = FAIL;
+       MaxGrade = '\0';
+       do {
+               np = strpbrk(p, ",|");  /* prefer , but allow | for compat */
+               if (np)
+                       *np = '\0';
+               g = ifadate(p);
+               DEBUG(11,"ifadate returns %o\n", g);
+               if (g != FAIL) {
+                       ret = SUCCESS;
+                       if (g > MaxGrade)
+                               MaxGrade = g;
+               }
+               if (np)
+                       *np = ',';
+               p = np + 1;
+       } while (np);
+       if (MaxGrade == '\0')
+               MaxGrade = DefMaxGrade;
+       return ret;
+}
 
 
-/*******
- *     ifadate(s)
- *     char *s;
- *
- *     ifadate  -  this routine will check a string (s)
+/*
+ *     this routine will check a string (string)
  *     like "MoTu0800-1730" to see if the present
  *     time is within the given limits.
  *     SIDE EFFECT - Retrytime is set
  *
  *     like "MoTu0800-1730" to see if the present
  *     time is within the given limits.
  *     SIDE EFFECT - Retrytime is set
  *
- *     String alternatives:
- *             Wk - Mo thru Fr
- *             zero or one time means all day
- *             Any - any day
- *
  *     return codes:
  *             0  -  not within limits
  *             1  -  within limits
  */
 
  *     return codes:
  *             0  -  not within limits
  *             1  -  within limits
  */
 
-ifadate(s)
-char *s;
+ifadate(string)
+char *string;
 {
        static char *days[]={
                "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0
        };
        time_t clock;
 {
        static char *days[]={
                "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0
        };
        time_t clock;
-       int rtime;
-       int i, tl, th, tn, flag, dayok=0;
+       register char *s = string;
+       int i, tl, th, tn, dayok=0;
        struct tm *localtime();
        struct tm *tp;
        struct tm *localtime();
        struct tm *tp;
-       char *index();
-       char *p;
+       char *p, MGrade;
 
 
-       /*  pick up retry time for failures  */
-       /*  global variable Retrytime is set here  */
-       if ((p = index(s, ',')) == NULL) {
-               Retrytime = RETRYTIME;
-       }
-       else {
-               i = sscanf(p+1, "%d", &rtime);
-               if (i < 1 || rtime < 5)
-                       rtime = 5;
-               Retrytime  = rtime * 60;
-       }
+       if ((p = index(s, '/')) == NULL)
+               MGrade = DefMaxGrade;
+       else
+               MGrade = p[1];
 
        time(&clock);
        tp = localtime(&clock);
 
        time(&clock);
        tp = localtime(&clock);
-       while (isalpha(*s)) {
+       while (isascii(*s) && isalpha(*s)) {
                for (i = 0; days[i]; i++) {
                        if (prefix(days[i], s))
                                if (tp->tm_wday == i)
                for (i = 0; days[i]; i++) {
                        if (prefix(days[i], s))
                                if (tp->tm_wday == i)
@@ -802,50 +941,49 @@ char *s;
                                dayok = 1;
                if (prefix("Any", s))
                        dayok = 1;
                                dayok = 1;
                if (prefix("Any", s))
                        dayok = 1;
+               if (prefix("Evening", s)) {
+                       /* Sat or Sun */
+                       if (tp->tm_wday == 6 || tp->tm_wday == 0
+                               || tp->tm_hour >= 17 || tp->tm_hour < 8)
+                                       dayok = 1;
+               }
+               if (prefix("Night", s)) {
+                       if (tp->tm_wday == 6  /* Sat */
+                               || tp->tm_hour >= 23 || tp->tm_hour < 8
+                                       /* Sunday before 5pm */
+                               || (tp->tm_wday == 0 && tp->tm_hour < 17))
+                                       dayok = 1;
+               }
+               if (prefix("NonPeak", s)) { /* For Tymnet and PC Pursuit */
+                       /* Sat or Sun */
+                       if (tp->tm_wday == 6 || tp->tm_wday == 0
+                               || tp->tm_hour >= 18 || tp->tm_hour < 7)
+                                       dayok = 1;
+               }
                s++;
        }
 
                s++;
        }
 
-       if (dayok == 0)
-               return(0);
+       if (dayok == 0 && s != string)
+               return FAIL;
        i = sscanf(s, "%d-%d", &tl, &th);
        i = sscanf(s, "%d-%d", &tl, &th);
+       if (i < 2)
+               return MGrade;
        tn = tp->tm_hour * 100 + tp->tm_min;
        tn = tp->tm_hour * 100 + tp->tm_min;
-       if (i < 2)
-               return(1);
-       if (th < tl)
-               flag = 0;  /* set up for crossover 2400 test */
-       else
-               flag = 1;
-       if ((tn >= tl && tn <= th)
-         || (tn >= th && tn <= tl)) /* test for crossover 2400 */
-               return(flag);
-       else
-               return(!flag);
-}
-
-
-/***
- *     char *
- *     lastc(s)        return pointer to last character
- *     char *s;
- *
- */
-
-char *
-lastc(s)
-register char *s;
-{
-       while (*s != '\0') s++;
-       return(s);
+       if (th < tl) {          /* crosses midnight */
+               if (tl <= tn || tn < th)
+                       return MGrade;
+       } else {
+               if (tl <= tn && tn < th)
+                       return MGrade;
+       }
+       return FAIL;
 }
 
 }
 
-
-/***
- *     char *
- *     fdig(cp)        find first digit in string
+/*
+ *     find first digit in string
  *
  *     return - pointer to first digit in string or end of string
  */
  *
  *     return - pointer to first digit in string or end of string
  */
-
 char *
 fdig(cp)
 register char *cp;
 char *
 fdig(cp)
 register char *cp;
@@ -855,33 +993,155 @@ register char *cp;
        for (c = cp; *c; c++)
                if (*c >= '0' && *c <= '9')
                        break;
        for (c = cp; *c; c++)
                if (*c >= '0' && *c <= '9')
                        break;
-       return(c);
+       return c;
 }
 
 }
 
-
 /*
  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
  * Strings are compared as if they contain all capital letters.
  */
 /*
  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
  * Strings are compared as if they contain all capital letters.
  */
-
 snccmp(s1, s2)
 register char *s1, *s2;
 {
        char c1, c2;
 
 snccmp(s1, s2)
 register char *s1, *s2;
 {
        char c1, c2;
 
-       if (islower(*s1)) c1 = toupper(*s1);
-       else c1 = *s1;
-       if (islower(*s2)) c2 = toupper(*s2);
-       else c2 = *s2;
+       if (islower(*s1))
+               c1 = toupper(*s1);
+       else
+               c1 = *s1;
+       if (islower(*s2))
+               c2 = toupper(*s2);
+       else
+               c2 = *s2;
 
        while (c1 == c2) {
 
        while (c1 == c2) {
-               if (*s1++=='\0')
-                       return(0);
+               if (*s1++ == '\0')
+                       return 0;
+               s2++;
+               if (islower(*s1))
+                       c1 = toupper(*s1);
+               else
+                       c1 = *s1;
+               if (islower(*s2))
+                       c2 = toupper(*s2);
+               else
+                       c2 = *s2;
+       }
+       return c1 - c2;
+}
+
+/*
+ * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
+ * Strings are compared as if they contain all capital letters.
+ */
+sncncmp(s1, s2, n)
+register char *s1, *s2;
+register int n;
+{
+       char c1, c2;
+
+       if (islower(*s1))
+               c1 = toupper(*s1);
+       else
+               c1 = *s1;
+       if (islower(*s2))
+               c2 = toupper(*s2);
+       else
+               c2 = *s2;
+
+       while ( --n >= 0 && c1 == c2) {
+               if (*s1++ == '\0')
+                       return 0;
                s2++;
                s2++;
-               if (islower(*s1)) c1 = toupper(*s1);
-               else c1 = *s1;
-               if (islower(*s2)) c2 = toupper(*s2);
-               else c2 = *s2;
+               if (islower(*s1))
+                       c1 = toupper(*s1);
+               else
+                       c1 = *s1;
+               if (islower(*s2))
+                       c2 = toupper(*s2);
+               else
+                       c2 = *s2;
+       }
+       return n<0 ? 0 : (c1 - c2);
+}
+/*
+ * do chat script
+ * occurs after local port is opened,
+ * before 'dialing' the other machine.
+ */
+dochat(dev, flds, fd)
+register struct Devices *dev;
+char *flds[];
+int fd;
+{
+       register int i;
+       register char *p;
+       char bfr[sizeof(dev->D_argbfr)];
+
+       if (dev->D_numargs <= 5)
+               return(0);
+       DEBUG(4, "dochat called %d\n", dev->D_numargs);
+       for (i = 0; i < dev->D_numargs-5; i++) {
+               sprintf(bfr, dev->D_arg[D_CHAT+i], flds[F_PHONE]);
+               if (strcmp(bfr, dev->D_arg[D_CHAT+i])) {
+                       p = malloc((unsigned)strlen(bfr)+1);
+                       if (p != NULL) {
+                               strcpy(p, bfr);
+                               dev->D_arg[D_CHAT+i] = p;
+                       }
+               }
        }
        }
-       return(c1 - c2);
+       /* following is a kludge because login() arglist is a kludge */
+       i = login(dev->D_numargs, &dev->D_arg[D_CHAT-5], fd);
+       /*
+        * If login() last did a sendthem(), must pause so things can settle.
+        * But don't bother if chat failed.
+        */
+       if (i == 0 && (dev->D_numargs&01))
+               sleep(2);
+       return(i);
+}
+
+/*
+ *     fix kill/echo/raw on line
+ *
+ *     return codes:  none
+ */
+fixmode(tty)
+register int tty;
+{
+#ifdef USG
+       struct termio ttbuf;
+#else  !USG
+       struct sgttyb ttbuf;
+#endif !USG
+       register struct sg_spds *ps;
+       int speed;
+
+       if (IsTcpIp)
+               return;
+#ifdef USG
+       ioctl(tty, TCGETA, &ttbuf);
+       ttbuf.c_iflag = ttbuf.c_oflag = ttbuf.c_lflag = (ushort)0;
+       speed = ttbuf.c_cflag &= (CBAUD);
+       ttbuf.c_cflag |= (CS8|CREAD);
+       ttbuf.c_cc[VMIN] = 6;
+       ttbuf.c_cc[VTIME] = 1;
+       ioctl(tty, TCSETA, &ttbuf);
+#else  !USG
+       ioctl(tty, TIOCGETP, &ttbuf);
+       ttbuf.sg_flags = (ANYP | RAW);
+       ioctl(tty, TIOCSETP, &ttbuf);
+       speed = ttbuf.sg_ispeed;
+       ioctl(tty, TIOCEXCL, STBNULL);
+#endif !USG
+
+       for (ps = spds; ps->sp_val; ps++)
+               if (ps->sp_name == speed) {
+                       linebaudrate = ps->sp_val;
+                       DEBUG(9,"Incoming baudrate is %d\n", linebaudrate);
+                       return;
+               }
+       ASSERT(linebaudrate >= 0, "BAD SPEED", CNULL, speed);
 }
 }
+