Browse Source

Plan 9 from Bell Labs 2003-07-22

David du Colombier 20 years ago
parent
commit
bd923e71f0

+ 17 - 0
dist/replica/plan9.db

@@ -190,6 +190,11 @@
 386/bin/dc - 775 sys sys 1056364238 97891
 386/bin/dd - 775 sys sys 1056364238 46261
 386/bin/deroff - 775 sys sys 1056364239 73222
+386/bin/dial - 20000000775 sys sys 1058790015 0
+386/bin/dial/at - 775 sys sys 1058790014 59165
+386/bin/dial/drain - 775 sys sys 1058790014 4122
+386/bin/dial/expect - 775 sys sys 1058790015 58571
+386/bin/dial/pass - 775 sys sys 1058790016 39133
 386/bin/dict - 775 sys sys 1056364240 168338
 386/bin/diff - 775 sys sys 1056364240 82287
 386/bin/disk - 20000000775 sys sys 984788664 0
@@ -2929,6 +2934,11 @@ rc/bin/eject - 775 sys sys 945617207 123
 rc/bin/fax - 775 sys sys 1015701359 2390
 rc/bin/fedex - 775 sys sys 1048862253 1122
 rc/bin/homespool - 775 sys sys 945617207 339
+rc/bin/ipconf - 20000000775 sys sys 1058791152 0
+rc/bin/ipconf/inside - 775 sys sys 1058790955 427
+rc/bin/ipconf/lra - 775 sys sys 1058791152 1558
+rc/bin/ipconf/outside - 775 sys sys 1058790951 550
+rc/bin/ipconf/theworld - 775 sys sys 1058790940 1212
 rc/bin/ipso - 775 sys sys 1035591514 2304
 rc/bin/iwhois - 775 sys sys 1032057928 435
 rc/bin/kill - 775 sys sys 1018387014 132
@@ -4492,6 +4502,7 @@ sys/man/1/echo - 664 sys sys 944959673 279
 sys/man/1/ed - 664 sys sys 961259286 13916
 sys/man/1/emacs - 664 sys sys 944959673 199
 sys/man/1/eqn - 664 sys sys 944959675 5655
+sys/man/1/expect - 664 sys sys 1058791094 3023
 sys/man/1/faces - 664 sys sys 1045501346 2052
 sys/man/1/factor - 664 sys sys 957920005 1019
 sys/man/1/file - 664 sys sys 1015024739 1578
@@ -6954,6 +6965,12 @@ sys/src/cmd/db/trcrun.c - 664 sys sys 1016731556 4432
 sys/src/cmd/dc.c - 664 sys sys 1014926693 36570
 sys/src/cmd/dd.c - 664 sys sys 1055693435 11721
 sys/src/cmd/deroff.c - 664 sys sys 1017679319 14611
+sys/src/cmd/dial - 20000000775 sys sys 1058789955 0
+sys/src/cmd/dial/at.c - 664 sys sys 1058789951 2011
+sys/src/cmd/dial/drain.c - 664 sys sys 1058789952 241
+sys/src/cmd/dial/expect.c - 664 sys sys 1058789952 1595
+sys/src/cmd/dial/mkfile - 664 sys sys 1058789952 191
+sys/src/cmd/dial/pass.c - 664 sys sys 1058789955 891
 sys/src/cmd/dict - 20000000775 sys sys 944960882 0
 sys/src/cmd/dict/ahd.c - 664 sys sys 944960882 2082
 sys/src/cmd/dict/canonind.awk - 664 sys sys 944960880 665

+ 18 - 0
dist/replica/plan9.log

@@ -12847,3 +12847,21 @@
 1058635864 0 a 386/9pcf - 775 sys sys 1058635394 2362604
 1058635864 1 a 386/9pcf.gz - 664 sys sys 1058635397 862669
 1058659288 0 a sys/src/9/pc/pcf.c - 664 sys sys 1058659039 5110
