]> git.mdlowis.com Git - projs/tide.git/commitdiff
added callback for getting job return status
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 29 Jul 2017 02:24:49 +0000 (22:24 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 29 Jul 2017 02:24:49 +0000 (22:24 -0400)
inc/edit.h
lib/exec.c
lib/x11.c
pickfile
picktag
tide.c

index 93f5bc8fd95e1dbc6783a27e4ead331a56e83c6a..c8ab63f04b6074a71193cdbc70209f504e882432 100644 (file)
@@ -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);
 
index 30476e4984c1f19b598d5d829dc04ec2602bf399..4fc33b624eed3161113f9d586984daca6021998e 100644 (file)
@@ -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 */
index 4c3bf8d39fb4bb843d395e38d9c00cd1004191bb..bc8950aa59b040e7e32bdd666712ddd56e326959 100644 (file)
--- 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));
     }
 }
index 495d49a8e953d8949cb7720a368b3c9f328dd8b2..f02e9d060de42da067617fa2f94f226d6fbe0e7b 100755 (executable)
--- 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 68a0b977d66162e0e96d4118c3d51e3d7ffcb8b0..d0ba890d31a358a933e36c18d0c26ccb15f3e536 100755 (executable)
--- 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 db183730088313979823fd2bbabac3d600b4ed9a..b1a89a0f707eb15ba9ae6344b2bc2204eafda751 100644 (file)
--- 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++);