2 * Chat -- a program for automatic session establishment (i.e. dial
3 * the phone and log in).
5 * Standard termination codes:
6 * 0 - successful completion of the script
7 * 1 - invalid argument, expect string too large, etc.
8 * 2 - error on an I/O operation or fatal error condtion.
9 * 3 - timeout waiting for a simple string.
10 * 4 - the first string declared as "ABORT"
11 * 5 - the second string declared as "ABORT"
12 * 6 - ... and so on for successive ABORT strings.
14 * This software is in the public domain.
16 * Please send all bug reports, requests for information, etc. to:
18 * Al Longyear (longyear@netcom.com)
19 * (I was the last person to change this code.)
21 * Added -r "report file" switch & REPORT keyword.
22 * Robert Geer <bgeer@xmission.com>
24 * The original author is:
26 * Karl Fox <karl@MorningStar.Com>
27 * Morning Star Technologies, Inc.
34 static char rcsid[] = "$Id: chat.c,v 1.9 1995/06/12 11:24:15 paulus Exp $";
43 #include <sys/types.h>
74 #define O_NONBLOCK O_NDELAY
77 /*************** Micro getopt() *********************************************/
78 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
79 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
80 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
81 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
82 (_O=4,(char*)0):(char*)0)
83 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
84 #define ARG(c,v) (c?(--c,*v++):(char*)0)
86 static int _O = 0; /* Internal state */
87 /*************** Micro getopt() *********************************************/
92 #define MAX_REPORTS 50
93 #define DEFAULT_CHAT_TIMEOUT 45
99 FILE* report_fp = (FILE *) 0;
100 char *report_file = (char *) 0;
101 char *chat_file = (char *) 0;
102 int timeout = DEFAULT_CHAT_TIMEOUT;
104 int have_tty_parameters = 0;
107 #define term_parms struct termio
108 #define get_term_param(param) ioctl(0, TCGETA, param)
109 #define set_term_param(param) ioctl(0, TCSETA, param)
110 struct termio saved_tty_parameters;
114 #define term_parms struct termios
115 #define get_term_param(param) tcgetattr(0, param)
116 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
117 struct termios saved_tty_parameters;
120 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
122 int n_aborts = 0, abort_next = 0, timeout_next = 0;
124 char *report_string[MAX_REPORTS] ;
125 char report_buffer[50] ;
126 int n_reports = 0, report_next = 0, report_gathering = 0 ;
128 void *dup_mem __P((void *b, size_t c));
129 void *copy_of __P((char *s));
130 void usage __P((void));
131 void logf __P((const char *str));
132 void logflush __P((void));
133 void fatal __P((const char *msg));
134 void sysfatal __P((const char *msg));
135 SIGTYPE sigalrm __P((int signo));
136 SIGTYPE sigint __P((int signo));
137 SIGTYPE sigterm __P((int signo));
138 SIGTYPE sighup __P((int signo));
139 void unalarm __P((void));
140 void init __P((void));
141 void set_tty_parameters __P((void));
142 void break_sequence __P((void));
143 void terminate __P((int status));
144 void do_file __P((char *chat_file));
145 int get_string __P((register char *string));
146 int put_string __P((register char *s));
147 int write_char __P((int c));
148 int put_char __P((int c));
149 int get_char __P((void));
150 void chat_send __P((register char *s));
151 char *character __P((int c));
152 void chat_expect __P((register char *s));
153 char *clean __P((register char *s, int sending));
154 void break_sequence __P((void));
155 void terminate __P((int status));
156 void die __P((void));
162 void *ans = malloc (c);
165 fatal ("memory error!\n");
174 return dup_mem (s, strlen (s) + 1);
178 * chat [ -v ] [ -t timeout ] [ -f chat-file ] [ -r report-file ] \
179 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
181 * Perform a UUCP-dialer-like chat script on stdin and stdout.
191 program_name = *argv;
194 while (option = OPTION(argc, argv))
203 if (arg = OPTARG(argc, argv))
205 chat_file = copy_of(arg);
214 if (arg = OPTARG(argc, argv))
225 arg = OPTARG (argc, argv);
228 if (report_fp != NULL)
232 report_file = copy_of (arg);
233 report_fp = fopen (report_file, "a");
234 if (report_fp != NULL)
238 fprintf (report_fp, "Opening \"%s\"...\n",
252 * Default the report file to the stderr location
254 if (report_fp == NULL)
260 openlog("chat", LOG_PID);
262 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
266 setlogmask(LOG_UPTO(LOG_INFO));
270 setlogmask(LOG_UPTO(LOG_WARNING));
276 if (chat_file != NULL)
278 arg = ARG(argc, argv);
290 while (arg = ARG(argc, argv))
294 if (arg = ARG(argc, argv))
305 * Process a chat script when read from a file.
308 void do_file (chat_file)
311 int linect, len, sendflg;
312 char *sp, *arg, quote;
316 cfp = fopen (chat_file, "r");
319 syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
326 while (fgets(buf, STR_LEN, cfp) != NULL)
328 sp = strchr (buf, '\n');
338 if (*sp == ' ' || *sp == '\t')
344 if (*sp == '"' || *sp == '\'')
352 syslog (LOG_ERR, "unterminated quote (line %d)",
369 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
395 * We got an error parsing the command line.
400 Usage: %s [-v] [-t timeout] [-r report-file] {-f chat-file | chat-script}\n",
411 p = line + strlen(line);
414 if (str[strlen(str)-1] == '\n')
416 syslog (LOG_INFO, "%s", line);
425 syslog(LOG_INFO, "%s", line);
431 * Terminate with an error.
439 * Print an error message and terminate.
445 syslog(LOG_ERR, "%s", msg);
450 * Print an error message along with the system error message and
457 syslog(LOG_ERR, "%s: %m", msg);
463 SIGTYPE sigalrm(signo)
469 alarmed = 1; /* Reset alarm to avoid race window */
470 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
473 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
475 sysfatal("Can't get file mode flags on stdin");
479 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
481 sysfatal("Can't set file mode flags on stdin");
487 syslog(LOG_INFO, "alarm");
495 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
497 sysfatal("Can't get file mode flags on stdin");
501 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
503 sysfatal("Can't set file mode flags on stdin");
508 SIGTYPE sigint(signo)
514 SIGTYPE sigterm(signo)
520 SIGTYPE sighup(signo)
528 signal(SIGINT, sigint);
529 signal(SIGTERM, sigterm);
530 signal(SIGHUP, sighup);
532 set_tty_parameters();
533 signal(SIGALRM, sigalrm);
538 void set_tty_parameters()
540 #if defined(get_term_param)
543 if (get_term_param (&t) < 0)
545 sysfatal("Can't get terminal parameters");
548 saved_tty_parameters = t;
549 have_tty_parameters = 1;
551 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
559 if (set_term_param (&t) < 0)
561 sysfatal("Can't set terminal parameters");
566 void break_sequence()
573 void terminate(status)
576 if (report_file != (char *) 0 && report_fp != (FILE *) NULL)
580 fprintf (report_fp, "Closing \"%s\".\n", report_file);
583 report_fp = (FILE*) NULL;
586 #if defined(get_term_param)
587 if (have_tty_parameters)
589 if (set_term_param (&saved_tty_parameters) < 0)
591 syslog(LOG_ERR, "Can't restore terminal parameters: %m");
601 * 'Clean up' this string.
603 char *clean(s, sending)
607 char temp[STR_LEN], cur_chr;
609 int add_return = sending;
610 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
656 if (sending && *s == '\0')
711 if (isoctal (cur_chr))
717 cur_chr |= *s++ - '0';
721 cur_chr |= *s++ - '0';
725 if (cur_chr != 0 || sending)
727 if (sending && (cur_chr == '\\' || cur_chr == 0))
750 *s1++ = '\0'; /* guarantee closure */
751 *s1++ = '\0'; /* terminate the string */
752 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
756 * Process the expect string
761 if (strcmp(s, "ABORT") == 0)
767 if (strcmp(s, "REPORT") == 0)
773 if (strcmp(s, "TIMEOUT") == 0)
781 register char *hyphen;
783 for (hyphen = s; *hyphen; ++hyphen)
787 if (hyphen == s || hyphen[-1] != '\\')
806 for (hyphen = s; *hyphen; ++hyphen)
810 if (hyphen == s || hyphen[-1] != '\\')
841 syslog(LOG_INFO, "Failed (%s)", fail_reason);
845 syslog(LOG_INFO, "Failed");
848 terminate(exit_code);
857 static char string[10];
860 meta = (c & 0x80) ? "M-" : "";
865 sprintf(string, "%s^%c", meta, (int)c + '@');
871 sprintf(string, "%s^?", meta);
875 sprintf(string, "%s%c", meta, c);
883 * process the reply string
894 if (n_aborts >= MAX_ABORTS)
896 fatal("Too many ABORT strings");
901 if (strlen(s1) > strlen(s)
902 || strlen(s1) + 1 > sizeof(fail_buffer))
904 syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
908 abort_string[n_aborts++] = s1;
914 for (s1 = s; *s1; ++s1)
916 logf(character(*s1));
929 if (n_reports >= MAX_REPORTS)
931 fatal("Too many REPORT strings");
936 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
938 syslog(LOG_WARNING, "Illegal or too-long REPORT string ('%s')", s);
942 report_string[n_reports++] = s1;
950 logf(character(*s1));
965 timeout = DEFAULT_CHAT_TIMEOUT;
970 syslog(LOG_INFO, "timeout set to %d seconds", timeout);
975 if (strcmp(s, "EOT") == 0)
981 if (strcmp(s, "BREAK") == 0)
989 syslog(LOG_INFO, "Failed");
999 status = read(0, &c, 1);
1004 return ((int)c & 0x7F);
1007 syslog(LOG_WARNING, "warning: read() on stdin returned %d",
1011 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1013 sysfatal("Can't get file mode flags on stdin");
1017 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1019 sysfatal("Can't set file mode flags on stdin");
1033 usleep(10000); /* inter-character typing delay (?) */
1035 status = write(1, &ch, 1);
1043 syslog(LOG_WARNING, "warning: write() on stdout returned %d",
1047 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1049 sysfatal("Can't get file mode flags on stdin");
1053 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1055 sysfatal("Can't set file mode flags on stdin");
1066 if (alarmed || put_char(c) < 0)
1075 if (errno == EINTR || errno == EWOULDBLOCK)
1077 syslog(LOG_INFO, " -- write timed out");
1081 syslog(LOG_INFO, " -- write failed: %m");
1104 register char *s1 = s;
1106 for (s1 = s; *s1; ++s1)
1108 logf(character(*s1));
1115 alarm(timeout); alarmed = 0;
1119 register char c = *s++;
1123 if (!write_char (c))
1142 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1146 if (!write_char (c))
1158 * 'Wait for' this string to appear on this file descriptor.
1160 int get_string(string)
1161 register char *string;
1164 int c, printed = 0, len, minlen;
1165 register char *s = temp, *end = s + STR_LEN;
1167 fail_reason = (char *)0;
1168 string = clean(string, 0);
1169 len = strlen(string);
1170 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1178 for (s1 = string; *s1; ++s1)
1180 logf(character(*s1));
1188 syslog(LOG_INFO, "expect string is too long");
1197 syslog(LOG_INFO, "got it");
1206 while ( ! alarmed && (c = get_char()) >= 0)
1208 int n, abort_len, report_len;
1224 if (s - temp >= len &&
1225 c == string[len - 1] &&
1226 strncmp(s - len, string, len) == 0)
1230 logf(" -- got it\n");
1238 for (n = 0; n < n_aborts; ++n)
1240 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1241 strncmp(s - abort_len, abort_string[n], abort_len) == 0)
1245 logf(" -- failed\n");
1251 strcpy(fail_reason = fail_buffer, abort_string[n]);
1256 if (!report_gathering)
1258 for (n = 0; n < n_reports; ++n)
1260 if ((report_string[n] != (char*) NULL) &&
1261 s - temp >= (report_len = strlen(report_string[n])) &&
1262 strncmp(s - report_len, report_string[n], report_len) == 0)
1264 time_t time_now = time ((time_t*) NULL);
1265 struct tm* tm_now = localtime (&time_now);
1267 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1268 strcat (report_buffer, report_string[n]);
1270 report_string[n] = (char *) NULL;
1271 report_gathering = 1;
1280 int rep_len = strlen (report_buffer);
1281 report_buffer[rep_len] = c;
1282 report_buffer[rep_len + 1] = '\0';
1286 report_gathering = 0;
1287 fprintf (report_fp, "chat: %s\n", report_buffer);
1293 strncpy (temp, s - minlen, minlen);
1297 if (alarmed && verbose)
1299 syslog(LOG_WARNING, "warning: alarm synchronization problem");
1305 if (verbose && printed)
1309 logf(" -- read timed out\n");
1314 syslog(LOG_INFO, " -- read failed: %m");
1324 #include <sys/types.h>
1325 #include <sys/time.h>
1328 usleep -- support routine for 4.2BSD system call emulations
1329 last edit: 29-Oct-1984 D A Gwyn
1332 extern int select();
1335 usleep( usec ) /* returns 0 if ok, else -1 */
1336 long usec; /* delay in microseconds */
1338 static struct /* `timeval' */
1340 long tv_sec; /* seconds */
1341 long tv_usec; /* microsecs */
1342 } delay; /* _select() timeout */
1344 delay.tv_sec = usec / 1000000L;
1345 delay.tv_usec = usec % 1000000L;
1347 return select( 0, (long *)0, (long *)0, (long *)0, &delay );