]> git.ozlabs.org Git - ppp.git/blobdiff - chat/chat.c
don't use options.leaf any more
[ppp.git] / chat / chat.c
index a4db3a00d6681a3a01e3c31fe8966729afa20ac0..115ab2d8bdcbba115d25ad4b93cde0b491c2ead3 100644 (file)
@@ -2,6 +2,15 @@
  *     Chat -- a program for automatic session establishment (i.e. dial
  *             the phone and log in).
  *
+ * Standard termination codes:
+ *  0 - successful completion of the script
+ *  1 - invalid argument, expect string too large, etc.
+ *  2 - error on an I/O operation or fatal error condtion.
+ *  3 - timeout waiting for a simple string.
+ *  4 - the first string declared as "ABORT"
+ *  5 - the second string declared as "ABORT"
+ *  6 - ... and so on for successive ABORT strings.
+ *
  *     This software is in the public domain.
  *
  *     Please send all bug reports, requests for information, etc. to:
@@ -9,6 +18,12 @@
  *             Al Longyear (longyear@netcom.com)
  *             (I was the last person to change this code.)
  *
+ *      Added -r "report file" switch & REPORT keyword.
+ *              Robert Geer <bgeer@xmission.com>
+ *
+ *     Added -e "echo" switch & ECHO keyword
+ *             Dick Streefland <dicks@tasking.nl>
+ *
  *     The original author is:
  *
  *             Karl Fox <karl@MorningStar.Com>
  *             1760 Zollinger Road
  *             Columbus, OH  43221
  *             (614)451-1883
+ *
  */
 
-static char rcsid[] = "$Id: chat.c,v 1.5 1995/01/11 03:14:45 paulus Exp $";
+static char rcsid[] = "$Id: chat.c,v 1.12 1996/06/26 00:51:06 paulus Exp $";
 
 #include <stdio.h>
+#include <time.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <errno.h>
@@ -56,6 +73,10 @@ static char rcsid[] = "$Id: chat.c,v 1.5 1995/01/11 03:14:45 paulus Exp $";
 #define const
 #endif
 
