Browse Source

Plan 9 from Bell Labs 2009-04-23

David du Colombier 15 years ago
parent
commit
cc4f537702
1 changed files with 43 additions and 2 deletions
  1. 43 2
      sys/src/cmd/ip/telnet.c

+ 43 - 2
sys/src/cmd/ip/telnet.c

@@ -12,6 +12,8 @@ int interrupted;
 int localecho;
 int notkbd;
 
+static char *srv;
+
 typedef struct Comm Comm;
 struct Comm {
 	int returns;
@@ -38,7 +40,7 @@ static int islikeatty(int);
 void
 usage(void)
 {
-	fatal("usage: telnet [-Cdnr] net!host[!service]", 0, 0);
+	fatal("usage: telnet [-Cdnr] [-s srv] net!host[!service]", 0, 0);
 }
 
 void
@@ -60,6 +62,9 @@ main(int argc, char *argv[])
 	case 'r':
 		returns = 0;
 		break;
+	case 's':
+		srv = EARGF(usage());
+		break;
 	default:
 		usage();
 	}ARGEND
@@ -96,6 +101,21 @@ dodial(char *dest)
 	return data;
 }
 
+void
+post(char *srv, int fd)
+{
+	int f;
+	char buf[32];
+
+	f = create(srv, OWRITE, 0666);
+	if(f < 0)
+		sysfatal("create %s: %r", srv);
+	snprint(buf, sizeof buf, "%d", fd);
+	if(write(f, buf, strlen(buf)) != strlen(buf))
+		sysfatal("write %s: %r", srv);
+	close(f);
+}
+
 /*
  *  two processes pass bytes back and forth between the
  *  terminal and the network.
@@ -104,8 +124,24 @@ void
 telnet(int net)
 {
 	int pid;
+	int p[2];
+	char *svc;
 
 	rawoff();
+	svc = nil;
+	if (srv) {
+		if(pipe(p) < 0)
+			sysfatal("pipe: %r");
+		if (srv[0] != '/')
+			svc = smprint("/srv/%s", srv);
+		else
+			svc = srv;
+		post(svc, p[0]);
+		close(p[0]);
+		dup(p[1], 0);
+		dup(p[1], 1);
+		/* pipe is now std in & out */
+	}
 	ttypid = getpid();
 	switch(pid = rfork(RFPROC|RFFDG|RFMEM)){
 	case -1:
@@ -115,6 +151,8 @@ telnet(int net)
 		rawoff();
 		notify(notifyf);
 		fromnet(net);
+		if (svc)
+			remove(svc);
 		sendnote(ttypid, "die");
 		exits(0);
 	default:
@@ -122,7 +160,10 @@ telnet(int net)
 		notify(notifyf);
 		fromkbd(net);
 		if(notkbd)
-			for(;;)sleep(0);
+			for(;;)
+				sleep(0);
+		if (svc)
+			remove(svc);
 		sendnote(netpid, "die");
 		exits(0);
 	}