2 * Chat -- a program for automatic session establishment (i.e. dial
3 * the phone and log in).
5 * This software is in the public domain.
7 * Please send all bug reports, requests for information, etc. to:
9 * Al Longyear (longyear@netcom.com)
10 * (I was the last person to change this code.)
12 * The original author is:
14 * Karl Fox <karl@MorningStar.Com>
15 * Morning Star Technologies, Inc.
21 static char rcsid[] = "$Id: chat.c,v 1.8 1995/05/19 04:19:29 paulus Exp $";
29 #include <sys/types.h>
60 #define O_NONBLOCK FNDELAY
63 /*************** Micro getopt() *********************************************/
64 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
65 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
66 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
67 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
68 (_O=4,(char*)0):(char*)0)
69 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
70 #define ARG(c,v) (c?(--c,*v++):(char*)0)
72 static int _O = 0; /* Internal state */
73 /*************** Micro getopt() *********************************************/
78 #define DEFAULT_CHAT_TIMEOUT 45
82 char *lock_file = (char *)0;
83 char *chat_file = (char *)0;
84 int timeout = DEFAULT_CHAT_TIMEOUT;
86 int have_tty_parameters = 0;
88 struct termio saved_tty_parameters;
91 struct termios saved_tty_parameters;
94 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
96 int n_aborts = 0, abort_next = 0, timeout_next = 0;
98 void *dup_mem __P((void *b, size_t c));
99 void *copy_of __P((char *s));
100 void usage __P((void));
101 void logf __P((const char *str));
102 void logflush __P((void));
103 void fatal __P((const char *msg));
104 void sysfatal __P((const char *msg));
105 SIGTYPE sigalrm __P((int signo));
106 SIGTYPE sigint __P((int signo));
107 SIGTYPE sigterm __P((int signo));
108 SIGTYPE sighup __P((int signo));
109 void unalarm __P((void));
110 void init __P((void));
111 void set_tty_parameters __P((void));
112 void break_sequence __P((void));
113 void terminate __P((int status));
114 void do_file __P((char *chat_file));
115 int get_string __P((register char *string));
116 int put_string __P((register char *s));
117 int write_char __P((int c));
118 int put_char __P((int c));
119 int get_char __P((void));
120 void chat_send __P((register char *s));
121 char *character __P((int c));
122 void chat_expect __P((register char *s));
123 char *clean __P((register char *s, int sending));
124 void break_sequence __P((void));
125 void terminate __P((int status));
126 void die __P((void));
132 void *ans = malloc (c);
134 fatal ("memory error!\n");
142 return dup_mem (s, strlen (s) + 1);
146 * chat [ -v ] [ -t timeout ] [ -f chat-file ] \
147 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
149 * Perform a UUCP-dialer-like chat script on stdin and stdout.
159 program_name = *argv;
161 while (option = OPTION(argc, argv))
169 if (arg = OPTARG(argc, argv))
170 chat_file = copy_of(arg);
177 if (arg = OPTARG(argc, argv))
178 lock_file = copy_of(arg);
185 if (arg = OPTARG(argc, argv))
197 openlog("chat", LOG_PID);
199 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
202 setlogmask(LOG_UPTO(LOG_INFO));
204 setlogmask(LOG_UPTO(LOG_WARNING));
210 if (chat_file != NULL)
212 arg = ARG(argc, argv);
220 while (arg = ARG(argc, argv))
224 if (arg = ARG(argc, argv))
233 * Process a chat script when read from a file.
236 void do_file (chat_file)
239 int linect, len, sendflg;
240 char *sp, *arg, quote;
244 if ((cfp = fopen (chat_file, "r")) == NULL)
246 syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
253 while (fgets(buf, STR_LEN, cfp) != NULL)
255 sp = strchr (buf, '\n');
263 if (*sp == ' ' || *sp == '\t')
269 if (*sp == '"' || *sp == '\'')
277 syslog (LOG_ERR, "unterminated quote (line %d)",
290 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
312 * We got an error parsing the command line.
317 Usage: %s [-v] [-t timeout] {-f chat-file || chat-script}\n",
328 p = line + strlen(line);
331 if (str[strlen(str)-1] == '\n')
333 syslog (LOG_INFO, "%s", line);
342 syslog(LOG_INFO, "%s", line);
348 * Terminate with an error.
356 * Print an error message and terminate.
362 syslog(LOG_ERR, "%s", msg);
367 * Print an error message along with the system error message and
374 syslog(LOG_ERR, "%s: %m", msg);
380 SIGTYPE sigalrm(signo)
386 alarmed = 1; /* Reset alarm to avoid race window */
387 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
390 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
391 sysfatal("Can't get file mode flags on stdin");
393 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
394 sysfatal("Can't set file mode flags on stdin");
398 syslog(LOG_INFO, "alarm");
406 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
407 sysfatal("Can't get file mode flags on stdin");
409 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
410 sysfatal("Can't set file mode flags on stdin");
413 SIGTYPE sigint(signo)
419 SIGTYPE sigterm(signo)
425 SIGTYPE sighup(signo)
433 signal(SIGINT, sigint);
434 signal(SIGTERM, sigterm);
435 signal(SIGHUP, sighup);
437 set_tty_parameters();
438 signal(SIGALRM, sigalrm);
443 void set_tty_parameters()
448 if (ioctl(0, TCGETA, &t) < 0)
449 sysfatal("Can't get terminal parameters");
454 if (tcgetattr(0, &t) < 0)
455 sysfatal("Can't get terminal parameters");
458 saved_tty_parameters = t;
459 have_tty_parameters = 1;
461 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
464 t.c_cc[VERASE] = t.c_cc[VKILL] = 0;
469 if (ioctl(0, TCSETA, &t) < 0)
470 sysfatal("Can't set terminal parameters");
473 if (tcsetattr(0, TCSANOW, &t) < 0)
474 sysfatal("Can't set terminal parameters");
478 void break_sequence()
485 void terminate(status)
488 if (have_tty_parameters &&
490 ioctl(0, TCSETA, &saved_tty_parameters) < 0
493 tcsetattr(0, TCSANOW, &saved_tty_parameters) < 0
496 syslog(LOG_ERR, "Can't restore terminal parameters: %m");
503 * 'Clean up' this string.
505 char *clean(s, sending)
509 char temp[STR_LEN], cur_chr;
511 int add_return = sending;
512 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
556 if (sending && *s == '\0')
603 if (isoctal (cur_chr))
609 cur_chr |= *s++ - '0';
613 cur_chr |= *s++ - '0';
617 if (cur_chr != 0 || sending)
619 if (sending && (cur_chr == '\\' || cur_chr == 0))
636 *s1++ = '\0'; /* guarantee closure */
637 *s1++ = '\0'; /* terminate the string */
638 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
642 * Process the expect string
647 if (strcmp(s, "ABORT") == 0)
653 if (strcmp(s, "TIMEOUT") == 0)
661 register char *hyphen;
663 for (hyphen = s; *hyphen; ++hyphen)
665 if (hyphen == s || hyphen[-1] != '\\')
678 for (hyphen = s; *hyphen; ++hyphen)
680 if (hyphen == s || hyphen[-1] != '\\')
703 syslog(LOG_INFO, "Failed (%s)", fail_reason);
705 syslog(LOG_INFO, "Failed");
715 static char string[10];
718 meta = (c & 0x80) ? "M-" : "";
722 sprintf(string, "%s^%c", meta, (int)c + '@');
725 sprintf(string, "%s^?", meta);
727 sprintf(string, "%s%c", meta, c);
733 * process the reply string
744 if (n_aborts >= MAX_ABORTS)
745 fatal("Too many ABORT strings");
749 if (strlen(s1) > strlen(s)
750 || strlen(s1) + 1 > sizeof(fail_buffer))
752 syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
756 abort_string[n_aborts++] = s1;
762 for (s1 = s; *s1; ++s1)
763 logf(character(*s1));
775 timeout = DEFAULT_CHAT_TIMEOUT;
779 syslog(LOG_INFO, "timeout set to %d seconds", timeout);
784 if (strcmp(s, "EOT") == 0)
787 if (strcmp(s, "BREAK") == 0)
789 if ( ! put_string(s))
791 syslog(LOG_INFO, "Failed");
802 status = read(0, &c, 1);
807 return ((int)c & 0x7F);
810 syslog(LOG_WARNING, "warning: read() on stdin returned %d",
814 if ((status = fcntl(0, F_GETFL, 0)) == -1)
815 sysfatal("Can't get file mode flags on stdin");
817 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
818 sysfatal("Can't set file mode flags on stdin");
830 usleep(10000); /* inter-character typing delay (?) */
832 status = write(1, &ch, 1);
840 syslog(LOG_WARNING, "warning: write() on stdout returned %d",
844 if ((status = fcntl(0, F_GETFL, 0)) == -1)
845 sysfatal("Can't get file mode flags on stdin");
847 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
848 sysfatal("Can't set file mode flags on stdin");
857 if (alarmed || put_char(c) < 0)
861 alarm(0); alarmed = 0;
865 if (errno == EINTR || errno == EWOULDBLOCK)
866 syslog(LOG_INFO, " -- write timed out");
868 syslog(LOG_INFO, " -- write failed: %m");
888 register char *s1 = s;
890 for (s1 = s; *s1; ++s1)
891 logf(character(*s1));
897 alarm(timeout); alarmed = 0;
901 register char c = *s++;
922 usleep(10000); /* 1/100th of a second. */
938 * 'Wait for' this string to appear on this file descriptor.
940 int get_string(string)
941 register char *string;
944 int c, printed = 0, len, minlen;
945 register char *s = temp, *end = s + STR_LEN;
947 fail_reason = (char *)0;
948 string = clean(string, 0);
949 len = strlen(string);
950 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
958 for (s1 = string; *s1; ++s1)
959 logf(character(*s1));
966 syslog(LOG_INFO, "expect string is too long");
974 syslog(LOG_INFO, "got it");
980 alarm(timeout); alarmed = 0;
982 while ( ! alarmed && (c = get_char()) >= 0)
996 if (s - temp >= len &&
997 c == string[len - 1] &&
998 strncmp(s - len, string, len) == 0)
1002 logf(" -- got it\n");
1005 alarm(0); alarmed = 0;
1009 for (n = 0; n < n_aborts; ++n)
1010 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1011 strncmp(s - abort_len, abort_string[n], abort_len) == 0)
1015 logf(" -- failed\n");
1018 alarm(0); alarmed = 0;
1019 strcpy(fail_reason = fail_buffer, abort_string[n]);
1025 strncpy(temp, s - minlen, minlen);
1029 if (alarmed && verbose)
1030 syslog(LOG_WARNING, "warning: alarm synchronization problem");
1035 if (verbose && printed)
1038 logf(" -- read timed out\n");
1042 syslog(LOG_INFO, " -- read failed: %m");
1051 #include <sys/types.h>
1052 #include <sys/time.h>
1055 usleep -- support routine for 4.2BSD system call emulations
1056 last edit: 29-Oct-1984 D A Gwyn
1059 extern int select();
1062 usleep( usec ) /* returns 0 if ok, else -1 */
1063 long usec; /* delay in microseconds */
1065 static struct /* `timeval' */
1067 long tv_sec; /* seconds */
1068 long tv_usec; /* microsecs */
1069 } delay; /* _select() timeout */
1071 delay.tv_sec = usec / 1000000L;
1072 delay.tv_usec = usec % 1000000L;
1074 return select( 0, (long *)0, (long *)0, (long *)0, &delay );