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.5 1995/01/11 03:14:45 paulus Exp $";
29 #include <sys/types.h>
59 /*************** Micro getopt() *********************************************/
60 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
61 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
62 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
63 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
64 (_O=4,(char*)0):(char*)0)
65 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
66 #define ARG(c,v) (c?(--c,*v++):(char*)0)
68 static int _O = 0; /* Internal state */
69 /*************** Micro getopt() *********************************************/
74 #define DEFAULT_CHAT_TIMEOUT 45
78 char *lock_file = (char *)0;
79 char *chat_file = (char *)0;
80 int timeout = DEFAULT_CHAT_TIMEOUT;
82 int have_tty_parameters = 0;
84 struct termio saved_tty_parameters;
87 struct termios saved_tty_parameters;
90 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
92 int n_aborts = 0, abort_next = 0, timeout_next = 0;
94 void *dup_mem __P((void *b, size_t c));
95 void *copy_of __P((char *s));
96 void usage __P((void));
97 void logf __P((const char *str));
98 void logflush __P((void));
99 void fatal __P((const char *msg));
100 void sysfatal __P((const char *msg));
101 SIGTYPE sigalrm __P((int signo));
102 SIGTYPE sigint __P((int signo));
103 SIGTYPE sigterm __P((int signo));
104 SIGTYPE sighup __P((int signo));
105 void unalarm __P((void));
106 void init __P((void));
107 void set_tty_parameters __P((void));
108 void break_sequence __P((void));
109 void terminate __P((int status));
110 void do_file __P((char *chat_file));
111 void delay __P((void));
112 int get_string __P((register char *string));
113 int put_string __P((register char *s));
114 int write_char __P((int c));
115 int put_char __P((char c));
116 int get_char __P((void));
117 void chat_send __P((register char *s));
118 char *character __P((char c));
119 void chat_expect __P((register char *s));
120 char *clean __P((register char *s, int sending));
121 void break_sequence __P((void));
122 void terminate __P((int status));
123 void die __P((void));
129 void *ans = malloc (c);
131 fatal ("memory error!\n");
139 return dup_mem (s, strlen (s) + 1);
143 * chat [ -v ] [ -t timeout ] [ -f chat-file ] \
144 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
146 * Perform a UUCP-dialer-like chat script on stdin and stdout.
156 program_name = *argv;
158 while (option = OPTION(argc, argv))
166 if (arg = OPTARG(argc, argv))
167 chat_file = copy_of(arg);
174 if (arg = OPTARG(argc, argv))
175 lock_file = copy_of(arg);
182 if (arg = OPTARG(argc, argv))
194 openlog("chat", LOG_PID);
196 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
199 setlogmask(LOG_UPTO(LOG_INFO));
201 setlogmask(LOG_UPTO(LOG_WARNING));
207 if (chat_file != NULL)
209 arg = ARG(argc, argv);
217 while (arg = ARG(argc, argv))
221 if (arg = ARG(argc, argv))
230 * Process a chat script when read from a file.
233 void do_file (chat_file)
236 int linect, len, sendflg;
237 char *sp, *arg, quote;
241 if ((cfp = fopen (chat_file, "r")) == NULL)
243 syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
250 while (fgets(buf, STR_LEN, cfp) != NULL)
252 sp = strchr (buf, '\n');
260 if (*sp == ' ' || *sp == '\t')
266 if (*sp == '"' || *sp == '\'')
274 syslog (LOG_ERR, "unterminated quote (line %d)",
287 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
309 * We got an error parsing the command line.
314 Usage: %s [-v] [-t timeout] {-f chat-file || chat-script}\n",
325 p = line + strlen(line);
328 if (str[strlen(str)-1] == '\n')
330 syslog (LOG_INFO, "%s", line);
339 syslog(LOG_INFO, "%s", line);
345 * Terminate with an error.
353 * Print an error message and terminate.
359 syslog(LOG_ERR, "%s", msg);
364 * Print an error message along with the system error message and
371 syslog(LOG_ERR, "%s: %m", msg);
377 SIGTYPE sigalrm(signo)
383 alarmed = 1; /* Reset alarm to avoid race window */
384 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
387 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
388 sysfatal("Can't get file mode flags on stdin");
390 if (fcntl(0, F_SETFL, flags | FNDELAY) == -1)
391 sysfatal("Can't set file mode flags on stdin");
395 syslog(LOG_INFO, "alarm");
403 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
404 sysfatal("Can't get file mode flags on stdin");
406 if (fcntl(0, F_SETFL, flags & ~FNDELAY) == -1)
407 sysfatal("Can't set file mode flags on stdin");
410 SIGTYPE sigint(signo)
416 SIGTYPE sigterm(signo)
422 SIGTYPE sighup(signo)
430 signal(SIGINT, sigint);
431 signal(SIGTERM, sigterm);
432 signal(SIGHUP, sighup);
434 set_tty_parameters();
435 signal(SIGALRM, sigalrm);
440 void set_tty_parameters()
445 if (ioctl(0, TCGETA, &t) < 0)
446 sysfatal("Can't get terminal parameters");
451 if (tcgetattr(0, &t) < 0)
452 sysfatal("Can't get terminal parameters");
455 saved_tty_parameters = t;
456 have_tty_parameters = 1;
458 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
461 t.c_cc[VERASE] = t.c_cc[VKILL] = 0;
466 if (ioctl(0, TCSETA, &t) < 0)
467 sysfatal("Can't set terminal parameters");
470 if (tcsetattr(0, TCSANOW, &t) < 0)
471 sysfatal("Can't set terminal parameters");
475 void break_sequence()
482 void terminate(status)
485 if (have_tty_parameters &&
487 ioctl(0, TCSETA, &saved_tty_parameters) < 0
490 tcsetattr(0, TCSANOW, &saved_tty_parameters) < 0
493 syslog(LOG_ERR, "Can't restore terminal parameters: %m");
500 * 'Clean up' this string.
502 char *clean(s, sending)
506 char temp[STR_LEN], cur_chr;
508 int add_return = sending;
509 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
553 if (sending && *s == '\0')
600 if (isoctal (cur_chr))
606 cur_chr |= *s++ - '0';
610 cur_chr |= *s++ - '0';
614 if (cur_chr != 0 || sending)
616 if (sending && (cur_chr == '\\' || cur_chr == 0))
633 *s1++ = '\0'; /* guarantee closure */
634 *s1++ = '\0'; /* terminate the string */
635 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
639 * Process the expect string
644 if (strcmp(s, "ABORT") == 0)
650 if (strcmp(s, "TIMEOUT") == 0)
658 register char *hyphen;
660 for (hyphen = s; *hyphen; ++hyphen)
662 if (hyphen == s || hyphen[-1] != '\\')
675 for (hyphen = s; *hyphen; ++hyphen)
677 if (hyphen == s || hyphen[-1] != '\\')
700 syslog(LOG_INFO, "Failed (%s)", fail_reason);
702 syslog(LOG_INFO, "Failed");
712 static char string[10];
715 meta = (c & 0x80) ? "M-" : "";
719 sprintf(string, "%s^%c", meta, (int)c + '@');
722 sprintf(string, "%s^?", meta);
724 sprintf(string, "%s%c", meta, c);
730 * process the reply string
741 if (n_aborts >= MAX_ABORTS)
742 fatal("Too many ABORT strings");
746 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
748 syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
752 abort_string[n_aborts++] = s1;
758 for (s1 = s; *s1; ++s1)
759 logf(character(*s1));
771 timeout = DEFAULT_CHAT_TIMEOUT;
775 syslog(LOG_INFO, "timeout set to %d seconds", timeout);
780 if (strcmp(s, "EOT") == 0)
783 if (strcmp(s, "BREAK") == 0)
785 if ( ! put_string(s))
787 syslog(LOG_INFO, "Failed");
798 status = read(0, &c, 1);
803 return ((int)c & 0x7F);
806 syslog(LOG_WARNING, "warning: read() on stdin returned %d",
810 if ((status = fcntl(0, F_GETFL, 0)) == -1)
811 sysfatal("Can't get file mode flags on stdin");
813 if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
814 sysfatal("Can't set file mode flags on stdin");
827 status = write(1, &c, 1);
835 syslog(LOG_WARNING, "warning: write() on stdout returned %d",
839 if ((status = fcntl(0, F_GETFL, 0)) == -1)
840 sysfatal("Can't get file mode flags on stdin");
842 if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
843 sysfatal("Can't set file mode flags on stdin");
852 if (alarmed || put_char(c) < 0)
856 alarm(0); alarmed = 0;
860 if (errno == EINTR || errno == EWOULDBLOCK)
861 syslog(LOG_INFO, " -- write timed out");
863 syslog(LOG_INFO, " -- write failed: %m");
883 register char *s1 = s;
885 for (s1 = s; *s1; ++s1)
886 logf(character(*s1));
892 alarm(timeout); alarmed = 0;
896 register char c = *s++;
917 usleep(10000); /* 1/100th of a second. */
933 * 'Wait for' this string to appear on this file descriptor.
935 int get_string(string)
936 register char *string;
939 int c, printed = 0, len, minlen;
940 register char *s = temp, *end = s + STR_LEN;
942 fail_reason = (char *)0;
943 string = clean(string, 0);
944 len = strlen(string);
945 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
953 for (s1 = string; *s1; ++s1)
954 logf(character(*s1));
961 syslog(LOG_INFO, "expect string is too long");
969 syslog(LOG_INFO, "got it");
975 alarm(timeout); alarmed = 0;
977 while ( ! alarmed && (c = get_char()) >= 0)
991 if (s - temp >= len &&
992 c == string[len - 1] &&
993 strncmp(s - len, string, len) == 0)
997 logf(" -- got it\n");
1000 alarm(0); alarmed = 0;
1004 for (n = 0; n < n_aborts; ++n)
1005 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1006 strncmp(s - abort_len, abort_string[n], abort_len) == 0)
1010 logf(" -- failed\n");
1013 alarm(0); alarmed = 0;
1014 strcpy(fail_reason = fail_buffer, abort_string[n]);
1020 strncpy(temp, s - minlen, minlen);
1024 if (alarmed && verbose)
1025 syslog(LOG_WARNING, "warning: alarm synchronization problem");
1030 if (verbose && printed)
1033 logf(" -- read timed out\n");
1037 syslog(LOG_INFO, " -- read failed: %m");
1047 #include <sys/types.h>
1048 #include <sys/time.h>
1051 usleep -- support routine for 4.2BSD system call emulations
1052 last edit: 29-Oct-1984 D A Gwyn
1055 extern int select();
1058 usleep( usec ) /* returns 0 if ok, else -1 */
1059 long usec; /* delay in microseconds */
1061 static struct /* `timeval' */
1063 long tv_sec; /* seconds */
1064 long tv_usec; /* microsecs */
1065 } delay; /* _select() timeout */
1067 delay.tv_sec = usec / 1000000L;
1068 delay.tv_usec = usec % 1000000L;
1070 return select( 0, (long *)0, (long *)0, (long *)0, &delay );
1075 * Delay an amount appropriate for between typed characters.
1082 for (i = 0; i < 30000; ++i) /* ... did we just say appropriate? */
1084 # else /* NO_USLEEP */
1086 # endif /* NO_USLEEP */