about summary refs log tree commit diff stats
path: root/src/extern/runproc.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-09 01:44:58 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-09 01:52:24 +0200
commit11219c7d21c8ec7f438d52de063c4ac25f84711d (patch)
tree36b1308d747cc467dc7dad2713eb2cc546390026 /src/extern/runproc.nim
parent6c435aa6c186c2fe7ec72cee2a5ae2f1ae8b22a7 (diff)
downloadchawan-11219c7d21c8ec7f438d52de063c4ac25f84711d.tar.gz
add extern, refactor some term functions
* Add an extern() call. Maybe it should be defined on client. It
  certainly should accept a dictionary instead of the enum type we use
  now. Perhaps it should return the error code?
  I'll leave it undocumented until I figure this out.
* Refactor enableRawMode, unblockStdin, etc. so that they operate on
  the term object instead of global state.
* Move editor to a separate folder, and factor out runprocess into
  a different module.
Diffstat (limited to 'src/extern/runproc.nim')
-rw-r--r--src/extern/runproc.nim29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/extern/runproc.nim b/src/extern/runproc.nim
new file mode 100644
index 00000000..c49d98db
--- /dev/null
+++ b/src/extern/runproc.nim
@@ -0,0 +1,29 @@
+import posix
+
+import display/term
+
+proc c_system(cmd: cstring): cint {.
+  importc: "system", header: "<stdlib.h>".}
+
+# Run process (without suspending the terminal controller).
+proc runProcess*(cmd: string): bool =
+  let wstatus = c_system(cstring(cmd))
+  if wstatus == -1:
+    result = false
+  else:
+    result = WIFEXITED(wstatus) and WEXITSTATUS(wstatus) == 0
+    if not result:
+      # Hack.
+      #TODO this is a very bad idea, e.g. say the editor is writing into the
+      # file, then receives SIGINT, now the file is corrupted but Chawan will
+      # happily read it as if nothing happened.
+      # We should find a proper solution for this.
+      result = WIFSIGNALED(wstatus) and WTERMSIG(wstatus) == SIGINT
+
+# Run process (and suspend the terminal controller).
+proc runProcess*(term: Terminal, cmd: string, wait = false): bool =
+  term.quit()
+  result = runProcess(cmd)
+  if wait:
+    term.anyKey()
+  term.restart()