1 | /* |
---|
2 | * Chat -- a program for automatic session establishment (i.e. dial |
---|
3 | * the phone and log in). |
---|
4 | * |
---|
5 | * Standard termination codes: |
---|
6 | * 0 - successful completion of the script |
---|
7 | * 1 - invalid argument, expect string too large, etc. |
---|
8 | * 2 - error on an I/O operation or fatal error condition. |
---|
9 | * 3 - timeout waiting for a simple string. |
---|
10 | * 4 - the first string declared as "ABORT" |
---|
11 | * 5 - the second string declared as "ABORT" |
---|
12 | * 6 - ... and so on for successive ABORT strings. |
---|
13 | * |
---|
14 | * This software is in the public domain. |
---|
15 | * |
---|
16 | * ----------------- |
---|
17 | * 22-May-99 added environment substitutuion, enabled with -E switch. |
---|
18 | * Andreas Arens <andras@cityweb.de>. |
---|
19 | * |
---|
20 | * 12-May-99 added a feature to read data to be sent from a file, |
---|
21 | * if the send string starts with @. Idea from gpk <gpk@onramp.net>. |
---|
22 | * |
---|
23 | * added -T and -U option and \T and \U substitution to pass a phone |
---|
24 | * number into chat script. Two are needed for some ISDN TA applications. |
---|
25 | * Keith Dart <kdart@cisco.com> |
---|
26 | * |
---|
27 | * |
---|
28 | * Added SAY keyword to send output to stderr. |
---|
29 | * This allows to turn ECHO OFF and to output specific, user selected, |
---|
30 | * text to give progress messages. This best works when stderr |
---|
31 | * exists (i.e.: pppd in nodetach mode). |
---|
32 | * |
---|
33 | * Added HANGUP directives to allow for us to be called |
---|
34 | * back. When HANGUP is set to NO, chat will not hangup at HUP signal. |
---|
35 | * We rely on timeouts in that case. |
---|
36 | * |
---|
37 | * Added CLR_ABORT to clear previously set ABORT string. This has been |
---|
38 | * dictated by the HANGUP above as "NO CARRIER" (for example) must be |
---|
39 | * an ABORT condition until we know the other host is going to close |
---|
40 | * the connection for call back. As soon as we have completed the |
---|
41 | * first stage of the call back sequence, "NO CARRIER" is a valid, non |
---|
42 | * fatal string. As soon as we got called back (probably get "CONNECT"), |
---|
43 | * we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command. |
---|
44 | * Note that CLR_ABORT packs the abort_strings[] array so that we do not |
---|
45 | * have unused entries not being reclaimed. |
---|
46 | * |
---|
47 | * In the same vein as above, added CLR_REPORT keyword. |
---|
48 | * |
---|
49 | * Allow for comments. Line starting with '#' are comments and are |
---|
50 | * ignored. If a '#' is to be expected as the first character, the |
---|
51 | * expect string must be quoted. |
---|
52 | * |
---|
53 | * |
---|
54 | * Francis Demierre <Francis@SwissMail.Com> |
---|
55 | * Thu May 15 17:15:40 MET DST 1997 |
---|
56 | * |
---|
57 | * |
---|
58 | * Added -r "report file" switch & REPORT keyword. |
---|
59 | * Robert Geer <bgeer@xmission.com> |
---|
60 | * |
---|
61 | * Added -s "use stderr" and -S "don't use syslog" switches. |
---|
62 | * June 18, 1997 |
---|
63 | * Karl O. Pinc <kop@meme.com> |
---|
64 | * |
---|
65 | * |
---|
66 | * Added -e "echo" switch & ECHO keyword |
---|
67 | * Dick Streefland <dicks@tasking.nl> |
---|
68 | * |
---|
69 | * |
---|
70 | * Considerable updates and modifications by |
---|
71 | * Al Longyear <longyear@pobox.com> |
---|
72 | * Paul Mackerras <paulus@cs.anu.edu.au> |
---|
73 | * |
---|
74 | * |
---|
75 | * The original author is: |
---|
76 | * |
---|
77 | * Karl Fox <karl@MorningStar.Com> |
---|
78 | * Morning Star Technologies, Inc. |
---|
79 | * 1760 Zollinger Road |
---|
80 | * Columbus, OH 43221 |
---|
81 | * (614)451-1883 |
---|
82 | * |
---|
83 | */ |
---|
84 | |
---|
85 | #include <stdio.h> |
---|
86 | #include <ctype.h> |
---|
87 | #include <time.h> |
---|
88 | #include <fcntl.h> |
---|
89 | #include <errno.h> |
---|
90 | #include <string.h> |
---|
91 | #include <stdlib.h> |
---|
92 | #include <unistd.h> |
---|
93 | #include <sys/types.h> |
---|
94 | #include <sys/stat.h> |
---|
95 | #include <syslog.h> |
---|
96 | #include <termios.h> |
---|
97 | #include "pppd.h" |
---|
98 | |
---|
99 | #undef TERMIOS |
---|
100 | #define TERMIOS |
---|
101 | |
---|
102 | |
---|
103 | #define STR_LEN 1024 |
---|
104 | char temp2[STR_LEN]; |
---|
105 | |
---|
106 | #ifndef SIGTYPE |
---|
107 | #define SIGTYPE void |
---|
108 | #endif |
---|
109 | |
---|
110 | #undef __P |
---|
111 | #undef __V |
---|
112 | |
---|
113 | #ifdef __STDC__ |
---|
114 | #include <stdarg.h> |
---|
115 | #define __V(x) x |
---|
116 | #define __P(x) x |
---|
117 | #else |
---|
118 | #include <varargs.h> |
---|
119 | #define __V(x) (va_alist) va_dcl |
---|
120 | #define __P(x) () |
---|
121 | #define const |
---|
122 | #endif |
---|
123 | |
---|
124 | #ifndef O_NONBLOCK |
---|
125 | #define O_NONBLOCK O_NDELAY |
---|
126 | #endif |
---|
127 | |
---|
128 | |
---|
129 | /*************** Micro getopt() *********************************************/ |
---|
130 | #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \ |
---|
131 | (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\ |
---|
132 | &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0)) |
---|
133 | #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \ |
---|
134 | (_O=4,(char*)0):(char*)0) |
---|
135 | #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0) |
---|
136 | #define ARG(c,v) (c?(--c,*v++):(char*)0) |
---|
137 | |
---|
138 | #if 0 |
---|
139 | static int _O = 0; /* Internal state */ |
---|
140 | #endif |
---|
141 | /*************** Micro getopt() *********************************************/ |
---|
142 | |
---|
143 | #define MAX_ABORTS 16 |
---|
144 | #define MAX_REPORTS 16 |
---|
145 | #define DEFAULT_CHAT_TIMEOUT 45 |
---|
146 | #define MAX_TIMEOUTS 10 |
---|
147 | |
---|
148 | static int echo = 0; |
---|
149 | static int quiet = 0; |
---|
150 | static int use_env = 0; |
---|
151 | static int exit_code = 0; |
---|
152 | static char *phone_num = (char *) 0; |
---|
153 | static char *phone_num2 = (char *) 0; |
---|
154 | static int ttyfd; |
---|
155 | static int timeout = DEFAULT_CHAT_TIMEOUT; |
---|
156 | |
---|
157 | #ifdef TERMIOS |
---|
158 | #define term_parms struct termios |
---|
159 | #define get_term_param(param) tcgetattr(0, param) |
---|
160 | #define set_term_param(param) tcsetattr(0, TCSANOW, param) |
---|
161 | struct termios saved_tty_parameters; |
---|
162 | #endif |
---|
163 | |
---|
164 | char *fail_reason = (char *)0; |
---|
165 | char fail_buffer[50]; |
---|
166 | char *abort_string[MAX_ABORTS]={"BUSY","NO DIALTONE","NO CARRIER","NO ANSWER","RING\r\nRING"}; |
---|
167 | int n_aborts = 5; |
---|
168 | int abort_next = 0, timeout_next = 0, echo_next = 0; |
---|
169 | int clear_abort_next = 0; |
---|
170 | |
---|
171 | char *report_string[MAX_REPORTS] ; |
---|
172 | char report_buffer[50] ; |
---|
173 | int n_reports = 0, report_next = 0, report_gathering = 0 ; |
---|
174 | int clear_report_next = 0; |
---|
175 | |
---|
176 | int say_next = 0, hup_next = 0; |
---|
177 | |
---|
178 | void *dup_mem __P((void *b, size_t c)); |
---|
179 | void *copy_of __P((char *s)); |
---|
180 | void break_sequence __P((void)); |
---|
181 | static int get_string __P((register char *string)); |
---|
182 | static int put_string __P((register char *s)); |
---|
183 | static int write_char __P((int c)); |
---|
184 | static int put_char __P((int c)); |
---|
185 | static int get_char __P((void)); |
---|
186 | void chat_send __P((register char *s)); |
---|
187 | static char *character __P((int c)); |
---|
188 | void chat_expect __P((register char *s)); |
---|
189 | static char *clean __P((register char *s, int sending)); |
---|
190 | char *expect_strtok __P((char *, char *)); |
---|
191 | int chatmain __P((int, int, char *)); |
---|
192 | |
---|
193 | |
---|
194 | void *dup_mem(b, c) |
---|
195 | void *b; |
---|
196 | size_t c; |
---|
197 | { |
---|
198 | void *ans = malloc (c); |
---|
199 | if (!ans) |
---|
200 | return NULL; |
---|
201 | |
---|
202 | memcpy(ans, b, c); |
---|
203 | return ans; |
---|
204 | } |
---|
205 | |
---|
206 | void *copy_of (s) |
---|
207 | char *s; |
---|
208 | { |
---|
209 | return dup_mem(s, strlen (s) + 1); |
---|
210 | } |
---|
211 | |
---|
212 | char *getnextcommand(char **string) |
---|
213 | { |
---|
214 | char *buf=*string,*res; |
---|
215 | res=strchr(buf,'|'); |
---|
216 | if (res==NULL) |
---|
217 | return NULL; |
---|
218 | *res='\0'; |
---|
219 | *string=res+1; |
---|
220 | return buf; |
---|
221 | } |
---|
222 | |
---|
223 | int chatmain(int fd, int mode, char *pScript) |
---|
224 | { |
---|
225 | char *arg; |
---|
226 | |
---|
227 | /* initialize exit code */ |
---|
228 | exit_code = 0; |
---|
229 | ttyfd = fd; |
---|
230 | |
---|
231 | if ( debug ) { |
---|
232 | dbglog("chat_main: %s\n", pScript); |
---|
233 | } |
---|
234 | |
---|
235 | /* get first expect string */ |
---|
236 | arg = getnextcommand(&pScript); |
---|
237 | while (( arg != NULL ) && ( exit_code == 0 )) { |
---|
238 | /* process the expect string */ |
---|
239 | chat_expect(arg); |
---|
240 | if ( exit_code == 0 ) { |
---|
241 | /* get the next send string */ |
---|
242 | arg = getnextcommand(&pScript); |
---|
243 | if ( arg != NULL ) { |
---|
244 | /* process the send string */ |
---|
245 | chat_send(arg); |
---|
246 | |
---|
247 | /* get the next expect string */ |
---|
248 | arg = getnextcommand(&pScript); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | ttyfd = (int)-1; |
---|
253 | |
---|
254 | return ( exit_code ); |
---|
255 | } |
---|
256 | |
---|
257 | void break_sequence() |
---|
258 | { |
---|
259 | tcsendbreak(ttyfd, 0); |
---|
260 | } |
---|
261 | |
---|
262 | /* |
---|
263 | * 'Clean up' this string. |
---|
264 | */ |
---|
265 | static char *clean(s, sending) |
---|
266 | register char *s; |
---|
267 | int sending; /* set to 1 when sending (putting) this string. */ |
---|
268 | { |
---|
269 | char temp[STR_LEN], env_str[STR_LEN], cur_chr; |
---|
270 | register char *s1, *phchar; |
---|
271 | int add_return = sending; |
---|
272 | #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7')) |
---|
273 | #define isalnumx(chr) ((((chr) >= '0') && ((chr) <= '9')) \ |
---|
274 | || (((chr) >= 'a') && ((chr) <= 'z')) \ |
---|
275 | || (((chr) >= 'A') && ((chr) <= 'Z')) \ |
---|
276 | || (chr) == '_') |
---|
277 | |
---|
278 | s1 = temp; |
---|
279 | while (*s) { |
---|
280 | cur_chr = *s++; |
---|
281 | if (cur_chr == '^') { |
---|
282 | cur_chr = *s++; |
---|
283 | if (cur_chr == '\0') { |
---|
284 | *s1++ = '^'; |
---|
285 | break; |
---|
286 | } |
---|
287 | cur_chr &= 0x1F; |
---|
288 | if (cur_chr != 0) { |
---|
289 | *s1++ = cur_chr; |
---|
290 | } |
---|
291 | continue; |
---|
292 | } |
---|
293 | |
---|
294 | if (use_env && cur_chr == '$') { /* ARI */ |
---|
295 | phchar = env_str; |
---|
296 | while (isalnumx(*s)) |
---|
297 | *phchar++ = *s++; |
---|
298 | *phchar = '\0'; |
---|
299 | phchar = getenv(env_str); |
---|
300 | if (phchar) |
---|
301 | while (*phchar) |
---|
302 | *s1++ = *phchar++; |
---|
303 | continue; |
---|
304 | } |
---|
305 | |
---|
306 | if (cur_chr != '\\') { |
---|
307 | *s1++ = cur_chr; |
---|
308 | continue; |
---|
309 | } |
---|
310 | |
---|
311 | cur_chr = *s++; |
---|
312 | if (cur_chr == '\0') { |
---|
313 | if (sending) { |
---|
314 | *s1++ = '\\'; |
---|
315 | *s1++ = '\\'; |
---|
316 | } |
---|
317 | break; |
---|
318 | } |
---|
319 | |
---|
320 | switch (cur_chr) { |
---|
321 | case 'b': |
---|
322 | *s1++ = '\b'; |
---|
323 | break; |
---|
324 | |
---|
325 | case 'c': |
---|
326 | if (sending && *s == '\0') |
---|
327 | add_return = 0; |
---|
328 | else |
---|
329 | *s1++ = cur_chr; |
---|
330 | break; |
---|
331 | |
---|
332 | case '\\': |
---|
333 | case 'K': |
---|
334 | case 'p': |
---|
335 | case 'd': |
---|
336 | if (sending) |
---|
337 | *s1++ = '\\'; |
---|
338 | *s1++ = cur_chr; |
---|
339 | break; |
---|
340 | |
---|
341 | case 'T': |
---|
342 | if (sending && phone_num) { |
---|
343 | for (phchar = phone_num; *phchar != '\0'; phchar++) |
---|
344 | *s1++ = *phchar; |
---|
345 | } |
---|
346 | else { |
---|
347 | *s1++ = '\\'; |
---|
348 | *s1++ = 'T'; |
---|
349 | } |
---|
350 | break; |
---|
351 | |
---|
352 | case 'U': |
---|
353 | if (sending && phone_num2) { |
---|
354 | for (phchar = phone_num2; *phchar != '\0'; phchar++) |
---|
355 | *s1++ = *phchar; |
---|
356 | } |
---|
357 | else { |
---|
358 | *s1++ = '\\'; |
---|
359 | *s1++ = 'U'; |
---|
360 | } |
---|
361 | break; |
---|
362 | |
---|
363 | case 'q': |
---|
364 | quiet = 1; |
---|
365 | break; |
---|
366 | |
---|
367 | case 'r': |
---|
368 | *s1++ = '\r'; |
---|
369 | break; |
---|
370 | |
---|
371 | case 'n': |
---|
372 | *s1++ = '\n'; |
---|
373 | break; |
---|
374 | |
---|
375 | case 's': |
---|
376 | *s1++ = ' '; |
---|
377 | break; |
---|
378 | |
---|
379 | case 't': |
---|
380 | *s1++ = '\t'; |
---|
381 | break; |
---|
382 | |
---|
383 | case 'N': |
---|
384 | if (sending) { |
---|
385 | *s1++ = '\\'; |
---|
386 | *s1++ = '\0'; |
---|
387 | } |
---|
388 | else |
---|
389 | *s1++ = 'N'; |
---|
390 | break; |
---|
391 | |
---|
392 | case '$': /* ARI */ |
---|
393 | if (use_env) { |
---|
394 | *s1++ = cur_chr; |
---|
395 | break; |
---|
396 | } |
---|
397 | /* FALL THROUGH */ |
---|
398 | |
---|
399 | default: |
---|
400 | if (isoctal (cur_chr)) { |
---|
401 | cur_chr &= 0x07; |
---|
402 | if (isoctal (*s)) { |
---|
403 | cur_chr <<= 3; |
---|
404 | cur_chr |= *s++ - '0'; |
---|
405 | if (isoctal (*s)) { |
---|
406 | cur_chr <<= 3; |
---|
407 | cur_chr |= *s++ - '0'; |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | if (cur_chr != 0 || sending) { |
---|
412 | if (sending && (cur_chr == '\\' || cur_chr == 0)) |
---|
413 | *s1++ = '\\'; |
---|
414 | *s1++ = cur_chr; |
---|
415 | } |
---|
416 | break; |
---|
417 | } |
---|
418 | |
---|
419 | if (sending) |
---|
420 | *s1++ = '\\'; |
---|
421 | *s1++ = cur_chr; |
---|
422 | break; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | if (add_return) |
---|
427 | *s1++ = '\r'; |
---|
428 | |
---|
429 | *s1++ = '\0'; /* guarantee closure */ |
---|
430 | *s1++ = '\0'; /* terminate the string */ |
---|
431 | return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */ |
---|
432 | } |
---|
433 | |
---|
434 | /* |
---|
435 | * A modified version of 'strtok'. This version skips \ sequences. |
---|
436 | */ |
---|
437 | char *expect_strtok (s, term) |
---|
438 | char *s, *term; |
---|
439 | { |
---|
440 | static char *str = ""; |
---|
441 | int escape_flag = 0; |
---|
442 | char *result; |
---|
443 | |
---|
444 | /* |
---|
445 | * If a string was specified then do initial processing. |
---|
446 | */ |
---|
447 | if (s) |
---|
448 | str = s; |
---|
449 | |
---|
450 | /* |
---|
451 | * If this is the escape flag then reset it and ignore the character. |
---|
452 | */ |
---|
453 | if (*str) |
---|
454 | result = str; |
---|
455 | else |
---|
456 | result = (char *) 0; |
---|
457 | |
---|
458 | while (*str) { |
---|
459 | if (escape_flag) { |
---|
460 | escape_flag = 0; |
---|
461 | ++str; |
---|
462 | continue; |
---|
463 | } |
---|
464 | |
---|
465 | if (*str == '\\') { |
---|
466 | ++str; |
---|
467 | escape_flag = 1; |
---|
468 | continue; |
---|
469 | } |
---|
470 | |
---|
471 | /* |
---|
472 | * If this is not in the termination string, continue. |
---|
473 | */ |
---|
474 | if (strchr (term, *str) == (char *) 0) { |
---|
475 | ++str; |
---|
476 | continue; |
---|
477 | } |
---|
478 | |
---|
479 | /* |
---|
480 | * This is the terminator. Mark the end of the string and stop. |
---|
481 | */ |
---|
482 | *str++ = '\0'; |
---|
483 | break; |
---|
484 | } |
---|
485 | return (result); |
---|
486 | } |
---|
487 | |
---|
488 | /* |
---|
489 | * Process the expect string |
---|
490 | */ |
---|
491 | void chat_expect (s) |
---|
492 | char *s; |
---|
493 | { |
---|
494 | char *expect; |
---|
495 | char *reply; |
---|
496 | |
---|
497 | if (strcmp(s, "HANGUP") == 0) { |
---|
498 | ++hup_next; |
---|
499 | return; |
---|
500 | } |
---|
501 | |
---|
502 | if (strcmp(s, "ABORT") == 0) { |
---|
503 | ++abort_next; |
---|
504 | return; |
---|
505 | } |
---|
506 | |
---|
507 | if (strcmp(s, "CLR_ABORT") == 0) { |
---|
508 | ++clear_abort_next; |
---|
509 | return; |
---|
510 | } |
---|
511 | |
---|
512 | if (strcmp(s, "REPORT") == 0) { |
---|
513 | ++report_next; |
---|
514 | return; |
---|
515 | } |
---|
516 | |
---|
517 | if (strcmp(s, "CLR_REPORT") == 0) { |
---|
518 | ++clear_report_next; |
---|
519 | return; |
---|
520 | } |
---|
521 | |
---|
522 | if (strcmp(s, "TIMEOUT") == 0) { |
---|
523 | ++timeout_next; |
---|
524 | return; |
---|
525 | } |
---|
526 | |
---|
527 | if (strcmp(s, "ECHO") == 0) { |
---|
528 | ++echo_next; |
---|
529 | return; |
---|
530 | } |
---|
531 | |
---|
532 | if (strcmp(s, "SAY") == 0) { |
---|
533 | ++say_next; |
---|
534 | return; |
---|
535 | } |
---|
536 | |
---|
537 | /* |
---|
538 | * Fetch the expect and reply string. |
---|
539 | */ |
---|
540 | for (;;) { |
---|
541 | expect = expect_strtok (s, "-"); |
---|
542 | s = (char *) 0; |
---|
543 | |
---|
544 | if (expect == (char *) 0) |
---|
545 | return; |
---|
546 | |
---|
547 | reply = expect_strtok (s, "-"); |
---|
548 | |
---|
549 | /* |
---|
550 | * Handle the expect string. If successful then exit. |
---|
551 | */ |
---|
552 | if (get_string (expect)) |
---|
553 | return; |
---|
554 | |
---|
555 | /* |
---|
556 | * If there is a sub-reply string then send it. Otherwise any condition |
---|
557 | * is terminal. |
---|
558 | */ |
---|
559 | if (reply == (char *) 0 || exit_code != 3) |
---|
560 | break; |
---|
561 | |
---|
562 | chat_send (reply); |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | /* |
---|
567 | * Translate the input character to the appropriate string for printing |
---|
568 | * the data. |
---|
569 | */ |
---|
570 | |
---|
571 | static char *character(c) |
---|
572 | int c; |
---|
573 | { |
---|
574 | static char string[10]; |
---|
575 | char *meta; |
---|
576 | |
---|
577 | meta = (c & 0x80) ? "M-" : ""; |
---|
578 | c &= 0x7F; |
---|
579 | |
---|
580 | if (c < 32) |
---|
581 | sprintf(string, "%s^%c", meta, (int)c + '@'); |
---|
582 | else if (c == 127) |
---|
583 | sprintf(string, "%s^?", meta); |
---|
584 | else |
---|
585 | sprintf(string, "%s%c", meta, c); |
---|
586 | |
---|
587 | return (string); |
---|
588 | } |
---|
589 | |
---|
590 | /* |
---|
591 | * process the reply string |
---|
592 | */ |
---|
593 | void chat_send (s) |
---|
594 | register char *s; |
---|
595 | { |
---|
596 | /* char file_data[STR_LEN]; */ |
---|
597 | |
---|
598 | if (say_next) { |
---|
599 | say_next = 0; |
---|
600 | s = clean(s, 1); |
---|
601 | write(2, s, strlen(s)); |
---|
602 | free(s); |
---|
603 | return; |
---|
604 | } |
---|
605 | |
---|
606 | if (hup_next) { |
---|
607 | hup_next = 0; |
---|
608 | return; |
---|
609 | } |
---|
610 | |
---|
611 | if (echo_next) { |
---|
612 | echo_next = 0; |
---|
613 | echo = (strcmp(s, "ON") == 0); |
---|
614 | return; |
---|
615 | } |
---|
616 | |
---|
617 | if (abort_next) { |
---|
618 | char *s1; |
---|
619 | |
---|
620 | abort_next = 0; |
---|
621 | if ( n_aborts < MAX_ABORTS ) { |
---|
622 | s1 = clean(s, 0); |
---|
623 | if (( strlen(s1) <= strlen(s) ) && |
---|
624 | ( strlen(s1) < sizeof(fail_buffer))) { |
---|
625 | |
---|
626 | abort_string[n_aborts++] = s1; |
---|
627 | } |
---|
628 | } |
---|
629 | return; |
---|
630 | } |
---|
631 | |
---|
632 | if (clear_abort_next) { |
---|
633 | clear_abort_next = 0; |
---|
634 | return; |
---|
635 | } |
---|
636 | |
---|
637 | if (report_next) { |
---|
638 | report_next = 0; |
---|
639 | return; |
---|
640 | } |
---|
641 | |
---|
642 | if (clear_report_next) { |
---|
643 | clear_report_next = 0; |
---|
644 | return; |
---|
645 | } |
---|
646 | |
---|
647 | if (timeout_next) { |
---|
648 | timeout_next = 0; |
---|
649 | timeout = atoi(s); |
---|
650 | |
---|
651 | if (timeout <= 0) |
---|
652 | timeout = DEFAULT_CHAT_TIMEOUT; |
---|
653 | |
---|
654 | return; |
---|
655 | } |
---|
656 | |
---|
657 | if (strcmp(s, "EOT") == 0) |
---|
658 | s = "^D\\c"; |
---|
659 | else if (strcmp(s, "BREAK") == 0) |
---|
660 | s = "\\K\\c"; |
---|
661 | |
---|
662 | if (!put_string(s)) { |
---|
663 | exit_code = 2; |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | static int get_char() |
---|
668 | { |
---|
669 | int status; |
---|
670 | char c; |
---|
671 | int tries=MAX_TIMEOUTS; |
---|
672 | |
---|
673 | while(tries) |
---|
674 | { |
---|
675 | status = read(ttyfd, &c, 1); |
---|
676 | switch (status) { |
---|
677 | case 1: |
---|
678 | return ((int)c & 0x7F); |
---|
679 | default: |
---|
680 | tries--; |
---|
681 | } |
---|
682 | } |
---|
683 | return -1; |
---|
684 | } |
---|
685 | |
---|
686 | static int put_char(c) |
---|
687 | int c; |
---|
688 | { |
---|
689 | char ch = c; |
---|
690 | |
---|
691 | write(ttyfd, &ch, 1); |
---|
692 | |
---|
693 | return 0; |
---|
694 | } |
---|
695 | |
---|
696 | static int write_char (c) |
---|
697 | int c; |
---|
698 | { |
---|
699 | if (put_char(c) < 0) { |
---|
700 | return (0); |
---|
701 | } |
---|
702 | return (1); |
---|
703 | } |
---|
704 | |
---|
705 | static int put_string (s) |
---|
706 | register char *s; |
---|
707 | { |
---|
708 | quiet = 0; |
---|
709 | s = clean(s, 1); |
---|
710 | while (*s) { |
---|
711 | register char c = *s++; |
---|
712 | |
---|
713 | if (c != '\\') { |
---|
714 | if (!write_char (c)) |
---|
715 | return 0; |
---|
716 | continue; |
---|
717 | } |
---|
718 | |
---|
719 | c = *s++; |
---|
720 | switch (c) { |
---|
721 | case 'd': |
---|
722 | sleep(1); |
---|
723 | break; |
---|
724 | |
---|
725 | case 'K': |
---|
726 | break_sequence(); |
---|
727 | break; |
---|
728 | |
---|
729 | case 'p': |
---|
730 | #if 0 /* FIXME!!! */ |
---|
731 | usleep(10000); /* 1/100th of a second (arg is microseconds) */ |
---|
732 | #else |
---|
733 | sleep(1); |
---|
734 | #endif |
---|
735 | break; |
---|
736 | |
---|
737 | default: |
---|
738 | if (!write_char (c)) |
---|
739 | return 0; |
---|
740 | break; |
---|
741 | } |
---|
742 | } |
---|
743 | |
---|
744 | return (1); |
---|
745 | } |
---|
746 | |
---|
747 | /* |
---|
748 | * 'Wait for' this string to appear on this file descriptor. |
---|
749 | */ |
---|
750 | static int get_string(string) |
---|
751 | register char *string; |
---|
752 | { |
---|
753 | int c, len, minlen; |
---|
754 | register char *s = temp2, *end = s + STR_LEN; |
---|
755 | char *logged = temp2; |
---|
756 | struct termios tios; |
---|
757 | |
---|
758 | memset(temp2, 0, sizeof(temp2)); |
---|
759 | |
---|
760 | tcgetattr(ttyfd, &tios); |
---|
761 | tios.c_cc[VMIN] = 0; |
---|
762 | tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS; |
---|
763 | tcsetattr(ttyfd, TCSANOW, &tios); |
---|
764 | |
---|
765 | string = clean(string, 0); |
---|
766 | len = strlen(string); |
---|
767 | minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1; |
---|
768 | |
---|
769 | if (len > STR_LEN) { |
---|
770 | exit_code = 1; |
---|
771 | return 0; |
---|
772 | } |
---|
773 | |
---|
774 | if (len == 0) { |
---|
775 | return (1); |
---|
776 | } |
---|
777 | |
---|
778 | while ( (c = get_char()) >= 0) { |
---|
779 | int n, abort_len; |
---|
780 | |
---|
781 | *s++ = c; |
---|
782 | *s=0; |
---|
783 | |
---|
784 | if (s - temp2 >= len && |
---|
785 | c == string[len - 1] && |
---|
786 | strncmp(s - len, string, len) == 0) { |
---|
787 | return (1); |
---|
788 | } |
---|
789 | |
---|
790 | for (n = 0; n < n_aborts; ++n) { |
---|
791 | if (s - temp2 >= (abort_len = strlen(abort_string[n])) && |
---|
792 | strncmp(s - abort_len, abort_string[n], abort_len) == 0) { |
---|
793 | |
---|
794 | exit_code = n + 4; |
---|
795 | strcpy(fail_reason = fail_buffer, abort_string[n]); |
---|
796 | return (0); |
---|
797 | } |
---|
798 | } |
---|
799 | |
---|
800 | if (s >= end) { |
---|
801 | if (logged < s - minlen) { |
---|
802 | logged = s; |
---|
803 | } |
---|
804 | s -= minlen; |
---|
805 | memmove(temp2, s, minlen); |
---|
806 | logged = temp2 + (logged - s); |
---|
807 | s = temp2 + minlen; |
---|
808 | } |
---|
809 | } |
---|
810 | |
---|
811 | exit_code = 3; |
---|
812 | return (0); |
---|
813 | } |
---|