[d0950ad] | 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 | * ----------------- |
---|
[aee474b] | 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 | * |
---|
[d0950ad] | 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 | |
---|
[7220366a] | 85 | /* $Id$ */ |
---|
| 86 | |
---|
| 87 | /* |
---|
| 88 | Fixes and some Changes by Wilfried Busalski Lancier-Monitoring GmbH Germany |
---|
| 89 | wilfried.busalski@muenster.de |
---|
| 90 | |
---|
| 91 | Fixes: put_string() Free memory allocated by clean() |
---|
| 92 | get_string() Free memory allocated by clean() |
---|
| 93 | chat_main() Will Distroy's no more the chat-script |
---|
| 94 | getnextcommand() sepatator changed to '@' |
---|
| 95 | */ |
---|
| 96 | |
---|
[d0950ad] | 97 | #include <stdio.h> |
---|
| 98 | #include <ctype.h> |
---|
| 99 | #include <time.h> |
---|
| 100 | #include <fcntl.h> |
---|
| 101 | #include <errno.h> |
---|
| 102 | #include <string.h> |
---|
| 103 | #include <stdlib.h> |
---|
| 104 | #include <unistd.h> |
---|
| 105 | #include <sys/types.h> |
---|
| 106 | #include <sys/stat.h> |
---|
| 107 | #include <syslog.h> |
---|
[aee474b] | 108 | #include <termios.h> |
---|
| 109 | #include "pppd.h" |
---|
[d0950ad] | 110 | |
---|
| 111 | #undef TERMIOS |
---|
| 112 | #define TERMIOS |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | #define STR_LEN 1024 |
---|
| 116 | char temp2[STR_LEN]; |
---|
| 117 | |
---|
| 118 | #ifndef SIGTYPE |
---|
| 119 | #define SIGTYPE void |
---|
| 120 | #endif |
---|
| 121 | |
---|
| 122 | #undef __P |
---|
| 123 | #undef __V |
---|
| 124 | |
---|
| 125 | #ifdef __STDC__ |
---|
| 126 | #include <stdarg.h> |
---|
| 127 | #define __V(x) x |
---|
| 128 | #define __P(x) x |
---|
| 129 | #else |
---|
| 130 | #include <varargs.h> |
---|
| 131 | #define __V(x) (va_alist) va_dcl |
---|
| 132 | #define __P(x) () |
---|
| 133 | #define const |
---|
| 134 | #endif |
---|
| 135 | |
---|
| 136 | #ifndef O_NONBLOCK |
---|
| 137 | #define O_NONBLOCK O_NDELAY |
---|
| 138 | #endif |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | /*************** Micro getopt() *********************************************/ |
---|
| 142 | #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \ |
---|
| 143 | (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\ |
---|
| 144 | &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0)) |
---|
| 145 | #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \ |
---|
| 146 | (_O=4,(char*)0):(char*)0) |
---|
| 147 | #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0) |
---|
| 148 | #define ARG(c,v) (c?(--c,*v++):(char*)0) |
---|
| 149 | |
---|
| 150 | #if 0 |
---|
| 151 | static int _O = 0; /* Internal state */ |
---|
| 152 | #endif |
---|
| 153 | /*************** Micro getopt() *********************************************/ |
---|
| 154 | |
---|
[aee474b] | 155 | #define MAX_ABORTS 16 |
---|
| 156 | #define MAX_REPORTS 16 |
---|
[d0950ad] | 157 | #define DEFAULT_CHAT_TIMEOUT 45 |
---|
[aee474b] | 158 | #define MAX_TIMEOUTS 10 |
---|
| 159 | |
---|
[48cdb95a] | 160 | static int echo = 0; |
---|
| 161 | static int quiet = 0; |
---|
| 162 | static int use_env = 0; |
---|
| 163 | static int exit_code = 0; |
---|
| 164 | static char *phone_num = (char *) 0; |
---|
| 165 | static char *phone_num2 = (char *) 0; |
---|
[0286b9f] | 166 | static int ttyfd; |
---|
[aee474b] | 167 | static int timeout = DEFAULT_CHAT_TIMEOUT; |
---|
[d0950ad] | 168 | |
---|
| 169 | #ifdef TERMIOS |
---|
| 170 | #define term_parms struct termios |
---|
[aee474b] | 171 | #define get_term_param(param) tcgetattr(0, param) |
---|
| 172 | #define set_term_param(param) tcsetattr(0, TCSANOW, param) |
---|
[d0950ad] | 173 | struct termios saved_tty_parameters; |
---|
| 174 | #endif |
---|
| 175 | |
---|
[aee474b] | 176 | char *fail_reason = (char *)0; |
---|
| 177 | char fail_buffer[50]; |
---|
| 178 | char *abort_string[MAX_ABORTS]={"BUSY","NO DIALTONE","NO CARRIER","NO ANSWER","RING\r\nRING"}; |
---|
| 179 | int n_aborts = 5; |
---|
| 180 | int abort_next = 0, timeout_next = 0, echo_next = 0; |
---|
[d0950ad] | 181 | int clear_abort_next = 0; |
---|
| 182 | |
---|
| 183 | char *report_string[MAX_REPORTS] ; |
---|
| 184 | char report_buffer[50] ; |
---|
| 185 | int n_reports = 0, report_next = 0, report_gathering = 0 ; |
---|
| 186 | int clear_report_next = 0; |
---|
| 187 | |
---|
| 188 | int say_next = 0, hup_next = 0; |
---|
| 189 | |
---|
| 190 | void *dup_mem __P((void *b, size_t c)); |
---|
| 191 | void *copy_of __P((char *s)); |
---|
| 192 | void break_sequence __P((void)); |
---|
[f81fb809] | 193 | static int get_string __P((register char *string)); |
---|
| 194 | static int put_string __P((register char *s)); |
---|
| 195 | static int write_char __P((int c)); |
---|
| 196 | static int put_char __P((int c)); |
---|
| 197 | static int get_char __P((void)); |
---|
[d0950ad] | 198 | void chat_send __P((register char *s)); |
---|
[1a5787d] | 199 | /* static char *character __P((int c)); */ |
---|
[d0950ad] | 200 | void chat_expect __P((register char *s)); |
---|
[f81fb809] | 201 | static char *clean __P((register char *s, int sending)); |
---|
[d0950ad] | 202 | char *expect_strtok __P((char *, char *)); |
---|
[0286b9f] | 203 | int chatmain __P((int, int, char *)); |
---|
[d0950ad] | 204 | |
---|
| 205 | |
---|
| 206 | void *dup_mem(b, c) |
---|
| 207 | void *b; |
---|
| 208 | size_t c; |
---|
| 209 | { |
---|
| 210 | void *ans = malloc (c); |
---|
| 211 | if (!ans) |
---|
[aee474b] | 212 | return NULL; |
---|
[d0950ad] | 213 | |
---|
[aee474b] | 214 | memcpy(ans, b, c); |
---|
[d0950ad] | 215 | return ans; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | void *copy_of (s) |
---|
| 219 | char *s; |
---|
| 220 | { |
---|
[aee474b] | 221 | return dup_mem(s, strlen (s) + 1); |
---|
[d0950ad] | 222 | } |
---|
| 223 | |
---|
[7220366a] | 224 | char *getnextcommand(char *string,char *buff) |
---|
[d0950ad] | 225 | { |
---|
[7220366a] | 226 | char *token; |
---|
| 227 | int len; |
---|
| 228 | |
---|
| 229 | token=strchr(string,'@'); |
---|
| 230 | if (token==NULL){ |
---|
[d0950ad] | 231 | return NULL; |
---|
[7220366a] | 232 | } |
---|
| 233 | len=token-string; |
---|
| 234 | if(len > 78 ){ |
---|
| 235 | len=78; |
---|
| 236 | } |
---|
| 237 | memcpy(buff,string,len); |
---|
| 238 | buff[len]=0; |
---|
| 239 | return(token+1); |
---|
[d0950ad] | 240 | } |
---|
| 241 | |
---|
[0286b9f] | 242 | int chatmain(int fd, int mode, char *pScript) |
---|
[d0950ad] | 243 | { |
---|
[7220366a] | 244 | char arg[80]; |
---|
| 245 | char *script; |
---|
[2f1b930] | 246 | |
---|
| 247 | /* initialize exit code */ |
---|
| 248 | exit_code = 0; |
---|
[0286b9f] | 249 | ttyfd = fd; |
---|
[d0950ad] | 250 | |
---|
[7220366a] | 251 | script=pScript; |
---|
| 252 | |
---|
[aee474b] | 253 | if ( debug ) { |
---|
[7220366a] | 254 | dbglog("chat_main: %s\n", script); |
---|
[aee474b] | 255 | } |
---|
[2f1b930] | 256 | |
---|
| 257 | /* get first expect string */ |
---|
[7220366a] | 258 | script = getnextcommand(script,arg); |
---|
| 259 | while (( script != NULL ) && ( exit_code == 0 )) { |
---|
[2f1b930] | 260 | /* process the expect string */ |
---|
| 261 | chat_expect(arg); |
---|
[aee474b] | 262 | if ( exit_code == 0 ) { |
---|
| 263 | /* get the next send string */ |
---|
[7220366a] | 264 | script = getnextcommand(script,arg); |
---|
| 265 | if ( script != NULL ) { |
---|
[aee474b] | 266 | /* process the send string */ |
---|
| 267 | chat_send(arg); |
---|
| 268 | |
---|
| 269 | /* get the next expect string */ |
---|
[7220366a] | 270 | script = getnextcommand(script,arg); |
---|
[aee474b] | 271 | } |
---|
[2f1b930] | 272 | } |
---|
| 273 | } |
---|
[0286b9f] | 274 | ttyfd = (int)-1; |
---|
[d0950ad] | 275 | |
---|
[aee474b] | 276 | return ( exit_code ); |
---|
[d0950ad] | 277 | } |
---|
| 278 | |
---|
| 279 | void break_sequence() |
---|
| 280 | { |
---|
[aee474b] | 281 | tcsendbreak(ttyfd, 0); |
---|
[d0950ad] | 282 | } |
---|
| 283 | |
---|
| 284 | /* |
---|
| 285 | * 'Clean up' this string. |
---|
| 286 | */ |
---|
[f81fb809] | 287 | static char *clean(s, sending) |
---|
[d0950ad] | 288 | register char *s; |
---|
| 289 | int sending; /* set to 1 when sending (putting) this string. */ |
---|
| 290 | { |
---|
[aee474b] | 291 | char temp[STR_LEN], env_str[STR_LEN], cur_chr; |
---|
[d0950ad] | 292 | register char *s1, *phchar; |
---|
| 293 | int add_return = sending; |
---|
[aee474b] | 294 | #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7')) |
---|
| 295 | #define isalnumx(chr) ((((chr) >= '0') && ((chr) <= '9')) \ |
---|
| 296 | || (((chr) >= 'a') && ((chr) <= 'z')) \ |
---|
| 297 | || (((chr) >= 'A') && ((chr) <= 'Z')) \ |
---|
| 298 | || (chr) == '_') |
---|
[d0950ad] | 299 | |
---|
| 300 | s1 = temp; |
---|
| 301 | while (*s) { |
---|
| 302 | cur_chr = *s++; |
---|
| 303 | if (cur_chr == '^') { |
---|
| 304 | cur_chr = *s++; |
---|
| 305 | if (cur_chr == '\0') { |
---|
| 306 | *s1++ = '^'; |
---|
| 307 | break; |
---|
| 308 | } |
---|
| 309 | cur_chr &= 0x1F; |
---|
| 310 | if (cur_chr != 0) { |
---|
| 311 | *s1++ = cur_chr; |
---|
| 312 | } |
---|
| 313 | continue; |
---|
| 314 | } |
---|
[aee474b] | 315 | |
---|
| 316 | if (use_env && cur_chr == '$') { /* ARI */ |
---|
| 317 | phchar = env_str; |
---|
| 318 | while (isalnumx(*s)) |
---|
| 319 | *phchar++ = *s++; |
---|
| 320 | *phchar = '\0'; |
---|
| 321 | phchar = getenv(env_str); |
---|
| 322 | if (phchar) |
---|
| 323 | while (*phchar) |
---|
| 324 | *s1++ = *phchar++; |
---|
| 325 | continue; |
---|
| 326 | } |
---|
[d0950ad] | 327 | |
---|
| 328 | if (cur_chr != '\\') { |
---|
| 329 | *s1++ = cur_chr; |
---|
| 330 | continue; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | cur_chr = *s++; |
---|
| 334 | if (cur_chr == '\0') { |
---|
| 335 | if (sending) { |
---|
| 336 | *s1++ = '\\'; |
---|
| 337 | *s1++ = '\\'; |
---|
| 338 | } |
---|
| 339 | break; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | switch (cur_chr) { |
---|
| 343 | case 'b': |
---|
| 344 | *s1++ = '\b'; |
---|
| 345 | break; |
---|
| 346 | |
---|
| 347 | case 'c': |
---|
| 348 | if (sending && *s == '\0') |
---|
| 349 | add_return = 0; |
---|
| 350 | else |
---|
| 351 | *s1++ = cur_chr; |
---|
| 352 | break; |
---|
| 353 | |
---|
| 354 | case '\\': |
---|
| 355 | case 'K': |
---|
| 356 | case 'p': |
---|
| 357 | case 'd': |
---|
| 358 | if (sending) |
---|
| 359 | *s1++ = '\\'; |
---|
| 360 | *s1++ = cur_chr; |
---|
| 361 | break; |
---|
| 362 | |
---|
| 363 | case 'T': |
---|
| 364 | if (sending && phone_num) { |
---|
[aee474b] | 365 | for (phchar = phone_num; *phchar != '\0'; phchar++) |
---|
[d0950ad] | 366 | *s1++ = *phchar; |
---|
| 367 | } |
---|
| 368 | else { |
---|
| 369 | *s1++ = '\\'; |
---|
| 370 | *s1++ = 'T'; |
---|
| 371 | } |
---|
| 372 | break; |
---|
| 373 | |
---|
| 374 | case 'U': |
---|
| 375 | if (sending && phone_num2) { |
---|
[aee474b] | 376 | for (phchar = phone_num2; *phchar != '\0'; phchar++) |
---|
[d0950ad] | 377 | *s1++ = *phchar; |
---|
| 378 | } |
---|
| 379 | else { |
---|
| 380 | *s1++ = '\\'; |
---|
| 381 | *s1++ = 'U'; |
---|
| 382 | } |
---|
| 383 | break; |
---|
| 384 | |
---|
| 385 | case 'q': |
---|
| 386 | quiet = 1; |
---|
| 387 | break; |
---|
| 388 | |
---|
| 389 | case 'r': |
---|
| 390 | *s1++ = '\r'; |
---|
| 391 | break; |
---|
| 392 | |
---|
| 393 | case 'n': |
---|
| 394 | *s1++ = '\n'; |
---|
| 395 | break; |
---|
| 396 | |
---|
| 397 | case 's': |
---|
| 398 | *s1++ = ' '; |
---|
| 399 | break; |
---|
| 400 | |
---|
| 401 | case 't': |
---|
| 402 | *s1++ = '\t'; |
---|
| 403 | break; |
---|
| 404 | |
---|
| 405 | case 'N': |
---|
| 406 | if (sending) { |
---|
| 407 | *s1++ = '\\'; |
---|
| 408 | *s1++ = '\0'; |
---|
| 409 | } |
---|
| 410 | else |
---|
| 411 | *s1++ = 'N'; |
---|
| 412 | break; |
---|
| 413 | |
---|
[aee474b] | 414 | case '$': /* ARI */ |
---|
| 415 | if (use_env) { |
---|
| 416 | *s1++ = cur_chr; |
---|
| 417 | break; |
---|
| 418 | } |
---|
| 419 | /* FALL THROUGH */ |
---|
| 420 | |
---|
[d0950ad] | 421 | default: |
---|
| 422 | if (isoctal (cur_chr)) { |
---|
| 423 | cur_chr &= 0x07; |
---|
| 424 | if (isoctal (*s)) { |
---|
| 425 | cur_chr <<= 3; |
---|
| 426 | cur_chr |= *s++ - '0'; |
---|
| 427 | if (isoctal (*s)) { |
---|
| 428 | cur_chr <<= 3; |
---|
| 429 | cur_chr |= *s++ - '0'; |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if (cur_chr != 0 || sending) { |
---|
| 434 | if (sending && (cur_chr == '\\' || cur_chr == 0)) |
---|
| 435 | *s1++ = '\\'; |
---|
| 436 | *s1++ = cur_chr; |
---|
| 437 | } |
---|
| 438 | break; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | if (sending) |
---|
| 442 | *s1++ = '\\'; |
---|
| 443 | *s1++ = cur_chr; |
---|
| 444 | break; |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | if (add_return) |
---|
| 449 | *s1++ = '\r'; |
---|
| 450 | |
---|
| 451 | *s1++ = '\0'; /* guarantee closure */ |
---|
| 452 | *s1++ = '\0'; /* terminate the string */ |
---|
| 453 | return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */ |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | /* |
---|
| 457 | * A modified version of 'strtok'. This version skips \ sequences. |
---|
| 458 | */ |
---|
| 459 | char *expect_strtok (s, term) |
---|
| 460 | char *s, *term; |
---|
| 461 | { |
---|
| 462 | static char *str = ""; |
---|
| 463 | int escape_flag = 0; |
---|
| 464 | char *result; |
---|
| 465 | |
---|
| 466 | /* |
---|
| 467 | * If a string was specified then do initial processing. |
---|
| 468 | */ |
---|
| 469 | if (s) |
---|
| 470 | str = s; |
---|
| 471 | |
---|
| 472 | /* |
---|
| 473 | * If this is the escape flag then reset it and ignore the character. |
---|
| 474 | */ |
---|
| 475 | if (*str) |
---|
| 476 | result = str; |
---|
| 477 | else |
---|
| 478 | result = (char *) 0; |
---|
| 479 | |
---|
| 480 | while (*str) { |
---|
| 481 | if (escape_flag) { |
---|
| 482 | escape_flag = 0; |
---|
| 483 | ++str; |
---|
| 484 | continue; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | if (*str == '\\') { |
---|
| 488 | ++str; |
---|
| 489 | escape_flag = 1; |
---|
| 490 | continue; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | /* |
---|
| 494 | * If this is not in the termination string, continue. |
---|
| 495 | */ |
---|
| 496 | if (strchr (term, *str) == (char *) 0) { |
---|
| 497 | ++str; |
---|
| 498 | continue; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | /* |
---|
| 502 | * This is the terminator. Mark the end of the string and stop. |
---|
| 503 | */ |
---|
| 504 | *str++ = '\0'; |
---|
| 505 | break; |
---|
| 506 | } |
---|
| 507 | return (result); |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | /* |
---|
| 511 | * Process the expect string |
---|
| 512 | */ |
---|
[aee474b] | 513 | void chat_expect (s) |
---|
[d0950ad] | 514 | char *s; |
---|
| 515 | { |
---|
| 516 | char *expect; |
---|
| 517 | char *reply; |
---|
| 518 | |
---|
| 519 | if (strcmp(s, "HANGUP") == 0) { |
---|
[7220366a] | 520 | ++hup_next; |
---|
[aee474b] | 521 | return; |
---|
[d0950ad] | 522 | } |
---|
| 523 | |
---|
| 524 | if (strcmp(s, "ABORT") == 0) { |
---|
[7220366a] | 525 | ++abort_next; |
---|
| 526 | return; |
---|
[d0950ad] | 527 | } |
---|
| 528 | |
---|
| 529 | if (strcmp(s, "CLR_ABORT") == 0) { |
---|
[7220366a] | 530 | ++clear_abort_next; |
---|
| 531 | return; |
---|
[d0950ad] | 532 | } |
---|
| 533 | |
---|
| 534 | if (strcmp(s, "REPORT") == 0) { |
---|
[7220366a] | 535 | ++report_next; |
---|
| 536 | return; |
---|
[d0950ad] | 537 | } |
---|
| 538 | |
---|
| 539 | if (strcmp(s, "CLR_REPORT") == 0) { |
---|
[7220366a] | 540 | ++clear_report_next; |
---|
| 541 | return; |
---|
[d0950ad] | 542 | } |
---|
| 543 | |
---|
| 544 | if (strcmp(s, "TIMEOUT") == 0) { |
---|
[7220366a] | 545 | ++timeout_next; |
---|
| 546 | return; |
---|
[d0950ad] | 547 | } |
---|
| 548 | |
---|
| 549 | if (strcmp(s, "ECHO") == 0) { |
---|
[7220366a] | 550 | ++echo_next; |
---|
| 551 | return; |
---|
[d0950ad] | 552 | } |
---|
| 553 | |
---|
| 554 | if (strcmp(s, "SAY") == 0) { |
---|
[7220366a] | 555 | ++say_next; |
---|
| 556 | return; |
---|
[d0950ad] | 557 | } |
---|
| 558 | |
---|
| 559 | /* |
---|
| 560 | * Fetch the expect and reply string. |
---|
| 561 | */ |
---|
| 562 | for (;;) { |
---|
| 563 | expect = expect_strtok (s, "-"); |
---|
[aee474b] | 564 | s = (char *) 0; |
---|
[d0950ad] | 565 | |
---|
| 566 | if (expect == (char *) 0) |
---|
[aee474b] | 567 | return; |
---|
[d0950ad] | 568 | |
---|
| 569 | reply = expect_strtok (s, "-"); |
---|
| 570 | |
---|
| 571 | /* |
---|
| 572 | * Handle the expect string. If successful then exit. |
---|
| 573 | */ |
---|
| 574 | if (get_string (expect)) |
---|
| 575 | return; |
---|
| 576 | |
---|
| 577 | /* |
---|
| 578 | * If there is a sub-reply string then send it. Otherwise any condition |
---|
| 579 | * is terminal. |
---|
| 580 | */ |
---|
| 581 | if (reply == (char *) 0 || exit_code != 3) |
---|
| 582 | break; |
---|
| 583 | |
---|
| 584 | chat_send (reply); |
---|
| 585 | } |
---|
[aee474b] | 586 | } |
---|
[d0950ad] | 587 | |
---|
[1a5787d] | 588 | #if 0 |
---|
[d0950ad] | 589 | /* |
---|
[aee474b] | 590 | * Translate the input character to the appropriate string for printing |
---|
| 591 | * the data. |
---|
[d0950ad] | 592 | */ |
---|
[aee474b] | 593 | |
---|
[f81fb809] | 594 | static char *character(c) |
---|
[aee474b] | 595 | int c; |
---|
| 596 | { |
---|
| 597 | static char string[10]; |
---|
| 598 | char *meta; |
---|
| 599 | |
---|
| 600 | meta = (c & 0x80) ? "M-" : ""; |
---|
| 601 | c &= 0x7F; |
---|
| 602 | |
---|
| 603 | if (c < 32) |
---|
| 604 | sprintf(string, "%s^%c", meta, (int)c + '@'); |
---|
| 605 | else if (c == 127) |
---|
| 606 | sprintf(string, "%s^?", meta); |
---|
| 607 | else |
---|
| 608 | sprintf(string, "%s%c", meta, c); |
---|
| 609 | |
---|
| 610 | return (string); |
---|
[d0950ad] | 611 | } |
---|
[1a5787d] | 612 | #endif |
---|
[d0950ad] | 613 | |
---|
| 614 | /* |
---|
| 615 | * process the reply string |
---|
| 616 | */ |
---|
| 617 | void chat_send (s) |
---|
| 618 | register char *s; |
---|
| 619 | { |
---|
[cb1e8a46] | 620 | /* char file_data[STR_LEN]; */ |
---|
[aee474b] | 621 | |
---|
[d0950ad] | 622 | if (say_next) { |
---|
[7220366a] | 623 | say_next = 0; |
---|
| 624 | s = clean(s, 1); |
---|
| 625 | write(2, s, strlen(s)); |
---|
[d0950ad] | 626 | free(s); |
---|
[7220366a] | 627 | return; |
---|
[d0950ad] | 628 | } |
---|
| 629 | |
---|
| 630 | if (hup_next) { |
---|
| 631 | hup_next = 0; |
---|
[aee474b] | 632 | return; |
---|
[d0950ad] | 633 | } |
---|
| 634 | |
---|
| 635 | if (echo_next) { |
---|
| 636 | echo_next = 0; |
---|
| 637 | echo = (strcmp(s, "ON") == 0); |
---|
| 638 | return; |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | if (abort_next) { |
---|
[7220366a] | 642 | char *s1; |
---|
[d0950ad] | 643 | |
---|
[7220366a] | 644 | abort_next = 0; |
---|
| 645 | if ( n_aborts < MAX_ABORTS ) { |
---|
| 646 | s1 = clean(s, 0); |
---|
| 647 | if (( strlen(s1) <= strlen(s) ) && ( strlen(s1) < sizeof(fail_buffer))) { |
---|
| 648 | abort_string[n_aborts++] = s1; |
---|
| 649 | } |
---|
| 650 | free(s1); |
---|
| 651 | } |
---|
| 652 | return; |
---|
[aee474b] | 653 | } |
---|
[d0950ad] | 654 | |
---|
[aee474b] | 655 | if (clear_abort_next) { |
---|
[7220366a] | 656 | clear_abort_next = 0; |
---|
| 657 | return; |
---|
[aee474b] | 658 | } |
---|
| 659 | |
---|
| 660 | if (report_next) { |
---|
[7220366a] | 661 | report_next = 0; |
---|
| 662 | return; |
---|
[aee474b] | 663 | } |
---|
| 664 | |
---|
| 665 | if (clear_report_next) { |
---|
[7220366a] | 666 | clear_report_next = 0; |
---|
| 667 | return; |
---|
[d0950ad] | 668 | } |
---|
| 669 | |
---|
| 670 | if (timeout_next) { |
---|
[7220366a] | 671 | timeout_next = 0; |
---|
| 672 | timeout = atoi(s); |
---|
[d0950ad] | 673 | |
---|
[7220366a] | 674 | if (timeout <= 0){ |
---|
| 675 | timeout = DEFAULT_CHAT_TIMEOUT; |
---|
| 676 | } |
---|
| 677 | return; |
---|
[d0950ad] | 678 | } |
---|
[aee474b] | 679 | |
---|
[7220366a] | 680 | if (strcmp(s, "EOT") == 0){ |
---|
| 681 | s = "^D\\c"; |
---|
| 682 | } |
---|
| 683 | else{ |
---|
| 684 | if (strcmp(s, "BREAK") == 0){ |
---|
| 685 | s = "\\K\\c"; |
---|
| 686 | } |
---|
[d0950ad] | 687 | |
---|
[7220366a] | 688 | if (!put_string(s)) { |
---|
| 689 | exit_code = 2; |
---|
| 690 | } |
---|
| 691 | } |
---|
[d0950ad] | 692 | } |
---|
| 693 | |
---|
[f81fb809] | 694 | static int get_char() |
---|
[d0950ad] | 695 | { |
---|
| 696 | int status; |
---|
| 697 | char c; |
---|
| 698 | int tries=MAX_TIMEOUTS; |
---|
| 699 | |
---|
| 700 | while(tries) |
---|
| 701 | { |
---|
[2f1b930] | 702 | status = read(ttyfd, &c, 1); |
---|
[d0950ad] | 703 | switch (status) { |
---|
| 704 | case 1: |
---|
| 705 | return ((int)c & 0x7F); |
---|
| 706 | default: |
---|
| 707 | tries--; |
---|
| 708 | } |
---|
| 709 | } |
---|
| 710 | return -1; |
---|
| 711 | } |
---|
| 712 | |
---|
[f81fb809] | 713 | static int put_char(c) |
---|
[d0950ad] | 714 | int c; |
---|
| 715 | { |
---|
[aee474b] | 716 | char ch = c; |
---|
[d0950ad] | 717 | |
---|
[7220366a] | 718 | return(write(ttyfd, &ch, 1)); |
---|
[d0950ad] | 719 | } |
---|
| 720 | |
---|
[f81fb809] | 721 | static int write_char (c) |
---|
[d0950ad] | 722 | int c; |
---|
| 723 | { |
---|
[7220366a] | 724 | if (put_char(c) < 1) { |
---|
| 725 | return (0); |
---|
[d0950ad] | 726 | } |
---|
| 727 | return (1); |
---|
| 728 | } |
---|
| 729 | |
---|
[f81fb809] | 730 | static int put_string (s) |
---|
[d0950ad] | 731 | register char *s; |
---|
| 732 | { |
---|
[7220366a] | 733 | char *out,*free_ptr=0; |
---|
| 734 | |
---|
[d0950ad] | 735 | quiet = 0; |
---|
[7220366a] | 736 | out = free_ptr = clean(s, 1); |
---|
| 737 | while (*out) { |
---|
| 738 | register char c = *out++; |
---|
| 739 | |
---|
| 740 | if (c != '\\') { |
---|
| 741 | if (!write_char (c)){ |
---|
| 742 | free(free_ptr); |
---|
| 743 | return 0; |
---|
| 744 | } |
---|
| 745 | continue; |
---|
| 746 | } |
---|
[d0950ad] | 747 | |
---|
[7220366a] | 748 | c = *out++; |
---|
[d0950ad] | 749 | |
---|
[7220366a] | 750 | switch (c) { |
---|
| 751 | case 'd': |
---|
| 752 | sleep(1); |
---|
| 753 | break; |
---|
[d0950ad] | 754 | |
---|
[7220366a] | 755 | case 'K': |
---|
| 756 | break_sequence(); |
---|
| 757 | break; |
---|
[d0950ad] | 758 | |
---|
[7220366a] | 759 | case 'p': |
---|
[a6abd67] | 760 | #if 0 /* FIXME!!! */ |
---|
[7220366a] | 761 | usleep(10000); /* 1/100th of a second (arg is microseconds) */ |
---|
[a6abd67] | 762 | #else |
---|
[7220366a] | 763 | sleep(1); |
---|
[a6abd67] | 764 | #endif |
---|
[7220366a] | 765 | break; |
---|
| 766 | |
---|
| 767 | default: |
---|
| 768 | if (!write_char (c)){ |
---|
| 769 | free(free_ptr); |
---|
| 770 | return 0; |
---|
| 771 | } |
---|
| 772 | break; |
---|
| 773 | } |
---|
[d0950ad] | 774 | } |
---|
[7220366a] | 775 | free(free_ptr); |
---|
[d0950ad] | 776 | |
---|
| 777 | return (1); |
---|
| 778 | } |
---|
| 779 | |
---|
| 780 | /* |
---|
| 781 | * 'Wait for' this string to appear on this file descriptor. |
---|
| 782 | */ |
---|
[7220366a] | 783 | static int get_string(in_string) |
---|
| 784 | register char *in_string; |
---|
[d0950ad] | 785 | { |
---|
| 786 | int c, len, minlen; |
---|
| 787 | register char *s = temp2, *end = s + STR_LEN; |
---|
| 788 | char *logged = temp2; |
---|
[7220366a] | 789 | char *string=0; |
---|
[d0950ad] | 790 | struct termios tios; |
---|
| 791 | |
---|
[aee474b] | 792 | memset(temp2, 0, sizeof(temp2)); |
---|
| 793 | |
---|
[2f1b930] | 794 | tcgetattr(ttyfd, &tios); |
---|
[d0950ad] | 795 | tios.c_cc[VMIN] = 0; |
---|
| 796 | tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS; |
---|
[2f1b930] | 797 | tcsetattr(ttyfd, TCSANOW, &tios); |
---|
[d0950ad] | 798 | |
---|
[7220366a] | 799 | string = clean(in_string, 0); |
---|
[d0950ad] | 800 | len = strlen(string); |
---|
| 801 | minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1; |
---|
| 802 | |
---|
| 803 | if (len > STR_LEN) { |
---|
[7220366a] | 804 | exit_code = 1; |
---|
| 805 | free(string); |
---|
| 806 | return 0; |
---|
[d0950ad] | 807 | } |
---|
| 808 | |
---|
| 809 | if (len == 0) { |
---|
| 810 | return (1); |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | while ( (c = get_char()) >= 0) { |
---|
| 814 | int n, abort_len; |
---|
| 815 | |
---|
[7220366a] | 816 | if(c == '\n' || c == '\r'){ |
---|
| 817 | s = temp2; |
---|
| 818 | *s=0; |
---|
| 819 | } |
---|
| 820 | else{ |
---|
| 821 | *s++ = c; |
---|
| 822 | *s=0; |
---|
| 823 | } |
---|
[aee474b] | 824 | |
---|
[d0950ad] | 825 | if (s - temp2 >= len && |
---|
| 826 | c == string[len - 1] && |
---|
| 827 | strncmp(s - len, string, len) == 0) { |
---|
[7220366a] | 828 | free(string); |
---|
[d0950ad] | 829 | return (1); |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | for (n = 0; n < n_aborts; ++n) { |
---|
| 833 | if (s - temp2 >= (abort_len = strlen(abort_string[n])) && |
---|
| 834 | strncmp(s - abort_len, abort_string[n], abort_len) == 0) { |
---|
| 835 | |
---|
| 836 | exit_code = n + 4; |
---|
| 837 | strcpy(fail_reason = fail_buffer, abort_string[n]); |
---|
[7220366a] | 838 | free(string); |
---|
[d0950ad] | 839 | return (0); |
---|
| 840 | } |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | if (s >= end) { |
---|
| 844 | if (logged < s - minlen) { |
---|
| 845 | logged = s; |
---|
| 846 | } |
---|
| 847 | s -= minlen; |
---|
| 848 | memmove(temp2, s, minlen); |
---|
| 849 | logged = temp2 + (logged - s); |
---|
| 850 | s = temp2 + minlen; |
---|
| 851 | } |
---|
| 852 | } |
---|
[aee474b] | 853 | |
---|
[d0950ad] | 854 | exit_code = 3; |
---|
[7220366a] | 855 | free(string); |
---|
[d0950ad] | 856 | return (0); |
---|
| 857 | } |
---|
[7220366a] | 858 | |
---|