diff options
author | John Wiegley <johnw@newartisans.com> | 2004-08-29 03:23:55 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2004-08-29 03:23:55 -0400 |
commit | 032afa5657bd600476cde256b6d7db84fde7d038 (patch) | |
tree | 3d9839f92ce2f99fda85583388dd258f376fdbbb /main.cc | |
parent | 3034529d54759f0fd3bcc6191f33a5e93a1988ca (diff) | |
download | fork-ledger-032afa5657bd600476cde256b6d7db84fde7d038.tar.gz fork-ledger-032afa5657bd600476cde256b6d7db84fde7d038.tar.bz2 fork-ledger-032afa5657bd600476cde256b6d7db84fde7d038.zip |
a bit of a reorg; still much more to go
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 615 |
1 files changed, 179 insertions, 436 deletions
@@ -31,14 +31,10 @@ using namespace ledger; #include <ctime> namespace { - bool cache_dirty = false; - TIMER_DEF(write_cache, "writing cache file"); TIMER_DEF(report_gen, "generation of final report"); - TIMER_DEF(handle_options, "configuring based on options"); TIMER_DEF(parse_files, "parsing ledger files"); - TIMER_DEF(process_env, "processing environment"); - TIMER_DEF(process_args, "processing command-line arguments"); + TIMER_DEF(process_opts, "processing args and environment"); TIMER_DEF(read_cache, "reading cache file"); } @@ -76,85 +72,166 @@ namespace std { #endif -static void -regexps_to_predicate(std::deque<std::string>::const_iterator begin, - std::deque<std::string>::const_iterator end, - config_t * config, - const bool account_regexp = false, - const bool add_account_short_masks = false) +void parse_ledger_data(journal_t * journal, + parser_t * text_parser, + parser_t * cache_parser) { - std::string regexps[2]; - - // Treat the remaining command-line arguments as regular - // expressions, used for refining report results. - - for (std::deque<std::string>::const_iterator i = begin; - i != end; - i++) - if ((*i)[0] == '-') { - if (! regexps[1].empty()) - regexps[1] += "|"; - regexps[1] += (*i).substr(1); + TIMER_START(parse_files); + + int entry_count = 0; + + if (! config.init_file.empty()) { + if (parse_journal_file(config.init_file, journal)) + throw error("Entries not allowed in initialization file"); + journal->sources.pop_front(); // remove init file + } + + if (config.use_cache && ! config.cache_file.empty() && + ! config.data_file.empty()) { + config.cache_dirty = true; + if (access(config.cache_file.c_str(), R_OK) != -1) { + std::ifstream stream(config.cache_file.c_str()); + if (cache_parser->test(stream)) { + entry_count += cache_parser->parse(stream, journal, NULL, + &config.data_file); + if (entry_count > 0) + config.cache_dirty = false; + } } - else if ((*i)[0] == '+') { - if (! regexps[0].empty()) - regexps[0] += "|"; - regexps[0] += (*i).substr(1); + } + + if (entry_count == 0 && ! config.data_file.empty()) { + account_t * account = NULL; + if (! config.account.empty()) + account = journal->find_account(config.account); + + if (config.data_file == "-") { + config.use_cache = false; + entry_count += parse_journal(std::cin, journal, account); + } else { + entry_count += parse_journal_file(config.data_file, journal, account); } - else { - if (! regexps[0].empty()) - regexps[0] += "|"; - regexps[0] += *i; + + if (! config.price_db.empty()) + if (parse_journal_file(config.price_db, journal)) + throw error("Entries not allowed in price history file"); + } + + for (strings_list::iterator i = config.price_settings.begin(); + i != config.price_settings.end(); + i++) { + std::string conversion = "C "; + conversion += *i; + int i = conversion.find('='); + if (i != -1) { + conversion[i] = ' '; + std::istringstream stream(conversion); + text_parser->parse(stream, journal, journal->master); } + } - for (int i = 0; i < 2; i++) { - if (regexps[i].empty()) - continue; + if (entry_count == 0) + throw error("Please specify ledger file using -f," + " or LEDGER_FILE environment variable."); - if (! config->predicate.empty()) - config->predicate += "&"; + VALIDATE(journal->valid()); - if (i == 1) { - config->predicate += "!"; - } - else if (add_account_short_masks) { - if (regexps[i].find(':') != std::string::npos) { - config->show_subtotal = true; - } else { - if (! config->display_predicate.empty()) - config->display_predicate += "&"; - else if (! config->show_empty) - config->display_predicate += "T&"; - - config->display_predicate += "///(?:"; - config->display_predicate += regexps[i]; - config->display_predicate += ")/"; - } - } + TIMER_STOP(parse_files); +} + +item_handler<transaction_t> * +chain_formatters(const std::string& command, + item_handler<transaction_t> * base_formatter) +{ + std::auto_ptr<item_handler<transaction_t> > formatter; + + // format_transactions write each transaction received to the + // output stream. + if (command == "b" || command == "E") { + formatter.reset(base_formatter); + } else { + formatter.reset(base_formatter); - if (! account_regexp) - config->predicate += "/"; - config->predicate += "/(?:"; - config->predicate += regexps[i]; - config->predicate += ")/"; + // filter_transactions will only pass through transactions + // matching the `display_predicate'. + if (! config.display_predicate.empty()) + formatter.reset(new filter_transactions(formatter.release(), + config.display_predicate)); + + // calc_transactions computes the running total. When this + // appears will determine, for example, whether filtered + // transactions are included or excluded from the running total. + formatter.reset(new calc_transactions(formatter.release(), + config.show_inverted)); + + // sort_transactions will sort all the transactions it sees, based + // on the `sort_order' value expression. + if (config.sort_order.get()) + formatter.reset(new sort_transactions(formatter.release(), + config.sort_order.get())); + + // changed_value_transactions adds virtual transactions to the + // list to account for changes in market value of commodities, + // which otherwise would affect the running total unpredictably. + if (config.show_revalued) + formatter.reset(new changed_value_transactions(formatter.release(), + config.show_revalued_only)); + + // collapse_transactions causes entries with multiple transactions + // to appear as entries with a subtotaled transaction for each + // commodity used. + if (config.show_collapsed) + formatter.reset(new collapse_transactions(formatter.release())); + + // subtotal_transactions combines all the transactions it receives + // into one subtotal entry, which has one transaction for each + // commodity in each account. + // + // interval_transactions is like subtotal_transactions, but it + // subtotals according to time intervals rather than totalling + // everything. + // + // dow_transactions is like interval_transactions, except that it + // reports all the transactions that fall on each subsequent day + // of the week. + if (config.show_subtotal) + formatter.reset(new subtotal_transactions(formatter.release())); + else if (config.report_interval) + formatter.reset(new interval_transactions(formatter.release(), + config.report_interval, + config.interval_begin)); + else if (config.days_of_the_week) + formatter.reset(new dow_transactions(formatter.release())); } + + // related_transactions will pass along all transactions related + // to the transaction received. If `show_all_related' is true, + // then all the entry's transactions are passed; meaning that if + // one transaction of an entry is to be printed, all the + // transaction for that entry will be printed. + if (config.show_related) + formatter.reset(new related_transactions(formatter.release(), + config.show_all_related)); + + // This filter_transactions will only pass through transactions + // matching the `predicate'. + if (! config.predicate.empty()) + formatter.reset(new filter_transactions(formatter.release(), + config.predicate)); + + return formatter.release(); } int parse_and_report(int argc, char * argv[], char * envp[]) { std::auto_ptr<journal_t> journal(new journal_t); - // Initialize the global configuration object for this run - - std::auto_ptr<config_t> global_config(new config_t); - config = global_config.get(); + // Parse command-line arguments, and those set in the environment - // Parse command-line arguments - - TIMER_START(process_args); + TIMER_START(process_opts); std::deque<std::string> args; - process_arguments(argc, argv, false, args); + process_arguments(argc - 1, argv + 1, false, args); if (args.empty()) { option_help(std::cerr); @@ -162,13 +239,7 @@ int parse_and_report(int argc, char * argv[], char * envp[]) } strings_list::iterator arg = args.begin(); - TIMER_STOP(process_args); - - bool use_cache = config->data_file.empty(); - - // Process options from the environment - - TIMER_START(process_env); + config.use_cache = config.data_file.empty(); process_environment(envp, "LEDGER_"); @@ -183,13 +254,10 @@ int parse_and_report(int argc, char * argv[], char * envp[]) process_option("price-exp", p); #endif - TIMER_STOP(process_env); + TIMER_STOP(process_opts); - // Parse ledger files - - TIMER_START(parse_files); + // Parse initialization files, ledger data, price database, etc. - // Setup the parsers std::auto_ptr<binary_parser_t> bin_parser(new binary_parser_t); #ifdef READ_GNUCASH std::auto_ptr<gnucash_parser_t> gnucash_parser(new gnucash_parser_t); @@ -204,81 +272,14 @@ int parse_and_report(int argc, char * argv[], char * envp[]) register_parser(qif_parser.get()); register_parser(text_parser.get()); - int entry_count = 0; + parse_ledger_data(journal.get(), text_parser.get(), bin_parser.get()); - try { - if (! config->init_file.empty()) { - if (parse_journal_file(config->init_file, journal.get())) - throw error("Entries not allowed in initialization file"); - journal->sources.pop_front(); // remove init file - } - - if (use_cache && ! config->cache_file.empty() && - ! config->data_file.empty()) { - cache_dirty = true; - if (access(config->cache_file.c_str(), R_OK) != -1) { - std::ifstream stream(config->cache_file.c_str()); - if (bin_parser->test(stream)) { - entry_count += bin_parser->parse(stream, journal.get(), NULL, - &config->data_file); - if (entry_count > 0) - cache_dirty = false; - } - } - } - - if (entry_count == 0 && ! config->data_file.empty()) { - account_t * account = NULL; - if (! config->account.empty()) - account = journal->find_account(config->account); - - if (config->data_file == "-") { - use_cache = false; - entry_count += text_parser->parse(std::cin, journal.get(), account); - } else { - entry_count += parse_journal_file(config->data_file, journal.get(), - account); - } - - if (! config->price_db.empty()) - if (parse_journal_file(config->price_db, journal.get())) - throw error("Entries not allowed in price history file"); - } - - for (strings_list::iterator i = config->price_settings.begin(); - i != config->price_settings.end(); - i++) { - std::string conversion = "C "; - conversion += *i; - int i = conversion.find('='); - if (i != -1) { - conversion[i] = ' '; - std::istringstream stream(conversion); - text_parser->parse(stream, journal.get(), journal->master); - } - } - } - catch (error& err) { - std::cerr << "Fatal: " << err.what() << std::endl; - return 1; - } - - if (entry_count == 0) { - std::cerr << "Please specify ledger file(s) using -f option " - << "or LEDGER environment variable." << std::endl; - return 1; - } - - VALIDATE(journal->valid()); - - TIMER_STOP(parse_files); - - // Read the command word, and then check and simplify it + // Read the command word, canonicalize it to its one letter form, + // then configure the system based on the kind of report to be + // generated std::string command = *arg++; - TIMER_START(handle_options); - if (command == "balance" || command == "bal" || command == "b") command = "b"; else if (command == "register" || command == "reg" || command == "r") @@ -290,294 +291,35 @@ int parse_and_report(int argc, char * argv[], char * envp[]) else if (command == "equity") command = "E"; else { - std::cerr << "Error: Unrecognized command '" << command << "'." - << std::endl; - return 1; + std::ostringstream msg; + msg << "Unrecognized command '" << command << "'"; + throw error(msg.str()); } - // Configure some other options depending on report type - - bool show_all_related = false; - - if (command == "p" || command == "e") { - config->show_related = - show_all_related = true; - } - else if (command == "E") { - config->show_subtotal = true; - } - else if (config->show_related) { - if (command == "r") { - config->show_inverted = true; - } else { - config->show_subtotal = true; - show_all_related = true; - } - } - - // Process remaining command-line arguments + config.process_options(command, arg, args.end()); std::auto_ptr<entry_t> new_entry; if (command == "e") { new_entry.reset(journal->derive_entry(arg, args.end())); if (! new_entry.get()) return 1; - } else { - // Treat the remaining command-line arguments as regular - // expressions, used for refining report results. - - std::deque<std::string>::iterator i = args.begin(); - for (; i != args.end(); i++) - if (*i == "--") - break; - - regexps_to_predicate(arg, i, config, true, - command == "b" && ! config->show_subtotal && - config->display_predicate.empty()); - if (i != args.end()) - regexps_to_predicate(i, args.end(), config); - } - - // Setup default value for the display predicate - - if (config->display_predicate.empty()) { - if (command == "b") { - if (! config->show_empty) - config->display_predicate = "T"; - if (! config->show_subtotal) { - if (! config->display_predicate.empty()) - config->display_predicate += "&"; - config->display_predicate += "l<=1"; - } - } - else if (command == "E") { - config->display_predicate = "t"; - } - } - - // Compile sorting criteria - - std::auto_ptr<value_expr_t> sort_order; - - if (! config->sort_string.empty()) { - try { - std::istringstream stream(config->sort_string); - sort_order.reset(parse_value_expr(stream)); - if (stream.peek() != -1) { - std::ostringstream err; - err << "Unexpected character '" << char(stream.peek()) << "'"; - throw value_expr_error(err.str()); - } - else if (! sort_order.get()) { - std::cerr << "Failed to parse sort criteria!" << std::endl; - return 1; - } - } - catch (const value_expr_error& err) { - std::cerr << "Error in sort criteria: " << err.what() << std::endl; - return 1; - } } - // Setup the values of %t and %T, used in format strings - - try { - format_t::value_expr = parse_value_expr(config->value_expr); - } - catch (const value_expr_error& err) { - std::cerr << "Error in amount (-t) specifier: " << err.what() - << std::endl; - return 1; - } - - try { - format_t::total_expr = parse_value_expr(config->total_expr); - } - catch (const value_expr_error& err) { - std::cerr << "Error in total (-T) specifier: " << err.what() - << std::endl; - return 1; - } - - // Setup local and global variables, depending on config settings. - - std::auto_ptr<std::ostream> output_stream; - - interval_t report_interval; - std::time_t interval_begin = 0; - - if (config->download_quotes) - commodity_t::updater = new quotes_by_script(config->price_db, - config->pricing_leeway, - cache_dirty); - - if (! config->output_file.empty()) - output_stream.reset(new std::ofstream(config->output_file.c_str())); - -#define OUT() (output_stream.get() ? *output_stream : std::cout) - - if (! config->interval_text.empty()) { - try { - std::istringstream stream(config->interval_text); - std::time_t begin = -1, end = -1; - - report_interval = interval_t::parse(stream, &begin, &end); - - if (begin != -1) { - interval_begin = begin; - - if (! config->predicate.empty()) - config->predicate += "&"; - char buf[32]; - std::sprintf(buf, "d>=%lu", begin); - config->predicate += buf; - } - - if (end != -1) { - if (! config->predicate.empty()) - config->predicate += "&"; - char buf[32]; - std::sprintf(buf, "d<%lu", end); - config->predicate += buf; - } - } - catch (const interval_expr_error& err) { - std::cerr << "Error in interval (-z) specifier: " << err.what() - << std::endl; - return 1; - } - } - - if (! config->date_format.empty()) - format_t::date_format = config->date_format; - -#ifdef DEBUG_ENABLED - DEBUG_PRINT("ledger.main.predicates", "predicate: " << config->predicate); - DEBUG_PRINT("ledger.main.predicates", - "disp-pred: " << config->display_predicate); -#endif - - // Compile the format strings - - const char * f; - if (! config->format_string.empty()) - f = config->format_string.c_str(); - else if (command == "b") - f = bal_fmt.c_str(); - else if (command == "r") - f = reg_fmt.c_str(); - else if (command == "E") - f = equity_fmt.c_str(); - else - f = print_fmt.c_str(); - - std::string first_line_format; - std::string next_lines_format; - - if (const char * p = std::strstr(f, "%/")) { - first_line_format = std::string(f, 0, p - f); - next_lines_format = std::string(p + 2); - } else { - first_line_format = next_lines_format = f; - } - - format_t format(first_line_format); - format_t nformat(next_lines_format); - - TIMER_STOP(handle_options); - // Walk the entries based on the report type and the options TIMER_START(report_gen); - // Stack up all the formatter needed to fulfills the user's - // requests. Some of these are order dependent, in terms of - // whether calc_transactions occurs before or after them. - std::auto_ptr<item_handler<transaction_t> > formatter; - // format_transactions write each transaction received to the - // output stream. if (command == "b" || command == "E") { -#ifdef DEBUG_ENABLED - if (DEBUG("ledger.balance.items")) { - formatter.reset(new format_transactions(OUT(), format, nformat)); - formatter.reset(new set_account_value(formatter.release())); - } else -#endif - formatter.reset(new set_account_value); + formatter.reset(chain_formatters(command, new set_account_value)); } else { - formatter.reset(new format_transactions(OUT(), format, nformat)); - - // filter_transactions will only pass through transactions - // matching the `display_predicate'. - if (! config->display_predicate.empty()) - formatter.reset(new filter_transactions(formatter.release(), - config->display_predicate)); - - // calc_transactions computes the running total. When this - // appears will determine, for example, whether filtered - // transactions are included or excluded from the running total. - formatter.reset(new calc_transactions(formatter.release(), - config->show_inverted)); - - // sort_transactions will sort all the transactions it sees, based - // on the `sort_order' value expression. - if (sort_order.get()) - formatter.reset(new sort_transactions(formatter.release(), - sort_order.get())); - - // changed_value_transactions adds virtual transactions to the - // list to account for changes in market value of commodities, - // which otherwise would affect the running total unpredictably. - if (config->show_revalued) - formatter.reset(new changed_value_transactions(formatter.release(), - config->show_revalued_only)); - - // collapse_transactions causes entries with multiple transactions - // to appear as entries with a subtotaled transaction for each - // commodity used. - if (config->show_collapsed) - formatter.reset(new collapse_transactions(formatter.release())); - - // subtotal_transactions combines all the transactions it receives - // into one subtotal entry, which has one transaction for each - // commodity in each account. - // - // interval_transactions is like subtotal_transactions, but it - // subtotals according to time intervals rather than totalling - // everything. - // - // dow_transactions is like interval_transactions, except that it - // reports all the transactions that fall on each subsequent day - // of the week. - if (config->show_subtotal) - formatter.reset(new subtotal_transactions(formatter.release())); - else if (report_interval) - formatter.reset(new interval_transactions(formatter.release(), - report_interval, - interval_begin)); - else if (config->days_of_the_week) - formatter.reset(new dow_transactions(formatter.release())); + std::ostream& out(config.output_stream.get() ? + *config.output_stream : std::cout); + formatter.reset(chain_formatters(command, + new format_transactions(out, config.format, config.nformat))); } - // related_transactions will pass along all transactions related - // to the transaction received. If `show_all_related' is true, - // then all the entry's transactions are passed; meaning that if - // one transaction of an entry is to be printed, all the - // transaction for that entry will be printed. - if (config->show_related) - formatter.reset(new related_transactions(formatter.release(), - show_all_related)); - - // This filter_transactions will only pass through transactions - // matching the `predicate'. - if (! config->predicate.empty()) - formatter.reset(new filter_transactions(formatter.release(), - config->predicate)); - - // Once the filters are chained, walk `journal's entries and start - // feeding each transaction that matches `predicate' to the chain. if (command == "e") walk_transactions(new_entry->transactions, *formatter); else @@ -585,32 +327,32 @@ int parse_and_report(int argc, char * argv[], char * envp[]) formatter->flush(); - // At this point all printing is finished if doing a register - // report; but if it's a balance or equity report, we've only - // finished calculating the totals and there is still reporting to - // be done. + // For the balance and equity reports, output the sum totals. + + std::ostream& out(config.output_stream.get() ? + *config.output_stream : std::cout); if (command == "b") { - format_account acct_formatter(OUT(), format, config->display_predicate); + format_account acct_formatter(out, config.format, + config.display_predicate); sum_accounts(journal->master); - walk_accounts(journal->master, acct_formatter, sort_order.get()); + walk_accounts(journal->master, acct_formatter, config.sort_order.get()); acct_formatter.flush(); if (journal->master->data) { ACCT_DATA(journal->master)->value = ACCT_DATA(journal->master)->total; if (ACCT_DATA(journal->master)->dflags & ACCOUNT_TO_DISPLAY) { - std::string end_format = "--------------------\n"; - format.reset(end_format + f); - format.format_elements(OUT(), details_t(journal->master)); + out << "--------------------\n"; + config.format.format_elements(out, details_t(journal->master)); } } } else if (command == "E") { - format_equity acct_formatter(OUT(), format, nformat, - config->display_predicate); + format_equity acct_formatter(out, config.format, config.nformat, + config.display_predicate); sum_accounts(journal->master); - walk_accounts(journal->master, acct_formatter, sort_order.get()); + walk_accounts(journal->master, acct_formatter, config.sort_order.get()); acct_formatter.flush(); } @@ -626,12 +368,12 @@ int parse_and_report(int argc, char * argv[], char * envp[]) TIMER_STOP(report_gen); - // Save the cache, if need be + // Write out the binary cache, if need be TIMER_START(write_cache); - if (use_cache && cache_dirty && ! config->cache_file.empty()) { - std::ofstream stream(config->cache_file.c_str()); + if (config.use_cache && config.cache_dirty && ! config.cache_file.empty()) { + std::ofstream stream(config.cache_file.c_str()); write_binary_journal(stream, journal.get(), &journal->sources); } @@ -649,11 +391,12 @@ int main(int argc, char * argv[], char * envp[]) try { status = parse_and_report(argc, argv, envp); } + catch (error& err) { + std::cerr << "Error: " << err.what() << std::endl; + status = 1; + } catch (int& val) { -#if DEBUG_LEVEL >= BETA - shutdown(); -#endif - return val; + status = val; } #if DEBUG_LEVEL >= BETA |