summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-05 04:20:49 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-05 04:20:49 -0400
commitedb9cff4a2c803bde01f75221aed0b7ecfc3b3bf (patch)
treeedda6471ea8bd3f4d8b3d211dba1642c607d88ea /src/main.cc
parent87cfef221a189deebc79a606800585f75ab05395 (diff)
downloadfork-ledger-edb9cff4a2c803bde01f75221aed0b7ecfc3b3bf.tar.gz
fork-ledger-edb9cff4a2c803bde01f75221aed0b7ecfc3b3bf.tar.bz2
fork-ledger-edb9cff4a2c803bde01f75221aed0b7ecfc3b3bf.zip
Moved work.cc and global_scope_t into a new global.cc file.
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc274
1 files changed, 1 insertions, 273 deletions
diff --git a/src/main.cc b/src/main.cc
index 061d6b2d..c8192082 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -31,283 +31,11 @@
#include <ledger.h> // Read this file for a top-level overview
-#include "work.h" // This is where the meat of main() is, which
+#include "global.h" // This is where the meat of main() is, which
// was moved there for the sake of clarity
using namespace ledger;
namespace {
- class global_scope_t : public noncopyable, public scope_t
- {
- scoped_ptr<session_t> session_ptr;
- ptr_list<report_t> report_stack;
-
- public:
- path script_file;
-
- global_scope_t(char ** envp)
- {
- TRACE_CTOR(global_scope_t, "");
-
- session_ptr.reset(new LEDGER_SESSION_T);
-
- set_session_context(session_ptr.get());
-
- // Create the report object, which maintains state relating to each
- // command invocation. Because we're running from main(), the
- // distinction between session and report doesn't really matter, but if
- // a GUI were calling into Ledger it would have one session object per
- // open document, with a separate report_t object for each report it
- // generated.
- report_stack.push_front(new report_t(session()));
-
- // Read the user's options, in the following order:
- //
- // 1. environment variables (LEDGER_<option>)
- // 2. initialization file (~/.ledgerrc)
- // 3. command-line (--option or -o)
- //
- // Before processing command-line options, we must notify the session object
- // 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();
- }
- ~global_scope_t()
- {
- TRACE_DTOR(global_scope_t);
-
- // If memory verification is being performed (which can be very slow),
- // clean up everything by closing the session and deleting the session
- // object, and then shutting down the memory tracing subsystem.
- // Otherwise, let it all leak because we're about to exit anyway.
- IF_VERIFY() set_session_context(NULL);
- }
-
- void read_journal_files()
- {
- INFO_START(journal, "Read journal file");
-
- std::size_t count = session().read_data(*session().create_journal(),
- report().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 * prompt_string()
- {
- static char prompt[32];
- std::size_t i;
- for (i = 0; i < report_stack.size(); i++)
- prompt[i] = ']';
- prompt[i++] = ' ';
- prompt[i] = '\0';
- return prompt;
- }
-
- session_t& session() {
- return *session_ptr.get();
- }
- report_t& report() {
- return report_stack.front();
- }
-
- void push_report() {
- report_stack.push_front(new report_t(report_stack.front()));
- }
- void pop_report() {
- if (! report_stack.empty())
- report_stack.pop_front();
- }
-
- void report_error(const std::exception& err)
- {
- std::cout.flush(); // first display anything that was pending
-
- if (caught_signal == NONE_CAUGHT) {
- // Display any pending error context information
- string context = error_context();
- if (! context.empty())
- std::cerr << context << std::endl;
-
- std::cerr << "Error: " << err.what() << std::endl;
- } else {
- caught_signal = NONE_CAUGHT;
- }
- }
-
- /**
- * @return \c true if a command was actually executed; otherwise, it probably
- * just resulted in setting some options.
- */
- void execute_command(strings_list args, bool at_repl)
- {
- // Process the command verb, arguments and options
- args = read_command_arguments(report(), args);
- if (args.empty())
- return;
-
- string_iterator arg = args.begin();
- string verb = *arg++;
-
- // Look for a precommand first, which is defined as any defined function
- // whose name starts with "ledger_precmd_". The difference between a
- // precommand and a regular command is that precommands ignore the journal
- // data file completely, nor is the user's init file read.
- //
- // Here are some examples of pre-commands:
- //
- // parse STRING ; show how a value expression is parsed
- // eval STRING ; simply evaluate a value expression
- // format STRING ; show how a format string is parsed
- //
- // If such a command is found, create the output stream for the result and
- // then invoke the command.
-
- function_t command;
- bool is_precommand = false;
- bind_scope_t bound_scope(*this, report());
-
- if (bool(command = look_for_precommand(bound_scope, verb)))
- is_precommand = true;
- else if (! bool(command = look_for_command(bound_scope, verb)))
- throw_(std::logic_error, "Unrecognized command '" << verb << "'");
-
- // If it is not a pre-command, then parse the user's ledger data at this
- // time if not done alreday (i.e., if not at a REPL). Then patch up the
- // report options based on the command verb.
-
- if (! is_precommand) {
- if (! at_repl)
- read_journal_files();
-
- // 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);
- }
-
- // Create the output stream (it might be a file, the console or a PAGER
- // subprocess) and invoke the report command. The output stream is closed
- // by the caller of this function.
-
- report().output_stream.initialize(report().output_file,
- session().pager_path);
-
- // Create an argument scope containing the report command's arguments, and
- // then invoke the command. The bound scope causes lookups to happen
- // first in the global scope, and then in the report scope.
-
- call_scope_t command_args(bound_scope);
- for (string_iterator i = arg; i != args.end(); i++)
- command_args.push_back(string_value(*i));
-
- INFO_START(command, "Finished executing command");
- command(command_args);
- INFO_FINISH(command);
- }
-
- int execute_command_wrapper(strings_list args, bool at_repl)
- {
- int status = 1;
-
- try {
- push_report();
- execute_command(args, at_repl);
- pop_report();
-
- // If we've reached this point, everything succeeded fine. Ledger uses
- // exceptions to notify of error conditions, so if you're using gdb,
- // just type "catch throw" to find the source point of any error.
- status = 0;
- }
- catch (const std::exception& err) {
- pop_report();
- report_error(err);
- }
-
- return status;
- }
-
- value_t push_report_cmd(call_scope_t&) {
- // Make a copy at position 2, because the topmost report object has an
- // open output stream at this point. We want it to get popped off as
- // soon as this command terminate so that the stream is closed cleanly.
- report_stack.insert(++report_stack.begin(),
- new report_t(report_stack.front()));
- return true;
- }
- value_t pop_report_cmd(call_scope_t&) {
- pop_report();
- return true;
- }
-
- value_t option_script_(call_scope_t& args) {
- script_file = args[0].as_string();
- return true;
- }
-
- value_t ignore(call_scope_t&) {
- return true;
- }
-
- virtual expr_t::ptr_op_t lookup(const string& name)
- {
- const char * p = name.c_str();
- switch (*p) {
- case 'l':
- if (std::strncmp(p, "ledger_precmd_", 14) == 0) {
- p = p + 14;
- switch (*p) {
- case 'p':
- if (std::strcmp(p, "push") == 0)
- return MAKE_FUNCTOR(global_scope_t::push_report_cmd);
- else if (std::strcmp(p, "pop") == 0)
- return MAKE_FUNCTOR(global_scope_t::pop_report_cmd);
- break;
- }
- }
- break;
-
- case 'o':
- if (std::strncmp(p, "opt_", 4) == 0) {
- p = p + 4;
- switch (*p) {
- case 'd':
- if (std::strcmp(p, "debug_") == 0)
- return MAKE_FUNCTOR(global_scope_t::ignore);
- break;
-
- case 's':
- if (std::strcmp(p, "script_") == 0)
- return MAKE_FUNCTOR(global_scope_t::option_script_);
- break;
-
- case 't':
- if (std::strcmp(p, "trace_") == 0)
- return MAKE_FUNCTOR(global_scope_t::ignore);
- break;
-
- case 'v':
- if (! *(p + 1) || std::strcmp(p, "verbose") == 0)
- return MAKE_FUNCTOR(global_scope_t::ignore);
- else if (std::strcmp(p, "verify") == 0)
- return MAKE_FUNCTOR(global_scope_t::ignore);
- break;
- }
- }
- }
-
- // If you're wondering how symbols from report() will be found, it's
- // because of the bind_scope_t object in execute_command() below.
- return expr_t::ptr_op_t();
- }
- };
-
strings_list split_arguments(char * line)
{
strings_list args;