]> git.ozlabs.org Git - ppp.git/blobdiff - chat/chat.c
use strlog on SNI
[ppp.git] / chat / chat.c
index 7b805ad9ac1ad17d98b5deb0090ff2b147d05f56..812a785fb5e453293d9a1a6d706974da7dac4ddc 100644 (file)
@@ -21,6 +21,9 @@
  *      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>
  *
  */
 
-static char rcsid[] = "$Id: chat.c,v 1.10 1995/12/18 03:32:46 paulus Exp $";
+#ifndef lint
+static char rcsid[] = "$Id: chat.c,v 1.14 1997/04/30 05:40:50 paulus Exp $";
+#endif
 
 #include <stdio.h>
+#include <ctype.h>
 #include <time.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <syslog.h>
@@ -92,6 +99,7 @@ char *program_name;
 #define        MAX_REPORTS             50
 #define        DEFAULT_CHAT_TIMEOUT    45
 
+int echo          = 0;
 int verbose       = 0;
 int Verbose       = 0;
 int quiet         = 0;
@@ -120,7 +128,7 @@ struct termios saved_tty_parameters;
 
 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] ;
@@ -140,6 +148,7 @@ 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));
@@ -155,6 +164,9 @@ char *clean __P((register char *s, int sending));
 void break_sequence __P((void));
 void terminate __P((int status));
 void die __P((void));
+char *expect_strtok __P((char *, char *));
+
+int main __P((int, char *[]));
 
 void *dup_mem(b, c)
 void *b;
@@ -192,10 +204,14 @@ char **argv;
     program_name = *argv;
     tzset();
 
