]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added logic for command execution. Segfaults galore, but it's on the right track
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 25 Nov 2016 19:16:29 +0000 (14:16 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 25 Nov 2016 19:16:29 +0000 (14:16 -0500)
inc/edit.h
libedit/exec.c
xedit.c

index bf7b8a9762f35adb4912b80b2f0b7d5a47d2fe0d..299d8cd99760326b095f6d5597a1fc9c9e170447 100644 (file)
@@ -186,6 +186,7 @@ void detach(Process* proc);
 void terminate(Process* proc, int sig);
 char* cmdread(char** cmd);
 void cmdwrite(char** cmd, char* text);
+char* cmdwriteread(char** cmd, char* text);
 
 /* Color Scheme Handling
  *****************************************************************************/
index 8f1b6c0a94f644ab15756ef7fa8d1a266fbbe5cd..1bd0c1e510b76c607e4d483adf518c060de7a79e 100644 (file)
@@ -80,3 +80,19 @@ void cmdwrite(char** cmd, char* text) {
     detach(&proc);
     waitpid(proc.pid, NULL, 0);
 }
+
+char* cmdwriteread(char** cmd, char* text) {
+    Process proc;
+    if (execute(cmd, &proc) < 0) {
+        perror("failed to execute");
+        return NULL;
+    }
+    if (write(proc.in, text, strlen(text)) < 0) {
+        perror("failed to write");
+        return NULL;
+    }
+    char* str = fdgets(proc.out);
+    detach(&proc);
+    waitpid(proc.pid, NULL, 0);
+    return str;
+}
diff --git a/xedit.c b/xedit.c
index 1ff626d4cb194cc5011e779fb16f40288c204101..5e7e7a98ccd2823f6452b424f5796c5f3c9c8a87 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -444,7 +444,40 @@ static void tag_exec(Tag* tag, char* arg) {
 }
 
 static void cmd_exec(char* cmd) {
-    printf("exec cmd: '%s'\n", cmd);
+    char op = '\0';
+    if (*cmd == '!' || *cmd == '<' || *cmd == '|' || *cmd == '>')
+        op = *cmd, cmd++;
+    /* convert the command to an array */
+    size_t len = 0;
+    char **cmdary = NULL, *part = NULL;
+    while (true) {
+        part = strtok((!part ? cmd : NULL), " ");
+        cmdary = realloc(cmdary, len+1 * sizeof(char*));
+        cmdary[len++] = part;
+        if (!part) break;
+    }
+    /* execute the command */
+    char *input = NULL, *output = NULL;
+    enum RegionId dest = EDIT;
+    input = view_getstr(getview(EDIT), NULL);
+    if (op == '!') {
+        printf("null: '%s' argc: %lu\n", cmd, len);
+    } else if (op == '>') {
+        cmdwrite(cmdary, input);
+    } else if (op == '|') {
+        output = cmdwriteread(cmdary, input);
+    } else {
+        if (op != '<') dest = Focused;
+        output = cmdread(cmdary);
+    }
+    if (output) {
+        view_putstr(getview(dest), output);
+        view_selprev(getview(dest));
+    }
+    /* cleanup */
+    free(input);
+    free(output);
+    free(cmdary);
 }
 
 static void exec(char* cmd) {