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.
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
40 #define PAUSE_CH (unsigned char)'\xff' /* pause kludge */
42 #define CHATDEBUG_RECEIVE 0x01
43 #define CHATDEBUG_SEND 0x02
44 #define CHATDEBUG_EXPECT 0x04
45 #define CHATDEBUG_MISC 0x08
47 #define CHATDEBUG_DEFAULT 0
48 #define CHAT_DEFAULT_TIMEOUT 10
51 static int chat_debug = CHATDEBUG_DEFAULT;
52 static int chat_alarm = CHAT_DEFAULT_TIMEOUT; /* Default */
54 static volatile int alarmed = 0;
57 static void chat_alrm(int);
58 static int chat_unalarm(void);
59 static int getdigit(char **, int, int);
60 static char **read_chat(char **);
61 static char *cleanchr(char **, unsigned char);
62 static const char *cleanstr(const char *, int);
63 static const char *result(int);
64 static int chat_expect(const char *);
65 static int chat_send(char const *);
69 * alarm signal handler
70 * handle timeouts in read/write
71 * change stdin to non-blocking mode to prevent
72 * possible hang in read().
76 chat_alrm(int signo __unused)
82 signal(SIGALRM, chat_alrm);
83 ioctl(STDIN_FILENO, FIONBIO, &on);
88 * Turn back on blocking mode reset by chat_alrm()
95 return ioctl(STDIN_FILENO, FIONBIO, &off);
100 * convert a string of a given base (octal/hex) to binary
104 getdigit(char **ptr, int base, int max)
109 static const char xdigits[] = "0123456789abcdef";
111 for (i = 0, q = *ptr; i++ < max; ++q) {
113 const char * s = strchr(xdigits, tolower(*q));
115 if (s == NULL || (sval = s - xdigits) >= base)
117 val = (val * base) + sval;
126 * Convert a whitespace delimtied string into an array
127 * of strings, being expect/send pairs
131 read_chat(char **chatstr)
133 char *str = *chatstr;
140 if ((l=strlen(str)) > 0 && (tmp=malloc(l + 1)) != NULL &&
141 (res=malloc(((l + 1) / 2 + 1) * sizeof(char *))) != NULL) {
142 static char ws[] = " \t";
145 for (l = 0, p = strtok(strcpy(tmp, str), ws);
147 p = strtok(NULL, ws))
152 for (q = r = p; *r; ++q)
156 /* handle special escapes */
180 case 'p': /* pause */
184 case 'S': /* space */
187 case 'x': /* hexdigit */
189 *r++ = getdigit(&q, 16, 2);
192 case '0': /* octal */
194 *r++ = getdigit(&q, 8, 3);
197 default: /* literal */
200 case 0: /* not past eos */
205 /* copy standard character */
210 /* Remove surrounding quotes, if any
212 if (*p == '"' || *p == '\'') {
213 q = strrchr(p+1, *p);
214 if (q != NULL && *q == *p && q[1] == '\0') {
233 * clean a character for display (ctrl/meta character)
237 cleanchr(char **buf, unsigned char ch)
240 static char tmpbuf[5];
241 char * tmp = buf ? *buf : tmpbuf;
253 } else if (ch == 127) {
267 * clean a string for display (ctrl/meta characters)
271 cleanstr(const char *s, int l)
273 static char * tmp = NULL;
274 static int tmplen = 0;
276 if (tmplen < l * 4 + 1)
277 tmp = realloc(tmp, tmplen = l * 4 + 1);
281 return "(mem alloc error)";
287 cleanchr(&p, s[i++]);
296 * return result as a pseudo-english word
302 static const char * results[] = {
303 "OK", "MEMERROR", "IOERROR", "TIMEOUT"
305 return results[r & 3];
311 * scan input for an expected string
315 chat_expect(const char *str)
319 if (chat_debug & CHATDEBUG_EXPECT)
320 syslog(LOG_DEBUG, "chat_expect '%s'", cleanstr(str, strlen(str)));
322 if ((len = strlen(str)) > 0) {
326 if ((got = malloc(len + 1)) == NULL)
330 memset(got, 0, len+1);
334 while (r == 0 && i < len) {
340 if (read(STDIN_FILENO, &ch, 1) == 1) {
342 if (chat_debug & CHATDEBUG_RECEIVE)
343 syslog(LOG_DEBUG, "chat_recv '%s' m=%d",
344 cleanchr(NULL, ch), i);
351 /* See if we can resync on a
352 * partial match in our buffer
354 while (j < i && memcmp(got + j, str, i - j) != 0)
357 memcpy(got, got + j, i - j);
371 if (chat_debug & CHATDEBUG_EXPECT)
372 syslog(LOG_DEBUG, "chat_expect %s", result(r));
384 chat_send(char const *str)
388 if (chat_debug & CHATDEBUG_SEND)
389 syslog(LOG_DEBUG, "chat_send '%s'", cleanstr(str, strlen(str)));
394 while (r == 0 && *str)
396 unsigned char ch = (unsigned char)*str++;
400 else if (ch == PAUSE_CH)
401 usleep(500000); /* 1/2 second */
403 usleep(10000); /* be kind to modem */
404 if (write(STDOUT_FILENO, &ch, 1) != 1)
413 if (chat_debug & CHATDEBUG_SEND)
414 syslog(LOG_DEBUG, "chat_send %s", result(r));
424 * -1 - no script supplied
425 * 0 - script terminated correctly
426 * 1 - invalid argument, expect string too large, etc.
427 * 2 - error on an I/O operation or fatal error condition
428 * 3 - timeout waiting for a simple string
431 * char *scrstr - unparsed chat script
432 * timeout - seconds timeout
433 * debug - debug value (bitmask)
437 getty_chat(char *scrstr, int timeout, int debug)
441 chat_alarm = timeout ? timeout : CHAT_DEFAULT_TIMEOUT;
444 if (scrstr != NULL) {
447 if (chat_debug & CHATDEBUG_MISC)
448 syslog(LOG_DEBUG, "getty_chat script='%s'", scrstr);
450 if ((script = read_chat(&scrstr)) != NULL) {
456 * We need to be in raw mode for all this
460 old_alarm = signal(SIGALRM, chat_alrm);
461 chat_unalarm(); /* Force blocking mode at start */
464 * This is the send/expect loop
466 while (r == 0 && script[i] != NULL)
467 if ((r = chat_expect(script[i++])) == 0 && script[i] != NULL)
468 r = chat_send(script[i++]);
470 signal(SIGALRM, old_alarm);
475 * Ensure stdin is in blocking mode
477 ioctl(STDIN_FILENO, FIONBIO, &off);
480 if (chat_debug & CHATDEBUG_MISC)
481 syslog(LOG_DEBUG, "getty_chat %s", result(r));