Commits

Robert Lowry committed 97a5425

started refactoring pcw.

  • Participants
  • Parent commits b9d74a6

Comments (0)

Files changed (1)

 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/param.h>
 
+#ifdef __linux__
+#define HAVE_INOTIFY
 #include <sys/inotify.h>
 #include <sys/wait.h>
+#endif
+
+#ifdef BSD
+#define HAVE_KQUEUE
+#endif
+
+#ifndef HAVE_INOTIFY
+#ifndef HAVE_KQUEUE
+#error "Either inotify or kqueue is required for pcw to function"
+#endif
+#endif
 
 #include "config.h"
 
 // fairly arbitray values...feel free to change
-#define PATH_MAX    1024
 #define CMD_MAX     1024
 #define MAX_ARGS    64
 #define MAX_OPENFD  4
 int wins[MAX_WATCH] = {0};
 char paths[MAX_WATCH][PATH_MAX] = {{0}};
 
-void win(int wd)
+void spawn_win(int wd)
 {
 	char out[PATH_MAX], in[PATH_MAX], path[PATH_MAX], channel[32];
 	char *cmd[] = CMD;
 		strcpy(paths[wd], fpath);
 		sprintf(out, "%s/out", fpath);
 		if (!init && stat(out, &st) == 0)
-			win(wd);
+			spawn_win(wd);
 	}
 
 	return 0;
 	}
 }
 
-int main(int argc, char **argv)
+#ifdef HAVE_INOTIFY
+void run_inotify(char *dir)
 {
 	char buf[4096];
 	int len;
 	struct inotify_event *cur;
+
+	if ((evq = inotify_init()) < 0)
+		err(1, "failed on inotify_init()");
+
+	init = 1;
+	ftw(dir, add_dir, MAX_OPENFD);
+	init = 0;
+
+	for (;;) {
+		if ((len = read(evq, buf, sizeof(buf))) < 0 && errno != EINTR)
+			warn("failed to read from inotify event queue");
+
+		for (cur = (struct inotify_event *)buf;
+			 (char *)cur - buf < len;
+			 cur = (struct inotify_event *)((char *)cur + sizeof(struct inotify_event) + cur->len)) {
+
+			if (cur->mask & (IN_CREATE | IN_ISDIR))
+				ftw(dir, add_dir, MAX_OPENFD);
+			if (cur->mask & (IN_CREATE | IN_MODIFY) && !strcmp(cur->name, "out"))
+				spawn_win(cur->wd);
+		}
+	}
+}
+#endif
+
+#ifdef HAVE_KQUEUE
+void run_kqueue()
+{
+}
+#endif
+
+int main(int argc, char **argv)
+{
 	struct stat st;
 
 	if (argc != 2 || !strcmp(argv[1], "-v")) {
 	if (signal(SIGCHLD, sigchld) == SIG_ERR)
 		err(1, "failed installing SIGCHLD 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)
-			warn("failed to read from inotify event queue");
-
-		for (cur = (struct inotify_event *)buf;
-			 (char *)cur - buf < len;
-			 cur = (struct inotify_event *)((char *)cur + sizeof(struct inotify_event) + cur->len)) {
-
-			if (cur->mask & (IN_CREATE | IN_ISDIR))
-				ftw(argv[1], add_dir, MAX_OPENFD);
-			if (cur->mask & (IN_CREATE | IN_MODIFY) && !strcmp(cur->name, "out"))
-				win(cur->wd);
-		}
-	}
-
 	return 0;
 }