* 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.9 1995/06/12 11:24:15 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>
#define MAX_REPORTS 50
#define DEFAULT_CHAT_TIMEOUT 45
+int echo = 0;
int verbose = 0;
+int Verbose = 0;
int quiet = 0;
int report = 0;
int exit_code = 0;
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] ;
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));
{
switch (option)
{
+ case 'e':
+ ++echo;
+ break;
+
case 'v':
++verbose;
break;
+ case 'V':
+ ++Verbose;
+ break;
+
case 'f':
if (arg = OPTARG(argc, argv))
{
}
}
}
+/*
+ * 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);
}
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);
}
void terminate(status)
int status;
{
+ 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;
+ report_fp = (FILE *) NULL;
}
#if defined(get_term_param)
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;
}
- while (*s)
+ if (strcmp(s, "ECHO") == 0)
{
- register char *hyphen;
+ ++echo_next;
+ return;
+ }
+/*
+ * Fetch the expect and reply string.
+ */
+ for (;;)
+ {
+ expect = expect_strtok (s, "-");
+ s = (char *) 0;
- for (hyphen = s; *hyphen; ++hyphen)
+ if (expect == (char *) 0)
{
- if (*hyphen == '-')
- {
- if (hyphen == s || hyphen[-1] != '\\')
- {
- break;
- }
- }
+ return;
}
-
- if (*hyphen == '-')
- {
- *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';
-
- 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;
}
- else
+/*
+ * If there is a sub-reply string then send it. Otherwise any condition
+ * is terminal.
+ */
+ if (reply == (char *) 0 || exit_code != 3)
{
- if (get_string(s))
- {
- return;
- }
- else
- {
- if (fail_reason)
- {
- syslog(LOG_INFO, "Failed (%s)", fail_reason);
- }
- else
- {
- syslog(LOG_INFO, "Failed");
- }
-
- terminate(exit_code);
- }
+ break;
}
+
+ 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)
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;
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.
*/
{
int n, abort_len, report_len;
+ if (echo)
+ {
+ echo_stderr(c);
+ }
if (verbose)
{
if (c == '\n')
}
}
- *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);
- }
+ if (Verbose) {
+ if (c == '\n') fputc( '\n', stderr );
+ else if (c != '\r') fprintf( stderr, "%s", character(c) );
+ }
- 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);
- }
- }
+ *s++ = c;
if (!report_gathering)
{
}
}
+ 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);