Changeset: 5130afcf32a8 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=5130afcf32a8
Modified Files:
        clients/mapiclient/ReadlineTools.c
Branch: Oct2020
Log Message:

mclient/EDITOR integration should handle more than 1024 bytes


diffs (99 lines):

diff --git a/clients/mapiclient/ReadlineTools.c 
b/clients/mapiclient/ReadlineTools.c
--- a/clients/mapiclient/ReadlineTools.c
+++ b/clients/mapiclient/ReadlineTools.c
@@ -291,10 +291,6 @@ continue_completion(rl_completion_func_t
        rl_attempted_completion_function = func;
 }
 
-#ifndef BUFSIZ
-#define BUFSIZ 1024
-#endif
-
 static void
 readline_show_error(const char *msg) {
        rl_save_prompt();
@@ -303,13 +299,19 @@ readline_show_error(const char *msg) {
        rl_clear_message();
 }
 
+#ifndef BUFFER_SIZE
+#define BUFFER_SIZE 1024
+#endif
+
 static int
 invoke_editor(int cnt, int key) {
        char template[] = "/tmp/mclient_temp_XXXXXX";
-       char cmd[BUFSIZ];
-       char *editor;
+       char editor_command[BUFFER_SIZE];
+       char *read_buff = NULL;
+       char *editor = NULL;
        FILE *fp;
-       size_t cmd_len;
+       size_t content_len;
+       size_t read_bytes;
 
        (void) cnt;
        (void) key;
@@ -324,35 +326,50 @@ invoke_editor(int cnt, int key) {
        fflush(fp);
 
        editor = getenv("VISUAL");
-       if (!editor) {
+       if (editor == NULL) {
                editor = getenv("EDITOR");
-               if (!editor) {
+               if (editor == NULL) {
                        readline_show_error("invoke_editor: EDITOR/VISUAL env 
variable not set\n");
                        goto bailout;
                }
        }
 
-       snprintf(cmd, BUFSIZ, "%s %s", editor, template);
-       if (system(cmd) != 0) {
+       snprintf(editor_command, BUFFER_SIZE, "%s %s", editor, template);
+       if (system(editor_command) != 0) {
                readline_show_error("invoke_editor: Starting editor failed\n");
                goto bailout;
        }
 
-       fseek(fp, 0, SEEK_SET);
-       cmd_len = fread(cmd, sizeof(char), BUFSIZ, fp);
-       fclose(fp);
+       fseek(fp, 0L, SEEK_END);
+       content_len = ftell(fp);
+       rewind(fp);
+
+       read_buff = (char *)malloc(content_len*sizeof(char));
+       if (read_buff == NULL) {
+               readline_show_error("invoke_editor: Cannot allocate memory\n");
+               goto bailout;
+       }
 
-       *(cmd + cmd_len) = 0;
+       read_bytes = fread(read_buff, sizeof(char), content_len, fp);
+       fclose(fp);
+       unlink(template);
+       if (read_bytes != content_len) {
+               readline_show_error("invoke_editor: Did not read from file 
correctly\n");
+               goto bailout;
+       }
 
-       rl_replace_line(cmd, 0);
-       rl_point = cmd_len - 1;
+       *(read_buff + read_bytes - 1) = 0;
+       rl_replace_line(read_buff, 0);
+       rl_point = read_bytes;
 
-       unlink(template);
+       free(read_buff);
 
        return 0;
 
 bailout:
        fclose(fp);
+       free(read_buff);
+       unlink(template);
        return 1;
 }
 
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to