Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read and write history from/to a file #160

Merged
merged 1 commit into from Jul 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions mod_notionflux/notionflux/notionflux.c
Expand Up @@ -53,6 +53,7 @@
#define SOCK_ATOM "_NOTION_MOD_NOTIONFLUX_SOCKET"

char const *sockfilename = NULL;
char const *histfile = NULL;

char const *get_socket_filename(void)
{
Expand Down Expand Up @@ -301,6 +302,33 @@ int initialize_readline(void)
return 0;
}

void read_histfile(void)
{
char *home = getenv("HOME");
if (!home) {
fputs("$HOME is not set, can't find history file.\n", stderr);
return;
}

char fname[4096];
snprintf(fname, sizeof(fname), "%s/.notion/notionflux.hist", home);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not fully .history?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always found long file extensions weird, but if that's your only gripe feel free to change it :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

histfile = strdup(fname);

int err = read_history(fname);
if (err != 0 && err != ENOENT)
fprintf(stderr, "Error reading history file: %s.\n", strerror(err));
}

void save_histfile(void)
{
if (histfile && strlen(histfile) > 0) {
int err = write_history(histfile);
if (err != 0)
fprintf(stderr, "Error writing history: %s.\n", strerror(err));
}
}


struct buf input = {MAX_DATA, input.buf, {0}};

#if 1 /* Ideas taken from lua/lua.c, because the REPL isn't part of liblua */
Expand Down Expand Up @@ -359,6 +387,7 @@ int repl_main(void)
rl_startup_hook = initialize_readline;
input_reset();

read_histfile();
while (1) {
free(line);
line = readline(prompt);
Expand Down Expand Up @@ -449,6 +478,7 @@ int main(int argc, char **argv)
char const *data;

if (argc == 1 && isatty(STDIN_FILENO)) {
atexit(save_histfile);
return repl_main();
} else if (argc == 1 || (argc == 2 && strcmp(argv[1], "-R") == 0)) {
data = buf.buf;
Expand Down