+#ifndef O_NONBLOCK
+#define O_NONBLOCK     O_NDELAY
+#endif
+
 /*************** Micro getopt() *********************************************/
 #define        OPTION(c,v)     (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
                                (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
@@ -71,25 +92,43 @@ static int _O = 0;          /* Internal state */
 char *program_name;
 
 #define        MAX_ABORTS              50
+#define        MAX_REPORTS             50
 #define        DEFAULT_CHAT_TIMEOUT    45
 
-int verbose = 0;
-int quiet = 0;
-char *lock_file = (char *)0;
-char *chat_file = (char *)0;
-int timeout = DEFAULT_CHAT_TIMEOUT;
+int echo          = 0;
+int verbose       = 0;
+int Verbose       = 0;
+int quiet         = 0;
+int report        = 0;
+int exit_code     = 0;
+FILE* report_fp   = (FILE *) 0;
+char *report_file = (char *) 0;
+char *chat_file   = (char *) 0;
+int timeout       = DEFAULT_CHAT_TIMEOUT;
 
 int have_tty_parameters = 0;
+
 #ifdef TERMIO
+#define term_parms struct termio
+#define get_term_param(param) ioctl(0, TCGETA, param)
+#define set_term_param(param) ioctl(0, TCSETA, param)
 struct termio saved_tty_parameters;
 #endif
+
 #ifdef TERMIOS
+#define term_parms struct termios
+#define get_term_param(param) tcgetattr(0, param)
+#define set_term_param(param) tcsetattr(0, TCSANOW, param)
 struct termios saved_tty_parameters;
 #endif
 
 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
        fail_buffer[50];
-int n_aborts = 0, abort_next = 0, timeout_next = 0;
+int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
+
+char *report_string[MAX_REPORTS] ;
+char  report_buffer[50] ;
+int n_reports = 0, report_next = 0, report_gathering = 0 ; 
 
 void *dup_mem __P((void *b, size_t c));
 void *copy_of __P((char *s));
@@ -105,17 +144,17 @@ SIGTYPE sighup __P((int signo));
 void unalarm __P((void));
 void init __P((void));
 void set_tty_parameters __P((void));
+void echo_stderr __P((int));
 void break_sequence __P((void));
 void terminate __P((int status));
 void do_file __P((char *chat_file));
-void delay __P((void));
 int  get_string __P((register char *string));
 int  put_string __P((register char *s));
 int  write_char __P((int c));
-int  put_char __P((char c));
+int  put_char __P((int c));
 int  get_char __P((void));
 void chat_send __P((register char *s));
-char *character __P((char c));
+char *character __P((int c));
 void chat_expect __P((register char *s));
 char *clean __P((register char *s, int sending));
 void break_sequence __P((void));
@@ -128,7 +167,9 @@ size_t c;
     {
     void *ans = malloc (c);
     if (!ans)
+        {
        fatal ("memory error!\n");
+        }
     memcpy (ans, b, c);
     return ans;
     }
@@ -140,7 +181,7 @@ char *s;
     }
 
 /*
- *     chat [ -v ] [ -t timeout ] [ -f chat-file ] \
+ *     chat [ -v ] [ -t timeout ] [ -f chat-file ] [ -r report-file ] \
  *             [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
  *
  *     Perform a UUCP-dialer-like chat script on stdin and stdout.
@@ -154,52 +195,94 @@ char **argv;
     char *arg;
 
     program_name = *argv;
+    tzset();
 
     while (option = OPTION(argc, argv))
+        {
        switch (option)
            {
+           case 'e':
+               ++echo;
+               break;
+
            case 'v':
                ++verbose;
                break;
 
-           case 'f':
-               if (arg = OPTARG(argc, argv))
-                   chat_file = copy_of(arg);
-               else
-                   usage();
-
+           case 'V':
+               ++Verbose;
                break;
 
-           case 'l':
+           case 'f':
                if (arg = OPTARG(argc, argv))
-                   lock_file = copy_of(arg);
+                   {
+                   chat_file = copy_of(arg);
+                   }
                else
+                   {
                    usage();
-
+                   }
                break;
 
            case 't':
                if (arg = OPTARG(argc, argv))
+                   {
                    timeout = atoi(arg);
+                   }
                else
+                   {
                    usage();
+                   }
+               break;
 
+           case 'r':
+               arg = OPTARG (argc, argv);
+               if (arg)
+                   {
+                   if (report_fp != NULL)
+                       {
+                       fclose (report_fp);
+                       }
+                   report_file = copy_of (arg);
+                   report_fp   = fopen (report_file, "a");
+                   if (report_fp != NULL)
+                       {
+                       if (verbose)
+                           {
+                           fprintf (report_fp, "Opening \"%s\"...\n",
+                                    report_file);
+                           }
+                       report = 1;
+                       }
+                   }
                break;
 
            default:
                usage();
+               break;
            }
+      }
+/*
+ * Default the report file to the stderr location
+ */
+    if (report_fp == NULL)
+        {
+       report_fp = stderr;
+        }
 
 #ifdef ultrix
     openlog("chat", LOG_PID);
 #else
     openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
 
-    if (verbose) {
+    if (verbose)
+        {
        setlogmask(LOG_UPTO(LOG_INFO));
-    } else {
+        }
+    else
+        {
        setlogmask(LOG_UPTO(LOG_WARNING));
-    }
+        }
 #endif
 
     init();
@@ -208,9 +291,13 @@ char **argv;
        {
        arg = ARG(argc, argv);
        if (arg != NULL)
+           {
            usage();
+           }
        else
+           {
            do_file (chat_file);
+           }
        }
     else
        {
@@ -219,9 +306,30 @@ char **argv;
            chat_expect(arg);
 
            if (arg = ARG(argc, argv))
+               {
                chat_send(arg);
+               }
            }
        }
