Anonymous avatar Anonymous committed 3d3a994

can define the command to execute on the command line

Comments (0)

Files changed (1)

 #include <sys/wait.h>
 
 #define PATH_MAX    1024
+#define CMD_MAX     1024
+#define MAX_ARGS    64
 #define MAX_OPENFD  4
 #define MAX_WATCH   512
 
 char paths[MAX_WATCH][PATH_MAX] = {{0}};
 int wins[MAX_WATCH] = {0};
 
-char term[16] = "urxvt";
+char cmd[CMD_MAX] = "urxvt -title %p -e srw -p %c cw %o %i";
+char *cmd_argv[MAX_ARGS] = {NULL};
 
 void usage(void)
 {
 
 void win(int wd)
 {
-	char out[PATH_MAX], in[PATH_MAX], path[PATH_MAX], prompt[32];
-	char *wincmd[] = {term, "-title", paths[wd], "-e", "srw", "-p", prompt, "cw", out, in, NULL};
+	char out[PATH_MAX], in[PATH_MAX], path[PATH_MAX], channel[32];
+	int i;
 
 	if (wins[wd])
 		return;
 
+	for (i = 0; cmd_argv[i]; i++) {
+	    if (cmd_argv[i][0] == '%' && cmd_argv[i][1] && cmd_argv[i][2] == '\0') {
+		switch (cmd_argv[i][1]) {
+		    case 'c' : cmd_argv[i] = channel; break;
+		    case 'i' : cmd_argv[i] = in;      break;
+		    case 'o' : cmd_argv[i] = out;     break;
+		    case 'p' : cmd_argv[i] = path;    break;
+		}
+	    }
+	}
+
 	strcpy(path, paths[wd]);
-	sprintf(prompt, "%s> ", basename(path));
+	sprintf(channel, "%s> ", basename(path));
 	sprintf(out, "%s/out", paths[wd]);
 	sprintf(in,  "%s/in",  paths[wd]);
 
+	for (i = 0; cmd_argv[i]; i++)
+	    printf("%s ", cmd_argv[i]);
+	printf("\n");
+
 	if ((wins[wd] = fork()) == 0) {
-		execvp(wincmd[0], wincmd);
-		err(1, "failed on execvp %s", term);
+		execvp(cmd_argv[0], cmd_argv);
+		err(1, "failed on execvp %s", cmd);
 	}
 }
 
 
 int main(int argc, char **argv)
 {
-	char buf[4096];
+	char *root, buf[4096], *t;
 	int i, len;
 	struct inotify_event *cur;
 	struct stat st;
 		if (argv[i][2] != '\0')
 			usage();
 		switch (argv[i][1]) {
-			case 't' : if (i + 1 >= argc) usage(); strncpy(term, argv[++i], sizeof(term)); break;
+			case 'c' : if (i + 1 >= argc) usage(); strncpy(cmd, argv[++i], sizeof(cmd)); break;
 			case 'v' : printf("pcw-"VERSION" © Evan Gates\n"); exit(1);
 			default  : usage();
 		}
 	if (i >= argc)
 		usage();
 
-	if (stat(argv[i], &st) < 0 || !S_ISDIR(st.st_mode))
-		errx(1, "%s does not exist or is not a directory", argv[i]);
+	root = argv[i];
+
+	for (i = 0, t = strtok(cmd, " "); i < MAX_ARGS - 1 && t; i++, t = strtok(NULL, " "))
+	    cmd_argv[i] = t;
+
+	if (stat(root, &st) < 0 || !S_ISDIR(st.st_mode))
+		errx(1, "%s does not exist or is not a directory", root);
 
 	if (signal(SIGCHLD, sigchld) == SIG_ERR)
 		err(1, "failed installing SIGCHLD handler");
 		err(1, "failed on inotify_init()");
 
 	init = 1;
-	ftw(argv[i], add_dir, MAX_OPENFD);
+	ftw(root, add_dir, MAX_OPENFD);
 	init = 0;
 
 	for (;;) {
 			warn("failed to read from inotify event queue");
 		for (cur = (struct inotify_event *)buf; (char *)cur - buf < len; cur += sizeof(struct inotify_event) + cur->len) {
 			if (cur->mask & (IN_CREATE | IN_ISDIR))
-				ftw(argv[i], add_dir, MAX_OPENFD);
+				ftw(root, add_dir, MAX_OPENFD);
 			if (cur->mask & (IN_CREATE | IN_MODIFY) && !strcmp(cur->name, "out"))
 				win(cur->wd);
 		}
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.