Commits

Evan Gates  committed a0b7245

add auto open on start, and SIGUSR1 to reopen, thanks bobertlo for the open() O_NONBLOCK | O_WRONLY tip

  • Participants
  • Parent commits 06ad5b8

Comments (0)

Files changed (2)

 
 .SH NOTES
 .TP
+pcw will initially open a terminal for each channel you are in
+.TP
+when pcw receives SIGUSR1 it opens a terminal for each channel you are in that does not currently have an open terminal
+.TP
 when cw receives SIGWINCH it clears the screen and reprints the infile (useful with srw, hit C\-l if the terminal cuts off text after being resized)
 
 .SH AUTHOR
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <ftw.h>
 #include <signal.h>
 #include <stdio.h>
 #include <sys/inotify.h>
 #include <sys/wait.h>
 
-#include "config.h"
+#include "config.h" // for CMD
 
 // fairly arbitray values...feel free to change
 #define PATH_MAX    1024
 #define MAX_OPENFD  4
 #define MAX_WATCH   512
 
-int evq, init;
+int evq;
 int wins[MAX_WATCH] = {0};
 char paths[MAX_WATCH][PATH_MAX] = {{0}};
 
 void win(int wd)
 {
 	char out[PATH_MAX], in[PATH_MAX], path[PATH_MAX], channel[32];
-	char *cmd[] = CMD;
+	char *cmd[] = CMD; // from config.h
 
 	if (wins[wd])
 		return;
 
 int add_dir(const char *fpath, const struct stat *sb, int typeflag)
 {
-	int wd;
-	char out[PATH_MAX];
+	int wd, fd;
+	char out[PATH_MAX], in[PATH_MAX];
 	struct stat st;
 
 	if (!(typeflag & FTW_D))
 	if (paths[wd][0] == '\0') {
 		strcpy(paths[wd], fpath);
 		sprintf(out, "%s/out", fpath);
-		if (!init && stat(out, &st) == 0)
+		sprintf(in,  "%s/in",  fpath);
+		// open() to see if ii has the fifo open for reading
+		if (stat(out, &st) == 0 && stat(in, &st) == 0 && (fd = open(in, O_WRONLY | O_NONBLOCK)) != -1) {
+			close(fd);
 			win(wd);
+		}
 	}
 
 	return 0;
 	return -1;
 }
 
+void sigusr(int unused) {
+	int i, fd;
+	char out[PATH_MAX], in[PATH_MAX];
+	struct stat st;
+
+	for (i = 0; i < MAX_WATCH; i++) {
+		if (paths[i][0] == '\0')
+			continue;
+		sprintf(out, "%s/out", paths[i]);
+		sprintf(in,  "%s/in",  paths[i]);
+		if (stat(out, &st) == 0 && stat(in, &st) == 0 && (fd = open(in, O_WRONLY | O_NONBLOCK)) != -1) {
+			close(fd);
+			win(i);
+		}
+	}
+}
+
 void sigchld(int unused) {
 	pid_t pid;
 	int wd;
 	if (signal(SIGCHLD, sigchld) == SIG_ERR)
 		err(1, "failed installing SIGCHLD handler");
 
+	if (signal(SIGUSR1, sigusr) == SIG_ERR)
+		err(1, "failed installing SIGUSR1 handler");
+
 	if ((evq = inotify_init()) < 0)
 		err(1, "failed on inotify_init()");
 
-	init = 1;
 	ftw(argv[1], add_dir, MAX_OPENFD);
-	init = 0;
 
 	for (;;) {
 		if ((len = read(evq, buf, sizeof(buf))) < 0 && errno != EINTR)