summaryrefslogtreecommitdiff
path: root/main.cc
blob: 17008cae479888e3a249a107e2053b175f16c1b8 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#include "ledger.h"
#include "error.h"
#include "valexpr.h"
#include "format.h"
#include "walk.h"
#include "option.h"
#include "timing.h"

#include <fstream>
#include <cstring>
#include <unistd.h>
#include <ctime>

namespace ledger {

static const std::string bal_fmt = "%20T  %2_%-n\n";
static const std::string reg_fmt
  = "%D %-.20P %-.22N %12.66t %12.80T\n\
%/                                %-.22N %12.66t %12.80T\n";
static const std::string plot_value_fmt = "%D %t\n";
static const std::string plot_total_fmt = "%D %T\n";
static const std::string print_fmt
  = "\n%D %X%C%P\n    %-34N  %12o\n%/    %-34N  %12o\n";
static const std::string equity_fmt
  = "\n%D %X%C%P\n%/    %-34N  %12t\n";


static long	   pricing_leeway = 24 * 3600;
static std::string price_db;
static bool        cache_dirty    = false;

void download_price_quote(commodity_t *	    commodity,
			  const std::time_t age,
			  const amount_t&   price,
			  const std::time_t moment)
{
  std::time_t now = std::time(NULL); // the time of the query

  if (! (commodity->flags & COMMODITY_STYLE_CONSULTED) &&
      std::difftime(now, moment) < pricing_leeway &&
      (! price || std::difftime(moment, age) > pricing_leeway)) {
    using namespace std;

    // Only consult the Internet once for any commodity
    commodity->flags |= COMMODITY_STYLE_CONSULTED;
    cache_dirty = true;

    char buf[256];
    buf[0] = '\0';

    if (FILE * fp = popen((string("getquote ") +
			   commodity->symbol).c_str(), "r")) {
      if (feof(fp) || ! fgets(buf, 255, fp)) {
	fclose(fp);
	return;
      }
      fclose(fp);
    }

    if (buf[0]) {
      char * p = strchr(buf, '\n');
      if (p) *p = '\0';

      amount_t current;
      current.parse(buf);

      commodity->add_price(now, current);

      if (! price_db.empty()) {
	char buf[128];
	strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now));
	ofstream database(price_db.c_str(), ios_base::out | ios_base::app);
	database << "P " << buf << " " << commodity->symbol << " "
		 << current << endl;
      }
    }
  }
}

} // namespace ledger


namespace {
  using namespace ledger;

  std::auto_ptr<journal_t>    journal(new journal_t);
  std::list<std::string>      files;
  std::auto_ptr<value_expr_t> sort_order;
  std::auto_ptr<std::ostream> output_stream;
  std::auto_ptr<interval_t>   report_interval;

#define OUT() (output_stream.get() ? *output_stream.get() : std::cout)

  std::string predicate;
  std::string display_predicate;
  std::string format_string;
  std::string sort_string;
  std::string value_expr = "a";
  std::string total_expr = "T";
  std::time_t interval_begin = 0;

  bool show_subtotals	  = true;
  bool show_expanded	  = false;
  bool show_related	  = false;
  bool show_all_related   = false;
  bool show_inverted	  = false;
  bool show_empty	  = false;
  bool days_of_the_week   = false;
  bool show_revalued      = false;
  bool show_revalued_only = false;
  bool use_cache          = false;
}

// jww (2004-08-13): fix this
static std::string ledger_cache_file()
{
  std::string cache_file;

  if (const char * p = std::getenv("LEDGER_CACHE")) {
    cache_file = p;
  }
  else if (const char * p = std::getenv("HOME")) {
    cache_file = p;
    cache_file += "/.ledger";
  }
  return cache_file;
}

static void show_version(std::ostream& out)
{
  out
    << "Ledger " << ledger::version << ", the command-line accounting tool\n\n"
    << "Copyright (c) 2003-2004, New Artisans LLC. All rights reserved.\n\n"
    << "This program is made available under the terms of the BSD Public\n"
    << "License.  See the LICENSE file included with the distribution for\n"
    << "details and disclaimer.\n";
}

