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.
25 * $FreeBSD: src/libexec/getty/chat.c,v 1.6 1999/08/28 00:09:34 peter Exp $
26 * $DragonFly: src/libexec/getty/chat.c,v 1.6 2007/11/25 18:10:06 swildner Exp $
29 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/resource.h>
33 #include <sys/ttydefaults.h>
34 #include <sys/utsname.h>
48 #include <sys/socket.h>
53 #define PAUSE_CH (unsigned char)'\xff' /* pause kludge */
55 #define CHATDEBUG_RECEIVE 0x01
56 #define CHATDEBUG_SEND 0x02
57 #define CHATDEBUG_EXPECT 0x04
58 #define CHATDEBUG_MISC 0x08
60 #define CHATDEBUG_DEFAULT 0
61 #define CHAT_DEFAULT_TIMEOUT 10
64 static int chat_debug = CHATDEBUG_DEFAULT;
65 static int chat_alarm = CHAT_DEFAULT_TIMEOUT; /* Default */
67 static volatile int alarmed = 0;
70 static void chat_alrm (int);
71 static int chat_unalarm (void);
72 static int getdigit (unsigned char **, int, int);
73 static char **read_chat (char **);
74 static char *cleanchr (char **, unsigned char);
75 static char *cleanstr (const unsigned char *, int);
76 static const char *result (int);
77 static int chat_expect (const char *);
78 static int chat_send (char const *);
82 * alarm signal handler
83 * handle timeouts in read/write
84 * change stdin to non-blocking mode to prevent
85 * possible hang in read().
95 signal(SIGALRM, chat_alrm);
96 ioctl(STDIN_FILENO, FIONBIO, &on);
101 * Turn back on blocking mode reset by chat_alrm()
109 return ioctl(STDIN_FILENO, FIONBIO, &off);
114 * convert a string of a given base (octal/hex) to binary
118 getdigit(unsigned char **ptr, int base, int max)
123 static const char xdigits[] = "0123456789abcdef";
125 for (i = 0, q = *ptr; i++ < max; ++q) {
127 const char * s = strchr(xdigits, tolower(*q));
129 if (s == NULL || (sval = s - xdigits) >= base)
131 val = (val * base) + sval;
140 * Convert a whitespace delimtied string into an array
141 * of strings, being expect/send pairs
145 read_chat(char **chatstr)
147 char *str = *chatstr;
154 if ((l=strlen(str)) > 0 && (tmp=malloc(l + 1)) != NULL &&
155 (res=malloc((l / 2 + 1) * sizeof(char *))) != NULL) {
156 static char ws[] = " \t";
159 for (l = 0, p = strtok(strcpy(tmp, str), ws);
161 p = strtok(NULL, ws))
163 unsigned char *q, *r;
166 for (q = r = (unsigned char *)p; *r; ++q)
170 /* handle special escapes */
194 case 'p': /* pause */
198 case 'S': /* space */
201 case 'x': /* hexdigit */
203 *r++ = getdigit(&q, 16, 2);
206 case '0': /* octal */
208 *r++ = getdigit(&q, 8, 3);
211 default: /* literal */
214 case 0: /* not past eos */
219 /* copy standard character */
224 /* Remove surrounding quotes, if any
226 if (*p == '"' || *p == '\'') {
227 q = strrchr(p+1, *p);
228 if (q != NULL && *q == *p && q[1] == '\0') {
247 * clean a character for display (ctrl/meta character)
251 cleanchr(char **buf, unsigned char ch)
254 static char tmpbuf[5];
255 char * tmp = buf ? *buf : tmpbuf;
267 } else if (ch == 127) {
281 * clean a string for display (ctrl/meta characters)
285 cleanstr(const unsigned char *s, int l)
287 static unsigned char * tmp = NULL;
288 static int tmplen = 0;
290 if (tmplen < l * 4 + 1)
291 tmp = realloc(tmp, tmplen = l * 4 + 1);
295 return (char *)"(mem alloc error)";
301 cleanchr(&p, s[i++]);
310 * return result as an pseudo-english word
316 static const char * results[] = {
317 "OK", "MEMERROR", "IOERROR", "TIMEOUT"
319 return results[r & 3];
325 * scan input for an expected string
329 chat_expect(const char *str)
333 if (chat_debug & CHATDEBUG_EXPECT)
334 syslog(LOG_DEBUG, "chat_expect '%s'", cleanstr(str, strlen(str)));
336 if ((len = strlen(str)) > 0) {
340 if ((got = malloc(len + 1)) == NULL)
344 memset(got, 0, len+1);
348 while (r == 0 && i < len) {
354 if (read(STDIN_FILENO, &ch, 1) == 1) {
356 if (chat_debug & CHATDEBUG_RECEIVE)
357 syslog(LOG_DEBUG, "chat_recv '%s' m=%d",
358 cleanchr(NULL, ch), i);
365 /* See if we can resync on a
366 * partial match in our buffer
368 while (j < i && memcmp(got + j, str, i - j) != 0)
371 memcpy(got, got + j, i - j);
385 if (chat_debug & CHATDEBUG_EXPECT)
386 syslog(LOG_DEBUG, "chat_expect %s", result(r));
398 chat_send(char const *str)
402 if (chat_debug & CHATDEBUG_SEND)
403 syslog(LOG_DEBUG, "chat_send '%s'", cleanstr(str, strlen(str)));
408 while (r == 0 && *str)
410 unsigned char ch = (unsigned char)*str++;
414 else if (ch == PAUSE_CH)
415 usleep(500000); /* 1/2 second */
417 usleep(10000); /* be kind to modem */
418 if (write(STDOUT_FILENO, &ch, 1) != 1)
427 if (chat_debug & CHATDEBUG_SEND)
428 syslog(LOG_DEBUG, "chat_send %s", result(r));
438 * -1 - no script supplied
439 * 0 - script terminated correctly
440 * 1 - invalid argument, expect string too large, etc.
441 * 2 - error on an I/O operation or fatal error condition
442 * 3 - timeout waiting for a simple string
445 * char *scrstr - unparsed chat script
446 * timeout - seconds timeout
447 * debug - debug value (bitmask)
451 getty_chat(char *scrstr, int timeout, int debug)
455 chat_alarm = timeout ? timeout : CHAT_DEFAULT_TIMEOUT;
458 if (scrstr != NULL) {
461 if (chat_debug & CHATDEBUG_MISC)
462 syslog(LOG_DEBUG, "getty_chat script='%s'", scrstr);
464 if ((script = read_chat(&scrstr)) != NULL) {
470 * We need to be in raw mode for all this
474 old_alarm = signal(SIGALRM, chat_alrm);
475 chat_unalarm(); /* Force blocking mode at start */
478 * This is the send/expect loop
480 while (r == 0 && script[i] != NULL)
481 if ((r = chat_expect(script[i++])) == 0 && script[i] != NULL)
482 r = chat_send(script[i++]);
484 signal(SIGALRM, old_alarm);
489 * Ensure stdin is in blocking mode
491 ioctl(STDIN_FILENO, FIONBIO, &off);
494 if (chat_debug & CHATDEBUG_MISC)
495 syslog(LOG_DEBUG, "getty_chat %s", result(r));