]> git.ozlabs.org Git - ppp.git/blobdiff - chat/chat.c
fix problem with REPORTing last line
[ppp.git] / chat / chat.c
index 006841359ee0b9a1848c9a296ee36c0514485ae2..0015e2bb9fa018be052acd8f2492d92c19d50bda 100644 (file)
@@ -14,6 +14,9 @@
  *     This software is in the public domain.
  *
  * -----------------
+ *     12-May-99 added a feature to read data to be sent from a file,
+ *     if the send string starts with @.  Idea from gpk <gpk@onramp.net>.
+ *
  *     added -T and -U option and \T and \U substitution to pass a phone
  *     number into chat script. Two are needed for some ISDN TA applications.
  *     Keith Dart <kdart@cisco.com>
@@ -78,7 +81,7 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: chat.c,v 1.16 1997/11/27 06:00:06 paulus Exp $";
+static const char rcsid[] = "$Id: chat.c,v 1.23 1999/08/13 01:54:32 paulus Exp $";
 #endif
 
 #include <stdio.h>
@@ -106,15 +109,6 @@ static char rcsid[] = "$Id: chat.c,v 1.16 1997/11/27 06:00:06 paulus Exp $";
 #include <termios.h>
 #endif
 
-#if __STDC__
-#include <stdarg.h>
-#define __V(x) x
-#else
-#include <varargs.h>
-#define __V(x) (va_alist) va_dcl
-#define const
-#endif
-
 #define        STR_LEN 1024
 
 #ifndef SIGTYPE
@@ -139,9 +133,17 @@ static char rcsid[] = "$Id: chat.c,v 1.16 1997/11/27 06:00:06 paulus Exp $";
 #define O_NONBLOCK     O_NDELAY
 #endif
 
+#ifdef SUNOS
+extern int sys_nerr;
+extern char *sys_errlist[];
+#define memmove(to, from, n)   bcopy(from, to, n)
+#define strerror(n)            ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
+                                "unknown error")
+#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=='-'\
+                               (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
                                &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
 #define        OPTARG(c,v)     (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
                                (_O=4,(char*)0):(char*)0)
@@ -470,7 +472,7 @@ void logf __V((const char *fmt, ...))
 {
     va_list args;
 
-#if __STDC__
+#ifdef __STDC__
     va_start(args, fmt);
 #else
     char *fmt;
@@ -493,7 +495,7 @@ void fatal __V((int code, const char *fmt, ...))
 {
     va_list args;
 
-#if __STDC__
+#ifdef __STDC__
     va_start(args, fmt);
 #else
     int code;
@@ -607,27 +609,32 @@ void break_sequence()
 void terminate(status)
 int status;
 {
+    static int terminating = 0;
+
+    if (terminating)
+       exit(status);
+    terminating = 1;
     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;
+    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);
+       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 (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
        if (verbose)
            fprintf (report_fp, "Closing \"%s\".\n", report_file);
        fclose (report_fp);
@@ -967,6 +974,8 @@ int c;
 void chat_send (s)
 register char *s;
 {
+    char file_data[STR_LEN];
+
     if (say_next) {
        say_next = 0;
        s = clean(s,0);
@@ -1013,7 +1022,6 @@ register char *s;
 
     if (clear_abort_next) {
        char *s1;
-       char *s2 = s;
        int   i;
         int   old_max;
        int   pack = 0;
@@ -1064,7 +1072,6 @@ register char *s;
 
     if (clear_report_next) {
        char *s1;
-        char *s2 = s;
        int   i;
        int   old_max;
        int   pack = 0;
@@ -1107,6 +1114,43 @@ register char *s;
        return;
     }
 
+    /*
+     * The syntax @filename means read the string to send from the
+     * file `filename'.
+     */
+    if (s[0] == '@') {
+       /* skip the @ and any following white-space */
+       char *fn = s;
+       while (*++fn == ' ' || *fn == '\t')
+           ;
+
+       if (*fn != 0) {
+           FILE *f;
+           int n = 0;
+
+           /* open the file and read until STR_LEN-1 bytes or end-of-file */
+           f = fopen(fn, "r");
+           if (f == NULL)
+               fatal(1, "%s -- open failed: %m", fn);
+           while (n < STR_LEN - 1) {
+               int nr = fread(&file_data[n], 1, STR_LEN - 1 - n, f);
+               if (nr < 0)
+                   fatal(1, "%s -- read error", fn);
+               if (nr == 0)
+                   break;
+               n += nr;
+           }
+           fclose(f);
+
+           /* use the string we got as the string to send,
+              but trim off the final newline if any. */
+           if (n > 0 && file_data[n-1] == '\n')
+               --n;
+           file_data[n] = 0;
+           s = file_data;
+       }
+    }
+
     if (strcmp(s, "EOT") == 0)
        s = "^D\\c";
     else if (strcmp(s, "BREAK") == 0)
@@ -1417,6 +1461,16 @@ register char *string;
     return (0);
 }
 
+/*
+ * Gross kludge to handle Solaris versions >= 2.6 having usleep.
+ */
+#ifdef SOL2
+#include <sys/param.h>
+#if MAXUID > 65536             /* then this is Solaris 2.6 or later */
+#undef NO_USLEEP
+#endif
+#endif /* SOL2 */
+
 #ifdef NO_USLEEP
 #include <sys/types.h>
 #include <sys/time.h>
@@ -1487,7 +1541,6 @@ vfmtmsg(buf, buflen, fmt, args)
     const char *f;
     unsigned char *p;
     char num[32];
-    time_t t;
     static char hexchars[] = "0123456789abcdef";
 
     buf0 = buf;