1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-21 11:13:30 +00:00
freebsd/usr.bin/chat/chat.c

1684 lines
28 KiB
C
Raw Normal View History

1994-11-12 05:25:32 +00:00
/*
* 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 condtion.
* 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.
*
1994-11-12 05:25:32 +00:00
* This software is in the public domain.
*
1997-08-22 15:24:36 +00:00
* -----------------
*
* 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
1994-11-12 05:25:32 +00:00
*
*
* Added -r "report file" switch & REPORT keyword.
* Robert Geer <bgeer@xmission.com>
*
1997-08-22 15:24:36 +00:00
*
* 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>
*
*
1994-11-12 05:25:32 +00:00
* The original author is:
*
* Karl Fox <karl@MorningStar.Com>
* Morning Star Technologies, Inc.
* 1760 Zollinger Road
* Columbus, OH 43221
* (614)451-1883
*
1997-08-22 15:24:36 +00:00
*
1994-11-12 05:25:32 +00:00
*/
1997-08-22 15:24:36 +00:00
#ifndef lint
static char rcsid[] = "$Id: chat.c,v 1.8 1997/06/24 06:52:33 charnier Exp $";
#endif
1994-11-12 05:25:32 +00:00
#include <stdio.h>
1997-08-22 15:24:36 +00:00
#include <ctype.h>
#include <time.h>
1994-11-12 05:25:32 +00:00
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
1997-08-22 15:24:36 +00:00
#include <unistd.h>
1994-11-12 05:25:32 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#ifndef TERMIO
#undef TERMIOS
#define TERMIOS
#endif
#ifdef TERMIO
#include <termio.h>
#endif
#ifdef TERMIOS
#include <termios.h>
#endif
#define STR_LEN 1024
#ifndef SIGTYPE
#define SIGTYPE void
#endif
#ifdef __STDC__
#undef __P
#define __P(x) x
#else
#define __P(x) ()
#define const
#endif
#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif
1994-11-12 05:25:32 +00:00
/*************** 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=='-'\
&&!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)
#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
#define ARG(c,v) (c?(--c,*v++):(char*)0)
static int _O = 0; /* Internal state */
/*************** Micro getopt() *********************************************/
#define MAX_ABORTS 50
#define MAX_REPORTS 50
1994-11-12 05:25:32 +00:00
#define DEFAULT_CHAT_TIMEOUT 45
1997-08-22 15:24:36 +00:00
int echo = 0;
int verbose = 0;
1997-08-22 15:24:36 +00:00
int Verbose = 0;
int quiet = 0;
int report = 0;
int exit_code = 0;
FILE* report_fp = (FILE *) 0;
char *report_file = (char *) 0;
char *chat_file = (char *) 0;
int timeout = DEFAULT_CHAT_TIMEOUT;
1994-11-12 05:25:32 +00:00
int have_tty_parameters = 0;
1994-11-12 05:25:32 +00:00
#ifdef TERMIO
#define term_parms struct termio
#define get_term_param(param) ioctl(0, TCGETA, param)
#define set_term_param(param) ioctl(0, TCSETA, param)
1994-11-12 05:25:32 +00:00
struct termio saved_tty_parameters;
#endif
1994-11-12 05:25:32 +00:00
#ifdef TERMIOS
#define term_parms struct termios
#define get_term_param(param) tcgetattr(0, param)
#define set_term_param(param) tcsetattr(0, TCSANOW, param)
1994-11-12 05:25:32 +00:00
struct termios saved_tty_parameters;
#endif
char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
fail_buffer[50];
1997-08-22 15:24:36 +00:00
int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
int clear_abort_next = 0;
1994-11-12 05:25:32 +00:00
char *report_string[MAX_REPORTS] ;
char report_buffer[50] ;
int n_reports = 0, report_next = 0, report_gathering = 0 ;
1997-08-22 15:24:36 +00:00
int clear_report_next = 0;
int say_next = 0, hup_next = 0;
1994-11-12 05:25:32 +00:00
void *dup_mem __P((void *b, size_t c));
void *copy_of __P((char *s));
1997-06-24 06:52:33 +00:00
static void usage __P((void));
1994-11-12 05:25:32 +00:00
void logf __P((const char *str));
void logflush __P((void));
void fatal __P((const char *msg));
void sysfatal __P((const char *msg));
SIGTYPE sigalrm __P((int signo));
SIGTYPE sigint __P((int signo));
SIGTYPE sigterm __P((int signo));
SIGTYPE sighup __P((int signo));
void unalarm __P((void));
void init __P((void));
void set_tty_parameters __P((void));
1997-08-22 15:24:36 +00:00
void echo_stderr __P((int));
1994-11-12 05:25:32 +00:00
void break_sequence __P((void));
void terminate __P((int status));
void do_file __P((char *chat_file));
int get_string __P((register char *string));
int put_string __P((register char *s));
int write_char __P((int c));
int put_char __P((int c));
1994-11-12 05:25:32 +00:00
int get_char __P((void));
void chat_send __P((register char *s));
char *character __P((int c));
1994-11-12 05:25:32 +00:00
void chat_expect __P((register char *s));
char *clean __P((register char *s, int sending));
void break_sequence __P((void));
void terminate __P((int status));
void die __P((void));
1997-08-22 15:24:36 +00:00
void pack_array __P((char **array, int end));
char *expect_strtok __P((char *, char *));
int main __P((int, char *[]));
1994-11-12 05:25:32 +00:00
void *dup_mem(b, c)
void *b;
size_t c;
{
void *ans = malloc (c);
if (!ans)
{
1994-11-12 05:25:32 +00:00
fatal ("memory error!\n");
}
1994-11-12 05:25:32 +00:00
memcpy (ans, b, c);
return ans;
}
void *copy_of (s)
char *s;
{
return dup_mem (s, strlen (s) + 1);
}
/*
* chat [ -v ] [ -t timeout ] [ -f chat-file ] [ -r report-file ] \
1994-11-12 05:25:32 +00:00
* [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
*
* Perform a UUCP-dialer-like chat script on stdin and stdout.
*/
int
main(argc, argv)
int argc;
char **argv;
{
int option;
char *arg;
tzset();
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
while ((option = OPTION(argc, argv)) != 0)
{
1994-11-12 05:25:32 +00:00
switch (option)
{
1997-08-22 15:24:36 +00:00
case 'e':
++echo;
break;
1994-11-12 05:25:32 +00:00
case 'v':
++verbose;
break;
1997-08-22 15:24:36 +00:00
case 'V':
++Verbose;
break;
1994-11-12 05:25:32 +00:00
case 'f':
1997-08-22 15:24:36 +00:00
if ((arg = OPTARG(argc, argv)) != NULL)
{
1994-11-12 05:25:32 +00:00
chat_file = copy_of(arg);
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
usage();
}
1994-11-12 05:25:32 +00:00
break;
case 't':
1997-08-22 15:24:36 +00:00
if ((arg = OPTARG(argc, argv)) != NULL)
{
1994-11-12 05:25:32 +00:00
timeout = atoi(arg);
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
usage();
}
break;
1994-11-12 05:25:32 +00:00
case 'r':
arg = OPTARG (argc, argv);
if (arg)
{
if (report_fp != NULL)
{
fclose (report_fp);
}
report_file = copy_of (arg);
report_fp = fopen (report_file, "a");
if (report_fp != NULL)
{
if (verbose)
{
fprintf (report_fp, "Opening \"%s\"...\n",
report_file);
}
report = 1;
}
}
1994-11-12 05:25:32 +00:00
break;
default:
usage();
break;
1994-11-12 05:25:32 +00:00
}
}
/*
* Default the report file to the stderr location
*/
if (report_fp == NULL)
{
report_fp = stderr;
}
1994-11-12 05:25:32 +00:00
#ifdef ultrix
openlog("chat", LOG_PID);
#else
openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
if (verbose)
{
1994-11-12 05:25:32 +00:00
setlogmask(LOG_UPTO(LOG_INFO));
}
else
{
1994-11-12 05:25:32 +00:00
setlogmask(LOG_UPTO(LOG_WARNING));
}
1994-11-12 05:25:32 +00:00
#endif
init();
1994-11-12 05:25:32 +00:00
if (chat_file != NULL)
{
arg = ARG(argc, argv);
if (arg != NULL)
{
1994-11-12 05:25:32 +00:00
usage();
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
do_file (chat_file);
}
1994-11-12 05:25:32 +00:00
}
else
{
1997-08-22 15:24:36 +00:00
while ((arg = ARG(argc, argv)) != NULL)
1994-11-12 05:25:32 +00:00
{
chat_expect(arg);
1997-08-22 15:24:36 +00:00
if ((arg = ARG(argc, argv)) != NULL)
{
1994-11-12 05:25:32 +00:00
chat_send(arg);
}
1994-11-12 05:25:32 +00:00
}
}
terminate(0);
1997-08-22 15:24:36 +00:00
return 0;
1994-11-12 05:25:32 +00:00
}
/*
* Process a chat script when read from a file.
*/
void do_file (chat_file)
char *chat_file;
{
int linect, len, sendflg;
char *sp, *arg, quote;
char buf [STR_LEN];
FILE *cfp;
cfp = fopen (chat_file, "r");
if (cfp == NULL)
1994-11-12 05:25:32 +00:00
{
syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
terminate (1);
}
linect = 0;
sendflg = 0;
while (fgets(buf, STR_LEN, cfp) != NULL)
{
sp = strchr (buf, '\n');
if (sp)
{
1994-11-12 05:25:32 +00:00
*sp = '\0';
}
1994-11-12 05:25:32 +00:00
linect++;
sp = buf;
1997-08-22 15:24:36 +00:00
/* lines starting with '#' are comments. If a real '#'
is to be expected, it should be quoted .... */
if ( *sp == '#' ) continue;
1994-11-12 05:25:32 +00:00
while (*sp != '\0')
{
if (*sp == ' ' || *sp == '\t')
{
++sp;
continue;
}
if (*sp == '"' || *sp == '\'')
{
quote = *sp++;
arg = sp;
while (*sp != quote)
{
if (*sp == '\0')
{
syslog (LOG_ERR, "unterminated quote (line %d)",
linect);
terminate (1);
}
1994-11-12 05:25:32 +00:00
if (*sp++ == '\\')
{
1994-11-12 05:25:32 +00:00
if (*sp != '\0')
{
1994-11-12 05:25:32 +00:00
++sp;
}
}
1994-11-12 05:25:32 +00:00
}
}
else
{
arg = sp;
while (*sp != '\0' && *sp != ' ' && *sp != '\t')
{
1994-11-12 05:25:32 +00:00
++sp;
}
1994-11-12 05:25:32 +00:00
}
if (*sp != '\0')
{
1994-11-12 05:25:32 +00:00
*sp++ = '\0';
}
1994-11-12 05:25:32 +00:00
if (sendflg)
{
chat_send (arg);
}
else
{
chat_expect (arg);
}
sendflg = !sendflg;
}
}
fclose (cfp);
}
/*
* We got an error parsing the command line.
*/
1997-06-24 06:52:33 +00:00
static void
usage()
1994-11-12 05:25:32 +00:00
{
1997-06-24 06:52:33 +00:00
fprintf(stderr, "%s %s\n",
1997-08-22 15:24:36 +00:00
"usage: chat [-e] [-v] [-V] [-t timeout] [-r report-file]",
1997-06-24 06:52:33 +00:00
"{-f chat-file | chat-script}");
1994-11-12 05:25:32 +00:00
exit(1);
}
char line[256];
char *p;
void logf (str)
const char *str;
1997-08-22 15:24:36 +00:00
{
int l = strlen(line);
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
if (l + strlen(str) >= sizeof(line)) {
syslog(LOG_INFO, "%s", line);
l = 0;
}
strcpy(line + l, str);
if (str[strlen(str)-1] == '\n') {
1994-11-12 05:25:32 +00:00
syslog (LOG_INFO, "%s", line);
line[0] = 0;
}
1997-08-22 15:24:36 +00:00
}
1994-11-12 05:25:32 +00:00
void logflush()
{
if (line[0] != 0)
{
syslog(LOG_INFO, "%s", line);
line[0] = 0;
}
}
/*
* Terminate with an error.
1994-11-12 05:25:32 +00:00
*/
void die()
{
terminate(1);
}
/*
* Print an error message and terminate.
*/
void fatal (msg)
const char *msg;
{
syslog(LOG_ERR, "%s", msg);
terminate(2);
1994-11-12 05:25:32 +00:00
}
/*
* Print an error message along with the system error message and
* terminate.
*/
void sysfatal (msg)
const char *msg;
{
syslog(LOG_ERR, "%s: %m", msg);
terminate(2);
1994-11-12 05:25:32 +00:00
}
int alarmed = 0;
SIGTYPE sigalrm(signo)
int signo;
{
int flags;
alarm(1);
alarmed = 1; /* Reset alarm to avoid race window */
signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
logflush();
if ((flags = fcntl(0, F_GETFL, 0)) == -1)
{
1994-11-12 05:25:32 +00:00
sysfatal("Can't get file mode flags on stdin");
}
1994-11-12 05:25:32 +00:00
else
{
if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
{
1994-11-12 05:25:32 +00:00
sysfatal("Can't set file mode flags on stdin");
}
}
1994-11-12 05:25:32 +00:00
if (verbose)
{
syslog(LOG_INFO, "alarm");
}
}
void unalarm()
{
int flags;
if ((flags = fcntl(0, F_GETFL, 0)) == -1)
{
1994-11-12 05:25:32 +00:00
sysfatal("Can't get file mode flags on stdin");
}
1994-11-12 05:25:32 +00:00
else
{
if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
{
1994-11-12 05:25:32 +00:00
sysfatal("Can't set file mode flags on stdin");
}
}
1994-11-12 05:25:32 +00:00
}
SIGTYPE sigint(signo)
int signo;
{
fatal("SIGINT");
}
SIGTYPE sigterm(signo)
int signo;
{
fatal("SIGTERM");
}
SIGTYPE sighup(signo)
int signo;
{
fatal("SIGHUP");
}
void init()
{
signal(SIGINT, sigint);
signal(SIGTERM, sigterm);
signal(SIGHUP, sighup);
set_tty_parameters();
signal(SIGALRM, sigalrm);
alarm(0);
alarmed = 0;
}
void set_tty_parameters()
{
#if defined(get_term_param)
term_parms t;
1994-11-12 05:25:32 +00:00
if (get_term_param (&t) < 0)
{
have_tty_parameters = 0;
return;
}
1994-11-12 05:25:32 +00:00
saved_tty_parameters = t;
have_tty_parameters = 1;
t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
t.c_oflag = 0;
t.c_lflag = 0;
t.c_cc[VERASE] =
t.c_cc[VKILL] = 0;
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
if (set_term_param (&t) < 0)
{
1994-11-12 05:25:32 +00:00
sysfatal("Can't set terminal parameters");
}
1994-11-12 05:25:32 +00:00
#endif
}
void break_sequence()
{
#ifdef TERMIOS
tcsendbreak (0, 0);
#endif
}
void terminate(status)
int status;
{
1997-08-22 15:24:36 +00:00
echo_stderr(-1);
if (report_file != (char *) 0 && report_fp != (FILE *) NULL)
1997-08-22 15:24:36 +00:00
{
/*
* Allow the last of the report string to be gathered before we terminate.
*/
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);
}
if (verbose)
{
fprintf (report_fp, "Closing \"%s\".\n", report_file);
}
fclose (report_fp);
1997-08-22 15:24:36 +00:00
report_fp = (FILE *) NULL;
1994-11-12 05:25:32 +00:00
}
#if defined(get_term_param)
if (have_tty_parameters)
{
if (set_term_param (&saved_tty_parameters) < 0)
{
syslog(LOG_ERR, "Can't restore terminal parameters: %m");
exit(1);
}
}
#endif
1994-11-12 05:25:32 +00:00
exit(status);
1994-11-12 05:25:32 +00:00
}
/*
* 'Clean up' this string.
*/
char *clean(s, sending)
register char *s;
int sending;
{
char temp[STR_LEN], cur_chr;
register char *s1;
int add_return = sending;
#define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
s1 = temp;
while (*s)
{
cur_chr = *s++;
if (cur_chr == '^')
{
cur_chr = *s++;
if (cur_chr == '\0')
{
*s1++ = '^';
break;
}
cur_chr &= 0x1F;
if (cur_chr != 0)
{
1994-11-12 05:25:32 +00:00
*s1++ = cur_chr;
}
1994-11-12 05:25:32 +00:00
continue;
}
if (cur_chr != '\\')
{
*s1++ = 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')
{
1994-11-12 05:25:32 +00:00
add_return = 0;
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
*s1++ = cur_chr;
}
1994-11-12 05:25:32 +00:00
break;
case '\\':
case 'K':
case 'p':
case 'd':
if (sending)
{
1994-11-12 05:25:32 +00:00
*s1++ = '\\';
}
1994-11-12 05:25:32 +00:00
*s1++ = cur_chr;
break;
case 'q':
1997-08-22 15:24:36 +00:00
quiet = 1;
1994-11-12 05:25:32 +00:00
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
{
1994-11-12 05:25:32 +00:00
*s1++ = 'N';
}
1994-11-12 05:25:32 +00:00
break;
1994-11-12 05:25:32 +00:00
default:
if (isoctal (cur_chr))
{
cur_chr &= 0x07;
if (isoctal (*s))
{
cur_chr <<= 3;
cur_chr |= *s++ - '0';
if (isoctal (*s))
{
cur_chr <<= 3;
cur_chr |= *s++ - '0';
}
}
if (cur_chr != 0 || sending)
{
if (sending && (cur_chr == '\\' || cur_chr == 0))
{
1994-11-12 05:25:32 +00:00
*s1++ = '\\';
}
1994-11-12 05:25:32 +00:00
*s1++ = cur_chr;
}
break;
}
if (sending)
{
1994-11-12 05:25:32 +00:00
*s1++ = '\\';
}
1994-11-12 05:25:32 +00:00
*s1++ = cur_chr;
break;
}
}
if (add_return)
{
1994-11-12 05:25:32 +00:00
*s1++ = '\r';
}
1994-11-12 05:25:32 +00:00
*s1++ = '\0'; /* guarantee closure */
*s1++ = '\0'; /* terminate the string */
return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
}
1997-08-22 15:24:36 +00:00
/*
* 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);
}
1994-11-12 05:25:32 +00:00
/*
* Process the expect string
*/
1997-08-22 15:24:36 +00:00
void chat_expect (s)
char *s;
1994-11-12 05:25:32 +00:00
{
1997-08-22 15:24:36 +00:00
char *expect;
char *reply;
if (strcmp(s, "HANGUP") == 0)
{
++hup_next;
return;
}
1994-11-12 05:25:32 +00:00
if (strcmp(s, "ABORT") == 0)
{
++abort_next;
return;
}
1997-08-22 15:24:36 +00:00
if (strcmp(s, "CLR_ABORT") == 0)
{
++clear_abort_next;
return;
}
if (strcmp(s, "REPORT") == 0)
{
++report_next;
return;
}
1997-08-22 15:24:36 +00:00
if (strcmp(s, "CLR_REPORT") == 0)
{
++clear_report_next;
return;
}
1994-11-12 05:25:32 +00:00
if (strcmp(s, "TIMEOUT") == 0)
{
++timeout_next;
return;
}
1997-08-22 15:24:36 +00:00
if (strcmp(s, "ECHO") == 0)
{
++echo_next;
return;
}
if (strcmp(s, "SAY") == 0)
1994-11-12 05:25:32 +00:00
{
1997-08-22 15:24:36 +00:00
++say_next;
return;
}
/*
* Fetch the expect and reply string.
*/
for (;;)
{
expect = expect_strtok (s, "-");
s = (char *) 0;
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
if (expect == (char *) 0)
{
1997-08-22 15:24:36 +00:00
return;
}
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
reply = expect_strtok (s, "-");
/*
* Handle the expect string. If successful then exit.
*/
if (get_string (expect))
{
return;
1994-11-12 05:25:32 +00:00
}
1997-08-22 15:24:36 +00:00
/*
* If there is a sub-reply string then send it. Otherwise any condition
* is terminal.
*/
if (reply == (char *) 0 || exit_code != 3)
{
1997-08-22 15:24:36 +00:00
break;
}
1997-08-22 15:24:36 +00:00
chat_send (reply);
}
/*
* The expectation did not occur. This is terminal.
*/
if (fail_reason)
{
syslog(LOG_INFO, "Failed (%s)", fail_reason);
1994-11-12 05:25:32 +00:00
}
1997-08-22 15:24:36 +00:00
else
{
syslog(LOG_INFO, "Failed");
}
terminate(exit_code);
1994-11-12 05:25:32 +00:00
}
1997-08-22 15:24:36 +00:00
/*
* Translate the input character to the appropriate string for printing
* the data.
*/
1994-11-12 05:25:32 +00:00
char *character(c)
int c;
1994-11-12 05:25:32 +00:00
{
static char string[10];
char *meta;
meta = (c & 0x80) ? "M-" : "";
c &= 0x7F;
if (c < 32)
{
1994-11-12 05:25:32 +00:00
sprintf(string, "%s^%c", meta, (int)c + '@');
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
if (c == 127)
{
1994-11-12 05:25:32 +00:00
sprintf(string, "%s^?", meta);
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
sprintf(string, "%s%c", meta, c);
}
}
1994-11-12 05:25:32 +00:00
return (string);
}
/*
* process the reply string
*/
void chat_send (s)
register char *s;
{
1997-08-22 15:24:36 +00:00
if (say_next)
{
say_next = 0;
s = clean(s,0);
write(2, s, strlen(s));
free(s);
return;
}
if (hup_next)
{
hup_next = 0;
if (strcmp(s, "OFF") == 0)
signal(SIGHUP, SIG_IGN);
else
signal(SIGHUP, sighup);
return;
}
if (echo_next)
{
echo_next = 0;
echo = (strcmp(s, "ON") == 0);
return;
}
1994-11-12 05:25:32 +00:00
if (abort_next)
{
1994-11-12 05:25:32 +00:00
char *s1;
1994-11-12 05:25:32 +00:00
abort_next = 0;
1994-11-12 05:25:32 +00:00
if (n_aborts >= MAX_ABORTS)
{
1994-11-12 05:25:32 +00:00
fatal("Too many ABORT strings");
}
1994-11-12 05:25:32 +00:00
s1 = clean(s, 0);
if (strlen(s1) > strlen(s)
|| strlen(s1) + 1 > sizeof(fail_buffer))
1994-11-12 05:25:32 +00:00
{
syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
die();
}
abort_string[n_aborts++] = s1;
if (verbose)
{
logf("abort on (");
for (s1 = s; *s1; ++s1)
{
1994-11-12 05:25:32 +00:00
logf(character(*s1));
}
1994-11-12 05:25:32 +00:00
logf(")\n");
}
return;
1994-11-12 05:25:32 +00:00
}
1997-08-22 15:24:36 +00:00
if (clear_abort_next)
{
char *s1;
char *s2 = s;
int i;
int old_max;
int pack = 0;
clear_abort_next = 0;
s1 = clean(s, 0);
if (strlen(s1) > strlen(s)
|| strlen(s1) + 1 > sizeof(fail_buffer))
{
syslog(LOG_WARNING, "Illegal or too-long CLR_ABORT string ('%s')", s);
die();
}
old_max = n_aborts;
for (i=0; i < n_aborts; i++)
{
if ( strcmp(s1,abort_string[i]) == 0 )
{
free(abort_string[i]);
abort_string[i] = NULL;
pack++;
n_aborts--;
if (verbose)
{
logf("clear abort on (");
for (s2 = s; *s2; ++s2)
{
logf(character(*s2));
}
logf(")\n");
}
}
}
free(s1);
if (pack) pack_array(abort_string,old_max);
return;
}
if (report_next)
{
char *s1;
report_next = 0;
if (n_reports >= MAX_REPORTS)
1994-11-12 05:25:32 +00:00
{
fatal("Too many REPORT strings");
}
s1 = clean(s, 0);
if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
{
syslog(LOG_WARNING, "Illegal or too-long REPORT string ('%s')", s);
die();
}
report_string[n_reports++] = s1;
if (verbose)
{
logf("report (");
s1 = s;
while (*s1)
{
logf(character(*s1));
++s1;
}
logf(")\n");
}
return;
}
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
if (clear_report_next)
{
char *s1;
char *s2 = s;
int i;
int old_max;
int pack = 0;
clear_report_next = 0;
s1 = clean(s, 0);
if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
{
syslog(LOG_WARNING, "Illegal or too-long REPORT string ('%s')", s);
die();
}
old_max = n_reports;
for (i=0; i < n_reports; i++)
{
if ( strcmp(s1,report_string[i]) == 0 )
{
free(report_string[i]);
report_string[i] = NULL;
pack++;
n_reports--;
if (verbose)
{
logf("clear report (");
for (s2 = s; *s2; ++s2)
{
logf(character(*s2));
}
logf(")\n");
}
}
}
free(s1);
if (pack) pack_array(report_string,old_max);
return;
}
if (timeout_next)
{
timeout_next = 0;
timeout = atoi(s);
if (timeout <= 0)
{
timeout = DEFAULT_CHAT_TIMEOUT;
}
1994-11-12 05:25:32 +00:00
if (verbose)
{
syslog(LOG_INFO, "timeout set to %d seconds", timeout);
1994-11-12 05:25:32 +00:00
}
return;
}
if (strcmp(s, "EOT") == 0)
{
s = "^D\\c";
}
else
{
if (strcmp(s, "BREAK") == 0)
1994-11-12 05:25:32 +00:00
{
s = "\\K\\c";
1994-11-12 05:25:32 +00:00
}
}
if (!put_string(s))
{
syslog(LOG_INFO, "Failed");
terminate(1);
}
1994-11-12 05:25:32 +00:00
}
int get_char()
{
int status;
char c;
status = read(0, &c, 1);
switch (status)
{
case 1:
return ((int)c & 0x7F);
1994-11-12 05:25:32 +00:00
default:
syslog(LOG_WARNING, "warning: read() on stdin returned %d",
status);
1994-11-12 05:25:32 +00:00
case -1:
if ((status = fcntl(0, F_GETFL, 0)) == -1)
{
sysfatal("Can't get file mode flags on stdin");
}
else
{
if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
{
sysfatal("Can't set file mode flags on stdin");
}
}
return (-1);
}
1994-11-12 05:25:32 +00:00
}
int put_char(c)
int c;
1994-11-12 05:25:32 +00:00
{
int status;
char ch = c;
1994-11-12 05:25:32 +00:00
usleep(10000); /* inter-character typing delay (?) */
1994-11-12 05:25:32 +00:00
status = write(1, &ch, 1);
1994-11-12 05:25:32 +00:00
switch (status)
{
case 1:
return (0);
default:
syslog(LOG_WARNING, "warning: write() on stdout returned %d",
status);
case -1:
if ((status = fcntl(0, F_GETFL, 0)) == -1)
{
sysfatal("Can't get file mode flags on stdin");
}
else
{
if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
{
sysfatal("Can't set file mode flags on stdin");
}
}
return (-1);
}
1994-11-12 05:25:32 +00:00
}
int write_char (c)
int c;
{
if (alarmed || put_char(c) < 0)
{
extern int errno;
alarm(0);
alarmed = 0;
1994-11-12 05:25:32 +00:00
if (verbose)
{
if (errno == EINTR || errno == EWOULDBLOCK)
{
1994-11-12 05:25:32 +00:00
syslog(LOG_INFO, " -- write timed out");
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
syslog(LOG_INFO, " -- write failed: %m");
}
1994-11-12 05:25:32 +00:00
}
return (0);
}
return (1);
}
int put_string (s)
register char *s;
{
1997-08-22 15:24:36 +00:00
quiet = 0;
1994-11-12 05:25:32 +00:00
s = clean(s, 1);
if (verbose)
{
logf("send (");
if (quiet)
{
1994-11-12 05:25:32 +00:00
logf("??????");
}
1994-11-12 05:25:32 +00:00
else
{
register char *s1 = s;
for (s1 = s; *s1; ++s1)
{
1994-11-12 05:25:32 +00:00
logf(character(*s1));
}
1994-11-12 05:25:32 +00:00
}
logf(")\n");
}
alarm(timeout); alarmed = 0;
while (*s)
{
register char c = *s++;
if (c != '\\')
{
if (!write_char (c))
{
1994-11-12 05:25:32 +00:00
return 0;
}
1994-11-12 05:25:32 +00:00
continue;
}
c = *s++;
switch (c)
{
case 'd':
sleep(1);
break;
case 'K':
break_sequence();
break;
case 'p':
usleep(10000); /* 1/100th of a second (arg is microseconds) */
1994-11-12 05:25:32 +00:00
break;
default:
if (!write_char (c))
return 0;
break;
}
}
alarm(0);
alarmed = 0;
return (1);
}
1997-08-22 15:24:36 +00:00
/*
* 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;
}
}
1994-11-12 05:25:32 +00:00
/*
* 'Wait for' this string to appear on this file descriptor.
*/
int get_string(string)
register char *string;
{
char temp[STR_LEN];
int c, printed = 0, len, minlen;
register char *s = temp, *end = s + STR_LEN;
fail_reason = (char *)0;
string = clean(string, 0);
len = strlen(string);
minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
if (verbose)
{
register char *s1;
logf("expect (");
for (s1 = string; *s1; ++s1)
{
1994-11-12 05:25:32 +00:00
logf(character(*s1));
}
1994-11-12 05:25:32 +00:00
logf(")\n");
}
if (len > STR_LEN)
{
syslog(LOG_INFO, "expect string is too long");
exit_code = 1;
1994-11-12 05:25:32 +00:00
return 0;
}
if (len == 0)
{
if (verbose)
{
syslog(LOG_INFO, "got it");
}
return (1);
}
alarm(timeout);
alarmed = 0;
1994-11-12 05:25:32 +00:00
while ( ! alarmed && (c = get_char()) >= 0)
{
int n, abort_len, report_len;
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
if (echo)
{
echo_stderr(c);
}
1994-11-12 05:25:32 +00:00
if (verbose)
{
if (c == '\n')
{
1994-11-12 05:25:32 +00:00
logf("\n");
}
1994-11-12 05:25:32 +00:00
else
{
1994-11-12 05:25:32 +00:00
logf(character(c));
}
1994-11-12 05:25:32 +00:00
}
1997-08-22 15:24:36 +00:00
if (Verbose) {
if (c == '\n') fputc( '\n', stderr );
else if (c != '\r') fprintf( stderr, "%s", character(c) );
}
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
*s++ = c;
if (!report_gathering)
{
for (n = 0; n < n_reports; ++n)
{
if ((report_string[n] != (char*) NULL) &&
s - temp >= (report_len = strlen(report_string[n])) &&
strncmp(s - report_len, report_string[n], report_len) == 0)
{
time_t time_now = time ((time_t*) NULL);
struct tm* tm_now = localtime (&time_now);
strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
strcat (report_buffer, report_string[n]);
report_string[n] = (char *) NULL;
report_gathering = 1;
break;
}
}
}
else
{
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);
}
}
1994-11-12 05:25:32 +00:00
1997-08-22 15:24:36 +00:00
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);
}
}
1994-11-12 05:25:32 +00:00
if (s >= end)
{
strncpy (temp, s - minlen, minlen);
1994-11-12 05:25:32 +00:00
s = temp + minlen;
}
if (alarmed && verbose)
{
1994-11-12 05:25:32 +00:00
syslog(LOG_WARNING, "warning: alarm synchronization problem");
}
1994-11-12 05:25:32 +00:00
}
alarm(0);
1994-11-12 05:25:32 +00:00
if (verbose && printed)
{
if (alarmed)
{
1994-11-12 05:25:32 +00:00
logf(" -- read timed out\n");
}
1994-11-12 05:25:32 +00:00
else
{
logflush();
syslog(LOG_INFO, " -- read failed: %m");
}
}
exit_code = 3;
alarmed = 0;
1994-11-12 05:25:32 +00:00
return (0);
}
#ifdef NO_USLEEP
1994-11-12 05:25:32 +00:00
#include <sys/types.h>
#include <sys/time.h>
/*
usleep -- support routine for 4.2BSD system call emulations
last edit: 29-Oct-1984 D A Gwyn
*/
extern int select();
int
usleep( usec ) /* returns 0 if ok, else -1 */
long usec; /* delay in microseconds */
{
static struct /* `timeval' */
{
long tv_sec; /* seconds */
long tv_usec; /* microsecs */
} delay; /* _select() timeout */
1994-11-12 05:25:32 +00:00
delay.tv_sec = usec / 1000000L;
1994-11-12 05:25:32 +00:00
delay.tv_usec = usec % 1000000L;
return select( 0, (long *)0, (long *)0, (long *)0, &delay );
}
#endif
1997-08-22 15:24:36 +00:00
void
pack_array (array, end)
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++) {
if (array[i] == NULL) {
for (j = i+1; j < end; ++j)
if (array[j] != NULL)
array[i++] = array[j];
for (; i < end; ++i)
array[i] = NULL;
break;
}
}
}