summaryrefslogtreecommitdiff
path: root/main.cc
blob: 19e3b868a8a3a67d9c01766c7806d0e37feee85d (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
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <exception>
#include <iterator>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include "acconf.h"

#ifdef HAVE_UNIX_PIPES
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fdstream.hpp"
#endif

#include "ledger.h"

using namespace ledger;

int parse_and_report(config_t& config, report_t& report,
		     int argc, char * argv[], char * envp[])
{
  // Configure the terminus for value expressions

  ledger::terminus = current_moment;

  // Parse command-line arguments, and those set in the environment

  std::list<string> args;
  process_arguments(ledger::config_options, argc - 1, argv + 1, false, args);

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

  if (config.cache_file == "<none>")
    config.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);

#if 0
  TRACE(main, "Processing options and environment variables");
#endif

  process_environment(ledger::config_options,
		      const_cast<const char **>(envp), "LEDGER_");

#if 1
  // These are here for backwards compatability, but are deprecated.

  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

  const char * p    = std::getenv("HOME");
  string  home = p ? p : "";

  if (config.init_file.empty())
    config.init_file  = home + "/.ledgerrc";
  if (config.price_db.empty())
    config.price_db   = home + "/.pricedb";

  if (config.cache_file.empty())
    config.cache_file = home + "/.ledger-cache";

  if (config.data_file == config.cache_file)
    config.use_cache = false;
  DEBUG("ledger.config.cache", "2. use_cache = " << config.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);

  TRACE(main, string("Based on option settings, binary cache ") +
	(config.use_cache ? "WILL " : "will NOT ") + "be used");
#endif

  // 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) {
      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: ";
    }

    value_t result = guarded_compute(expr.get());
    std::cout << result.strip_annotations() << std::endl;

    return 0;
  }
  else if (command == "expr") {
    // this gets done below...
  }
  else {
    throw new error(string("Unrecognized command '") + command + "'");
  }

  // Parse initialization files, ledger data, price database, etc.

  std::auto_ptr<journal_t> journal(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

  // process the command word and its following arguments

  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");
  }

#if 0
  TRACE(options, string("Post-processing options ") +
	"for command \"" + command + "\"");
#endif

  report.process_options(command, arg, args.end());

#if 0
  // jww (2008-05-08): Is this disabled now?
  // If downloading is to be supported, configure the updater

  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

  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;
  }

  // Configure the output stream

#ifdef HAVE_UNIX_PIPES
  int status, pfd[2];		// Pipe file descriptors
#endif
  std::ostream * out = &std::cout;

  if (! report.output_file.empty()) {
    out = new ofstream(report.output_file);
  }
#ifdef HAVE_UNIX_PIPES
  else if (! config.pager.empty()) {
    status = pipe(pfd);
    if (status == -1)
      throw new error("Failed to create pipe");

    status = fork();
    if (status < 0) {
      throw new 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)
	perror("dup2");

      // Close unuseful file descriptors: the pipe's writing and
      // reading ends (the latter is not needed anymore, after the
      // duplication).
      close(pfd[1]);
      close(pfd[0]);

      // 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);
      perror("execl");
      exit(1);
    }
    else {			// parent
      close(pfd[0]);
      out = new boost::fdostream(pfd[1]);
    }
  }
#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: ";
    }

    value_t result = guarded_compute(expr.get());
    std::cout << result.strip_annotations() << std::endl;

    return 0;
  }

  // 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 (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 (command != "P" && command != "D")
      formatter->flush();

#if 0
    TRACE_POP(main, "Finished entry walk");
#endif
  }

  // For the balance and equity reports, output the sum totals.

  if (command == "b") {
#if 0
    TRACE_PUSH(main, "Walking journal accounts");
#endif

    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));
      }
    }
#if 0
    TRACE_POP(main, "Finished account walk");
#endif
  }
  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();

#if 0
    TRACE_POP(main, "Finished account walk");
#endif
  }

#if DEBUG_LEVEL >= BETA
#if 0
  { TRACE_PUSH(cleanup, "Cleaning up allocated memory");
#endif

  clear_transaction_xdata xact_cleaner;
  walk_entries(journal->entries, xact_cleaner);

  clear_account_xdata acct_cleaner;
  walk_accounts(*journal->master, acct_cleaner);

  if (! report.output_file.empty())
    delete out;

  for (std::list<item_handler<transaction_t> *>::iterator i
	 = formatter_ptrs.begin();
       i != formatter_ptrs.end();
       i++)
    delete *i;

#if 0
  TRACE_POP(cleanup, "Finished cleaning"); }
#endif
#endif

  // 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 0
    TRACE_POP(binary_cache, "Finished writing");
#endif
  }

#ifdef HAVE_UNIX_PIPES
  if (! config.pager.empty()) {
    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");
  }
#endif

  return 0;
}

int main(int argc, char * argv[], char * envp[])
{
  try {
#if DEBUG_LEVEL < BETA
    ledger::do_cleanup = false;
#endif
    config_t config;
    report_t report;
    ledger::config = &config;
    ledger::report = &report;
#if 0
    TRACE_PUSH(main, "Ledger starting");
#endif
    int status = parse_and_report(config, report, argc, argv, envp);
#if 0
    TRACE_POP(main, "Ledger done");
#endif
    return status;
  }
  catch (error * err) {
    std::cout.flush();
    // Push a null here since there's no file context
    if (err->context.empty() ||
	! dynamic_cast<xact_context *>(err->context.front()))
      err->context.push_front(new error_context(""));
    err->reveal_context(std::cerr, "Error");
    std::cerr << err->what() << std::endl;
    delete err;
    return 1;
  }
  catch (fatal * err) {
    std::cout.flush();
    // Push a null here since there's no file context
    if (err->context.empty() ||
	! dynamic_cast<xact_context *>(err->context.front()))
      err->context.push_front(new error_context(""));
    err->reveal_context(std::cerr, "Fatal");
    std::cerr << err->what() << std::endl;
    delete err;
    return 1;
  }
  catch (const std::exception& err) {
    std::cout.flush();
    std::cerr << "Error: " << err.what() << std::endl;
    return 1;
  }
  catch (int status) {
    return status;
  }
}

// main.cc ends here.