+1058790758 0 a 386/bin/dial - 20000000775 sys sys 1058790015 0
+1058790758 1 a 386/bin/dial/at - 775 sys sys 1058790014 59165
+1058790758 2 a 386/bin/dial/drain - 775 sys sys 1058790014 4122
+1058790758 3 a 386/bin/dial/expect - 775 sys sys 1058790015 58571
+1058790758 4 a 386/bin/dial/pass - 775 sys sys 1058790016 39133
+1058790758 5 a sys/man/1/expect - 664 sys sys 1058790575 3100
+1058790758 6 a sys/src/cmd/dial - 20000000775 sys sys 1058789955 0
+1058790758 7 a sys/src/cmd/dial/at.c - 664 sys sys 1058789951 2011
+1058790758 8 a sys/src/cmd/dial/drain.c - 664 sys sys 1058789952 241
+1058790758 9 a sys/src/cmd/dial/expect.c - 664 sys sys 1058789952 1595
+1058790758 10 a sys/src/cmd/dial/mkfile - 664 sys sys 1058789952 191
+1058790758 11 a sys/src/cmd/dial/pass.c - 664 sys sys 1058789955 891
+1058792561 0 a rc/bin/ipconf - 20000000775 sys sys 1058791152 0
+1058792561 1 a rc/bin/ipconf/inside - 775 sys sys 1058790955 427
+1058792561 2 a rc/bin/ipconf/lra - 775 sys sys 1058791152 1558
+1058792561 3 a rc/bin/ipconf/outside - 775 sys sys 1058790951 550
+1058792561 4 a rc/bin/ipconf/theworld - 775 sys sys 1058790940 1212
+1058792561 5 c sys/man/1/expect - 664 sys sys 1058791094 3023

+ 19 - 0
rc/bin/ipconf/inside

@@ -0,0 +1,19 @@
+#!/bin/rc
+
+ip/ipconfig
+
+# supply unknowns with inside addresses
+if( ! grep -s 'auth=' /net/ndb){
+	echo '	auth=135.104.9.7'>>/net/ndb
+	echo '	authdom=cs.bell-labs.com'>>/net/ndb
+}
+if( ! grep -s 'ntp=' /net/ndb)
+	echo '	ntp=135.104.9.2'>>/net/ndb
+if( ! grep -s 'dns=' /net/ndb){
+	echo '	dns=135.104.8.38'>>/net/ndb
+	echo '	dns=135.104.70.11'>>/net/ndb
+}
+
+# start dns if it isn't already going
+if(! test -e /srv/dns )
+	ndb/dns -r

+ 92 - 0
rc/bin/ipconf/lra

@@ -0,0 +1,92 @@
+#!/bin/rc
+
+# on hook and initialize
+fn initfn {
+	dial/drain
+	dial/at -q -t 5 zh0
+}
+
+# dial telephone number
+fn dialfn {
+	dial/drain
+	dial/at -q -t 60 dt^$1
+}
+
+# process options
+for(i in $*){
+	switch($i){
+	case '-P'
+		primary=-P
+	}
+}
+
+# the following can be inherited
+switch($dev){
+case ''
+	dev=/dev/eia1
+}
+switch($telno){
+case ''
+	telno=18009878722
+}
+switch($baud){
+case ''
+	baud=115200
+}
+
+{
+	# set up uart
+	if( test -e $dev^ctl ){
+		echo -n b^$baud	# baud rate
+		echo -n m1	# cts/rts flow control
+		echo -n q64000	# big buffer
+		echo -n n1	# nonblocking writes
+		echo -n r1	# rts on
+		echo -n d1	# dtr on
+		echo -n c1	# handup wen we lose dcd
+	} > $dev^ctl
+
+	# get the modem's attention
+	while( ! initfn )
+		sleep 1
+
+	# dial
+	while( ! dialfn $telno )
+		sleep 30
+		
+	if( ! dial/expect -it 60 'username:' ){
+		echo lra: can''t connect >[1=2]
+		exit connect
+	}
+	dial/pass
+	if( ! dial/expect -it 60 'password:' ){
+		echo lra: can''t connect >[1=2]
+		exit connect
+	}
+	dial/pass
+	if( ! dial/expect -t 60 'telnet:' ){
+		echo lra: can''t connect >[1=2]
+		exit connect
+	}
+	echo ppp
+	echo connected to lra >[1=2]
+
+	# start ppp
+	ip/ppp $primary -f
+} < $dev > $dev
+
+# supply unknowns with inside addresses
+if( ! grep -s 'auth=' /net/ndb){
+	echo '	auth=135.104.9.7'>>/net/ndb
+	echo '	authdom=cs.bell-labs.com'>>/net/ndb
+}
+if( ! grep -s 'ntp=' /net/ndb)
+	echo '	ntp=135.104.9.2'>>/net/ndb
+if( ! grep -s 'dns=' /net/ndb){
+	echo '	dns=135.104.8.38'>>/net/ndb
+	echo '	dns=135.104.70.11'>>/net/ndb
+}
+
+# start dns if it isn't already going
+if(! test -e /srv/dns )
+	ndb/dns -r

