📄 chat.c
字号:
} if (cur_chr != 0 || sending) { if (sending && (cur_chr == '\\' || cur_chr == 0)) *s1++ = '\\'; *s1++ = cur_chr; } break; } if (sending) *s1++ = '\\'; *s1++ = cur_chr; break; } } if (add_return) *s1++ = '\r'; *s1++ = '\0'; /* guarantee closure */ *s1++ = '\0'; /* terminate the string */ return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */}/* * A modified version of 'strtok'. This version skips \ sequences. */char *expect_strtok (s, term) char *s, *term;{ static char *str = ""; int escape_flag = 0; char *result;/* * If a string was specified then do initial processing. */ if (s) str = s;/* * If this is the escape flag then reset it and ignore the character. */ if (*str) result = str; else result = (char *) 0; while (*str) { if (escape_flag) { escape_flag = 0; ++str; continue; } if (*str == '\\') { ++str; escape_flag = 1; continue; }/* * If this is not in the termination string, continue. */ if (strchr (term, *str) == (char *) 0) { ++str; continue; }/* * This is the terminator. Mark the end of the string and stop. */ *str++ = '\0'; break; } return (result);}/* * Process the expect string */void chat_expect (s)char *s;{ char *expect; char *reply; if (strcmp(s, "HANGUP") == 0) { ++hup_next; return; } if (strcmp(s, "ABORT") == 0) { ++abort_next; return; } if (strcmp(s, "CLR_ABORT") == 0) { ++clear_abort_next; return; } if (strcmp(s, "REPORT") == 0) { ++report_next; return; } if (strcmp(s, "CLR_REPORT") == 0) { ++clear_report_next; return; } if (strcmp(s, "TIMEOUT") == 0) { ++timeout_next; return; } if (strcmp(s, "ECHO") == 0) { ++echo_next; return; } if (strcmp(s, "SAY") == 0) { ++say_next; return; }/* * Fetch the expect and reply string. */ for (;;) { expect = expect_strtok (s, "-"); s = (char *) 0; if (expect == (char *) 0) return; reply = expect_strtok (s, "-");/* * Handle the expect string. If successful then exit. */ if (get_string (expect)) return;/* * If there is a sub-reply string then send it. Otherwise any condition * is terminal. */ if (reply == (char *) 0 || exit_code != 3) break; chat_send (reply); }}#if 0/* * Translate the input character to the appropriate string for printing * the data. */static char *character(c)int c;{ static char string[10]; char *meta; meta = (c & 0x80) ? "M-" : ""; c &= 0x7F; if (c < 32) sprintf(string, "%s^%c", meta, (int)c + '@'); else if (c == 127) sprintf(string, "%s^?", meta); else sprintf(string, "%s%c", meta, c); return (string);}#endif/* * process the reply string */void chat_send (s)register char *s;{/* char file_data[STR_LEN]; */ if (say_next) { say_next = 0; s = clean(s, 1); write(2, s, strlen(s)); free(s); return; } if (hup_next) { hup_next = 0; return; } if (echo_next) { echo_next = 0; echo = (strcmp(s, "ON") == 0); return; } if (abort_next) { char *s1; abort_next = 0; if ( n_aborts < MAX_ABORTS ) { s1 = clean(s, 0); if (( strlen(s1) <= strlen(s) ) && ( strlen(s1) < sizeof(fail_buffer))) { abort_string[n_aborts++] = s1; } free(s1); } return; } if (clear_abort_next) { clear_abort_next = 0; return; } if (report_next) { report_next = 0; return; } if (clear_report_next) { clear_report_next = 0; return; } if (timeout_next) { timeout_next = 0; timeout = atoi(s); if (timeout <= 0){ timeout = DEFAULT_CHAT_TIMEOUT; } return; } if (strcmp(s, "EOT") == 0){ s = "^D\\c"; } else{ if (strcmp(s, "BREAK") == 0){ s = "\\K\\c"; } if (!put_string(s)) { exit_code = 2; } }}static int get_char(){ int status; char c; int tries=MAX_TIMEOUTS; while(tries) { status = read(ttyfd, &c, 1); switch (status) { case 1: return ((int)c & 0x7F); default: tries--; } } return -1; }static int put_char(c)int c;{ char ch = c; return(write(ttyfd, &ch, 1));}static int write_char (c)int c;{ if (put_char(c) < 1) { return (0); } return (1);}static int put_string (s)register char *s;{ char *out,*free_ptr=0; quiet = 0; out = free_ptr = clean(s, 1); while (*out) { register char c = *out++; if (c != '\\') { if (!write_char (c)){ free(free_ptr); return 0; } continue; } c = *out++; switch (c) { case 'd': sleep(1); break; case 'K': break_sequence(); break; case 'p':#if 0 /* FIXME!!! */ usleep(10000); /* 1/100th of a second (arg is microseconds) */#else sleep(1);#endif break; default: if (!write_char (c)){ free(free_ptr); return 0; } break; } } free(free_ptr); return (1);}/* * 'Wait for' this string to appear on this file descriptor. */static int get_string(in_string)register char *in_string;{ int c, len, minlen; register char *s = temp2, *end = s + STR_LEN; char *logged = temp2; char *string=0; struct termios tios; memset(temp2, 0, sizeof(temp2)); tcgetattr(ttyfd, &tios); tios.c_cc[VMIN] = 0; tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS; tcsetattr(ttyfd, TCSANOW, &tios); string = clean(in_string, 0); len = strlen(string); minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1; if (len > STR_LEN) { exit_code = 1; free(string); return 0; } if (len == 0) { return (1); } while ( (c = get_char()) >= 0) { int n, abort_len; if(c == '\n' || c == '\r'){ s = temp2; *s=0; } else{ *s++ = c; *s=0; } if (s - temp2 >= len && c == string[len - 1] && strncmp(s - len, string, len) == 0) { free(string); return (1); } for (n = 0; n < n_aborts; ++n) { if (s - temp2 >= (abort_len = strlen(abort_string[n])) && strncmp(s - abort_len, abort_string[n], abort_len) == 0) { exit_code = n + 4; strcpy(fail_reason = fail_buffer, abort_string[n]); free(string); return (0); } } if (s >= end) { if (logged < s - minlen) { logged = s; } s -= minlen; memmove(temp2, s, minlen); logged = temp2 + (logged - s); s = temp2 + minlen; } } exit_code = 3; free(string); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -