summaryrefslogtreecommitdiff
path: root/chat
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@samba.org>1999-09-06 05:10:23 +0000
committerPaul Mackerras <paulus@samba.org>1999-09-06 05:10:23 +0000
commiteaca954c2d4a4336c955f4a973ae85dd68d99862 (patch)
treef050d0a8a0be43e0e315983b01a7f681ed065a59 /chat
parent10a68fd183c5e16e95fce56e7b292f1436b1e714 (diff)
add -E option to use environment variables, from Andreas Arens
Diffstat (limited to 'chat')
-rw-r--r--chat/chat.822
-rw-r--r--chat/chat.c50
2 files changed, 60 insertions, 12 deletions
diff --git a/chat/chat.8 b/chat/chat.8
index 7d4ad5f..8c031f5 100644
--- a/chat/chat.8
+++ b/chat/chat.8
@@ -1,12 +1,12 @@
.\" -*- nroff -*-
.\" manual page [] for chat 1.8
-.\" $Id: chat.8,v 1.8 1999/05/12 06:13:22 paulus Exp $
+.\" $Id: chat.8,v 1.9 1999/09/06 05:10:23 paulus Exp $
.\" SH section heading
.\" SS subsection heading
.\" LP paragraph
.\" IP indented paragraph
.\" TP hanging label
-.TH CHAT 8 "27 Sep 1997" "Chat Version 1.17"
+.TH CHAT 8 "22 May 1999" "Chat Version 1.22"
.SH NAME
chat \- Automated conversational script with a modem
.SH SYNOPSIS
@@ -49,6 +49,10 @@ or off at specific points in the chat script by using the \fIECHO\fR
keyword. When echoing is enabled, all output from the modem is echoed
to \fIstderr\fR.
.TP
+.B -E
+Enables environment variable substituion within chat scripts using the
+standard \fI$xxx\fR syntax.
+.TP
.B -v
Request that the \fIchat\fR script be executed in a verbose mode. The
\fIchat\fR program will then log the execution state of the chat
@@ -429,6 +433,14 @@ sequence 'HI TIM' and HI\\sTIM are the same.
.B \\\\t
Send or expect a tab character.
.TP
+.B \\\\T
+Send the phone number string as specified with the \fI-T\fR option
+.I (not valid in expect.)
+.TP
+.B \\\\U
+Send the phone number 2 string as specified with the \fI-U\fR option
+.I (not valid in expect.)
+.TP
.B \\\\\\\\
Send or expect a backslash character.
.TP
@@ -441,6 +453,12 @@ character.
Substitute the sequence with the control character represented by C.
For example, the character DC1 (17) is shown as \^^Q.
.I (some characters are not valid in expect.)
+.SH ENVIRONMENT VARIABLES
+Environment variables are available within chat scripts, if the \fI-E\fR
+option was specified in the command line. The metacharacter \fI$\fR is used
+to introduce the name of the environment variable to substitute. If the
+substition fails, because the requested environment variable is not set,
+\fInothing\fR is replaced for the variable.
.SH TERMINATION CODES
The \fIchat\fR program will terminate with the following completion
codes.
diff --git a/chat/chat.c b/chat/chat.c
index c9e01e2..02fa449 100644
--- a/chat/chat.c
+++ b/chat/chat.c
@@ -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>.
*
@@ -84,7 +87,7 @@
#endif
#ifndef lint
-static const char rcsid[] = "$Id: chat.c,v 1.24 1999/08/13 06:46:09 paulus Exp $";
+static const char rcsid[] = "$Id: chat.c,v 1.25 1999/09/06 05:10:23 paulus Exp $";
#endif
#include <stdio.h>
@@ -169,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;
@@ -257,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]] ...]]]
*
@@ -280,6 +284,10 @@ main(argc, argv)
++echo;
break;
+ case 'E':
+ ++use_env;
+ break;
+
case 'v':
++verbose;
break;
@@ -461,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);
}
@@ -661,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) {
@@ -681,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;
@@ -714,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 {
@@ -731,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 {
@@ -769,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;
@@ -981,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;