static void show_help(std::ostream& out)
{
  out
    << "usage: ledger [options] COMMAND [options] [REGEXPS]\n\n"
    << "Basic options:\n"
    << "  -h        display this help text\n"
    << "  -v        display version information\n"
    << "  -f FILE   specify pathname of ledger data file\n\n"
    << "Report filtering:\n"
    << "  -a REGEX  specify an account regex for \"print\"\n"
    << "  -b DATE   specify a beginning date\n"
    << "  -e DATE   specify an ending date\n"
    << "  -c        do not show future entries (same as -e TODAY)\n"
    << "  -d DATE   specify a date mask ('-d mon', for all mondays)\n"
    << "  -C        show only cleared transactions and balances\n"
    << "  -U        show only uncleared transactions and balances\n"
    << "  -R        do not consider virtual transactions: real only\n"
    << "  -l EXPR   don't print entries for which EXPR yields 0\n\n"
    << "Output customization:\n"
    << "  -n        do not calculate parent account totals\n"
    << "  -s        show sub-accounts in balance, and splits in register\n"
    << "  -M        print register using monthly sub-totals\n"
    << "  -E        show accounts that total to zero\n"
    << "  -S EXPR   sort entry output based on EXPR\n\n"
    << "Commodity reporting:\n"
    << "  -T        report commodity totals, not their market value\n"
    << "  -B        report cost basis of commodities\n"
    << "  -V        report the market value of commodities\n"
    << "  -P FILE   sets the price database, for reading/writing price info\n"
    << "  -Q        download price information from the Internet\n"
    << "            (works by running \"getquote SYMBOL\")\n"
    << "  -L MINS   with -Q, fetch quotes only if data is older than MINS\n"
    << "  -p STR    specifies a direct commodity conversion: COMM=AMOUNT\n\n"
    << "Commands:\n"
    << "  balance   show balance totals\n"
    << "  register  display a register for ACCOUNT\n"
    << "  print     print all ledger entries\n"
    << "  entry     output a newly formed entry, based on arguments\n"
    << "  equity    output equity entries for specified accounts\n";
}


//////////////////////////////////////////////////////////////////////
//
// Basic options

DEF_OPT_HANDLERS();

OPT_BEGIN(help, "h", false) {
  show_help(std::cout);
  std::exit(0);
} OPT_END(help);

OPT_BEGIN(version, "v", false) {
  show_version(std::cout);
  std::exit(0);
} OPT_END(version);

OPT_BEGIN(init, "i:", true) {
  std::ifstream stream(optarg);
  parse_textual_journal(stream, journal.get(), journal->master);
} OPT_END(init);

OPT_BEGIN(file, "f:", true) {
  files.push_back(optarg);
  use_cache = false;
} OPT_END(file);

OPT_BEGIN(output, "o:", false) {
  if (std::string(optarg) != "-")
    output_stream.reset(new std::ofstream(optarg));
} OPT_END(output);

OPT_BEGIN(set_price, "p:", true) {
  if (char * p = std::strchr(optarg, '=')) {
    *p = ' ';
    std::string conversion = "C ";
    conversion += p;
    std::istringstream stream(conversion);
    parse_textual_journal(stream, journal.get(), journal->master);
  } else {
    std::cerr << "Error: Invalid price setting: " << optarg
	      << std::endl;
    std::exit(1);
  }
} OPT_END(set_price);

//////////////////////////////////////////////////////////////////////
//
// Report filtering

OPT_BEGIN(begin_date, "b:", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "(d>=[";
  predicate += optarg;
  predicate += "])";
} OPT_END(begin_date);

OPT_BEGIN(end_date, "e:", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "(d<[";
  predicate += optarg;
  predicate += "])";
} OPT_END(end_date);

OPT_BEGIN(current, "c", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "(d<t)";
} OPT_END(current);

OPT_BEGIN(cleared, "C", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "X";
} OPT_END(cleared);

OPT_BEGIN(uncleared, "U", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "!X";
} OPT_END(uncleared);

OPT_BEGIN(real, "R", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "R";
} OPT_END(real);

//////////////////////////////////////////////////////////////////////
//
// Output customization