+ 24 - 0
rc/bin/ipconf/outside

@@ -0,0 +1,24 @@
+#!/bin/rc
+ip/ipconfig -dDG
+
+# supply unknowns with outside addresses
+if( ! grep -s 'auth=' /net/ndb){
+	echo '	auth=204.178.31.3'>>/net/ndb
+	echo '	authdom=cs.bell-labs.com'>>/net/ndb
+}
+if( ! grep -s 'ntp=' /net/ndb)
+	echo '	ntp=204.178.31.2'>>/net/ndb
+if( ! grep -s 'dns=' /net/ndb){
+	echo '	dns=204.178.31.3'>>/net/ndb
+	echo '	dns=204.178.31.4'>>/net/ndb
+}
+
+# set ndbfile for termrc touse
+NDBFILE=/lib/ndb/external
+
+# start dns if it isn't already going
+if(! test -e /srv/dns )
+	ndb/dns -r
+
+# just in case we can't find achille
+cpu=tcp!204.178.31.2

+ 76 - 0
rc/bin/ipconf/theworld

@@ -0,0 +1,76 @@
+#!/bin/rc
+
+fn initfn {
+	dial/flush
+	echo +++
+	echo -n atzh0
+	dial/expect -q -t 5 OK
+}
+
+fn dialfn {
+	dial/flush
+	echo -n atdt^$telno^
+	dial/expect -q -t 60 CONNECT
+}
+
+# process options
+for(i in $*){
+	switch($i){
+	case '-P'
+		primary=-P
+	}
+}
+
+switch($dev){
+case ''
+	dev=/dev/eia1
+}
+switch($telno){
+case ''
+	telno=18009878722
+}
+switch($baud){
+case ''
+	baud=115200
+}
+
+{
+	# set up uart
+	if( test -e $dev^ctl ){
+		echo -n b^$baud
+		echo -n m1	# cts/rts flow control
+		echo -n q64000	# big buffer
+		echo -n n1	# nonblocking writes
+		echo -n r1	# rts on
+		echo -n d1	# dtr on
+		echo -n c1	# handup wen we lose dcd
+	} > $dev^ctl
+
+	# get the modem's attention
+	while( ! initfn )
+		sleep 1
+
+	# dial
+	while( ! dialfn )
+		sleep 30
+	echo connected to the world >[1=2]
+
+	# start ppp
+	ip/ppp $primary -f
+} < $dev > $dev
+
+# supply unknowns with outside addresses
+if( ! grep -s 'auth=' /net/ndb){
+	echo '	auth=204.178.31.3'>>/net/ndb
+	echo '	authdom=cs.bell-labs.com'>>/net/ndb
+}
+if( ! grep -s 'ntp=' /net/ndb)
+	echo '	ntp=204.178.31.2'>>/net/ndb
+if( ! grep -s 'dns=' /net/ndb){
+	echo '	dns=204.178.31.3'>>/net/ndb
+	echo '	dns=204.178.31.4'>>/net/ndb
+}
+
+# start dns if it isn't already going
+if(! test -e /srv/dns )
+	ndb/dns -r

+ 174 - 0
sys/man/1/expect

