aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-05-03 09:22:02 -0400
committerShav Kinderlehrer <[email protected]>2023-05-03 09:22:02 -0400
commit4618e66f550e7e6b8410fba771345dd4730329f1 (patch)
treec9120bbb63dbf4ac3bf03316092021d026255641
parent0e157084ea2f576cb1786c6c9064056e28be57c4 (diff)
downloadlat-4618e66f550e7e6b8410fba771345dd4730329f1.tar.gz
lat-4618e66f550e7e6b8410fba771345dd4730329f1.zip
Add extension exec scaffolding
-rw-r--r--include/exec.h6
-rw-r--r--src/lib/exec.c73
-rw-r--r--src/main.c9
3 files changed, 85 insertions, 3 deletions
diff --git a/include/exec.h b/include/exec.h
new file mode 100644
index 0000000..a761c3e
--- /dev/null
+++ b/include/exec.h
@@ -0,0 +1,6 @@
+#ifndef EXEC_H
+#define exec_h
+#include "types.h"
+
+struct line runextension(struct line *data, char *filename, size_t n);
+#endif
diff --git a/src/lib/exec.c b/src/lib/exec.c
new file mode 100644
index 0000000..eb0aebc
--- /dev/null
+++ b/src/lib/exec.c
@@ -0,0 +1,73 @@
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "arg.h"
+#include "types.h"
+#include "util.h"
+
+struct line runextension(struct line *data, char *filename, size_t n) {
+ struct line line;
+
+ int in[2];
+ int out[2];
+ if (pipe(out) == -1 || pipe(in) == -1)
+ die("pipe");
+
+ pid_t pid = fork();
+ if (pid == -1)
+ die("fork");
+
+ if (pid == 0) {
+ // child
+ dup2(out[1], STDOUT_FILENO);
+ close(out[0]);
+ close(out[1]);
+
+ dup2(in[1], STDIN_FILENO);
+ close(in[0]);
+ close(in[1]);
+
+ char *eargv[4];
+ eargv[0] = conf.extension;
+ eargv[1] = filename;
+ eargv[2] = malloc((intlen(n) + 1) * sizeof(char));
+ if (eargv[2] == NULL)
+ die("malloc");
+ sprintf(eargv[2], "%zu", n);
+ eargv[3] = NULL;
+
+ if (execvp(conf.extension, eargv) == -1)
+ die("exec");
+ } else {
+ // parent
+ close(out[1]);
+ close(in[1]);
+
+ write(in[0], "test\0", 4);
+
+ char *buf = malloc(256);
+ memset(buf, '\0', 256);
+
+ size_t len = read(out[0], buf, 256);
+
+ line.buf = buf;
+ line.len = len;
+
+ int status;
+ waitpid(pid, &status, 0);
+
+ if (WEXITSTATUS(status) != EXIT_SUCCESS) {
+ die("exec fail");
+ }
+ }
+
+ close(out[0]);
+ close(out[1]);
+ close(in[0]);
+ close(in[1]);
+
+ return line;
+}
diff --git a/src/main.c b/src/main.c
index 781ab41..66cedbd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,11 +1,13 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
#include "arg.h"
+#include "exec.h"
#include "file.h"
#include "process.h"
#include "types.h"
#include "util.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
#define INVERT_T "\x1b[7m"
#define UINVERT_T "\x1b[27m"
@@ -89,6 +91,7 @@ void run(FILE *fp, char *filename, bool tty) {
free(padding);
}
+ f.lines[i] = runextension(&f.lines[i], filename, i + 1);
fwrite(f.lines[i].buf, 1, f.lines[i].len, st);
fprintf(st, "\n");
linecount++;