OPT_BEGIN(format, "F:", false) {
  format_string = optarg;
} OPT_END(format);

OPT_BEGIN(date_format, "y:", false) {
  format_t::date_format = optarg;
} OPT_END(date_format);

OPT_BEGIN(empty, "E", false) {
  show_empty = true;
} OPT_END(empty);

OPT_BEGIN(collapse, "n", false) {
  show_subtotals = false;
} OPT_END(collapse);

OPT_BEGIN(show_all, "s", false) {
  show_expanded = true;
} OPT_END(show_all);

OPT_BEGIN(sort, "S:", false) {
  sort_string = optarg;
} OPT_END(sort);

OPT_BEGIN(related, "r", false) {
  show_related = true;
} OPT_END(related);

OPT_BEGIN(interval, "z:", false) {
  std::string str(optarg);
  std::istringstream stream(str);
  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))
	throw interval_expr_error("Could not parse 'from' date");
    }
  }
} OPT_END(interval);

OPT_BEGIN(weekly, "W", false) {
  report_interval.reset(new interval_t(604800, 0, 0));
} OPT_END(weekly);

OPT_BEGIN(dow, "w", false) {
  days_of_the_week = true;
} OPT_END(dow);

OPT_BEGIN(monthly, "M", false) {
  report_interval.reset(new interval_t(0, 1, 0));
} OPT_END(monthly);

OPT_BEGIN(yearly, "Y", false) {
  report_interval.reset(new interval_t(0, 0, 1));
} OPT_END(yearly);

OPT_BEGIN(limit, "l:", false) {
  if (! predicate.empty())
    predicate += "&";
  predicate += "(";
  predicate += optarg;
  predicate += ")";
} OPT_END(limit);

OPT_BEGIN(display, "d:", false) {
  if (! display_predicate.empty())
    display_predicate += "&";
  display_predicate += "(";
  display_predicate += optarg;
  display_predicate += ")";
} OPT_END(display);

OPT_BEGIN(value, "t:", false) {
  value_expr = optarg;
} OPT_END(value);

OPT_BEGIN(total, "T:", false) {
  total_expr = optarg;
} OPT_END(total);

OPT_BEGIN(value_data, "j", false) {
  value_expr    = "S" + value_expr;
  format_string = plot_value_fmt;
} OPT_END(value_data);

OPT_BEGIN(total_data, "J", false) {
  total_expr    = "S" + total_expr;
  format_string = plot_total_fmt;
} OPT_END(total_data);

//////////////////////////////////////////////////////////////////////
//
// Commodity reporting

OPT_BEGIN(price_db, "P:", false) {
  price_db = optarg;
} OPT_END(price_db);

OPT_BEGIN(price_exp, "L:", false) {
  pricing_leeway = std::atol(optarg) * 60;
} OPT_END(price_exp);

OPT_BEGIN(download, "Q", false) {
  commodity_t::updater = download_price_quote;
} OPT_END(download);

OPT_BEGIN(quantity, "O", false) {
  value_expr = "a";
  total_expr = "T";
} OPT_END(quantity);

OPT_BEGIN(basis, "B", false) {
  value_expr = "c";
  total_expr = "C";
} OPT_END(basis);

OPT_BEGIN(market, "V", false) {
  show_revalued = true;

  value_expr = "v";
  total_expr = "V";
} OPT_END(market);

OPT_BEGIN(gain, "G", false) {
  show_revalued      =
  show_revalued_only = true;

  value_expr = "c";
  total_expr = "G";
} OPT_END(gain);

OPT_BEGIN(average, "A", false) {
  value_expr = "a";
  total_expr = "MT";
} OPT_END(average);

OPT_BEGIN(deviation, "D", false) {
  value_expr = "a";
  total_expr = "DMT";
} OPT_END(deviation);

OPT_BEGIN(trend, "X", false) {
  value_expr = "a";
  total_expr = "MDMT";
} OPT_END(trend);

OPT_BEGIN(weighted_trend, "Z", false) {
  value_expr = "a";
  total_expr = "MD(MT/(1+(((t-d)/(30*86400))<0?0:((t-d)/(30*86400)))))";
} OPT_END(weighted_trend);


