1999-08-25 06:52:15 +04:00
|
|
|
/* $NetBSD: chat.c,v 1.17 1999/08/25 02:52:15 christos Exp $ */
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
/*
|
|
|
|
* Chat -- a program for automatic session establishment (i.e. dial
|
|
|
|
* the phone and log in).
|
|
|
|
*
|
1995-07-05 03:36:19 +04:00
|
|
|
* Standard termination codes:
|
|
|
|
* 0 - successful completion of the script
|
|
|
|
* 1 - invalid argument, expect string too large, etc.
|
1998-05-02 18:55:19 +04:00
|
|
|
* 2 - error on an I/O operation or fatal error condition.
|
1995-07-05 03:36:19 +04:00
|
|
|
* 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.
|
|
|
|
*
|
1993-08-14 10:29:28 +04:00
|
|
|
* This software is in the public domain.
|
|
|
|
*
|
1997-09-26 23:52:08 +04:00
|
|
|
* -----------------
|
1999-08-25 06:52:15 +04:00
|
|
|
* 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>.
|
|
|
|
*
|
1998-05-02 18:55:19 +04:00
|
|
|
* 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>
|
|
|
|
*
|
1997-09-26 23:52:08 +04: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
|
1993-08-14 10:29:28 +04:00
|
|
|
*
|
1994-05-30 05:38:57 +04:00
|
|
|
*
|
1995-07-05 03:36:19 +04:00
|
|
|
* Added -r "report file" switch & REPORT keyword.
|
|
|
|
* Robert Geer <bgeer@xmission.com>
|
|
|
|
*
|
1998-05-02 18:55:19 +04:00
|
|
|
* Added -s "use stderr" and -S "don't use syslog" switches.
|
|
|
|
* June 18, 1997
|
|
|
|
* Karl O. Pinc <kop@meme.com>
|
|
|
|
*
|
1997-09-26 23:52:08 +04:00
|
|
|
*
|
1997-03-12 23:17:18 +03:00
|
|
|
* Added -e "echo" switch & ECHO keyword
|
|
|
|
* Dick Streefland <dicks@tasking.nl>
|
|
|
|
*
|
1997-09-26 23:52:08 +04:00
|
|
|
*
|
|
|
|
* Considerable updates and modifications by
|
|
|
|
* Al Longyear <longyear@pobox.com>
|
|
|
|
* Paul Mackerras <paulus@cs.anu.edu.au>
|
|
|
|
*
|
|
|
|
*
|
1994-05-30 05:38:57 +04:00
|
|
|
* The original author is:
|
|
|
|
*
|
1993-08-14 10:29:28 +04:00
|
|
|
* Karl Fox <karl@MorningStar.Com>
|
|
|
|
* Morning Star Technologies, Inc.
|
|
|
|
* 1760 Zollinger Road
|
|
|
|
* Columbus, OH 43221
|
|
|
|
* (614)451-1883
|
1995-07-05 03:36:19 +04:00
|
|
|
*
|
1993-08-14 10:29:28 +04:00
|
|
|
*/
|
|
|
|
|
1997-09-26 23:52:08 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-03-12 23:17:18 +03:00
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
1999-08-25 06:52:15 +04:00
|
|
|
static char rcsid[] = "Id: chat.c,v 1.24 1999/08/13 06:46:09 paulus Exp ";
|
1997-03-12 23:17:18 +03:00
|
|
|
#else
|
1999-08-25 06:52:15 +04:00
|
|
|
__RCSID("$NetBSD: chat.c,v 1.17 1999/08/25 02:52:15 christos Exp $");
|
1997-03-12 23:17:18 +03:00
|
|
|
#endif
|
1999-08-25 06:52:15 +04:00
|
|
|
#ifndef __STDC__
|
|
|
|
#define const
|
1997-03-12 23:17:18 +03:00
|
|
|
#endif
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1997-03-12 23:17:18 +03:00
|
|
|
#include <ctype.h>
|
1995-07-05 03:36:19 +04:00
|
|
|
#include <time.h>
|
1993-08-14 10:29:28 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
1994-05-30 05:38:57 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1997-03-12 23:17:18 +03:00
|
|
|
#include <unistd.h>
|
1993-08-14 10:29:28 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
1993-11-09 07:57:19 +03:00
|
|
|
#ifndef TERMIO
|
1994-05-30 05:38:57 +04:00
|
|
|
#undef TERMIOS
|
1993-08-14 10:29:28 +04:00
|
|
|
#define TERMIOS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TERMIO
|
|
|
|
#include <termio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef TERMIOS
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define STR_LEN 1024
|
|
|
|
|
1993-11-09 07:57:19 +03:00
|
|
|
#ifndef SIGTYPE
|
1993-08-14 10:29:28 +04:00
|
|
|
#define SIGTYPE void
|
|
|
|
#endif
|
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
#undef __P
|
1998-05-02 18:55:19 +04:00
|
|
|
#undef __V
|
|
|
|
|
|
|
|
#ifdef __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#define __V(x) x
|
1994-05-30 05:38:57 +04:00
|
|
|
#define __P(x) x
|
|
|
|
#else
|
1998-05-02 18:55:19 +04:00
|
|
|
#include <varargs.h>
|
|
|
|
#define __V(x) (va_alist) va_dcl
|
1994-05-30 05:38:57 +04:00
|
|
|
#define __P(x) ()
|
|
|
|
#define const
|
|
|
|
#endif
|
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
#ifdef SUNOS
|
|
|
|
extern int sys_nerr;
|
|
|
|
extern char *sys_errlist[];
|
|
|
|
#define memmove(to, from, n) bcopy(from, to, n)
|
|
|
|
#define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
|
|
|
|
"unknown error")
|
|
|
|
#endif
|
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
char *program_name;
|
|
|
|
|
|
|
|
#define MAX_ABORTS 50
|
1995-07-05 03:36:19 +04:00
|
|
|
#define MAX_REPORTS 50
|
1993-08-14 10:29:28 +04:00
|
|
|
#define DEFAULT_CHAT_TIMEOUT 45
|
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
int echo = 0;
|
1995-07-05 03:36:19 +04:00
|
|
|
int verbose = 0;
|
1998-05-02 18:55:19 +04:00
|
|
|
int to_log = 1;
|
|
|
|
int to_stderr = 0;
|
1997-03-12 23:17:18 +03:00
|
|
|
int Verbose = 0;
|
1995-07-05 03:36:19 +04:00
|
|
|
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;
|
1998-05-02 18:55:19 +04:00
|
|
|
char *phone_num = (char *) 0;
|
|
|
|
char *phone_num2 = (char *) 0;
|
1995-07-05 03:36:19 +04:00
|
|
|
int timeout = DEFAULT_CHAT_TIMEOUT;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
int have_tty_parameters = 0;
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
#ifdef TERMIO
|
1995-07-05 03:36:19 +04:00
|
|
|
#define term_parms struct termio
|
|
|
|
#define get_term_param(param) ioctl(0, TCGETA, param)
|
|
|
|
#define set_term_param(param) ioctl(0, TCSETA, param)
|
1993-08-14 10:29:28 +04:00
|
|
|
struct termio saved_tty_parameters;
|
|
|
|
#endif
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
#ifdef TERMIOS
|
1995-07-05 03:36:19 +04:00
|
|
|
#define term_parms struct termios
|
|
|
|
#define get_term_param(param) tcgetattr(0, param)
|
|
|
|
#define set_term_param(param) tcsetattr(0, TCSANOW, param)
|
1993-08-14 10:29:28 +04:00
|
|
|
struct termios saved_tty_parameters;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
|
|
|
|
fail_buffer[50];
|
1997-03-12 23:17:18 +03:00
|
|
|
int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
|
1997-09-26 23:52:08 +04:00
|
|
|
int clear_abort_next = 0;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
char *report_string[MAX_REPORTS] ;
|
|
|
|
char report_buffer[50] ;
|
|
|
|
int n_reports = 0, report_next = 0, report_gathering = 0 ;
|
1997-09-26 23:52:08 +04:00
|
|
|
int clear_report_next = 0;
|
|
|
|
|
|
|
|
int say_next = 0, hup_next = 0;
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
void *dup_mem __P((void *b, size_t c));
|
|
|
|
void *copy_of __P((char *s));
|
|
|
|
void usage __P((void));
|
1998-05-02 18:55:19 +04:00
|
|
|
void logf __P((const char *fmt, ...));
|
|
|
|
void fatal __P((int code, const char *fmt, ...));
|
1994-05-30 05:38:57 +04:00
|
|
|
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-03-12 23:17:18 +03:00
|
|
|
void echo_stderr __P((int));
|
1994-05-30 05:38:57 +04: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));
|
1995-07-05 03:36:19 +04:00
|
|
|
int put_char __P((int c));
|
1994-05-30 05:38:57 +04:00
|
|
|
int get_char __P((void));
|
|
|
|
void chat_send __P((register char *s));
|
1995-07-05 03:36:19 +04:00
|
|
|
char *character __P((int c));
|
1994-05-30 05:38:57 +04: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));
|
1997-09-26 23:52:08 +04:00
|
|
|
void pack_array __P((char **array, int end));
|
1997-03-12 23:17:18 +03:00
|
|
|
char *expect_strtok __P((char *, char *));
|
1998-05-02 18:55:19 +04:00
|
|
|
int vfmtmsg __P((char *, int, const char *, va_list)); /* vsprintf++ */
|
1997-03-12 23:17:18 +03:00
|
|
|
|
|
|
|
int main __P((int, char *[]));
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
void *dup_mem(b, c)
|
|
|
|
void *b;
|
|
|
|
size_t c;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1994-05-30 05:38:57 +04:00
|
|
|
void *ans = malloc (c);
|
|
|
|
if (!ans)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "memory error!");
|
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
memcpy (ans, b, c);
|
|
|
|
return ans;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
void *copy_of (s)
|
|
|
|
char *s;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1994-05-30 05:38:57 +04:00
|
|
|
return dup_mem (s, strlen (s) + 1);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
/*
|
1998-05-02 18:55:19 +04:00
|
|
|
* chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
|
|
|
|
* [ -r report-file ] \
|
1993-08-14 10:29:28 +04:00
|
|
|
* [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
|
|
|
|
*
|
|
|
|
* Perform a UUCP-dialer-like chat script on stdin and stdout.
|
|
|
|
*/
|
1994-05-30 05:38:57 +04:00
|
|
|
int
|
1993-08-14 10:29:28 +04:00
|
|
|
main(argc, argv)
|
1998-05-02 18:55:19 +04:00
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1994-05-30 05:38:57 +04:00
|
|
|
int option;
|
1997-10-15 00:38:09 +04:00
|
|
|
int i;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
program_name = *argv;
|
1995-07-05 03:36:19 +04:00
|
|
|
tzset();
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
while ((option = getopt(argc, argv, ":evVf:t:r:sST:U:")) != -1) {
|
|
|
|
switch (option) {
|
|
|
|
case 'e':
|
|
|
|
++echo;
|
|
|
|
break;
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 'v':
|
|
|
|
++verbose;
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 'V':
|
|
|
|
++Verbose;
|
|
|
|
break;
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 's':
|
|
|
|
++to_stderr;
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 'S':
|
|
|
|
to_log = 0;
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 'f':
|
|
|
|
if (optarg != NULL)
|
|
|
|
chat_file = copy_of(optarg);
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 't':
|
|
|
|
if (optarg != NULL)
|
|
|
|
timeout = atoi(optarg);
|
|
|
|
else
|
1993-08-14 10:29:28 +04:00
|
|
|
usage();
|
1998-05-02 18:55:19 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
if (optarg) {
|
|
|
|
if (report_fp != NULL)
|
|
|
|
fclose (report_fp);
|
|
|
|
report_file = copy_of (optarg);
|
|
|
|
report_fp = fopen (report_file, "a");
|
|
|
|
if (report_fp != NULL) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf (report_fp, "Opening \"%s\"...\n",
|
|
|
|
report_file);
|
|
|
|
report = 1;
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
if (optarg != NULL)
|
|
|
|
phone_num = copy_of(optarg);
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
if (optarg != NULL)
|
|
|
|
phone_num2 = copy_of(optarg);
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-10-15 00:38:09 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1995-07-05 03:36:19 +04:00
|
|
|
/*
|
|
|
|
* Default the report file to the stderr location
|
|
|
|
*/
|
|
|
|
if (report_fp == NULL)
|
|
|
|
report_fp = stderr;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (to_log) {
|
1994-05-30 05:38:57 +04:00
|
|
|
#ifdef ultrix
|
1998-05-02 18:55:19 +04:00
|
|
|
openlog("chat", LOG_PID);
|
1994-05-30 05:38:57 +04:00
|
|
|
#else
|
1998-05-02 18:55:19 +04:00
|
|
|
openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (verbose)
|
|
|
|
setlogmask(LOG_UPTO(LOG_INFO));
|
|
|
|
else
|
|
|
|
setlogmask(LOG_UPTO(LOG_WARNING));
|
1994-05-30 05:38:57 +04:00
|
|
|
#endif
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (chat_file != NULL) {
|
1997-10-15 00:38:09 +04:00
|
|
|
if (argc)
|
1994-05-30 05:38:57 +04:00
|
|
|
usage();
|
|
|
|
else
|
|
|
|
do_file (chat_file);
|
1998-05-02 18:55:19 +04:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
chat_expect(argv[i]);
|
1997-10-15 00:38:09 +04:00
|
|
|
if (++i < argc)
|
1998-05-02 18:55:19 +04:00
|
|
|
chat_send(argv[i]);
|
1993-08-14 10:29:28 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
terminate(0);
|
1997-03-12 23:17:18 +03:00
|
|
|
return 0;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
/*
|
|
|
|
* Process a chat script when read from a file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void do_file (chat_file)
|
|
|
|
char *chat_file;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1997-09-27 00:14:42 +04:00
|
|
|
int linect, sendflg;
|
1994-05-30 05:38:57 +04:00
|
|
|
char *sp, *arg, quote;
|
|
|
|
char buf [STR_LEN];
|
|
|
|
FILE *cfp;
|
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
cfp = fopen (chat_file, "r");
|
|
|
|
if (cfp == NULL)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "%s -- open failed: %m", chat_file);
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
linect = 0;
|
|
|
|
sendflg = 0;
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
while (fgets(buf, STR_LEN, cfp) != NULL) {
|
1994-05-30 05:38:57 +04:00
|
|
|
sp = strchr (buf, '\n');
|
|
|
|
if (sp)
|
|
|
|
*sp = '\0';
|
|
|
|
|
|
|
|
linect++;
|
|
|
|
sp = buf;
|
1997-09-26 23:52:08 +04:00
|
|
|
|
|
|
|
/* lines starting with '#' are comments. If a real '#'
|
|
|
|
is to be expected, it should be quoted .... */
|
1998-05-02 18:55:19 +04:00
|
|
|
if ( *sp == '#' )
|
|
|
|
continue;
|
1997-09-26 23:52:08 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
while (*sp != '\0') {
|
|
|
|
if (*sp == ' ' || *sp == '\t') {
|
1994-05-30 05:38:57 +04:00
|
|
|
++sp;
|
|
|
|
continue;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (*sp == '"' || *sp == '\'') {
|
1994-05-30 05:38:57 +04:00
|
|
|
quote = *sp++;
|
|
|
|
arg = sp;
|
1998-05-02 18:55:19 +04:00
|
|
|
while (*sp != quote) {
|
1994-05-30 05:38:57 +04:00
|
|
|
if (*sp == '\0')
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "unterminated quote (line %d)", linect);
|
|
|
|
|
|
|
|
if (*sp++ == '\\') {
|
1994-05-30 05:38:57 +04:00
|
|
|
if (*sp != '\0')
|
|
|
|
++sp;
|
|
|
|
}
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
else {
|
1994-05-30 05:38:57 +04:00
|
|
|
arg = sp;
|
|
|
|
while (*sp != '\0' && *sp != ' ' && *sp != '\t')
|
|
|
|
++sp;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
if (*sp != '\0')
|
|
|
|
*sp++ = '\0';
|
|
|
|
|
|
|
|
if (sendflg)
|
|
|
|
chat_send (arg);
|
|
|
|
else
|
|
|
|
chat_expect (arg);
|
|
|
|
sendflg = !sendflg;
|
|
|
|
}
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
fclose (cfp);
|
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
/*
|
|
|
|
* We got an error parsing the command line.
|
|
|
|
*/
|
1994-05-30 05:38:57 +04:00
|
|
|
void usage()
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1994-05-30 05:38:57 +04:00
|
|
|
fprintf(stderr, "\
|
1998-05-02 18:55:19 +04:00
|
|
|
Usage: %s [-e] [-v] [-t timeout] [-r report-file] [-T phone-number]\n\
|
|
|
|
[-U phone-number2] {-f chat-file | chat-script}\n", program_name);
|
1993-08-14 10:29:28 +04:00
|
|
|
exit(1);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
char line[1024];
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
/*
|
|
|
|
* Send a message to syslog and/or stderr.
|
|
|
|
*/
|
|
|
|
void logf __V((const char *fmt, ...))
|
1997-03-12 23:17:18 +03:00
|
|
|
{
|
1998-05-02 18:55:19 +04:00
|
|
|
va_list args;
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(args, fmt);
|
|
|
|
#else
|
|
|
|
char *fmt;
|
|
|
|
va_start(args);
|
|
|
|
fmt = va_arg(args, char *);
|
|
|
|
#endif
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
vfmtmsg(line, sizeof(line), fmt, args);
|
|
|
|
if (to_log)
|
1994-05-30 05:38:57 +04:00
|
|
|
syslog(LOG_INFO, "%s", line);
|
1998-05-02 18:55:19 +04:00
|
|
|
if (to_stderr)
|
|
|
|
fprintf(stderr, "%s\n", line);
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Print an error message and terminate.
|
|
|
|
*/
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
void fatal __V((int code, const char *fmt, ...))
|
|
|
|
{
|
|
|
|
va_list args;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(args, fmt);
|
|
|
|
#else
|
|
|
|
int code;
|
|
|
|
char *fmt;
|
|
|
|
va_start(args);
|
|
|
|
code = va_arg(args, int);
|
|
|
|
fmt = va_arg(args, char *);
|
|
|
|
#endif
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
vfmtmsg(line, sizeof(line), fmt, args);
|
|
|
|
if (to_log)
|
|
|
|
syslog(LOG_ERR, "%s", line);
|
|
|
|
if (to_stderr)
|
|
|
|
fprintf(stderr, "%s\n", line);
|
|
|
|
terminate(code);
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
int alarmed = 0;
|
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
SIGTYPE sigalrm(signo)
|
|
|
|
int signo;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-08-14 10:29:28 +04:00
|
|
|
int flags;
|
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
alarm(1);
|
|
|
|
alarmed = 1; /* Reset alarm to avoid race window */
|
1993-11-09 07:57:19 +03:00
|
|
|
signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
if ((flags = fcntl(0, F_GETFL, 0)) == -1)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't get file mode flags on stdin: %m");
|
|
|
|
|
|
|
|
if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
|
|
|
|
fatal(2, "Can't set file mode flags on stdin: %m");
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
if (verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("alarm");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
void unalarm()
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-08-14 10:29:28 +04:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((flags = fcntl(0, F_GETFL, 0)) == -1)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't get file mode flags on stdin: %m");
|
|
|
|
|
|
|
|
if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
|
|
|
|
fatal(2, "Can't set file mode flags on stdin: %m");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
SIGTYPE sigint(signo)
|
|
|
|
int signo;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
|
|
|
fatal(2, "SIGINT");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
SIGTYPE sigterm(signo)
|
|
|
|
int signo;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
|
|
|
fatal(2, "SIGTERM");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
SIGTYPE sighup(signo)
|
|
|
|
int signo;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
|
|
|
fatal(2, "SIGHUP");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
void init()
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-11-09 07:57:19 +03:00
|
|
|
signal(SIGINT, sigint);
|
|
|
|
signal(SIGTERM, sigterm);
|
|
|
|
signal(SIGHUP, sighup);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
set_tty_parameters();
|
1993-11-09 07:57:19 +03:00
|
|
|
signal(SIGALRM, sigalrm);
|
1994-05-30 05:38:57 +04:00
|
|
|
alarm(0);
|
|
|
|
alarmed = 0;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
void set_tty_parameters()
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1995-07-05 03:36:19 +04:00
|
|
|
#if defined(get_term_param)
|
|
|
|
term_parms t;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
if (get_term_param (&t) < 0)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't get terminal parameters: %m");
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
saved_tty_parameters = t;
|
1995-07-05 03:36:19 +04:00
|
|
|
have_tty_parameters = 1;
|
|
|
|
|
|
|
|
t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
|
1999-05-06 12:59:21 +04:00
|
|
|
t.c_oflag |= OPOST | ONLCR;
|
1995-07-05 03:36:19 +04:00
|
|
|
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)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't set terminal parameters: %m");
|
1993-08-14 10:29:28 +04:00
|
|
|
#endif
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
void break_sequence()
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1994-05-30 05:38:57 +04:00
|
|
|
#ifdef TERMIOS
|
|
|
|
tcsendbreak (0, 0);
|
|
|
|
#endif
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
void terminate(status)
|
|
|
|
int status;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1999-08-25 06:52:15 +04:00
|
|
|
static int terminating = 0;
|
|
|
|
|
|
|
|
if (terminating)
|
|
|
|
exit(status);
|
|
|
|
terminating = 1;
|
1997-03-12 23:17:18 +03:00
|
|
|
echo_stderr(-1);
|
|
|
|
/*
|
|
|
|
* Allow the last of the report string to be gathered before we terminate.
|
|
|
|
*/
|
1999-08-25 06:52:15 +04:00
|
|
|
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;
|
1997-03-12 23:17:18 +03:00
|
|
|
}
|
1999-08-25 06:52:15 +04:00
|
|
|
report_buffer[rep_len] = 0;
|
|
|
|
fprintf (report_fp, "chat: %s\n", report_buffer);
|
|
|
|
}
|
|
|
|
if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
|
1995-07-05 03:36:19 +04:00
|
|
|
if (verbose)
|
|
|
|
fprintf (report_fp, "Closing \"%s\".\n", report_file);
|
|
|
|
fclose (report_fp);
|
1997-03-12 23:17:18 +03:00
|
|
|
report_fp = (FILE *) NULL;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
#if defined(get_term_param)
|
1998-05-02 18:55:19 +04:00
|
|
|
if (have_tty_parameters) {
|
1995-07-05 03:36:19 +04:00
|
|
|
if (set_term_param (&saved_tty_parameters) < 0)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't restore terminal parameters: %m");
|
|
|
|
}
|
1995-07-05 03:36:19 +04:00
|
|
|
#endif
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
exit(status);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 'Clean up' this string.
|
|
|
|
*/
|
|
|
|
char *clean(s, sending)
|
|
|
|
register char *s;
|
1998-05-02 18:55:19 +04:00
|
|
|
int sending; /* set to 1 when sending (putting) this string. */
|
|
|
|
{
|
1994-05-30 05:38:57 +04:00
|
|
|
char temp[STR_LEN], cur_chr;
|
1998-05-02 18:55:19 +04:00
|
|
|
register char *s1, *phchar;
|
1993-08-14 10:29:28 +04:00
|
|
|
int add_return = sending;
|
1994-05-30 05:38:57 +04:00
|
|
|
#define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
|
|
|
|
|
|
|
|
s1 = temp;
|
1998-05-02 18:55:19 +04:00
|
|
|
while (*s) {
|
1994-05-30 05:38:57 +04:00
|
|
|
cur_chr = *s++;
|
1998-05-02 18:55:19 +04:00
|
|
|
if (cur_chr == '^') {
|
1994-05-30 05:38:57 +04:00
|
|
|
cur_chr = *s++;
|
1998-05-02 18:55:19 +04:00
|
|
|
if (cur_chr == '\0') {
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = '^';
|
|
|
|
break;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
cur_chr &= 0x1F;
|
1998-05-02 18:55:19 +04:00
|
|
|
if (cur_chr != 0) {
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = cur_chr;
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
continue;
|
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (cur_chr != '\\') {
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = cur_chr;
|
|
|
|
continue;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
cur_chr = *s++;
|
1998-05-02 18:55:19 +04:00
|
|
|
if (cur_chr == '\0') {
|
|
|
|
if (sending) {
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = '\\';
|
|
|
|
*s1++ = '\\';
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
break;
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
switch (cur_chr) {
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'b':
|
|
|
|
*s1++ = '\b';
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'c':
|
|
|
|
if (sending && *s == '\0')
|
|
|
|
add_return = 0;
|
|
|
|
else
|
|
|
|
*s1++ = cur_chr;
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case '\\':
|
|
|
|
case 'K':
|
|
|
|
case 'p':
|
|
|
|
case 'd':
|
|
|
|
if (sending)
|
|
|
|
*s1++ = '\\';
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = cur_chr;
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
case 'T':
|
|
|
|
if (sending && phone_num) {
|
|
|
|
for ( phchar = phone_num; *phchar != '\0'; phchar++)
|
|
|
|
*s1++ = *phchar;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*s1++ = '\\';
|
|
|
|
*s1++ = 'T';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
if (sending && phone_num2) {
|
|
|
|
for ( phchar = phone_num2; *phchar != '\0'; phchar++)
|
|
|
|
*s1++ = *phchar;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*s1++ = '\\';
|
|
|
|
*s1++ = 'U';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'q':
|
1997-05-18 01:23:04 +04:00
|
|
|
quiet = 1;
|
1994-05-30 05:38:57 +04:00
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'r':
|
|
|
|
*s1++ = '\r';
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'n':
|
|
|
|
*s1++ = '\n';
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 's':
|
|
|
|
*s1++ = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
*s1++ = '\t';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
1998-05-02 18:55:19 +04:00
|
|
|
if (sending) {
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = '\\';
|
|
|
|
*s1++ = '\0';
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
else
|
|
|
|
*s1++ = 'N';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1998-05-02 18:55:19 +04:00
|
|
|
if (isoctal (cur_chr)) {
|
1994-05-30 05:38:57 +04:00
|
|
|
cur_chr &= 0x07;
|
1998-05-02 18:55:19 +04:00
|
|
|
if (isoctal (*s)) {
|
1994-05-30 05:38:57 +04:00
|
|
|
cur_chr <<= 3;
|
|
|
|
cur_chr |= *s++ - '0';
|
1998-05-02 18:55:19 +04:00
|
|
|
if (isoctal (*s)) {
|
1994-05-30 05:38:57 +04:00
|
|
|
cur_chr <<= 3;
|
|
|
|
cur_chr |= *s++ - '0';
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (cur_chr != 0 || sending) {
|
1994-05-30 05:38:57 +04:00
|
|
|
if (sending && (cur_chr == '\\' || cur_chr == 0))
|
|
|
|
*s1++ = '\\';
|
|
|
|
*s1++ = cur_chr;
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
break;
|
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
if (sending)
|
|
|
|
*s1++ = '\\';
|
|
|
|
*s1++ = cur_chr;
|
|
|
|
break;
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
if (add_return)
|
|
|
|
*s1++ = '\r';
|
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
*s1++ = '\0'; /* guarantee closure */
|
|
|
|
*s1++ = '\0'; /* terminate the string */
|
|
|
|
return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* A modified version of 'strtok'. This version skips \ sequences.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *expect_strtok (s, term)
|
1998-05-02 18:55:19 +04:00
|
|
|
char *s, *term;
|
|
|
|
{
|
1997-03-12 23:17:18 +03:00
|
|
|
static char *str = "";
|
|
|
|
int escape_flag = 0;
|
|
|
|
char *result;
|
1998-05-02 18:55:19 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* If a string was specified then do initial processing.
|
|
|
|
*/
|
|
|
|
if (s)
|
|
|
|
str = s;
|
1998-05-02 18:55:19 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* If this is the escape flag then reset it and ignore the character.
|
|
|
|
*/
|
|
|
|
if (*str)
|
|
|
|
result = str;
|
|
|
|
else
|
|
|
|
result = (char *) 0;
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
while (*str) {
|
|
|
|
if (escape_flag) {
|
1997-03-12 23:17:18 +03:00
|
|
|
escape_flag = 0;
|
|
|
|
++str;
|
|
|
|
continue;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (*str == '\\') {
|
1997-03-12 23:17:18 +03:00
|
|
|
++str;
|
|
|
|
escape_flag = 1;
|
|
|
|
continue;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* If this is not in the termination string, continue.
|
|
|
|
*/
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strchr (term, *str) == (char *) 0) {
|
1997-03-12 23:17:18 +03:00
|
|
|
++str;
|
|
|
|
continue;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* This is the terminator. Mark the end of the string and stop.
|
|
|
|
*/
|
|
|
|
*str++ = '\0';
|
|
|
|
break;
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
return (result);
|
|
|
|
}
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
/*
|
1994-05-30 05:38:57 +04:00
|
|
|
* Process the expect string
|
1993-08-14 10:29:28 +04:00
|
|
|
*/
|
1997-03-12 23:17:18 +03:00
|
|
|
|
|
|
|
void chat_expect (s)
|
|
|
|
char *s;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1997-03-12 23:17:18 +03:00
|
|
|
char *expect;
|
|
|
|
char *reply;
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "HANGUP") == 0) {
|
1997-09-26 23:52:08 +04:00
|
|
|
++hup_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "ABORT") == 0) {
|
1993-08-14 10:29:28 +04:00
|
|
|
++abort_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "CLR_ABORT") == 0) {
|
1997-09-26 23:52:08 +04:00
|
|
|
++clear_abort_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "REPORT") == 0) {
|
1995-07-05 03:36:19 +04:00
|
|
|
++report_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "CLR_REPORT") == 0) {
|
1997-09-26 23:52:08 +04:00
|
|
|
++clear_report_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "TIMEOUT") == 0) {
|
1993-08-14 10:29:28 +04:00
|
|
|
++timeout_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (strcmp(s, "ECHO") == 0) {
|
1997-03-12 23:17:18 +03:00
|
|
|
++echo_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(s, "SAY") == 0) {
|
1997-09-26 23:52:08 +04:00
|
|
|
++say_next;
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* Fetch the expect and reply string.
|
|
|
|
*/
|
1998-05-02 18:55:19 +04:00
|
|
|
for (;;) {
|
1997-03-12 23:17:18 +03:00
|
|
|
expect = expect_strtok (s, "-");
|
|
|
|
s = (char *) 0;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
if (expect == (char *) 0)
|
|
|
|
return;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
reply = expect_strtok (s, "-");
|
1998-05-02 18:55:19 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* Handle the expect string. If successful then exit.
|
|
|
|
*/
|
|
|
|
if (get_string (expect))
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* If there is a sub-reply string then send it. Otherwise any condition
|
|
|
|
* is terminal.
|
|
|
|
*/
|
|
|
|
if (reply == (char *) 0 || exit_code != 3)
|
|
|
|
break;
|
|
|
|
|
|
|
|
chat_send (reply);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* The expectation did not occur. This is terminal.
|
|
|
|
*/
|
|
|
|
if (fail_reason)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("Failed (%s)", fail_reason);
|
1997-03-12 23:17:18 +03:00
|
|
|
else
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("Failed");
|
1997-03-12 23:17:18 +03:00
|
|
|
terminate(exit_code);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
/*
|
|
|
|
* Translate the input character to the appropriate string for printing
|
|
|
|
* the data.
|
|
|
|
*/
|
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
char *character(c)
|
1995-07-05 03:36:19 +04:00
|
|
|
int c;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-08-14 10:29:28 +04:00
|
|
|
static char string[10];
|
|
|
|
char *meta;
|
|
|
|
|
|
|
|
meta = (c & 0x80) ? "M-" : "";
|
|
|
|
c &= 0x7F;
|
|
|
|
|
|
|
|
if (c < 32)
|
|
|
|
sprintf(string, "%s^%c", meta, (int)c + '@');
|
1998-05-02 18:55:19 +04:00
|
|
|
else if (c == 127)
|
|
|
|
sprintf(string, "%s^?", meta);
|
1993-08-14 10:29:28 +04:00
|
|
|
else
|
1998-05-02 18:55:19 +04:00
|
|
|
sprintf(string, "%s%c", meta, c);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
return (string);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
/*
|
1994-05-30 05:38:57 +04:00
|
|
|
* process the reply string
|
1993-08-14 10:29:28 +04:00
|
|
|
*/
|
1994-05-30 05:38:57 +04:00
|
|
|
void chat_send (s)
|
1993-08-14 10:29:28 +04:00
|
|
|
register char *s;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1999-08-25 06:52:15 +04:00
|
|
|
char file_data[STR_LEN];
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (say_next) {
|
1997-09-26 23:52:08 +04:00
|
|
|
say_next = 0;
|
|
|
|
s = clean(s,0);
|
|
|
|
write(2, s, strlen(s));
|
|
|
|
free(s);
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hup_next) {
|
1997-09-26 23:52:08 +04:00
|
|
|
hup_next = 0;
|
|
|
|
if (strcmp(s, "OFF") == 0)
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
else
|
|
|
|
signal(SIGHUP, sighup);
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (echo_next) {
|
1997-03-12 23:17:18 +03:00
|
|
|
echo_next = 0;
|
|
|
|
echo = (strcmp(s, "ON") == 0);
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (abort_next) {
|
1993-08-14 10:29:28 +04:00
|
|
|
char *s1;
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
abort_next = 0;
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
if (n_aborts >= MAX_ABORTS)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Too many ABORT strings");
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
s1 = clean(s, 0);
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
if (strlen(s1) > strlen(s)
|
|
|
|
|| strlen(s1) + 1 > sizeof(fail_buffer))
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "Illegal or too-long ABORT string ('%v')", s);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
abort_string[n_aborts++] = s1;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
if (verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("abort on (%v)", s);
|
1995-07-05 03:36:19 +04:00
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (clear_abort_next) {
|
1997-09-26 23:52:08 +04:00
|
|
|
char *s1;
|
|
|
|
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))
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
|
1997-09-26 23:52:08 +04:00
|
|
|
|
|
|
|
old_max = n_aborts;
|
1998-05-02 18:55:19 +04:00
|
|
|
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 (%v)", s);
|
1997-09-26 23:52:08 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
free(s1);
|
1998-05-02 18:55:19 +04:00
|
|
|
if (pack)
|
|
|
|
pack_array(abort_string,old_max);
|
1997-09-26 23:52:08 +04:00
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (report_next) {
|
1995-07-05 03:36:19 +04:00
|
|
|
char *s1;
|
|
|
|
|
|
|
|
report_next = 0;
|
|
|
|
if (n_reports >= MAX_REPORTS)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Too many REPORT strings");
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
s1 = clean(s, 0);
|
|
|
|
|
|
|
|
if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "Illegal or too-long REPORT string ('%v')", s);
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
report_string[n_reports++] = s1;
|
|
|
|
|
|
|
|
if (verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("report (%v)", s);
|
1995-07-05 03:36:19 +04:00
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (clear_report_next) {
|
1997-09-26 23:52:08 +04:00
|
|
|
char *s1;
|
|
|
|
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)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "Illegal or too-long REPORT string ('%v')", s);
|
1997-09-26 23:52:08 +04:00
|
|
|
|
|
|
|
old_max = n_reports;
|
1998-05-02 18:55:19 +04:00
|
|
|
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 (%v)", s);
|
1997-09-26 23:52:08 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
free(s1);
|
1998-05-02 18:55:19 +04:00
|
|
|
if (pack)
|
|
|
|
pack_array(report_string,old_max);
|
1997-09-26 23:52:08 +04:00
|
|
|
|
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-09-26 23:52:08 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (timeout_next) {
|
1995-07-05 03:36:19 +04:00
|
|
|
timeout_next = 0;
|
|
|
|
timeout = atoi(s);
|
|
|
|
|
|
|
|
if (timeout <= 0)
|
|
|
|
timeout = DEFAULT_CHAT_TIMEOUT;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
if (verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("timeout set to %d seconds", timeout);
|
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
return;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1999-08-25 06:52:15 +04:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
while (*++fn == ' ' || *fn == '\t')
|
|
|
|
;
|
|
|
|
|
|
|
|
if (*fn != 0) {
|
|
|
|
FILE *f;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
/* open the file and read until STR_LEN-1 bytes or end-of-file */
|
|
|
|
f = fopen(fn, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
fatal(1, "%s -- open failed: %m", fn);
|
|
|
|
while (n < STR_LEN - 1) {
|
|
|
|
int nr = fread(&file_data[n], 1, STR_LEN - 1 - n, f);
|
|
|
|
if (nr < 0)
|
|
|
|
fatal(1, "%s -- read error", fn);
|
|
|
|
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. */
|
|
|
|
if (n > 0 && file_data[n-1] == '\n')
|
|
|
|
--n;
|
|
|
|
file_data[n] = 0;
|
|
|
|
s = file_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
if (strcmp(s, "EOT") == 0)
|
|
|
|
s = "^D\\c";
|
1998-05-02 18:55:19 +04:00
|
|
|
else if (strcmp(s, "BREAK") == 0)
|
|
|
|
s = "\\K\\c";
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
if (!put_string(s))
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(1, "Failed");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
int get_char()
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-08-14 10:29:28 +04:00
|
|
|
int status;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
status = read(0, &c, 1);
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
switch (status) {
|
1995-07-05 03:36:19 +04:00
|
|
|
case 1:
|
|
|
|
return ((int)c & 0x7F);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
default:
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("warning: read() on stdin returned %d", status);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
case -1:
|
|
|
|
if ((status = fcntl(0, F_GETFL, 0)) == -1)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't get file mode flags on stdin: %m");
|
|
|
|
|
|
|
|
if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
|
|
|
|
fatal(2, "Can't set file mode flags on stdin: %m");
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
return (-1);
|
1993-08-14 10:29:28 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
int put_char(c)
|
1995-07-05 03:36:19 +04:00
|
|
|
int c;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-08-14 10:29:28 +04:00
|
|
|
int status;
|
1995-07-05 03:36:19 +04:00
|
|
|
char ch = c;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
usleep(10000); /* inter-character typing delay (?) */
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
status = write(1, &ch, 1);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
switch (status) {
|
1995-07-05 03:36:19 +04:00
|
|
|
case 1:
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
default:
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("warning: write() on stdout returned %d", status);
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
case -1:
|
|
|
|
if ((status = fcntl(0, F_GETFL, 0)) == -1)
|
1998-05-02 18:55:19 +04:00
|
|
|
fatal(2, "Can't get file mode flags on stdin, %m");
|
|
|
|
|
|
|
|
if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
|
|
|
|
fatal(2, "Can't set file mode flags on stdin: %m");
|
1995-07-05 03:36:19 +04:00
|
|
|
|
|
|
|
return (-1);
|
1993-08-14 10:29:28 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
int write_char (c)
|
|
|
|
int c;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
|
|
|
if (alarmed || put_char(c) < 0) {
|
1995-07-05 03:36:19 +04:00
|
|
|
alarm(0);
|
|
|
|
alarmed = 0;
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (verbose) {
|
1994-05-30 05:38:57 +04:00
|
|
|
if (errno == EINTR || errno == EWOULDBLOCK)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf(" -- write timed out");
|
1994-05-30 05:38:57 +04:00
|
|
|
else
|
1998-05-02 18:55:19 +04:00
|
|
|
logf(" -- write failed: %m");
|
1994-05-30 05:38:57 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
return (0);
|
1994-05-30 05:38:57 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
return (1);
|
|
|
|
}
|
1994-05-30 05:38:57 +04:00
|
|
|
|
|
|
|
int put_string (s)
|
1993-08-14 10:29:28 +04:00
|
|
|
register char *s;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1997-05-18 01:23:04 +04:00
|
|
|
quiet = 0;
|
1993-08-14 10:29:28 +04:00
|
|
|
s = clean(s, 1);
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (verbose) {
|
1993-08-14 10:29:28 +04:00
|
|
|
if (quiet)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("send (??????)");
|
1993-08-14 10:29:28 +04:00
|
|
|
else
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("send (%v)", s);
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
alarm(timeout); alarmed = 0;
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
while (*s) {
|
1994-05-30 05:38:57 +04:00
|
|
|
register char c = *s++;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (c != '\\') {
|
1994-05-30 05:38:57 +04:00
|
|
|
if (!write_char (c))
|
|
|
|
return 0;
|
|
|
|
continue;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
c = *s++;
|
1998-05-02 18:55:19 +04:00
|
|
|
switch (c) {
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'd':
|
|
|
|
sleep(1);
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'K':
|
|
|
|
break_sequence();
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
case 'p':
|
1995-07-05 03:36:19 +04:00
|
|
|
usleep(10000); /* 1/100th of a second (arg is microseconds) */
|
1994-05-30 05:38:57 +04:00
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
default:
|
|
|
|
if (!write_char (c))
|
|
|
|
return 0;
|
|
|
|
break;
|
1993-08-14 10:29:28 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1994-05-30 05:38:57 +04:00
|
|
|
alarm(0);
|
|
|
|
alarmed = 0;
|
1993-08-14 10:29:28 +04:00
|
|
|
return (1);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03: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;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
|
|
|
static int need_lf;
|
|
|
|
char *s;
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
switch (n) {
|
|
|
|
case '\r': /* ignore '\r' */
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
if (need_lf == 0)
|
1997-03-12 23:17:18 +03:00
|
|
|
break;
|
1998-05-02 18:55:19 +04:00
|
|
|
/* 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;
|
1997-03-12 23:17:18 +03:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
/*
|
|
|
|
* 'Wait for' this string to appear on this file descriptor.
|
|
|
|
*/
|
|
|
|
int get_string(string)
|
|
|
|
register char *string;
|
1998-05-02 18:55:19 +04:00
|
|
|
{
|
1993-08-14 10:29:28 +04:00
|
|
|
char temp[STR_LEN];
|
1993-11-09 07:57:19 +03:00
|
|
|
int c, printed = 0, len, minlen;
|
1993-08-14 10:29:28 +04:00
|
|
|
register char *s = temp, *end = s + STR_LEN;
|
1998-05-02 18:55:19 +04:00
|
|
|
char *logged = temp;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
fail_reason = (char *)0;
|
|
|
|
string = clean(string, 0);
|
|
|
|
len = strlen(string);
|
1993-11-09 07:57:19 +03:00
|
|
|
minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
if (verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("expect (%v)", string);
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (len > STR_LEN) {
|
|
|
|
logf("expect string is too long");
|
1995-07-05 03:36:19 +04:00
|
|
|
exit_code = 1;
|
1994-05-30 05:38:57 +04:00
|
|
|
return 0;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-11-09 07:57:19 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (len == 0) {
|
1993-08-14 10:29:28 +04:00
|
|
|
if (verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("got it");
|
1993-08-14 10:29:28 +04:00
|
|
|
return (1);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
alarm(timeout);
|
|
|
|
alarmed = 0;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
while ( ! alarmed && (c = get_char()) >= 0) {
|
1995-07-05 03:36:19 +04:00
|
|
|
int n, abort_len, report_len;
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
if (echo)
|
|
|
|
echo_stderr(c);
|
1998-05-02 18:55:19 +04:00
|
|
|
if (verbose && c == '\n') {
|
|
|
|
if (s == logged)
|
|
|
|
logf(""); /* blank line */
|
1993-08-14 10:29:28 +04:00
|
|
|
else
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("%0.*v", s - logged, logged);
|
|
|
|
logged = s + 1;
|
1997-03-12 23:17:18 +03:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
*s++ = c;
|
1995-07-05 03:36:19 +04:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (verbose && s >= logged + 80) {
|
|
|
|
logf("%0.*v", s - logged, logged);
|
|
|
|
logged = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Verbose) {
|
|
|
|
if (c == '\n')
|
|
|
|
fputc( '\n', stderr );
|
|
|
|
else if (c != '\r')
|
|
|
|
fprintf( stderr, "%s", character(c) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!report_gathering) {
|
|
|
|
for (n = 0; n < n_reports; ++n) {
|
1995-07-05 03:36:19 +04:00
|
|
|
if ((report_string[n] != (char*) NULL) &&
|
|
|
|
s - temp >= (report_len = strlen(report_string[n])) &&
|
1998-05-02 18:55:19 +04:00
|
|
|
strncmp(s - report_len, report_string[n], report_len) == 0) {
|
1995-07-05 03:36:19 +04:00
|
|
|
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;
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1995-07-05 03:36:19 +04:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!iscntrl (c)) {
|
1995-07-05 03:36:19 +04:00
|
|
|
int rep_len = strlen (report_buffer);
|
|
|
|
report_buffer[rep_len] = c;
|
|
|
|
report_buffer[rep_len + 1] = '\0';
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
else {
|
1995-07-05 03:36:19 +04:00
|
|
|
report_gathering = 0;
|
|
|
|
fprintf (report_fp, "chat: %s\n", report_buffer);
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
if (s - temp >= len &&
|
|
|
|
c == string[len - 1] &&
|
1998-05-02 18:55:19 +04:00
|
|
|
strncmp(s - len, string, len) == 0) {
|
|
|
|
if (verbose) {
|
|
|
|
if (s > logged)
|
|
|
|
logf("%0.*v", s - logged, logged);
|
1997-03-12 23:17:18 +03:00
|
|
|
logf(" -- got it\n");
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-03-12 23:17:18 +03:00
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
alarmed = 0;
|
|
|
|
return (1);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
for (n = 0; n < n_aborts; ++n) {
|
1997-03-12 23:17:18 +03:00
|
|
|
if (s - temp >= (abort_len = strlen(abort_string[n])) &&
|
1998-05-02 18:55:19 +04:00
|
|
|
strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
|
|
|
|
if (verbose) {
|
|
|
|
if (s > logged)
|
|
|
|
logf("%0.*v", s - logged, logged);
|
|
|
|
logf(" -- failed");
|
|
|
|
}
|
|
|
|
|
1997-03-12 23:17:18 +03:00
|
|
|
alarm(0);
|
|
|
|
alarmed = 0;
|
|
|
|
exit_code = n + 4;
|
|
|
|
strcpy(fail_reason = fail_buffer, abort_string[n]);
|
|
|
|
return (0);
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
1997-03-12 23:17:18 +03:00
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (s >= end) {
|
|
|
|
if (logged < s - minlen) {
|
|
|
|
logf("%0.*v", s - logged, logged);
|
|
|
|
logged = s;
|
1993-11-09 07:57:19 +03:00
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
s -= minlen;
|
|
|
|
memmove(temp, s, minlen);
|
|
|
|
logged = temp + (logged - s);
|
|
|
|
s = temp + minlen;
|
|
|
|
}
|
1993-11-09 07:57:19 +03:00
|
|
|
|
1993-08-14 10:29:28 +04:00
|
|
|
if (alarmed && verbose)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf("warning: alarm synchronization problem");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
if (verbose && printed) {
|
1993-08-14 10:29:28 +04:00
|
|
|
if (alarmed)
|
1998-05-02 18:55:19 +04:00
|
|
|
logf(" -- read timed out");
|
1993-08-14 10:29:28 +04:00
|
|
|
else
|
1998-05-02 18:55:19 +04:00
|
|
|
logf(" -- read failed: %m");
|
|
|
|
}
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
exit_code = 3;
|
|
|
|
alarmed = 0;
|
1993-08-14 10:29:28 +04:00
|
|
|
return (0);
|
1998-05-02 18:55:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gross kludge to handle Solaris versions >= 2.6 having usleep.
|
|
|
|
*/
|
|
|
|
#ifdef SOL2
|
|
|
|
#include <sys/param.h>
|
|
|
|
#if MAXUID > 65536 /* then this is Solaris 2.6 or later */
|
|
|
|
#undef NO_USLEEP
|
|
|
|
#endif
|
|
|
|
#endif /* SOL2 */
|
1993-08-14 10:29:28 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
#ifdef NO_USLEEP
|
1994-05-30 05:38:57 +04: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 */
|
|
|
|
{
|
1998-05-02 18:55:19 +04:00
|
|
|
static struct { /* `timeval' */
|
1995-07-05 03:36:19 +04:00
|
|
|
long tv_sec; /* seconds */
|
|
|
|
long tv_usec; /* microsecs */
|
1998-05-02 18:55:19 +04:00
|
|
|
} delay; /* _select() timeout */
|
1994-05-30 05:38:57 +04:00
|
|
|
|
1995-07-05 03:36:19 +04:00
|
|
|
delay.tv_sec = usec / 1000000L;
|
1994-05-30 05:38:57 +04:00
|
|
|
delay.tv_usec = usec % 1000000L;
|
|
|
|
|
1998-05-02 18:55:19 +04:00
|
|
|
return select(0, (long *)0, (long *)0, (long *)0, &delay);
|
1994-05-30 05:38:57 +04:00
|
|
|
}
|
|
|
|
#endif
|
1997-09-26 23:52:08 +04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-05-02 18:55:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
|
|
|
|
|
|
|
|
int
|
|
|
|
vfmtmsg(buf, buflen, fmt, args)
|
|
|
|
char *buf;
|
|
|
|
int buflen;
|
|
|
|
const char *fmt;
|
|
|
|
va_list args;
|
|
|
|
{
|
|
|
|
int c, i, n;
|
|
|
|
int width, prec, fillch;
|
|
|
|
int base, len, neg, quoted;
|
|
|
|
unsigned long val = 0;
|
1998-09-03 00:55:55 +04:00
|
|
|
char *str, *buf0;
|
|
|
|
const char *f;
|
1998-05-02 18:55:19 +04:00
|
|
|
unsigned char *p;
|
|
|
|
char num[32];
|
1998-09-03 00:55:55 +04:00
|
|
|
static char hexchars[] = "0123456789abcdef";
|
1998-05-02 18:55:19 +04:00
|
|
|
|
|
|
|
buf0 = buf;
|
|
|
|
--buflen;
|
|
|
|
while (buflen > 0) {
|
|
|
|
for (f = fmt; *f != '%' && *f != 0; ++f)
|
|
|
|
;
|
|
|
|
if (f > fmt) {
|
|
|
|
len = f - fmt;
|
|
|
|
if (len > buflen)
|
|
|
|
len = buflen;
|
|
|
|
memcpy(buf, fmt, len);
|
|
|
|
buf += len;
|
|
|
|
buflen -= len;
|
|
|
|
fmt = f;
|
|
|
|
}
|
|
|
|
if (*fmt == 0)
|
|
|
|
break;
|
|
|
|
c = *++fmt;
|
|
|
|
width = prec = 0;
|
|
|
|
fillch = ' ';
|
|
|
|
if (c == '0') {
|
|
|
|
fillch = '0';
|
|
|
|
c = *++fmt;
|
|
|
|
}
|
|
|
|
if (c == '*') {
|
|
|
|
width = va_arg(args, int);
|
|
|
|
c = *++fmt;
|
|
|
|
} else {
|
|
|
|
while (isdigit(c)) {
|
|
|
|
width = width * 10 + c - '0';
|
|
|
|
c = *++fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '.') {
|
|
|
|
c = *++fmt;
|
|
|
|
if (c == '*') {
|
|
|
|
prec = va_arg(args, int);
|
|
|
|
c = *++fmt;
|
|
|
|
} else {
|
|
|
|
while (isdigit(c)) {
|
|
|
|
prec = prec * 10 + c - '0';
|
|
|
|
c = *++fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str = 0;
|
|
|
|
base = 0;
|
|
|
|
neg = 0;
|
|
|
|
++fmt;
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
i = va_arg(args, int);
|
|
|
|
if (i < 0) {
|
|
|
|
neg = 1;
|
|
|
|
val = -i;
|
|
|
|
} else
|
|
|
|
val = i;
|
|
|
|
base = 10;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
val = va_arg(args, unsigned int);
|
|
|
|
base = 8;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
val = va_arg(args, unsigned int);
|
|
|
|
base = 16;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
val = (unsigned long) va_arg(args, void *);
|
|
|
|
base = 16;
|
|
|
|
neg = 2;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
str = va_arg(args, char *);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
num[0] = va_arg(args, int);
|
|
|
|
num[1] = 0;
|
|
|
|
str = num;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
str = strerror(errno);
|
|
|
|
break;
|
|
|
|
case 'v': /* "visible" string */
|
|
|
|
case 'q': /* quoted string */
|
|
|
|
quoted = c == 'q';
|
|
|
|
p = va_arg(args, unsigned char *);
|
|
|
|
if (fillch == '0' && prec > 0) {
|
|
|
|
n = prec;
|
|
|
|
} else {
|
|
|
|
n = strlen((char *)p);
|
|
|
|
if (prec > 0 && prec < n)
|
|
|
|
n = prec;
|
|
|
|
}
|
|
|
|
while (n > 0 && buflen > 0) {
|
|
|
|
c = *p++;
|
|
|
|
--n;
|
|
|
|
if (!quoted && c >= 0x80) {
|
|
|
|
OUTCHAR('M');
|
|
|
|
OUTCHAR('-');
|
|
|
|
c -= 0x80;
|
|
|
|
}
|
|
|
|
if (quoted && (c == '"' || c == '\\'))
|
|
|
|
OUTCHAR('\\');
|
|
|
|
if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
|
|
|
|
if (quoted) {
|
|
|
|
OUTCHAR('\\');
|
|
|
|
switch (c) {
|
|
|
|
case '\t': OUTCHAR('t'); break;
|
|
|
|
case '\n': OUTCHAR('n'); break;
|
|
|
|
case '\b': OUTCHAR('b'); break;
|
|
|
|
case '\f': OUTCHAR('f'); break;
|
|
|
|
default:
|
|
|
|
OUTCHAR('x');
|
|
|
|
OUTCHAR(hexchars[c >> 4]);
|
|
|
|
OUTCHAR(hexchars[c & 0xf]);
|
|
|
|
}
|
|
|
|
} 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) {
|
1998-09-03 00:55:55 +04:00
|
|
|
str = num + sizeof(num);
|
|
|
|
*--str = 0;
|
|
|
|
while (str > num + neg) {
|
|
|
|
*--str = hexchars[val % base];
|
1998-05-02 18:55:19 +04:00
|
|
|
val = val / base;
|
|
|
|
if (--prec <= 0 && val == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (neg) {
|
|
|
|
case 1:
|
1998-09-03 00:55:55 +04:00
|
|
|
*--str = '-';
|
1998-05-02 18:55:19 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
1998-09-03 00:55:55 +04:00
|
|
|
*--str = 'x';
|
|
|
|
*--str = '0';
|
1998-05-02 18:55:19 +04:00
|
|
|
break;
|
|
|
|
}
|
1998-09-03 00:55:55 +04:00
|
|
|
len = num + sizeof(num) - 1 - str;
|
1998-05-02 18:55:19 +04:00
|
|
|
} else {
|
|
|
|
len = strlen(str);
|
|
|
|
if (prec > 0 && len > prec)
|
|
|
|
len = prec;
|
|
|
|
}
|
|
|
|
if (width > 0) {
|
|
|
|
if (width > buflen)
|
|
|
|
width = buflen;
|
|
|
|
if ((n = width - len) > 0) {
|
|
|
|
buflen -= n;
|
|
|
|
for (; n > 0; --n)
|
|
|
|
*buf++ = fillch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len > buflen)
|
|
|
|
len = buflen;
|
|
|
|
memcpy(buf, str, len);
|
|
|
|
buf += len;
|
|
|
|
buflen -= len;
|
|
|
|
}
|
|
|
|
*buf = 0;
|
|
|
|
return buf - buf0;
|
|
|
|
}
|