+/*
+ * Allow the last of the report string to be gathered before we terminate.
+ */
+    while (report_gathering)
+        {
+       int c;
+       c = get_char();
+       if (!iscntrl (c))
+           {
+           int rep_len = strlen (report_buffer);
+           report_buffer[rep_len]     = c;
+           report_buffer[rep_len + 1] = '\0';
+           }
+       else
+           {
+           report_gathering = 0;
+           fprintf (report_fp, "chat:  %s\n", report_buffer);
+           }
+        }
 
     terminate(0);
     }
@@ -238,7 +346,8 @@ char *chat_file;
     char buf [STR_LEN];
     FILE *cfp;
 
-    if ((cfp = fopen (chat_file, "r")) == NULL)
+    cfp = fopen (chat_file, "r");
+    if (cfp == NULL)
        {
        syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
        terminate (1);
@@ -251,7 +360,9 @@ char *chat_file;
        {
        sp = strchr (buf, '\n');
        if (sp)
+           {
            *sp = '\0';
+           }
 
        linect++;
        sp = buf;
@@ -277,19 +388,27 @@ char *chat_file;
                        }
                    
                    if (*sp++ == '\\')
+                       {
                        if (*sp != '\0')
+                           {
                            ++sp;
+                           }
+                       }
                    }
                }
            else
                {
                arg = sp;
                while (*sp != '\0' && *sp != ' ' && *sp != '\t')
+                   {
                    ++sp;
+                   }
                }
 
            if (*sp != '\0')