int main(int argc, char * argv[], char * envp[])
{
#ifdef DEBUG_ENABLED
  if (char * p = std::getenv("DEBUG_FILE")) {
    debug_stream      = new std::ofstream(p);
    free_debug_stream = true;
  }
#endif

  // A ledger data file must be specified

  TIMER_START(read_cache, "reading cache file");

  // jww (2004-08-13): use LEDGER_FILE
  use_cache = std::getenv("LEDGER") != NULL;

  if (use_cache) {
    // jww (2004-08-13): fix this
    for (int i = 0; i < argc; i++)
      if (std::strcmp(argv[i], "-f") == 0 ||
	  std::strcmp(argv[i], "--file") == 0) {
	use_cache = false;
	break;
      }

    cache_dirty = true;

    if (use_cache) {
      std::string cache_file = ledger_cache_file();
      if (! cache_file.empty() &&
	  access(cache_file.c_str(), R_OK) != -1) {
	std::ifstream stream(cache_file.c_str());
	if (! read_binary_journal(stream, std::getenv("LEDGER"),
				  journal.get())) {
	  // Throw away what's been read, and create a new journal
	  journal.reset(new journal_t);
	} else {
	  cache_dirty = false;
	}
      }
    }
  }

  TIMER_STOP(read_cache);

  // Parse the command-line options

  TIMER_START(process_args, "processing command-line arguments");

  std::list<std::string> args;

  process_arguments(argc, argv, false, args);

  if (args.empty()) {
    show_help(std::cerr);
    return 1;
  }
  std::list<std::string>::iterator arg = args.begin();

  TIMER_STOP(process_args);

  // Process options from the environment

  TIMER_START(process_env, "processing environment");

  process_environment(envp, "LEDGER_");

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

  TIMER_STOP(process_env);

  // Read the ledger file, unless we already read it from the cache

  TIMER_START(parse_files, "parsing ledger files");

  if (! use_cache || cache_dirty) {
    int entry_count = 0;

    try {
      if (files.empty()) {
	if (char * p = std::getenv("LEDGER"))
	  for (p = std::strtok(p, ":"); p; p = std::strtok(NULL, ":"))
	    entry_count += parse_journal_file(p, journal.get());
      } else {
	for (std::list<std::string>::iterator i = files.begin();
	     i != files.end(); i++) {
	  char buf[4096];
	  char * p = buf;
	  std::strcpy(p, (*i).c_str());
	  entry_count += parse_journal_file(p, journal.get());
	}
      }

      // Read prices from their own ledger file, after all others have
      // been read.

      if (! price_db.empty()) {
	const char * path = price_db.c_str();
	std::ifstream db(path);
	journal->sources.push_back(path);
	entry_count += parse_textual_journal(db, 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, "configuring based on 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.

    std::list<std::string>::iterator i = args.begin();
    for (; i != args.end(); i++)
      if (*i == "--")
	break;

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

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

  // Compile the predicates

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

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

  // Compile the sorting criteria

  if (! sort_string.empty()) {
    try {
      std::istringstream stream(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(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(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

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

  // Compile the format strings

  const char * f;
  if (! format_string.empty())
    f = 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, "generation of final report");

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

    format_account acct_formatter(OUT(), format, display_predicate);
    if (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(), predicate));
    walk_entries(journal->entries, *formatter.get());

    format_equity acct_formatter(OUT(), format, nformat, 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(),
					    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(), 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 (show_revalued)
      formatter.reset(new changed_value_transactions(formatter.release(),
						     show_revalued_only));

    // collapse_transactions causes entries with multiple transactions
    // to appear as entries with a subtotaled transaction for each
    // commodity used.
    if (! 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 (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 (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 (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(), 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());

#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, "writing cache file");

  if (use_cache && cache_dirty) {
    std::string cache_file = ledger_cache_file();
    if (! cache_file.empty()) {
      assert(std::getenv("LEDGER"));
      std::ofstream stream(cache_file.c_str());
      write_binary_journal(stream, journal.get(), std::getenv("LEDGER"));
    }
  }

  TIMER_STOP(write_cache);

  return 0;
}

// main.cc ends here.