+int
+setautorun(char *selector, char *value, int flags)
+{
+ int ws_id;
+ char s[1024];
+ union arg a;
+ char *real_args[] = { NULL, NULL };
+ long pid;
+ struct pid_e *p;
+
+ if (getenv("SWM_STARTED"))
+ return (0);
+
+ bzero(s, sizeof s);
+ if (sscanf(value, "ws[%d]:%1023s", &ws_id, s) != 2)
+ errx(1, "invalid autorun entry, should be 'ws:command'\n");
+ ws_id--;
+ if (ws_id < 0 || ws_id >= SWM_WS_MAX)
+ errx(1, "invalid workspace %d\n", ws_id + 1);
+
+ /*
+ * This is a little intricate
+ *
+ * If the pid already exists we simply reuse it because it means it was
+ * used before AND not claimed by manage_window. We get away with
+ * altering it in the parent after INSERT because this can not be a race
+ */
+ real_args[0] = s;
+ a.argv = real_args; /* XXX this sucks and should have args for real */
+ if ((pid = fork()) == 0) {
+ spawn(ws_id, &a, 1);
+ /* NOTREACHED */
+ _exit(1);
+ }
+
+ /* parent */
+ p = find_pid(pid);
+ if (p == NULL) {
+ p = calloc(1, sizeof *p);
+ if (p == NULL)
+ return (1);
+ TAILQ_INSERT_TAIL(&pidlist, p, entry);
+ }
+
+ p->pid = pid;
+ p->ws = ws_id;
+
+ return (0);
+}
+