-    while (option = OPTION(argc, argv))
+    while ((option = OPTION(argc, argv)) != 0)
         {
        switch (option)
            {
+           case 'e':
+               ++echo;
+               break;
+
            case 'v':
                ++verbose;
                break;
@@ -205,7 +221,7 @@ char **argv;
                break;
 
            case 'f':
-               if (arg = OPTARG(argc, argv))
+               if ((arg = OPTARG(argc, argv)) != NULL)
                    {
                    chat_file = copy_of(arg);
                    }
@@ -216,7 +232,7 @@ char **argv;
                break;
 
            case 't':
-               if (arg = OPTARG(argc, argv))
+               if ((arg = OPTARG(argc, argv)) != NULL)
                    {
                    timeout = atoi(arg);
                    }
@@ -292,11 +308,11 @@ char **argv;
        }
     else
        {
-       while (arg = ARG(argc, argv))
+       while ((arg = ARG(argc, argv)) != NULL)
            {
            chat_expect(arg);
 
-           if (arg = ARG(argc, argv))
+           if ((arg = ARG(argc, argv)) != NULL)
                {
                chat_send(arg);
                }
@@ -304,6 +320,7 @@ char **argv;
        }
 
     terminate(0);
+    return 0;
     }
 
 /*
@@ -313,7 +330,7 @@ char **argv;
 void do_file (chat_file)
 char *chat_file;
     {
-    int linect, len, sendflg;
+    int linect, sendflg;
     char *sp, *arg, quote;
     char buf [STR_LEN];
     FILE *cfp;
@@ -402,26 +419,30 @@ char *chat_file;
 void usage()
     {
     fprintf(stderr, "\
-Usage: %s [-v] [-t timeout] [-r report-file] {-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);
     }
 
-char line[256];
+char line[128];
 char *p;
 
 void logf (str)
 const char *str;
-    {
-    p = line + strlen(line);
-    strcat (p, str);
+{
+    int l = strlen(line);
 
-    if (str[strlen(str)-1] == '\n')
-       {
+    if (l + strlen(str) >= sizeof(line)) {
+       syslog(LOG_INFO, "%s", line);
+       l = 0;
+    }
+    strcpy(line + l, str);
+
+    if (str[strlen(str)-1] == '\n') {
        syslog (LOG_INFO, "%s", line);
        line[0] = 0;
-       }
     }
+}
 
 void logflush()
     {
@@ -578,8 +599,28 @@ void break_sequence()
 void terminate(status)
 int status;
     {
+    echo_stderr(-1);
     if (report_file != (char *) 0 && report_fp != (FILE *) NULL)
-        {
+       {
+/*
+ * Allow the last of the report string to be gathered before we terminate.
+ */
+       if (report_gathering) {
+           int c, rep_len;
+
+           rep_len = strlen(report_buffer);
+           while (rep_len + 1 <= sizeof(report_buffer)) {
+               alarm(1);
+               c = get_char();
+               alarm(0);
+               if (c < 0 || iscntrl(c))
+                   break;
+               report_buffer[rep_len] = c;
+               ++rep_len;
+           }
+           report_buffer[rep_len] = 0;
+           fprintf (report_fp, "chat:  %s\n", report_buffer);
+       }
        if (verbose)
            {
            fprintf (report_fp, "Closing \"%s\".\n", report_file);
@@ -681,7 +722,7 @@ int sending;
            break;
 
        case 'q':
-           quiet = ! quiet;
+           quiet = 1;
            break;
 
        case 'r':
@@ -764,50 +805,56 @@ int sending;
 char *expect_strtok (s, term)
 char *s, *term;
     {
-    static char *str        = "";
-    static int escape_flag = 0;
+    static  char *str   = "";
+    int            escape_flag = 0;
     char   *result;
 /*
  * If a string was specified then do initial processing.
  */
     if (s)
        {
-       str         = s;
-       escape_flag = 0;
+       str = s;
        }
+/*
+ * If this is the escape flag then reset it and ignore the character.
+ */
+    if (*str)
+        {
+       result = str;
+        }
+    else
+        {
+       result = (char *) 0;
+        }
 
-    result = (char *) 0;
     while (*str)
        {
-/*
- * Look for the terminator sequence.
- * If the escape flag is set then this character is not the terminator.
- * We assume that term does not contain the backslash character.
- */
-       if (escape_flag || strchr (term, *str) == (char *) 0)
+       if (escape_flag)
            {
-           if (result == (char *) 0)
-               {
-               result = str;
-               }
-
-           if (escape_flag || *str == '\\')
-               {
-               escape_flag = !escape_flag;
-               }
+           escape_flag = 0;
+           ++str;
+           continue;
+           }
 
+       if (*str == '\\')
+           {
            ++str;
+           escape_flag = 1;
            continue;
            }
 /*
- * This is the terminator. If we have a string then this is the end of the
- * scan operation.
+ * If this is not in the termination string, continue.
  */
-       *str++ = '\0';
-       if (result)
+       if (strchr (term, *str) == (char *) 0)
            {
-           break;
+           ++str;
+           continue;
            }
+/*
+ * This is the terminator. Mark the end of the string and stop.
+ */
+       *str++ = '\0';
+       break;
        }
     return (result);
     }
@@ -839,6 +886,12 @@ char *s;
        ++timeout_next;
        return;
        }
+
+    if (strcmp(s, "ECHO") == 0)
+       {
+       ++echo_next;
+       return;
+       }
 /*
  * Fetch the expect and reply string.
  */
@@ -924,6 +977,12 @@ int c;
 void chat_send (s)
 register char *s;
     {
+    if (echo_next)
+       {
+       echo_next = 0;
+       echo = (strcmp(s, "ON") == 0);
+       return;
+       }
     if (abort_next)
         {
        char *s1;
@@ -1128,6 +1187,7 @@ int c;
 int put_string (s)
 register char *s;
     {
+    quiet = 0;
     s = clean(s, 1);
 
     if (verbose)
@@ -1193,6 +1253,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.
  */
@@ -1246,6 +1337,10 @@ register char *string;
        {
        int n, abort_len, report_len;
 
+       if (echo)
+           {
+           echo_stderr(c);
+           }
        if (verbose)
            {
            if (c == '\n')
@@ -1265,38 +1360,6 @@ register char *string;
 
        *s++ = c;
 
-       if (s - temp >= len &&
-           c == string[len - 1] &&
-           strncmp(s - len, string, len) == 0)
-           {
-           if (verbose)
-               {
-               logf(" -- got it\n");
-               }
-
-           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;
-               exit_code = n + 4;
-               strcpy(fail_reason = fail_buffer, abort_string[n]);
-               return (0);
-               }
-           }
-
        if (!report_gathering)
            {
            for (n = 0; n < n_reports; ++n)
@@ -1332,6 +1395,38 @@ register char *string;
                }
            }
 
+       if (s - temp >= len &&
+           c == string[len - 1] &&
+           strncmp(s - len, string, len) == 0)
+           {
+           if (verbose)
+               {
+               logf(" -- got it\n");
+               }
+
+           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;
+               exit_code = n + 4;
+               strcpy(fail_reason = fail_buffer, abort_string[n]);
+               return (0);
+               }
+           }
+
        if (s >= end)
            {
            strncpy (temp, s - minlen, minlen);