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.
27 #include <sys/types.h>
37 # if defined(SUNOS) && SUNOS >= 41
65 /*************** Micro getopt() *********************************************/
66 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
67 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
68 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
69 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
70 (_O=4,(char*)0):(char*)0)
71 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
72 #define ARG(c,v) (c?(--c,*v++):(char*)0)
74 static int _O = 0; /* Internal state */
75 /*************** Micro getopt() *********************************************/
82 # define LOCK_DIR "/var/spool/lock"
86 # define LOCK_DIR "/usr/spool/locks"
88 # define LOCK_DIR "/usr/spool/uucp"
94 #define DEFAULT_CHAT_TIMEOUT 45
98 char *lock_file = (char *)0;
99 char *chat_file = (char *)0;
100 int timeout = DEFAULT_CHAT_TIMEOUT;
102 int have_tty_parameters = 0;
104 struct termio saved_tty_parameters;
107 struct termios saved_tty_parameters;
110 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
112 int n_aborts = 0, abort_next = 0, timeout_next = 0;
114 void *dup_mem __P((void *b, size_t c));
115 void *copy_of __P((char *s));
116 void usage __P((void));
117 void logf __P((const char *str));
118 void logflush __P((void));
119 void fatal __P((const char *msg));
120 void sysfatal __P((const char *msg));
121 SIGTYPE sigalrm __P((int signo));
122 SIGTYPE sigint __P((int signo));
123 SIGTYPE sigterm __P((int signo));
124 SIGTYPE sighup __P((int signo));
125 void unalarm __P((void));
126 void init __P((void));
127 void set_tty_parameters __P((void));
128 void break_sequence __P((void));
129 void terminate __P((int status));
130 void do_file __P((char *chat_file));
131 void lock __P((void));
132 void delay __P((void));
133 int get_string __P((register char *string));
134 int put_string __P((register char *s));
135 int write_char __P((int c));
136 int put_char __P((char c));
137 int get_char __P((void));
138 void chat_send __P((register char *s));
139 char *character __P((char c));
140 void chat_expect __P((register char *s));
141 char *clean __P((register char *s, int sending));
142 void unlock __P((void));
143 void lock __P((void));
144 void break_sequence __P((void));
145 void terminate __P((int status));
146 void die __P((void));
152 void *ans = malloc (c);
154 fatal ("memory error!\n");
162 return dup_mem (s, strlen (s) + 1);
166 * chat [ -v ] [ -t timeout ] [ -l lock-file ] [ -f chat-file ] \
167 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
169 * Perform a UUCP-dialer-like chat script on stdin and stdout.
179 program_name = *argv;
181 while (option = OPTION(argc, argv))
189 if (arg = OPTARG(argc, argv))
190 chat_file = copy_of(arg);
197 if (arg = OPTARG(argc, argv))
198 lock_file = copy_of(arg);
205 if (arg = OPTARG(argc, argv))
217 openlog("chat", LOG_PID);
219 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
222 setlogmask(LOG_UPTO(LOG_INFO));
224 setlogmask(LOG_UPTO(LOG_WARNING));
230 if (chat_file != NULL)
232 arg = ARG(argc, argv);
240 while (arg = ARG(argc, argv))
244 if (arg = ARG(argc, argv))
253 * Process a chat script when read from a file.
256 void do_file (chat_file)
259 int linect, len, sendflg;
260 char *sp, *arg, quote;
264 if ((cfp = fopen (chat_file, "r")) == NULL)
266 syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
273 while (fgets(buf, STR_LEN, cfp) != NULL)
275 sp = strchr (buf, '\n');
283 if (*sp == ' ' || *sp == '\t')
289 if (*sp == '"' || *sp == '\'')
297 syslog (LOG_ERR, "unterminated quote (line %d)",
310 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
332 * We got an error parsing the command line.
337 Usage: %s [-v] [-l lock-file] [-t timeout] {-f chat-file || chat-script}\n",
348 p = line + strlen(line);
351 if (str[strlen(str)-1] == '\n')
353 syslog (LOG_INFO, "%s", line);
362 syslog(LOG_INFO, "%s", line);
368 * Unlock and terminate with an error.
377 * Print an error message and terminate.
383 syslog(LOG_ERR, "%s", msg);
389 * Print an error message along with the system error message and
396 syslog(LOG_ERR, "%s: %m", msg);
403 SIGTYPE sigalrm(signo)
409 alarmed = 1; /* Reset alarm to avoid race window */
410 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
413 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
414 sysfatal("Can't get file mode flags on stdin");
416 if (fcntl(0, F_SETFL, flags | FNDELAY) == -1)
417 sysfatal("Can't set file mode flags on stdin");
421 syslog(LOG_INFO, "alarm");
429 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
430 sysfatal("Can't get file mode flags on stdin");
432 if (fcntl(0, F_SETFL, flags & ~FNDELAY) == -1)
433 sysfatal("Can't set file mode flags on stdin");
436 SIGTYPE sigint(signo)
442 SIGTYPE sigterm(signo)
448 SIGTYPE sighup(signo)
456 signal(SIGINT, sigint);
457 signal(SIGTERM, sigterm);
458 signal(SIGHUP, sighup);
463 set_tty_parameters();
464 signal(SIGALRM, sigalrm);
469 void set_tty_parameters()
474 if (ioctl(0, TCGETA, &t) < 0)
475 sysfatal("Can't get terminal parameters");
480 if (tcgetattr(0, &t) < 0)
481 sysfatal("Can't get terminal parameters");
484 saved_tty_parameters = t;
485 have_tty_parameters = 1;
487 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
490 t.c_cc[VERASE] = t.c_cc[VKILL] = 0;
495 if (ioctl(0, TCSETA, &t) < 0)
496 sysfatal("Can't set terminal parameters");
499 if (tcsetattr(0, TCSANOW, &t) < 0)
500 sysfatal("Can't set terminal parameters");
504 void break_sequence()
511 void terminate(status)
514 if (have_tty_parameters &&
516 ioctl(0, TCSETA, &saved_tty_parameters) < 0
519 tcsetattr(0, TCSANOW, &saved_tty_parameters) < 0
522 syslog(LOG_ERR, "Can't restore terminal parameters: %m");
530 * Create a lock file for the named lock device
536 char hdb_lock_buffer[12];
539 lock_file = strcat(strcat(strcpy(malloc(strlen(LOCK_DIR)
540 + 1 + strlen(lock_file) + 1),
541 LOCK_DIR), "/"), lock_file);
543 if ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0)
546 lock_file = (char *)0; /* Don't remove someone else's lock file! */
547 syslog(LOG_ERR, "Can't get lock file '%s': %m", s);
552 sprintf(hdb_lock_buffer, "%10d\n", getpid());
553 write(fd, hdb_lock_buffer, 11);
556 write(fd, &pid, sizeof pid);
563 * Remove our lockfile
570 lock_file = (char *)0;
575 * 'Clean up' this string.
577 char *clean(s, sending)
581 char temp[STR_LEN], cur_chr;
583 int add_return = sending;
584 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
628 if (sending && *s == '\0')
675 if (isoctal (cur_chr))
681 cur_chr |= *s++ - '0';
685 cur_chr |= *s++ - '0';
689 if (cur_chr != 0 || sending)
691 if (sending && (cur_chr == '\\' || cur_chr == 0))
708 *s1++ = '\0'; /* guarantee closure */
709 *s1++ = '\0'; /* terminate the string */
710 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
714 * Process the expect string
719 if (strcmp(s, "ABORT") == 0)
725 if (strcmp(s, "TIMEOUT") == 0)
733 register char *hyphen;
735 for (hyphen = s; *hyphen; ++hyphen)
737 if (hyphen == s || hyphen[-1] != '\\')
750 for (hyphen = s; *hyphen; ++hyphen)
752 if (hyphen == s || hyphen[-1] != '\\')
775 syslog(LOG_INFO, "Failed (%s)", fail_reason);
777 syslog(LOG_INFO, "Failed");
788 static char string[10];
791 meta = (c & 0x80) ? "M-" : "";
795 sprintf(string, "%s^%c", meta, (int)c + '@');
798 sprintf(string, "%s^?", meta);
800 sprintf(string, "%s%c", meta, c);
806 * process the reply string
817 if (n_aborts >= MAX_ABORTS)
818 fatal("Too many ABORT strings");
822 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
824 syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
829 abort_string[n_aborts++] = s;
833 register char *s1 = s;
837 for (s1 = s; *s1; ++s1)
838 logf(character(*s1));
850 timeout = DEFAULT_CHAT_TIMEOUT;
854 syslog(LOG_INFO, "timeout set to %d seconds", timeout);
859 if (strcmp(s, "EOT") == 0)
862 if (strcmp(s, "BREAK") == 0)
864 if ( ! put_string(s))
866 syslog(LOG_INFO, "Failed");
878 status = read(0, &c, 1);
883 return ((int)c & 0x7F);
886 syslog(LOG_WARNING, "warning: read() on stdin returned %d",
890 if ((status = fcntl(0, F_GETFL, 0)) == -1)
891 sysfatal("Can't get file mode flags on stdin");
893 if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
894 sysfatal("Can't set file mode flags on stdin");
907 status = write(1, &c, 1);
915 syslog(LOG_WARNING, "warning: write() on stdout returned %d",
919 if ((status = fcntl(0, F_GETFL, 0)) == -1)
920 sysfatal("Can't get file mode flags on stdin");
922 if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
923 sysfatal("Can't set file mode flags on stdin");
932 if (alarmed || put_char(c) < 0)
936 alarm(0); alarmed = 0;
940 if (errno == EINTR || errno == EWOULDBLOCK)
941 syslog(LOG_INFO, " -- write timed out");
943 syslog(LOG_INFO, " -- write failed: %m");
963 register char *s1 = s;
965 for (s1 = s; *s1; ++s1)
966 logf(character(*s1));
972 alarm(timeout); alarmed = 0;
976 register char c = *s++;
997 usleep(10000); /* 1/100th of a second. */
1001 if (!write_char (c))
1013 * 'Wait for' this string to appear on this file descriptor.
1015 int get_string(string)
1016 register char *string;
1019 int c, printed = 0, len, minlen;
1020 register char *s = temp, *end = s + STR_LEN;
1022 fail_reason = (char *)0;
1023 string = clean(string, 0);
1024 len = strlen(string);
1025 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1033 for (s1 = string; *s1; ++s1)
1034 logf(character(*s1));
1041 syslog(LOG_INFO, "expect string is too long");
1049 syslog(LOG_INFO, "got it");
1055 alarm(timeout); alarmed = 0;
1057 while ( ! alarmed && (c = get_char()) >= 0)
1071 if (s - temp >= len &&
1072 c == string[len - 1] &&
1073 strncmp(s - len, string, len) == 0)
1077 logf(" -- got it\n");
1080 alarm(0); alarmed = 0;
1084 for (n = 0; n < n_aborts; ++n)
1085 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1086 strncmp(s - abort_len, abort_string[n], abort_len) == 0)
1090 logf(" -- failed\n");
1093 alarm(0); alarmed = 0;
1094 strcpy(fail_reason = fail_buffer, abort_string[n]);
1100 strncpy(temp, s - minlen, minlen);
1104 if (alarmed && verbose)
1105 syslog(LOG_WARNING, "warning: alarm synchronization problem");
1110 if (verbose && printed)
1113 logf(" -- read timed out\n");
1117 syslog(LOG_INFO, " -- read failed: %m");
1127 #include <sys/types.h>
1128 #include <sys/time.h>
1131 usleep -- support routine for 4.2BSD system call emulations
1132 last edit: 29-Oct-1984 D A Gwyn
1135 extern int select();
1138 usleep( usec ) /* returns 0 if ok, else -1 */
1139 long usec; /* delay in microseconds */
1141 static struct /* `timeval' */
1143 long tv_sec; /* seconds */
1144 long tv_usec; /* microsecs */
1145 } delay; /* _select() timeout */
1147 delay.tv_sec = usec / 1000000L;
1148 delay.tv_usec = usec % 1000000L;
1150 return select( 0, (long *)0, (long *)0, (long *)0, &delay );
1155 * Delay an amount appropriate for between typed characters.
1162 for (i = 0; i < 30000; ++i) /* ... did we just say appropriate? */
1164 # else /* NO_USLEEP */
1166 # endif /* NO_USLEEP */