--- src/usr.bin/chat/chat.c 2004/12/31 19:53:51 1.7 +++ src/usr.bin/chat/chat.c 2004/12/31 21:16:09 1.8 @@ -75,34 +75,25 @@ * (614)451-1883 * * - * $FreeBSD: src/usr.bin/chat/chat.c,v 1.19 2002/08/25 13:22:33 charnier Exp $ + * $FreeBSD: src/usr.bin/chat/chat.c,v 1.20 2003/08/22 17:47:40 markm Exp $ * $DragonFly$ */ -#include +#include +#include +#include #include -#include +#include #include #include -#include -#include +#include +#include #include -#include -#include -#include +#include #include - -#ifndef TERMIO -#undef TERMIOS -#define TERMIOS -#endif - -#ifdef TERMIO -#include -#endif -#ifdef TERMIOS #include -#endif +#include +#include #define STR_LEN 1024 @@ -110,36 +101,16 @@ #define SIGTYPE void #endif -#undef __V - -#ifdef __STDC__ -#include -#define __V(x) x -#else -#include -#define __V(x) (va_alist) va_dcl -#define const -#endif - #ifndef O_NONBLOCK #define O_NONBLOCK O_NDELAY #endif -#ifdef SUNOS -extern int sys_nerr; -extern char *sys_errlist[]; -#define memmove(to, from, n) bcopy(from, to, n) -#define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\ - "unknown error") -#endif - /*************** Micro getopt() *********************************************/ #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \ (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\ &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0)) #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \ (_O=4,(char*)0):(char*)0) -#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0) #define ARG(c,v) (c?(--c,*v++):(char*)0) static int _O = 0; /* Internal state */ @@ -155,7 +126,6 @@ int to_log = 1; int to_stderr = 0; int Verbose = 0; int quiet = 0; -int report = 0; int exit_code = 0; FILE* report_fp = (FILE *) 0; char *report_file = (char *) 0; @@ -164,21 +134,14 @@ char *phone_num = (char *) 0; char *phone_num2 = (char *) 0; int timeout = DEFAULT_CHAT_TIMEOUT; -int have_tty_parameters = 0; +static char blank[] = ""; -#ifdef TERMIO -#define term_parms struct termio -#define get_term_param(param) ioctl(0, TCGETA, param) -#define set_term_param(param) ioctl(0, TCSETA, param) -struct termio saved_tty_parameters; -#endif +int have_tty_parameters = 0; -#ifdef TERMIOS #define term_parms struct termios #define get_term_param(param) tcgetattr(0, param) #define set_term_param(param) tcsetattr(0, TCSANOW, param) struct termios saved_tty_parameters; -#endif char *abort_string[MAX_ABORTS], *fail_reason = (char *)0, fail_buffer[50]; @@ -201,31 +164,27 @@ SIGTYPE sigalrm(int signo); SIGTYPE sigint(int signo); SIGTYPE sigterm(int signo); SIGTYPE sighup(int signo); -void unalarm(void); void init(void); void set_tty_parameters(void); void echo_stderr(int); void break_sequence(void); void terminate(int status); -void do_file(char *chat_file); -int get_string(register char *string); -int put_string(register char *s); +void do_file(char *chatfile); +int get_string(char *string); +int put_string(char *s); int write_char(int c); int put_char(int c); int get_char(void); -void chat_send(register char *s); +void chat_send(char *s); char *character(int c); -void chat_expect(register char *s); -char *clean(register char *s, int sending); -void break_sequence(void); -void terminate(int status); +void chat_expect(char *s); +char *clean(char *s, int sending); void pack_array(char **array, int end); -char *expect_strtok(char *, char *); +char *expect_strtok(char *, const char *); int vfmtmsg(char *, int, const char *, va_list); /* vsprintf++ */ -void *dup_mem(b, c) -void *b; -size_t c; +void * +dup_mem(void *b, size_t c) { void *ans = malloc (c); if (!ans) @@ -235,7 +194,8 @@ size_t c; return ans; } -void *copy_of (char *s) +void * +copy_of(char *s) { return dup_mem (s, strlen (s) + 1); } @@ -248,7 +208,7 @@ void *copy_of (char *s) * Perform a UUCP-dialer-like chat script on stdin and stdout. */ int -main(int argc, char **argv) +main(int argc, char *argv[]) { int option; char *arg; @@ -302,7 +262,6 @@ main(int argc, char **argv) if (verbose) fprintf (report_fp, "Opening \"%s\"...\n", report_file); - report = 1; } } break; @@ -333,16 +292,12 @@ main(int argc, char **argv) report_fp = stderr; if (to_log) { -#ifdef ultrix - openlog("chat", LOG_PID); -#else openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2); if (verbose) setlogmask(LOG_UPTO(LOG_INFO)); else setlogmask(LOG_UPTO(LOG_WARNING)); -#endif } init(); @@ -370,16 +325,17 @@ main(int argc, char **argv) * Process a chat script when read from a file. */ -void do_file (char *chat_file) +void +do_file(char *chatfile) { int linect, sendflg; char *sp, *arg, quote; char buf [STR_LEN]; FILE *cfp; - cfp = fopen (chat_file, "r"); + cfp = fopen (chatfile, "r"); if (cfp == NULL) - fatal(1, "%s -- open failed: %m", chat_file); + fatal(1, "%s -- open failed: %m", chatfile); linect = 0; sendflg = 0; @@ -452,12 +408,12 @@ char line[1024]; /* * Send a message to syslog and/or stderr. */ -void logf __V((const char *fmt, ...)) +void +logf(const char *fmt, ...) { va_list args; va_start(args, fmt); - vfmtmsg(line, sizeof(line), fmt, args); if (to_log) syslog(LOG_INFO, "%s", line); @@ -469,12 +425,12 @@ void logf __V((const char *fmt, ...)) * Print an error message and terminate. */ -void fatal __V((int code, const char *fmt, ...)) +void +fatal(int code, const char *fmt, ...) { va_list args; va_start(args, fmt); - vfmtmsg(line, sizeof(line), fmt, args); if (to_log) syslog(LOG_ERR, "%s", line); @@ -485,7 +441,7 @@ void fatal __V((int code, const char *fm int alarmed = 0; -SIGTYPE sigalrm(int signo) +SIGTYPE sigalrm(int signo __unused) { int flags; @@ -503,28 +459,17 @@ SIGTYPE sigalrm(int signo) logf("alarm"); } -void unalarm(void) -{ - int flags; - - if ((flags = fcntl(0, F_GETFL, 0)) == -1) - fatal(2, "Can't get file mode flags on stdin: %m"); - - if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1) - fatal(2, "Can't set file mode flags on stdin: %m"); -} - -SIGTYPE sigint(int signo) +SIGTYPE sigint(int signo __unused) { fatal(2, "SIGINT"); } -SIGTYPE sigterm(int signo) +SIGTYPE sigterm(int signo __unused) { fatal(2, "SIGTERM"); } -SIGTYPE sighup(int signo) +SIGTYPE sighup(int signo __unused) { fatal(2, "SIGHUP"); } @@ -567,9 +512,7 @@ void set_tty_parameters(void) void break_sequence(void) { -#ifdef TERMIOS tcsendbreak (0, 0); -#endif } void terminate(int status) @@ -580,7 +523,8 @@ void terminate(int status) * Allow the last of the report string to be gathered before we terminate. */ if (report_gathering) { - int c, rep_len; + int c; + size_t rep_len; rep_len = strlen(report_buffer); while (rep_len + 1 <= sizeof(report_buffer)) { @@ -614,17 +558,17 @@ void terminate(int status) /* * 'Clean up' this string. */ -/* sending: set to 1 when sending (putting) this string. */ -char *clean(register char *s, int sending) +char * +clean(char *s, int sending) { char temp[STR_LEN], cur_chr; - register char *s1, *phchar; + char *s1, *phchar; int add_return = sending; #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7')) s1 = temp; /* Don't overflow buffer, leave room for chars we append later */ - while (*s && s1 - temp < sizeof(temp) - 2 - add_return) { + while (*s && s1 - temp < (off_t)(sizeof(temp) - 2 - add_return)) { cur_chr = *s++; if (cur_chr == '^') { cur_chr = *s++; @@ -765,9 +709,10 @@ char *clean(register char *s, int sendin * A modified version of 'strtok'. This version skips \ sequences. */ -char *expect_strtok (char *s, char *term) +char * +expect_strtok (char *s, const char *term) { - static char *str = ""; + static char *str = blank; int escape_flag = 0; char *result; @@ -819,7 +764,8 @@ char *expect_strtok (char *s, char *term * Process the expect string */ -void chat_expect (char *s) +void +chat_expect(char *s) { char *expect; char *reply; @@ -907,10 +853,11 @@ void chat_expect (char *s) * the data. */ -char *character(int c) +char * +character(int c) { static char string[10]; - char *meta; + const char *meta; meta = (c & 0x80) ? "M-" : ""; c &= 0x7F; @@ -928,7 +875,8 @@ char *character(int c) /* * process the reply string */ -void chat_send (register char *s) +void +chat_send(char *s) { if (say_next) { say_next = 0; @@ -1069,15 +1017,16 @@ void chat_send (register char *s) } if (strcmp(s, "EOT") == 0) - s = "^D\\c"; + s = strdup("^D\\c"); else if (strcmp(s, "BREAK") == 0) - s = "\\K\\c"; + s = strdup("\\K\\c"); if (!put_string(s)) fatal(1, "Failed"); } -int get_char(void) +int +get_char(void) { int status; char c; @@ -1129,7 +1078,8 @@ int put_char(int c) } } -int write_char (int c) +int +write_char(int c) { if (alarmed || put_char(c) < 0) { alarm(0); @@ -1146,22 +1096,19 @@ int write_char (int c) return (1); } -int put_string (register char *s) +int +put_string(char *s) { quiet = 0; s = clean(s, 1); - if (verbose) { - if (quiet) - logf("send (??????)"); - else - logf("send (%v)", s); - } + if (verbose) + logf("send (%v)", quiet ? "??????" : s); alarm(timeout); alarmed = 0; while (*s) { - register char c = *s++; + char c = *s++; if (c != '\\') { if (!write_char (c)) @@ -1200,7 +1147,8 @@ int put_string (register char *s) * When called with -1, a '\n' character is generated when * the cursor is not at the beginning of a line. */ -void echo_stderr(int n) +void +echo_stderr(int n) { static int need_lf; char *s; @@ -1227,11 +1175,13 @@ void echo_stderr(int n) /* * 'Wait for' this string to appear on this file descriptor. */ -int get_string(register char *string) +int +get_string(char *string) { char temp[STR_LEN]; - int c, printed = 0, len, minlen; - register char *s = temp, *end = s + STR_LEN; + int c, printed = 0; + size_t len, minlen; + char *s = temp, *end = s + STR_LEN; char *logged = temp; fail_reason = (char *)0; @@ -1314,7 +1264,7 @@ int get_string(register char *string) } } - if (s - temp >= len && + if ((size_t)(s - temp) >= len && c == string[len - 1] && strncmp(s - len, string, len) == 0) { if (verbose) { @@ -1374,45 +1324,8 @@ int get_string(register char *string) return (0); } -/* - * Gross kludge to handle Solaris versions >= 2.6 having usleep. - */ -#ifdef SOL2 -#include -#if MAXUID > 65536 /* then this is Solaris 2.6 or later */ -#undef NO_USLEEP -#endif -#endif /* SOL2 */ - -#ifdef NO_USLEEP -#include -#include - -/* - usleep -- support routine for 4.2BSD system call emulations - last edit: 29-Oct-1984 D A Gwyn - */ - -extern int select(); - -/* usec: delay in microseconds */ -int -usleep( long usec ) /* returns 0 if ok, else -1 */ -{ - static struct { /* `timeval' */ - long tv_sec; /* seconds */ - long tv_usec; /* microsecs */ - } delay; /* _select() timeout */ - - delay.tv_sec = usec / 1000000L; - delay.tv_usec = usec % 1000000L; - - return select(0, (long *)0, (long *)0, (long *)0, &delay); -} -#endif - void -pack_array (char **array, int end) +pack_array(char **array, int end) { int i, j;