2008-02-19 17:05:08 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* bare bones chat utility
|
|
|
|
* inspired by ppp's chat
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-02-19 17:05:08 +05:30
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config CHAT
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "chat (6.6 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Simple chat utility.
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_NOFAIL
|
|
|
|
//config: bool "Enable NOFAIL expect strings"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: When enabled expect strings which are started with a dash trigger
|
|
|
|
//config: no-fail mode. That is when expectation is not met within timeout
|
|
|
|
//config: the script is not terminated but sends next SEND string and waits
|
|
|
|
//config: for next EXPECT string. This allows to compose far more flexible
|
|
|
|
//config: scripts.
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_TTY_HIFI
|
|
|
|
//config: bool "Force STDIN to be a TTY"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default n
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Original chat always treats STDIN as a TTY device and sets for it
|
|
|
|
//config: so-called raw mode. This option turns on such behaviour.
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_IMPLICIT_CR
|
|
|
|
//config: bool "Enable implicit Carriage Return"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: When enabled make chat to terminate all SEND strings with a "\r"
|
|
|
|
//config: unless "\c" is met anywhere in the string.
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_SWALLOW_OPTS
|
|
|
|
//config: bool "Swallow options"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Busybox chat require no options. To make it not fail when used
|
|
|
|
//config: in place of original chat (which has a bunch of options) turn
|
|
|
|
//config: this on.
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_SEND_ESCAPES
|
|
|
|
//config: bool "Support weird SEND escapes"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Original chat uses some escape sequences in SEND arguments which
|
|
|
|
//config: are not sent to device but rather performs special actions.
|
|
|
|
//config: E.g. "\K" means to send a break sequence to device.
|
|
|
|
//config: "\d" delays execution for a second, "\p" -- for a 1/100 of second.
|
|
|
|
//config: Before turning this option on think twice: do you really need them?
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_VAR_ABORT_LEN
|
|
|
|
//config: bool "Support variable-length ABORT conditions"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Original chat uses fixed 50-bytes length ABORT conditions. Say N here.
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CHAT_CLR_ABORT
|
|
|
|
//config: bool "Support revoking of ABORT conditions"
|
|
|
|
//config: depends on CHAT
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Support CLR_ABORT directive.
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_CHAT(APPLET(chat, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CHAT) += chat.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define chat_trivial_usage
|
|
|
|
//usage: "EXPECT [SEND [EXPECT [SEND...]]]"
|
|
|
|
//usage:#define chat_full_usage "\n\n"
|
|
|
|
//usage: "Useful for interacting with a modem connected to stdin/stdout.\n"
|
|
|
|
//usage: "A script consists of one or more \"expect-send\" pairs of strings,\n"
|
|
|
|
//usage: "each pair is a pair of arguments. Example:\n"
|
|
|
|
//usage: "chat '' ATZ OK ATD123456 CONNECT '' ogin: pppuser word: ppppass '~'"
|
|
|
|
|
2008-02-19 17:05:08 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2008-02-19 17:05:08 +05:30
|
|
|
|
|
|
|
// default timeout: 45 sec
|
2010-10-28 22:27:19 +05:30
|
|
|
#define DEFAULT_CHAT_TIMEOUT 45*1000
|
2008-02-19 17:05:08 +05:30
|
|
|
// max length of "abort string",
|
|
|
|
// i.e. device reply which causes termination
|
|
|
|
#define MAX_ABORT_LEN 50
|
|
|
|
|
|
|
|
// possible exit codes
|
|
|
|
enum {
|
|
|
|
ERR_OK = 0, // all's well
|
|
|
|
ERR_MEM, // read too much while expecting
|
|
|
|
ERR_IO, // signalled or I/O error
|
|
|
|
ERR_TIMEOUT, // timed out while expecting
|
|
|
|
ERR_ABORT, // first abort condition was met
|
|
|
|
// ERR_ABORT2, // second abort condition was met
|
|
|
|
// ...
|
|
|
|
};
|
|
|
|
|
|
|
|
// exit code
|
2009-03-17 04:36:23 +05:30
|
|
|
#define exitcode bb_got_signal
|
2008-02-19 17:05:08 +05:30
|
|
|
|
|
|
|
// trap for critical signals
|
2008-07-05 14:48:54 +05:30
|
|
|
static void signal_handler(UNUSED_PARAM int signo)
|
2008-02-19 17:05:08 +05:30
|
|
|
{
|
|
|
|
// report I/O error condition
|
|
|
|
exitcode = ERR_IO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !ENABLE_FEATURE_CHAT_IMPLICIT_CR
|
|
|
|
#define unescape(s, nocr) unescape(s)
|
|
|
|
#endif
|
|
|
|
static size_t unescape(char *s, int *nocr)
|
|
|
|
{
|
|
|
|
char *start = s;
|
|
|
|
char *p = s;
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
char c = *s;
|
|
|
|
// do we need special processing?
|
|
|
|
// standard escapes + \s for space and \N for \0
|
|
|
|
// \c inhibits terminating \r for commands and is noop for expects
|
|
|
|
if ('\\' == c) {
|
|
|
|
c = *++s;
|
|
|
|
if (c) {
|
|
|
|
#if ENABLE_FEATURE_CHAT_IMPLICIT_CR
|
|
|
|
if ('c' == c) {
|
|
|
|
*nocr = 1;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if ('N' == c) {
|
|
|
|
c = '\0';
|
|
|
|
} else if ('s' == c) {
|
|
|
|
c = ' ';
|
|
|
|
#if ENABLE_FEATURE_CHAT_NOFAIL
|
|
|
|
// unescape leading dash only
|
|
|
|
// TODO: and only for expect, not command string
|
|
|
|
} else if ('-' == c && (start + 1 == s)) {
|
|
|
|
//c = '-';
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
c = bb_process_escape_sequence((const char **)&s);
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ^A becomes \001, ^B -- \002 and so on...
|
|
|
|
} else if ('^' == c) {
|
|
|
|
c = *++s-'@';
|
|
|
|
}
|
|
|
|
// put unescaped char
|
|
|
|
*p++ = c;
|
|
|
|
#if ENABLE_FEATURE_CHAT_IMPLICIT_CR
|
|
|
|
next:
|
|
|
|
#endif
|
|
|
|
// next char
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return p - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int chat_main(int argc UNUSED_PARAM, char **argv)
|
2008-02-19 17:05:08 +05:30
|
|
|
{
|
2009-03-17 04:36:23 +05:30
|
|
|
int record_fd = -1;
|
2008-02-19 17:05:08 +05:30
|
|
|
bool echo = 0;
|
|
|
|
// collection of device replies which cause unconditional termination
|
|
|
|
llist_t *aborts = NULL;
|
|
|
|
// inactivity period
|
|
|
|
int timeout = DEFAULT_CHAT_TIMEOUT;
|
|
|
|
// maximum length of abort string
|
|
|
|
#if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
|
|
|
|
size_t max_abort_len = 0;
|
|
|
|
#else
|
|
|
|
#define max_abort_len MAX_ABORT_LEN
|
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_CHAT_TTY_HIFI
|
|
|
|
struct termios tio0, tio;
|
|
|
|
#endif
|
|
|
|
// directive names
|
|
|
|
enum {
|
|
|
|
DIR_HANGUP = 0,
|
|
|
|
DIR_ABORT,
|
|
|
|
#if ENABLE_FEATURE_CHAT_CLR_ABORT
|
|
|
|
DIR_CLR_ABORT,
|
|
|
|
#endif
|
|
|
|
DIR_TIMEOUT,
|
|
|
|
DIR_ECHO,
|
|
|
|
DIR_SAY,
|
2009-03-17 04:36:23 +05:30
|
|
|
DIR_RECORD,
|
2008-02-19 17:05:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// make x* functions fail with correct exitcode
|
|
|
|
xfunc_error_retval = ERR_IO;
|
|
|
|
|
|
|
|
// trap vanilla signals to prevent process from being killed suddenly
|
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGHUP)
|
|
|
|
+ (1 << SIGINT)
|
|
|
|
+ (1 << SIGTERM)
|
|
|
|
+ (1 << SIGPIPE)
|
|
|
|
, signal_handler);
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_CHAT_TTY_HIFI
|
2017-01-11 20:47:59 +05:30
|
|
|
//TODO: use set_termios_to_raw()
|
2008-02-19 17:05:08 +05:30
|
|
|
tcgetattr(STDIN_FILENO, &tio);
|
|
|
|
tio0 = tio;
|
|
|
|
cfmakeraw(&tio);
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_CHAT_SWALLOW_OPTS
|
|
|
|
getopt32(argv, "vVsSE");
|
|
|
|
argv += optind;
|
|
|
|
#else
|
|
|
|
argv++; // goto first arg
|
|
|
|
#endif
|
|
|
|
// handle chat expect-send pairs
|
|
|
|
while (*argv) {
|
|
|
|
// directive given? process it
|
|
|
|
int key = index_in_strings(
|
|
|
|
"HANGUP\0" "ABORT\0"
|
|
|
|
#if ENABLE_FEATURE_CHAT_CLR_ABORT
|
|
|
|
"CLR_ABORT\0"
|
|
|
|
#endif
|
2009-03-17 04:36:23 +05:30
|
|
|
"TIMEOUT\0" "ECHO\0" "SAY\0" "RECORD\0"
|
2008-02-19 17:05:08 +05:30
|
|
|
, *argv
|
|
|
|
);
|
|
|
|
if (key >= 0) {
|
|
|
|
// cache directive value
|
|
|
|
char *arg = *++argv;
|
2009-03-17 04:36:23 +05:30
|
|
|
// OFF -> 0, anything else -> 1
|
|
|
|
bool onoff = (0 != strcmp("OFF", arg));
|
2008-02-19 17:05:08 +05:30
|
|
|
// process directive
|
|
|
|
if (DIR_HANGUP == key) {
|
|
|
|
// turn SIGHUP on/off
|
|
|
|
signal(SIGHUP, onoff ? signal_handler : SIG_IGN);
|
|
|
|
} else if (DIR_ABORT == key) {
|
|
|
|
// append the string to abort conditions
|
|
|
|
#if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
|
|
|
|
size_t len = strlen(arg);
|
|
|
|
if (len > max_abort_len)
|
|
|
|
max_abort_len = len;
|
|
|
|
#endif
|
|
|
|
llist_add_to_end(&aborts, arg);
|
|
|
|
#if ENABLE_FEATURE_CHAT_CLR_ABORT
|
|
|
|
} else if (DIR_CLR_ABORT == key) {
|
2010-12-18 06:12:50 +05:30
|
|
|
llist_t *l;
|
2008-02-19 17:05:08 +05:30
|
|
|
// remove the string from abort conditions
|
|
|
|
// N.B. gotta refresh maximum length too...
|
2010-12-18 06:12:50 +05:30
|
|
|
# if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
|
2008-02-19 17:05:08 +05:30
|
|
|
max_abort_len = 0;
|
2010-12-18 06:12:50 +05:30
|
|
|
# endif
|
|
|
|
for (l = aborts; l; l = l->link) {
|
|
|
|
# if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
|
2008-02-19 17:05:08 +05:30
|
|
|
size_t len = strlen(l->data);
|
2010-12-18 06:12:50 +05:30
|
|
|
# endif
|
|
|
|
if (strcmp(arg, l->data) == 0) {
|
2008-02-19 17:05:08 +05:30
|
|
|
llist_unlink(&aborts, l);
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-18 06:12:50 +05:30
|
|
|
# if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
|
2008-02-19 17:05:08 +05:30
|
|
|
if (len > max_abort_len)
|
|
|
|
max_abort_len = len;
|
2010-12-18 06:12:50 +05:30
|
|
|
# endif
|
2008-02-19 17:05:08 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if (DIR_TIMEOUT == key) {
|
|
|
|
// set new timeout
|
|
|
|
// -1 means OFF
|
|
|
|
timeout = atoi(arg) * 1000;
|
|
|
|
// 0 means default
|
|
|
|
// >0 means value in msecs
|
|
|
|
if (!timeout)
|
|
|
|
timeout = DEFAULT_CHAT_TIMEOUT;
|
|
|
|
} else if (DIR_ECHO == key) {
|
|
|
|
// turn echo on/off
|
2009-03-17 04:36:23 +05:30
|
|
|
// N.B. echo means dumping device input/output to stderr
|
2008-02-19 17:05:08 +05:30
|
|
|
echo = onoff;
|
2009-03-17 04:36:23 +05:30
|
|
|
} else if (DIR_RECORD == key) {
|
|
|
|
// turn record on/off
|
|
|
|
// N.B. record means dumping device input to a file
|
|
|
|
// close previous record_fd
|
|
|
|
if (record_fd > 0)
|
|
|
|
close(record_fd);
|
|
|
|
// N.B. do we have to die here on open error?
|
|
|
|
record_fd = (onoff) ? xopen(arg, O_WRONLY|O_CREAT|O_TRUNC) : -1;
|
2008-02-19 17:05:08 +05:30
|
|
|
} else if (DIR_SAY == key) {
|
|
|
|
// just print argument verbatim
|
2009-03-17 04:36:23 +05:30
|
|
|
// TODO: should we use full_write() to avoid unistd/stdio conflict?
|
|
|
|
bb_error_msg("%s", arg);
|
2008-02-19 17:05:08 +05:30
|
|
|
}
|
|
|
|
// next, please!
|
|
|
|
argv++;
|
|
|
|
// ordinary expect-send pair!
|
|
|
|
} else {
|
|
|
|
//-----------------------
|
|
|
|
// do expect
|
|
|
|
//-----------------------
|
2008-03-17 14:34:04 +05:30
|
|
|
int expect_len;
|
|
|
|
size_t buf_len = 0;
|
2008-02-19 17:05:08 +05:30
|
|
|
size_t max_len = max_abort_len;
|
|
|
|
|
|
|
|
struct pollfd pfd;
|
|
|
|
#if ENABLE_FEATURE_CHAT_NOFAIL
|
|
|
|
int nofail = 0;
|
|
|
|
#endif
|
|
|
|
char *expect = *argv++;
|
|
|
|
|
|
|
|
// sanity check: shall we really expect something?
|
|
|
|
if (!expect)
|
|
|
|
goto expect_done;
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_CHAT_NOFAIL
|
|
|
|
// if expect starts with -
|
|
|
|
if ('-' == *expect) {
|
|
|
|
// swallow -
|
|
|
|
expect++;
|
|
|
|
// and enter nofail mode
|
|
|
|
nofail++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ___TEST___BUF___ // test behaviour with a small buffer
|
|
|
|
# undef COMMON_BUFSIZE
|
|
|
|
# define COMMON_BUFSIZE 6
|
|
|
|
#endif
|
|
|
|
// expand escape sequences in expect
|
|
|
|
expect_len = unescape(expect, &expect_len /*dummy*/);
|
|
|
|
if (expect_len > max_len)
|
|
|
|
max_len = expect_len;
|
|
|
|
// sanity check:
|
|
|
|
// we should expect more than nothing but not more than input buffer
|
|
|
|
// TODO: later we'll get rid of fixed-size buffer
|
|
|
|
if (!expect_len)
|
|
|
|
goto expect_done;
|
|
|
|
if (max_len >= COMMON_BUFSIZE) {
|
|
|
|
exitcode = ERR_MEM;
|
|
|
|
goto expect_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get reply
|
|
|
|
pfd.fd = STDIN_FILENO;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
while (!exitcode
|
|
|
|
&& poll(&pfd, 1, timeout) > 0
|
|
|
|
&& (pfd.revents & POLLIN)
|
|
|
|
) {
|
|
|
|
llist_t *l;
|
|
|
|
ssize_t delta;
|
2016-04-21 22:08:51 +05:30
|
|
|
#define buf bb_common_bufsiz1
|
|
|
|
setup_common_bufsiz();
|
2008-02-19 17:05:08 +05:30
|
|
|
|
|
|
|
// read next char from device
|
|
|
|
if (safe_read(STDIN_FILENO, buf+buf_len, 1) > 0) {
|
2009-03-17 04:36:23 +05:30
|
|
|
// dump device input if RECORD fname
|
|
|
|
if (record_fd > 0) {
|
|
|
|
full_write(record_fd, buf+buf_len, 1);
|
|
|
|
}
|
|
|
|
// dump device input if ECHO ON
|
2013-11-29 21:13:33 +05:30
|
|
|
if (echo) {
|
2009-03-17 04:36:23 +05:30
|
|
|
// if (buf[buf_len] < ' ') {
|
|
|
|
// full_write(STDERR_FILENO, "^", 1);
|
|
|
|
// buf[buf_len] += '@';
|
|
|
|
// }
|
2008-02-19 17:05:08 +05:30
|
|
|
full_write(STDERR_FILENO, buf+buf_len, 1);
|
2009-03-17 04:36:23 +05:30
|
|
|
}
|
2008-02-19 17:05:08 +05:30
|
|
|
buf_len++;
|
|
|
|
// move input frame if we've reached higher bound
|
|
|
|
if (buf_len > COMMON_BUFSIZE) {
|
|
|
|
memmove(buf, buf+buf_len-max_len, max_len);
|
|
|
|
buf_len = max_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// N.B. rule of thumb: values being looked for can
|
|
|
|
// be found only at the end of input buffer
|
|
|
|
// this allows to get rid of strstr() and memmem()
|
|
|
|
|
|
|
|
// TODO: make expect and abort strings processed uniformly
|
|
|
|
// abort condition is met? -> bail out
|
|
|
|
for (l = aborts, exitcode = ERR_ABORT; l; l = l->link, ++exitcode) {
|
|
|
|
size_t len = strlen(l->data);
|
|
|
|
delta = buf_len-len;
|
|
|
|
if (delta >= 0 && !memcmp(buf+delta, l->data, len))
|
|
|
|
goto expect_done;
|
|
|
|
}
|
|
|
|
exitcode = ERR_OK;
|
|
|
|
|
|
|
|
// expected reply received? -> goto next command
|
2008-03-17 14:34:04 +05:30
|
|
|
delta = buf_len - expect_len;
|
2008-02-19 17:05:08 +05:30
|
|
|
if (delta >= 0 && !memcmp(buf+delta, expect, expect_len))
|
|
|
|
goto expect_done;
|
|
|
|
#undef buf
|
2009-03-17 04:36:23 +05:30
|
|
|
} /* while (have data) */
|
2008-02-19 17:05:08 +05:30
|
|
|
|
|
|
|
// device timed out or unexpected reply received
|
|
|
|
exitcode = ERR_TIMEOUT;
|
|
|
|
expect_done:
|
|
|
|
#if ENABLE_FEATURE_CHAT_NOFAIL
|
|
|
|
// on success and when in nofail mode
|
|
|
|
// we should skip following subsend-subexpect pairs
|
|
|
|
if (nofail) {
|
|
|
|
if (!exitcode) {
|
|
|
|
// find last send before non-dashed expect
|
|
|
|
while (*argv && argv[1] && '-' == argv[1][0])
|
|
|
|
argv += 2;
|
|
|
|
// skip the pair
|
|
|
|
// N.B. do we really need this?!
|
|
|
|
if (!*argv++ || !*argv++)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// nofail mode also clears all but IO errors (or signals)
|
|
|
|
if (ERR_IO != exitcode)
|
|
|
|
exitcode = ERR_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// bail out unless we expected successfully
|
|
|
|
if (exitcode)
|
|
|
|
break;
|
|
|
|
|
|
|
|
//-----------------------
|
|
|
|
// do send
|
|
|
|
//-----------------------
|
|
|
|
if (*argv) {
|
|
|
|
#if ENABLE_FEATURE_CHAT_IMPLICIT_CR
|
|
|
|
int nocr = 0; // inhibit terminating command with \r
|
|
|
|
#endif
|
|
|
|
char *loaded = NULL; // loaded command
|
|
|
|
size_t len;
|
|
|
|
char *buf = *argv++;
|
|
|
|
|
|
|
|
// if command starts with @
|
|
|
|
// load "real" command from file named after @
|
|
|
|
if ('@' == *buf) {
|
|
|
|
// skip the @ and any following white-space
|
|
|
|
trim(++buf);
|
2008-04-20 01:02:08 +05:30
|
|
|
buf = loaded = xmalloc_xopen_read_close(buf, NULL);
|
2008-02-19 17:05:08 +05:30
|
|
|
}
|
|
|
|
// expand escape sequences in command
|
|
|
|
len = unescape(buf, &nocr);
|
|
|
|
|
|
|
|
// send command
|
2009-03-17 04:36:23 +05:30
|
|
|
alarm(timeout);
|
2008-02-19 17:05:08 +05:30
|
|
|
pfd.fd = STDOUT_FILENO;
|
|
|
|
pfd.events = POLLOUT;
|
|
|
|
while (len && !exitcode
|
2009-03-17 04:36:23 +05:30
|
|
|
&& poll(&pfd, 1, -1) > 0
|
2008-02-19 17:05:08 +05:30
|
|
|
&& (pfd.revents & POLLOUT)
|
|
|
|
) {
|
2009-03-17 04:36:23 +05:30
|
|
|
#if ENABLE_FEATURE_CHAT_SEND_ESCAPES
|
2008-02-19 17:05:08 +05:30
|
|
|
// "\\d" means 1 sec delay, "\\p" means 0.01 sec delay
|
|
|
|
// "\\K" means send BREAK
|
|
|
|
char c = *buf;
|
|
|
|
if ('\\' == c) {
|
|
|
|
c = *++buf;
|
|
|
|
if ('d' == c) {
|
|
|
|
sleep(1);
|
|
|
|
len--;
|
|
|
|
continue;
|
2009-03-17 04:36:23 +05:30
|
|
|
}
|
|
|
|
if ('p' == c) {
|
2008-02-19 17:05:08 +05:30
|
|
|
usleep(10000);
|
|
|
|
len--;
|
|
|
|
continue;
|
2009-03-17 04:36:23 +05:30
|
|
|
}
|
|
|
|
if ('K' == c) {
|
2008-02-19 17:05:08 +05:30
|
|
|
tcsendbreak(STDOUT_FILENO, 0);
|
|
|
|
len--;
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-17 04:36:23 +05:30
|
|
|
buf--;
|
2008-02-19 17:05:08 +05:30
|
|
|
}
|
2009-03-17 04:36:23 +05:30
|
|
|
if (safe_write(STDOUT_FILENO, buf, 1) != 1)
|
2008-02-19 17:05:08 +05:30
|
|
|
break;
|
2009-03-17 04:36:23 +05:30
|
|
|
len--;
|
|
|
|
buf++;
|
2008-02-19 17:05:08 +05:30
|
|
|
#else
|
|
|
|
len -= full_write(STDOUT_FILENO, buf, len);
|
|
|
|
#endif
|
2009-03-17 04:36:23 +05:30
|
|
|
} /* while (can write) */
|
|
|
|
alarm(0);
|
2008-02-19 17:05:08 +05:30
|
|
|
|
|
|
|
// report I/O error if there still exists at least one non-sent char
|
|
|
|
if (len)
|
|
|
|
exitcode = ERR_IO;
|
|
|
|
|
|
|
|
// free loaded command (if any)
|
|
|
|
if (loaded)
|
|
|
|
free(loaded);
|
|
|
|
#if ENABLE_FEATURE_CHAT_IMPLICIT_CR
|
|
|
|
// or terminate command with \r (if not inhibited)
|
|
|
|
else if (!nocr)
|
|
|
|
xwrite(STDOUT_FILENO, "\r", 1);
|
|
|
|
#endif
|
|
|
|
// bail out unless we sent command successfully
|
|
|
|
if (exitcode)
|
|
|
|
break;
|
2009-03-17 04:36:23 +05:30
|
|
|
} /* if (*argv) */
|
2008-02-19 17:05:08 +05:30
|
|
|
}
|
2009-03-17 04:36:23 +05:30
|
|
|
} /* while (*argv) */
|
2008-02-19 17:05:08 +05:30
|
|
|
|
|
|
|
#if ENABLE_FEATURE_CHAT_TTY_HIFI
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return exitcode;
|
|
|
|
}
|