void colors_init(char* path) {
if (config_get_bool(SyntaxEnabled))
- cmdspawn((char*[]){ "tide-hl.rb", path, NULL }, &ChildIn, &ChildOut);
+ exec_spawn((char*[]){ "tide-hl.rb", path, NULL }, &ChildIn, &ChildOut);
}
SyntaxSpan* colors_scan(SyntaxSpan* spans, Buf* buf, size_t beg, size_t end) {
if (e > 0 && c > 0) {
c = (c > 15 ? config_get_int(SynNormal + (c >> 4) - 1) : c) & 0xf;
currspan = mkspan(beg+b, beg+e-1, c, currspan);
- //printf("(%lu-%lu) %lu,%lu,%lu\n", beg, end, b, e, c);
}
if (!firstspan)
firstspan = currspan;
} while (e > 0);
- //printf("left: %lu\n", DataEnd-DataBeg);
- //printf("done\n\n");
fflush(stdout);
DataBeg = DataEnd = Buffer;
}
}
}
long nwrite = write(ChildIn, wbuf, wlen);
- //printf("write: %lu -> %d\n", wlen, nwrite);
if (nwrite < 0) {
- //perror("write failed:");
/* child process probably died. shut everything down */
close(ChildIn), ChildIn = -1;
close(ChildOut), ChildOut = -1;
#include <unistd.h>
#include <sys/wait.h>
-static uint NumChildren = 0;
-
#define PIPE_READ 0
#define PIPE_WRITE 1
int err; /* file descriptor for the child process's standard error */
} Proc;
-static int execute(char** cmd, Proc* proc) {
- int inpipe[2], outpipe[2], errpipe[2];
- /* create the pipes */
- if ((pipe(inpipe) < 0) || (pipe(outpipe) < 0) || (pipe(errpipe) < 0))
- return -1;
- /* create the process */
- proc->pid = fork();
- if (proc->pid < 0) {
- /* signal that we failed to fork */
- proc->in = -1;
- proc->out = -1;
- proc->err = -1;
- } else if (0 == proc->pid) {
- /* redirect child process's io to the pipes */
- if ((dup2(inpipe[PIPE_READ], STDIN_FILENO) < 0) ||
- (dup2(outpipe[PIPE_WRITE], STDOUT_FILENO) < 0) ||
- (dup2(errpipe[PIPE_WRITE], STDERR_FILENO) < 0)) {
- perror("failed to pipe");
- exit(1);
- }
- /* execute the process */
- close(inpipe[PIPE_WRITE]);
- close(outpipe[PIPE_READ]);
- close(errpipe[PIPE_READ]);
- exit(execvp(cmd[0], cmd));
- } else {
- close(inpipe[PIPE_READ]);
- close(outpipe[PIPE_WRITE]);
- close(errpipe[PIPE_WRITE]);
- proc->in = inpipe[PIPE_WRITE];
- proc->out = outpipe[PIPE_READ];
- proc->err = errpipe[PIPE_READ];
- }
- return proc->pid;
-}
-
-int cmdspawn(char** cmd, int* in, int* out) {
- Proc proc;
- if (execute(cmd, &proc) < 0) {
- perror("failed to execute");
- return -1;
- }
- *in = proc.in;
- *out = proc.out;
- return proc.pid;
-}
-
-int cmdrun(char** cmd, char** err) {
- Proc proc;
- if (execute(cmd, &proc) < 0) {
- perror("failed to execute");
- return -1;
- }
- NumChildren++;
- if (err) *err = fdgets(proc.err);
- close(proc.in);
- close(proc.out);
- close(proc.err);
- return proc.pid;
-}
-
-char* cmdread(char** cmd, char** err) {
- Proc proc;
- if (execute(cmd, &proc) < 0) {
- perror("failed to execute");
- return NULL;
- }
- close(proc.in);
- char* str = fdgets(proc.out);
- close(proc.out);
- if (err) *err = fdgets(proc.err);
- close(proc.err);
- waitpid(proc.pid, NULL, 0);
- return str;
-}
-
-void cmdwrite(char** cmd, char* text, char** err) {
- Proc proc;
- if (execute(cmd, &proc) < 0) {
- perror("failed to execute");
- return;
- }
- if (text && write(proc.in, text, strlen(text)) < 0) {
- perror("failed to write");
- return;
- }
- close(proc.in);
- if (err) *err = fdgets(proc.err);
- close(proc.err);
- close(proc.out);
- waitpid(proc.pid, NULL, 0);
-}
-
-char* cmdwriteread(char** cmd, char* text, char** err) {
- Proc proc;
- if (execute(cmd, &proc) < 0) {
- perror("failed to execute");
- return NULL;
- }
- if (text && write(proc.in, text, strlen(text)) < 0) {
- perror("failed to write");
- return NULL;
- }
- close(proc.in);
- char* str = fdgets(proc.out);
- close(proc.out);
- if (err) *err = fdgets(proc.err);
- close(proc.err);
- waitpid(proc.pid, NULL, 0);
- return str;
-}
-
-/******************************************************************************/
-
typedef struct Job Job;
typedef struct {
static void recv_data(int fd, void* data);
static void watch_or_close(bool valid, int dir, int fd, void* data);
static void rcvr_finish(Rcvr* rcvr);
+static int execute(char** cmd, Proc* proc);
-void exec_reap(void) {
+bool exec_reap(void) {
int pid;
while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
Job* job = JobList;
free(job);
}
}
+ return (JobList != NULL);
}
void exec_job(char** cmd, char* data, size_t ndata, View* dest) {
}
}
+void exec_cmd(char** cmd, char* text, char** out, char** err) {
+ Proc proc;
+ if (execute(cmd, &proc) < 0) {
+ perror("failed to execute");
+ return;
+ }
+ /* send the input to stdin of the command */
+ if (text && write(proc.in, text, strlen(text)) < 0) {
+ perror("failed to write");
+ return;
+ }
+ close(proc.in);
+ /* read the stderr of the command */
+ if (err) *err = fdgets(proc.err);
+ close(proc.err);
+ /* read the stdout of the command */
+ if (out) *out = fdgets(proc.out);
+ close(proc.out);
+ /* wait for the process to finish */
+ waitpid(proc.pid, NULL, 0);
+}
+
+int exec_spawn(char** cmd, int* in, int* out) {
+ Proc proc;
+ if (execute(cmd, &proc) < 0) {
+ perror("failed to execute");
+ return -1;
+ }
+ *in = proc.in;
+ *out = proc.out;
+ return proc.pid;
+}
+
static void send_data(int fd, void* data) {
Job* job = data;
long nwrite = write(fd, (job->data + job->nwrite), job->ndata);
view->selection.end = rcvr->beg + rcvr->count;
}
}
+
+static int execute(char** cmd, Proc* proc) {
+ int inpipe[2], outpipe[2], errpipe[2];
+ /* create the pipes */
+ if ((pipe(inpipe) < 0) || (pipe(outpipe) < 0) || (pipe(errpipe) < 0))
+ return -1;
+ /* create the process */
+ proc->pid = fork();
+ if (proc->pid < 0) {
+ /* signal that we failed to fork */
+ proc->in = -1;
+ proc->out = -1;
+ proc->err = -1;
+ } else if (0 == proc->pid) {
+ /* redirect child process's io to the pipes */
+ if ((dup2(inpipe[PIPE_READ], STDIN_FILENO) < 0) ||
+ (dup2(outpipe[PIPE_WRITE], STDOUT_FILENO) < 0) ||
+ (dup2(errpipe[PIPE_WRITE], STDERR_FILENO) < 0)) {
+ perror("failed to pipe");
+ exit(1);
+ }
+ /* execute the process */
+ close(inpipe[PIPE_WRITE]);
+ close(outpipe[PIPE_READ]);
+ close(errpipe[PIPE_READ]);
+ exit(execvp(cmd[0], cmd));
+ } else {
+ close(inpipe[PIPE_READ]);
+ close(outpipe[PIPE_WRITE]);
+ close(errpipe[PIPE_WRITE]);
+ proc->in = inpipe[PIPE_WRITE];
+ proc->out = outpipe[PIPE_READ];
+ proc->err = errpipe[PIPE_READ];
+ }
+ return proc->pid;
+}
}
static void open_file(void) {
- char* file = cmdread(PickFileCmd, NULL);
+ char* file = NULL;
+ exec_cmd(PickFileCmd, NULL, &file, NULL);
if (file) {
file = chomp(file);
if ((!win_buf(EDIT)->path || x11_keymodsset(ModShift)) &&
view_init(win_view(EDIT), file, ondiagmsg);
} else {
OpenCmd[1] = file;
- cmdrun(OpenCmd, NULL);
+ exec_job(OpenCmd,0,0,0);
}
}
free(file);
static void pick_symbol(char* symbol) {
PickTagCmd[1] = "fetch";
PickTagCmd[3] = symbol;
- char* pick = cmdread(PickTagCmd, NULL);
+ char* pick = NULL;
+ exec_cmd(PickTagCmd, NULL, &pick, NULL);
if (pick) {
Buf* buf = win_buf(EDIT);
if (buf->path && 0 == strncmp(buf->path, pick, strlen(buf->path))) {
view_init(win_view(EDIT), pick, ondiagmsg);
} else {
OpenCmd[1] = chomp(pick);
- cmdrun(OpenCmd, NULL);
+ exec_job(OpenCmd,0,0,0);
+
}
}
}
view->selection.end = buf_byrune(&(view->buffer), view->selection.end, RIGHT);
PickTagCmd[1] = "print";
PickTagCmd[3] = view_getstr(view, NULL);
- char* pick = cmdread(PickTagCmd, NULL);
+ char* pick = NULL;
+ exec_cmd(PickTagCmd, NULL, &pick, NULL);
if (pick)
view_putstr(view, chomp(pick));
free(PickTagCmd[3]);
for (argc--, argv++; argc > 1; argc--, argv++) {
if (!strcmp(*argv, "--")) break;
OpenCmd[1] = *argv;
- cmdrun(OpenCmd, NULL);
+ exec_job(OpenCmd,0,0,0);
}
/* if we still have args left we're going to open it in this instance */