X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=chat%2Fchat.c;h=c9e01e2572851a56f51317a39a1c2cb93c686272;hb=a3630de20e34796f434a728bfd9cf1a961380c82;hp=5ce1336e381329afb811bb71e209efd840ade535;hpb=084ed8efcf30963586ac5823711f9e040b8e70ce;p=ppp.git diff --git a/chat/chat.c b/chat/chat.c index 5ce1336..c9e01e2 100644 --- a/chat/chat.c +++ b/chat/chat.c @@ -14,6 +14,9 @@ * This software is in the public domain. * * ----------------- + * 12-May-99 added a feature to read data to be sent from a file, + * if the send string starts with @. Idea from gpk . + * * added -T and -U option and \T and \U substitution to pass a phone * number into chat script. Two are needed for some ISDN TA applications. * Keith Dart @@ -74,11 +77,14 @@ * Columbus, OH 43221 * (614)451-1883 * - * */ +#ifndef __STDC__ +#define const +#endif + #ifndef lint -static char rcsid[] = "$Id: chat.c,v 1.20 1999/03/31 12:28:16 paulus Exp $"; +static const char rcsid[] = "$Id: chat.c,v 1.24 1999/08/13 06:46:09 paulus Exp $"; #endif #include @@ -612,26 +618,26 @@ int status; exit(status); terminating = 1; echo_stderr(-1); - if (report_file != (char *) 0 && report_fp != (FILE *) NULL) { /* * Allow the last of the report string to be gathered before we terminate. */ - if (report_gathering) { - int c, rep_len; + 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); + 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); @@ -971,6 +977,8 @@ int c; void chat_send (s) register char *s; { + char file_data[STR_LEN]; + if (say_next) { say_next = 0; s = clean(s,0); @@ -1109,6 +1117,43 @@ register char *s; 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) + break; + n += nr; + } + fclose(f); + + /* use the string we got as the string to send, + but trim off the final newline if any. */ + if (n > 0 && file_data[n-1] == '\n') + --n; + file_data[n] = 0; + s = file_data; + } + } + if (strcmp(s, "EOT") == 0) s = "^D\\c"; else if (strcmp(s, "BREAK") == 0)