+               {
                *sp++ = '\0';
+               }
 
            if (sendflg)
                {
@@ -311,7 +430,7 @@ char *chat_file;
 void usage()
     {
     fprintf(stderr, "\
-Usage: %s [-v] [-t timeout] {-f chat-file || chat-script}\n",
+Usage: %s [-e] [-v] [-t timeout] [-r report-file] {-f chat-file | chat-script}\n",
            program_name);
     exit(1);
     }
@@ -357,7 +476,7 @@ void fatal (msg)
 const char *msg;
     {
     syslog(LOG_ERR, "%s", msg);
-    terminate(1);
+    terminate(2);
     }
 
 /*
@@ -369,7 +488,7 @@ void sysfatal (msg)
 const char *msg;
     {
     syslog(LOG_ERR, "%s: %m", msg);
-    terminate(1);
+    terminate(2);
     }
 
 int alarmed = 0;
@@ -385,10 +504,16 @@ int signo;
 
     logflush();
     if ((flags = fcntl(0, F_GETFL, 0)) == -1)
+        {
        sysfatal("Can't get file mode flags on stdin");
+        }
     else
-       if (fcntl(0, F_SETFL, flags | FNDELAY) == -1)
+        {
+       if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
+           {
            sysfatal("Can't set file mode flags on stdin");
+           }
+        }
 
     if (verbose)
        {
@@ -401,10 +526,16 @@ void unalarm()
     int flags;
 
     if ((flags = fcntl(0, F_GETFL, 0)) == -1)
+        {
        sysfatal("Can't get file mode flags on stdin");
+        }
     else
-       if (fcntl(0, F_SETFL, flags & ~FNDELAY) == -1)
+        {
+       if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
+           {
            sysfatal("Can't set file mode flags on stdin");
+           }
+        }
     }
 
 SIGTYPE sigint(signo)
@@ -439,36 +570,29 @@ void init()
 
 void set_tty_parameters()
     {
-#ifdef TERMIO
-    struct termio t;
-
-    if (ioctl(0, TCGETA, &t) < 0)
-       sysfatal("Can't get terminal parameters");
-#endif
-#ifdef TERMIOS
-    struct termios t;
+#if defined(get_term_param)
+    term_parms t;
 
-    if (tcgetattr(0, &t) < 0)
+    if (get_term_param (&t) < 0)
+        {
        sysfatal("Can't get terminal parameters");
-#endif
+        }
 
     saved_tty_parameters = t;
-    have_tty_parameters = 1;
-
-    t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
-    t.c_oflag  = 0;
-    t.c_lflag  = 0;
-    t.c_cc[VERASE] = t.c_cc[VKILL] = 0;
-    t.c_cc[VMIN] = 1;
-    t.c_cc[VTIME] = 0;
-
-#ifdef TERMIO
-    if (ioctl(0, TCSETA, &t) < 0)
-       sysfatal("Can't set terminal parameters");
-#endif
-#ifdef TERMIOS
-    if (tcsetattr(0, TCSANOW, &t) < 0)
+    have_tty_parameters  = 1;
+
+    t.c_iflag     |= IGNBRK | ISTRIP | IGNPAR;
+    t.c_oflag      = 0;
+    t.c_lflag      = 0;
+    t.c_cc[VERASE] =
+    t.c_cc[VKILL]  = 0;
+    t.c_cc[VMIN]   = 1;
+    t.c_cc[VTIME]  = 0;
+
+    if (set_term_param (&t) < 0)
+        {
        sysfatal("Can't set terminal parameters");
+        }
 #endif
     }
 
@@ -482,17 +606,28 @@ void break_sequence()
 void terminate(status)
 int status;
     {
-    if (have_tty_parameters &&
-#ifdef TERMIO
-        ioctl(0, TCSETA, &saved_tty_parameters) < 0
-#endif
-#ifdef TERMIOS
-       tcsetattr(0, TCSANOW, &saved_tty_parameters) < 0
-#endif
-       ) {
-       syslog(LOG_ERR, "Can't restore terminal parameters: %m");
-       exit(1);
+    echo_stderr(-1);
+    if (report_file != (char *) 0 && report_fp != (FILE *) NULL)
+        {
+       if (verbose)
+           {
+           fprintf (report_fp, "Closing \"%s\".\n", report_file);
+           }
+       fclose (report_fp);
+       report_fp = (FILE *) NULL;
         }
+
+#if defined(get_term_param)
+    if (have_tty_parameters)
+        {
+       if (set_term_param (&saved_tty_parameters) < 0)
+           {
+           syslog(LOG_ERR, "Can't restore terminal parameters: %m");
+           exit(1);
+           }
+        }
+#endif
+
     exit(status);
     }
 
@@ -522,7 +657,9 @@ int sending;
                }
            cur_chr &= 0x1F;
            if (cur_chr != 0)
+               {
                *s1++ = cur_chr;
+               }
            continue;
            }
 
@@ -551,9 +688,13 @@ int sending;
 
        case 'c':
            if (sending && *s == '\0')
+               {
                add_return = 0;
+               }
            else
+               {
                *s1++ = cur_chr;
+               }
            break;
 
        case '\\':
@@ -561,7 +702,9 @@ int sending;
        case 'p':
        case 'd':
            if (sending)
+               {
                *s1++ = '\\';
+               }
 
            *s1++ = cur_chr;
            break;
@@ -593,7 +736,9 @@ int sending;
                *s1++ = '\0';
                }
            else
+               {
                *s1++ = 'N';
+               }
            break;
            
        default:
@@ -614,100 +759,180 @@ int sending;
                if (cur_chr != 0 || sending)
                    {
                    if (sending && (cur_chr == '\\' || cur_chr == 0))
+                       {
                        *s1++ = '\\';
+                       }
                    *s1++ = cur_chr;
                    }
                break;
                }
 
            if (sending)
+               {
                *s1++ = '\\';
+               }
            *s1++ = cur_chr;
            break;
            }
        }
 
     if (add_return)
+        {
        *s1++ = '\r';
+        }
 
     *s1++ = '\0'; /* guarantee closure */
     *s1++ = '\0'; /* terminate the string */
     return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
     }
 
+/*
+ * A modified version of 'strtok'. This version skips \ sequences.
+ */
+
+char *expect_strtok (s, term)
+char *s, *term;
+    {
+    static  char *str   = "";
+    int            escape_flag = 0;
+    char   *result;
+/*
+ * If a string was specified then do initial processing.
+ */
+    if (s)
+       {
+       str = s;
+       }
+/*
+ * If this is the escape flag then reset it and ignore the character.
+ */
+    if (*str)
+        {
+       result = str;
+        }
+    else
+        {
+       result = (char *) 0;
+        }
+
+    while (*str)
+       {
+       if (escape_flag)
+           {
+           escape_flag = 0;
+           ++str;
+           continue;
+           }
+
+       if (*str == '\\')
+           {
+           ++str;
+           escape_flag = 1;
+           continue;
+           }
+/*
+ * If this is not in the termination string, continue.
+ */
+       if (strchr (term, *str) == (char *) 0)
+           {
+           ++str;
+           continue;
+           }
+/*
+ * This is the terminator. Mark the end of the string and stop.
+ */
+       *str++ = '\0';
+       break;
+       }
+    return (result);
+    }
+
 /*
  * Process the expect string
  */
