]> git.ozlabs.org Git - ppp.git/blobdiff - chat/chat.c
Forgot to remote *.ln files (made by lint) when doing make clean.
[ppp.git] / chat / chat.c
index 6206cbea96c86b9d1f38f4ab90c4d6d7212c335c..02fa4499439911ff3d39809f966c01a0f5de15fb 100644 (file)
@@ -14,6 +14,9 @@
  *     This software is in the public domain.
  *
  * -----------------
+ *     22-May-99 added environment substitutuion, enabled with -E switch.
+ *     Andreas Arens <andras@cityweb.de>.
+ *
  *     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>.
  *
  *             Columbus, OH  43221
  *             (614)451-1883
  *
- *
  */
 
+#ifndef __STDC__
+#define const
+#endif
+
 #ifndef lint
-static const char rcsid[] = "$Id: chat.c,v 1.22 1999/08/12 03:56:05 paulus Exp $";
+static const char rcsid[] = "$Id: chat.c,v 1.25 1999/09/06 05:10:23 paulus Exp $";
 #endif
 
 #include <stdio.h>
@@ -166,6 +172,7 @@ int to_stderr     = 0;
 int Verbose       = 0;
 int quiet         = 0;
 int report        = 0;
+int use_env       = 0;
 int exit_code     = 0;
 FILE* report_fp   = (FILE *) 0;
 char *report_file = (char *) 0;
@@ -254,7 +261,7 @@ char *s;
 }
 
 /*
- * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
+ * chat [ -v ] [ -E ] [ -T number ] [ -U number ] [ -t timeout ] [ -f chat-file ] \
  * [ -r report-file ] \
  *             [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
  *
@@ -277,6 +284,10 @@ main(argc, argv)
            ++echo;
            break;
 
+       case 'E':
+           ++use_env;
+           break;
+
        case 'v':
            ++verbose;
            break;
@@ -458,8 +469,8 @@ char *chat_file;
 void usage()
 {
     fprintf(stderr, "\
-Usage: %s [-e] [-v] [-t timeout] [-r report-file] [-T phone-number]\n\
-     [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
+Usage: %s [-e] [-E] [-v] [-V] [-t timeout] [-r report-file]\n\
+     [-T phone-number] [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
     exit(1);
 }
 
@@ -615,26 +626,26 @@ int status;
        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);
@@ -658,10 +669,14 @@ char *clean(s, sending)
 register char *s;
 int sending;  /* set to 1 when sending (putting) this string. */
 {
-    char temp[STR_LEN], cur_chr;
+    char temp[STR_LEN], env_str[STR_LEN], cur_chr;
     register char *s1, *phchar;
     int add_return = sending;
-#define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
+#define isoctal(chr)   (((chr) >= '0') && ((chr) <= '7'))
+#define isalnumx(chr)  ((((chr) >= '0') && ((chr) <= '9')) \
+                        || (((chr) >= 'a') && ((chr) <= 'z')) \
+                        || (((chr) >= 'A') && ((chr) <= 'Z')) \
+                        || (chr) == '_')
 
     s1 = temp;
     while (*s) {
@@ -678,6 +693,18 @@ int sending;  /* set to 1 when sending (putting) this string. */
            }
            continue;
        }
+       
+       if (use_env && cur_chr == '$') {                /* ARI */
+           phchar = env_str;
+           while (isalnumx(*s))
+               *phchar++ = *s++;
+           *phchar = '\0';
+           phchar = getenv(env_str);
+           if (phchar)
+               while (*phchar)
+                   *s1++ = *phchar++;
+           continue;
+       }
 
        if (cur_chr != '\\') {
            *s1++ = cur_chr;
@@ -711,13 +738,12 @@ int sending;  /* set to 1 when sending (putting) this string. */
        case 'd':
            if (sending)
                *s1++ = '\\';
-
            *s1++ = cur_chr;
            break;
 
        case 'T':
            if (sending && phone_num) {
-               for ( phchar = phone_num; *phchar != '\0'; phchar++) 
+               for (phchar = phone_num; *phchar != '\0'; phchar++) 
                    *s1++ = *phchar;
            }
            else {
@@ -728,7 +754,7 @@ int sending;  /* set to 1 when sending (putting) this string. */
 
        case 'U':
            if (sending && phone_num2) {
-               for ( phchar = phone_num2; *phchar != '\0'; phchar++) 
+               for (phchar = phone_num2; *phchar != '\0'; phchar++) 
                    *s1++ = *phchar;
            }
            else {
@@ -766,6 +792,13 @@ int sending;  /* set to 1 when sending (putting) this string. */
                *s1++ = 'N';
            break;
            
+       case '$':                       /* ARI */
+           if (use_env) {
+               *s1++ = cur_chr;
+               break;
+           }
+           /* FALL THROUGH */
+
        default:
            if (isoctal (cur_chr)) {
                cur_chr &= 0x07;
@@ -978,7 +1011,7 @@ register char *s;
 
     if (say_next) {
        say_next = 0;
-       s = clean(s,0);
+       s = clean(s, 1);
        write(2, s, strlen(s));
         free(s);
        return;