summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-13 05:24:28 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-13 05:24:28 -0400
commit70344b82e70f454462f31daec05d79554804e8f8 (patch)
treed9f3252972cd3b83453ea0d5dfee932f99c4a793 /src
parent037dd0f716e82568b005a78ee4d79fce0b886af3 (diff)
downloadfork-ledger-70344b82e70f454462f31daec05d79554804e8f8.tar.gz
fork-ledger-70344b82e70f454462f31daec05d79554804e8f8.tar.bz2
fork-ledger-70344b82e70f454462f31daec05d79554804e8f8.zip
Added a "reload" command, for use at the REPL
Created a new function, session_t::reread_journal_files, which throws away all previous state data and reads in the same files again. This is needed to allow Emacs to communicate with Ledger via the REPL, so that it tell Ledger when it has made changes to the user's data file.
Diffstat (limited to 'src')
-rw-r--r--src/global.cc20
-rw-r--r--src/global.h1
-rw-r--r--src/main.cc4
-rw-r--r--src/report.cc2
-rw-r--r--src/report.h5
-rw-r--r--src/session.cc96
-rw-r--r--src/session.h3
7 files changed, 76 insertions, 55 deletions
diff --git a/src/global.cc b/src/global.cc
index c012b17d..b2dc24fc 100644
--- a/src/global.cc
+++ b/src/global.cc
@@ -109,24 +109,6 @@ void global_scope_t::read_init()
}
}
-void global_scope_t::read_journal_files()
-{
- INFO_START(journal, "Read journal file");
-
- string master_account;
- if (session().HANDLED(account_))
- master_account = session().HANDLER(account_).str();
-
- std::size_t count = session().read_data(master_account);
- if (count == 0)
- throw_(parse_error, "Failed to locate any journal entries; "
- "did you specify a valid file with -f?");
-
- INFO_FINISH(journal);
-
- INFO("Found " << count << " entries");
-}
-
char * global_scope_t::prompt_string()
{
static char prompt[32];
@@ -195,7 +177,7 @@ void global_scope_t::execute_command(strings_list args, bool at_repl)
if (! is_precommand) {
if (! at_repl)
- read_journal_files();
+ session().read_journal_files();
normalize_report_options(verb);
}
diff --git a/src/global.h b/src/global.h
index 0851cdaa..883972fc 100644
--- a/src/global.h
+++ b/src/global.h
@@ -52,7 +52,6 @@ public:
~global_scope_t();
void read_init();
- void read_journal_files();
void read_environment_settings(char * envp[]);
strings_list read_command_arguments(scope_t& scope, strings_list args);
void normalize_session_options();
diff --git a/src/main.cc b/src/main.cc
index 30a54b3b..74ef9c64 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -92,7 +92,7 @@ int main(int argc, char * argv[], char * envp[])
if (global_scope->HANDLED(script_)) {
// Ledger is being invoked as a script command interpreter
- global_scope->read_journal_files();
+ global_scope->session().read_journal_files();
status = 0;
@@ -115,7 +115,7 @@ int main(int argc, char * argv[], char * envp[])
// Commence the REPL by displaying the current Ledger version
global_scope->show_version_info(std::cout);
- global_scope->read_journal_files();
+ global_scope->session().read_journal_files();
bool exit_loop = false;
diff --git a/src/report.cc b/src/report.cc
index a4076d9f..335974d2 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -556,6 +556,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return WRAP_FUNCTOR
(reporter<>(new format_xacts(*this, HANDLER(register_format_).str()),
*this));
+ else if (is_eq(p, "reload"))
+ return MAKE_FUNCTOR(report_t::reload_command);
break;
case 's':
diff --git a/src/report.h b/src/report.h
index fd268c91..9ea99bbd 100644
--- a/src/report.h
+++ b/src/report.h
@@ -143,6 +143,11 @@ public:
return value_t(static_cast<scope_t *>(this));
}
+ value_t reload_command(call_scope_t& scope) {
+ session.reread_journal_files();
+ return true;
+ }
+
void append_predicate(const string& str) {
if (HANDLED(limit_))
HANDLER(limit_).on(string("(") + HANDLER(limit_).str() + ")&" + str);
diff --git a/src/session.cc b/src/session.cc
index 99b0d013..52c84acb 100644
--- a/src/session.cc
+++ b/src/session.cc
@@ -60,7 +60,7 @@ session_t::session_t()
current_year(CURRENT_DATE().year()),
commodity_pool(new commodity_pool_t),
- master(new account_t(NULL, "")),
+ master(new account_t),
journal(new journal_t(master.get()))
{
TRACE_CTOR(session_t, "");
@@ -106,42 +106,39 @@ std::size_t session_t::read_data(const string& master_account)
std::size_t entry_count = 0;
- if (entry_count == 0) {
- account_t * acct = journal->master;
- if (! master_account.empty())
- acct = journal->find_account(master_account);
+ account_t * acct = journal->master;
+ if (! master_account.empty())
+ acct = journal->find_account(master_account);
- if (HANDLED(price_db_) && exists(path(HANDLER(price_db_).str()))) {
- if (read_journal(HANDLER(price_db_).str()))
- throw_(parse_error, "Entries not allowed in price history file");
- }
-
-
- foreach (const path& pathname, HANDLER(file_).data_files) {
- if (pathname == "-") {
- // To avoid problems with stdin and pipes, etc., we read the entire
- // file in beforehand into a memory buffer, and then parcel it out
- // from there.
- std::ostringstream buffer;
+ if (HANDLED(price_db_) && exists(path(HANDLER(price_db_).str()))) {
+ if (read_journal(HANDLER(price_db_).str()))
+ throw_(parse_error, "Entries not allowed in price history file");
+ }
- while (std::cin.good() && ! std::cin.eof()) {
- char line[8192];
- std::cin.read(line, 8192);
- std::streamsize count = std::cin.gcount();
- buffer.write(line, count);
- }
- buffer.flush();
+ foreach (const path& pathname, HANDLER(file_).data_files) {
+ if (pathname == "-") {
+ // To avoid problems with stdin and pipes, etc., we read the entire
+ // file in beforehand into a memory buffer, and then parcel it out
+ // from there.
+ std::ostringstream buffer;
+
+ while (std::cin.good() && ! std::cin.eof()) {
+ char line[8192];
+ std::cin.read(line, 8192);
+ std::streamsize count = std::cin.gcount();
+ buffer.write(line, count);
+ }
+ buffer.flush();
- std::istringstream buf_in(buffer.str());
+ std::istringstream buf_in(buffer.str());
- entry_count += read_journal(buf_in, "/dev/stdin", acct);
- }
- else if (exists(pathname)) {
- entry_count += read_journal(pathname, acct);
- }
- else {
- throw_(parse_error, "Could not open journal file '" << pathname << "'");
- }
+ entry_count += read_journal(buf_in, "/dev/stdin", acct);
+ }
+ else if (exists(pathname)) {
+ entry_count += read_journal(pathname, acct);
+ }
+ else {
+ throw_(parse_error, "Could not open journal file '" << pathname << "'");
}
}
@@ -150,6 +147,39 @@ std::size_t session_t::read_data(const string& master_account)
return entry_count;
}
+void session_t::read_journal_files()
+{
+ INFO_START(journal, "Read journal file");
+
+ string master_account;
+ if (HANDLED(account_))
+ master_account = HANDLER(account_).str();
+
+ std::size_t count = read_data(master_account);
+ if (count == 0)
+ throw_(parse_error, "Failed to locate any journal entries; "
+ "did you specify a valid file with -f?");
+
+ INFO_FINISH(journal);
+
+ INFO("Found " << count << " entries");
+}
+
+void session_t::reread_journal_files()
+{
+ journal.reset();
+ master.reset();
+ commodity_pool.reset();
+ amount_t::shutdown();
+
+ commodity_pool.reset(new commodity_pool_t);
+ amount_t::initialize(commodity_pool);
+ master.reset(new account_t);
+ journal.reset(new journal_t(master.get()));
+
+ read_journal_files();
+}
+
void session_t::clean_xacts()
{
journal_xacts_iterator walker(*journal.get());
diff --git a/src/session.h b/src/session.h
index 6ee7af9d..f4937f10 100644
--- a/src/session.h
+++ b/src/session.h
@@ -87,6 +87,9 @@ public:
std::size_t read_data(const string& master_account = "");
+ void read_journal_files();
+ void reread_journal_files();
+
void clean_xacts();
void clean_xacts(entry_t& entry);
void clean_accounts();