-void chat_expect(s)
-register char *s;
+
+void chat_expect (s)
+char *s;
     {
+    char *expect;
+    char *reply;
+
     if (strcmp(s, "ABORT") == 0)
        {
        ++abort_next;
        return;
        }
 
+    if (strcmp(s, "REPORT") == 0)
+       {
+       ++report_next;
+       return;
+       }
+
     if (strcmp(s, "TIMEOUT") == 0)
        {
        ++timeout_next;
        return;
        }
 
-    while (*s)
+    if (strcmp(s, "ECHO") == 0)
+       {
+       ++echo_next;
+       return;
+       }
+/*
+ * Fetch the expect and reply string.
+ */
+    for (;;)
        {
-       register char *hyphen;
+       expect = expect_strtok (s, "-");
+       s      = (char *) 0;
 
-       for (hyphen = s; *hyphen; ++hyphen)
-           if (*hyphen == '-')
-               if (hyphen == s || hyphen[-1] != '\\')
-                   break;
-       
-       if (*hyphen == '-')
+       if (expect == (char *) 0)
            {
-           *hyphen = '\0';
-
-           if (get_string(s))
-               return;
-           else
-               {
-               s = hyphen + 1;
-
-               for (hyphen = s; *hyphen; ++hyphen)
-                   if (*hyphen == '-')
-                       if (hyphen == s || hyphen[-1] != '\\')
-                           break;
-
-               if (*hyphen == '-')
-                   {
-                   *hyphen = '\0';
+           return;
+           }
 
-                   chat_send(s);
-                   s = hyphen + 1;
-                   }
-               else
-                   {
-                   chat_send(s);
-                   return;
-                   }
-               }
+       reply = expect_strtok (s, "-");
+/*
+ * Handle the expect string. If successful then exit.
+ */
+       if (get_string (expect))
+           {
+           return;
+           }
+/*
+ * If there is a sub-reply string then send it. Otherwise any condition
+ * is terminal.
+ */
+       if (reply == (char *) 0 || exit_code != 3)
+           {
+           break;
            }
-       else
-           if (get_string(s))
-               return;
-           else
-               {
-               if (fail_reason)
-                   syslog(LOG_INFO, "Failed (%s)", fail_reason);
-               else
-                   syslog(LOG_INFO, "Failed");
 
-               terminate(1);
-               }
+       chat_send (reply);
+       }
+/*
+ * The expectation did not occur. This is terminal.
+ */
+    if (fail_reason)
+       {
+       syslog(LOG_INFO, "Failed (%s)", fail_reason);
+       }
+    else
+       {
+       syslog(LOG_INFO, "Failed");
        }
+    terminate(exit_code);
     }
 
+/*
+ * Translate the input character to the appropriate string for printing
+ * the data.
+ */
+
 char *character(c)
-char c;
+int c;
     {
     static char string[10];
     char *meta;
@@ -716,12 +941,20 @@ char c;
     c &= 0x7F;
 
     if (c < 32)
+        {
        sprintf(string, "%s^%c", meta, (int)c + '@');
+        }
     else
+        {
        if (c == 127)
+           {
            sprintf(string, "%s^?", meta);
+           }
        else
+           {
            sprintf(string, "%s%c", meta, c);
+           }
+        }
 
     return (string);
     }
