3 * David L Nugent <davidn@blaze.net.au>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, is permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. This work was done expressly for inclusion into FreeBSD. Other use
17 * is permitted provided this notation is included.
18 * 4. Absolutely no warranty of function or purpose is made by the authors.
19 * 5. Modifications may be freely made to this file providing the above
22 * Modem chat module - send/expect style functions for getty
23 * For semi-intelligent modem handling.
27 static const char rcsid[] =
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/utsname.h>
45 #define PAUSE_CH (unsigned char)'\xff' /* pause kludge */
47 #define CHATDEBUG_RECEIVE 0x01
48 #define CHATDEBUG_SEND 0x02
49 #define CHATDEBUG_EXPECT 0x04
50 #define CHATDEBUG_MISC 0x08
52 #define CHATDEBUG_DEFAULT 0
53 #define CHAT_DEFAULT_TIMEOUT 10
56 static int chat_debug = CHATDEBUG_DEFAULT;
57 static int chat_alarm = CHAT_DEFAULT_TIMEOUT; /* Default */
59 static volatile int alarmed = 0;
62 static void chat_alrm(int);
63 static int chat_unalarm(void);
64 static int getdigit(unsigned char **, int, int);
65 static char **read_chat(char **);
66 static char *cleanchr(char **, unsigned char);
67 static char *cleanstr(const unsigned char *, int);
68 static const char *result(int);
69 static int chat_expect(const char *);
70 static int chat_send(char const *);
74 * alarm signal handler
75 * handle timeouts in read/write
76 * change stdin to non-blocking mode to prevent
77 * possible hang in read().
87 signal(SIGALRM, chat_alrm);
88 ioctl(STDIN_FILENO, FIONBIO, &on);
93 * Turn back on blocking mode reset by chat_alrm()
100 return ioctl(STDIN_FILENO, FIONBIO, &off);
105 * convert a string of a given base (octal/hex) to binary
109 getdigit(unsigned char **ptr, int base, int max)
114 static const char xdigits[] = "0123456789abcdef";
116 for (i = 0, q = *ptr; i++ < max; ++q) {
118 const char * s = strchr(xdigits, tolower(*q));
120 if (s == NULL || (sval = s - xdigits) >= base)
122 val = (val * base) + sval;
131 * Convert a whitespace delimtied string into an array
132 * of strings, being expect/send pairs
136 read_chat(char **chatstr)
138 char *str = *chatstr;
145 if ((l=strlen(str)) > 0 && (tmp=malloc(l + 1)) != NULL &&
146 (res=malloc((l / 2 + 1) * sizeof(char *))) != NULL) {
147 static char ws[] = " \t";
150 for (l = 0, p = strtok(strcpy(tmp, str), ws);
152 p = strtok(NULL, ws))
154 unsigned char *q, *r;
157 for (q = r = (unsigned char *)p; *r; ++q)
161 /* handle special escapes */
185 case 'p': /* pause */
189 case 'S': /* space */
192 case 'x': /* hexdigit */
194 *r++ = getdigit(&q, 16, 2);
197 case '0': /* octal */
199 *r++ = getdigit(&q, 8, 3);
202 default: /* literal */
205 case 0: /* not past eos */
210 /* copy standard character */
215 /* Remove surrounding quotes, if any
217 if (*p == '"' || *p == '\'') {
218 q = strrchr(p+1, *p);
219 if (q != NULL && *q == *p && q[1] == '\0') {
238 * clean a character for display (ctrl/meta character)
242 cleanchr(char **buf, unsigned char ch)
245 static char tmpbuf[5];
246 char * tmp = buf ? *buf : tmpbuf;
258 } else if (ch == 127) {
272 * clean a string for display (ctrl/meta characters)
276 cleanstr(const unsigned char *s, int l)
278 static unsigned char * tmp = NULL;
279 static int tmplen = 0;
281 if (tmplen < l * 4 + 1)
282 tmp = realloc(tmp, tmplen = l * 4 + 1);
286 return (char *)"(mem alloc error)";
292 cleanchr(&p, s[i++]);
301 * return result as a pseudo-english word
307 static const char * results[] = {
308 "OK", "MEMERROR", "IOERROR", "TIMEOUT"
310 return results[r & 3];
316 * scan input for an expected string
320 chat_expect(const char *str)
324 if (chat_debug & CHATDEBUG_EXPECT)
325 syslog(LOG_DEBUG, "chat_expect '%s'", cleanstr(str, strlen(str)));
327 if ((len = strlen(str)) > 0) {
331 if ((got = malloc(len + 1)) == NULL)
335 memset(got, 0, len+1);
339 while (r == 0 && i < len) {
345 if (read(STDIN_FILENO, &ch, 1) == 1) {
347 if (chat_debug & CHATDEBUG_RECEIVE)
348 syslog(LOG_DEBUG, "chat_recv '%s' m=%d",
349 cleanchr(NULL, ch), i);
356 /* See if we can resync on a
357 * partial match in our buffer
359 while (j < i && memcmp(got + j, str, i - j) != 0)
362 memcpy(got, got + j, i - j);
376 if (chat_debug & CHATDEBUG_EXPECT)
377 syslog(LOG_DEBUG, "chat_expect %s", result(r));
389 chat_send(char const *str)
393 if (chat_debug && CHATDEBUG_SEND)
394 syslog(LOG_DEBUG, "chat_send '%s'", cleanstr(str, strlen(str)));
399 while (r == 0 && *str)
401 unsigned char ch = (unsigned char)*str++;
405 else if (ch == PAUSE_CH)
406 usleep(500000); /* 1/2 second */
408 usleep(10000); /* be kind to modem */
409 if (write(STDOUT_FILENO, &ch, 1) != 1)
418 if (chat_debug & CHATDEBUG_SEND)
419 syslog(LOG_DEBUG, "chat_send %s", result(r));
429 * -1 - no script supplied
430 * 0 - script terminated correctly
431 * 1 - invalid argument, expect string too large, etc.
432 * 2 - error on an I/O operation or fatal error condition
433 * 3 - timeout waiting for a simple string
436 * char *scrstr - unparsed chat script
437 * timeout - seconds timeout
438 * debug - debug value (bitmask)
442 getty_chat(char *scrstr, int timeout, int debug)
446 chat_alarm = timeout ? timeout : CHAT_DEFAULT_TIMEOUT;
449 if (scrstr != NULL) {
452 if (chat_debug & CHATDEBUG_MISC)
453 syslog(LOG_DEBUG, "getty_chat script='%s'", scrstr);
455 if ((script = read_chat(&scrstr)) != NULL) {
461 * We need to be in raw mode for all this
465 old_alarm = signal(SIGALRM, chat_alrm);
466 chat_unalarm(); /* Force blocking mode at start */
469 * This is the send/expect loop
471 while (r == 0 && script[i] != NULL)
472 if ((r = chat_expect(script[i++])) == 0 && script[i] != NULL)
473 r = chat_send(script[i++]);
475 signal(SIGALRM, old_alarm);
480 * Ensure stdin is in blocking mode
482 ioctl(STDIN_FILENO, FIONBIO, &off);
485 if (chat_debug & CHATDEBUG_MISC)
486 syslog(LOG_DEBUG, "getty_chat %s", result(r));