Commits

Robert Lowry committed 6c9d4dd

got pcw working on OpenBSD, still some issues with cw_color.

Comments (0)

Files changed (1)

 #include "config.h"
 
 // fairly arbitray values...feel free to change
-#define CMD_MAX     1024
-#define MAX_ARGS    64
 #define MAX_OPENFD  4
-#define MAX_WATCH   512
 
 typedef struct watch_t watch_t;
 struct watch_t {
 	int wd;
 	int pid;
-	int isdir;
 	char *path;
 	watch_t *next;
+#ifdef HAVE_KQUEUE
+	int outwd;
+#endif
 };
 
 watch_t *watch = NULL;
 	}
 }
 
-watch_t *add_watch(int wd, const char *path, int isdir)
+watch_t *add_watch(int wd, const char *path)
 {
 	char out[PATH_MAX];
 	struct stat st;
 	}
 	for(w = watch; w; w = w->next) {
 		if (w->wd == wd) {
-			w->isdir = isdir;
 			if(!w->path) {
 				w->path = strdup(path);
-				if(w->isdir) {
-					snprintf(out,sizeof(out),"%s/out",path);
-					if(!init && stat(out, &st) == 0)
-						spawn_win(w);
-				}
+				snprintf(out,sizeof(out),"%s/out",path);
+				if(!init && stat(out, &st) == 0)
+					spawn_win(w);
 			}
 			break;
 		}
 			w->next->wd = wd;
 		}
 	}
-	
 	return w;
 }
 
 		return 0;
 	}
 
-	w = add_watch(wd,fpath,1);
+	add_watch(wd,fpath);
 
 	return 0;
 }
 			if (cur->mask & (IN_CREATE | IN_ISDIR))
 				ftw(dir, add_dir_inotify, MAX_OPENFD);
 			if (cur->mask & (IN_CREATE | IN_MODIFY) && !strcmp(cur->name, "out")) {
-				for (w = watch; w; w = w->next)
-					if(w->wd == cur->wd)
+				for (w = watch; w; w = w->next) {
+					if(w->wd == cur->wd) {
+						spawn_win(w);
 						break;
-				if (w)
-					spawn_win(w);
+					}
+				}
 			}
 		}
 	}
 	watch_t *w;
 	int fd;
 
-	printf("adding %s\n",fpath);
-
 	if(!(typeflag & FTW_D))
 		return 0;
 
-	for (w = watch; w; w = w->next)
-		if (w->path && !strcmp(w->path, fpath))
-			return 0;
+	printf("adding %s\n",fpath);
 
-	if((fd = open(fpath, O_RDONLY)) == -1)
-		err(1,"directory open");
+	for (w = watch; w; w = w->next) {
+		if (w->path && !strcmp(w->path, fpath)) {
+			if(w->outwd)
+				return 0;
+			else
+				break;
+		}
+	}
 
-	EV_SET(&kadd, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, 0);
-	if(kevent(kq, &kadd, 1, NULL, 0, NULL) == -1)
-		err(1,"kevent set EVFILT_VNODE");
+	if (!w) {
+		if((fd = open(fpath, O_RDONLY)) == -1)
+			err(1,"directory open");
+		w = add_watch(fd,fpath);
 
-	/* TODO: replace with add_watch()*/
-	if (!watch)
-		watch = new_watch();
-	for(w = watch; w; w = w->next)
-		if(w->next == NULL)
-			break;
-	w->next = new_watch();
-	w = w->next;
-	w->wd = fd;
-	w->path = strdup(fpath);
-	snprintf(out,sizeof(out),"%s/out",fpath);
-	if(stat(out, &st) == 0) {
-		fd = open(out, O_RDONLY);
-		EV_SET(&kadd, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, 0);
+		EV_SET(&kadd, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, w);
 		if(kevent(kq, &kadd, 1, NULL, 0, NULL) == -1)
-			err(1,"kevent set EVFILT_NODE");
-		if(!init)
-			spawn_win(w); 
+			err(1,"kevent set EVFILT_VNODE");
+		}
+
+	if(!w->outwd) {
+		snprintf(out,sizeof(out),"%s/out",fpath);
+		if(stat(out, &st) == 0) {
+			printf("adding %s/out\n",fpath);
+			fd = open(out, O_RDONLY);
+			EV_SET(&kadd, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, w);
+			if(kevent(kq, &kadd, 1, NULL, 0, NULL) == -1)
+				err(1,"kevent set EVFILT_NODE");
+			w->outwd = fd;
+			if(!init)
+				spawn_win(w); 
+		}
 	}
 
 	return 0;
 void run_kqueue(char *dir)
 {
 	struct kevent event;
+	watch_t *w;
 	int i;
 
 	if ((kq = kqueue()) == -1)
 
 	for (;;) {
 		i = kevent(kq, NULL, 0, &event, 1, NULL);
-		if (i == -1) {
-			if (errno == EINTR)
-				continue;
-			else
-				err(1, "kevent read");
+		if (i == -1 && errno != EINTR)
+			err(1, "kevent read");
+		if (i > 0) {
+			printf("event recieved\n");
+			w = (watch_t *)event.udata;
+			if(event.ident == w->wd) {
+				ftw(w->path, add_dir_kqueue, MAX_OPENFD);
+			} else if (event.ident == w->outwd) {
+				spawn_win(w);
+			}
 		}
-		if (i > 0) 
-			printf("event recieved\n");
-
 	}
 }
 #endif // HAVE_KQUEUE