Source

pcw-bsd / pcw.c

Full commit
#include <err.h>
#include <errno.h>
#include <ftw.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <libgen.h>

#ifdef __linux__
#define HAVE_INOTIFY
#include <sys/inotify.h>
#include <sys/wait.h>
#else
#define HAVE_KQUEUE
#include <sys/event.h>
#endif

#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 pid;
	char *path;
	watch_t *next;
#ifdef HAVE_INOTIFY
	int wd;
#endif
};

watch_t *watch = NULL;
char *nick = DEFAULT_NICK;
int color = DEFAULT_COLOR_SETTING;
int init;

#ifdef HAVE_INOTIFY
int evq;
#else
int kq;
#endif

void usage()
{
	fprintf(stderr, "pcw-"VERSION" (c) Evan Gates\n");
	fprintf(stderr, "Usage: pcw [-v] [-c nick] dir\n");
	exit(1);
}

void sigchld(int unused) {
	pid_t pid;
	watch_t *w;

	while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
		for (w = watch; w; w = w->next)
			if (w->pid == pid)
				w->pid = 0;
}

watch_t *new_watch()
{
	watch_t *w = malloc(sizeof(watch_t));
	memset(w,0x00,sizeof(watch_t));
	return w;
}

void spawn_win(watch_t *w)
{
	char out[PATH_MAX], in[PATH_MAX], path[PATH_MAX], channel[32];
	char *cmd[] = CMD;
	char *cmd_color[] = CMD_COLOR;

	if(w->pid) return;

	strncpy(path,w->path,sizeof(path));
	snprintf(channel,sizeof(channel),"%s> ",basename(path));
	snprintf(out,sizeof(out),"%s/out",w->path);
	snprintf(in,sizeof(in),"%s/in",w->path);

	if ((w->pid = fork()) == 0) {
		color ? execvp(cmd_color[0],cmd_color) : execvp(cmd[0],cmd);
		err(1, "failed on execvp %s", color ? cmd_color[0] : cmd[0]);
	}
}

#ifdef HAVE_INOTIFY
int add_dir_inotify(const char *fpath, const struct stat *sb, int typeflag)
{
	char out[PATH_MAX];
	struct stat st;
	watch_t *w;
	int wd;

	if(!(typeflag & FTW_D))
		return 0;

	if ((wd = inotify_add_watch(evq, fpath, IN_CREATE | IN_MODIFY)) < 0) {
		warn("failed on inotify_add_watch on %s", fpath);
		return 0;
	}

	/* TODO: oops, this can be simplified greatly */
	if (!watch) {
		watch = new_watch();
		watch->wd = wd;
	}
	for(w = watch; w; w = w->next) {
		if (w->wd == wd) {
			if(!w->path) {
				w->path = strdup(fpath);
				snprintf(out,sizeof(out),"%s/out",fpath);
				if(!init && stat(out, &st) == 0)
					spawn_win(w);
			}
			break;
		}
		if (w->next == NULL) {
			w->next = new_watch();
			w->next->wd = wd;
		}
	}
	return 0;
}

void run_inotify(char *dir)
{
	char buf[4096];
	int len;
	struct inotify_event *cur;
	watch_t *w;

	if ((evq = inotify_init()) < 0)
		err(1, "failed on inotify_init()");

	init = 1;
	ftw(dir, add_dir_inotify, 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_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)
						break;
				if (w)
					spawn_win(w);
			}
		}
	}
}
#endif

#ifdef HAVE_KQUEUE
int add_dir_kqueue(const char *fpath, const struct stat *sb, int typeflag)
{
	char out[PATH_MAX];
	struct stat st;
	watch_t *w;

	if(!(typeflag & FTW_D))
		return 0;

	// add watch here

	if (!watch)
		watch = new_watch();

	return 0;
}

void run_kqueue(char *dir)
{
	struct kevent change;
	struct kevent event;
	int i;

	if ((kq == kqueue()) == -1)
		perror("kqueue failed");
}
#endif

int main(int argc, char **argv)
{
	struct stat st;
	char *dir = NULL;
	int i;

	if (argc<2) usage();

	for (i = 1; i < argc; i++) {
		if (argv[i][0] != '-') {
			if(!dir) {
				dir = argv[i];
				continue;
			} else usage();
		}
		if (argv[i][2] != '\0')
			usage();
		switch (argv[i][1]) {
			case 'c': color = 1; nick = argv[++i]; break;
			case 'v': break;
			default: usage();
		}
	}
	if (!dir) usage();

	if (stat(dir, &st) < 0 || !S_ISDIR(st.st_mode))
		errx(1, "%s does not exist or is not a directory", dir);

	if (signal(SIGCHLD, sigchld) == SIG_ERR)
		err(1, "failed installing SIGCHLD handler");

#ifdef HAVE_INOTIFY
	run_inotify(dir);
#else
	run_kqueue(dir);
#endif

	return 0;
}