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