Anonymous avatar Anonymous committed d811377

working?

Comments (0)

Files changed (2)

     while (fgets(buf, sizeof(buf), in))
 	fputs(buf, stdout);
 
-    fputs("NOTICE: waiting for other side to open out fifo...", stdout);
+    fputs("NOTICE: waiting for other side to open out fifo...\n", stdout);
     fflush(stdout);
 
     if ((out = fopen(argv[2], "w")) == NULL)
 	err(1, "failed fopen()");
 
     fputs("OK\n", stdout);
+	fflush(stdout);
 
     for (;;) {
 	FD_ZERO(&rd);
 #include <termios.h>
 #include <stdio.h>
+#include <sys/select.h>
 
 int main(int argc, char **argv)
 {
 	struct termios set;
-	char buf[4096], tput_cub[32];
+	char buf[4096], bufout[4096], tput_cub[32], tmp[L_tmpnam], cmd[1024];
 	int i, j;
-	FILE *file;
+	FILE *pipe, *fifo;
+	fd_set rd;
 
-	if ((file = popen("tput cub1", "r")) == NULL)
+	if ((pipe = popen("tput cub1", "r")) == NULL)
 		warn("failed on popen tput (please forgive me)");
 
-	if (fgets(tput_cub, sizeof(tput_cub), file) == NULL)
+	if (fgets(tput_cub, sizeof(tput_cub), pipe) == NULL)
 		warn("failed to read tput cub1 (please forgive me)");
 
-	pclose(file);
+	pclose(pipe);
 
-	if ((file = popen(argv[1], "w")) == NULL)
-		warn("could not popen");
+	if (mkfifo(tmpnam(tmp), 0700) < 0)
+		err(1, "could not mkfifo %s", tmp);
+
+	sprintf(cmd, "%s > %s", argv[1], tmp);
+	puts(cmd);
+
+	if ((pipe = popen(cmd, "w")) == NULL)
+		warn("could not popen %s", cmd);
+
+	if ((fifo = fopen(tmp, "w+")) == NULL)
+		err(1, "could not open fifo %s", tmp);
 
 	tcgetattr(0, &set);
 	set.c_lflag &= ~(ICANON | ECHO);
 	set.c_cc[VTIME] = 0;
 	tcsetattr(0, TCSANOW, &set);
 
-	for (i = 0; i < sizeof(buf) - 1 && read(0, buf + i, 1) > 0; i++) {
-		buf[i + 1] = '\0';
+	//for (i = 0; i < sizeof(buf) - 1 && read(0, buf + i, 1) > 0; i++) {
+	for (;;) {
+		FD_ZERO(&rd);
+		FD_SET(0, &rd);
+		FD_SET(fileno(fifo), &rd);
 
-		switch (buf[i]) {
-			case '\n' :
-				for (j = 0; j < i; j++)
-					fprintf(stderr, "%s %s", tput_cub, tput_cub);
-				fputs(buf, file);
-				fflush(stderr);
-				fflush(file);
-				i = -1;
-				break;
-			case '\b' :
-			case 127  :
-				if (i--) {
-					buf[i--] = '\0';
-					fprintf(stderr, "%s %s", tput_cub, tput_cub);
-				}
-				break;
-			default :
-				putc(buf[i], stderr);
-				fflush(stderr);
+		select(fileno(fifo) + 1, &rd, NULL, NULL, NULL);
+
+		if (FD_ISSET(0, &rd)) {
+			if (read(0, buf + i, 1) < 0)
+				warn("failed reading from stdin");
+
+			buf[i + 1] = '\0';
+
+			switch (buf[i]) {
+				case '\n' :
+					for (j = 0; j < i; j++)
+						fprintf(stdout, "%s %s", tput_cub, tput_cub);
+					fputs(buf, pipe);
+					fflush(stdout);
+					fflush(pipe);
+					buf[0] = '\0';
+					i = -1;
+					break;
+				case '\b' :
+				case 127  :
+					if (i--) {
+						buf[i--] = '\0';
+						fprintf(stdout, "%s %s", tput_cub, tput_cub);
+					}
+					break;
+				default :
+					putc(buf[i], stdout);
+					fflush(stdout);
+			}
+			i++;
+		}
+		if (FD_ISSET(fileno(fifo), &rd)) {
+			if (fgets(bufout, sizeof(bufout), fifo) == NULL)
+				warn("failed to fgets from fifo %s", tmp);
+			for (j = 0; j < i; j++)
+				printf("%s %s", tput_cub, tput_cub);
+			fputs(bufout, stdout);
+			fputs(buf, stdout);
+			fflush(stdout);
 		}
 	}
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.