about summary refs log tree commit diff stats
path: root/src/lua.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-11 16:16:55 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-11 16:16:55 -0800
commit7b389ce41efa4f28687d418a84063907bc66380a (patch)
treef139305bc3c17312701e26e016a785b030a23fb0 /src/lua.c
parent2d05f5090ff14652e4abe3b02e3eb382356f98b0 (diff)
downloadteliva-7b389ce41efa4f28687d418a84063907bc66380a.tar.gz
extract a few helper functions
Diffstat (limited to 'src/lua.c')
-rw-r--r--src/lua.c47
1 files changed, 26 insertions, 21 deletions
diff --git a/src/lua.c b/src/lua.c
index 8c9a8e5..afee3a1 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -316,32 +316,31 @@ void teliva_get_definition(lua_State *L, const char *name) {
 }
 
 
-extern void edit(char *filename, const char *status);
-void editString(lua_State *L, char *name) {
-    /* write given definition out to tmp file */
-//?     stackDump(L);
+void write_definition_to_file(lua_State *L, char *name, char *outfilename) {
     teliva_get_definition(L, name);
-//?     stackDump(L);
     const char *contents = lua_tostring(L, -1);
     lua_pop(L, 1);
-    int outfd = open("teliva_editbuffer", O_WRONLY|O_CREAT|O_TRUNC, 0644);
+    int outfd = open(outfilename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
     write(outfd, contents, strlen(contents));
     close(outfd);
+}
 
-    /* edit tmp file */
-    edit("teliva_editbuffer", "");
 
-    /* read contents of tmp file */
-    char new_contents[8192] = {0};
-    int infd = open("teliva_editbuffer", O_RDONLY);
-    read(infd, new_contents, 8190);  /* TODO: handle overly large file */
+void read_contents(lua_State *L, char *filename, char *out) {
+    int infd = open(filename, O_RDONLY);
+    read(infd, out, 8190);  /* TODO: handle overly large file */
     close(infd);
+}
+
 
-    /* save contents back into image */
-    lua_pushstring(L, new_contents);
+/* table to update is at top of stack */
+void update_definition(lua_State *L, char *name, char *out) {
+    lua_pushstring(L, out);
     lua_setfield(L, -2, name);
+}
+
 
-    /* save teliva_program to disk */
+void save_image(lua_State *L) {
     int table = lua_gettop(L);
     FILE* fp = fopen(Image_name, "w");
     fprintf(fp, "teliva_program = {\n");
@@ -354,22 +353,28 @@ void editString(lua_State *L, char *name) {
     }
     fprintf(fp, "}\n");
     fclose(fp);
-
-    /* reload binding */
-    dostring(L, new_contents, name);
-    /* TODO: handle error */
 }
 
 
 /* death and rebirth */
 char *Script_name = NULL;
 char **Argv = NULL;
+extern void edit(char *filename, const char *status);
 void switch_to_editor(lua_State *L, const char *message) {
   endwin();
   if (Script_name)
     edit(Script_name, message);
-  else
-    editString(L, "main");
+  else {
+    write_definition_to_file(L, "main", "teliva_editbuffer");
+    edit("teliva_editbuffer", "");
+    char new_contents[8192] = {0};
+    read_contents(L, "teliva_editbuffer", new_contents);
+    update_definition(L, "main", new_contents);
+    save_image(L);
+    /* reload binding */
+    dostring(L, new_contents, "main");
+    /* TODO: handle error */
+  }
   execv(Argv[0], Argv);
   /* never returns */
 }