VFS_MOUNT.9: Adjust to the current state in /usr/src.
[dragonfly.git] / usr.bin / chat / chat.c
1 /*
2  *      Chat -- a program for automatic session establishment (i.e. dial
3  *              the phone and log in).
4  *
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 condition.
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.
13  *
14  *      This software is in the public domain.
15  *
16  * -----------------
17  *      added -T and -U option and \T and \U substitution to pass a phone
18  *      number into chat script. Two are needed for some ISDN TA applications.
19  *      Keith Dart <kdart@cisco.com>
20  *      
21  *
22  *      Added SAY keyword to send output to stderr.
23  *      This allows to turn ECHO OFF and to output specific, user selected,
24  *      text to give progress messages. This best works when stderr
25  *      exists (i.e.: pppd in nodetach mode).
26  *
27  *      Added HANGUP directives to allow for us to be called
28  *      back. When HANGUP is set to NO, chat will not hangup at HUP signal.
29  *      We rely on timeouts in that case.
30  *
31  *      Added CLR_ABORT to clear previously set ABORT string. This has been
32  *      dictated by the HANGUP above as "NO CARRIER" (for example) must be
33  *      an ABORT condition until we know the other host is going to close
34  *      the connection for call back. As soon as we have completed the
35  *      first stage of the call back sequence, "NO CARRIER" is a valid, non
36  *      fatal string. As soon as we got called back (probably get "CONNECT"),
37  *      we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
38  *      Note that CLR_ABORT packs the abort_strings[] array so that we do not
39  *      have unused entries not being reclaimed.
40  *
41  *      In the same vein as above, added CLR_REPORT keyword.
42  *
43  *      Allow for comments. Line starting with '#' are comments and are
44  *      ignored. If a '#' is to be expected as the first character, the 
45  *      expect string must be quoted.
46  *
47  *
48  *              Francis Demierre <Francis@SwissMail.Com>
49  *              Thu May 15 17:15:40 MET DST 1997
50  *
51  *
52  *      Added -r "report file" switch & REPORT keyword.
53  *              Robert Geer <bgeer@xmission.com>
54  *
55  *      Added -s "use stderr" and -S "don't use syslog" switches.
56  *              June 18, 1997
57  *              Karl O. Pinc <kop@meme.com>
58  *
59  *
60  *      Added -e "echo" switch & ECHO keyword
61  *              Dick Streefland <dicks@tasking.nl>
62  *
63  *
64  *      Considerable updates and modifications by
65  *              Al Longyear <longyear@pobox.com>
66  *              Paul Mackerras <paulus@cs.anu.edu.au>
67  *
68  *
69  *      The original author is:
70  *
71  *              Karl Fox <karl@MorningStar.Com>
72  *              Morning Star Technologies, Inc.
73  *              1760 Zollinger Road
74  *              Columbus, OH  43221
75  *              (614)451-1883
76  *
77  *
78  * $FreeBSD: src/usr.bin/chat/chat.c,v 1.21 2003/10/31 06:22:03 kientzle Exp $
79  */
80
81 #include <sys/types.h>
82 #include <sys/stat.h>
83 #include <ctype.h>
84 #include <errno.h>
85 #include <fcntl.h>
86 #include <signal.h>
87 #include <stdarg.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <syslog.h>
92 #include <termios.h>
93 #include <time.h>
94 #include <unistd.h>
95
96 #define STR_LEN 1024
97
98 #ifndef SIGTYPE
99 #define SIGTYPE void
100 #endif
101
102 #ifndef O_NONBLOCK
103 #define O_NONBLOCK      O_NDELAY
104 #endif
105
106 #define MAX_ABORTS              50
107 #define MAX_REPORTS             50
108 #define DEFAULT_CHAT_TIMEOUT    45
109
110 int echo          = 0;
111 int verbose       = 0;
112 int to_log        = 1;
113 int to_stderr     = 0;
114 int Verbose       = 0;
115 int quiet         = 0;
116 int exit_code     = 0;
117 FILE* report_fp   = NULL;
118 char *report_file = NULL;
119 char *chat_file   = NULL;
120 char *phone_num   = NULL;
121 char *phone_num2  = NULL;
122 int timeout       = DEFAULT_CHAT_TIMEOUT;
123
124 static char blank[] = "";
125
126 int have_tty_parameters = 0;
127
128 #define term_parms struct termios
129 #define get_term_param(param) tcgetattr(0, param)
130 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
131 struct termios saved_tty_parameters;
132
133 char *abort_string[MAX_ABORTS], *fail_reason = NULL,
134         fail_buffer[50];
135 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
136 int clear_abort_next = 0;
137
138 char *report_string[MAX_REPORTS] ;
139 char  report_buffer[50] ;
140 int n_reports = 0, report_next = 0, report_gathering = 0 ; 
141 int clear_report_next = 0;
142
143 int say_next = 0, hup_next = 0;
144
145 void *dup_mem(void *b, size_t c);
146 void *copy_of(char *s);
147 static void usage(void);
148 void chat_logf(const char *fmt, ...);
149 void fatal(int code, const char *fmt, ...);
150 SIGTYPE sigalrm(int signo);
151 SIGTYPE sigint(int signo);
152 SIGTYPE sigterm(int signo);
153 SIGTYPE sighup(int signo);
154 void init(void);
155 void set_tty_parameters(void);
156 void echo_stderr(int);
157 void break_sequence(void);
158 void terminate(int status);
159 void do_file(char *chatfile);
160 int  get_string(char *string);
161 int  put_string(char *s);
162 int  write_char(int c);
163 int  put_char(int c);
164 int  get_char(void);
165 void chat_send(char *s);
166 char *character(int c);
167 void chat_expect(char *s);
168 char *clean(char *s, int sending);
169 void pack_array(char **array, int end);
170 char *expect_strtok(char *, const char *);
171 int vfmtmsg(char *, int, const char *, va_list);        /* vsprintf++ */
172
173 void *
174 dup_mem(void *b, size_t c)
175 {
176     void *ans = malloc (c);
177     if (!ans)
178         fatal(2, "memory error!");
179
180     memcpy (ans, b, c);
181     return ans;
182 }
183
184 void *
185 copy_of(char *s)
186 {
187     return dup_mem (s, strlen (s) + 1);
188 }
189
190 /*
191  * chat [-esSvV] [-f chat-file] [-r report-file] [-t timeout]
192  *      [-T phone-number] [-U phone-number2] [chat-script]
193  * where chat-script has the form:
194  *      [...[[expect[-send[-expect...]] send expect[-send[-expect]] ...]]]
195  *
196  * Perform a UUCP-dialer-like chat script on stdin and stdout.
197  */
198 int
199 main(int argc, char *argv[])
200 {
201     int option;
202
203     tzset();
204
205     while ((option = getopt(argc, argv, "ef:r:sSt:T:U:vV")) != -1) {
206         switch (option) {
207         case 'e':
208             ++echo;
209             break;
210
211         case 'f':
212             if (chat_file != NULL)
213                 free(chat_file);
214             chat_file = copy_of(optarg);
215             break;
216
217         case 'r':
218             if (report_fp != NULL)
219                 fclose(report_fp);
220             if (report_file != NULL)
221                 free(report_file);
222             report_file = copy_of(optarg);
223             report_fp = fopen(report_file, "a");
224             if (report_fp != NULL) {
225                 if (verbose)
226                     fprintf(report_fp, "Opening \"%s\"...\n", report_file);
227             } else
228                 fatal(2, "cannot open \"%s\" for appending", report_file);
229             break;
230
231         case 's':
232             ++to_stderr;
233             break;
234
235         case 'S':
236             to_log = 0;
237             break;
238
239         case 't':
240             timeout = atoi(optarg);
241             break;
242
243         case 'T':
244             if (phone_num != NULL)
245                 free(phone_num);
246             phone_num = copy_of(optarg);
247             break;
248
249         case 'U':
250             if (phone_num2 != NULL)
251                 free(phone_num2);
252             phone_num2 = copy_of(optarg);
253             break;
254
255         case 'v':
256             ++verbose;
257             break;
258
259         case 'V':
260             ++Verbose;
261             break;
262
263         default:
264             usage();
265             break;
266         }
267     }
268
269     argc -= optind;
270     argv += optind;
271
272 /*
273  * Default the report file to the stderr location
274  */
275     if (report_fp == NULL)
276         report_fp = stderr;
277
278     if (to_log) {
279         openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
280
281         if (verbose)
282             setlogmask(LOG_UPTO(LOG_INFO));
283         else
284             setlogmask(LOG_UPTO(LOG_WARNING));
285     }
286
287     if (chat_file != NULL) {
288         if (*argv != NULL)
289             usage();
290         else {
291             init();
292             do_file(chat_file);
293         }
294     } else {
295         init();
296         while (*argv != NULL && argc > 0) {
297             chat_expect(*argv);
298             argv++;
299             argc--;
300
301             if (*argv != NULL && argc > 0) {
302                 chat_send(*argv);
303                 argv++;
304                 argc--;
305             }
306         }
307     }
308
309     terminate(0);
310     return 0;
311 }
312
313 /*
314  *  Process a chat script when read from a file.
315  */
316
317 void
318 do_file(char *chatfile)
319 {
320     int linect, sendflg;
321     char *sp, *arg, quote;
322     char buf [STR_LEN];
323     FILE *cfp;
324
325     cfp = fopen (chatfile, "r");
326     if (cfp == NULL)
327         fatal(1, "%s -- open failed: %m", chatfile);
328
329     linect = 0;
330     sendflg = 0;
331
332     while (fgets(buf, STR_LEN, cfp) != NULL) {
333         sp = strchr (buf, '\n');
334         if (sp)
335             *sp = '\0';
336
337         linect++;
338         sp = buf;
339
340         /* lines starting with '#' are comments. If a real '#'
341            is to be expected, it should be quoted .... */
342         if ( *sp == '#' )
343             continue;
344
345         while (*sp != '\0') {
346             if (*sp == ' ' || *sp == '\t') {
347                 ++sp;
348                 continue;
349             }
350
351             if (*sp == '"' || *sp == '\'') {
352                 quote = *sp++;
353                 arg = sp;
354                 while (*sp != quote) {
355                     if (*sp == '\0')
356                         fatal(1, "unterminated quote (line %d)", linect);
357
358                     if (*sp++ == '\\') {
359                         if (*sp != '\0')
360                             ++sp;
361                     }
362                 }
363             }
364             else {
365                 arg = sp;
366                 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
367                     ++sp;
368             }
369
370             if (*sp != '\0')
371                 *sp++ = '\0';
372
373             if (sendflg)
374                 chat_send (arg);
375             else
376                 chat_expect (arg);
377             sendflg = !sendflg;
378         }
379     }
380     fclose (cfp);
381 }
382
383 /*
384  *      We got an error parsing the command line.
385  */
386 static void
387 usage(void)
388 {
389     fprintf(stderr,
390       "Usage: chat [-esSvV] [-f chat-file] [-r report-file] [-t timeout]\n"
391       "            [-T phone-number] [-U phone-number2] [chat-script]\n"
392       "where chat-script has the form:\n"
393       "            [...[[expect[-send[-expect...]] send expect[-send[-expect]] ...]]]\n");
394     exit(1);
395 }
396
397 char line[1024];
398
399 /*
400  * Send a message to syslog and/or stderr.
401  */
402 void
403 chat_logf(const char *fmt, ...)
404 {
405     va_list args;
406
407     va_start(args, fmt);
408     vfmtmsg(line, sizeof(line), fmt, args);
409     if (to_log)
410         syslog(LOG_INFO, "%s", line);
411     if (to_stderr)
412         fprintf(stderr, "%s\n", line);
413 }
414
415 /*
416  *      Print an error message and terminate.
417  */
418
419 void
420 fatal(int code, const char *fmt, ...)
421 {
422     va_list args;
423
424     va_start(args, fmt);
425     vfmtmsg(line, sizeof(line), fmt, args);
426     if (to_log)
427         syslog(LOG_ERR, "%s", line);
428     if (to_stderr)
429         fprintf(stderr, "%s\n", line);
430     terminate(code);
431 }
432
433 int alarmed = 0;
434
435 SIGTYPE sigalrm(int signo __unused)
436 {
437     int flags;
438
439     alarm(1);
440     alarmed = 1;                /* Reset alarm to avoid race window */
441     signal(SIGALRM, sigalrm);   /* that can cause hanging in read() */
442
443     if ((flags = fcntl(0, F_GETFL, 0)) == -1)
444         fatal(2, "Can't get file mode flags on stdin: %m");
445
446     if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
447         fatal(2, "Can't set file mode flags on stdin: %m");
448
449     if (verbose)
450         chat_logf("alarm");
451 }
452
453 SIGTYPE sigint(int signo __unused)
454 {
455     fatal(2, "SIGINT");
456 }
457
458 SIGTYPE sigterm(int signo __unused)
459 {
460     fatal(2, "SIGTERM");
461 }
462
463 SIGTYPE sighup(int signo __unused)
464 {
465     fatal(2, "SIGHUP");
466 }
467
468 void init(void)
469 {
470     signal(SIGINT, sigint);
471     signal(SIGTERM, sigterm);
472     signal(SIGHUP, sighup);
473
474     set_tty_parameters();
475     signal(SIGALRM, sigalrm);
476     alarm(0);
477     alarmed = 0;
478 }
479
480 void set_tty_parameters(void)
481 {
482 #if defined(get_term_param)
483     term_parms t;
484
485     if (get_term_param (&t) < 0)
486         fatal(2, "Can't get terminal parameters: %m");
487
488     saved_tty_parameters = t;
489     have_tty_parameters  = 1;
490
491     t.c_iflag     |= IGNBRK | ISTRIP | IGNPAR;
492     t.c_oflag      = 0;
493     t.c_lflag      = 0;
494     t.c_cc[VERASE] =
495     t.c_cc[VKILL]  = 0;
496     t.c_cc[VMIN]   = 1;
497     t.c_cc[VTIME]  = 0;
498
499     if (set_term_param (&t) < 0)
500         fatal(2, "Can't set terminal parameters: %m");
501 #endif
502 }
503
504 void break_sequence(void)
505 {
506     tcsendbreak (0, 0);
507 }
508
509 void terminate(int status)
510 {
511     echo_stderr(-1);
512     if (report_file != NULL && report_fp != NULL) {
513 /*
514  * Allow the last of the report string to be gathered before we terminate.
515  */
516         if (report_gathering) {
517             int c;
518             size_t rep_len;
519
520             rep_len = strlen(report_buffer);
521             while (rep_len + 1 <= sizeof(report_buffer)) {
522                 alarm(1);
523                 c = get_char();
524                 alarm(0);
525                 if (c < 0 || iscntrl(c))
526                     break;
527                 report_buffer[rep_len] = c;
528                 ++rep_len;
529             }
530             report_buffer[rep_len] = 0;
531             fprintf (report_fp, "chat:  %s\n", report_buffer);
532         }
533         if (verbose)
534             fprintf (report_fp, "Closing \"%s\".\n", report_file);
535         fclose (report_fp);
536         report_fp = NULL;
537     }
538
539 #if defined(get_term_param)
540     if (have_tty_parameters) {
541         if (set_term_param (&saved_tty_parameters) < 0)
542             fatal(2, "Can't restore terminal parameters: %m");
543     }
544 #endif
545
546     exit(status);
547 }
548
549 /*
550  *      'Clean up' this string.
551  */
552 char *
553 clean(char *s, int sending)
554 {
555     char temp[STR_LEN], cur_chr;
556     char *s1, *phchar;
557     int add_return = sending;
558 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
559
560     s1 = temp;
561     /* Don't overflow buffer, leave room for chars we append later */
562     while (*s && s1 - temp < (off_t)(sizeof(temp) - 2 - add_return)) {
563         cur_chr = *s++;
564         if (cur_chr == '^') {
565             cur_chr = *s++;
566             if (cur_chr == '\0') {
567                 *s1++ = '^';
568                 break;
569             }
570             cur_chr &= 0x1F;
571             if (cur_chr != 0) {
572                 *s1++ = cur_chr;
573             }
574             continue;
575         }
576
577         if (cur_chr != '\\') {
578             *s1++ = cur_chr;
579             continue;
580         }
581
582         cur_chr = *s++;
583         if (cur_chr == '\0') {
584             if (sending) {
585                 *s1++ = '\\';
586                 *s1++ = '\\';
587             }
588             break;
589         }
590
591         switch (cur_chr) {
592         case 'b':
593             *s1++ = '\b';
594             break;
595
596         case 'c':
597             if (sending && *s == '\0')
598                 add_return = 0;
599             else
600                 *s1++ = cur_chr;
601             break;
602
603         case '\\':
604         case 'K':
605         case 'p':
606         case 'd':
607             if (sending)
608                 *s1++ = '\\';
609
610             *s1++ = cur_chr;
611             break;
612
613         case 'T':
614             if (sending && phone_num) {
615                 for ( phchar = phone_num; *phchar != '\0'; phchar++) 
616                     *s1++ = *phchar;
617             }
618             else {
619                 *s1++ = '\\';
620                 *s1++ = 'T';
621             }
622             break;
623
624         case 'U':
625             if (sending && phone_num2) {
626                 for ( phchar = phone_num2; *phchar != '\0'; phchar++) 
627                     *s1++ = *phchar;
628             }
629             else {
630                 *s1++ = '\\';
631                 *s1++ = 'U';
632             }
633             break;
634
635         case 'q':
636             quiet = 1;
637             break;
638
639         case 'r':
640             *s1++ = '\r';
641             break;
642
643         case 'n':
644             *s1++ = '\n';
645             break;
646
647         case 's':
648             *s1++ = ' ';
649             break;
650
651         case 't':
652             *s1++ = '\t';
653             break;
654
655         case 'N':
656             if (sending) {
657                 *s1++ = '\\';
658                 *s1++ = '\0';
659             }
660             else
661                 *s1++ = 'N';
662             break;
663             
664         default:
665             if (isoctal (cur_chr)) {
666                 cur_chr &= 0x07;
667                 if (isoctal (*s)) {
668                     cur_chr <<= 3;
669                     cur_chr |= *s++ - '0';
670                     if (isoctal (*s)) {
671                         cur_chr <<= 3;
672                         cur_chr |= *s++ - '0';
673                     }
674                 }
675
676                 if (cur_chr != 0 || sending) {
677                     if (sending && (cur_chr == '\\' || cur_chr == 0))
678                         *s1++ = '\\';
679                     *s1++ = cur_chr;
680                 }
681                 break;
682             }
683
684             if (sending)
685                 *s1++ = '\\';
686             *s1++ = cur_chr;
687             break;
688         }
689     }
690
691     if (add_return)
692         *s1++ = '\r';
693
694     *s1++ = '\0'; /* guarantee closure */
695     *s1++ = '\0'; /* terminate the string */
696     return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
697 }
698
699 /*
700  * A modified version of 'strtok'. This version skips \ sequences.
701  */
702
703 char *
704 expect_strtok (char *s, const char *term)
705 {
706     static  char *str   = blank;
707     int     escape_flag = 0;
708     char   *result;
709
710 /*
711  * If a string was specified then do initial processing.
712  */
713     if (s)
714         str = s;
715
716 /*
717  * If this is the escape flag then reset it and ignore the character.
718  */
719     if (*str)
720         result = str;
721     else
722         result = NULL;
723
724     while (*str) {
725         if (escape_flag) {
726             escape_flag = 0;
727             ++str;
728             continue;
729         }
730
731         if (*str == '\\') {
732             ++str;
733             escape_flag = 1;
734             continue;
735         }
736
737 /*
738  * If this is not in the termination string, continue.
739  */
740         if (strchr (term, *str) == NULL) {
741             ++str;
742             continue;
743         }
744
745 /*
746  * This is the terminator. Mark the end of the string and stop.
747  */
748         *str++ = '\0';
749         break;
750     }
751     return (result);
752 }
753
754 /*
755  * Process the expect string
756  */
757
758 void
759 chat_expect(char *s)
760 {
761     char *expect;
762     char *reply;
763
764     if (strcmp(s, "HANGUP") == 0) {
765         ++hup_next;
766         return;
767     }
768  
769     if (strcmp(s, "ABORT") == 0) {
770         ++abort_next;
771         return;
772     }
773
774     if (strcmp(s, "CLR_ABORT") == 0) {
775         ++clear_abort_next;
776         return;
777     }
778
779     if (strcmp(s, "REPORT") == 0) {
780         ++report_next;
781         return;
782     }
783
784     if (strcmp(s, "CLR_REPORT") == 0) {
785         ++clear_report_next;
786         return;
787     }
788
789     if (strcmp(s, "TIMEOUT") == 0) {
790         ++timeout_next;
791         return;
792     }
793
794     if (strcmp(s, "ECHO") == 0) {
795         ++echo_next;
796         return;
797     }
798
799     if (strcmp(s, "SAY") == 0) {
800         ++say_next;
801         return;
802     }
803
804 /*
805  * Fetch the expect and reply string.
806  */
807     for (;;) {
808         expect = expect_strtok (s, "-");
809         s      = NULL;
810
811         if (expect == NULL)
812             return;
813
814         reply = expect_strtok (s, "-");
815
816 /*
817  * Handle the expect string. If successful then exit.
818  */
819         if (get_string (expect))
820             return;
821
822 /*
823  * If there is a sub-reply string then send it. Otherwise any condition
824  * is terminal.
825  */
826         if (reply == NULL || exit_code != 3)
827             break;
828
829         chat_send (reply);
830     }
831
832 /*
833  * The expectation did not occur. This is terminal.
834  */
835     if (fail_reason)
836         chat_logf("Failed (%s)", fail_reason);
837     else
838         chat_logf("Failed");
839     terminate(exit_code);
840 }
841
842 /*
843  * Translate the input character to the appropriate string for printing
844  * the data.
845  */
846
847 char *
848 character(int c)
849 {
850     static char string[10];
851     const char *meta;
852
853     meta = (c & 0x80) ? "M-" : "";
854     c &= 0x7F;
855
856     if (c < 32)
857         sprintf(string, "%s^%c", meta, (int)c + '@');
858     else if (c == 127)
859         sprintf(string, "%s^?", meta);
860     else
861         sprintf(string, "%s%c", meta, c);
862
863     return (string);
864 }
865
866 /*
867  *  process the reply string
868  */
869 void
870 chat_send(char *s)
871 {
872     if (say_next) {
873         say_next = 0;
874         s = clean(s,0);
875         write(STDERR_FILENO, s, strlen(s));
876         free(s);
877         return;
878     }
879
880     if (hup_next) {
881         hup_next = 0;
882         if (strcmp(s, "OFF") == 0)
883            signal(SIGHUP, SIG_IGN);
884         else
885            signal(SIGHUP, sighup);
886         return;
887     }
888
889     if (echo_next) {
890         echo_next = 0;
891         echo = (strcmp(s, "ON") == 0);
892         return;
893     }
894
895     if (abort_next) {
896         char *s1;
897         
898         abort_next = 0;
899         
900         if (n_aborts >= MAX_ABORTS)
901             fatal(2, "Too many ABORT strings");
902         
903         s1 = clean(s, 0);
904         
905         if (strlen(s1) > strlen(s)
906             || strlen(s1) + 1 > sizeof(fail_buffer))
907             fatal(1, "Illegal or too-long ABORT string ('%v')", s);
908
909         abort_string[n_aborts++] = s1;
910
911         if (verbose)
912             chat_logf("abort on (%v)", s);
913         return;
914     }
915
916     if (clear_abort_next) {
917         char *s1;
918         int   i;
919         int   old_max;
920         int   pack = 0;
921         
922         clear_abort_next = 0;
923         
924         s1 = clean(s, 0);
925         
926         if (strlen(s1) > strlen(s)
927             || strlen(s1) + 1 > sizeof(fail_buffer))
928             fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
929
930         old_max = n_aborts;
931         for (i=0; i < n_aborts; i++) {
932             if ( strcmp(s1,abort_string[i]) == 0 ) {
933                 free(abort_string[i]);
934                 abort_string[i] = NULL;
935                 pack++;
936                 n_aborts--;
937                 if (verbose)
938                     chat_logf("clear abort on (%v)", s);
939             }
940         }
941         free(s1);
942         if (pack)
943             pack_array(abort_string,old_max);
944         return;
945     }
946
947     if (report_next) {
948         char *s1;
949         
950         report_next = 0;
951         if (n_reports >= MAX_REPORTS)
952             fatal(2, "Too many REPORT strings");
953         
954         s1 = clean(s, 0);
955         
956         if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
957             fatal(1, "Illegal or too-long REPORT string ('%v')", s);
958         
959         report_string[n_reports++] = s1;
960         
961         if (verbose)
962             chat_logf("report (%v)", s);
963         return;
964     }
965
966     if (clear_report_next) {
967         char *s1;
968         int   i;
969         int   old_max;
970         int   pack = 0;
971         
972         clear_report_next = 0;
973         
974         s1 = clean(s, 0);
975         
976         if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
977             fatal(1, "Illegal or too-long REPORT string ('%v')", s);
978
979         old_max = n_reports;
980         for (i=0; i < n_reports; i++) {
981             if ( strcmp(s1,report_string[i]) == 0 ) {
982                 free(report_string[i]);
983                 report_string[i] = NULL;
984                 pack++;
985                 n_reports--;
986                 if (verbose)
987                     chat_logf("clear report (%v)", s);
988             }
989         }
990         free(s1);
991         if (pack)
992             pack_array(report_string,old_max);
993         
994         return;
995     }
996
997     if (timeout_next) {
998         timeout_next = 0;
999         timeout = atoi(s);
1000         
1001         if (timeout <= 0)
1002             timeout = DEFAULT_CHAT_TIMEOUT;
1003
1004         if (verbose)
1005             chat_logf("timeout set to %d seconds", timeout);
1006
1007         return;
1008     }
1009
1010     if (strcmp(s, "EOT") == 0)
1011         s = strdup("^D\\c");
1012     else if (strcmp(s, "BREAK") == 0)
1013         s = strdup("\\K\\c");
1014
1015     if (!put_string(s))
1016         fatal(1, "Failed");
1017 }
1018
1019 int
1020 get_char(void)
1021 {
1022     int status;
1023     char c;
1024
1025     status = read(STDIN_FILENO, &c, 1);
1026
1027     switch (status) {
1028     case 1:
1029         return ((int)c & 0x7F);
1030
1031     default:
1032         chat_logf("warning: read() on stdin returned %d", status);
1033
1034     case -1:
1035         if ((status = fcntl(0, F_GETFL, 0)) == -1)
1036             fatal(2, "Can't get file mode flags on stdin: %m");
1037
1038         if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1039             fatal(2, "Can't set file mode flags on stdin: %m");
1040         
1041         return (-1);
1042     }
1043 }
1044
1045 int put_char(int c)
1046 {
1047     int status;
1048     char ch = c;
1049
1050     usleep(10000);              /* inter-character typing delay (?) */
1051
1052     status = write(STDOUT_FILENO, &ch, 1);
1053
1054     switch (status) {
1055     case 1:
1056         return (0);
1057         
1058     default:
1059         chat_logf("warning: write() on stdout returned %d", status);
1060         
1061     case -1:
1062         if ((status = fcntl(0, F_GETFL, 0)) == -1)
1063             fatal(2, "Can't get file mode flags on stdin, %m");
1064
1065         if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1066             fatal(2, "Can't set file mode flags on stdin: %m");
1067         
1068         return (-1);
1069     }
1070 }
1071
1072 int
1073 write_char(int c)
1074 {
1075     if (alarmed || put_char(c) < 0) {
1076         alarm(0);
1077         alarmed = 0;
1078
1079         if (verbose) {
1080             if (errno == EINTR || errno == EWOULDBLOCK)
1081                 chat_logf(" -- write timed out");
1082             else
1083                 chat_logf(" -- write failed: %m");
1084         }
1085         return (0);
1086     }
1087     return (1);
1088 }
1089
1090 int
1091 put_string(char *s)
1092 {
1093     quiet = 0;
1094     s = clean(s, 1);
1095
1096     if (verbose)
1097         chat_logf("send (%v)", quiet ? "??????" : s);
1098
1099     alarm(timeout); alarmed = 0;
1100
1101     while (*s) {
1102         char c = *s++;
1103
1104         if (c != '\\') {
1105             if (!write_char (c))
1106                 return 0;
1107             continue;
1108         }
1109
1110         c = *s++;
1111         switch (c) {
1112         case 'd':
1113             sleep(1);
1114             break;
1115
1116         case 'K':
1117             break_sequence();
1118             break;
1119
1120         case 'p':
1121             usleep(10000);      /* 1/100th of a second (arg is microseconds) */
1122             break;
1123
1124         default:
1125             if (!write_char (c))
1126                 return 0;
1127             break;
1128         }
1129     }
1130
1131     alarm(0);
1132     alarmed = 0;
1133     return (1);
1134 }
1135
1136 /*
1137  *      Echo a character to stderr.
1138  *      When called with -1, a '\n' character is generated when
1139  *      the cursor is not at the beginning of a line.
1140  */
1141 void
1142 echo_stderr(int n)
1143 {
1144     static int need_lf;
1145     char *s;
1146
1147     switch (n) {
1148     case '\r':          /* ignore '\r' */
1149         break;
1150     case -1:
1151         if (need_lf == 0)
1152             break;
1153         /* FALLTHROUGH */
1154     case '\n':
1155         write(STDERR_FILENO, "\n", 1);
1156         need_lf = 0;
1157         break;
1158     default:
1159         s = character(n);
1160         write(STDERR_FILENO, s, strlen(s));
1161         need_lf = 1;
1162         break;
1163     }
1164 }
1165
1166 /*
1167  *      'Wait for' this string to appear on this file descriptor.
1168  */
1169 int
1170 get_string(char *string)
1171 {
1172     char temp[STR_LEN];
1173     int c, printed = 0;
1174     size_t len, minlen;
1175     char *s = temp, *end = s + STR_LEN;
1176     char *logged = temp;
1177
1178     fail_reason = NULL;
1179
1180     if (strlen(string) > STR_LEN) {
1181         chat_logf("expect string is too long");
1182         exit_code = 1;
1183         return 0;
1184     }
1185
1186     string = clean(string, 0);
1187     len = strlen(string);
1188     minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1189
1190     if (verbose)
1191         chat_logf("expect (%v)", string);
1192
1193     if (len == 0) {
1194         if (verbose)
1195             chat_logf("got it");
1196         return (1);
1197     }
1198
1199     alarm(timeout);
1200     alarmed = 0;
1201
1202     while ( ! alarmed && (c = get_char()) >= 0) {
1203         int n, abort_len, report_len;
1204
1205         if (echo)
1206             echo_stderr(c);
1207         if (verbose && c == '\n') {
1208             if (s == logged)
1209                 chat_logf("");  /* blank line */
1210             else
1211                 chat_logf("%0.*v", s - logged, logged);
1212             logged = s + 1;
1213         }
1214
1215         *s++ = c;
1216
1217         if (verbose && s >= logged + 80) {
1218             chat_logf("%0.*v", s - logged, logged);
1219             logged = s;
1220         }
1221
1222         if (Verbose) {
1223            if (c == '\n')
1224                fputc( '\n', stderr );
1225            else if (c != '\r')
1226                fprintf( stderr, "%s", character(c) );
1227         }
1228
1229         if (!report_gathering) {
1230             for (n = 0; n < n_reports; ++n) {
1231                 if ((report_string[n] != NULL) &&
1232                     s - temp >= (report_len = strlen(report_string[n])) &&
1233                     strncmp(s - report_len, report_string[n], report_len) == 0) {
1234                     time_t time_now   = time (NULL);
1235                     struct tm* tm_now = localtime (&time_now);
1236
1237                     strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1238                     strcat (report_buffer, report_string[n]);
1239
1240                     report_string[n] = NULL;
1241                     report_gathering = 1;
1242                     break;
1243                 }
1244             }
1245         }
1246         else {
1247             if (!iscntrl (c)) {
1248                 int rep_len = strlen (report_buffer);
1249                 report_buffer[rep_len]     = c;
1250                 report_buffer[rep_len + 1] = '\0';
1251             }
1252             else {
1253                 report_gathering = 0;
1254                 fprintf (report_fp, "chat:  %s\n", report_buffer);
1255             }
1256         }
1257
1258         if ((size_t)(s - temp) >= len &&
1259             c == string[len - 1] &&
1260             strncmp(s - len, string, len) == 0) {
1261             if (verbose) {
1262                 if (s > logged)
1263                     chat_logf("%0.*v", s - logged, logged);
1264                 chat_logf(" -- got it\n");
1265             }
1266
1267             alarm(0);
1268             alarmed = 0;
1269             return (1);
1270         }
1271
1272         for (n = 0; n < n_aborts; ++n) {
1273             if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1274                 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1275                 if (verbose) {
1276                     if (s > logged)
1277                         chat_logf("%0.*v", s - logged, logged);
1278                     chat_logf(" -- failed");
1279                 }
1280
1281                 alarm(0);
1282                 alarmed = 0;
1283                 exit_code = n + 4;
1284                 strcpy(fail_reason = fail_buffer, abort_string[n]);
1285                 return (0);
1286             }
1287         }
1288
1289         if (s >= end) {
1290             if (logged < s - minlen) {
1291                 chat_logf("%0.*v", s - logged, logged);
1292                 logged = s;
1293             }
1294             s -= minlen;
1295             memmove(temp, s, minlen);
1296             logged = temp + (logged - s);
1297             s = temp + minlen;
1298         }
1299
1300         if (alarmed && verbose)
1301             chat_logf("warning: alarm synchronization problem");
1302     }
1303
1304     alarm(0);
1305     
1306     if (verbose && printed) {
1307         if (alarmed)
1308             chat_logf(" -- read timed out");
1309         else
1310             chat_logf(" -- read failed: %m");
1311     }
1312
1313     exit_code = 3;
1314     alarmed   = 0;
1315     return (0);
1316 }
1317
1318 void
1319 pack_array(char **array, int end)
1320 {
1321     int i, j;
1322
1323     for (i = 0; i < end; i++) {
1324         if (array[i] == NULL) {
1325             for (j = i+1; j < end; ++j)
1326                 if (array[j] != NULL)
1327                     array[i++] = array[j];
1328             for (; i < end; ++i)
1329                 array[i] = NULL;
1330             break;
1331         }
1332     }
1333 }
1334
1335 /*
1336  * vfmtmsg - format a message into a buffer.  Like vsprintf except we
1337  * also specify the length of the output buffer, and we handle the
1338  * %m (error message) format.
1339  * Doesn't do floating-point formats.
1340  * Returns the number of chars put into buf.
1341  */
1342 #define OUTCHAR(c)      (buflen > 0? (--buflen, *buf++ = (c)): 0)
1343
1344 int
1345 vfmtmsg(char *buf, int buflen, const char *fmt, va_list args)
1346 {
1347     int c, i, n;
1348     int width, prec, fillch;
1349     int base, len, neg, quoted;
1350     unsigned long val = 0;
1351     char *str, *buf0;
1352     const char *f;
1353     unsigned char *p;
1354     char num[32];
1355     static char hexchars[] = "0123456789abcdef";
1356
1357     buf0 = buf;
1358     --buflen;
1359     while (buflen > 0) {
1360         for (f = fmt; *f != '%' && *f != 0; ++f)
1361             ;
1362         if (f > fmt) {
1363             len = f - fmt;
1364             if (len > buflen)
1365                 len = buflen;
1366             memcpy(buf, fmt, len);
1367             buf += len;
1368             buflen -= len;
1369             fmt = f;
1370         }
1371         if (*fmt == 0)
1372             break;
1373         c = *++fmt;
1374         width = prec = 0;
1375         fillch = ' ';
1376         if (c == '0') {
1377             fillch = '0';
1378             c = *++fmt;
1379         }
1380         if (c == '*') {
1381             width = va_arg(args, int);
1382             c = *++fmt;
1383         } else {
1384             while (isdigit(c)) {
1385                 width = width * 10 + c - '0';
1386                 c = *++fmt;
1387             }
1388         }
1389         if (c == '.') {
1390             c = *++fmt;
1391             if (c == '*') {
1392                 prec = va_arg(args, int);
1393                 c = *++fmt;
1394             } else {
1395                 while (isdigit(c)) {
1396                     prec = prec * 10 + c - '0';
1397                     c = *++fmt;
1398                 }
1399             }
1400         }
1401         str = NULL;
1402         base = 0;
1403         neg = 0;
1404         ++fmt;
1405         switch (c) {
1406         case 'd':
1407             i = va_arg(args, int);
1408             if (i < 0) {
1409                 neg = 1;
1410                 val = -i;
1411             } else
1412                 val = i;
1413             base = 10;
1414             break;
1415         case 'o':
1416             val = va_arg(args, unsigned int);
1417             base = 8;
1418             break;
1419         case 'x':
1420             val = va_arg(args, unsigned int);
1421             base = 16;
1422             break;
1423         case 'p':
1424             val = (unsigned long) va_arg(args, void *);
1425             base = 16;
1426             neg = 2;
1427             break;
1428         case 's':
1429             str = va_arg(args, char *);
1430             break;
1431         case 'c':
1432             num[0] = va_arg(args, int);
1433             num[1] = 0;
1434             str = num;
1435             break;
1436         case 'm':
1437             str = strerror(errno);
1438             break;
1439         case 'v':               /* "visible" string */
1440         case 'q':               /* quoted string */
1441             quoted = c == 'q';
1442             p = va_arg(args, unsigned char *);
1443             if (fillch == '0' && prec > 0) {
1444                 n = prec;
1445             } else {
1446                 n = strlen((char *)p);
1447                 if (prec > 0 && prec < n)
1448                     n = prec;
1449             }
1450             while (n > 0 && buflen > 0) {
1451                 c = *p++;
1452                 --n;
1453                 if (!quoted && c >= 0x80) {
1454                     OUTCHAR('M');
1455                     OUTCHAR('-');
1456                     c -= 0x80;
1457                 }
1458                 if (quoted && (c == '"' || c == '\\'))
1459                     OUTCHAR('\\');
1460                 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1461                     if (quoted) {
1462                         OUTCHAR('\\');
1463                         switch (c) {
1464                         case '\t':      OUTCHAR('t');   break;
1465                         case '\n':      OUTCHAR('n');   break;
1466                         case '\b':      OUTCHAR('b');   break;
1467                         case '\f':      OUTCHAR('f');   break;
1468                         default:
1469                             OUTCHAR('x');
1470                             OUTCHAR(hexchars[c >> 4]);
1471                             OUTCHAR(hexchars[c & 0xf]);
1472                         }
1473                     } else {
1474                         if (c == '\t')
1475                             OUTCHAR(c);
1476                         else {
1477                             OUTCHAR('^');
1478                             OUTCHAR(c ^ 0x40);
1479                         }
1480                     }
1481                 } else
1482                     OUTCHAR(c);
1483             }
1484             continue;
1485         default:
1486             *buf++ = '%';
1487             if (c != '%')
1488                 --fmt;          /* so %z outputs %z etc. */
1489             --buflen;
1490             continue;
1491         }
1492         if (base != 0) {
1493             str = num + sizeof(num);
1494             *--str = 0;
1495             while (str > num + neg) {
1496                 *--str = hexchars[val % base];
1497                 val = val / base;
1498                 if (--prec <= 0 && val == 0)
1499                     break;
1500             }
1501             switch (neg) {
1502             case 1:
1503                 *--str = '-';
1504                 break;
1505             case 2:
1506                 *--str = 'x';
1507                 *--str = '0';
1508                 break;
1509             }
1510             len = num + sizeof(num) - 1 - str;
1511         } else {
1512             len = strlen(str);
1513             if (prec > 0 && len > prec)
1514                 len = prec;
1515         }
1516         if (width > 0) {
1517             if (width > buflen)
1518                 width = buflen;
1519             if ((n = width - len) > 0) {
1520                 buflen -= n;
1521                 for (; n > 0; --n)
1522                     *buf++ = fillch;
1523             }
1524         }
1525         if (len > buflen)
1526             len = buflen;
1527         memcpy(buf, str, len);
1528         buf += len;
1529         buflen -= len;
1530     }
1531     *buf = 0;
1532     return buf - buf0;
1533 }