summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorWormHeamer2025-12-31 00:22:12 -0500
committerWormHeamer2025-12-31 00:22:12 -0500
commit801c4bd5a2ea6b3c27836e6d2e8cf8386560e6db (patch)
treed6a37d9c2fcfcff2a5c14bd660ac7d9224e52e51 /main.c
parentb4edb16e7611f90cd541ce470f2e16532de3aee5 (diff)
run and build files
Diffstat (limited to 'main.c')
-rw-r--r--main.c104
1 files changed, 98 insertions, 6 deletions
diff --git a/main.c b/main.c
index ed892b2..2ff1260 100644
--- a/main.c
+++ b/main.c
@@ -13,12 +13,14 @@
#define ARENA_IMPL
#define UTF8_IMPL
+#define STR_IMPL
#include "wrmr.h"
#include "arena.h"
#include "txt.h"
#include "vui.h"
#include "utf8.h"
+#include "str.h"
Arena scratch = { 0 };
Txt txt = { 0 };
@@ -198,6 +200,17 @@ int shell_replace(TxtLoc start, TxtLoc end, const char *cmd) {
return 0;
}
+int shell_run(const char *cmd) {
+ vui_disable();
+ int r = system(cmd);
+ printf("[Press any key to resume editing]");
+ fflush(stdout);
+ vui_enable();
+ vui_key();
+ vui_redraw();
+ return r;
+}
+
/* main */
#define ODD_ATTR (FG_CYAN | BG_BLACK)
@@ -415,16 +428,82 @@ TxtLoc ins_newline(TxtLoc l) {
return l;
}
+static Str normalize_path(Str s) {
+ char pwd[8192];
+ if (s.n > 0 && s.s[0] != '/') {
+ if (!getcwd(pwd, sizeof(pwd))) {
+ return (Str) { 0, 0 };
+ }
+ Str d = str_from_cstr(pwd);
+ str_catc(&d, '/', &scratch);
+ str_cat(&d, s, &scratch);
+ s = d;
+ }
+ DYNARR(Str) bits = { 0 };
+ while (s.n > 0) {
+ Cut c = str_cut(s, '/');
+ if (str_eql(c.head, S(".."))) {
+ if (bits.n > 0) bits.n--;
+ } else if (str_eql(c.head, S("."))) {
+ /* do nothing */
+ } else if (c.head.n > 0) {
+ DA_APUSH(&bits, &scratch, c.head);
+ }
+ s = c.tail;
+ }
+ Str path = { 0 };
+ for (u32 i = 0; i < bits.n; i++) {
+ str_catc(&path, '/', &scratch);
+ str_cat(&path, bits.v[i], &scratch);
+ }
+ return path;
+}
+
+static void build_file(Str path) {
+ (void)path;
+ shell_run("make");
+}
+
+#define WED_DEV_DIR "/mnt/user/wrmr/src/wed/"
+#define WED_DEV_EXE (WED_DEV_DIR "wed")
+
+static void run_file(Str path) {
+ path = normalize_path(path);
+ if (str_starts(path, S(WED_DEV_DIR))) {
+ vui_fini();
+ system("make");
+ char ofs[32];
+ sprintf(ofs, "-b%d", txt_ofs(cur));
+ execl(WED_DEV_EXE, WED_DEV_EXE, ofs, str_to_cstr(path, &scratch), 0);
+ } else {
+ shell_run("make run");
+ }
+}
+
int main(int argc, const char **argv) {
+ /* TODO: Have a command-line argument to start editing at the given byte offset,
+ * e.g. wed -b 1027 */
+
scratch = arena_init(128L << 20);
+
+ int start_ofs = -1;
+ if (argc > 1 && !strncmp(argv[1], "-b", 2)) {
+ start_ofs = atoi(argv[1] + 2);
+ if (argc > 2) {
+ MOVE(&argv[1], &argv[2], argc - 2);
+ }
+ argc--;
+ }
+
+ const char *path = "test.txt";
+ if (argc > 1) path = argv[1];
+
vui_init();
vui_curs_vis(1);
vui_redraw_fn(draw);
- const char *path = "test.txt";
- //const char *path = "/usr/share/dict/words";
- if (argc > 1) path = argv[1];
+
if (txt_load(&txt, path)) err(1, "couldn't open file");
- cur = txt_end(&txt);
+ cur = start_ofs < 0 ? txt_end(&txt) : txt_at(&txt, start_ofs);
//srand(clock());
srand(123);
@@ -514,7 +593,20 @@ int main(int argc, const char **argv) {
if (shell_replace(start, end, "fmt -w80 -u")) {
err(1, "shell_replace");
}
- };
+ } break;
+ case ' ':
+ switch ((u32)vui_key()) {
+ case 'm':
+ build_file(str_from_cstr(path));
+ break;
+ case 'r':
+ run_file(str_from_cstr(path));
+ break;
+ default:
+ /* TODO: flash */
+ break;
+ }
+ break;
default:
motion(&cur, c);
break;
@@ -538,7 +630,7 @@ int main(int argc, const char **argv) {
cur = txt_delete_range(cur, next_word(cur));
break;
case 0x0c /* ^L */:
- draw(NULL);
+ vui_redraw();
break;
case '\r':
cur = ins_newline(cur);