6 #include <readline/readline.h>
7 #include <readline/history.h>
8 #include <readline/tilde.h>
10 #include <editline/readline.h>
11 #include <editline/history.h>
14 int history_loaded
= 0;
16 char HISTORY_FILE
[] = "~/.mal-history";
19 if (history_loaded
) { return 0; }
21 char *hf
= tilde_expand(HISTORY_FILE
);
22 if (access(hf
, F_OK
) != -1) {
23 // TODO: check if file exists first, use non-static path
25 ret
= read_history(hf
);
27 FILE *fp
= fopen(hf
, "r");
28 char *line
= malloc(80); // getline reallocs as necessary
30 while ((ret
= getline(&line
, &sz
, fp
)) > 0) {
31 add_history(line
); // Add line to in-memory history
41 int append_to_history() {
42 char *hf
= tilde_expand(HISTORY_FILE
);
44 append_history(1, hf
);
46 HIST_ENTRY
*he
= history_get(history_length
-1);
47 FILE *fp
= fopen(hf
, "a");
49 fprintf(fp
, "%s\n", he
->line
);
57 // line must be freed by caller
58 char *_readline (char prompt
[]) {
63 line
= readline(prompt
);
64 if (!line
) return NULL
; // EOF
65 add_history(line
); // Add input to in-memory history
67 append_to_history(); // Flush new line of history to disk