/*
* Copyright 2000 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Chat -- a program for automatic session establishment (i.e. dial
* the phone and log in).
*
* Standard termination codes:
* 0 - successful completion of the script
* 1 - invalid argument, expect string too large, etc.
* 2 - error on an I/O operation or fatal error condition.
* 3 - timeout waiting for a simple string.
* 4 - the first string declared as "ABORT"
* 5 - the second string declared as "ABORT"
* 6 - ... and so on for successive ABORT strings.
*
* 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>.
*
* 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>
*
*
* Added SAY keyword to send output to stderr.
* This allows to turn ECHO OFF and to output specific, user selected,
* text to give progress messages. This best works when stderr
* exists (i.e.: pppd in nodetach mode).
*
* Added HANGUP directives to allow for us to be called
* back. When HANGUP is set to NO, chat will not hangup at HUP signal.
* We rely on timeouts in that case.
*
* Added CLR_ABORT to clear previously set ABORT string. This has been
* dictated by the HANGUP above as "NO CARRIER" (for example) must be
* an ABORT condition until we know the other host is going to close
* the connection for call back. As soon as we have completed the
* first stage of the call back sequence, "NO CARRIER" is a valid, non
* fatal string. As soon as we got called back (probably get "CONNECT"),
* we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
* Note that CLR_ABORT packs the abort_strings[] array so that we do not
* have unused entries not being reclaimed.
*
* In the same vein as above, added CLR_REPORT keyword.
*
* Allow for comments. Line starting with '#' are comments and are
* ignored. If a '#' is to be expected as the first character, the
* expect string must be quoted.
*
*
* Francis Demierre <Francis@SwissMail.Com>
* Thu May 15 17:15:40 MET DST 1997
*
*
* Added -r "report file" switch & REPORT keyword.
* Robert Geer <bgeer@xmission.com>
*
* Added -s "use stderr" and -S "don't use syslog" switches.
* June 18, 1997
* Karl O. Pinc <kop@meme.com>
*
*
* Added -e "echo" switch & ECHO keyword
* Dick Streefland <dicks@tasking.nl>
*
*
* Considerable updates and modifications by
* Al Longyear <longyear@pobox.com>
* Paul Mackerras <paulus@cs.anu.edu.au>
*
*
* The original author is:
*
* Karl Fox <karl@MorningStar.Com>
* Morning Star Technologies, Inc.
* 1760 Zollinger Road
* Columbus, OH 43221
* (614)451-1883
*
*/
#ifndef __STDC__
#define const
#endif
#pragma ident "%Z%%M% %I% %E% SMI"
#ifndef lint
#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 <syslog.h>
#ifndef TERMIO
#define TERMIOS
#endif
#ifdef TERMIO
#include <termio.h>
#endif
#ifdef TERMIOS
#include <termios.h>
#endif
#ifndef SIGTYPE
#define SIGTYPE void
#endif
#ifdef __STDC__
#include <stdarg.h>
#define __V(x) x
#define __P(x) x
#else
#include <varargs.h>
#define __P(x) ()
#define const
#endif
#ifndef O_NONBLOCK
#endif
#ifdef SUNOS
extern int sys_nerr;
extern char *sys_errlist[];
"unknown error")
#endif
/*************** Micro getopt() *********************************************/
(_O=4,(char*)0):(char*)0)
#define ARG(c,v) (c?(--c,*v++):(char*)0)
/*************** Micro getopt() *********************************************/
char *program_name;
int echo = 0;
int verbose = 0;
int to_stderr = 0;
int Verbose = 0;
int quiet = 0;
int report = 0;
int use_env = 0;
int exit_code = 0;
char *report_file = (char *) 0;
char *chat_file = (char *) 0;
char *phone_num = (char *) 0;
char *phone_num2 = (char *) 0;
int have_tty_parameters = 0;
#ifdef TERMIO
#endif
#ifdef TERMIOS
#endif
int clear_abort_next = 0;
int clear_report_next = 0;
void set_tty_parameters __P((void));
void echo_stderr __P((int));
void break_sequence __P((void));
int put_string __P((register char *s));
int write_char __P((int c));
void chat_expect __P((register char *s));
void break_sequence __P((void));
char *expect_strtok __P((char *, char *));
void *dup_mem(b, c)
void *b;
size_t c;
{
if (!ans)
return ans;
}
void *copy_of (s)
char *s;
{
}
/*
* chat [ -v ] [ -E ] [ -T number ] [ -U number ] [ -t timeout ] [ -f chat-file ] \
* [ -r report-file ] \
* [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
*
* Perform a UUCP-dialer-like chat script on stdin and stdout.
*/
int
int argc;
char **argv;
{
int option;
char *arg;
program_name = *argv;
tzset();
switch (option) {
case 'e':
++echo;
break;
case 'E':
++use_env;
break;
case 'v':
++verbose;
break;
case 'V':
++Verbose;
break;
case 's':
++to_stderr;
break;
case 'S':
to_log = 0;
break;
case 'f':
else
usage();
break;
case 't':
else
usage();
break;
case 'r':
if (arg) {
if (verbose)
report = 1;
}
}
break;
case 'T':
else
usage();
break;
case 'U':
else
usage();
break;
default:
usage();
break;
}
}
/*
* Default the report file to the stderr location
*/
if (to_log) {
#ifdef ultrix
#else
if (verbose)
else
#endif
}
init();
usage();
else
} else {
}
}
terminate(0);
return 0;
}
/*
* Process a chat script when read from a file.
*/
char *chat_file;
{
linect = 0;
sendflg = 0;
if (sp)
*sp = '\0';
linect++;
/* lines starting with '#' are comments. If a real '#'
is to be expected, it should be quoted .... */
if ( *sp == '#' )
continue;
while (*sp != '\0') {
++sp;
continue;
}
if (*sp == '\0')
if (*sp++ == '\\') {
if (*sp != '\0')
++sp;
}
}
}
else {
++sp;
}
if (*sp != '\0')
*sp++ = '\0';
if (sendflg)
else
chat_expect (arg);
}
}
}
/*
* We got an error parsing the command line.
*/
void usage()
{
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);
}
/*
*/
{
#ifdef __STDC__
#else
char *fmt;
#endif
if (to_log)
if (to_stderr)
}
/*
* Print an error message and terminate.
*/
{
#ifdef __STDC__
#else
int code;
char *fmt;
#endif
if (to_log)
if (to_stderr)
}
int alarmed = 0;
int signo;
{
int flags;
alarm(1);
if (verbose)
logf("alarm");
}
void unalarm()
{
int flags;
}
int signo;
{
}
int signo;
{
}
int signo;
{
}
void init()
{
alarm(0);
alarmed = 0;
}
void set_tty_parameters()
{
#if defined(get_term_param)
term_parms t;
if (get_term_param (&t) < 0)
saved_tty_parameters = t;
have_tty_parameters = 1;
t.c_oflag = 0;
t.c_lflag = 0;
if (set_term_param (&t) < 0)
#endif
}
void break_sequence()
{
#ifdef TERMIOS
tcsendbreak (0, 0);
#endif
}
int status;
{
static int terminating = 0;
if (terminating)
terminating = 1;
echo_stderr(-1);
/*
* Allow the last of the report string to be gathered before we terminate.
*/
if (report_gathering) {
int c, rep_len;
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;
}
if (verbose)
}
#if defined(get_term_param)
if (have_tty_parameters) {
if (set_term_param (&saved_tty_parameters) < 0)
}
#endif
}
/*
* 'Clean up' this string.
*/
register char *s;
int sending; /* set to 1 when sending (putting) this string. */
{
|| (chr) == '_')
while (*s) {
cur_chr = *s++;
if (cur_chr == '^') {
cur_chr = *s++;
if (cur_chr == '\0') {
*s1++ = '^';
break;
}
cur_chr &= 0x1F;
if (cur_chr != 0) {
}
continue;
}
while (isalnumx(*s))
*phchar++ = *s++;
*phchar = '\0';
if (phchar)
while (*phchar)
continue;
}
if (cur_chr != '\\') {
continue;
}
cur_chr = *s++;
if (cur_chr == '\0') {
if (sending) {
*s1++ = '\\';
*s1++ = '\\';
}
break;
}
switch (cur_chr) {
case 'b':
*s1++ = '\b';
break;
case 'c':
if (sending && *s == '\0')
add_return = 0;
else
break;
case '\\':
case 'K':
case 'p':
case 'd':
if (sending)
*s1++ = '\\';
break;
case 'T':
}
else {
*s1++ = '\\';
*s1++ = 'T';
}
break;
case 'U':
if (sending && phone_num2) {
}
else {
*s1++ = '\\';
*s1++ = 'U';
}
break;
case 'q':
quiet = 1;
break;
case 'r':
*s1++ = '\r';
break;
case 'n':
*s1++ = '\n';
break;
case 's':
*s1++ = ' ';
break;
case 't':
*s1++ = '\t';
break;
case 'N':
if (sending) {
*s1++ = '\\';
*s1++ = '\0';
}
else
*s1++ = 'N';
break;
case '$': /* ARI */
if (use_env) {
break;
}
/* FALL THROUGH */
default:
cur_chr &= 0x07;
if (isoctal (*s)) {
cur_chr <<= 3;
cur_chr |= *s++ - '0';
if (isoctal (*s)) {
cur_chr <<= 3;
cur_chr |= *s++ - '0';
}
}
*s1++ = '\\';
}
break;
}
if (sending)
*s1++ = '\\';
break;
}
}
if (add_return)
*s1++ = '\r';
}
/*
* A modified version of 'strtok'. This version skips \ sequences.
*/
char *s, *term;
{
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)
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.
*/
++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)
char *s;
{
char *expect;
char *reply;
if (strcmp(s, "HANGUP") == 0) {
++hup_next;
return;
}
if (strcmp(s, "ABORT") == 0) {
++abort_next;
return;
}
if (strcmp(s, "CLR_ABORT") == 0) {
return;
}
if (strcmp(s, "REPORT") == 0) {
++report_next;
return;
}
if (strcmp(s, "CLR_REPORT") == 0) {
return;
}
if (strcmp(s, "TIMEOUT") == 0) {
++timeout_next;
return;
}
if (strcmp(s, "ECHO") == 0) {
++echo_next;
return;
}
if (strcmp(s, "SAY") == 0) {
++say_next;
return;
}
/*
* Fetch the expect and reply string.
*/
for (;;) {
s = (char *) 0;
if (expect == (char *) 0)
return;
/*
* Handle the expect string. If successful then exit.
*/
if (get_string (expect))
return;
/*
* If there is a sub-reply string then send it. Otherwise any condition
* is terminal.
*/
break;
}
/*
* The expectation did not occur. This is terminal.
*/
if (fail_reason)
else
logf("Failed");
}
/*
* Translate the input character to the appropriate string for printing
* the data.
*/
char *character(c)
int c;
{
char *meta;
c &= 0x7F;
if (c < 32)
else if (c == 127)
else
return (string);
}
/*
* process the reply string
*/
void chat_send (s)
register char *s;
{
if (say_next) {
say_next = 0;
s = clean(s, 1);
free(s);
return;
}
if (hup_next) {
hup_next = 0;
if (strcmp(s, "OFF") == 0)
else
return;
}
if (echo_next) {
echo_next = 0;
return;
}
if (abort_next) {
char *s1;
abort_next = 0;
if (n_aborts >= MAX_ABORTS)
if (verbose)
logf("abort on (%v)", s);
return;
}
if (clear_abort_next) {
char *s1;
int i;
int old_max;
int pack = 0;
clear_abort_next = 0;
for (i=0; i < n_aborts; i++) {
free(abort_string[i]);
abort_string[i] = NULL;
pack++;
n_aborts--;
if (verbose)
logf("clear abort on (%v)", s);
}
}
if (pack)
return;
}
if (report_next) {
char *s1;
report_next = 0;
if (n_reports >= MAX_REPORTS)
if (verbose)
logf("report (%v)", s);
return;
}
if (clear_report_next) {
char *s1;
int i;
int old_max;
int pack = 0;
clear_report_next = 0;
for (i=0; i < n_reports; i++) {
free(report_string[i]);
report_string[i] = NULL;
pack++;
n_reports--;
if (verbose)
logf("clear report (%v)", s);
}
}
if (pack)
return;
}
if (timeout_next) {
timeout_next = 0;
if (timeout <= 0)
if (verbose)
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;
;
if (*fn != 0) {
FILE *f;
int n = 0;
/* open the file and read until STR_LEN-1 bytes or end-of-file */
if (f == NULL)
while (n < STR_LEN - 1) {
if (nr < 0)
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. */
--n;
file_data[n] = 0;
s = file_data;
}
}
if (strcmp(s, "EOT") == 0)
s = "^D\\c";
else if (strcmp(s, "BREAK") == 0)
s = "\\K\\c";
if (!put_string(s))
}
int get_char()
{
int status;
char c;
switch (status) {
case 1:
return ((int)c & 0x7F);
default:
case -1:
return (-1);
}
}
int put_char(c)
int c;
{
int status;
char ch = c;
switch (status) {
case 1:
return (0);
default:
case -1:
return (-1);
}
}
int write_char (c)
int c;
{
alarm(0);
alarmed = 0;
if (verbose) {
logf(" -- write timed out");
else
logf(" -- write failed: %m");
}
return (0);
}
return (1);
}
int put_string (s)
register char *s;
{
quiet = 0;
s = clean(s, 1);
if (verbose) {
if (quiet)
logf("send (??????)");
else
logf("send (%v)", s);
}
while (*s) {
register char c = *s++;
if (c != '\\') {
if (!write_char (c))
return 0;
continue;
}
c = *s++;
switch (c) {
case 'd':
sleep(1);
break;
case 'K':
break;
case 'p':
break;
default:
if (!write_char (c))
return 0;
break;
}
}
alarm(0);
alarmed = 0;
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':
need_lf = 0;
break;
default:
s = character(n);
need_lf = 1;
break;
}
}
/*
* 'Wait for' this string to appear on this file descriptor.
*/
register char *string;
{
fail_reason = (char *)0;
if (verbose)
logf("expect string is too long");
exit_code = 1;
return 0;
}
if (len == 0) {
if (verbose)
logf("got it");
return (1);
}
alarmed = 0;
if (echo)
echo_stderr(c);
if (verbose && c == '\n') {
if (s == logged)
else
logged = s + 1;
}
*s++ = c;
logged = s;
}
if (Verbose) {
if (c == '\n')
else if (c != '\r')
}
if (!report_gathering) {
for (n = 0; n < n_reports; ++n) {
if ((report_string[n] != (char*) NULL) &&
report_string[n] = (char *) NULL;
report_gathering = 1;
break;
}
}
}
else {
if (!iscntrl (c)) {
report_buffer[rep_len] = c;
}
else {
report_gathering = 0;
}
}
if (verbose) {
if (s > logged)
logf(" -- got it\n");
}
alarm(0);
alarmed = 0;
return (1);
}
for (n = 0; n < n_aborts; ++n) {
if (verbose) {
if (s > logged)
logf(" -- failed");
}
alarm(0);
alarmed = 0;
exit_code = n + 4;
return (0);
}
}
if (s >= end) {
if (verbose)
logged = s;
}
s -= minlen;
}
logf("warning: alarm synchronization problem");
}
alarm(0);
if (alarmed)
logf(" -- read timed out");
else
logf(" -- read failed: %m");
}
exit_code = 3;
alarmed = 0;
return (0);
}
/*
* Gross kludge to handle Solaris versions >= 2.6 having usleep.
*/
#ifdef SOL2
#endif
#endif /* SOL2 */
#ifdef NO_USLEEP
/*
usleep -- support routine for 4.2BSD system call emulations
last edit: 29-Oct-1984 D A Gwyn
*/
extern int select();
int
long usec; /* delay in microseconds */
{
static struct { /* `timeval' */
}
#endif
void
char **array; /* The address of the array of string pointers */
int end; /* The index of the next free entry before CLR_ */
{
int i, j;
for (i = 0; i < end; i++) {
for (j = i+1; j < end; ++j)
for (; i < end; ++i)
break;
}
}
}
/*
* vfmtmsg - format a message into a buffer. Like vsprintf except we
* also specify the length of the output buffer, and we handle the
* %m (error message) format.
* Doesn't do floating-point formats.
* Returns the number of chars put into buf.
*/
int
char *buf;
int buflen;
const char *fmt;
{
int c, i, n;
unsigned long val = 0;
const char *f;
unsigned char *p;
--buflen;
while (buflen > 0) {
for (f = fmt; *f != '%' && *f != 0; ++f)
;
if (f > fmt) {
fmt = f;
}
if (*fmt == 0)
break;
c = *++fmt;
fillch = ' ';
if (c == '0') {
fillch = '0';
c = *++fmt;
}
if (c == '*') {
c = *++fmt;
} else {
while (isdigit(c)) {
c = *++fmt;
}
}
if (c == '.') {
c = *++fmt;
if (c == '*') {
c = *++fmt;
} else {
while (isdigit(c)) {
c = *++fmt;
}
}
}
str = 0;
base = 0;
neg = 0;
++fmt;
switch (c) {
case 'd':
if (i < 0) {
neg = 1;
val = -i;
} else
val = i;
base = 10;
break;
case 'o':
base = 8;
break;
case 'x':
base = 16;
break;
case 'p':
base = 16;
neg = 2;
break;
case 's':
break;
case 'c':
num[1] = 0;
break;
case 'm':
break;
case 'v': /* "visible" string */
case 'q': /* quoted string */
quoted = c == 'q';
n = prec;
} else {
n = strlen((char *)p);
n = prec;
}
while (n > 0 && buflen > 0) {
c = *p++;
--n;
if (!quoted && c >= 0x80) {
OUTCHAR('M');
OUTCHAR('-');
c -= 0x80;
}
OUTCHAR('\\');
if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
if (quoted) {
OUTCHAR('\\');
switch (c) {
default:
OUTCHAR('x');
}
} else {
if (c == '\t')
OUTCHAR(c);
else {
OUTCHAR('^');
OUTCHAR(c ^ 0x40);
}
}
} else
OUTCHAR(c);
}
continue;
default:
*buf++ = '%';
if (c != '%')
--fmt; /* so %z outputs %z etc. */
--buflen;
continue;
}
if (base != 0) {
*--str = 0;
break;
}
switch (neg) {
case 1:
*--str = '-';
break;
case 2:
*--str = 'x';
*--str = '0';
break;
}
} else {
}
if (width > 0) {
buflen -= n;
for (; n > 0; --n)
}
}
}
*buf = 0;
}