summaryrefslogtreecommitdiff
path: root/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'main.cc')
-rw-r--r--main.cc698
1 files changed, 316 insertions, 382 deletions
diff --git a/main.cc b/main.cc
index bae22bcd..469bb5ee 100644
--- a/main.cc
+++ b/main.cc
@@ -1,232 +1,214 @@
-#include <iostream>
-#include <fstream>
-#include <sstream>
-#include <algorithm>
-#include <exception>
-#include <iterator>
-#include <string>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-
-#include "acconf.h"
+/*
+ * Copyright (c) 2003-2007, 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 "utils.h"
+#include "option.h"
+//#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
+//#include "gnucash.h"
+//#endif
+//#include "qif.h"
+//#include "ofx.h"
+#include "jbuilder.h"
+#include "compile.h"
+
+#include <ledger.h>
#ifdef HAVE_UNIX_PIPES
#include <sys/types.h>
#include <sys/wait.h>
-#include <unistd.h>
-#include "fdstream.hpp"
+#include <fdstream.hpp>
#endif
-#include "ledger.h"
-
-using namespace ledger;
-
-int parse_and_report(config_t& config, std::auto_ptr<journal_t>& journal,
- report_t& report, int argc, char * argv[], char * envp[])
+static int read_and_report(ledger::report_t& report, int argc, char * argv[],
+ char * envp[])
{
- // Configure the terminus for value expressions
+ using namespace ledger;
- ledger::terminus = current_moment;
+ session_t& session(report.session);
- // Parse command-line arguments, and those set in the environment
+ // Handle the command-line arguments
- std::list<string> args;
- process_arguments(ledger::config_options, argc - 1, argv + 1, false, args);
+ strings_list args;
+ process_arguments(argc - 1, argv + 1, false, report, args);
if (args.empty()) {
- option_help(std::cerr);
+#if 0
+ help(std::cerr);
+#endif
return 1;
}
strings_list::iterator arg = args.begin();
- if (config.cache_file == "<none>")
- config.use_cache = false;
+ if (! session.cache_file)
+ session.use_cache = false;
else
- config.use_cache = config.data_file.empty() && config.price_db.empty();
- DEBUG("ledger.config.cache", "1. use_cache = " << config.use_cache);
+ session.use_cache = ! session.data_file.empty() && session.price_db;
-#if 0
- TRACE(main, "Processing options and environment variables");
-#endif
+ DEBUG("ledger.session.cache", "1. use_cache = " << session.use_cache);
- process_environment(ledger::config_options,
- const_cast<const char **>(envp), "LEDGER_");
+ // Process the environment settings
-#if 1
- // These are here for backwards compatability, but are deprecated.
+ TRACE_START(environment, 1, "Processed environment variables");
+ process_environment(const_cast<const char **>(envp), "LEDGER_", report);
+ TRACE_FINISH(environment, 1);
- if (const char * p = std::getenv("LEDGER"))
- process_option(ledger::config_options, "file", p);
- if (const char * p = std::getenv("LEDGER_INIT"))
- process_option(ledger::config_options, "init-file", p);
- if (const char * p = std::getenv("PRICE_HIST"))
- process_option(ledger::config_options, "price-db", p);
- if (const char * p = std::getenv("PRICE_EXP"))
- process_option(ledger::config_options, "price-exp", p);
-#endif
+ optional<path> home;
+ if (const char * home_var = std::getenv("HOME"))
+ home = home_var;
- const char * p = std::getenv("HOME");
- string home = p ? p : "";
+ if (! session.init_file)
+ session.init_file = home ? *home / ".ledgerrc" : "./.ledgerrc";
+ if (! session.price_db)
+ session.price_db = home ? *home / ".pricedb" : "./.pricedb";
- if (config.init_file.empty())
- config.init_file = home + "/.ledgerrc";
- if (config.price_db.empty())
- config.price_db = home + "/.pricedb";
+ if (! session.cache_file)
+ session.cache_file = home ? *home / ".ledger-cache" : "./.ledger-cache";
- if (config.cache_file.empty())
- config.cache_file = home + "/.ledger-cache";
+ if (session.data_file == *session.cache_file)
+ session.use_cache = false;
- if (config.data_file == config.cache_file)
- config.use_cache = false;
- DEBUG("ledger.config.cache", "2. use_cache = " << config.use_cache);
+ DEBUG("ledger.session.cache", "2. use_cache = " << session.use_cache);
-#if 0
- TRACE(main, string("Initialization file is ") + config.init_file);
- TRACE(main, string("Price database is ") + config.price_db);
- TRACE(main, string("Binary cache is ") + config.cache_file);
- TRACE(main, string("Main journal is ") + config.data_file);
+ INFO("Initialization file is " << session.init_file->string());
+ INFO("Price database is " << session.price_db->string());
+ INFO("Binary cache is " << session.cache_file->string());
+ INFO("Journal file is " << session.data_file.string());
- TRACE(main, string("Based on option settings, binary cache ") +
- (config.use_cache ? "WILL " : "will NOT ") + "be used");
-#endif
+ if (! session.use_cache)
+ INFO("Binary cache mechanism will not be used");
+
+ // Read the command word and create a command object based on it
+
+ string verb = *arg++;
+
+ xml::xpath_t::function_t command;
- // Read the command word, canonicalize it to its one letter form,
- // then configure the system based on the kind of report to be
- // generated
-
- string command = *arg++;
-
- if (command == "balance" || command == "bal" || command == "b")
- command = "b";
- else if (command == "register" || command == "reg" || command == "r")
- command = "r";
- else if (command == "print" || command == "p")
- command = "p";
- else if (command == "output")
- command = "w";
- else if (command == "dump")
- command = "W";
- else if (command == "emacs" || command == "lisp")
- command = "x";
- else if (command == "xml")
- command = "X";
- else if (command == "entry")
- command = "e";
- else if (command == "equity")
- command = "E";
- else if (command == "prices")
- command = "P";
- else if (command == "pricesdb")
- command = "D";
- else if (command == "csv")
- command = "c";
- else if (command == "parse") {
- value_expr expr(ledger::parse_value_expr(*arg));
-
- if (config.verbose_mode) {
+#if 0
+ if (verb == "register" || verb == "reg" || verb == "r")
+ command = register_command();
+ else if (verb == "balance" || verb == "bal" || verb == "b")
+ command = balance_command();
+ else if (verb == "print" || verb == "p")
+ command = print_command();
+ else if (verb == "equity")
+ command = equity_command();
+ else if (verb == "entry")
+ command = entry_command();
+ else if (verb == "dump")
+ command = dump_command();
+ else if (verb == "output")
+ command = output_command();
+ else if (verb == "prices")
+ command = prices_command();
+ else if (verb == "pricesdb")
+ command = pricesdb_command();
+ else if (verb == "csv")
+ command = csv_command();
+ else if (verb == "emacs" || verb == "lisp")
+ command = emacs_command();
+ else
+#endif
+ if (verb == "xml")
+ command = bind(xml_command, _1);
+ else if (verb == "expr")
+ ;
+ else if (verb == "xpath")
+ ;
+ else if (verb == "parse") {
+ xml::xpath_t expr(*arg);
+ xml::document_t temp(xml::LEDGER_NODE);
+
+ xml::xpath_t::context_scope_t doc_scope(report, &temp);
+
+ IF_INFO() {
std::cout << "Value expression tree:" << std::endl;
- ledger::dump_value_expr(std::cout, expr.get());
+ expr.dump(std::cout);
std::cout << std::endl;
+
std::cout << "Value expression parsed was:" << std::endl;
- ledger::print_value_expr(std::cout, expr.get());
+ expr.print(std::cout, doc_scope);
std::cout << std::endl << std::endl;
- std::cout << "Result of computation: ";
+
+ expr.compile(doc_scope);
+
+ std::cout << "Value expression after compiling:" << std::endl;
+ expr.dump(std::cout);
+ std::cout << std::endl;
+
+ std::cout << "Value expression is now:" << std::endl;
+ expr.print(std::cout, doc_scope);
+ std::cout << std::endl << std::endl;
+
+ std::cout << "Result of calculation: ";
}
- value_t result = guarded_compute(expr.get());
- std::cout << result.strip_annotations() << std::endl;
+ std::cout << expr.calc(doc_scope).strip_annotations() << std::endl;
return 0;
}
- else if (command == "expr") {
- // this gets done below...
- }
else {
- throw new error(string("Unrecognized command '") + command + "'");
- }
+ char buf[128];
+ std::strcpy(buf, "command_");
+ std::strcat(buf, verb.c_str());
- // Parse initialization files, ledger data, price database, etc.
+ if (xml::xpath_t::ptr_op_t def = report.lookup(buf))
+ command = def->as_function();
- journal.reset(new journal_t);
-
-#if 0
- { TRACE_PUSH(parser, "Parsing journal file");
-#endif
-
- if (parse_ledger_data(config, journal.get()) == 0)
- throw new error("Please specify ledger file using -f"
- " or LEDGER_FILE environment variable.");
-
-#if 0
- TRACE_POP(parser, "Finished parsing"); }
-#endif
+ if (! command)
+ throw_(std::logic_error, string("Unrecognized command '") + verb + "'");
+ }
- // process the command word and its following arguments
+ // Parse the initialization file, which can only be textual; then
+ // parse the journal data.
- string first_arg;
- if (command == "w") {
- if (arg != args.end())
- first_arg = *arg++;
- }
- else if (command == "W") {
- if (report.output_file.empty())
- throw new error("The 'dump' command requires use of the --output option");
- }
+ session.read_init();
-#if 0
- TRACE(options, string("Post-processing options ") +
- "for command \"" + command + "\"");
-#endif
+ INFO_START(journal, "Read journal file");
- report.process_options(command, arg, args.end());
+ xml::document_t xml_document(xml::LEDGER_NODE);
+ journal_t * journal = session.create_journal();
+ xml::journal_builder_t builder(xml_document, journal);
-#if 0
- // jww (2008-05-08): Is this disabled now?
- // If downloading is to be supported, configure the updater
+ if (! session.read_data(builder, journal, report.account))
+ throw_(parse_error, "Failed to locate any journal entries; "
+ "did you specify a valid file with -f?");
- if (! commodity_base_t::updater && config.download_quotes)
- commodity_base_t::updater =
- new quotes_by_script(config.price_db, config.pricing_leeway,
- config.cache_dirty);
-#endif
+ INFO_FINISH(journal);
- std::auto_ptr<entry_t> new_entry;
- if (command == "e") {
- if (arg == args.end()) {
- std::cout << "\
-The entry command requires at least one argument, so Ledger can intelligently\n\
-create a new entry for you. The possible arguments are:\n\
- DATE PAYEE [ACCOUNT] [AMOUNT] [DRAW ACCOUNT]\n\n\
-Some things to note:\n\
- - The ACCOUNT is optional; if no account is given, the last account affected\n\
- by PAYEE is used. If no payee can be found, the generic account 'Expenses'\n\
- is used.\n\
- - The AMOUNT is optional; if not specified, the same amount is used as the\n\
- last time PAYEE was seen, or 0 if not applicable.\n\
- - The AMOUNT does not require a commodity; if none is given, the commodity\n\
- currently contained within ACCOUNT is used, or no commodity at all if\n\
- either: the ACCOUNT was not found, or it contains more than one commodity.\n\
- - Lastly, the DRAW ACCOUNT is optional; if not present, the last account\n\
- drawn from by PAYEE is used, or the 'basket' account (specified with\n\
- 'A ACCOUNT' in your Ledger file) if that does not apply, or the generic\n\
- account 'Equity' is used.\n\n\
-Here are a few examples, all of which may be equivalent depending on your\n\
-Ledger data:\n\
- ledger entry 3/25 chevron\n\
- ledger entry 3/25 chevron 20\n\
- ledger entry 3/25 chevron \\$20\n\
- ledger entry 3/25 chevron gas 20\n\
- ledger entry 3/25 chevron gas \\$20 checking\n\n\
-A final note: Ledger never modifies your data! You are responsible for\n\
-appending the output of this command to your Ledger file if you so choose."
- << std::endl;
- return 1;
- }
- new_entry.reset(derive_new_entry(*journal, arg, args.end()));
- if (! new_entry.get())
- return 1;
- }
+ TRACE_FINISH(entry_text, 1);
+ TRACE_FINISH(entry_date, 1);
+ TRACE_FINISH(entry_details, 1);
+ TRACE_FINISH(entry_xacts, 1);
+ TRACE_FINISH(entries, 1);
+ TRACE_FINISH(parsing_total, 1);
// Configure the output stream
@@ -235,22 +217,20 @@ appending the output of this command to your Ledger file if you so choose."
#endif
std::ostream * out = &std::cout;
- if (! report.output_file.empty()) {
- out = new ofstream(report.output_file);
+ if (report.output_file) {
+ out = new ofstream(*report.output_file);
}
#ifdef HAVE_UNIX_PIPES
- else if (! config.pager.empty()) {
+ else if (report.pager) {
status = pipe(pfd);
if (status == -1)
- throw new error("Failed to create pipe");
+ throw_(std::logic_error, "Failed to create pipe");
status = fork();
if (status < 0) {
- throw new error("Failed to fork child process");
+ throw_(std::logic_error, "Failed to fork child process");
}
else if (status == 0) { // child
- const char *arg0;
-
// Duplicate pipe's reading end into stdin
status = dup2(pfd[0], STDIN_FILENO);
if (status == -1)
@@ -265,13 +245,8 @@ appending the output of this command to your Ledger file if you so choose."
// Find command name: its the substring starting right of the
// rightmost '/' character in the pager pathname. See manpage
// for strrchr.
- arg0 = std::strrchr(config.pager.c_str(), '/');
- if (arg0)
- arg0++;
- else
- arg0 = config.pager.c_str(); // No slashes in pager.
-
- execlp(config.pager.c_str(), arg0, (char *)0);
+ execlp(report.pager->native_file_string().c_str(),
+ basename(*report.pager).c_str(), (char *)0);
perror("execl");
exit(1);
}
@@ -282,236 +257,188 @@ appending the output of this command to your Ledger file if you so choose."
}
#endif
- // Are we handling the parse or expr commands? Do so now.
-
- if (command == "expr") {
- value_expr expr(ledger::parse_value_expr(*arg));
-
- if (config.verbose_mode) {
- std::cout << "Value expression tree:" << std::endl;
- ledger::dump_value_expr(std::cout, expr.get());
- std::cout << std::endl;
- std::cout << "Value expression parsed was:" << std::endl;
- ledger::print_value_expr(std::cout, expr.get());
- std::cout << std::endl << std::endl;
- std::cout << "Result of computation: ";
- }
+ report.define("ostream", value_t(out));
- value_t result = guarded_compute(expr.get());
- std::cout << result.strip_annotations() << std::endl;
+ // Are we handling the expr commands? Do so now.
- return 0;
- }
+ xml::xpath_t::context_scope_t doc_scope(report, &xml_document);
- // Compile the format strings
-
- const string * format;
-
- if (! report.format_string.empty())
- format = &report.format_string;
- else if (command == "b")
- format = &config.balance_format;
- else if (command == "r")
- format = &config.register_format;
- else if (command == "E")
- format = &config.equity_format;
- else if (command == "P")
- format = &config.prices_format;
- else if (command == "D")
- format = &config.pricesdb_format;
- else if (command == "w")
- format = &config.write_xact_format;
- else
- format = &config.print_format;
-
- // Walk the entries based on the report type and the options
-
- item_handler<transaction_t> * formatter;
- std::list<item_handler<transaction_t> *> formatter_ptrs;
-
- if (command == "b" || command == "E")
- formatter = new set_account_value;
- else if (command == "p" || command == "e")
- formatter = new format_entries(*out, *format);
- else if (command == "x")
- formatter = new format_emacs_transactions(*out);
- else if (command == "X")
- formatter = new format_xml_entries(*out, report.show_totals);
- else if (command == "c")
- formatter = new format_csv_transactions(*out);
- else
- formatter = new format_transactions(*out, *format);
+ if (verb == "expr") {
+ xml::xpath_t expr(*arg);
- if (command == "w") {
-#if 0
- TRACE_PUSH(text_writer, "Writing journal file");
-#endif
- write_textual_journal(*journal, first_arg, *formatter,
- config.write_hdr_format, *out);
-#if 0
- TRACE_POP(text_writer, "Finished writing");
-#endif
- }
- else if (command == "W") {
-#if 0
- TRACE_PUSH(binary_writer, "Writing binary file");
-#endif
- ofstream stream(report.output_file);
- binary::write_journal(stream, journal.get());
-#if 0
- TRACE_POP(binary_writer, "Finished writing");
-#endif
- }
- else {
-#if 0
- TRACE_PUSH(main, "Walking journal entries");
-#endif
-
- formatter = report.chain_xact_handlers(command, formatter, journal.get(),
- journal->master, formatter_ptrs);
- if (command == "e")
- walk_transactions(new_entry->transactions, *formatter);
- else if (command == "P" || command == "D")
- walk_commodities(amount_t::current_pool->commodities, *formatter);
- else
- walk_entries(journal->entries, *formatter);
+ IF_INFO() {
+ *out << "Value expression tree:" << std::endl;
+ expr.dump(*out);
+ *out << std::endl;
+ *out << "Value expression parsed was:" << std::endl;
+ expr.print(*out, doc_scope);
+ *out << std::endl << std::endl;
+ *out << "Result of calculation: ";
+ }
- if (command != "P" && command != "D")
- formatter->flush();
+ *out << expr.calc(doc_scope).strip_annotations() << std::endl;
-#if 0
- TRACE_POP(main, "Finished entry walk");
-#endif
+ return 0;
}
+ else if (verb == "xpath") {
+ std::cout << "XPath parsed: ";
+
+ xml::xpath_t xpath(*arg);
+ xpath.print(*out, doc_scope);
+ *out << std::endl;
+
+ IF_INFO() {
+ *out << "Raw results:" << std::endl;
+
+ foreach (const value_t& value, xpath.find_all(doc_scope)) {
+ if (value.is_xml_node())
+ value.as_xml_node()->print(std::cout);
+ else
+ std::cout << value;
+ std::cout << std::endl;
+ }
- // For the balance and equity reports, output the sum totals.
+ *out << "Compiled results:" << std::endl;
+ }
- if (command == "b") {
-#if 0
- TRACE_PUSH(main, "Walking journal accounts");
-#endif
+ xml::compile_node(xml_document, report);
- format_account acct_formatter(*out, *format, report.display_predicate);
- sum_accounts(*journal->master);
- walk_accounts(*journal->master, acct_formatter, report.sort_string);
- acct_formatter.flush();
-
- if (account_has_xdata(*journal->master)) {
- account_xdata_t& xdata = account_xdata(*journal->master);
- if (! report.show_collapsed && xdata.total) {
- *out << "--------------------\n";
- xdata.value = xdata.total;
- acct_formatter.format.format(*out, details_t(*journal->master));
- }
+ foreach (const value_t& value, xpath.find_all(doc_scope)) {
+ if (value.is_xml_node())
+ value.as_xml_node()->print(std::cout);
+ else
+ std::cout << value;
+ std::cout << std::endl;
}
-#if 0
- TRACE_POP(main, "Finished account walk");
-#endif
+ return 0;
}
- else if (command == "E") {
-#if 0
- TRACE_PUSH(main, "Walking journal accounts");
-#endif
- format_equity acct_formatter(*out, *format, report.display_predicate);
- sum_accounts(*journal->master);
- walk_accounts(*journal->master, acct_formatter, report.sort_string);
- acct_formatter.flush();
+ // Apply transforms to the hierarchical document structure
-#if 0
- TRACE_POP(main, "Finished account walk");
-#endif
- }
+ INFO_START(transforms, "Applied transforms");
+ report.apply_transforms(doc_scope);
+ INFO_FINISH(transforms);
-#if DEBUG_LEVEL >= BETA
-#if 0
- { TRACE_PUSH(cleanup, "Cleaning up allocated memory");
-#endif
+ // Create an argument scope containing the report command's
+ // arguments, and then invoke the command.
- clear_transaction_xdata xact_cleaner;
- walk_entries(journal->entries, xact_cleaner);
+ xml::xpath_t::call_scope_t command_args(doc_scope);
- clear_account_xdata acct_cleaner;
- walk_accounts(*journal->master, acct_cleaner);
+ for (strings_list::iterator i = arg; i != args.end(); i++)
+ command_args.push_back(value_t(*i, true));
- if (! report.output_file.empty())
- delete out;
+ INFO_START(command, "Did user command '" << verb << "'");
- for (std::list<item_handler<transaction_t> *>::iterator i
- = formatter_ptrs.begin();
- i != formatter_ptrs.end();
- i++)
- delete *i;
+ command(command_args);
-#if 0
- TRACE_POP(cleanup, "Finished cleaning"); }
-#endif
-#endif
+ INFO_FINISH(command);
// Write out the binary cache, if need be
- if (config.use_cache && config.cache_dirty &&
- ! config.cache_file.empty()) {
-#if 0
- TRACE_PUSH(binary_cache, "Writing journal file");
-#endif
-
- ofstream stream(config.cache_file);
- binary::write_journal(stream, journal.get());
+ if (session.use_cache && session.cache_dirty && session.cache_file) {
+ TRACE_START(binary_cache, 1, "Wrote binary journal file");
#if 0
- TRACE_POP(binary_cache, "Finished writing");
+ ofstream stream(*session.cache_file);
+ write_binary_journal(stream, journal);
#endif
+
+ TRACE_FINISH(binary_cache, 1);
}
+ // If the user specified a pager, wait for it to exit now
+
#ifdef HAVE_UNIX_PIPES
- if (! config.pager.empty()) {
- delete out;
+ if (! report.output_file && report.pager) {
+ checked_delete(out);
close(pfd[1]);
// Wait for child to finish
wait(&status);
if (status & 0xffff != 0)
- throw new error("Something went wrong in the pager");
+ throw_(std::logic_error, "Something went wrong in the pager");
}
#endif
+ else if (DO_VERIFY() && report.output_file) {
+ checked_delete(out);
+ }
return 0;
}
int main(int argc, char * argv[], char * envp[])
{
- // This variable must be defined here so that any memory it holds is still
- // available should the subsequent exception handlers catch an inner
- // exception and need to report something on the invalid state of the
- // journal (such as an unbalanced entry).
- std::auto_ptr<journal_t> journal;
+ int status = 1;
- try {
-#if DEBUG_LEVEL < BETA
- ledger::do_cleanup = false;
+ for (int i = 1; i < argc; i++)
+ if (argv[i][0] == '-') {
+ if (std::strcmp(argv[i], "--verify") == 0) {
+#if defined(VERIFY_ON)
+ ledger::verify_enabled = true;
+#endif
+ }
+ else if (std::strcmp(argv[i], "--verbose") == 0 ||
+ std::strcmp(argv[i], "-v") == 0) {
+#if defined(LOGGING_ON)
+ ledger::_log_level = ledger::LOG_INFO;
+#endif
+ }
+ else if (i + 1 < argc && std::strcmp(argv[i], "--debug") == 0) {
+#if defined(DEBUG_ON)
+ ledger::_log_level = ledger::LOG_DEBUG;
+ ledger::_log_category = argv[i + 1];
+ i++;
+#endif
+ }
+ else if (i + 1 < argc && std::strcmp(argv[i], "--trace") == 0) {
+#if defined(TRACING_ON)
+ ledger::_log_level = ledger::LOG_TRACE;
+ ledger::_trace_level = boost::lexical_cast<int>(argv[i + 1]);
+ i++;
#endif
- config_t config;
- report_t report;
- ledger::config = &config;
- ledger::report = &report;
+ }
+ }
- amount_t::initialize();
- value_t::initialize();
+ IF_VERIFY()
+ ledger::initialize_memory_tracing();
-#if 0
- TRACE_PUSH(main, "Ledger starting");
-#endif
- int status = parse_and_report(config, journal, report, argc, argv, envp);
+ try {
+ std::ios::sync_with_stdio(false);
+
+ boost::filesystem::path::default_name_check
+ (boost::filesystem::portable_posix_name);
+
+ INFO("Ledger starting");
- value_t::shutdown();
- amount_t::shutdown();
+ std::auto_ptr<ledger::session_t> session(new ledger::session_t);
+
+ ledger::set_session_context(session.get());
#if 0
- TRACE_POP(main, "Ledger done");
+ session->register_parser(new binary_parser_t);
+#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
+ session->register_parser(new xml::xml_parser_t);
+ session->register_parser(new gnucash_parser_t);
+#endif
+#ifdef HAVE_LIBOFX
+ session->register_parser(new ofx_parser_t);
#endif
- return status;
+ session->register_parser(new qif_parser_t);
+#endif
+ session->register_parser(new ledger::textual_parser_t);
+
+ std::auto_ptr<ledger::report_t> report(new ledger::report_t(*session.get()));
+
+ status = read_and_report(*report.get(), argc, argv, envp);
+
+ if (DO_VERIFY()) {
+ ledger::set_session_context();
+ } else {
+ report.release();
+ session.release();
+ }
}
+#if 0
catch (error * err) {
std::cout.flush();
// Push a null here since there's no file context
@@ -520,8 +447,7 @@ int main(int argc, char * argv[], char * envp[])
err->context.push_front(new error_context(""));
err->reveal_context(std::cerr, "Error");
std::cerr << err->what() << std::endl;
- delete err;
- return 1;
+ checked_delete(err);
}
catch (fatal * err) {
std::cout.flush();
@@ -531,17 +457,25 @@ int main(int argc, char * argv[], char * envp[])
err->context.push_front(new error_context(""));
err->reveal_context(std::cerr, "Fatal");
std::cerr << err->what() << std::endl;
- delete err;
- return 1;
+ checked_delete(err);
}
+#endif
catch (const std::exception& err) {
std::cout.flush();
std::cerr << "Error: " << err.what() << std::endl;
- return 1;
}
- catch (int status) {
- return status;
+ catch (int _status) {
+ status = _status;
}
+
+ IF_VERIFY() {
+ INFO("Ledger ended (Boost/libstdc++ may still hold memory)");
+ ledger::shutdown_memory_tracing();
+ } else {
+ INFO("Ledger ended");
+ }
+
+ return status;
}
// main.cc ends here.