/* * Copyright (c) 2003-2009, John Wiegley. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of New Artisans LLC nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include // Read this file for a top-level overview #include "work.h" // This is where the meat of main() is, which // was moved there for the sake of clarity using namespace ledger; namespace { strings_list split_arguments(char * line) { strings_list args; // jww (2009-02-04): This is too naive for (char * p = std::strtok(line, " \t"); p; p = std::strtok(NULL, " \t")) args.push_back(p); return args; } char * prompt_string(const ptr_list& report_stack) { 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; } 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(session_t& session, report_t& report, strings_list args, bool at_repl) { // Create a new report command object based on the current one, so that // the next command's option don't corrupt state. std::auto_ptr manager(new report_t(report)); // Process the command verb, arguments and options args = read_command_arguments(*manager.get(), 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; if (bool(command = look_for_precommand(*manager.get(), verb))) is_precommand = true; else if (! bool(command = look_for_command(*manager.get(), 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(session, manager->account); // 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(*manager.get(), verb); } // Create the output stream (it might be a file, the console or a PAGER // subprocess) and invoke the report command. create_output_stream(*manager.get()); // closed by auto_ptr destructor invoke_command_verb(*manager.get(), command, arg, args.end()); } int execute_command_wrapper(session_t& session, report_t& report, strings_list args, bool at_repl) { int status = 1; try { execute_command(session, report, args, at_repl); // 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) { report_error(err); } return status; } } int main(int argc, char * argv[], char * envp[]) { int status; // The very first thing we do is handle some very special command-line // options, since they affect how the environment is setup: // // --verify ; turns on memory tracing // --verbose ; turns on logging // --debug CATEGORY ; turns on debug logging // --trace LEVEL ; turns on trace logging handle_debug_options(argc, argv); IF_VERIFY() initialize_memory_tracing(); INFO("Ledger starting"); // Initialize global Boost/C++ environment std::ios::sync_with_stdio(false); filesystem::path::default_name_check(filesystem::portable_posix_name); std::signal(SIGINT, sigint_handler); std::signal(SIGPIPE, sigpipe_handler); // Create the session object, which maintains nearly all state relating to // this invocation of Ledger; and register all known journal parsers. session_t * session = new LEDGER_SESSION_T; set_session_context(session); // 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. ptr_list report_stack; report_stack.push_front(new report_t(*session)); try { // Read the user's options, in the following order: // // 1. environment variables (LEDGER_