@@ -732,18 +965,27 @@ char c;
 void chat_send (s)
 register char *s;
     {
-    if (abort_next)
+    if (echo_next)
        {
+       echo_next = 0;
+       echo = (strcmp(s, "ON") == 0);
+       return;
+       }
+    if (abort_next)
+        {
        char *s1;
-
+       
        abort_next = 0;
-
+       
        if (n_aborts >= MAX_ABORTS)
+           {
            fatal("Too many ABORT strings");
-
+           }
+       
        s1 = clean(s, 0);
-
-       if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
+       
+       if (strlen(s1) > strlen(s)
+           || strlen(s1) + 1 > sizeof(fail_buffer))
            {
            syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
            die();
@@ -756,38 +998,83 @@ register char *s;
            logf("abort on (");
 
            for (s1 = s; *s1; ++s1)
+               {
                logf(character(*s1));
+               }
 
            logf(")\n");
            }
+       return;
        }
-    else
-       if (timeout_next)
+
+    if (report_next)
+        {
+       char *s1;
+       
+       report_next = 0;
+       if (n_reports >= MAX_REPORTS)
            {
-           timeout_next = 0;
-           timeout = atoi(s);
+           fatal("Too many REPORT strings");
+           }
+       
+       s1 = clean(s, 0);
+       
+       if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
+           {
+           syslog(LOG_WARNING, "Illegal or too-long REPORT string ('%s')", s);
+           die();
+           }
+       
+       report_string[n_reports++] = s1;
+       
+       if (verbose)
+           {
+           logf("report (");
+           s1 = s;
+           while (*s1)
+               {
+               logf(character(*s1));
+               ++s1;
+               }
+           logf(")\n");
+           }
+       return;
+        }
 
-           if (timeout <= 0)
-               timeout = DEFAULT_CHAT_TIMEOUT;
+    if (timeout_next)
+        {
+       timeout_next = 0;
+       timeout = atoi(s);
+       
+       if (timeout <= 0)
+           {
+           timeout = DEFAULT_CHAT_TIMEOUT;
+           }
 
-           if (verbose)
-               {
-               syslog(LOG_INFO, "timeout set to %d seconds", timeout);
-               }
+       if (verbose)
+           {
+           syslog(LOG_INFO, "timeout set to %d seconds", timeout);
            }
-       else
+       return;
+        }
+
+    if (strcmp(s, "EOT") == 0)
+        {
+       s = "^D\\c";
+        }
+    else
+        {
+       if (strcmp(s, "BREAK") == 0)
            {
-           if (strcmp(s, "EOT") == 0)
-               s = "^D\\c";
-           else
-               if (strcmp(s, "BREAK") == 0)
-                   s = "\\K\\c";
-           if ( ! put_string(s))
-               {
-               syslog(LOG_INFO, "Failed");
-               terminate(1);
-               }
+           s = "\\K\\c";
            }
+        }
+
+    if (!put_string(s))
+        {
+       syslog(LOG_INFO, "Failed");
+       terminate(1);
+        }
     }
 
 int get_char()
@@ -798,52 +1085,65 @@ int get_char()
     status = read(0, &c, 1);
 
     switch (status)
-       {
-       case 1:
-           return ((int)c & 0x7F);
+        {
+    case 1:
+       return ((int)c & 0x7F);
 
-       default:
-           syslog(LOG_WARNING, "warning: read() on stdin returned %d",
-                  status);
+    default:
+       syslog(LOG_WARNING, "warning: read() on stdin returned %d",
+              status);
 
-       case -1:
-           if ((status = fcntl(0, F_GETFL, 0)) == -1)
-               sysfatal("Can't get file mode flags on stdin");
-           else
-               if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
-                   sysfatal("Can't set file mode flags on stdin");
-
-           return (-1);
-       }
+    case -1:
+       if ((status = fcntl(0, F_GETFL, 0)) == -1)
+           {
+           sysfatal("Can't get file mode flags on stdin");
+           }
+       else
+           {
+           if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
+               {
+               sysfatal("Can't set file mode flags on stdin");
+               }
+           }
+       
+       return (-1);
+        }
     }
 
 int put_char(c)
