xref: /csrg-svn/contrib/connectd/cd/con.c (revision 60591)
1*60591Sbostic /*-
2*60591Sbostic  * Copyright (c) 1993 The Regents of the University of California.
338014Sbostic  * All rights reserved.
438014Sbostic  *
538014Sbostic  * This code is derived from software contributed to Berkeley by
638014Sbostic  * Bill Jolitz.
738014Sbostic  *
8*60591Sbostic  * %sccs.include.redist.c%
938014Sbostic  */
1038014Sbostic 
1138014Sbostic #ifndef lint
12*60591Sbostic static char sccsid[] = "@(#)con.c	5.2 (Berkeley) 05/29/93";
1338014Sbostic #endif /* not lint */
1438014Sbostic 
1538014Sbostic #include <sys/types.h>
1638014Sbostic #include <sys/time.h>
1738014Sbostic #include <sys/ioctl.h>
1838014Sbostic #include <stdio.h>
1938014Sbostic 
2038014Sbostic char string[80] ; char *seq(), *find(), *passuntil() ;
2138014Sbostic fd_set fdset;
2238014Sbostic struct timeval tv_10th = { 0, 100000 } ; /* 1/10th of a second */
2338014Sbostic # define TIMEOUT	600	/* 10ths of a second */
main(argc,argv)2438014Sbostic main (argc, argv) char *argv[]; {
2538014Sbostic 	int n,m,r,rv; char *cp ;
2638014Sbostic 
2738014Sbostic fprintf(stderr,"%s\n", argv[1]) ;
2838014Sbostic 	FD_ZERO(&fdset) ;
2938014Sbostic 	/* flush out any line noise */
3038014Sbostic 	seq ("\r", 0) ;
3138014Sbostic 	sleep (1) ;
3238014Sbostic 	ioctl (0, TIOCFLUSH, &n) ;
3338014Sbostic 
3438014Sbostic 	if (strcmp (argv[1], "drop") == 0) {
3538014Sbostic 		(void) select(0,0,0,0,&tv_10th);
3638014Sbostic 		seq("+", 5) ;
3738014Sbostic 		(void) select(0,0,0,0,&tv_10th);
3838014Sbostic 		seq("+", 5) ;
3938014Sbostic 		(void) select(0,0,0,0,&tv_10th);
4038014Sbostic 		seq("+", 5) ;
4138014Sbostic 		ioctl (0, TIOCFLUSH, &n) ;
4238014Sbostic 		(void) select(0,0,0,0,&tv_10th);
4338014Sbostic 		if(!find("\r\nOK\r\n", 30)) ;
4438014Sbostic 		ioctl (0, TIOCFLUSH, &n) ;
4538014Sbostic 		seq("ATH0\r", 5) ;
4638014Sbostic 		/*if(!find("\r\nOK\r\n", 10)) ;*/
4738014Sbostic 		cp = passuntil("\n",TIMEOUT);
4838014Sbostic 		fprintf(stderr, "|%s| ", cp) ;
4938014Sbostic 		cp = passuntil("\n",TIMEOUT);
5038014Sbostic 		fprintf(stderr, "|%s| ", cp) ;
5138014Sbostic 		if (strncmp(cp, "OK", 2) == 0) rv = 0;
5238014Sbostic 		else	rv = 1;
5338014Sbostic 	} else {
5438014Sbostic 	system ("stty raw -echo 1200") ;
5538014Sbostic 		if ((seq ("ATDT", 10) == 0)
5638014Sbostic 		&& (seq (argv[1], 30) == 0) && (seq ("\r", 10) == 0) ) ;
5738014Sbostic 		else write(2,"Cannot sync with hayes\n", 23) ;
5838014Sbostic 		/*if(!find("\r\nCONNECT", TIMEOUT))
5938014Sbostic 			passuntil("\r\n",20);*/
6038014Sbostic 		cp = passuntil("\n",TIMEOUT);
6138014Sbostic 		fprintf(stderr, "|%s| ", cp) ;
6238014Sbostic 		cp = passuntil("\n",TIMEOUT);
6338014Sbostic 		fprintf(stderr, "|%s| ", cp) ;
6438014Sbostic 		if (strncmp(cp, "CONNECT", 7) == 0) rv = 0;
6538014Sbostic 		else	rv = 1;
6638014Sbostic fprintf(stderr,"rv = %d\n", rv) ;
6738014Sbostic 	}
6838014Sbostic 	write (2, "\r\n", 2) ;
6938014Sbostic 	return (rv) ;
7038014Sbostic }
7138014Sbostic 
seq(s,t)7238014Sbostic char *seq (s,t) char *s; {
7338014Sbostic 	char c; int n;
7438014Sbostic 
7538014Sbostic 	do {
7638014Sbostic 		write (1, s, 1) ;
7738014Sbostic 		n = 0 ;
7838014Sbostic 	loop:
7938014Sbostic 		FD_SET(0,&fdset) ;
8038014Sbostic 		if (select(1,&fdset,0,0,&tv_10th) > 0) {
8138014Sbostic 			n = read (0, &c, 1) ;
8238014Sbostic 			write (2, &c, 1) ;
8338014Sbostic 		}
8438014Sbostic 		else if (!t)  return ((char *) -1) ;
8538014Sbostic 		else  { t-- ; write (2, "*", 1); goto loop ; }
8638014Sbostic 		if (n != 1 || c != *s) return (s) ;
8738014Sbostic 	}	while (*++s != '\0') ;
8838014Sbostic 	return ( (char *) 0) ;
8938014Sbostic }
9038014Sbostic 
9138014Sbostic char buf2[80];
find(s,n)9238014Sbostic char *find (s, n) char *s; {
9338014Sbostic 	int m,r; char *sp = buf2;
9438014Sbostic 
9538014Sbostic 	m = n ;
9638014Sbostic 	while (m > 0) {
9738014Sbostic 		FD_SET(0,&fdset) ;
9838014Sbostic 		if ((r=select(1,&fdset,0,0,&tv_10th)) > 0) {
9938014Sbostic 			ioctl (0, FIONREAD, &n) ;
10038014Sbostic 			read (0, sp, n) ;
10138014Sbostic 			write (2, sp, n) ;
10238014Sbostic 			sp += n;
10338014Sbostic 		} else	m-- ;
10438014Sbostic 		if (strcmp(s, buf2) == 0) return ( (char *) 0) ;
10538014Sbostic 	}
10638014Sbostic 	return ( (char *) -1) ;
10738014Sbostic }
10838014Sbostic 
passuntil(s,n)10938014Sbostic char *passuntil (s, n) char *s; {
11038014Sbostic 	int m,r; char *sp = buf2;
11138014Sbostic 
11238014Sbostic 	m = n ;
11338014Sbostic 	while (m > 0) {
11438014Sbostic 		FD_SET(0,&fdset) ;
11538014Sbostic 		if ((r=select(1,&fdset,0,0,&tv_10th)) > 0) {
11638014Sbostic 			ioctl (0, FIONREAD, &n) ;
11738014Sbostic 			n = read (0, sp, n) ;
11838014Sbostic 			write (2, sp, n) ;
11938014Sbostic 			sp += n; *sp = '\0' ;
12038014Sbostic 			do if(index(s,sp[-n])) return (buf2) ; while (--n) ;
12138014Sbostic 		} else	m-- ;
12238014Sbostic 	}
12338014Sbostic 	return ( (char *) -1) ;
12438014Sbostic }
125