From 887539662180de9be34d65ea48ee0726f95acfbb Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 28 Jul 2017 22:24:49 -0400 Subject: [PATCH] added callback for getting job return status --- inc/edit.h | 2 +- lib/exec.c | 28 +++++++++++++++++----------- lib/x11.c | 2 +- pickfile | 3 ++- picktag | 24 +++++++++++++----------- tide.c | 9 ++++----- 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 93f5bc8..c8ab63f 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -224,7 +224,7 @@ Rune view_getrune(View* view); /* Command Executions *****************************************************************************/ bool exec_reap(void); -void exec_job(char** cmd, char* data, size_t ndata, View* dest); +void exec_job(char** cmd, char* data, size_t ndata, View* dest, void (*donefn)(int)); int exec_cmd(char** cmd, char* text, char** out, char** err); int exec_spawn(char** cmd, int* in, int* out); diff --git a/lib/exec.c b/lib/exec.c index 30476e4..4fc33b6 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -26,15 +26,16 @@ typedef struct { } Rcvr; struct Job { - Job* next; /* Pointer to previous job in the job list */ - Job* prev; /* Pointer to next job in the job list */ - Proc proc; /* Process id and descriptors */ - size_t ndata; /* number of bytes to write to stdout */ - size_t nwrite; /* number of bytes written to stdout so far */ - char* data; /* data to write to stdout */ - Rcvr err_rcvr; /* reciever for the error output of the job */ - Rcvr out_rcvr; /* receiver for the normal output of the job */ - View* dest; /* destination view where output will be placed */ + Job* next; /* Pointer to previous job in the job list */ + Job* prev; /* Pointer to next job in the job list */ + Proc proc; /* Process id and descriptors */ + size_t ndata; /* number of bytes to write to stdout */ + size_t nwrite; /* number of bytes written to stdout so far */ + char* data; /* data to write to stdout */ + Rcvr err_rcvr; /* reciever for the error output of the job */ + Rcvr out_rcvr; /* receiver for the normal output of the job */ + View* dest; /* destination view where output will be placed */ + void (*donefn)(int); /* function called with return status of the job */ }; static Job* JobList = NULL; @@ -49,21 +50,25 @@ static void rcvr_finish(Rcvr* rcvr); static int execute(char** cmd, Proc* proc); bool exec_reap(void) { + int status; Job* job = JobList; while (job) { if (job_done(job)) { rcvr_finish(&(job->out_rcvr)); rcvr_finish(&(job->err_rcvr)); + waitpid(job->proc.pid, &status, WNOHANG); + if (job->donefn) job->donefn(status); job = job_finish(job); } else { job = job->next; } } - while (waitpid(-1, NULL, WNOHANG) > 0); + if (JobList == NULL) + while (waitpid(-1, &status, WNOHANG) > 0); return (JobList != NULL); } -void exec_job(char** cmd, char* data, size_t ndata, View* dest) { +void exec_job(char** cmd, char* data, size_t ndata, View* dest, void (*donefn)(int)) { Job* job = calloc(1, sizeof(Job)); job->proc.pid = execute(cmd, &(job->proc)); if (job->proc.pid < 0) { @@ -78,6 +83,7 @@ void exec_job(char** cmd, char* data, size_t ndata, View* dest) { job->nwrite = 0; job->data = data; job->next = JobList; + job->donefn = donefn; if (JobList) JobList->prev = job; JobList = job; /* register watch events for file descriptors */ diff --git a/lib/x11.c b/lib/x11.c index 4c3bf8d..bc8950a 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -200,7 +200,7 @@ void x11_finish(void) { if (Selections[CLIPBOARD].text) { char* text = Selections[CLIPBOARD].text; size_t len = strlen(text); - exec_job((char*[]){ "xcpd", NULL }, text, len, NULL); + exec_job((char*[]){ "xcpd", NULL }, text, len, NULL, NULL); while (event_poll(100)); } } diff --git a/pickfile b/pickfile index 495d49a..f02e9d0 100755 --- a/pickfile +++ b/pickfile @@ -5,4 +5,5 @@ if [ "$#" -ne 1 ]; then fi export PICKTITLE="Pick File ($PWD)" -find $1 -not -path '*/\.*' -type f | sed "s|^\./||" | pick | xargs -r tide +file="$(find $1 -not -path '*/\.*' -type f | sed 's|^\./||' | pick)" +tide "$file" diff --git a/picktag b/picktag index 68a0b97..d0ba890 100755 --- a/picktag +++ b/picktag @@ -15,7 +15,7 @@ usage(){ if [ "" == "$TAGFILE" ] || [ "" == "$ACTION" ]; then usage fi - +ma printtags(){ cat "$TAGFILE" | grep -v '^!' | cut -f1 | uniq } @@ -29,16 +29,18 @@ fetch(){ TAG=$(printtags | pick) [ "" == "$TAG" ] && exit fi - awk -v TAG="$TAG" ' - BEGIN { FS = "[\t]+" } - ($1 == TAG) { - matchstr = $3 - sub(/^\//, "\"", matchstr) - sub(/\$?\/;"$/, "\"", matchstr) - gsub(/\*/, "\\*", matchstr) - print "grep -Hn", matchstr, $2, "| cut -d: -f1,2" - } - ' "$TAGFILE" | /bin/sh | pick | xargs -r tide + file=$(awk -v TAG="$TAG" ' + BEGIN { FS = "[\t]+" } + ($1 == TAG) { + matchstr = $3 + sub(/^\//, "\"", matchstr) + sub(/\$?\/;"$/, "\"", matchstr) + gsub(/\*/, "\\*", matchstr) + gsub(/(\[|\])/, "\\\1", matchstr) + print "grep -Hn", matchstr, $2, "| cut -d: -f1,2" + } + ' "$TAGFILE" | /bin/sh | pick) + [ "" != "$file" ] && tide "$file" } export PICKTITLE="Pick CTag ($PWD)" diff --git a/tide.c b/tide.c index db18373..b1a89a0 100644 --- a/tide.c +++ b/tide.c @@ -80,13 +80,13 @@ static void cmd_exec(char* cmd) { /* execute the job */ if (op == '!') - free(input), exec_job(execcmd, NULL, 0, NULL); + free(input), exec_job(execcmd, NULL, 0, NULL, NULL); else if (op == '>') - exec_job(execcmd, input, len, tags); + exec_job(execcmd, input, len, tags, NULL); else if (op == '|' || op == ':') - exec_job(execcmd, input, len, edit); + exec_job(execcmd, input, len, edit, NULL); else - exec_job(execcmd, input, len, (op != '<' ? curr : edit)); + exec_job(execcmd, input, len, (op != '<' ? curr : edit), NULL); } static void cmd_execwitharg(char* cmd, char* arg) { @@ -95,7 +95,6 @@ static void cmd_execwitharg(char* cmd, char* arg) { free(cmd); } - static void exec(char* cmd) { /* skip leading space */ for (; *cmd && isspace(*cmd); cmd++); -- 2.49.0