-char c;
+int c;
     {
     int status;
+    char ch = c;
 
-    delay();
+    usleep(10000);             /* inter-character typing delay (?) */
 
-    status = write(1, &c, 1);
+    status = write(1, &ch, 1);
 
     switch (status)
-       {
-       case 1:
-           return (0);
-
-       default:
-           syslog(LOG_WARNING, "warning: write() on stdout returned %d",
-                  status);
-
-       case -1:
-           if ((status = fcntl(0, F_GETFL, 0)) == -1)
-               sysfatal("Can't get file mode flags on stdin");
-           else
-               if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
-                   sysfatal("Can't set file mode flags on stdin");
-
-           return (-1);
-       }
+        {
+    case 1:
+       return (0);
+       
+    default:
+       syslog(LOG_WARNING, "warning: write() on stdout returned %d",
+              status);
+       
+    case -1:
+       if ((status = fcntl(0, F_GETFL, 0)) == -1)
+           {
+           sysfatal("Can't get file mode flags on stdin");
+           }
+       else
+           {
+           if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
+               {
+               sysfatal("Can't set file mode flags on stdin");
+               }
+           }
+       
+       return (-1);
+        }
     }
 
 int write_char (c)
@@ -853,14 +1153,19 @@ int c;
        {
        extern int errno;
 
-       alarm(0); alarmed = 0;
+       alarm(0);
+       alarmed = 0;
 
        if (verbose)
            {
            if (errno == EINTR || errno == EWOULDBLOCK)
+               {
                syslog(LOG_INFO, " -- write timed out");
+               }
            else
+               {
                syslog(LOG_INFO, " -- write failed: %m");
+               }
            }
        return (0);
        }
@@ -877,13 +1182,17 @@ register char *s;
        logf("send (");
 
        if (quiet)
+           {
            logf("??????");
+           }
        else
            {
            register char *s1 = s;
 
            for (s1 = s; *s1; ++s1)
+               {
                logf(character(*s1));
+               }
            }
 
        logf(")\n");
@@ -898,7 +1207,9 @@ register char *s;
        if (c != '\\')
            {
            if (!write_char (c))
+               {
                return 0;
+               }
            continue;
            }
 
@@ -914,7 +1225,7 @@ register char *s;
            break;
 
        case 'p':
-           usleep(10000); /* 1/100th of a second. */
+           usleep(10000);      /* 1/100th of a second (arg is microseconds) */
            break;
 
        default:
@@ -929,6 +1240,37 @@ register char *s;
     return (1);
     }
 
+/*
+ *     Echo a character to stderr.
+ *     When called with -1, a '\n' character is generated when
+ *     the cursor is not at the beginning of a line.
+ */
+void echo_stderr(n)
+int n;
+    {
+       static int need_lf;
+       char *s;
+
+       switch (n)
+           {
+       case '\r':              /* ignore '\r' */
+           break;
+       case -1:
+           if (need_lf == 0)
+               break;
+           /* fall through */
+       case '\n':
+           write(2, "\n", 1);
+           need_lf = 0;
+           break;
+       default:
+           s = character(n);
+           write(2, s, strlen(s));
+           need_lf = 1;
+           break;
+           }
+    }
+
 /*
  *     'Wait for' this string to appear on this file descriptor.
  */
@@ -951,7 +1293,9 @@ register char *string;
        logf("expect (");
 
        for (s1 = string; *s1; ++s1)
+           {
            logf(character(*s1));
+           }
 
        logf(")\n");
        }
@@ -959,6 +1303,7 @@ register char *string;
     if (len > STR_LEN)
        {
        syslog(LOG_INFO, "expect string is too long");
+       exit_code = 1;
        return 0;
        }
 
@@ -972,22 +1317,71 @@ register char *string;
        return (1);
        }
 
