mirror of
https://github.com/louisrubet/rpn
synced 2024-12-28 09:58:52 +01:00
#157: history file stored in ~/.rpn_history
This commit is contained in:
parent
0fed76efcb
commit
ee8d007c71
2 changed files with 52 additions and 1 deletions
|
@ -69,4 +69,8 @@ typedef enum {
|
||||||
//
|
//
|
||||||
#define CMD_TYPE_STRINGS { "undef", "number", "complex", "string", "symbol", "program", "keyword", "keyword" }
|
#define CMD_TYPE_STRINGS { "undef", "number", "complex", "string", "symbol", "program", "keyword", "keyword" }
|
||||||
|
|
||||||
|
// history
|
||||||
|
#define HISTORY_FILE ".rpn_history"
|
||||||
|
#define HISTORY_FILE_MAX_LINES (100)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
49
src/main.cpp
49
src/main.cpp
|
@ -2,6 +2,11 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <linux/limits.h>
|
||||||
|
|
||||||
// internal includes
|
// internal includes
|
||||||
#include "program.hpp"
|
#include "program.hpp"
|
||||||
|
@ -10,6 +15,40 @@ static heap s_global_heap;
|
||||||
static stack s_global_stack;
|
static stack s_global_stack;
|
||||||
static program* s_prog_to_interrupt = NULL;
|
static program* s_prog_to_interrupt = NULL;
|
||||||
|
|
||||||
|
// actions to be done at rpn exit
|
||||||
|
void exit_interactive_rpn()
|
||||||
|
{
|
||||||
|
struct passwd* pw = getpwuid(getuid());
|
||||||
|
if (pw != NULL)
|
||||||
|
{
|
||||||
|
char history_path[PATH_MAX];
|
||||||
|
sprintf(history_path, "%s/%s", pw->pw_dir, HISTORY_FILE);
|
||||||
|
|
||||||
|
// trunc current history
|
||||||
|
ofstream history(history_path, ios_base::out|ios_base::trunc);
|
||||||
|
history.close();
|
||||||
|
|
||||||
|
// save it
|
||||||
|
if (linenoiseHistorySave(history_path) != 0)
|
||||||
|
fprintf(stderr, "warning, could not save %s (errno=%d, '%s')\n", history_path, errno, strerror(errno));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// actions to be done at rpn exit
|
||||||
|
void init_interactive_rpn()
|
||||||
|
{
|
||||||
|
struct passwd* pw = getpwuid(getuid());
|
||||||
|
if (pw != NULL)
|
||||||
|
{
|
||||||
|
char history_path[PATH_MAX];
|
||||||
|
sprintf(history_path, "%s/%s", pw->pw_dir, HISTORY_FILE);
|
||||||
|
|
||||||
|
// don't care about errors
|
||||||
|
linenoiseHistorySetMaxLen(HISTORY_FILE_MAX_LINES);
|
||||||
|
linenoiseHistoryLoad(history_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// handle CtrlC signal
|
// handle CtrlC signal
|
||||||
static void ctrlc_handler(int sig, siginfo_t* siginfo, void* context)
|
static void ctrlc_handler(int sig, siginfo_t* siginfo, void* context)
|
||||||
{
|
{
|
||||||
|
@ -18,6 +57,8 @@ static void ctrlc_handler(int sig, siginfo_t* siginfo, void* context)
|
||||||
s_prog_to_interrupt->stop();
|
s_prog_to_interrupt->stop();
|
||||||
s_prog_to_interrupt = NULL;
|
s_prog_to_interrupt = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exit_interactive_rpn();
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle SIGSEGV signal
|
// handle SIGSEGV signal
|
||||||
|
@ -54,7 +95,10 @@ int main(int argc, char* argv[])
|
||||||
// run with interactive prompt
|
// run with interactive prompt
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
{
|
{
|
||||||
//
|
// init history
|
||||||
|
init_interactive_rpn();
|
||||||
|
|
||||||
|
// entry loop
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
// make program from interactive entry
|
// make program from interactive entry
|
||||||
|
@ -73,6 +117,9 @@ int main(int argc, char* argv[])
|
||||||
program::show_stack(s_global_stack);
|
program::show_stack(s_global_stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// manage history and exit
|
||||||
|
exit_interactive_rpn();
|
||||||
}
|
}
|
||||||
// run with cmd line arguments
|
// run with cmd line arguments
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in a new issue