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: head/libexec/getty/chat.c 329724 2018-02-21 15:57:24Z trasz $
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/utsname.h>
42 #define PAUSE_CH (unsigned char)'\xff' /* pause kludge */
44 #define CHATDEBUG_RECEIVE 0x01
45 #define CHATDEBUG_SEND 0x02
46 #define CHATDEBUG_EXPECT 0x04
47 #define CHATDEBUG_MISC 0x08
49 #define CHATDEBUG_DEFAULT 0
50 #define CHAT_DEFAULT_TIMEOUT 10
53 static int chat_debug
= CHATDEBUG_DEFAULT
;
54 static int chat_alarm
= CHAT_DEFAULT_TIMEOUT
; /* Default */
56 static volatile int alarmed
= 0;
59 static void chat_alrm(int);
60 static int chat_unalarm(void);
61 static int getdigit(unsigned char **, int, int);
62 static char **read_chat(char **);
63 static char *cleanchr(char **, unsigned char);
64 static const char *cleanstr(const unsigned char *, int);
65 static const char *result(int);
66 static int chat_expect(const char *);
67 static int chat_send(char const *);
71 * alarm signal handler
72 * handle timeouts in read/write
73 * change stdin to non-blocking mode to prevent
74 * possible hang in read().
78 chat_alrm(int signo __unused
)
84 signal(SIGALRM
, chat_alrm
);
85 ioctl(STDIN_FILENO
, FIONBIO
, &on
);
90 * Turn back on blocking mode reset by chat_alrm()
98 return ioctl(STDIN_FILENO
, FIONBIO
, &off
);
103 * convert a string of a given base (octal/hex) to binary
107 getdigit(unsigned char **ptr
, int base
, int max
)
112 static const char xdigits
[] = "0123456789abcdef";
114 for (i
= 0, q
= *ptr
; i
++ < max
; ++q
) {
116 const char * s
= strchr(xdigits
, tolower(*q
));
118 if (s
== NULL
|| (sval
= s
- xdigits
) >= base
)
120 val
= (val
* base
) + sval
;
129 * Convert a whitespace delimtied string into an array
130 * of strings, being expect/send pairs
134 read_chat(char **chatstr
)
136 char *str
= *chatstr
;
143 if ((l
=strlen(str
)) > 0 && (tmp
=malloc(l
+ 1)) != NULL
&&
144 (res
=malloc((l
/ 2 + 1) * sizeof(char *))) != NULL
) {
145 static char ws
[] = " \t";
148 for (l
= 0, p
= strtok(strcpy(tmp
, str
), ws
);
150 p
= strtok(NULL
, ws
))
152 unsigned char *q
, *r
;
155 for (q
= r
= (unsigned char *)p
; *r
; ++q
)
159 /* handle special escapes */
183 case 'p': /* pause */
187 case 'S': /* space */
190 case 'x': /* hexdigit */
192 *r
++ = getdigit(&q
, 16, 2);
195 case '0': /* octal */
197 *r
++ = getdigit(&q
, 8, 3);
200 default: /* literal */
203 case 0: /* not past eos */
208 /* copy standard character */
213 /* Remove surrounding quotes, if any
215 if (*p
== '"' || *p
== '\'') {
216 q
= strrchr(p
+1, *p
);
217 if (q
!= NULL
&& *q
== *p
&& q
[1] == '\0') {
236 * clean a character for display (ctrl/meta character)
240 cleanchr(char **buf
, unsigned char ch
)
243 static char tmpbuf
[5];
244 char * tmp
= buf
? *buf
: tmpbuf
;
256 } else if (ch
== 127) {
270 * clean a string for display (ctrl/meta characters)
274 cleanstr(const unsigned char *s
, int l
)
276 static unsigned char * tmp
= NULL
;
277 static int tmplen
= 0;
279 if (tmplen
< l
* 4 + 1)
280 tmp
= realloc(tmp
, tmplen
= l
* 4 + 1);
284 return "(mem alloc error)";
290 cleanchr(&p
, s
[i
++]);
299 * return result as a pseudo-english word
305 static const char * results
[] = {
306 "OK", "MEMERROR", "IOERROR", "TIMEOUT"
308 return results
[r
& 3];
314 * scan input for an expected string
318 chat_expect(const char *str
)
322 if (chat_debug
& CHATDEBUG_EXPECT
)
323 syslog(LOG_DEBUG
, "chat_expect '%s'", cleanstr(str
, strlen(str
)));
325 if ((len
= strlen(str
)) > 0) {
329 if ((got
= malloc(len
+ 1)) == NULL
)
333 memset(got
, 0, len
+1);
337 while (r
== 0 && i
< len
) {
343 if (read(STDIN_FILENO
, &ch
, 1) == 1) {
345 if (chat_debug
& CHATDEBUG_RECEIVE
)
346 syslog(LOG_DEBUG
, "chat_recv '%s' m=%d",
347 cleanchr(NULL
, ch
), i
);
354 /* See if we can resync on a
355 * partial match in our buffer
357 while (j
< i
&& memcmp(got
+ j
, str
, i
- j
) != 0)
360 memcpy(got
, got
+ j
, i
- j
);
374 if (chat_debug
& CHATDEBUG_EXPECT
)
375 syslog(LOG_DEBUG
, "chat_expect %s", result(r
));
387 chat_send(char const *str
)
391 if (chat_debug
& CHATDEBUG_SEND
)
392 syslog(LOG_DEBUG
, "chat_send '%s'", cleanstr(str
, strlen(str
)));
397 while (r
== 0 && *str
)
399 unsigned char ch
= (unsigned char)*str
++;
403 else if (ch
== PAUSE_CH
)
404 usleep(500000); /* 1/2 second */
406 usleep(10000); /* be kind to modem */
407 if (write(STDOUT_FILENO
, &ch
, 1) != 1)
416 if (chat_debug
& CHATDEBUG_SEND
)
417 syslog(LOG_DEBUG
, "chat_send %s", result(r
));
427 * -1 - no script supplied
428 * 0 - script terminated correctly
429 * 1 - invalid argument, expect string too large, etc.
430 * 2 - error on an I/O operation or fatal error condition
431 * 3 - timeout waiting for a simple string
434 * char *scrstr - unparsed chat script
435 * timeout - seconds timeout
436 * debug - debug value (bitmask)
440 getty_chat(char *scrstr
, int timeout
, int debug
)
444 chat_alarm
= timeout
? timeout
: CHAT_DEFAULT_TIMEOUT
;
447 if (scrstr
!= NULL
) {
450 if (chat_debug
& CHATDEBUG_MISC
)
451 syslog(LOG_DEBUG
, "getty_chat script='%s'", scrstr
);
453 if ((script
= read_chat(&scrstr
)) != NULL
) {
459 * We need to be in raw mode for all this
463 old_alarm
= signal(SIGALRM
, chat_alrm
);
464 chat_unalarm(); /* Force blocking mode at start */
467 * This is the send/expect loop
469 while (r
== 0 && script
[i
] != NULL
)
470 if ((r
= chat_expect(script
[i
++])) == 0 && script
[i
] != NULL
)
471 r
= chat_send(script
[i
++]);
473 signal(SIGALRM
, old_alarm
);
478 * Ensure stdin is in blocking mode
480 ioctl(STDIN_FILENO
, FIONBIO
, &off
);
483 if (chat_debug
& CHATDEBUG_MISC
)
484 syslog(LOG_DEBUG
, "getty_chat %s", result(r
));