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[] =
28 "$FreeBSD: src/libexec/getty/chat.c,v 1.6 1999/08/28 00:09:34 peter Exp $";
31 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/resource.h>
35 #include <sys/ttydefaults.h>
36 #include <sys/utsname.h>
50 #include <sys/socket.h>
54 #define PAUSE_CH (unsigned char)'\xff' /* pause kludge */
56 #define CHATDEBUG_RECEIVE 0x01
57 #define CHATDEBUG_SEND 0x02
58 #define CHATDEBUG_EXPECT 0x04
59 #define CHATDEBUG_MISC 0x08
61 #define CHATDEBUG_DEFAULT 0
62 #define CHAT_DEFAULT_TIMEOUT 10
65 static int chat_debug = CHATDEBUG_DEFAULT;
66 static int chat_alarm = CHAT_DEFAULT_TIMEOUT; /* Default */
68 static volatile int alarmed = 0;
71 static void chat_alrm __P((int));
72 static int chat_unalarm __P((void));
73 static int getdigit __P((unsigned char **, int, int));
74 static char **read_chat __P((char **));
75 static char *cleanchr __P((char **, unsigned char));
76 static char *cleanstr __P((const unsigned char *, int));
77 static const char *result __P((int));
78 static int chat_expect __P((const char *));
79 static int chat_send __P((char const *));
83 * alarm signal handler
84 * handle timeouts in read/write
85 * change stdin to non-blocking mode to prevent
86 * possible hang in read().
97 signal(SIGALRM, chat_alrm);
98 ioctl(STDIN_FILENO, FIONBIO, &on);
103 * Turn back on blocking mode reset by chat_alrm()
110 return ioctl(STDIN_FILENO, FIONBIO, &off);
115 * convert a string of a given base (octal/hex) to binary
119 getdigit(ptr, base, max)
126 static const char xdigits[] = "0123456789abcdef";
128 for (i = 0, q = *ptr; i++ < max; ++q) {
130 const char * s = strchr(xdigits, tolower(*q));
132 if (s == NULL || (sval = s - xdigits) >= base)
134 val = (val * base) + sval;
143 * Convert a whitespace delimtied string into an array
144 * of strings, being expect/send pairs
151 char *str = *chatstr;
158 if ((l=strlen(str)) > 0 && (tmp=malloc(l + 1)) != NULL &&
159 (res=malloc((l / 2 + 1) * sizeof(char *))) != NULL) {
160 static char ws[] = " \t";
163 for (l = 0, p = strtok(strcpy(tmp, str), ws);
165 p = strtok(NULL, ws))
167 unsigned char *q, *r;
170 for (q = r = (unsigned char *)p; *r; ++q)
174 /* handle special escapes */
198 case 'p': /* pause */
202 case 'S': /* space */
205 case 'x': /* hexdigit */
207 *r++ = getdigit(&q, 16, 2);
210 case '0': /* octal */
212 *r++ = getdigit(&q, 8, 3);
215 default: /* literal */
218 case 0: /* not past eos */
223 /* copy standard character */
228 /* Remove surrounding quotes, if any
230 if (*p == '"' || *p == '\'') {
231 q = strrchr(p+1, *p);
232 if (q != NULL && *q == *p && q[1] == '\0') {
251 * clean a character for display (ctrl/meta character)
260 static char tmpbuf[5];
261 char * tmp = buf ? *buf : tmpbuf;
273 } else if (ch == 127) {
287 * clean a string for display (ctrl/meta characters)
292 const unsigned char *s;
295 static unsigned char * tmp = NULL;
296 static int tmplen = 0;
298 if (tmplen < l * 4 + 1)
299 tmp = realloc(tmp, tmplen = l * 4 + 1);
303 return (char *)"(mem alloc error)";
309 cleanchr(&p, s[i++]);
318 * return result as an pseudo-english word
325 static const char * results[] = {
326 "OK", "MEMERROR", "IOERROR", "TIMEOUT"
328 return results[r & 3];
334 * scan input for an expected string
343 if (chat_debug & CHATDEBUG_EXPECT)
344 syslog(LOG_DEBUG, "chat_expect '%s'", cleanstr(str, strlen(str)));
346 if ((len = strlen(str)) > 0) {
350 if ((got = malloc(len + 1)) == NULL)
354 memset(got, 0, len+1);
358 while (r == 0 && i < len) {
364 if (read(STDIN_FILENO, &ch, 1) == 1) {
366 if (chat_debug & CHATDEBUG_RECEIVE)
367 syslog(LOG_DEBUG, "chat_recv '%s' m=%d",
368 cleanchr(NULL, ch), i);
375 /* See if we can resync on a
376 * partial match in our buffer
378 while (j < i && memcmp(got + j, str, i - j) != NULL)
381 memcpy(got, got + j, i - j);
395 if (chat_debug & CHATDEBUG_EXPECT)
396 syslog(LOG_DEBUG, "chat_expect %s", result(r));
413 if (chat_debug && CHATDEBUG_SEND)
414 syslog(LOG_DEBUG, "chat_send '%s'", cleanstr(str, strlen(str)));
419 while (r == 0 && *str)
421 unsigned char ch = (unsigned char)*str++;
425 else if (ch == PAUSE_CH)
426 usleep(500000); /* 1/2 second */
428 usleep(10000); /* be kind to modem */
429 if (write(STDOUT_FILENO, &ch, 1) != 1)
438 if (chat_debug & CHATDEBUG_SEND)
439 syslog(LOG_DEBUG, "chat_send %s", result(r));
449 * -1 - no script supplied
450 * 0 - script terminated correctly
451 * 1 - invalid argument, expect string too large, etc.
452 * 2 - error on an I/O operation or fatal error condition
453 * 3 - timeout waiting for a simple string
456 * char *scrstr - unparsed chat script
457 * timeout - seconds timeout
458 * debug - debug value (bitmask)
462 getty_chat(scrstr, timeout, debug)
468 chat_alarm = timeout ? timeout : CHAT_DEFAULT_TIMEOUT;
471 if (scrstr != NULL) {
474 if (chat_debug & CHATDEBUG_MISC)
475 syslog(LOG_DEBUG, "getty_chat script='%s'", scrstr);
477 if ((script = read_chat(&scrstr)) != NULL) {
483 * We need to be in raw mode for all this
487 old_alarm = signal(SIGALRM, chat_alrm);
488 chat_unalarm(); /* Force blocking mode at start */
491 * This is the send/expect loop
493 while (r == 0 && script[i] != NULL)
494 if ((r = chat_expect(script[i++])) == 0 && script[i] != NULL)
495 r = chat_send(script[i++]);
497 signal(SIGALRM, old_alarm);
502 * Ensure stdin is in blocking mode
504 ioctl(STDIN_FILENO, FIONBIO, &off);
507 if (chat_debug & CHATDEBUG_MISC)
508 syslog(LOG_DEBUG, "getty_chat %s", result(r));