-    alarm(timeout); alarmed = 0;
+    alarm(timeout);
+    alarmed = 0;
 
     while ( ! alarmed && (c = get_char()) >= 0)
        {
-       int n, abort_len;
+       int n, abort_len, report_len;
 
+       if (echo)
+           {
+           echo_stderr(c);
+           }
        if (verbose)
            {
            if (c == '\n')
+               {
                logf("\n");
+               }
            else
+               {
                logf(character(c));
+               }
            }
 
+       if (Verbose) {
+          if (c == '\n')      fputc( '\n', stderr );
+          else if (c != '\r') fprintf( stderr, "%s", character(c) );
+       }
+
        *s++ = c;
 
+       if (!report_gathering)
+           {
+           for (n = 0; n < n_reports; ++n)
+               {
+               if ((report_string[n] != (char*) NULL) &&
+                   s - temp >= (report_len = strlen(report_string[n])) &&
+                   strncmp(s - report_len, report_string[n], report_len) == 0)
+                   {
+                   time_t time_now   = time ((time_t*) NULL);
+                   struct tm* tm_now = localtime (&time_now);
+
+                   strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
+                   strcat (report_buffer, report_string[n]);
+
+                   report_string[n] = (char *) NULL;
+                   report_gathering = 1;
+                   break;
+                   }
+               }
+           }
+       else
+           {
+           if (!iscntrl (c))
+               {
+               int rep_len = strlen (report_buffer);
+               report_buffer[rep_len]     = c;
+               report_buffer[rep_len + 1] = '\0';
+               }
+           else
+               {
+               report_gathering = 0;
+               fprintf (report_fp, "chat:  %s\n", report_buffer);
+               }
+           }
+
        if (s - temp >= len &&
            c == string[len - 1] &&
            strncmp(s - len, string, len) == 0)
@@ -997,32 +1391,39 @@ register char *string;
                logf(" -- got it\n");
                }
 
-           alarm(0); alarmed = 0;
+           alarm(0);
+           alarmed = 0;
            return (1);
            }
 
        for (n = 0; n < n_aborts; ++n)
+           {
            if (s - temp >= (abort_len = strlen(abort_string[n])) &&
                strncmp(s - abort_len, abort_string[n], abort_len) == 0)
-               {
+               {
                if (verbose)
                    {
                    logf(" -- failed\n");
                    }
-
-               alarm(0); alarmed = 0;
+               
+               alarm(0);
+               alarmed = 0;
+               exit_code = n + 4;
                strcpy(fail_reason = fail_buffer, abort_string[n]);
                return (0);
-               }
+               }
+           }
 
        if (s >= end)
            {
-           strncpy(temp, s - minlen, minlen);
+           strncpy (temp, s - minlen, minlen);
            s = temp + minlen;
            }
 
        if (alarmed && verbose)
+           {
            syslog(LOG_WARNING, "warning: alarm synchronization problem");
+           }
        }
 
     alarm(0);
@@ -1030,7 +1431,9 @@ register char *string;
     if (verbose && printed)
        {
        if (alarmed)
+           {
            logf(" -- read timed out\n");
+           }
        else
            {
            logflush();
@@ -1038,12 +1441,12 @@ register char *string;
            }
        }
 
-    alarmed = 0;
+    exit_code = 3;
+    alarmed   = 0;
     return (0);
     }
 
-#ifdef ultrix
-#undef NO_USLEEP
+#ifdef NO_USLEEP
 #include <sys/types.h>
 #include <sys/time.h>
 
@@ -1059,29 +1462,14 @@ usleep( usec )                            /* returns 0 if ok, else -1 */
     long               usec;           /* delay in microseconds */
 {
     static struct                      /* `timeval' */
-       {
-           long        tv_sec;         /* seconds */
-           long        tv_usec;        /* microsecs */
-       }   delay;          /* _select() timeout */
+        {
+       long    tv_sec;         /* seconds */
+       long    tv_usec;        /* microsecs */
+        } delay;           /* _select() timeout */
 
-    delay.tv_sec = usec / 1000000L;
+    delay.tv_sec  = usec / 1000000L;
     delay.tv_usec = usec % 1000000L;
 
     return select( 0, (long *)0, (long *)0, (long *)0, &delay );
 }
 #endif
-
-/*
- *     Delay an amount appropriate for between typed characters.
- */
-void delay()
-    {
-# ifdef NO_USLEEP
-    register int i;
-
-    for (i = 0; i < 30000; ++i)                /* ... did we just say appropriate? */
-       ;
-# else /* NO_USLEEP */
-    usleep(100);
-# endif /* NO_USLEEP */
-    }