summaryrefslogtreecommitdiff
path: root/src/global.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-06 03:31:41 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-06 03:31:41 -0400
commitcf6babcf9048f7e9205b1bead4794d8e2e9e8d62 (patch)
tree38688d2cd2433517045a67d2ef3f2777ee335050 /src/global.cc
parentecc5a1aab50d57e65b654ea9f8a84f17208ca612 (diff)
downloadfork-ledger-cf6babcf9048f7e9205b1bead4794d8e2e9e8d62.tar.gz
fork-ledger-cf6babcf9048f7e9205b1bead4794d8e2e9e8d62.tar.bz2
fork-ledger-cf6babcf9048f7e9205b1bead4794d8e2e9e8d62.zip
Restored all the option handlers from 2.6.2, but not the options themselves.
Diffstat (limited to 'src/global.cc')
-rw-r--r--src/global.cc192
1 files changed, 110 insertions, 82 deletions
diff --git a/src/global.cc b/src/global.cc
index 915713bc..36614c14 100644
--- a/src/global.cc
+++ b/src/global.cc
@@ -61,8 +61,8 @@ global_scope_t::global_scope_t(char ** envp)
// that such options are beginning, since options like -f cause a complete
// override of files found anywhere else.
session().now_at_command_line(false);
- read_environment_settings(report(), envp);
- session().read_init();
+ read_environment_settings(envp);
+ read_init();
}
global_scope_t::~global_scope_t()
@@ -76,12 +76,37 @@ global_scope_t::~global_scope_t()
IF_VERIFY() set_session_context(NULL);
}
+void global_scope_t::read_init()
+{
+ if (HANDLED(init_file_)) {
+ path init_file(HANDLER(init_file_).str());
+ if (exists(init_file)) {
+ TRACE_START(init, 1, "Read initialization file");
+
+ ifstream init(init_file);
+
+ journal_t temp;
+ if (session().read_journal(temp, init_file) > 0 ||
+ temp.auto_entries.size() > 0 || temp.period_entries.size() > 0) {
+ throw_(parse_error,
+ "Entries found in initialization file '" << init_file << "'");
+ }
+
+ TRACE_FINISH(init, 1);
+ }
+ }
+}
+
void global_scope_t::read_journal_files()
{
INFO_START(journal, "Read journal file");
+ string master_account;
+ if (report().HANDLED(account_))
+ master_account = report().HANDLER(account_).str();
+
std::size_t count = session().read_data(*session().create_journal(),
- report().account);
+ master_account);
if (count == 0)
throw_(parse_error, "Failed to locate any journal entries; "
"did you specify a valid file with -f?");
@@ -161,7 +186,7 @@ void global_scope_t::execute_command(strings_list args, bool at_repl)
// jww (2009-02-02): This is a complete hack, and a leftover from long,
// long ago. The question is, how best to remove its necessity...
- normalize_report_options(report(), verb);
+ normalize_report_options(verb);
}
// Create the output stream (it might be a file, the console or a PAGER
@@ -211,9 +236,6 @@ int global_scope_t::execute_command_wrapper(strings_list args, bool at_repl)
return status;
}
-namespace {
-}
-
expr_t::ptr_op_t global_scope_t::lookup(const string& name)
{
const char * p = name.c_str();
@@ -224,6 +246,9 @@ expr_t::ptr_op_t global_scope_t::lookup(const string& name)
case 'd':
OPT(debug_);
break;
+ case 'i':
+ OPT(init_file_);
+ break;
case 's':
OPT(script_);
break;
@@ -233,6 +258,7 @@ expr_t::ptr_op_t global_scope_t::lookup(const string& name)
case 'v':
OPT_(verbose);
else OPT(verify);
+ else OPT(version);
break;
}
}
@@ -254,69 +280,30 @@ expr_t::ptr_op_t global_scope_t::lookup(const string& name)
return expr_t::ptr_op_t();
}
-void handle_debug_options(int argc, char * argv[])
-{
- for (int i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- if (std::strcmp(argv[i], "--verify") == 0) {
-#if defined(VERIFY_ON)
- verify_enabled = true; // global in utils.h
-#endif
- }
- else if (std::strcmp(argv[i], "--verbose") == 0 ||
- std::strcmp(argv[i], "-v") == 0) {
-#if defined(LOGGING_ON)
- _log_level = LOG_INFO; // global in utils.h
-#endif
- }
- else if (i + 1 < argc && std::strcmp(argv[i], "--debug") == 0) {
-#if defined(DEBUG_ON)
- _log_level = LOG_DEBUG; // global in utils.h
- _log_category = argv[i + 1]; // global in utils.h
- i++;
-#endif
- }
- else if (i + 1 < argc && std::strcmp(argv[i], "--trace") == 0) {
-#if defined(TRACING_ON)
- _log_level = LOG_TRACE; // global in utils.h
- try {
- // global in utils.h
- _trace_level = boost::lexical_cast<int>(argv[i + 1]);
- }
- catch (const boost::bad_lexical_cast& e) {
- throw std::logic_error("Argument to --trace must be an integer");
- }
- i++;
-#endif
- }
- }
- }
-}
-
-void read_environment_settings(report_t& report, char * envp[])
+void global_scope_t::read_environment_settings(char * envp[])
{
TRACE_START(environment, 1, "Processed environment variables");
- process_environment(const_cast<const char **>(envp), "LEDGER_",
- report);
+ process_environment(const_cast<const char **>(envp), "LEDGER_", report());
#if 1
// These are here for backwards compatability, but are deprecated.
if (const char * p = std::getenv("LEDGER"))
- process_option("file", report, p, "LEDGER");
+ process_option("file", report(), p, "LEDGER");
if (const char * p = std::getenv("LEDGER_INIT"))
- process_option("init-file", report, p, "LEDGER_INIT");
+ process_option("init-file", report(), p, "LEDGER_INIT");
if (const char * p = std::getenv("PRICE_HIST"))
- process_option("price-db", report, p, "PRICE_HIST");
+ process_option("price-db", report(), p, "PRICE_HIST");
if (const char * p = std::getenv("PRICE_EXP"))
- process_option("price-exp", report, p, "PRICE_EXP");
+ process_option("price-exp", report(), p, "PRICE_EXP");
#endif
TRACE_FINISH(environment, 1);
}
-strings_list read_command_arguments(scope_t& scope, strings_list args)
+strings_list
+global_scope_t::read_command_arguments(scope_t& scope, strings_list args)
{
TRACE_START(arguments, 1, "Processed command-line arguments");
@@ -327,16 +314,17 @@ strings_list read_command_arguments(scope_t& scope, strings_list args)
return remaining;
}
-void normalize_session_options(session_t& session)
+void global_scope_t::normalize_session_options()
{
- INFO("Initialization file is " << session.init_file->string());
- INFO("Price database is " << session.price_db->string());
+ INFO("Initialization file is " << HANDLER(init_file_).str());
+ INFO("Price database is " << session().HANDLER(price_db_).str());
- foreach (const path& pathname, session.data_files)
+ foreach (const path& pathname, session().HANDLER(file_).data_files)
INFO("Journal file is " << pathname.string());
}
-function_t look_for_precommand(scope_t& scope, const string& verb)
+function_t global_scope_t::look_for_precommand(scope_t& scope,
+ const string& verb)
{
if (expr_t::ptr_op_t def = scope.lookup(string("precmd_") + verb))
return def->as_function();
@@ -344,7 +332,8 @@ function_t look_for_precommand(scope_t& scope, const string& verb)
return function_t();
}
-function_t look_for_command(scope_t& scope, const string& verb)
+function_t global_scope_t::look_for_command(scope_t& scope,
+ const string& verb)
{
if (expr_t::ptr_op_t def = scope.lookup(string("cmd_") + verb))
return def->as_function();
@@ -352,55 +341,94 @@ function_t look_for_command(scope_t& scope, const string& verb)
return function_t();
}
-void normalize_report_options(report_t& report, const string& verb)
+void global_scope_t::normalize_report_options(const string& verb)
{
// Patch up some of the reporting options based on what kind of
// command it was.
+ report_t& rep(report());
+
// jww (2008-08-14): This code really needs to be rationalized away
// for 3.0.
if (verb == "print" || verb == "entry" || verb == "dump") {
- report.HANDLER(related).on();
- report.HANDLER(related_all).on();
+ rep.HANDLER(related).on();
+ rep.HANDLER(related_all).on();
}
else if (verb == "equity") {
- report.HANDLER(subtotal).on();
+ rep.HANDLER(subtotal).on();
}
- else if (report.HANDLED(related)) {
+ else if (rep.HANDLED(related)) {
if (verb[0] == 'r') {
- report.HANDLER(invert).on();
+ rep.HANDLER(invert).on();
} else {
- report.HANDLER(subtotal).on();
- report.HANDLER(related_all).on();
+ rep.HANDLER(subtotal).on();
+ rep.HANDLER(related_all).on();
}
}
if (verb[0] != 'b' && verb[0] != 'r')
- report.what_to_keep.keep_base = true;
+ rep.HANDLER(base).on();
// Setup the default value for the display predicate
- if (report.display_predicate.empty()) {
+ if (! rep.HANDLED(display_)) {
if (verb[0] == 'b') {
- if (! report.HANDLED(empty))
- report.display_predicate = "total";
- if (! report.HANDLED(subtotal)) {
- if (! report.display_predicate.empty())
- report.display_predicate += "&";
- report.display_predicate += "depth<=1";
- }
+ if (! rep.HANDLED(empty))
+ rep.append_display_predicate("total");
+ if (! rep.HANDLED(subtotal))
+ rep.append_display_predicate("depth<=1");
}
else if (verb == "equity") {
- report.display_predicate = "amount_expr"; // jww (2008-08-14): ???
+ // jww (2008-08-14): ???
+ rep.append_display_predicate("amount_expr");
}
- else if (verb[0] == 'r' && ! report.HANDLED(empty)) {
- report.display_predicate = "amount";
+ else if (verb[0] == 'r' && ! rep.HANDLED(empty)) {
+ rep.append_display_predicate("amount");
}
}
- if (! report.report_period.empty() && ! report.HANDLED(sort_all_))
- report.HANDLER(sort_entries_).on();
+ if (rep.HANDLED(period_) && ! rep.HANDLED(sort_all_))
+ rep.HANDLER(sort_entries_).on();
+}
+
+void handle_debug_options(int argc, char * argv[])
+{
+ for (int i = 1; i < argc; i++) {
+ if (argv[i][0] == '-') {
+ if (std::strcmp(argv[i], "--verify") == 0) {
+#if defined(VERIFY_ON)
+ verify_enabled = true; // global in utils.h
+#endif
+ }
+ else if (std::strcmp(argv[i], "--verbose") == 0 ||
+ std::strcmp(argv[i], "-v") == 0) {
+#if defined(LOGGING_ON)
+ _log_level = LOG_INFO; // global in utils.h
+#endif
+ }
+ else if (i + 1 < argc && std::strcmp(argv[i], "--debug") == 0) {
+#if defined(DEBUG_ON)
+ _log_level = LOG_DEBUG; // global in utils.h
+ _log_category = argv[i + 1]; // global in utils.h
+ i++;
+#endif
+ }
+ else if (i + 1 < argc && std::strcmp(argv[i], "--trace") == 0) {
+#if defined(TRACING_ON)
+ _log_level = LOG_TRACE; // global in utils.h
+ try {
+ // global in utils.h
+ _trace_level = boost::lexical_cast<int>(argv[i + 1]);
+ }
+ catch (const boost::bad_lexical_cast& e) {
+ throw std::logic_error("Argument to --trace must be an integer");
+ }
+ i++;
+#endif
+ }
+ }
+ }
}
} // namespace ledger