📄 chat.c
字号:
}void set_tty_parameters(){#if defined(get_term_param) term_parms t; if (get_term_param (&t) < 0) fatal(2, "Can't get terminal parameters: %m"); saved_tty_parameters = t; have_tty_parameters = 1; t.c_iflag |= IGNBRK | ISTRIP | IGNPAR; t.c_oflag = 0; t.c_lflag = 0; t.c_cc[VERASE] = t.c_cc[VKILL] = 0; t.c_cc[VMIN] = 1; t.c_cc[VTIME] = 0; if (set_term_param (&t) < 0) fatal(2, "Can't set terminal parameters: %m");#endif}void break_sequence(){#ifdef TERMIOS tcsendbreak (0, 0);#endif}void terminate(status)int status;{ static int terminating = 0; if (terminating) exit(status); terminating = 1; echo_stderr(-1);/* * Allow the last of the report string to be gathered before we terminate. */ if (report_gathering) { int c, rep_len; rep_len = strlen(report_buffer); while (rep_len + 1 <= sizeof(report_buffer)) { alarm(1); c = get_char(); alarm(0); if (c < 0 || iscntrl(c)) break; report_buffer[rep_len] = c; ++rep_len; } report_buffer[rep_len] = 0; fprintf (report_fp, "chat: %s\n", report_buffer); } if (report_file != (char *) 0 && report_fp != (FILE *) NULL) { if (verbose) fprintf (report_fp, "Closing \"%s\".\n", report_file); fclose (report_fp); report_fp = (FILE *) NULL; }#if defined(get_term_param) if (have_tty_parameters) { if (set_term_param (&saved_tty_parameters) < 0) fatal(2, "Can't restore terminal parameters: %m"); }#endif exit(status);}/* * 'Clean up' this string. */char *clean(s, sending)register char *s;int sending; /* set to 1 when sending (putting) this string. */{ char temp[STR_LEN], env_str[STR_LEN], cur_chr; register char *s1, *phchar; int add_return = sending;#define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))#define isalnumx(chr) ((((chr) >= '0') && ((chr) <= '9')) \ || (((chr) >= 'a') && ((chr) <= 'z')) \ || (((chr) >= 'A') && ((chr) <= 'Z')) \ || (chr) == '_') s1 = temp; while (*s) { cur_chr = *s++; if (cur_chr == '^') { cur_chr = *s++; if (cur_chr == '\0') { *s1++ = '^'; break; } cur_chr &= 0x1F; if (cur_chr != 0) { *s1++ = cur_chr; } continue; } if (use_env && cur_chr == '$') { /* ARI */ phchar = env_str; while (isalnumx(*s)) *phchar++ = *s++; *phchar = '\0'; phchar = getenv(env_str); if (phchar) while (*phchar) *s1++ = *phchar++; continue; } if (cur_chr != '\\') { *s1++ = cur_chr; continue; } cur_chr = *s++; if (cur_chr == '\0') { if (sending) { *s1++ = '\\'; *s1++ = '\\'; } break; } switch (cur_chr) { case 'b': *s1++ = '\b'; break; case 'c': if (sending && *s == '\0') add_return = 0; else *s1++ = cur_chr; break; case '\\': case 'K': case 'p': case 'd': if (sending) *s1++ = '\\'; *s1++ = cur_chr; break; case 'T': if (sending && phone_num) { for (phchar = phone_num; *phchar != '\0'; phchar++) *s1++ = *phchar; } else { *s1++ = '\\'; *s1++ = 'T'; } break; case 'U': if (sending && phone_num2) { for (phchar = phone_num2; *phchar != '\0'; phchar++) *s1++ = *phchar; } else { *s1++ = '\\'; *s1++ = 'U'; } break; case 'q': quiet = 1; break; case 'r': *s1++ = '\r'; break; case 'n': *s1++ = '\n'; break; case 's': *s1++ = ' '; break; case 't': *s1++ = '\t'; break; case 'N': if (sending) { *s1++ = '\\'; *s1++ = '\0'; } else *s1++ = 'N'; break; case '$': /* ARI */ if (use_env) { *s1++ = cur_chr; break; } /* FALL THROUGH */ default: if (isoctal (cur_chr)) { cur_chr &= 0x07; if (isoctal (*s)) { cur_chr <<= 3; cur_chr |= *s++ - '0'; if (isoctal (*s)) { cur_chr <<= 3; cur_chr |= *s++ - '0'; } } 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); }/* * The expectation did not occur. This is terminal. */ if (fail_reason) logf("Failed (%s)", fail_reason); else logf("Failed here"); terminate(exit_code);}/* * Translate the input character to the appropriate string for printing * the data. */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);}/* * 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; if (strcmp(s, "OFF") == 0) signal(SIGHUP, SIG_IGN); else signal(SIGHUP, sighup); 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) fatal(2, "Too many ABORT strings"); s1 = clean(s, 0); if (strlen(s1) > strlen(s) || strlen(s1) + 1 > sizeof(fail_buffer)) fatal(1, "Illegal or too-long ABORT string ('%v')", s); abort_string[n_aborts++] = s1; if (verbose) logf("abort on (%v)", s); return; } if (clear_abort_next) { char *s1; int i; int old_max; int pack = 0; clear_abort_next = 0; s1 = clean(s, 0); if (strlen(s1) > strlen(s) || strlen(s1) + 1 > sizeof(fail_buffer)) fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s); old_max = n_aborts; for (i=0; i < n_aborts; i++) { if ( strcmp(s1,abort_string[i]) == 0 ) { free(abort_string[i]); abort_string[i] = NULL; pack++; n_aborts--; if (verbose) logf("clear abort on (%v)", s); } } free(s1); if (pack) pack_array(abort_string,old_max); return; } if (report_next) { char *s1; report_next = 0; if (n_reports >= MAX_REPORTS) fatal(2, "Too many REPORT strings"); s1 = clean(s, 0); if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1) fatal(1, "Illegal or too-long REPORT string ('%v')", s); report_string[n_reports++] = s1; if (verbose) logf("report (%v)", s); return; } if (clear_report_next) { char *s1; int i; int old_max; int pack = 0; clear_report_next = 0; s1 = clean(s, 0); if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1) fatal(1, "Illegal or too-long REPORT string ('%v')", s); old_max = n_reports; for (i=0; i < n_reports; i++) { if ( strcmp(s1,report_string[i]) == 0 ) { free(report_string[i]); report_string[i] = NULL; pack++; n_reports--; if (verbose) logf("clear report (%v)", s); } } free(s1); if (pack) pack_array(report_string,old_max); return; } if (timeout_next) { timeout_next = 0; timeout = atoi(s); if (timeout <= 0) timeout = DEFAULT_CHAT_TIMEOUT; if (verbose) logf("timeout set to %d seconds", timeout); return; } /* * The syntax @filename means read the string to send from the * file `filename'. */ if (s[0] == '@') { /* skip the @ and any following white-space */ char *fn = s; while (*++fn == ' ' || *fn == '\t') ; if (*fn != 0) { FILE *f; int n = 0; /* open the file and read until STR_LEN-1 bytes or end-of-file */ f = fopen(fn, "r"); if (f == NULL) fatal(1, "%s -- open failed: %m", fn); while (n < STR_LEN - 1) { int nr = fread(&file_data[n], 1, STR_LEN - 1 - n, f); if (nr < 0) fatal(1, "%s -- read error", fn); if (nr == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -