File: xispdial.c

package info (click to toggle)
xisp 2.5p4-1
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 1,328 kB
  • ctags: 1,908
  • sloc: ansic: 16,648; asm: 214; sh: 210; makefile: 202; perl: 55
file content (627 lines) | stat: -rw-r--r-- 21,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
  Copyright (C) 1997,1998  Dimitrios P. Bouras

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   For author contact information, look in the README file.
*/

#include <stdio.h>
#include <stdlib.h>
#include <varargs.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <signal.h>
#include "common.h"

#ifdef SUNOS41x
extern int sys_nerr;
extern char *sys_errlist[];
extern int errno;
extern int fputs(), fscanf(), fprintf(), fclose();
extern char *vsprintf();
#endif

/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         Global program storage                          |
  |                                                                         |
  +-------------------------------------------------------------------------+*/

char *scriptfname;						/* login script filename */
char *envfname;							/* dialing environment filename */
char Chat[MAXLEN_FNAME+2];				/* complete path to chat utility */
char Pipe[MAXLEN_FNAME+2];				/* named pipe report file */
char Terminal[MAXLEN_FNAME+2];			/* complete path to xispterm */
int pipeFD = 0;							/* report file descriptor */
int childPID;							/* PID of running child */
int maxAttempts = MAXNUM_RETRY;			/* max dialing attempts */
int sleepDelay = MAXSEC_DELAY;			/* post-dial sleep delay */
int connectWait = MAXSEC_CNWAIT;		/* maximum wait for connection */
char account[MAXLEN_ACCOUNT+2];			/* account name */
char **phone;							/* phone numbers */
int numPhones;							/* how many there are to dial */
char **sline;							/* script lines */
int numSlines;							/* how many there are to include */
char **CBsln;							/* call-back script lines */
int numCBSlns;							/* how many there are to include */
int CBDelay;							/* time wait for call-back */
char *modemInit;						/* modem init string */				
char modemUInit[MAXLEN_MDMCMD+2];		/* user specified modem init str */
char modemUReset[MAXLEN_MDMCMD+2];		/* user specified modem reset string */				
char modemUConn[MAXLEN_MDMSTR+2];		/* user specified modem connect str */
char modemDial[MAXLEN_DIALEXTRA+2+2];	/* dialing chars + method char */
int autologin = 1;						/* true for automatic login */
int callback = 0;						/* true when call-back desired */
int manual = 0;							/* true for manual terminal */
char dispname[MAXLEN_TTPARM+2];			/* X11 display for auto login */
char geometry[MAXLEN_TTPARM+2];			/* and terminal window geometry */
int authlogin;							/* true for PAP/PAPS/CHAPS login */
char bgcol[16] = {0};					/* form-background color string */


/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                             Utility routines                            |
  |                                                                         |
  +-------------------------------------------------------------------------+*/

/* Print message together with system error message and exit. Note the
   implicit total length of MSGLEN_ERR bytes for the resulting string. */

#define MSGLEN_ERR 256

void doErr(char *msg)
{
	char emsg[MSGLEN_ERR];

#ifndef SUNOS5x
	if (errno < sys_nerr)
		sprintf(emsg, "xispdial: %s: %s\n", msg, sys_errlist[errno]);
	else
		sprintf(emsg, "xispdial: %s: error #%d\n", msg, errno);
#else
	sprintf(emsg, "xispdial: %s: %s\n", msg, strerror(errno));
#endif
	fputs(emsg, stderr);
	if (pipeFD) close(pipeFD);
	exit(1);
}


/* Open a named pipe for writing only */

int namedPipe(char *fname)
{
	struct stat st;
	int fd;

	if (access(fname, F_OK) == -1)				/* check to see if it exists */
		doErr("namedPipe: access");				/* nope, this is not right! */
	else {
		stat(fname, &st);						/* yes, get the node status */
		if (!S_ISFIFO(st.st_mode))				/* is it a FIFO? */
			doErr("namedPipe: stat");			/* nope, still not right! */
	}
#ifndef SUNOS5x
	fd = open(fname, O_WRONLY|O_NDELAY);		/* yes, open it for writing */
#else
	fd = open(fname, O_WRONLY|O_NONBLOCK);		/* yes, open it for writing */
#endif
	if (fd < 0)									/* error means no process has */
		doErr("namedPipe: open");				/* opened it for reading */
	return fd;									/* return the descriptor */
}


/* Write messages printf() style to the named pipe file descriptor. Note
   the implicit total length of MSGLEN_PIPE bytes for the resulting string. */

#define MSGLEN_PIPE 256

int pprintf(va_alist) va_dcl
{
	int iw, bw;
	va_list ap;
	char *fmt, msg[MSGLEN_PIPE];

	va_start(ap);								/* start variable arg list */
	fmt = va_arg(ap, char*);					/* first string is format */
#ifndef SUNOS41x
	iw = vsprintf(msg, fmt, ap);				/* pass rest to vsprintf() */
#else
	vsprintf(msg, fmt, ap);
	iw = strlen(msg);
#endif
	va_end(ap);									/* end variable arg list */
	bw = write(pipeFD, msg, strlen(msg));		/* write buffer to pipe */
	if (bw < strlen(msg))						/* all bytes written? */
		doErr("xispdial: pprintf");				/* nope, bail out */
	return iw;									/* else return items written */
}


/* Execute a child process, attaching its stderr to a named pipe. The
   pipe output is read by xisp and displayed on its browser window.   */

#define MAXARGS_CHILD 16

int child(va_alist) va_dcl
{
	int i, stat;
	char *args[MAXARGS_CHILD+1];
	va_list ap;

	childPID = fork();							/* fork to create child */
	if (childPID < 0)							/* ret < 0 : fork failed */
		doErr("child: fork");
	if (childPID) {								/* in parrent process */
		i = waitpid(childPID, &stat, 0);		/* get child return status */
		if (i < 0)								/* ret < 0 : wait failed */
			doErr("child: waitpid");
		childPID = 0;							/* OK, indicate child done */
	}
	else {										/* in child process */
		dup2(pipeFD, 2);						/* tack stderr on pipe input */
		va_start(ap);							/* start variable arg list */
		for (									/* parse program arguments */
			i=0; i < MAXARGS_CHILD &&			/* to a max of MAXARGS_CHILD */
			(args[i]=va_arg(ap, char*)) !=
				(char *)0;
			i++
		);
		va_end(ap);								/* end variable arg list */
		execv(args[0], args);					/* exec child */
		doErr("child: execv");					/* return here means error */
	}
	if (WIFEXITED(stat))						/* parent returns child stat */
		return(WEXITSTATUS(stat));				/* unless something funny */
	else										/* has happened, in which */
		return -1;								/* case -1 is returned */
}

/* Prints out of memory error and exits */

void outOfMem(char *msg)
{
	fprintf(stderr, "xispdial: %s: out of memory!\n", msg);
	if (pipeFD) close(pipeFD);
	exit(1);
}

/* Initializes the dialer file names using the user home directory. */

void initFnames(void)
{
	struct passwd *user;

	user = getpwuid(getuid());
	scriptfname = (char *)malloc(strlen(user->pw_dir)+1+strlen(SCRIPTFNAME)+1);
	envfname = (char *)malloc(strlen(user->pw_dir)+1+strlen(ENVFNAME)+1);
	if (scriptfname != NULL && envfname != NULL) {
		strcpy(scriptfname, user->pw_dir); strcat(scriptfname, "/");
		strcat(scriptfname, SCRIPTFNAME);
		strcpy(envfname, user->pw_dir); strcat(envfname, "/");
		strcat(envfname, ENVFNAME);
	}
	else outOfMem("initFnames");
}


/* Reads the environment file created by xISP and then deletes it */

void uEOF(void)
{
	pprintf("%s: %s!\n", "xispdial",			/* notify xisp */
			"environ premature EOF");
	close(pipeFD);								/* close the report pipe */
	exit(1);									/* and exit with error */
}

FILE *envfp;  /* environment file pointer common to readLine(), getISPenv() */

/* The readLine function reads a single line from the environment file
   prepared by xisp and before returning can be instructed to either escape
   all '\\' characters, or to delete them from the buffer. It assumes that
   the buffer has the space required for escaping all '\\' characters found. */

void readLine(char *param, int len, int escape)
{
	char *p;
	int zlen;	/* line length (includes ending '\x0') */

	if (fgets(param, len+1,						/* read environment line */
			  envfp) == NULL)
		uEOF();									/* bail out on error */
	param[(zlen = strlen(param))-1]=0;			/* kill the ending '\n' */
	for (p=param; *p;) {						/* scan the line */
		if (*p == '\\') {						/* if a '\\' is found */
			if (escape) {						/* if auto escaping selected */
#ifndef SUNOS41x
				memmove(p+1, p, zlen);			/* increase string len by 1 */
#else											/* by duplicating the string */
				bcopy(p, p+1, zlen);			/* contents starting from */
#endif											/* next character position */
				*p = '\\';						/* insert an extra '\\' */
				p += 2;							/* skip the next '\\' */
			}
			else {								/* deletion of '\\'s desired */
#ifndef SUNOS41x
				memmove(p, p+1, zlen);			/* decrease string len by 1 */
#else
				bcopy(p+1, p, zlen);
#endif
			}
		}
		else									/* char not a '\\' */
			++p;								/* so carry on */
	}
}

void getISPenv(void)
{
	int ir, i;
	char LM[16] = {0};

	envfp = fopen(envfname, "r");				/* open temp environment file */
	if (envfp == NULL)							/* if not there bail out */
		doErr("getISPenv: fopen");

	readLine(Chat, MAXLEN_FNAME, 0);			/* read all needed dialing */
	readLine(Pipe, MAXLEN_FNAME, 0);			/* environment variables */
	readLine(Terminal, MAXLEN_FNAME, 0);
	ir = fscanf(envfp,"%d ",&maxAttempts);
	ir += fscanf(envfp,"%d ",&sleepDelay);
	ir += fscanf(envfp,"%d ",&connectWait);
	ir += fscanf(envfp,"%d ",&numPhones);		/* read # of phone numbers */
	if (ir < 4)									/* all read so far? */
		uEOF();									/* nope, bail out */
	phone = (char **)							/* allocate space for */
			malloc(numPhones*sizeof(char *));	/* numPhones phone numbers */
	for (i=0; i<numPhones && phone!=NULL;		/* which follow */
		 i++)
	{
		phone[i] = (char *)						/* allocate phone number */
					malloc(MAXLEN_PHONE+2);
		if (phone[i] != NULL)
			*(phone[i]) = 0;					/* and initialize it */
		else
			phone = NULL;
	}
	if (phone == NULL)							/* allocation completed? */
		outOfMem("getISPenv");					/* nope, bail out */
	for (i=0; i<numPhones; i++)
		readLine(phone[i], MAXLEN_PHONE, 0);	/* read telephone numbers */
	readLine(LM, (sizeof LM)-2, 0);				/* read login method */
	autologin = (strstr(LM, "AUTO") != NULL);	/* adjust autologin flag */
	if (autologin)
		callback = !strcmp(LM, "AUTO-CB");		/* adjust call-back flag */
	else {
		authlogin = (strstr(LM, "AUTH")!=NULL);	/* adjust authlogin flag */
		if (authlogin) {
			callback = !strcmp(LM, "AUTH-CB");	/* adjust call-back flag */
			manual = !strcmp(LM, "AUTH-MAN");	/* adjust manual flag */
		}
		else
			manual = 1;
	}

	if (autologin) {							/* if login is indeed auto */
		readLine(account, MAXLEN_ACCOUNT, 0);	/* read account name and */
		ir = fscanf(envfp,"%d ",&numSlines);	/* number of script lines */
		if (! ir)								/* bail out on error */
			uEOF();
		if (numSlines) {						/* 0 lines is possible */
			sline = (char **)					/* allocate space for */
			 malloc(numSlines*sizeof(char *));	/* numSlines scipt lines */
			for (i=0; i<numSlines&&sline!=NULL;	/* which follow */
				 i++)
			{
				sline[i] = (char *)				/* allocate script line */
					malloc(4*MAXLEN_SLINE+10	/* double the size for */
						  +5*MAXLEN_ACCOUNT+2);	/* automatic escaping */
				if (sline[i] != NULL)
					*(sline[i]) = 0;			/* and initialize it */
				else
					sline = NULL;
			}
			if (sline == NULL)					/* allocation completed? */
				outOfMem("getISPenv");			/* nope, bail out */
			for (i=0; i<numSlines; i++)
				readLine(sline[i],				/* read script lines */
						 4*MAXLEN_SLINE+10+
						 5*MAXLEN_ACCOUNT, 1);
		}
		if (callback) {
			ir= fscanf(envfp,"%d ",&CBDelay);	/* time wait for call-back */
			ir+=fscanf(envfp,"%d ",&numCBSlns);	/* number of call-back lines */
			if (ir < 2)							/* bail out on error */
				uEOF();
			if (numCBSlns) {					/* 0 lines is possible */
				CBsln = (char **)				/* allocate space for */
				 malloc(numSlines*				/* numCBSlns call-back lines */
						sizeof(char *));
				for (i=0; i<numCBSlns &&		/* which follow */
						  CBsln!=NULL;
					 i++)
				{
					CBsln[i] = (char *)			/* allocate call-back line */
						malloc(4*MAXLEN_SLINE+
						 10+5*MAXLEN_ACCOUNT+2);
					if (CBsln[i] != NULL)
						*(CBsln[i]) = 0;		/* and initialize it */
					else
						CBsln = NULL;
				}
				if (CBsln == NULL)				/* allocation completed? */
					outOfMem("getISPenv");		/* nope, bail out */
				for (i=0; i<numCBSlns; i++)
					readLine(CBsln[i],			/* read script lines */
							 4*MAXLEN_SLINE+10+
							 5*MAXLEN_ACCOUNT, 1);
			}
		}
	}
	else if (authlogin) {						/* auth-login? */
		if (callback) {							/* yes, call-back desired? */
			if (fscanf(envfp,"%d ",&CBDelay)<1)	/* time wait for call-back */
				uEOF();							/* bail out on error */
		}
		else if (manual) {						/* manual terminal desired */
			readLine(dispname,					/* read the X11 display */
					 MAXLEN_TTPARM, 0);
			readLine(bgcol,						/* read background color */
					 (sizeof bgcol)-2, 0);
			readLine(geometry,					/* read window geometry */
					 MAXLEN_TTPARM, 0);
		}
	}
	else {										/* else manual login desired */
		readLine(dispname, MAXLEN_TTPARM, 0);	/* read the X11 display */
		readLine(bgcol, (sizeof bgcol)-2, 0);	/* read background color */
		readLine(geometry, MAXLEN_TTPARM, 0);	/* read window geometry */
	}

	readLine(modemUReset, MAXLEN_MDMCMD, 1);	/* modem reset string */
	readLine(modemUInit, MAXLEN_MDMCMD, 1);		/* modem init string */
	readLine(modemDial, MAXLEN_DIALEXTRA+2, 0);	/* dial char(s) + method */
	readLine(modemUConn, MAXLEN_MDMSTR, 0);		/* modem connect string */

	fclose(envfp);								/* close and remove the */
	unlink(envfname);							/* temp environment file */
}


/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                             Dialer routines                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/

#ifdef _SLOW_MODEM_
#define MODEM_SLEEP 2
#else
#define MODEM_SLEEP 1
#endif

int init_modem(char *initCmd)					/* initialize modem */
{
	int rcode;

	rcode = child(Chat,"-V","TIMEOUT","3",
				  "", modemUReset, "OK", initCmd,
				  (char *)0);
	if (rcode) {
		rcode = child(Chat,"-V","TIMEOUT","3",
					  "", "+++\\c", "OK", modemUReset,
					  "OK", initCmd, (char *)0);
		if (rcode)
			rcode = child(Chat,"-V","TIMEOUT","3",
						  "","AT","OK-AT-OK",
						  modemUReset, "OK",
						  initCmd, (char *)0);
	}
	sleep(MODEM_SLEEP);
	return rcode;
}

#define MAXLEN_PREAMBLE 512
#define MAXLEN_CBTRAIL  256

int callnumber(char *number)					/* dial the number given */
{
	int i, rcode;
	FILE *scriptF;
	char preambleD[MAXLEN_PREAMBLE+1] =
			"TIMEOUT\t\t3\nABORT\t\tBUSY\nABORT\t\t'NO CARRIER'\n"
			"ABORT\t\t'NO DIALTONE'\nABORT\t\tenied\nABORT\t\timeout\n"
			"''\t\t\t'AT %s %s'\nTIMEOUT\t\t%d\n%s\t\t\\c\n"
			"TIMEOUT\t\t5\n\\r\t\t\\c\n",
		 preambleCB[MAXLEN_PREAMBLE+1] =
			"TIMEOUT\t\t3\nABORT\t\tBUSY\nABORT 'NO DIALTONE'\n"
			"ABORT\t\tenied\nABORT\t\timeout\n''\t\t\t'AT %s %s'\n"
			"TIMEOUT\t\t%d\n%s\t\t\\c\nTIMEOUT\t\t5\n\\r\t\t\\c\n",
		 cbtrail[MAXLEN_CBTRAIL+1] =
			"TIMEOUT\t\t%d\n%s\t\t\\c\nTIMEOUT\t\t5\n\\r\t\t\\c\n",
		 script[MAXNUM_SLINES*(2*MAXLEN_SLINE+5)+MAXLEN_PREAMBLE+1];

	umask(077);									/* read/write only by owner */
	scriptF = fopen(scriptfname, "w");			/* open the script file */
	if (scriptF == NULL)						/* failed to do so? */
		doErr("callnumber: fopen");				/* yup, bail out */
	if (autologin) {							/* if automatic login */
		if (callback)							/* and if call-back enabled */
			strcpy(script, preambleCB);			/* copy call-back preamble */
		else									/* otherwise copy */
			strcpy(script, preambleD);			/* the dialing preamble */
		for (i=0; i<numSlines; i++) {			/* append the script lines */
			strcat(script, sline[i]);
			strcat(script, "\n");
		}
		fprintf(scriptF, script, modemDial,		/* using the script as fmt */
				number, connectWait,			/* stick all the rest in */
				modemUConn);
		if (callback) {							/* if call-back is desired */
			strcpy(script, cbtrail);			/* to the script trail add */
			for (i=0; i<numCBSlns; i++) {		/* the call-back lines */
				strcat(script, CBsln[i]);
				strcat(script, "\n");
			}
			fprintf(scriptF, script,			/* using script as format */
					CBDelay, modemUConn);		/* stick the rest in */
		}
	}
	else {										/* PAP/PAPS/CHAPS or manual */
		if (authlogin && callback) {			/* auth-login and call-back? */
			strcpy(script, preambleCB);			/* copy the dialing preamble */
			fprintf(scriptF, script, modemDial,	/* print dial-in script out */
					number, connectWait,
					modemUConn);
			strcpy(script, cbtrail);			/* copy the call-back trail */
			fprintf(scriptF, script, CBDelay,	/* and print the rest out */
					modemUConn);
		}
		else {									/* auth, auth-manual, manual */
			strcpy(script, preambleD);			/* copy the dialing preamble */
			fprintf(scriptF, script, modemDial,	/* and print the script out */
					number, connectWait,
					modemUConn);
		}
	}
	fclose(scriptF);							/* close the script file */
	rcode = child(Chat,"-V","-f",				/* hand it over to chat */
				  scriptfname,(char *)0);
	unlink(scriptfname);						/* done with script, delete */
	if (rcode == 0) {							/* chat terminated OK? */
		pprintf("\n%s: %s %s\n", "xispdial",	/* yes, send "done" message */
				"done dialing", number);
		if (manual && !callback)				/* start manual login window */
			rcode = child(Terminal, "-bgcol",	/* if it is so desired */
						bgcol, "-display",
					  	dispname, "-geometry",
						geometry, (char *)0);
		close(pipeFD);							/* close the report pipe */
		exit(rcode);							/* and exit */
	}
	if (rcode < 0) {							/* nope, chat problem */
		pprintf("\n%s: %s\n", "xispdial",		/* report it */
				"chat execution failed!");
		close(pipeFD);							/* and bail out */
		exit(1);							
	}
	return rcode;								/* else valid report code */
}

void callall(int sn)							/* call the list of numbers */
{
	int i, rcode;

	pprintf("%s: %s #%d\n", "xispdial",			/* report dialing attempt */
			"dialing attempt", sn+1);
	for (i=0; i<numPhones; i++) {				/* try all phones given */
		pprintf("xispdial: dialing: %s\n",		/* report dialed number */
				phone[i]);
		if ((rcode = callnumber(phone[i])))		/* dial it */
			pprintf("\n");						/* not successful, print NL */
		if (rcode == 3) {						/* did chat timeout? */
			pprintf("TIMEOUT\n");				/* yup, report it */
			pprintf("%s: %s %s\n",
				"xispdial", "timeout dialing",
				phone[i]);
			child(Chat,"-V",""," ",(char *)0);	/* and hang-up */
		}
		sleep(sleepDelay);						/* sleep a while */
	}
}


/*+-------------------------------------------------------------------------+
  |                                                                         |
  |                         SIGTERM trap and Main                           |
  |                                                                         |
  +-------------------------------------------------------------------------+*/

void Term(int signum)
{
	switch (signum) {
		case SIGTERM: {							/* SIGTERM signal received */
			pprintf("\nxispdial: SIGTERM\n");	/* return status via pipe */
			if (childPID)						/* if a child is in progress */
				kill(childPID, SIGTERM);		/* send it SIGTERM also */
			child(Chat,"-V",""," ",(char *)0);	/* try hanging-up */
			unlink(envfname);					/* delete environment file */
			unlink(scriptfname);				/* and script file */
			sleep(MODEM_SLEEP);					/* wait a sec before */
			init_modem("\\c");					/* reseting the modem */
			close(pipeFD);						/* close the report pipe */
			exit(1);							/* and exit */
			break;
		}
		default: exit(1);
	}
}

int main()
{
	int rcode, attempt;

	signal(SIGTERM, Term);			/* register SIGTERM handler */
	initFnames();					/* assemble file names used */
	getISPenv();					/* get info via dialing environment file */
	pipeFD = namedPipe(Pipe);		/* initialize named pipe to xISP */

	pprintf("xispdial: PID=%d.\n", getpid());	/* the '.' is important! */
	if (authlogin)
		pprintf("xispdial: dialing ISP: using PAP/CHAP\n");
	else
		pprintf("xispdial: dialing ISP: user: %s\n", account);

/* Prepare the modem init string
   ----------------------------- */
	modemInit = malloc(strlen(modemUInit)+4+1);
	if (modemInit == NULL)
		outOfMem("main");
	strcpy(modemInit, modemUInit);
	strcat(modemInit, " H0");

/* Initialize the modem for dialing
   -------------------------------- */
	if ((rcode=init_modem(modemInit))) {
		pprintf("xispdial: chat returned %d!\n", rcode);
		pprintf("xispdial: modem init failed!\n");
		return 1;
	}

/* Loop dialing the numbers given maxAttempts times
   ------------------------------------------------ */
	for (attempt=0; attempt<maxAttempts; attempt++) {
		pprintf("\n");
		callall(attempt);
		if ((rcode=init_modem(modemInit))) {
			pprintf("xispdial: chat returned %d!\n", rcode);
			pprintf("xispdial: modem re-init failed!\n");
			return 1;
		}
	}
	close(pipeFD);
	return 1;
}