]>
git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/getty.tproj/chat.c
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/cdefs.h>
29 __unused
static const char rcsid
[] =
30 "$FreeBSD: src/libexec/getty/chat.c,v 1.11 2005/04/06 17:42:24 stefanf Exp $";
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/utsname.h>
47 #define PAUSE_CH (unsigned char)'\xff' /* pause kludge */
49 #define CHATDEBUG_RECEIVE 0x01
50 #define CHATDEBUG_SEND 0x02
51 #define CHATDEBUG_EXPECT 0x04
52 #define CHATDEBUG_MISC 0x08
54 #define CHATDEBUG_DEFAULT 0
55 #define CHAT_DEFAULT_TIMEOUT 10
58 static int chat_debug
= CHATDEBUG_DEFAULT
;
59 static int chat_alarm
= CHAT_DEFAULT_TIMEOUT
; /* Default */
61 static volatile int alarmed
= 0;
64 static void chat_alrm(int);
65 static int chat_unalarm(void);
66 static int getdigit(unsigned char **, int, int);
67 static char **read_chat(char **);
68 static char *cleanchr(char **, unsigned char);
69 static char *cleanstr(const char *, int);
70 static const char *result(int);
71 static int chat_expect(const char *);
72 static int chat_send(char const *);
76 * alarm signal handler
77 * handle timeouts in read/write
78 * change stdin to non-blocking mode to prevent
79 * possible hang in read().
89 signal(SIGALRM
, chat_alrm
);
90 ioctl(STDIN_FILENO
, FIONBIO
, &on
);
95 * Turn back on blocking mode reset by chat_alrm()
102 return ioctl(STDIN_FILENO
, FIONBIO
, &off
);
107 * convert a string of a given base (octal/hex) to binary
111 getdigit(unsigned char **ptr
, int base
, int max
)
116 static const char xdigits
[] = "0123456789abcdef";
118 for (i
= 0, q
= *ptr
; i
++ < max
; ++q
) {
120 const char * s
= strchr(xdigits
, tolower(*q
));
122 if (s
== NULL
|| (sval
= s
- xdigits
) >= base
)
124 val
= (val
* base
) + sval
;
133 * Convert a whitespace delimtied string into an array
134 * of strings, being expect/send pairs
138 read_chat(char **chatstr
)
140 char *str
= *chatstr
;
147 if ((l
=strlen(str
)) > 0 && (tmp
=malloc(l
+ 1)) != NULL
&&
148 (res
=malloc((l
/ 2 + 1) * sizeof(char *))) != NULL
) {
149 static char ws
[] = " \t";
152 for (l
= 0, p
= strtok(strcpy(tmp
, str
), ws
);
154 p
= strtok(NULL
, ws
))
156 unsigned char *q
, *r
;
159 for (q
= r
= (unsigned char *)p
; *r
; ++q
)
163 /* handle special escapes */
187 case 'p': /* pause */
191 case 'S': /* space */
194 case 'x': /* hexdigit */
196 *r
++ = getdigit(&q
, 16, 2);
199 case '0': /* octal */
201 *r
++ = getdigit(&q
, 8, 3);
204 default: /* literal */
207 case 0: /* not past eos */
212 /* copy standard character */
217 /* Remove surrounding quotes, if any
219 if (*p
== '"' || *p
== '\'') {
220 q
= (unsigned char*)strrchr(p
+1, *p
);
221 if (q
!= NULL
&& *q
== *p
&& q
[1] == '\0') {
240 * clean a character for display (ctrl/meta character)
244 cleanchr(char **buf
, unsigned char ch
)
247 static char tmpbuf
[5];
248 char * tmp
= buf
? *buf
: tmpbuf
;
260 } else if (ch
== 127) {
274 * clean a string for display (ctrl/meta characters)
278 cleanstr(const char *s
, int l
)
280 static char * tmp
= NULL
;
281 static int tmplen
= 0;
283 if (tmplen
< l
* 4 + 1)
284 tmp
= realloc(tmp
, tmplen
= l
* 4 + 1);
288 return (char *)"
(mem alloc error)"
;
294 cleanchr(&p
, s
[i
++]);
303 * return result as a pseudo-english word
309 static const char * results
[] = {
310 "OK", "MEMERROR", "IOERROR", "TIMEOUT"
312 return results
[r
& 3];
318 * scan input for an expected string
322 chat_expect(const char *str
)
326 if (chat_debug
& CHATDEBUG_EXPECT
)
327 syslog(LOG_DEBUG
, "chat_expect '%s'", cleanstr(str
, strlen(str
)));
329 if ((len
= strlen(str
)) > 0) {
333 if ((got
= malloc(len
+ 1)) == NULL
)
337 memset(got
, 0, len
+1);
341 while (r
== 0 && i
< len
) {
347 if (read(STDIN_FILENO
, &ch
, 1) == 1) {
349 if (chat_debug
& CHATDEBUG_RECEIVE
)
350 syslog(LOG_DEBUG
, "chat_recv '%s' m=%d",
351 cleanchr(NULL
, ch
), i
);
358 /* See if we can resync on a
359 * partial match in our buffer
361 while (j
< i
&& memcmp(got
+ j
, str
, i
- j
) != 0)
364 memcpy(got
, got
+ j
, i
- j
);
378 if (chat_debug
& CHATDEBUG_EXPECT
)
379 syslog(LOG_DEBUG
, "chat_expect %s", result(r
));
391 chat_send(char const *str
)
395 if (chat_debug
& CHATDEBUG_SEND
)
396 syslog(LOG_DEBUG
, "chat_send '%s'", cleanstr(str
, strlen(str
)));
401 while (r
== 0 && *str
)
403 unsigned char ch
= (unsigned char)*str
++;
407 else if (ch
== PAUSE_CH
)
408 usleep(500000); /* 1/2 second */
410 usleep(10000); /* be kind to modem */
411 if (write(STDOUT_FILENO
, &ch
, 1) != 1)
420 if (chat_debug
& CHATDEBUG_SEND
)
421 syslog(LOG_DEBUG
, "chat_send %s", result(r
));
431 * -1 - no script supplied
432 * 0 - script terminated correctly
433 * 1 - invalid argument, expect string too large, etc.
434 * 2 - error on an I/O operation or fatal error condition
435 * 3 - timeout waiting for a simple string
438 * char *scrstr - unparsed chat script
439 * timeout - seconds timeout
440 * debug - debug value (bitmask)
444 getty_chat(char *scrstr
, int timeout
, int debug
)
448 chat_alarm
= timeout
? timeout
: CHAT_DEFAULT_TIMEOUT
;
451 if (scrstr
!= NULL
) {
454 if (chat_debug
& CHATDEBUG_MISC
)
455 syslog(LOG_DEBUG
, "getty_chat script='%s'", scrstr
);
457 if ((script
= read_chat(&scrstr
)) != NULL
) {
463 * We need to be in raw mode for all this
467 old_alarm
= signal(SIGALRM
, chat_alrm
);
468 chat_unalarm(); /* Force blocking mode at start */
471 * This is the send/expect loop
473 while (r
== 0 && script
[i
] != NULL
)
474 if ((r
= chat_expect(script
[i
++])) == 0 && script
[i
] != NULL
)
475 r
= chat_send(script
[i
++]);
477 signal(SIGALRM
, old_alarm
);
482 * Ensure stdin is in blocking mode
484 ioctl(STDIN_FILENO
, FIONBIO
, &off
);
487 if (chat_debug
& CHATDEBUG_MISC
)
488 syslog(LOG_DEBUG
, "getty_chat %s", result(r
));