@@ -0,0 +1,174 @@
+.TH EXPECT 1
+.SH NAME
+at, drain, expect, pass \- dialer scripting tools
+.SH SYNOPSIS
+.B dial/at
+[
+.B -q
+] [
+.B -t
+.I seconds
+]
+atcommand
+.br
+.B dial/expect
+[
+.B -q
+] [
+.B -t
+.I seconds
+] [
+.B -i
+]
+.I goodstring
+[
+.IR badstring ...
+]
+.br
+.B dial/drain
+.br
+.B dial/pass
+[
+.B -q
+]
+.SH DESCRIPTION
+These commands are used to write telephone dialing
+scripts, mostly for PPP sessions.  They all expect standard input and
+output to be connected to a communications device, e.g,
+a serial line to a modem.  They communicate with the user using
+.BR /dev/cons .
+.PP
+.I At
+sends
+.B atcommand
+to the modem prefixed with the string
+.BR at .
+It then reads from the modem expecting an AT response. 
+.I At
+will return success if it gets and
+.B OK
+of
+.B CONNECT
+response.  Otherwise it will return the response as an
+error status.  The options are:
+.TP
+.B -t
+set the timeout to
+.IR seconds .
+The default is 300.
+.TP
+.B -q
+don't write to
+.B /dev/cons
+what is read from standard in.  The default is
+to copy everything through.
+.PD
+.PP
+.I Expect
+reads standard input looking for one of the strings given
+as arguments.  Reading the first string causes a successul exit
+status.  Reading any of the others causes an exit status equal to
+the string.  The command also terminates on a timeout.  The options
+are:
+.TP
+.B -t
+set the timeout to
+.IR seconds .
+The default is 300.
+.TP
+.B -i
+ignore case when doing the matches.
+.TP
+.B -q
+don't write to
+.B /dev/cons
+what is read from standard in.  The default is
+to copy everything through.
+.PD
+.PP
+.I Pass
+copies input from
+.B /dev/cons
+to standard output.
+It terminates on a newline.  The only flag is
+.B -q
+and means the same as it does for
+.IR expect .
+.PP
+.I Drain
+discards any input waiting on standard input.  It
+is used to sync up the stream at the start of dialing
+or after an error.
+.SH FILES
+.B /rc/bin/ipconf/*
+example dialer scripts for ppp
+.SH SOURCE
+.B /sys/src/dial/*.c
+.SH SEE ALSO
+.IR ppp (8),
+.IR telco (4)
+.SH EXAMPLE
+The following
+.B rc
+script dials out through a Hayes compatible modem on
+.B /dev/eia1
+and lets the user type in a user name and password
+before starting
+.BR ppp .
+.EX
+#!/bin/rc
+dev=/dev/eia1
+telno=18005551212
+
+fn initfn {
+	dial/drain
+	echo +++
+	dial/at zh0
+}
+
+fn dialfn {
+	dial/drain
+	dial/at dt^$telno
+}
+{
+	# set up uart
+	if( test -e $dev^ctl ){
+		echo -n b^$baud
+		echo -n m1	# cts/rts flow control
+		echo -n q64000	# big buffer
+		echo -n n1	# nonblocking writes
+		echo -n r1	# rts on
+		echo -n d1	# dtr on
+		echo -n c1	# handup when we lose dcd
+	} > $dev^ctl
+
+	# get the modem's attention
+	while( ! initfn )
+		sleep 1
+
+	# dial
+	while( ! dialfn )
+		sleep 30
+		
+	if( ! dial/expect -it 60 'username:' ){
+		echo can''t connect >[1=2]
+		exit connect
+	}
+	dial/pass
+	if( ! dial/expect -it 60 'password:' ){
+		echo can''t connect >[1=2]
+		exit connect
+	}
+	dial/pass
+	if( ! dial/expect -t 60 'ppp or telnet:' ){
+		echo can''t connect >[1=2]
+		exit connect
+	}
+	echo ppp
+	dial/expect -t 5 something
+	echo connected >[1=2]
+
+	# start ppp
+	ip/ppp $primary -f
+} < $dev > $dev
+.EE

+ 144 - 0
sys/src/cmd/dial/at.c

@@ -0,0 +1,144 @@
+#include <u.h>
+#include <libc.h>
+
+void
+usage(void)
+{
+	fprint(2, "usage: %s [-q] [-t seconds] command\n", argv0);
+	exits("usage");
+}
+
+struct {
+	char	*resp;
+	int	ok;
+} tab[] =
+{
+	{ "ok",			1 },
+	{ "connect",		1 },
+	{ "no carrier",		0 },
+	{ "no dialtone",	0 },
+	{ "error",		0 },
+	{ "busy",		0 },
+	{ "no answer",		0 },
+	{ "delayed",		0 },
+	{ "blacklisted",	0 },
+};
+
+int
+writewithoutcr(int fd, char *p, int i)
+{
+	char *q, *e;
+
+	/* dump cr's */
+	for(e = p+i; p < e; ){
+		q = memchr(p, '\r', e-p);
+		if(q == nil)
+			break;
+		if(q > p)
+			if(write(fd, p, q-p) < 0)
+				return -1;
+		p = q+1;
+	}
+	if(p < e)
+		if(write(fd, p, e-p) < 0)
+			return -1;
+	return i;
+}
+
+int
+readln(int fd, char *buf, int n)
+{
+	int c, i, sofar;
+
+	sofar = 0;
+	buf[sofar] = 0;
+	while(sofar < n-1){
+		i = read(fd, buf+sofar, 1);
+		if(i <= 0)
+			return i;
+		c = buf[sofar];
+		if(c == '\r')
+			continue;
+		sofar++;
+		if(c == '\n')
+			break;
+	}
+	buf[sofar] = 0;
+	return sofar;
+}
+
+void
+docmd(char *cmd, int timeout, int quiet, int consfd)
+{
+	char buf[4096];
+	int i;
+	char *p, *cp;
+
+	if(timeout == 0){
+		if(*cmd == 'd' || *cmd == 'D')
+			timeout = 2*60;
+		else
+			timeout = 5;
+	}
+
+	p = smprint("at%s\r", cmd);
+	for(cp = p; *cp; cp++){
+		write(1, cp, 1);
+		sleep(100);
+	}
+	free(p);
+
+	alarm(timeout*1000);
+	for(;;){
+		i = readln(0, buf, sizeof(buf));
+		if(i <= 0){
+			rerrstr(buf, sizeof buf);
+			exits(buf);
+		}
+		if(!quiet)
+			writewithoutcr(consfd, buf, i);
+		for(i = 0; i < nelem(tab); i++)
+			if(cistrstr(buf, tab[i].resp)){
+				if(tab[i].ok)
+					goto out;
+				else
+					exits(buf);
+			}
+	}
+out:
+	alarm(0);
+}
+
+void
+main(int argc, char **argv)
+{
+	int timeout;
+	int quiet;
+	int i;
+	int consfd;
+
+	timeout = 0;
+	quiet = 0;
+	ARGBEGIN {
+	case 't':
+		timeout = atoi(EARGF(usage()));
+		break;
+	case 'q':
+		quiet = 1;
+		break;
+	default:
+		usage();
+	} ARGEND;
+
+	if(argc < 1)
+		usage();
+
+	consfd = open("/dev/cons", ORDWR);
+	if(consfd < 0)
+		sysfatal("opening /dev/cons: %r");
+
+	for(i = 0; i < argc; i++)
+		docmd(argv[i], timeout, quiet, consfd);
+
+	exits(0);
+}

