summaryrefslogtreecommitdiff
path: root/option.cc
diff options
context:
space:
mode:
Diffstat (limited to 'option.cc')
-rw-r--r--option.cc448
1 files changed, 158 insertions, 290 deletions
diff --git a/option.cc b/option.cc
index 624a8f0b..d4de29de 100644
--- a/option.cc
+++ b/option.cc
@@ -1,79 +1,156 @@
-#include "option.h"
-#include "config.h"
-#include "report.h"
-#include "debug.h"
-#include "error.h"
+/*
+ * Copyright (c) 2003-2008, 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 <iostream>
-#include <cstdarg>
-#include <cstdlib>
-#include <unistd.h>
+#include "option.h"
-#include "util.h"
+namespace ledger {
namespace {
- inline void process_option(option_t * opt, const char * arg = NULL) {
- if (! opt->handled) {
- try {
- opt->handler(arg);
- }
- catch (error * err) {
- err->context.push_back
- (new error_context
- (std::string("While parsing option '--") + opt->long_opt +
- "'" + (opt->short_opt != '\0' ?
- (std::string(" (-") + opt->short_opt + "):") : ":")));
- throw err;
- }
- opt->handled = true;
- }
- }
+ typedef tuple<expr::ptr_op_t, bool> op_bool_tuple;
- option_t * search_options(option_t * array, const char * name)
+ op_bool_tuple find_option(expr::scope_t& scope, const string& name)
{
- int first = 0;
- int last = CONFIG_OPTIONS_SIZE;
- while (first <= last) {
- int mid = (first + last) / 2; // compute mid point.
-
- int result;
- if ((result = (int)name[0] - (int)array[mid].long_opt[0]) == 0)
- result = std::strcmp(name, array[mid].long_opt);
-
- if (result > 0)
- first = mid + 1; // repeat search in top half.
- else if (result < 0)
- last = mid - 1; // repeat search in bottom half.
+ char buf[128];
+ std::strcpy(buf, "option_");
+ char * p = &buf[7];
+ for (const char * q = name.c_str(); *q; q++) {
+ if (*q == '-')
+ *p++ = '_';
else
- return &array[mid];
+ *p++ = *q;
}
- return NULL;
+ *p = '\0';
+
+ expr::ptr_op_t op = scope.lookup(buf);
+ if (op)
+ return op_bool_tuple(op, false);
+
+ *p++ = '_';
+ *p = '\0';
+
+ return op_bool_tuple(scope.lookup(buf), true);
}
- option_t * search_options(option_t * array, const char letter)
+ op_bool_tuple find_option(expr::scope_t& scope, const char letter)
{
- for (int i = 0; i < CONFIG_OPTIONS_SIZE; i++)
- if (letter == array[i].short_opt)
- return &array[i];
- return NULL;
+ char buf[10];
+ std::strcpy(buf, "option_");
+ buf[7] = letter;
+ buf[8] = '\0';
+
+ expr::ptr_op_t op = scope.lookup(buf);
+ if (op)
+ return op_bool_tuple(op, false);
+
+ buf[8] = '_';
+ buf[9] = '\0';
+
+ return op_bool_tuple(scope.lookup(buf), true);
+ }
+
+ void process_option(const expr::function_t& opt,
+ expr::scope_t& scope, const char * arg)
+ {
+#if 0
+ try {
+#endif
+ expr::call_scope_t args(scope);
+ if (arg)
+ args.push_back(value_t(arg, true));
+
+ opt(args);
+#if 0
+ }
+ catch (error * err) {
+ err->context.push_back
+ (new error_context
+ (string("While parsing option '--") + opt->long_opt +
+ "'" + (opt->short_opt != '\0' ?
+ (string(" (-") + opt->short_opt + "):") : ":")));
+ throw err;
+ }
+#endif
}
}
-bool process_option(option_t * options, const std::string& name,
+void process_option(const string& name, expr::scope_t& scope,
const char * arg)
{
- option_t * opt = search_options(options, name.c_str());
- if (opt) {
- process_option(opt, arg);
- return true;
- }
- return false;
+ op_bool_tuple opt(find_option(scope, name));
+ if (opt.get<0>())
+ process_option(opt.get<0>()->as_function(), scope, arg);
}
-void process_arguments(option_t * options, int argc, char ** argv,
- const bool anywhere, std::list<std::string>& args)
+void process_environment(const char ** envp, const string& tag,
+ expr::scope_t& scope)
+{
+ const char * tag_p = tag.c_str();
+ unsigned int tag_len = tag.length();
+
+ for (const char ** p = envp; *p; p++)
+ if (! tag_p || std::strncmp(*p, tag_p, tag_len) == 0) {
+ char buf[128];
+ char * r = buf;
+ const char * q;
+ for (q = *p + tag_len;
+ *q && *q != '=' && r - buf < 128;
+ q++)
+ if (*q == '_')
+ *r++ = '-';
+ else
+ *r++ = std::tolower(*q);
+ *r = '\0';
+
+ if (*q == '=') {
+#if 0
+ try {
+#endif
+ process_option(string(buf), scope, q + 1);
+#if 0
+ }
+ catch (error * err) {
+ err->context.push_back
+ (new error_context
+ (string("While parsing environment variable option '") +
+ *p + "':"));
+ throw err;
+ }
+#endif
+ }
+ }
+}
+
+void process_arguments(int argc, char ** argv, const bool anywhere,
+ expr::scope_t& scope, std::list<string>& args)
{
- int index = 0;
for (char ** i = argv; *i; i++) {
if ((*i)[0] != '-') {
if (anywhere) {
@@ -87,7 +164,6 @@ void process_arguments(option_t * options, int argc, char ** argv,
}
// --long-option or -s
- again:
if ((*i)[1] == '-') {
if ((*i)[2] == '\0')
break;
@@ -99,109 +175,52 @@ void process_arguments(option_t * options, int argc, char ** argv,
value = p;
}
- option_t * opt = search_options(options, name);
- if (! opt)
- throw new option_error(std::string("illegal option --") + name);
+ op_bool_tuple opt(find_option(scope, name));
+ if (! opt.get<0>())
+ throw_(option_error, "illegal option --" << name);
- if (opt->wants_arg && value == NULL) {
+ if (opt.get<1>() && value == NULL) {
value = *++i;
if (value == NULL)
- throw new option_error(std::string("missing option argument for --") +
- name);
+ throw_(option_error, "missing option argument for --" << name);
}
- process_option(opt, value);
+ process_option(opt.get<0>()->as_function(), scope, value);
}
else if ((*i)[1] == '\0') {
- throw new option_error(std::string("illegal option -"));
+ throw_(option_error, "illegal option -");
}
else {
- std::list<option_t *> opt_queue;
+ typedef tuple<expr::ptr_op_t, bool, char> op_bool_char_tuple;
+
+ std::list<op_bool_char_tuple> option_queue;
int x = 1;
for (char c = (*i)[x]; c != '\0'; x++, c = (*i)[x]) {
- option_t * opt = search_options(options, c);
- if (! opt)
- throw new option_error(std::string("illegal option -") + c);
- opt_queue.push_back(opt);
+ op_bool_tuple opt(find_option(scope, c));
+ if (! opt.get<0>())
+ throw_(option_error, "illegal option -" << c);
+
+ option_queue.push_back
+ (op_bool_char_tuple(opt.get<0>(), opt.get<1>(), c));
}
- for (std::list<option_t *>::iterator o = opt_queue.begin();
- o != opt_queue.end(); o++) {
+ foreach (op_bool_char_tuple& o, option_queue) {
char * value = NULL;
- if ((*o)->wants_arg) {
+ if (o.get<1>()) {
value = *++i;
if (value == NULL)
- throw new option_error(std::string("missing option argument for -") +
- (*o)->short_opt);
+ throw_(option_error,
+ "missing option argument for -" << o.get<2>());
}
- process_option(*o, value);
+ process_option(o.get<0>()->as_function(), scope, value);
}
}
-
- next:
- ;
}
}
-void process_environment(option_t * options, const char ** envp,
- const std::string& tag)
-{
- const char * tag_p = tag.c_str();
- unsigned int tag_len = tag.length();
-
- for (const char ** p = envp; *p; p++)
- if (! tag_p || std::strncmp(*p, tag_p, tag_len) == 0) {
- char buf[128];
- char * r = buf;
- const char * q;
- for (q = *p + tag_len;
- *q && *q != '=' && r - buf < 128;
- q++)
- if (*q == '_')
- *r++ = '-';
- else
- *r++ = std::tolower(*q);
- *r = '\0';
-
- if (*q == '=') {
- try {
- process_option(options, buf, q + 1);
- }
- catch (error * err) {
- err->context.pop_back();
- err->context.push_back
- (new error_context
- (std::string("While parsing environment variable option '") +
- *p + "':"));
- throw err;
- }
- }
- }
-}
-
-//////////////////////////////////////////////////////////////////////
-
-namespace ledger {
-
-config_t * config = NULL;
-report_t * report = NULL;
-
-static void show_version(std::ostream& out)
-{
- out << "Ledger " << ledger::version << ", the command-line accounting tool";
- out << "\n\nCopyright (c) 2003-2008, John Wiegley. All rights reserved.\n\n\
-This program is made available under the terms of the BSD Public License.\n\
-See LICENSE file included with the distribution for details and disclaimer.\n";
- out << "\n(modules: gmp, pcre";
-#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
- out << ", xml";
-#endif
-#ifdef HAVE_LIBOFX
- out << ", ofx";
-#endif
- out << ")\n";
-}
+} // namespace ledger
+#if 0
void option_full_help(std::ostream& out)
{
out << "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\
@@ -911,155 +930,4 @@ namespace {
}
}
}
-
-OPT_BEGIN(set_price, ":") {
- std::string arg(optarg);
- std::string::size_type beg = 0;
- for (std::string::size_type pos = arg.find(';');
- pos != std::string::npos;
- beg = pos + 1, pos = arg.find(';', beg))
- parse_price_setting(std::string(arg, beg, pos - beg).c_str());
- parse_price_setting(std::string(arg, beg).c_str());
-} OPT_END(set_price);
-
-OPT_BEGIN(performance, "g") {
- ledger::amount_expr = "@P(@a,@m)-@b";
- ledger::total_expr = "@P(@O,@m)-@B";
-} OPT_END(performance);
-
-OPT_BEGIN(gain, "G") {
- report->show_revalued =
- report->show_revalued_only = true;
-
- ledger::amount_expr = "@a";
- ledger::total_expr = "@G";
-} OPT_END(gain);
-
-static std::string expand_value_expr(const std::string& tmpl,
- const std::string& expr)
-{
- std::string xp = tmpl;
- for (std::string::size_type i = xp.find('#');
- i != std::string::npos;
- i = xp.find('#'))
- xp = (std::string(xp, 0, i) + "(" + expr + ")" +
- std::string(xp, i + 1));
- return xp;
-}
-
-OPT_BEGIN(average, "A") {
- ledger::total_expr = expand_value_expr("@A(#)", ledger::total_expr.expr);
-} OPT_END(average);
-
-OPT_BEGIN(deviation, "D") {
- ledger::total_expr = expand_value_expr("@t-@A(#)", ledger::total_expr.expr);
-} OPT_END(deviation);
-
-OPT_BEGIN(percentage, "%") {
- ledger::total_expr = expand_value_expr("^#&{100.0%}*(#/^#)",
- ledger::total_expr.expr);
-} OPT_END(percentage);
-
-//////////////////////////////////////////////////////////////////////
-
-option_t config_options[CONFIG_OPTIONS_SIZE] = {
- { "abbrev-len", '\0', true, opt_abbrev_len, false },
- { "account", 'a', true, opt_account, false },
- { "actual", 'L', false, opt_actual, false },
- { "add-budget", '\0', false, opt_add_budget, false },
- { "amount", 't', true, opt_amount, false },
- { "amount-data", 'j', false, opt_amount_data, false },
- { "ansi", '\0', false, opt_ansi, false },
- { "ansi-invert", '\0', false, opt_ansi_invert, false },
- { "average", 'A', false, opt_average, false },
- { "balance-format", '\0', true, opt_balance_format, false },
- { "base", '\0', false, opt_base, false },
- { "basis", 'B', false, opt_basis, false },
- { "begin", 'b', true, opt_begin, false },
- { "budget", '\0', false, opt_budget, false },
- { "by-payee", 'P', false, opt_by_payee, false },
- { "cache", '\0', true, opt_cache, false },
- { "cleared", 'C', false, opt_cleared, false },
- { "code-as-payee", '\0', false, opt_code_as_payee, false },
- { "collapse", 'n', false, opt_collapse, false },
- { "comm-as-payee", 'x', false, opt_comm_as_payee, false },
- { "cost", '\0', false, opt_basis, false },
- { "current", 'c', false, opt_current, false },
- { "daily", '\0', false, opt_daily, false },
- { "date-format", 'y', true, opt_date_format, false },
- { "debug", '\0', true, opt_debug, false },
- { "descend", '\0', true, opt_descend, false },
- { "descend-if", '\0', true, opt_descend_if, false },
- { "deviation", 'D', false, opt_deviation, false },
- { "display", 'd', true, opt_display, false },
- { "dow", '\0', false, opt_dow, false },
- { "download", 'Q', false, opt_download, false },
- { "effective", '\0', false, opt_effective, false },
- { "empty", 'E', false, opt_empty, false },
- { "end", 'e', true, opt_end, false },
- { "equity-format", '\0', true, opt_equity_format, false },
- { "file", 'f', true, opt_file, false },
- { "forecast", '\0', true, opt_forecast, false },
- { "format", 'F', true, opt_format, false },
- { "full-help", 'H', false, opt_full_help, false },
- { "gain", 'G', false, opt_gain, false },
- { "head", '\0', true, opt_head, false },
- { "help", 'h', false, opt_help, false },
- { "help-calc", '\0', false, opt_help_calc, false },
- { "help-comm", '\0', false, opt_help_comm, false },
- { "help-disp", '\0', false, opt_help_disp, false },
- { "init-file", 'i', true, opt_init_file, false },
- { "input-date-format", '\0', true, opt_input_date_format, false },
- { "limit", 'l', true, opt_limit, false },
- { "lot-dates", '\0', false, opt_lot_dates, false },
- { "lot-prices", '\0', false, opt_lot_prices, false },
- { "lot-tags", '\0', false, opt_lot_tags, false },
- { "lots", '\0', false, opt_lots, false },
- { "market", 'V', false, opt_market, false },
- { "monthly", 'M', false, opt_monthly, false },
- { "no-cache", '\0', false, opt_no_cache, false },
- { "only", '\0', true, opt_only, false },
- { "output", 'o', true, opt_output, false },
- { "pager", '\0', true, opt_pager, false },
- { "percentage", '%', false, opt_percentage, false },
- { "performance", 'g', false, opt_performance, false },
- { "period", 'p', true, opt_period, false },
- { "period-sort", '\0', true, opt_period_sort, false },
- { "plot-amount-format", '\0', true, opt_plot_amount_format, false },
- { "plot-total-format", '\0', true, opt_plot_total_format, false },
- { "price", 'I', false, opt_price, false },
- { "price-db", '\0', true, opt_price_db, false },
- { "price-exp", 'Z', true, opt_price_exp, false },
- { "prices-format", '\0', true, opt_prices_format, false },
- { "print-format", '\0', true, opt_print_format, false },
- { "quantity", 'O', false, opt_quantity, false },
- { "quarterly", '\0', false, opt_quarterly, false },
- { "real", 'R', false, opt_real, false },
- { "reconcile", '\0', true, opt_reconcile, false },
- { "reconcile-date", '\0', true, opt_reconcile_date, false },
- { "register-format", '\0', true, opt_register_format, false },
- { "related", 'r', false, opt_related, false },
- { "set-price", '\0', true, opt_set_price, false },
- { "sort", 'S', true, opt_sort, false },
- { "sort-all", '\0', true, opt_sort_all, false },
- { "sort-entries", '\0', true, opt_sort_entries, false },
- { "subtotal", 's', false, opt_subtotal, false },
- { "tail", '\0', true, opt_tail, false },
- { "total", 'T', true, opt_total, false },
- { "total-data", 'J', false, opt_total_data, false },
- { "totals", '\0', false, opt_totals, false },
- { "trace", '\0', false, opt_trace, false },
- { "truncate", '\0', true, opt_truncate, false },
- { "unbudgeted", '\0', false, opt_unbudgeted, false },
- { "uncleared", 'U', false, opt_uncleared, false },
- { "verbose", '\0', false, opt_verbose, false },
- { "version", 'v', false, opt_version, false },
- { "weekly", 'W', false, opt_weekly, false },
- { "wide", 'w', false, opt_wide, false },
- { "wide-register-format", '\0', true, opt_wide_register_format, false },
- { "write-hdr-format", '\0', true, opt_write_hdr_format, false },
- { "write-xact-format", '\0', true, opt_write_xact_format, false },
- { "yearly", 'Y', false, opt_yearly, false },
-};
-
-} // namespace ledger
+#endif