summaryrefslogtreecommitdiff
path: root/main.cc
blob: fd9342dfd5956adfe4caaca44e451664de544144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "ledger.h"
#include "error.h"
#include "valexpr.h"
#include "format.h"
#include "walk.h"
#include "quotes.h"
#include "option.h"
#include "config.h"
#include "timing.h"

using namespace ledger;

#include <iostream>
#include <fstream>
#include <memory>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstring>
#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(read_cache,	    "reading cache file");
}

int main(int argc, char * argv[], char * envp[])
{
  std::auto_ptr<journal_t> journal(new journal_t);

  // Parse command-line arguments

  TIMER_START(process_args);

  strings_list args;
  process_arguments(argc, argv, false, args);

  if (args.empty()) {
    option_help(std::cerr);
    return 1;
  }
  strings_list::iterator arg = args.begin();

  TIMER_STOP(process_args);

  const bool use_cache = config->files.empty();

  // Process options from the environment

  TIMER_START(process_env);

  process_environment(envp, "LEDGER_");

  if (const char * p = std::getenv("LEDGER"))
    process_option("file", p);
  if (const char * p = std::getenv("PRICE_HIST"))
    process_option("price-db", p);
  if (const char * p = std::getenv("PRICE_EXP"))
    process_option("price-exp", p);

  TIMER_STOP(process_env);

  // Parse ledger files

  TIMER_START(parse_files);

  int entry_count = 0;

  try {
    if (! config->init_file.empty())
      if (parse_journal_file(config->init_file, journal.get()))
	throw error("Entries not allowed in initialization file");

    if (use_cache && ! config->cache_file.empty()) {
      journal->sources.clear();		// remove init_file
      entry_count += parse_journal_file(config->cache_file, journal.get());
      journal->sources.pop_front();	// remove cache_file

      strings_list exceptions;
      std::set_difference(journal->sources.begin(), journal->sources.end(),
			  config->files.begin(), config->files.end(),
			  exceptions.begin());

      if (entry_count == 0 || exceptions.size() > 0) {
	journal.reset(new journal_t);
	entry_count = 0;
      } else {
	cache_dirty = false;
      }
    }

    if (entry_count == 0)
      for (strings_list::iterator i = config->files.begin();
	   i != config->files.end();
	   i++)
	entry_count += parse_journal_file(*i, journal.get());

    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);
	parse_textual_journal(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;
  }

  TIMER_STOP(parse_files);

  // Read the command word, and then check and simplify it

  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")
    command = "r";
  else if (command == "print" || command == "p")
    command = "p";
  else if (command == "entry")
    command = "e";
  else if (command == "equity")
    command = "E";
  else {
    std::cerr << "Error: Unrecognized command '" << command << "'."
	      << std::endl;
    return 1;
  }

  // Process the remaining command-line arguments

  std::auto_ptr<entry_t> new_entry;
  if (command == "e") {
    new_entry.reset(journal->derive_entry(arg, args.end()));
  } else {
    // Treat the remaining command-line arguments as regular
    // expressions, used for refining report results.

    strings_list::iterator i = args.begin();
    for (; i != args.end(); i++)
      if (*i == "--")
	break;

    const std::string pred = regexps_to_predicate(arg, i);
    if (! pred.empty()) {
      if (! config->predicate.empty())
	config->predicate += "&";
      config->predicate += pred;
    }

    if (i != args.end()) {
      const std::string pred = regexps_to_predicate(i, args.end(), false);
      if (! pred.empty()) {
	if (! config->predicate.empty())
	  config->predicate += "&";
	config->predicate += pred;
      }
    }
  }

  // Compile the predicates

  if (config->display_predicate.empty()) {
    if (command == "b") {
      if (! config->show_empty)
	config->display_predicate = "T";

      if (! config->show_expanded && config->predicate.empty()) {
	if (! config->display_predicate.empty())
	  config->display_predicate += "&";
	config->display_predicate += "!n";
      }
    }
    else if (command == "E") {
      config->display_predicate = "a";
    }
  }

  // Compile the 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 meaning of %t and %T, used in format strings

  try {
    format_t::value_expr.reset(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.reset(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;
  }

  // Configure some option depending on the report type

  bool show_all_related = false;

  if (command == "p" || command == "e") {
    config->show_related = show_all_related = true;
    config->show_expanded = true;
  }
  else if (command == "E") {
    config->show_expanded = true;
  }
  else if (config->show_related) {
    if (command == "r")
      config->show_inverted = true;
    else
      show_all_related = true;
  }

  // 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);

  // Setup a few local and global variables, depending on the config
  // settings.

  std::auto_ptr<std::ostream> output_stream;
  std::auto_ptr<interval_t>   report_interval;
  std::time_t		      interval_begin;

  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.get() : std::cout)

  if (! config->interval_text.empty()) {
    std::istringstream stream(config->interval_text);
    report_interval.reset(interval_t::parse(stream));
    if (! stream.eof()) {
      std::string word;
      stream >> word;
      if (word == "from") {
	stream >> word;
	if (! parse_date(word.c_str(), &interval_begin)) {
	  std::cerr << "Error in report interval: "
		    << "Could not parse 'from' date"
		    << std::endl;
	  return 1;
	}
      }
    }
  }

  if (! config->date_format.empty())
    format_t::date_format = config->date_format;

  // Walk the entries based on the report type and the options

  TIMER_START(report_gen);

  if (command == "b") {
    std::auto_ptr<item_handler<transaction_t> > formatter;
    formatter.reset(new add_to_account_value);
    if (config->show_related)
      formatter.reset(new related_transactions(formatter.release(),
					       show_all_related));
    formatter.reset(new filter_transactions(formatter.release(),
					    config->predicate));
    walk_entries(journal->entries, *formatter.get());

    format_account acct_formatter(OUT(), format, config->display_predicate);
    if (config->show_subtotals)
      sum_accounts(journal->master);
    walk_accounts(journal->master, acct_formatter, sort_order.get());

    if (format_account::disp_subaccounts_p(journal->master)) {
      std::string end_format = "--------------------\n";
      format.reset(end_format + f);
      format.format_elements(OUT(), details_t(journal->master));
    }
  }
  else if (command == "E") {
    std::auto_ptr<item_handler<transaction_t> > formatter;
    formatter.reset(new add_to_account_value);
    formatter.reset(new filter_transactions(formatter.release(),
					    config->predicate));
    walk_entries(journal->entries, *formatter.get());

    format_equity acct_formatter(OUT(), format, nformat,
				 config->display_predicate);
    sum_accounts(journal->master);
    walk_accounts(journal->master, acct_formatter, sort_order.get());
  }
  else if (command == "e") {
    format_transactions formatter(OUT(), format, nformat);
    walk_transactions(new_entry->transactions, formatter);
  }
  else {
    std::auto_ptr<item_handler<transaction_t> > formatter;

    // 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.

    // format_transactions write each transaction received to the
    // output stream.
    formatter.reset(new format_transactions(OUT(), format, nformat));

    // 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()));

    // filter_transactions will only pass through transactions
    // matching the `display_predicate'.
    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));

    // 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_subtotals)
      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_expanded)
      formatter.reset(new subtotal_transactions(formatter.release()));
    else if (report_interval.get())
      formatter.reset(new interval_transactions(formatter.release(),
						*report_interval.get(),
						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(),
					       show_all_related));

    // This filter_transactions will only pass through transactions
    // matching the `predicate'.
    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.
    walk_entries(journal->entries, *formatter.get());

    formatter->flush();

#ifdef DEBUG_ENABLED
    // The transaction display flags (dflags) are not recorded in the
    // binary cache, and only need to be cleared if the transactions
    // are to be displayed a second time.
    clear_display_flags cleanup;
    walk_entries(journal->entries, cleanup);
#endif
  }

  TIMER_STOP(report_gen);

  // Save the 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());
    write_binary_journal(stream, journal.get(), &journal->sources);
  }

  TIMER_STOP(write_cache);

  return 0;
}

// main.cc ends here.