X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=chat%2Fchat.c;h=b8bbda8e02763f399e2ca8a3c2c7deaf92903515;hp=6c614d49dbb12922fd3388cea21feff6c702c2f9;hb=680642d0a93d312462abd8cb2bf3ac86cb9c1cd7;hpb=f9e81f0e97fc9a4258ee0fada70a008adf781a26 diff --git a/chat/chat.c b/chat/chat.c index 6c614d4..b8bbda8 100644 --- a/chat/chat.c +++ b/chat/chat.c @@ -18,7 +18,7 @@ * (614)451-1883 */ -static char rcsid[] = "$Id: chat.c,v 1.4 1994/05/30 00:30:37 paulus Exp $"; +static char rcsid[] = "$Id: chat.c,v 1.8 1995/05/19 04:19:29 paulus Exp $"; #include #include @@ -35,14 +35,6 @@ static char rcsid[] = "$Id: chat.c,v 1.4 1994/05/30 00:30:37 paulus Exp $"; #define TERMIOS #endif -#ifdef sun -# if defined(SUNOS) && SUNOS >= 41 -# ifndef HDB -# define HDB -# endif -# endif -#endif - #ifdef TERMIO #include #endif @@ -64,6 +56,10 @@ static char rcsid[] = "$Id: chat.c,v 1.4 1994/05/30 00:30:37 paulus Exp $"; #define const #endif +#ifndef O_NONBLOCK +#define O_NONBLOCK FNDELAY +#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=='-'\ @@ -78,20 +74,6 @@ static int _O = 0; /* Internal state */ char *program_name; -#ifndef LOCK_DIR -# ifdef __NetBSD__ -# define PIDSTRING -# define LOCK_DIR "/var/spool/lock" -# else -# ifdef HDB -# define PIDSTRING -# define LOCK_DIR "/usr/spool/locks" -# else /* HDB */ -# define LOCK_DIR "/usr/spool/uucp" -# endif /* HDB */ -# endif -#endif /* LOCK_DIR */ - #define MAX_ABORTS 50 #define DEFAULT_CHAT_TIMEOUT 45 @@ -130,19 +112,15 @@ void set_tty_parameters __P((void)); void break_sequence __P((void)); void terminate __P((int status)); void do_file __P((char *chat_file)); -void lock __P((void)); -void delay __P((void)); int get_string __P((register char *string)); int put_string __P((register char *s)); int write_char __P((int c)); -int put_char __P((char c)); +int put_char __P((int c)); int get_char __P((void)); void chat_send __P((register char *s)); -char *character __P((char c)); +char *character __P((int c)); void chat_expect __P((register char *s)); char *clean __P((register char *s, int sending)); -void unlock __P((void)); -void lock __P((void)); void break_sequence __P((void)); void terminate __P((int status)); void die __P((void)); @@ -165,7 +143,7 @@ char *s; } /* - * chat [ -v ] [ -t timeout ] [ -l lock-file ] [ -f chat-file ] \ + * chat [ -v ] [ -t timeout ] [ -f chat-file ] \ * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]] * * Perform a UUCP-dialer-like chat script on stdin and stdout. @@ -336,7 +314,7 @@ char *chat_file; void usage() { fprintf(stderr, "\ -Usage: %s [-v] [-l lock-file] [-t timeout] {-f chat-file || chat-script}\n", +Usage: %s [-v] [-t timeout] {-f chat-file || chat-script}\n", program_name); exit(1); } @@ -367,11 +345,10 @@ void logflush() } /* - * Unlock and terminate with an error. + * Terminate with an error. */ void die() { - unlock(); terminate(1); } @@ -383,7 +360,6 @@ void fatal (msg) const char *msg; { syslog(LOG_ERR, "%s", msg); - unlock(); terminate(1); } @@ -396,7 +372,6 @@ void sysfatal (msg) const char *msg; { syslog(LOG_ERR, "%s: %m", msg); - unlock(); terminate(1); } @@ -415,7 +390,7 @@ int signo; if ((flags = fcntl(0, F_GETFL, 0)) == -1) sysfatal("Can't get file mode flags on stdin"); else - if (fcntl(0, F_SETFL, flags | FNDELAY) == -1) + if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1) sysfatal("Can't set file mode flags on stdin"); if (verbose) @@ -431,7 +406,7 @@ void unalarm() if ((flags = fcntl(0, F_GETFL, 0)) == -1) sysfatal("Can't get file mode flags on stdin"); else - if (fcntl(0, F_SETFL, flags & ~FNDELAY) == -1) + if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1) sysfatal("Can't set file mode flags on stdin"); } @@ -459,9 +434,6 @@ void init() signal(SIGTERM, sigterm); signal(SIGHUP, sighup); - if (lock_file) - lock(); - set_tty_parameters(); signal(SIGALRM, sigalrm); alarm(0); @@ -522,57 +494,11 @@ int status; #endif ) { syslog(LOG_ERR, "Can't restore terminal parameters: %m"); - unlock(); exit(1); } exit(status); } -/* - * Create a lock file for the named lock device - */ -void lock() - { - int fd, pid; -# ifdef PIDSTRING - char hdb_lock_buffer[12]; -# endif - - lock_file = strcat(strcat(strcpy(malloc(strlen(LOCK_DIR) - + 1 + strlen(lock_file) + 1), - LOCK_DIR), "/"), lock_file); - - if ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) - { - char *s = lock_file; - lock_file = (char *)0; /* Don't remove someone else's lock file! */ - syslog(LOG_ERR, "Can't get lock file '%s': %m", s); - die(); - } - -# ifdef PIDSTRING - sprintf(hdb_lock_buffer, "%10d\n", getpid()); - write(fd, hdb_lock_buffer, 11); -# else - pid = getpid(); - write(fd, &pid, sizeof pid); -# endif - - close(fd); - } - -/* - * Remove our lockfile - */ -void unlock() - { - if (lock_file) - { - unlink(lock_file); - lock_file = (char *)0; - } - } - /* * 'Clean up' this string. */ @@ -778,14 +704,13 @@ register char *s; else syslog(LOG_INFO, "Failed"); - unlock(); terminate(1); } } } char *character(c) -char c; +int c; { static char string[10]; char *meta; @@ -821,7 +746,8 @@ register char *s; s1 = clean(s, 0); - if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1) + if (strlen(s1) > strlen(s) + || strlen(s1) + 1 > sizeof(fail_buffer)) { syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s); die(); @@ -863,7 +789,6 @@ register char *s; if ( ! put_string(s)) { syslog(LOG_INFO, "Failed"); - unlock(); terminate(1); } } @@ -889,7 +814,7 @@ int get_char() if ((status = fcntl(0, F_GETFL, 0)) == -1) sysfatal("Can't get file mode flags on stdin"); else - if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1) + if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1) sysfatal("Can't set file mode flags on stdin"); return (-1); @@ -897,13 +822,14 @@ int get_char() } int put_char(c) -char c; +int c; { int status; + char ch = c; - delay(); + usleep(10000); /* inter-character typing delay (?) */ - status = write(1, &c, 1); + status = write(1, &ch, 1); switch (status) { @@ -918,7 +844,7 @@ char c; if ((status = fcntl(0, F_GETFL, 0)) == -1) sysfatal("Can't get file mode flags on stdin"); else - if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1) + if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1) sysfatal("Can't set file mode flags on stdin"); return (-1); @@ -1121,8 +1047,7 @@ register char *string; return (0); } -#ifdef ultrix -#undef NO_USLEEP +#ifdef NO_USLEEP #include #include @@ -1149,18 +1074,3 @@ usleep( usec ) /* returns 0 if ok, else -1 */ return select( 0, (long *)0, (long *)0, (long *)0, &delay ); } #endif - -/* - * Delay an amount appropriate for between typed characters. - */ -void delay() - { -# ifdef NO_USLEEP - register int i; - - for (i = 0; i < 30000; ++i) /* ... did we just say appropriate? */ - ; -# else /* NO_USLEEP */ - usleep(100); -# endif /* NO_USLEEP */ - }