1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 18:48:12 +03:00
mal/c/readline.c
Joel Martin c9de2e82ed Tests: add testing Dockerfile. Impl fixes.
- tests/docker/Dockerfile: specifies full docker image containing all
  tools/languages (except matlab).
- tests/docker-build.sh: build above image.
- tests/docker-run.sh: launch above image.
    Example: ./tests/docker-run.sh make test^js^step2
- Various fixes across multiple languages:
    - Unicode fixes for bash and R on Ubuntu Utopic
    - readline history fixes for when ~/.mal-history is not available
      or readable/writable. No fatal errors.
    - fixes to work with perl 5.20 (and still perl 5.18)
2015-03-11 22:22:35 -05:00

72 lines
1.5 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#if USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#include <readline/tilde.h>
#else
#include <editline/readline.h>
#include <editline/history.h>
#endif
int history_loaded = 0;
char HISTORY_FILE[] = "~/.mal-history";
int load_history() {
if (history_loaded) { return 0; }
int ret;
char *hf = tilde_expand(HISTORY_FILE);
if (access(hf, F_OK) != -1) {
// TODO: check if file exists first, use non-static path
#if USE_READLINE
ret = read_history(hf);
#else
FILE *fp = fopen(hf, "r");
char *line = malloc(80); // getline reallocs as necessary
size_t sz = 80;
while ((ret = getline(&line, &sz, fp)) > 0) {
add_history(line); // Add line to in-memory history
}
free(line);
fclose(fp);
#endif
history_loaded = 1;
}
free(hf);
}
int append_to_history() {
char *hf = tilde_expand(HISTORY_FILE);
#ifdef USE_READLINE
append_history(1, hf);
#else
HIST_ENTRY *he = history_get(history_length-1);
FILE *fp = fopen(hf, "a");
if (fp) {
fprintf(fp, "%s\n", he->line);
fclose(fp);
}
#endif
free(hf);
}
// line must be freed by caller
char *_readline (char prompt[]) {
char *line;
load_history();
line = readline(prompt);
if (!line) return NULL; // EOF
add_history(line); // Add input to in-memory history
append_to_history(); // Flush new line of history to disk
return line;
}