+ 23 - 0
sys/src/cmd/dial/drain.c

@@ -0,0 +1,23 @@
+#include <u.h>
+#include <libc.h>
+
+void
+ding(void*, char *s)
+{
+	if(strstr(s, "alarm"))
+		noted(NCONT);
+	else
+		noted(NDFLT);
+}
+
+void
+main(void)
+{
+	char buf[256];
+
+	alarm(100);
+	while(read(0, buf, sizeof(buf)) > 0)
+		;
+	alarm(0);
+	exits(0);
+}

+ 110 - 0
sys/src/cmd/dial/expect.c

@@ -0,0 +1,110 @@
+#include <u.h>
+#include <libc.h>
+
+void
+usage(void)
+{
+	fprint(2, "usage: %s [-t secs] [-iq] good-string [bad-strings]\n", argv0);
+	exits("usage");
+}
+
+void
+catch(void*, char *s)
+{
+	exits(s);
+}
+
+int
+writewithoutcr(int fd, char *p, int i)
+{
+	char *q, *e;
+
+	/* dump cr's */
+	for(e = p+i; p < e; ){
+		q = memchr(p, '\r', e-p);
+		if(q == nil)
+			break;
+		if(q > p)
+			if(write(fd, p, q-p) < 0)
+				return -1;
+		p = q+1;
+	}
+	if(p < e)
+		if(write(fd, p, e-p) < 0)
+			return -1;
+	return i;
+}
+
+void
+main(int argc, char **argv)
+{
+	int timeout = 5*60;
+	int quiet = 0;
+	int ignorecase = 0;
+	int fd, i, m, n, bsize;
+	char *good;
+	char *buf;
+	int sofar;
+
+	ARGBEGIN {
+	case 'i':
+		ignorecase = 1;
+		break;
+	case 't':
+		timeout = atoi(EARGF(usage()));
+		break;
+	case 'q':
+		quiet = 1;
+		break;
+	} ARGEND;
+
+	if(argc < 1)
+		usage();
+
+	good = argv[0];
+	n = strlen(good);
+
+	for(i = 1; i < argc; i++){
+		m = strlen(argv[i]);
+		if(m > n)
+			n = m;
+	}
+
+	fd = open("/dev/cons", ORDWR);
+	if(fd < 0)
+		sysfatal("opening /dev/cons: %r");
+
+	bsize = n+4096;
+	buf = malloc(bsize+1);
+
+	sofar = 0;
+	alarm(timeout*1000);
+	for(;;){
+		if(sofar > n){
+			memmove(buf, &buf[sofar-n], n);
+			sofar = n;
+		}
+		i = read(0, buf+sofar, bsize);
+		if(i <= 0)
+			exits("EOF");
+		if(!quiet)
+			writewithoutcr(fd, buf+sofar, i);
+		sofar += i;
+		buf[sofar] = 0;
+		if(ignorecase){
+			if(cistrstr(buf, good))
+				break;
+			for(i = 1; i < argc; i++)
+				if(cistrstr(buf, argv[i]))
+					exits(argv[i]);
+		} else {
+			if(strstr(buf, good))
+				break;
+			for(i = 1; i < argc; i++)
+				if(strstr(buf, argv[i]))
+					exits(argv[i]);
+		}
+	}
+
+	exits(0);
+}

