xref: /original-bsd/contrib/connectd/cd/con.c (revision e59fb703)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Bill Jolitz.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #ifndef lint
22 static char sccsid[] = "@(#)con.c	5.1 (Berkeley) 05/16/89";
23 #endif /* not lint */
24 
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <sys/ioctl.h>
28 #include <stdio.h>
29 
30 char string[80] ; char *seq(), *find(), *passuntil() ;
31 fd_set fdset;
32 struct timeval tv_10th = { 0, 100000 } ; /* 1/10th of a second */
33 # define TIMEOUT	600	/* 10ths of a second */
34 main (argc, argv) char *argv[]; {
35 	int n,m,r,rv; char *cp ;
36 
37 fprintf(stderr,"%s\n", argv[1]) ;
38 	FD_ZERO(&fdset) ;
39 	/* flush out any line noise */
40 	seq ("\r", 0) ;
41 	sleep (1) ;
42 	ioctl (0, TIOCFLUSH, &n) ;
43 
44 	if (strcmp (argv[1], "drop") == 0) {
45 		(void) select(0,0,0,0,&tv_10th);
46 		seq("+", 5) ;
47 		(void) select(0,0,0,0,&tv_10th);
48 		seq("+", 5) ;
49 		(void) select(0,0,0,0,&tv_10th);
50 		seq("+", 5) ;
51 		ioctl (0, TIOCFLUSH, &n) ;
52 		(void) select(0,0,0,0,&tv_10th);
53 		if(!find("\r\nOK\r\n", 30)) ;
54 		ioctl (0, TIOCFLUSH, &n) ;
55 		seq("ATH0\r", 5) ;
56 		/*if(!find("\r\nOK\r\n", 10)) ;*/
57 		cp = passuntil("\n",TIMEOUT);
58 		fprintf(stderr, "|%s| ", cp) ;
59 		cp = passuntil("\n",TIMEOUT);
60 		fprintf(stderr, "|%s| ", cp) ;
61 		if (strncmp(cp, "OK", 2) == 0) rv = 0;
62 		else	rv = 1;
63 	} else {
64 	system ("stty raw -echo 1200") ;
65 		if ((seq ("ATDT", 10) == 0)
66 		&& (seq (argv[1], 30) == 0) && (seq ("\r", 10) == 0) ) ;
67 		else write(2,"Cannot sync with hayes\n", 23) ;
68 		/*if(!find("\r\nCONNECT", TIMEOUT))
69 			passuntil("\r\n",20);*/
70 		cp = passuntil("\n",TIMEOUT);
71 		fprintf(stderr, "|%s| ", cp) ;
72 		cp = passuntil("\n",TIMEOUT);
73 		fprintf(stderr, "|%s| ", cp) ;
74 		if (strncmp(cp, "CONNECT", 7) == 0) rv = 0;
75 		else	rv = 1;
76 fprintf(stderr,"rv = %d\n", rv) ;
77 	}
78 	write (2, "\r\n", 2) ;
79 	return (rv) ;
80 }
81 
82 char *seq (s,t) char *s; {
83 	char c; int n;
84 
85 	do {
86 		write (1, s, 1) ;
87 		n = 0 ;
88 	loop:
89 		FD_SET(0,&fdset) ;
90 		if (select(1,&fdset,0,0,&tv_10th) > 0) {
91 			n = read (0, &c, 1) ;
92 			write (2, &c, 1) ;
93 		}
94 		else if (!t)  return ((char *) -1) ;
95 		else  { t-- ; write (2, "*", 1); goto loop ; }
96 		if (n != 1 || c != *s) return (s) ;
97 	}	while (*++s != '\0') ;
98 	return ( (char *) 0) ;
99 }
100 
101 char buf2[80];
102 char *find (s, n) char *s; {
103 	int m,r; char *sp = buf2;
104 
105 	m = n ;
106 	while (m > 0) {
107 		FD_SET(0,&fdset) ;
108 		if ((r=select(1,&fdset,0,0,&tv_10th)) > 0) {
109 			ioctl (0, FIONREAD, &n) ;
110 			read (0, sp, n) ;
111 			write (2, sp, n) ;
112 			sp += n;
113 		} else	m-- ;
114 		if (strcmp(s, buf2) == 0) return ( (char *) 0) ;
115 	}
116 	return ( (char *) -1) ;
117 }
118 
119 char *passuntil (s, n) char *s; {
120 	int m,r; char *sp = buf2;
121 
122 	m = n ;
123 	while (m > 0) {
124 		FD_SET(0,&fdset) ;
125 		if ((r=select(1,&fdset,0,0,&tv_10th)) > 0) {
126 			ioctl (0, FIONREAD, &n) ;
127 			n = read (0, sp, n) ;
128 			write (2, sp, n) ;
129 			sp += n; *sp = '\0' ;
130 			do if(index(s,sp[-n])) return (buf2) ; while (--n) ;
131 		} else	m-- ;
132 	}
133 	return ( (char *) -1) ;
134 }
135