6 #include <readline/readline.h>
7 #include <readline/history.h>
8 #include <readline/tilde.h>
10 #include <editline/readline.h>
13 int history_loaded
= 0;
15 char HISTORY_FILE
[] = "~/.mal-history";
18 if (history_loaded
) { return; }
20 char *hf
= tilde_expand(HISTORY_FILE
);
21 if (access(hf
, F_OK
) != -1) {
22 // TODO: check if file exists first, use non-static path
24 ret
= read_history(hf
);
26 FILE *fp
= fopen(hf
, "r");
27 char *line
= malloc(80); // getline reallocs as necessary
29 while ((ret
= getline(&line
, &sz
, fp
)) > 0) {
30 add_history(line
); // Add line to in-memory history
40 void append_to_history() {
41 char *hf
= tilde_expand(HISTORY_FILE
);
43 append_history(1, hf
);
45 #if defined(RL_READLINE_VERSION)
46 HIST_ENTRY
*he
= history_get(history_base
+history_length
-1);
48 // libedit-2 segfaults if we add history_base
49 HIST_ENTRY
*he
= history_get(history_length
-1);
51 FILE *fp
= fopen(hf
, "a");
53 fprintf(fp
, "%s\n", he
->line
);
61 // line must be freed by caller
62 char *_readline (char prompt
[]) {
67 line
= readline(prompt
);
68 if (!line
) return NULL
; // EOF
69 add_history(line
); // Add input to in-memory history
71 append_to_history(); // Flush new line of history to disk