Deleted Added
full compact
chat.c (8874) chat.c (11990)
1/*
2 * Chat -- a program for automatic session establishment (i.e. dial
3 * the phone and log in).
4 *
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 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.
13 *
5 * This software is in the public domain.
6 *
7 * Please send all bug reports, requests for information, etc. to:
8 *
9 * Al Longyear (longyear@netcom.com)
10 * (I was the last person to change this code.)
11 *
14 * This software is in the public domain.
15 *
16 * Please send all bug reports, requests for information, etc. to:
17 *
18 * Al Longyear (longyear@netcom.com)
19 * (I was the last person to change this code.)
20 *
21 * Added -r "report file" switch & REPORT keyword.
22 * Robert Geer <bgeer@xmission.com>
23 *
12 * The original author is:
13 *
14 * Karl Fox <karl@MorningStar.Com>
15 * Morning Star Technologies, Inc.
16 * 1760 Zollinger Road
17 * Columbus, OH 43221
18 * (614)451-1883
24 * The original author is:
25 *
26 * Karl Fox <karl@MorningStar.Com>
27 * Morning Star Technologies, Inc.
28 * 1760 Zollinger Road
29 * Columbus, OH 43221
30 * (614)451-1883
31 *
19 */
20
32 */
33
21static char rcsid[] = "$Id: chat.c,v 1.2 1994/12/19 01:02:11 ache Exp $";
34static char rcsid[] = "$Id: chat.c,v 1.1.1.1 1994/11/12 05:25:32 lars Exp $";
22
23#include <stdio.h>
35
36#include <stdio.h>
37#include <time.h>
24#include <fcntl.h>
25#include <signal.h>
26#include <errno.h>
27#include <string.h>
28#include <stdlib.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <syslog.h>
32
33#ifndef TERMIO
34#undef TERMIOS
35#define TERMIOS
36#endif
37
38#include <fcntl.h>
39#include <signal.h>
40#include <errno.h>
41#include <string.h>
42#include <stdlib.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <syslog.h>
46
47#ifndef TERMIO
48#undef TERMIOS
49#define TERMIOS
50#endif
51
38#ifdef sun
39# if defined(SUNOS) && SUNOS >= 41
40# ifndef HDB
41# define HDB
42# endif
43# endif
44#endif
45
46#ifdef TERMIO
47#include <termio.h>
48#endif
49#ifdef TERMIOS
50#include <termios.h>
51#endif
52
53#define STR_LEN 1024

--- 5 unchanged lines hidden (view full) ---

59#ifdef __STDC__
60#undef __P
61#define __P(x) x
62#else
63#define __P(x) ()
64#define const
65#endif
66
52#ifdef TERMIO
53#include <termio.h>
54#endif
55#ifdef TERMIOS
56#include <termios.h>
57#endif
58
59#define STR_LEN 1024

--- 5 unchanged lines hidden (view full) ---

