1 /* vi: set sw=4 ts=4: */
3 * bare bones chat utility
4 * inspired by ppp's chat
6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
12 // default timeout: 45 sec
13 #define DEFAULT_CHAT_TIMEOUT 45*1000
14 // max length of "abort string",
15 // i.e. device reply which causes termination
16 #define MAX_ABORT_LEN 50
18 // possible exit codes
20 ERR_OK = 0, // all's well
21 ERR_MEM, // read too much while expecting
22 ERR_IO, // signalled or I/O error
23 ERR_TIMEOUT, // timed out while expecting
24 ERR_ABORT, // first abort condition was met
25 // ERR_ABORT2, // second abort condition was met
30 #define exitcode bb_got_signal
32 // trap for critical signals
33 static void signal_handler(UNUSED_PARAM int signo)
35 // report I/O error condition
39 #if !ENABLE_FEATURE_CHAT_IMPLICIT_CR
40 #define unescape(s, nocr) unescape(s)
42 static size_t unescape(char *s, int *nocr)
49 // do we need special processing?
50 // standard escapes + \s for space and \N for \0
51 // \c inhibits terminating \r for commands and is noop for expects
55 #if ENABLE_FEATURE_CHAT_IMPLICIT_CR
63 } else if ('s' == c) {
65 #if ENABLE_FEATURE_CHAT_NOFAIL
66 // unescape leading dash only
67 // TODO: and only for expect, not command string
68 } else if ('-' == c && (start + 1 == s)) {
72 c = bb_process_escape_sequence((const char **)&s);
76 // ^A becomes \001, ^B -- \002 and so on...
77 } else if ('^' == c) {
82 #if ENABLE_FEATURE_CHAT_IMPLICIT_CR
93 int chat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
94 int chat_main(int argc UNUSED_PARAM, char **argv)
98 // collection of device replies which cause unconditional termination
99 llist_t *aborts = NULL;
101 int timeout = DEFAULT_CHAT_TIMEOUT;
102 // maximum length of abort string
103 #if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
104 size_t max_abort_len = 0;
106 #define max_abort_len MAX_ABORT_LEN
108 #if ENABLE_FEATURE_CHAT_TTY_HIFI
109 struct termios tio0, tio;
115 #if ENABLE_FEATURE_CHAT_CLR_ABORT
124 // make x* functions fail with correct exitcode
125 xfunc_error_retval = ERR_IO;
127 // trap vanilla signals to prevent process from being killed suddenly
135 #if ENABLE_FEATURE_CHAT_TTY_HIFI
136 tcgetattr(STDIN_FILENO, &tio);
139 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
142 #if ENABLE_FEATURE_CHAT_SWALLOW_OPTS
143 getopt32(argv, "vVsSE");
146 argv++; // goto first arg
148 // handle chat expect-send pairs
150 // directive given? process it
151 int key = index_in_strings(
153 #if ENABLE_FEATURE_CHAT_CLR_ABORT
156 "TIMEOUT\0" "ECHO\0" "SAY\0" "RECORD\0"
160 // cache directive value
162 // OFF -> 0, anything else -> 1
163 bool onoff = (0 != strcmp("OFF", arg));
165 if (DIR_HANGUP == key) {
166 // turn SIGHUP on/off
167 signal(SIGHUP, onoff ? signal_handler : SIG_IGN);
168 } else if (DIR_ABORT == key) {
169 // append the string to abort conditions
170 #if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
171 size_t len = strlen(arg);
172 if (len > max_abort_len)
175 llist_add_to_end(&aborts, arg);
176 #if ENABLE_FEATURE_CHAT_CLR_ABORT
177 } else if (DIR_CLR_ABORT == key) {
179 // remove the string from abort conditions
180 // N.B. gotta refresh maximum length too...
181 # if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
184 for (l = aborts; l; l = l->link) {
185 # if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
186 size_t len = strlen(l->data);
188 if (strcmp(arg, l->data) == 0) {
189 llist_unlink(&aborts, l);
192 # if ENABLE_FEATURE_CHAT_VAR_ABORT_LEN
193 if (len > max_abort_len)
198 } else if (DIR_TIMEOUT == key) {
201 timeout = atoi(arg) * 1000;
203 // >0 means value in msecs
205 timeout = DEFAULT_CHAT_TIMEOUT;
206 } else if (DIR_ECHO == key) {
208 // N.B. echo means dumping device input/output to stderr
210 } else if (DIR_RECORD == key) {
211 // turn record on/off
212 // N.B. record means dumping device input to a file
213 // close previous record_fd
216 // N.B. do we have to die here on open error?
217 record_fd = (onoff) ? xopen(arg, O_WRONLY|O_CREAT|O_TRUNC) : -1;
218 } else if (DIR_SAY == key) {
219 // just print argument verbatim
220 // TODO: should we use full_write() to avoid unistd/stdio conflict?
221 bb_error_msg("%s", arg);
225 // ordinary expect-send pair!
227 //-----------------------
229 //-----------------------
232 size_t max_len = max_abort_len;
235 #if ENABLE_FEATURE_CHAT_NOFAIL
238 char *expect = *argv++;
240 // sanity check: shall we really expect something?
244 #if ENABLE_FEATURE_CHAT_NOFAIL
245 // if expect starts with -
246 if ('-' == *expect) {
249 // and enter nofail mode
254 #ifdef ___TEST___BUF___ // test behaviour with a small buffer
255 # undef COMMON_BUFSIZE
256 # define COMMON_BUFSIZE 6
258 // expand escape sequences in expect
259 expect_len = unescape(expect, &expect_len /*dummy*/);
260 if (expect_len > max_len)
261 max_len = expect_len;
263 // we should expect more than nothing but not more than input buffer
264 // TODO: later we'll get rid of fixed-size buffer
267 if (max_len >= COMMON_BUFSIZE) {
273 pfd.fd = STDIN_FILENO;
276 && poll(&pfd, 1, timeout) > 0
277 && (pfd.revents & POLLIN)
279 #define buf bb_common_bufsiz1
283 // read next char from device
284 if (safe_read(STDIN_FILENO, buf+buf_len, 1) > 0) {
285 // dump device input if RECORD fname
287 full_write(record_fd, buf+buf_len, 1);
289 // dump device input if ECHO ON
291 // if (buf[buf_len] < ' ') {
292 // full_write(STDERR_FILENO, "^", 1);
293 // buf[buf_len] += '@';
295 full_write(STDERR_FILENO, buf+buf_len, 1);
298 // move input frame if we've reached higher bound
299 if (buf_len > COMMON_BUFSIZE) {
300 memmove(buf, buf+buf_len-max_len, max_len);
304 // N.B. rule of thumb: values being looked for can
305 // be found only at the end of input buffer
306 // this allows to get rid of strstr() and memmem()
308 // TODO: make expect and abort strings processed uniformly
309 // abort condition is met? -> bail out
310 for (l = aborts, exitcode = ERR_ABORT; l; l = l->link, ++exitcode) {
311 size_t len = strlen(l->data);
313 if (delta >= 0 && !memcmp(buf+delta, l->data, len))
318 // expected reply received? -> goto next command
319 delta = buf_len - expect_len;
320 if (delta >= 0 && !memcmp(buf+delta, expect, expect_len))
323 } /* while (have data) */
325 // device timed out or unexpected reply received
326 exitcode = ERR_TIMEOUT;
328 #if ENABLE_FEATURE_CHAT_NOFAIL
329 // on success and when in nofail mode
330 // we should skip following subsend-subexpect pairs
333 // find last send before non-dashed expect
334 while (*argv && argv[1] && '-' == argv[1][0])
337 // N.B. do we really need this?!
338 if (!*argv++ || !*argv++)
341 // nofail mode also clears all but IO errors (or signals)
342 if (ERR_IO != exitcode)
346 // bail out unless we expected successfully
350 //-----------------------
352 //-----------------------
354 #if ENABLE_FEATURE_CHAT_IMPLICIT_CR
355 int nocr = 0; // inhibit terminating command with \r
357 char *loaded = NULL; // loaded command
361 // if command starts with @
362 // load "real" command from file named after @
364 // skip the @ and any following white-space
366 buf = loaded = xmalloc_xopen_read_close(buf, NULL);
368 // expand escape sequences in command
369 len = unescape(buf, &nocr);
373 pfd.fd = STDOUT_FILENO;
374 pfd.events = POLLOUT;
375 while (len && !exitcode
376 && poll(&pfd, 1, -1) > 0
377 && (pfd.revents & POLLOUT)
379 #if ENABLE_FEATURE_CHAT_SEND_ESCAPES
380 // "\\d" means 1 sec delay, "\\p" means 0.01 sec delay
381 // "\\K" means send BREAK
396 tcsendbreak(STDOUT_FILENO, 0);
402 if (safe_write(STDOUT_FILENO, buf, 1) != 1)
407 len -= full_write(STDOUT_FILENO, buf, len);
409 } /* while (can write) */
412 // report I/O error if there still exists at least one non-sent char
416 // free loaded command (if any)
419 #if ENABLE_FEATURE_CHAT_IMPLICIT_CR
420 // or terminate command with \r (if not inhibited)
422 xwrite(STDOUT_FILENO, "\r", 1);
424 // bail out unless we sent command successfully
429 } /* while (*argv) */
431 #if ENABLE_FEATURE_CHAT_TTY_HIFI
432 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);