+ 20 - 0
sys/src/cmd/dial/mkfile

@@ -0,0 +1,20 @@
+</$objtype/mkfile
+
+TARG=expect\
+	pass\
+	drain\
+	at\
+
+OFILES=
+
+BIN=/$objtype/bin/dial
+
+UPDATE=\
+	mkfile\
+	$HFILES\
+	${OFILES:%.$O=%.c}\
+	${TARG:%=%.c}\
+	/sys/man/1/con\
+
+</sys/src/cmd/mkmany
+

+ 64 - 0
sys/src/cmd/dial/pass.c

@@ -0,0 +1,64 @@
+#include <u.h>
+#include <libc.h>
+
+int alarmed;
+
+void
+usage(void)
+{
+	fprint(2, "usage: %s [-q]\n", argv0);
+	exits("usage");
+}
+
+void
+ding(void*, char *s)
+{
+	if(strstr(s, "alarm")){
+		alarmed = 1;
+		noted(NCONT);
+	} else
+		noted(NDFLT);
+}
+
+void
+main(int argc, char **argv)
+{
+	int fd, cfd;
+	char buf[1];
+	int quiet = 0;
+	int done = 0;
+
+	ARGBEGIN {
+	case 'q':
+		quiet = 1;
+		break;
+	} ARGEND;
+
+	notify(ding);
+
+	fd = open("/dev/cons", ORDWR);
+	if(fd < 0)
+		sysfatal("opening /dev/cons: %r");
+	cfd = open("/dev/consctl", OWRITE);
+	if(cfd >= 0)
+		fprint(cfd, "rawon");
+
+	while(!done){
+		if(read(fd, buf, 1) <= 0)
+			break;
+		if(buf[0] == '\n' || buf[0] == '\r')
+			done = 1;
+		if(write(1, buf, 1) < 0)
+			break;
+		if(!quiet){
+			alarmed = 0;
+			alarm(500);
+			if(read(0, buf, 1) <= 0 && !alarmed)
+				break;
+			alarm(0);
+			if(buf[0] != '\r' && write(fd, buf, 1) < 0)
+				break;
+		}
+	}
+	exits(0);
+}