65#ifdef __STDC__
66#undef __P
67#define __P(x) x
68#else
69#define __P(x) ()
70#define const
71#endif
72
73#ifndef O_NONBLOCK
74#define O_NONBLOCK O_NDELAY
75#endif
76
67/*************** Micro getopt() *********************************************/
68#define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
69 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
70 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
71#define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
72 (_O=4,(char*)0):(char*)0)
73#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
74#define ARG(c,v) (c?(--c,*v++):(char*)0)
75
76static int _O = 0; /* Internal state */
77/*************** Micro getopt() *********************************************/
78
79char *program_name;
80
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)
85
86static int _O = 0; /* Internal state */
87/*************** Micro getopt() *********************************************/
88
89char *program_name;
90
81#ifndef LOCK_DIR
82# if defined(__NetBSD__) || defined(__FreeBSD__)
83# define PIDSTRING
84# define LOCK_DIR "/var/spool/lock"
85# else
86# ifdef HDB
87# define PIDSTRING
88# define LOCK_DIR "/usr/spool/locks"
89# else /* HDB */
90# define LOCK_DIR "/usr/spool/uucp"
91# endif /* HDB */
92# endif
93#endif /* LOCK_DIR */
94
95#define MAX_ABORTS 50
91#define MAX_ABORTS 50
92#define MAX_REPORTS 50
96#define DEFAULT_CHAT_TIMEOUT 45
97
93#define DEFAULT_CHAT_TIMEOUT 45
94
98int verbose = 0;
99int quiet = 0;
100char *lock_file = (char *)0;
101char *chat_file = (char *)0;
102int timeout = DEFAULT_CHAT_TIMEOUT;
95int verbose = 0;
96int quiet = 0;
97int report = 0;
98int exit_code = 0;
99FILE* report_fp = (FILE *) 0;
100char *report_file = (char *) 0;
101char *chat_file = (char *) 0;
102int timeout = DEFAULT_CHAT_TIMEOUT;
103
104int have_tty_parameters = 0;
103
104int have_tty_parameters = 0;
105
105#ifdef TERMIO
106#ifdef TERMIO
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)
106struct termio saved_tty_parameters;
107#endif
110struct termio saved_tty_parameters;
111#endif
112
108#ifdef TERMIOS
113#ifdef TERMIOS
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)
109struct termios saved_tty_parameters;
110#endif
111
112char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
113 fail_buffer[50];
114int n_aborts = 0, abort_next = 0, timeout_next = 0;
115
117struct termios saved_tty_parameters;
118#endif
119
120char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
121 fail_buffer[50];
122int n_aborts = 0, abort_next = 0, timeout_next = 0;
123
124char *report_string[MAX_REPORTS] ;
125char report_buffer[50] ;
126int n_reports = 0, report_next = 0, report_gathering = 0 ;
127
116void *dup_mem __P((void *b, size_t c));
117void *copy_of __P((char *s));
118void usage __P((void));
119void logf __P((const char *str));
120void logflush __P((void));
121void fatal __P((const char *msg));
122void sysfatal __P((const char *msg));
123SIGTYPE sigalrm __P((int signo));
124SIGTYPE sigint __P((int signo));
125SIGTYPE sigterm __P((int signo));
126SIGTYPE sighup __P((int signo));
127void unalarm __P((void));
128void init __P((void));
129void set_tty_parameters __P((void));
130void break_sequence __P((void));
131void terminate __P((int status));
132void do_file __P((char *chat_file));
128void *dup_mem __P((void *b, size_t c));
129void *copy_of __P((char *s));
130void usage __P((void));
131void logf __P((const char *str));
132void logflush __P((void));
133void fatal __P((const char *msg));
134void sysfatal __P((const char *msg));
135SIGTYPE sigalrm __P((int signo));
136SIGTYPE sigint __P((int signo));
137SIGTYPE sigterm __P((int signo));
138SIGTYPE sighup __P((int signo));
139void unalarm __P((void));
140void init __P((void));
141void set_tty_parameters __P((void));
142void break_sequence __P((void));
143void terminate __P((int status));
144void do_file __P((char *chat_file));
133void lock __P((void));
134void delay __P((void));
135int get_string __P((register char *string));
136int put_string __P((register char *s));
137int write_char __P((int c));
145int get_string __P((register char *string));
146int put_string __P((register char *s));
147int write_char __P((int c));
138int put_char __P((char c));
148int put_char __P((int c));
139int get_char __P((void));
140void chat_send __P((register char *s));
149int get_char __P((void));
150void chat_send __P((register char *s));
141char *character __P((char c));
151char *character __P((int c));
142void chat_expect __P((register char *s));
143char *clean __P((register char *s, int sending));
152void chat_expect __P((register char *s));
153char *clean __P((register char *s, int sending));
144void unlock __P((void));
145void lock __P((void));
146void break_sequence __P((void));
147void terminate __P((int status));
148void die __P((void));
149
150void *dup_mem(b, c)
151void *b;
152size_t c;
153 {
154 void *ans = malloc (c);
155 if (!ans)
154void break_sequence __P((void));
155void terminate __P((int status));
156void die __P((void));
157
158void *dup_mem(b, c)
159void *b;
160size_t c;
161 {
162 void *ans = malloc (c);
163 if (!ans)
164 {
156 fatal ("memory error!\n");
165 fatal ("memory error!\n");
166 }
157 memcpy (ans, b, c);
158 return ans;
159 }
160
161void *copy_of (s)
162char *s;
163 {
164 return dup_mem (s, strlen (s) + 1);
165 }
166
167/*
167 memcpy (ans, b, c);
168 return ans;
169 }
170
171void *copy_of (s)
172char *s;
173 {
174 return dup_mem (s, strlen (s) + 1);
175 }
176
177/*
168 * chat [ -v ] [ -t timeout ] [ -l lock-file ] [ -f chat-file ] \
178 * chat [ -v ] [ -t timeout ] [ -f chat-file ] [ -r report-file ] \
169 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
170 *
171 * Perform a UUCP-dialer-like chat script on stdin and stdout.
172 */
173int
174main(argc, argv)
175int argc;
176char **argv;
177 {
178 int option;
179 char *arg;
180
181 program_name = *argv;
179 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
180 *
181 * Perform a UUCP-dialer-like chat script on stdin and stdout.
182 */
183int
184main(argc, argv)
185int argc;
186char **argv;
187 {
188 int option;
189 char *arg;
190
191 program_name = *argv;
192 tzset();
182
183 while (option = OPTION(argc, argv))
193
194 while (option = OPTION(argc, argv))
195 {
184 switch (option)
185 {
186 case 'v':
187 ++verbose;
188 break;
189
190 case 'f':
191 if (arg = OPTARG(argc, argv))
196 switch (option)
197 {
198 case 'v':
199 ++verbose;
200 break;
201
202 case 'f':
203 if (arg = OPTARG(argc, argv))
204 {
192 chat_file = copy_of(arg);
205 chat_file = copy_of(arg);
206 }
193 else
207 else
208 {
194 usage();
209 usage();
195
210 }
196 break;
197
211 break;
212
198 case 'l':
199 if (arg = OPTARG(argc, argv))
200 lock_file = copy_of(arg);
201 else
202 usage();
203
204 break;
205
206 case 't':
207 if (arg = OPTARG(argc, argv))
213 case 't':
214 if (arg = OPTARG(argc, argv))
215 {
208 timeout = atoi(arg);
216 timeout = atoi(arg);
217 }
209 else
218 else
219 {
210 usage();
220 usage();
221 }
222 break;
211
223
224 case 'r':
225 arg = OPTARG (argc, argv);
226 if (arg)
227 {
228 if (report_fp != NULL)
229 {
230 fclose (report_fp);
231 }
232 report_file = copy_of (arg);
233 report_fp = fopen (report_file, "a");
234 if (report_fp != NULL)
235 {
236 if (verbose)
237 {
238 fprintf (report_fp, "Opening \"%s\"...\n",
239 report_file);
240 }
241 report = 1;
242 }
243 }
212 break;
213
214 default:
215 usage();
244 break;
245
246 default:
247 usage();
248 break;
216 }
249 }
250 }
251/*
252 * Default the report file to the stderr location
253 */
254 if (report_fp == NULL)
255 {
256 report_fp = stderr;
257 }
217
218#ifdef ultrix
219 openlog("chat", LOG_PID);
220#else
221 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
222
258
259#ifdef ultrix
260 openlog("chat", LOG_PID);
261#else
262 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
263
223 if (verbose) {
264 if (verbose)
265 {
224 setlogmask(LOG_UPTO(LOG_INFO));
266 setlogmask(LOG_UPTO(LOG_INFO));
225 } else {
267 }
268 else
269 {
226 setlogmask(LOG_UPTO(LOG_WARNING));
270 setlogmask(LOG_UPTO(LOG_WARNING));
227 }
271 }
228#endif
229
230 init();
272#endif
273
274 init();
231
275
232 if (chat_file != NULL)
233 {
234 arg = ARG(argc, argv);
235 if (arg != NULL)
276 if (chat_file != NULL)
277 {
278 arg = ARG(argc, argv);
279 if (arg != NULL)
280 {
236 usage();
281 usage();
282 }
237 else
283 else
284 {
238 do_file (chat_file);
285 do_file (chat_file);
286 }
239 }
240 else
241 {
242 while (arg = ARG(argc, argv))
243 {
244 chat_expect(arg);
245
246 if (arg = ARG(argc, argv))
287 }
288 else
289 {
290 while (arg = ARG(argc, argv))
291 {
292 chat_expect(arg);
293
294 if (arg = ARG(argc, argv))
295 {
247 chat_send(arg);
296 chat_send(arg);
297 }
248 }
249 }
250
251 terminate(0);
252 }
253
254/*
255 * Process a chat script when read from a file.
256 */
257
258void do_file (chat_file)
259char *chat_file;
260 {
261 int linect, len, sendflg;
262 char *sp, *arg, quote;
263 char buf [STR_LEN];
264 FILE *cfp;
265
298 }
299 }
300
301 terminate(0);
302 }
303
304/*
305 * Process a chat script when read from a file.
306 */
307
308void do_file (chat_file)
309char *chat_file;
310 {
311 int linect, len, sendflg;
312 char *sp, *arg, quote;
313 char buf [STR_LEN];
314 FILE *cfp;
315
266 if ((cfp = fopen (chat_file, "r")) == NULL)
316 cfp = fopen (chat_file, "r");
317 if (cfp == NULL)
267 {
268 syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
269 terminate (1);
270 }
271
272 linect = 0;
273 sendflg = 0;
274
275 while (fgets(buf, STR_LEN, cfp) != NULL)
276 {
277 sp = strchr (buf, '\n');
278 if (sp)
318 {
319 syslog (LOG_ERR, "%s -- open failed: %m", chat_file);
320 terminate (1);
321 }
322
323 linect = 0;
324 sendflg = 0;
325
326 while (fgets(buf, STR_LEN, cfp) != NULL)
327 {
328 sp = strchr (buf, '\n');
329 if (sp)
330 {
279 *sp = '\0';
331 *sp = '\0';
332 }
280
281 linect++;
282 sp = buf;
283 while (*sp != '\0')
284 {
285 if (*sp == ' ' || *sp == '\t')
286 {
287 ++sp;

--- 7 unchanged lines hidden (view full) ---

295 while (*sp != quote)
296 {
297 if (*sp == '\0')
298 {
299 syslog (LOG_ERR, "unterminated quote (line %d)",
300 linect);
301 terminate (1);
302 }
333
334 linect++;
335 sp = buf;
336 while (*sp != '\0')
337 {
338 if (*sp == ' ' || *sp == '\t')
339 {
340 ++sp;

--- 7 unchanged lines hidden (view full) ---

348 while (*sp != quote)
349 {
350 if (*sp == '\0')
351 {
352 syslog (LOG_ERR, "unterminated quote (line %d)",
353 linect);
354 terminate (1);
355 }
303
356
304 if (*sp++ == '\\')
357 if (*sp++ == '\\')
358 {
305 if (*sp != '\0')
359 if (*sp != '\0')
360 {
306 ++sp;
361 ++sp;
362 }
363 }
307 }
308 }
309 else
310 {
311 arg = sp;
312 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
364 }
365 }
366 else
367 {
368 arg = sp;
369 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
370 {
313 ++sp;
371 ++sp;
372 }
314 }
315
316 if (*sp != '\0')
373 }
374
375 if (*sp != '\0')
376 {
317 *sp++ = '\0';
377 *sp++ = '\0';
378 }
318
319 if (sendflg)
320 {
321 chat_send (arg);
322 }
323 else
324 {
325 chat_expect (arg);

--- 5 unchanged lines hidden (view full) ---

331 }
332
333/*
334 * We got an error parsing the command line.
335 */
336void usage()
337 {
338 fprintf(stderr, "\
379
380 if (sendflg)
381 {
382 chat_send (arg);
383 }
384 else
385 {
386 chat_expect (arg);

--- 5 unchanged lines hidden (view full) ---

392 }
393
394/*
395 * We got an error parsing the command line.
396 */
397void usage()
398 {
399 fprintf(stderr, "\
339Usage: %s [-v] [-l lock-file] [-t timeout] {-f chat-file || chat-script}\n",
400Usage: %s [-v] [-t timeout] [-r report-file] {-f chat-file | chat-script}\n",
340 program_name);
341 exit(1);
342 }
343
344char line[256];
345char *p;
346
347void logf (str)

--- 14 unchanged lines hidden (view full) ---

362 if (line[0] != 0)
363 {
364 syslog(LOG_INFO, "%s", line);
365 line[0] = 0;
366 }
367 }
368
369/*
401 program_name);
402 exit(1);
403 }
404
405char line[256];
406char *p;
407
408void logf (str)

--- 14 unchanged lines hidden (view full) ---

423 if (line[0] != 0)
424 {
425 syslog(LOG_INFO, "%s", line);
426 line[0] = 0;
427 }
428 }
429
430/*
370 * Unlock and terminate with an error.
431 * Terminate with an error.
371 */
372void die()
373 {
432 */
433void die()
434 {
374 unlock();
375 terminate(1);
376 }
377
378/*
379 * Print an error message and terminate.
380 */
381
382void fatal (msg)
383const char *msg;
384 {
385 syslog(LOG_ERR, "%s", msg);
435 terminate(1);
436 }
437
438/*
439 * Print an error message and terminate.
440 */
441
442void fatal (msg)
443const char *msg;
444 {
445 syslog(LOG_ERR, "%s", msg);
386 unlock();
387 terminate(1);
446 terminate(2);
388 }
389
390/*
391 * Print an error message along with the system error message and
392 * terminate.
393 */
394
395void sysfatal (msg)
396const char *msg;
397 {
398 syslog(LOG_ERR, "%s: %m", msg);
447 }
448
449/*
450 * Print an error message along with the system error message and
451 * terminate.
452 */
453
454void sysfatal (msg)
455const char *msg;
456 {
457 syslog(LOG_ERR, "%s: %m", msg);
399 unlock();
400 terminate(1);
458 terminate(2);
401 }
402
403int alarmed = 0;
404
405SIGTYPE sigalrm(signo)
406int signo;
407 {
408 int flags;
409
410 alarm(1);
411 alarmed = 1; /* Reset alarm to avoid race window */
412 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
413
414 logflush();
415 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
459 }
460
461int alarmed = 0;
462
463SIGTYPE sigalrm(signo)
464int signo;
465 {
466 int flags;
467
468 alarm(1);
469 alarmed = 1; /* Reset alarm to avoid race window */
470 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
471
472 logflush();
473 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
474 {
416 sysfatal("Can't get file mode flags on stdin");
475 sysfatal("Can't get file mode flags on stdin");
476 }
417 else
477 else
418 if (fcntl(0, F_SETFL, flags | FNDELAY) == -1)
478 {
479 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
480 {
419 sysfatal("Can't set file mode flags on stdin");
481 sysfatal("Can't set file mode flags on stdin");
482 }
483 }
420
421 if (verbose)
422 {
423 syslog(LOG_INFO, "alarm");
424 }
425 }
426
427void unalarm()
428 {
429 int flags;
430
431 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
484
485 if (verbose)
486 {
487 syslog(LOG_INFO, "alarm");
488 }
489 }
490
491void unalarm()
492 {
493 int flags;
494
495 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
496 {
432 sysfatal("Can't get file mode flags on stdin");
497 sysfatal("Can't get file mode flags on stdin");
498 }
433 else
499 else
434 if (fcntl(0, F_SETFL, flags & ~FNDELAY) == -1)
500 {
501 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
502 {
435 sysfatal("Can't set file mode flags on stdin");
503 sysfatal("Can't set file mode flags on stdin");
504 }
505 }
436 }
437
438SIGTYPE sigint(signo)
439int signo;
440 {
441 fatal("SIGINT");
442 }
443

--- 10 unchanged lines hidden (view full) ---

454 }
455
456void init()
457 {
458 signal(SIGINT, sigint);
459 signal(SIGTERM, sigterm);
460 signal(SIGHUP, sighup);
461
506 }
507
508SIGTYPE sigint(signo)
509int signo;
510 {
511 fatal("SIGINT");
512 }
513

--- 10 unchanged lines hidden (view full) ---

524 }
525
526void init()
527 {
528 signal(SIGINT, sigint);
529 signal(SIGTERM, sigterm);
530 signal(SIGHUP, sighup);
531
462 if (lock_file)
463 lock();
464
465 set_tty_parameters();
466 signal(SIGALRM, sigalrm);
467 alarm(0);
468 alarmed = 0;
469 }
470
471void set_tty_parameters()
472 {
532 set_tty_parameters();
533 signal(SIGALRM, sigalrm);
534 alarm(0);
535 alarmed = 0;
536 }
537
538void set_tty_parameters()
539 {
473#ifdef TERMIO
474 struct termio t;
540#if defined(get_term_param)
541 term_parms t;
475
542
476 if (ioctl(0, TCGETA, &t) < 0)
543 if (get_term_param (&t) < 0)
544 {
477 sysfatal("Can't get terminal parameters");
545 sysfatal("Can't get terminal parameters");
478#endif
479#ifdef TERMIOS
480 struct termios t;
546 }
481
547
482 if (tcgetattr(0, &t) < 0)
483 sysfatal("Can't get terminal parameters");
484#endif
485
486 saved_tty_parameters = t;
548 saved_tty_parameters = t;
487 have_tty_parameters = 1;
549 have_tty_parameters = 1;
488
550
489 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
490 t.c_oflag = 0;
491 t.c_lflag = 0;
492 t.c_cc[VERASE] = t.c_cc[VKILL] = 0;
493 t.c_cc[VMIN] = 1;
494 t.c_cc[VTIME] = 0;
551 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
552 t.c_oflag = 0;
553 t.c_lflag = 0;
554 t.c_cc[VERASE] =
555 t.c_cc[VKILL] = 0;
556 t.c_cc[VMIN] = 1;
557 t.c_cc[VTIME] = 0;
495
558
496#ifdef TERMIO
497 if (ioctl(0, TCSETA, &t) < 0)
559 if (set_term_param (&t) < 0)
560 {
498 sysfatal("Can't set terminal parameters");
561 sysfatal("Can't set terminal parameters");
562 }
499#endif
563#endif
500#ifdef TERMIOS
501 if (tcsetattr(0, TCSANOW, &t) < 0)
502 sysfatal("Can't set terminal parameters");
503#endif
504 }
505
506void break_sequence()
507 {
508#ifdef TERMIOS
509 tcsendbreak (0, 0);
510#endif
511 }
512
513void terminate(status)
514int status;
515 {
564 }
565
566void break_sequence()
567 {
568#ifdef TERMIOS
569 tcsendbreak (0, 0);
570#endif
571 }
572
573void terminate(status)
574int status;
575 {
516 if (have_tty_parameters &&
517#ifdef TERMIO
518 ioctl(0, TCSETA, &saved_tty_parameters) < 0
519#endif
520#ifdef TERMIOS
521 tcsetattr(0, TCSANOW, &saved_tty_parameters) < 0
522#endif
523 ) {
524 syslog(LOG_ERR, "Can't restore terminal parameters: %m");
525 unlock();
526 exit(1);
576 if (report_file != (char *) 0 && report_fp != (FILE *) NULL)
577 {
578 if (verbose)
579 {
580 fprintf (report_fp, "Closing \"%s\".\n", report_file);
581 }
582 fclose (report_fp);
583 report_fp = (FILE*) NULL;
527 }
584 }
528 exit(status);
529 }
530
585
531/*
532 * Create a lock file for the named lock device
533 */
534void lock()
535 {
536 int fd, pid;
537# ifdef PIDSTRING
538 char hdb_lock_buffer[12];
539# endif
586#if defined(get_term_param)
587 if (have_tty_parameters)
588 {
589 if (set_term_param (&saved_tty_parameters) < 0)
590 {
591 syslog(LOG_ERR, "Can't restore terminal parameters: %m");
592 exit(1);
593 }
594 }
595#endif
540
596
541 lock_file = strcat(strcat(strcpy(malloc(strlen(LOCK_DIR)
542 + 1 + strlen(lock_file) + 1),
543 LOCK_DIR), "/"), lock_file);
544
545 if ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0)
546 {
547 char *s = lock_file;
548 lock_file = (char *)0; /* Don't remove someone else's lock file! */
549 syslog(LOG_ERR, "Can't get lock file '%s': %m", s);
550 die();
551 }
552
553# ifdef PIDSTRING
554 sprintf(hdb_lock_buffer, "%10d\n", getpid());
555 write(fd, hdb_lock_buffer, 11);
556# else
557 pid = getpid();
558 write(fd, &pid, sizeof pid);
559# endif
560
561 close(fd);
597 exit(status);
562 }
563
564/*
598 }
599
600/*
565 * Remove our lockfile
566 */
567void unlock()
568 {
569 if (lock_file)
570 {
571 unlink(lock_file);
572 lock_file = (char *)0;
573 }
574 }
575
576/*
577 * 'Clean up' this string.
578 */
579char *clean(s, sending)
580register char *s;
581int sending;
582 {
583 char temp[STR_LEN], cur_chr;
584 register char *s1;

--- 9 unchanged lines hidden (view full) ---

594 cur_chr = *s++;
595 if (cur_chr == '\0')
596 {
597 *s1++ = '^';
598 break;
599 }
600 cur_chr &= 0x1F;
601 if (cur_chr != 0)
601 * 'Clean up' this string.
602 */
603char *clean(s, sending)
604register char *s;
605int sending;
606 {
607 char temp[STR_LEN], cur_chr;
608 register char *s1;

--- 9 unchanged lines hidden (view full) ---

618 cur_chr = *s++;
619 if (cur_chr == '\0')
620 {
621 *s1++ = '^';
622 break;
623 }
624 cur_chr &= 0x1F;
625 if (cur_chr != 0)
626 {
602 *s1++ = cur_chr;
627 *s1++ = cur_chr;
628 }
603 continue;
604 }
605
606 if (cur_chr != '\\')
607 {
608 *s1++ = cur_chr;
609 continue;
610 }

--- 12 unchanged lines hidden (view full) ---

623 switch (cur_chr)
624 {
625 case 'b':
626 *s1++ = '\b';
627 break;
628
629 case 'c':
630 if (sending && *s == '\0')
629 continue;
630 }
631
632 if (cur_chr != '\\')
633 {
634 *s1++ = cur_chr;
635 continue;
636 }

--- 12 unchanged lines hidden (view full) ---

649 switch (cur_chr)
650 {
651 case 'b':
652 *s1++ = '\b';
653 break;
654
655 case 'c':
656 if (sending && *s == '\0')
657 {
631 add_return = 0;
658 add_return = 0;
659 }
632 else
660 else
661 {
633 *s1++ = cur_chr;
662 *s1++ = cur_chr;
663 }
634 break;
635
636 case '\\':
637 case 'K':
638 case 'p':
639 case 'd':
640 if (sending)
664 break;
665
666 case '\\':
667 case 'K':
668 case 'p':
669 case 'd':
670 if (sending)
671 {
641 *s1++ = '\\';
672 *s1++ = '\\';
673 }
642
643 *s1++ = cur_chr;
644 break;
645
646 case 'q':
647 quiet = ! quiet;
648 break;
649

--- 15 unchanged lines hidden (view full) ---

665
666 case 'N':
667 if (sending)
668 {
669 *s1++ = '\\';
670 *s1++ = '\0';
671 }
672 else
674
675 *s1++ = cur_chr;
676 break;
677
678 case 'q':
679 quiet = ! quiet;
680 break;
681

--- 15 unchanged lines hidden (view full) ---

697
698 case 'N':
699 if (sending)
700 {
701 *s1++ = '\\';
702 *s1++ = '\0';
703 }
704 else
705 {
673 *s1++ = 'N';
706 *s1++ = 'N';
707 }
674 break;
708 break;
675
709
676 default:
677 if (isoctal (cur_chr))
678 {
679 cur_chr &= 0x07;
680 if (isoctal (*s))
681 {
682 cur_chr <<= 3;
683 cur_chr |= *s++ - '0';
684 if (isoctal (*s))
685 {
686 cur_chr <<= 3;
687 cur_chr |= *s++ - '0';
688 }
689 }
690
691 if (cur_chr != 0 || sending)
692 {
693 if (sending && (cur_chr == '\\' || cur_chr == 0))
710 default:
711 if (isoctal (cur_chr))
712 {
713 cur_chr &= 0x07;
714 if (isoctal (*s))
715 {
716 cur_chr <<= 3;
717 cur_chr |= *s++ - '0';
718 if (isoctal (*s))
719 {
720 cur_chr <<= 3;
721 cur_chr |= *s++ - '0';
722 }
723 }
724
725 if (cur_chr != 0 || sending)
726 {
727 if (sending && (cur_chr == '\\' || cur_chr == 0))
728 {
694 *s1++ = '\\';
729 *s1++ = '\\';
730 }
695 *s1++ = cur_chr;
696 }
697 break;
698 }
699
700 if (sending)
731 *s1++ = cur_chr;
732 }
733 break;
734 }
735
736 if (sending)
737 {
701 *s1++ = '\\';
738 *s1++ = '\\';
739 }
702 *s1++ = cur_chr;
703 break;
704 }
705 }
706
707 if (add_return)
740 *s1++ = cur_chr;
741 break;
742 }
743 }
744
745 if (add_return)
746 {
708 *s1++ = '\r';
747 *s1++ = '\r';
748 }
709
710 *s1++ = '\0'; /* guarantee closure */
711 *s1++ = '\0'; /* terminate the string */
712 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
713 }
714
715/*
716 * Process the expect string
717 */
718void chat_expect(s)
719register char *s;
720 {
721 if (strcmp(s, "ABORT") == 0)
722 {
723 ++abort_next;
724 return;
725 }
726
749
750 *s1++ = '\0'; /* guarantee closure */
751 *s1++ = '\0'; /* terminate the string */
752 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
753 }
754
755/*
756 * Process the expect string
757 */
758void chat_expect(s)
759register char *s;
760 {
761 if (strcmp(s, "ABORT") == 0)
762 {
763 ++abort_next;
764 return;
765 }
766
767 if (strcmp(s, "REPORT") == 0)
768 {
769 ++report_next;
770 return;
771 }
772
727 if (strcmp(s, "TIMEOUT") == 0)
728 {
729 ++timeout_next;
730 return;
731 }
732
733 while (*s)
734 {
735 register char *hyphen;
736
737 for (hyphen = s; *hyphen; ++hyphen)
773 if (strcmp(s, "TIMEOUT") == 0)
774 {
775 ++timeout_next;
776 return;
777 }
778
779 while (*s)
780 {
781 register char *hyphen;
782
783 for (hyphen = s; *hyphen; ++hyphen)
784 {
738 if (*hyphen == '-')
785 if (*hyphen == '-')
786 {
739 if (hyphen == s || hyphen[-1] != '\\')
787 if (hyphen == s || hyphen[-1] != '\\')
788 {
740 break;
789 break;
741
790 }
791 }
792 }
793
742 if (*hyphen == '-')
743 {
744 *hyphen = '\0';
745
746 if (get_string(s))
794 if (*hyphen == '-')
795 {
796 *hyphen = '\0';
797
798 if (get_string(s))
799 {
747 return;
800 return;
801 }
748 else
749 {
750 s = hyphen + 1;
751
752 for (hyphen = s; *hyphen; ++hyphen)
802 else
803 {
804 s = hyphen + 1;
805
806 for (hyphen = s; *hyphen; ++hyphen)
807 {
753 if (*hyphen == '-')
808 if (*hyphen == '-')
809 {
754 if (hyphen == s || hyphen[-1] != '\\')
810 if (hyphen == s || hyphen[-1] != '\\')
811 {
755 break;
812 break;
813 }
814 }
815 }
756
757 if (*hyphen == '-')
758 {
759 *hyphen = '\0';
760
761 chat_send(s);
762 s = hyphen + 1;
763 }
764 else
765 {
766 chat_send(s);
767 return;
768 }
769 }
770 }
771 else
816
817 if (*hyphen == '-')
818 {
819 *hyphen = '\0';
820
821 chat_send(s);
822 s = hyphen + 1;
823 }
824 else
825 {
826 chat_send(s);
827 return;
828 }
829 }
830 }
831 else
832 {
772 if (get_string(s))
833 if (get_string(s))
834 {
773 return;
835 return;
836 }
774 else
775 {
776 if (fail_reason)
837 else
838 {
839 if (fail_reason)
840 {
777 syslog(LOG_INFO, "Failed (%s)", fail_reason);
841 syslog(LOG_INFO, "Failed (%s)", fail_reason);
842 }
778 else
843 else
844 {
779 syslog(LOG_INFO, "Failed");
845 syslog(LOG_INFO, "Failed");
846 }
780
847
781 unlock();
782 terminate(1);
848 terminate(exit_code);
783 }
849 }
850 }
784 }
785 }
786
787char *character(c)
851 }
852 }
853
854char *character(c)
788char c;
855int c;
789 {
790 static char string[10];
791 char *meta;
792
793 meta = (c & 0x80) ? "M-" : "";
794 c &= 0x7F;
795
796 if (c < 32)
856 {
857 static char string[10];
858 char *meta;
859
860 meta = (c & 0x80) ? "M-" : "";
861 c &= 0x7F;
862
863 if (c < 32)
864 {
797 sprintf(string, "%s^%c", meta, (int)c + '@');
865 sprintf(string, "%s^%c", meta, (int)c + '@');
866 }
798 else
867 else
868 {
799 if (c == 127)
869 if (c == 127)
870 {
800 sprintf(string, "%s^?", meta);
871 sprintf(string, "%s^?", meta);
872 }
801 else
873 else
874 {
802 sprintf(string, "%s%c", meta, c);
875 sprintf(string, "%s%c", meta, c);
876 }
877 }
803
804 return (string);
805 }
806
807/*
808 * process the reply string
809 */
810void chat_send (s)
811register char *s;
812 {
813 if (abort_next)
878
879 return (string);
880 }
881
882/*
883 * process the reply string
884 */
885void chat_send (s)
886register char *s;
887 {
888 if (abort_next)
814 {
889 {
815 char *s1;
890 char *s1;
816
891
817 abort_next = 0;
892 abort_next = 0;
818
893
819 if (n_aborts >= MAX_ABORTS)
894 if (n_aborts >= MAX_ABORTS)
895 {
820 fatal("Too many ABORT strings");
896 fatal("Too many ABORT strings");
821
897 }
898
822 s1 = clean(s, 0);
899 s1 = clean(s, 0);
823
824 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
900
901 if (strlen(s1) > strlen(s)
902 || strlen(s1) + 1 > sizeof(fail_buffer))
825 {
826 syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
827 die();
828 }
829
830 abort_string[n_aborts++] = s1;
831
832 if (verbose)
833 {
834 logf("abort on (");
835
836 for (s1 = s; *s1; ++s1)
903 {
904 syslog(LOG_WARNING, "Illegal or too-long ABORT string ('%s')", s);
905 die();
906 }
907
908 abort_string[n_aborts++] = s1;
909
910 if (verbose)
911 {
912 logf("abort on (");
913
914 for (s1 = s; *s1; ++s1)
915 {
837 logf(character(*s1));
916 logf(character(*s1));
917 }
838
839 logf(")\n");
840 }
918
919 logf(")\n");
920 }
921 return;
841 }
922 }
842 else
843 if (timeout_next)
923
924 if (report_next)
925 {
926 char *s1;
927
928 report_next = 0;
929 if (n_reports >= MAX_REPORTS)
844 {
930 {
845 timeout_next = 0;
846 timeout = atoi(s);
931 fatal("Too many REPORT strings");
932 }
933
934 s1 = clean(s, 0);
935
936 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
937 {
938 syslog(LOG_WARNING, "Illegal or too-long REPORT string ('%s')", s);
939 die();
940 }
941
942 report_string[n_reports++] = s1;
943
944 if (verbose)
945 {
946 logf("report (");
947 s1 = s;
948 while (*s1)
949 {
950 logf(character(*s1));
951 ++s1;
952 }
953 logf(")\n");
954 }
955 return;
956 }
847
957
848 if (timeout <= 0)
849 timeout = DEFAULT_CHAT_TIMEOUT;
958 if (timeout_next)
959 {
960 timeout_next = 0;
961 timeout = atoi(s);
962
963 if (timeout <= 0)
964 {
965 timeout = DEFAULT_CHAT_TIMEOUT;
966 }
850
967
851 if (verbose)
852 {
853 syslog(LOG_INFO, "timeout set to %d seconds", timeout);
854 }
968 if (verbose)
969 {
970 syslog(LOG_INFO, "timeout set to %d seconds", timeout);
855 }
971 }
856 else
972 return;
973 }
974
975 if (strcmp(s, "EOT") == 0)
976 {
977 s = "^D\\c";
978 }
979 else
980 {
981 if (strcmp(s, "BREAK") == 0)
857 {
982 {
858 if (strcmp(s, "EOT") == 0)
859 s = "^D\\c";
860 else
861 if (strcmp(s, "BREAK") == 0)
862 s = "\\K\\c";
863 if ( ! put_string(s))
864 {
865 syslog(LOG_INFO, "Failed");
866 unlock();
867 terminate(1);
868 }
983 s = "\\K\\c";
869 }
984 }
985 }
986
987 if (!put_string(s))
988 {
989 syslog(LOG_INFO, "Failed");
990 terminate(1);
991 }
870 }
871
872int get_char()
873 {
874 int status;
875 char c;
876
877 status = read(0, &c, 1);
878
879 switch (status)
992 }
993
994int get_char()
995 {
996 int status;
997 char c;
998
999 status = read(0, &c, 1);
1000
1001 switch (status)
880 {
881 case 1:
882 return ((int)c & 0x7F);
1002 {
1003 case 1:
1004 return ((int)c & 0x7F);
883
1005
884 default:
885 syslog(LOG_WARNING, "warning: read() on stdin returned %d",
886 status);
1006 default:
1007 syslog(LOG_WARNING, "warning: read() on stdin returned %d",
1008 status);
887
1009
888 case -1:
889 if ((status = fcntl(0, F_GETFL, 0)) == -1)
890 sysfatal("Can't get file mode flags on stdin");
891 else
892 if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
893 sysfatal("Can't set file mode flags on stdin");
894
895 return (-1);
896 }
1010 case -1:
1011 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1012 {
1013 sysfatal("Can't get file mode flags on stdin");
1014 }
1015 else
1016 {
1017 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1018 {
1019 sysfatal("Can't set file mode flags on stdin");
1020 }
1021 }
1022
1023 return (-1);
1024 }
897 }
898
899int put_char(c)
1025 }
1026
1027int put_char(c)
900char c;
1028int c;
901 {
902 int status;
1029 {
1030 int status;
1031 char ch = c;
903
1032
904 delay();
1033 usleep(10000); /* inter-character typing delay (?) */
905
1034
906 status = write(1, &c, 1);
1035 status = write(1, &ch, 1);
907
908 switch (status)
1036
1037 switch (status)
909 {
910 case 1:
911 return (0);
912
913 default:
914 syslog(LOG_WARNING, "warning: write() on stdout returned %d",
915 status);
916
917 case -1:
918 if ((status = fcntl(0, F_GETFL, 0)) == -1)
919 sysfatal("Can't get file mode flags on stdin");
920 else
921 if (fcntl(0, F_SETFL, status & ~FNDELAY) == -1)
922 sysfatal("Can't set file mode flags on stdin");
923
924 return (-1);
925 }
1038 {
1039 case 1:
1040 return (0);
1041
1042 default:
1043 syslog(LOG_WARNING, "warning: write() on stdout returned %d",
1044 status);
1045
1046 case -1:
1047 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1048 {
1049 sysfatal("Can't get file mode flags on stdin");
1050 }
1051 else
1052 {
1053 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1054 {
1055 sysfatal("Can't set file mode flags on stdin");
1056 }
1057 }
1058
1059 return (-1);
1060 }
926 }
927
928int write_char (c)
929int c;
930 {
931 if (alarmed || put_char(c) < 0)
932 {
933 extern int errno;
934
1061 }
1062
1063int write_char (c)
1064int c;
1065 {
1066 if (alarmed || put_char(c) < 0)
1067 {
1068 extern int errno;
1069
935 alarm(0); alarmed = 0;
1070 alarm(0);
1071 alarmed = 0;
936
937 if (verbose)
938 {
939 if (errno == EINTR || errno == EWOULDBLOCK)
1072
1073 if (verbose)
1074 {
1075 if (errno == EINTR || errno == EWOULDBLOCK)
1076 {
940 syslog(LOG_INFO, " -- write timed out");
1077 syslog(LOG_INFO, " -- write timed out");
1078 }
941 else
1079 else
1080 {
942 syslog(LOG_INFO, " -- write failed: %m");
1081 syslog(LOG_INFO, " -- write failed: %m");
1082 }
943 }
944 return (0);
945 }
946 return (1);
947 }
948
949int put_string (s)
950register char *s;
951 {
952 s = clean(s, 1);
953
954 if (verbose)
955 {
956 logf("send (");
957
958 if (quiet)
1083 }
1084 return (0);
1085 }
1086 return (1);
1087 }
1088
1089int put_string (s)
1090register char *s;
1091 {
1092 s = clean(s, 1);
1093
1094 if (verbose)
1095 {
1096 logf("send (");
1097
1098 if (quiet)
1099 {
959 logf("??????");
1100 logf("??????");
1101 }
960 else
961 {
962 register char *s1 = s;
963
964 for (s1 = s; *s1; ++s1)
1102 else
1103 {
1104 register char *s1 = s;
1105
1106 for (s1 = s; *s1; ++s1)
1107 {
965 logf(character(*s1));
1108 logf(character(*s1));
1109 }
966 }
967
968 logf(")\n");
969 }
970
971 alarm(timeout); alarmed = 0;
972
973 while (*s)
974 {
975 register char c = *s++;
976
977 if (c != '\\')
978 {
979 if (!write_char (c))
1110 }
1111
1112 logf(")\n");
1113 }
1114
1115 alarm(timeout); alarmed = 0;
1116
1117 while (*s)
1118 {
1119 register char c = *s++;
1120
1121 if (c != '\\')
1122 {
1123 if (!write_char (c))
1124 {
980 return 0;
1125 return 0;
1126 }
981 continue;
982 }
983
984 c = *s++;
985 switch (c)
986 {
987 case 'd':
988 sleep(1);
989 break;
990
991 case 'K':
992 break_sequence();
993 break;
994
995 case 'p':
1127 continue;
1128 }
1129
1130 c = *s++;
1131 switch (c)
1132 {
1133 case 'd':
1134 sleep(1);
1135 break;
1136
1137 case 'K':
1138 break_sequence();
1139 break;
1140
1141 case 'p':
996 usleep(10000); /* 1/100th of a second. */
1142 usleep(10000); /* 1/100th of a second (arg is microseconds) */
997 break;
998
999 default:
1000 if (!write_char (c))
1001 return 0;
1002 break;
1003 }
1004 }

--- 20 unchanged lines hidden (view full) ---

1025
1026 if (verbose)
1027 {
1028 register char *s1;
1029
1030 logf("expect (");
1031
1032 for (s1 = string; *s1; ++s1)
1143 break;
1144
1145 default:
1146 if (!write_char (c))
1147 return 0;
1148 break;
1149 }
1150 }

--- 20 unchanged lines hidden (view full) ---

1171
1172 if (verbose)
1173 {
1174 register char *s1;
1175
1176 logf("expect (");
1177
1178 for (s1 = string; *s1; ++s1)
1179 {
1033 logf(character(*s1));
1180 logf(character(*s1));
1181 }
1034
1035 logf(")\n");
1036 }
1037
1038 if (len > STR_LEN)
1039 {
1040 syslog(LOG_INFO, "expect string is too long");
1182
1183 logf(")\n");
1184 }
1185
1186 if (len > STR_LEN)
1187 {
1188 syslog(LOG_INFO, "expect string is too long");
1189 exit_code = 1;
1041 return 0;
1042 }
1043
1044 if (len == 0)
1045 {
1046 if (verbose)
1047 {
1048 syslog(LOG_INFO, "got it");
1049 }
1050
1051 return (1);
1052 }
1053
1190 return 0;
1191 }
1192
1193 if (len == 0)
1194 {
1195 if (verbose)
1196 {
1197 syslog(LOG_INFO, "got it");
1198 }
1199
1200 return (1);
1201 }
1202
1054 alarm(timeout); alarmed = 0;
1203 alarm(timeout);
1204 alarmed = 0;
1055
1056 while ( ! alarmed && (c = get_char()) >= 0)
1057 {
1205
1206 while ( ! alarmed && (c = get_char()) >= 0)
1207 {
1058 int n, abort_len;
1208 int n, abort_len, report_len;
1059
1060 if (verbose)
1061 {
1062 if (c == '\n')
1209
1210 if (verbose)
1211 {
1212 if (c == '\n')
1213 {
1063 logf("\n");
1214 logf("\n");
1215 }
1064 else
1216 else
1217 {
1065 logf(character(c));
1218 logf(character(c));
1219 }
1066 }
1067
1068 *s++ = c;
1069
1070 if (s - temp >= len &&
1071 c == string[len - 1] &&
1072 strncmp(s - len, string, len) == 0)
1073 {
1074 if (verbose)
1075 {
1076 logf(" -- got it\n");
1077 }
1078
1220 }
1221
1222 *s++ = c;
1223
1224 if (s - temp >= len &&
1225 c == string[len - 1] &&
1226 strncmp(s - len, string, len) == 0)
1227 {
1228 if (verbose)
1229 {
1230 logf(" -- got it\n");
1231 }
1232
1079 alarm(0); alarmed = 0;
1233 alarm(0);
1234 alarmed = 0;
1080 return (1);
1081 }
1082
1083 for (n = 0; n < n_aborts; ++n)
1235 return (1);
1236 }
1237
1238 for (n = 0; n < n_aborts; ++n)
1239 {
1084 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1085 strncmp(s - abort_len, abort_string[n], abort_len) == 0)
1240 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1241 strncmp(s - abort_len, abort_string[n], abort_len) == 0)
1086 {
1242 {
1087 if (verbose)
1088 {
1089 logf(" -- failed\n");
1090 }
1243 if (verbose)
1244 {
1245 logf(" -- failed\n");
1246 }
1091
1092 alarm(0); alarmed = 0;
1247
1248 alarm(0);
1249 alarmed = 0;
1250 exit_code = n + 4;
1093 strcpy(fail_reason = fail_buffer, abort_string[n]);
1094 return (0);
1251 strcpy(fail_reason = fail_buffer, abort_string[n]);
1252 return (0);
1095 }
1253 }
1254 }
1096
1255
1256 if (!report_gathering)
1257 {
1258 for (n = 0; n < n_reports; ++n)
1259 {
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)
1263 {
1264 time_t time_now = time ((time_t*) NULL);
1265 struct tm* tm_now = localtime (&time_now);
1266
1267 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1268 strcat (report_buffer, report_string[n]);
1269
1270 report_string[n] = (char *) NULL;
1271 report_gathering = 1;
1272 break;
1273 }
1274 }
1275 }
1276 else
1277 {
1278 if (!iscntrl (c))
1279 {
1280 int rep_len = strlen (report_buffer);
1281 report_buffer[rep_len] = c;
1282 report_buffer[rep_len + 1] = '\0';
1283 }
1284 else
1285 {
1286 report_gathering = 0;
1287 fprintf (report_fp, "chat: %s\n", report_buffer);
1288 }
1289 }
1290
1097 if (s >= end)
1098 {
1291 if (s >= end)
1292 {
1099 strncpy(temp, s - minlen, minlen);
1293 strncpy (temp, s - minlen, minlen);
1100 s = temp + minlen;
1101 }
1102
1103 if (alarmed && verbose)
1294 s = temp + minlen;
1295 }
1296
1297 if (alarmed && verbose)
1298 {
1104 syslog(LOG_WARNING, "warning: alarm synchronization problem");
1299 syslog(LOG_WARNING, "warning: alarm synchronization problem");
1300 }
1105 }
1106
1107 alarm(0);
1301 }
1302
1303 alarm(0);
1108
1304
1109 if (verbose && printed)
1110 {
1111 if (alarmed)
1305 if (verbose && printed)
1306 {
1307 if (alarmed)
1308 {
1112 logf(" -- read timed out\n");
1309 logf(" -- read timed out\n");
1310 }
1113 else
1114 {
1115 logflush();
1116 syslog(LOG_INFO, " -- read failed: %m");
1117 }
1118 }
1119
1311 else
1312 {
1313 logflush();
1314 syslog(LOG_INFO, " -- read failed: %m");
1315 }
1316 }
1317
1120 alarmed = 0;
1318 exit_code = 3;
1319 alarmed = 0;
1121 return (0);
1122 }
1123
1320 return (0);
1321 }
1322
1124#ifdef ultrix
1125#undef NO_USLEEP
1323#ifdef NO_USLEEP
1126#include <sys/types.h>
1127#include <sys/time.h>
1128
1129/*
1130 usleep -- support routine for 4.2BSD system call emulations
1131 last edit: 29-Oct-1984 D A Gwyn
1132 */
1133
1134extern int select();
1135
1136int
1137usleep( usec ) /* returns 0 if ok, else -1 */
1138 long usec; /* delay in microseconds */
1139{
1140 static struct /* `timeval' */
1324#include <sys/types.h>
1325#include <sys/time.h>
1326
1327/*
1328 usleep -- support routine for 4.2BSD system call emulations
1329 last edit: 29-Oct-1984 D A Gwyn
1330 */
1331
1332extern int select();
1333
1334int
1335usleep( usec ) /* returns 0 if ok, else -1 */
1336 long usec; /* delay in microseconds */
1337{
1338 static struct /* `timeval' */
1141 {
1142 long tv_sec; /* seconds */
1143 long tv_usec; /* microsecs */
1144 } delay; /* _select() timeout */
1339 {
1340 long tv_sec; /* seconds */
1341 long tv_usec; /* microsecs */
1342 } delay; /* _select() timeout */
1145
1343
1146 delay.tv_sec = usec / 1000000L;
1344 delay.tv_sec = usec / 1000000L;
1147 delay.tv_usec = usec % 1000000L;
1148
1149 return select( 0, (long *)0, (long *)0, (long *)0, &delay );
1150}
1151#endif
1345 delay.tv_usec = usec % 1000000L;
1346
1347 return select( 0, (long *)0, (long *)0, (long *)0, &delay );
1348}
1349#endif
1152
1153/*
1154 * Delay an amount appropriate for between typed characters.
1155 */
1156void delay()
1157 {
1158# ifdef NO_USLEEP
1159 register int i;
1160
1161 for (i = 0; i < 30000; ++i) /* ... did we just say appropriate? */
1162 ;
1163# else /* NO_USLEEP */
1164 usleep(100);
1165# endif /* NO_USLEEP */
1166 }