summaryrefslogtreecommitdiff
path: root/src/session.cc
blob: 42e02c7528b790b61372e08fcd3b873a70181cbd (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
/*
 * Copyright (c) 2003-2009, 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 "session.h"
#include "report.h"
#include "handler.h"
#include "iterators.h"
#include "filters.h"
#include "textual.h"

namespace ledger {

session_t * session_t::current = NULL;

#if 0
boost::mutex session_t::session_mutex;
#endif

void set_session_context(session_t * session)
{
#if 0
  session_t::session_mutex.lock();
#endif

  if (session && ! session_t::current) {
    session_t::initialize();
  }
  else if (! session && session_t::current) {
    session_t::shutdown();
#if 0
    session_t::session_mutex.unlock();
#endif
  }

  session_t::current = session;
}

void release_session_context()
{
#if 0
  session_t::session_mutex.unlock();
#endif
}

session_t::session_t()
  : next_data_file_from_command_line(false),
    saw_data_file_from_command_line(false),
    next_price_db_from_command_line(false),
    saw_price_db_from_command_line(false),

    register_format
    ("%-.9(date) %-.20(payee) %-.23(account(23)) %!12(print_balance(amount_expr, 12, 67)) "
     "%!12(print_balance(display_total, 12, 80, true))\n%/"
     "%31|%-.23(account(23)) %!12(print_balance(amount_expr, 12, 67)) "
     "%!12(print_balance(display_total, 12, 80, true))\n"),
    wide_register_format
    ("%-.9D  %-.35P %-.39A %22.108t %!22.132T\n%/"
     "%48|%-.38A %22.108t %!22.132T\n"),
    print_format
    ("%(date)%(cleared ? \" *\" : (uncleared ? \"\" : \" !\"))%(code ? \" (\" + code + \")\" : \"\") %(payee)%(entry.comment | \"\")\n    %-34(account)  %12(amount)%(comment | \"\")\n%/    %-34(account)  %12(amount)%(comment | \"\")\n%/\n"),
    balance_format
    ("%20(display_total)  %(depth_spacer)%-(partial_account)\n"),
    equity_format
    ("\n%D %Y%C%P\n%/    %-34W  %12t\n"),
    plot_amount_format
    ("%D %(S(t))\n"),
    plot_total_format
    ("%D %(S(T))\n"),
    write_hdr_format
    ("%d %Y%C%P\n"),
    write_xact_format
    ("    %-34W  %12o%n\n"),
    prices_format
    ("%[%Y/%m/%d %H:%M:%S %Z]   %-10A %12t %12T\n"),
    pricesdb_format
    ("P %[%Y/%m/%d %H:%M:%S] %A %t\n"),

    pricing_leeway(24 * 3600),

    download_quotes(false),
    use_cache(true),
    cache_dirty(false),

    now(now),

#if 0
    elision_style(ABBREVIATE),
#endif
    abbrev_length(2),

    ansi_codes(false),
    ansi_invert(false),

    master(new account_t(NULL, ""))
{
  TRACE_CTOR(session_t, "");

  optional<path> home;
  if (const char * home_var = std::getenv("HOME"))
    home = home_var;

  init_file  = home ? *home / ".ledgerrc"	: "./.ledgerrc";
  price_db   = home ? *home / ".pricedb"	: "./.pricedb";
  cache_file = home ? *home / ".ledger-cache" : "./.ledger-cache";

  register_parser(new textual_parser_t);
}

session_t::~session_t()
{
  TRACE_DTOR(session_t);
}

std::size_t session_t::read_journal(journal_t&	  journal,
				    std::istream& in,
				    const path&	  pathname,
				    account_t *   master)
{
  if (! master)
    master = journal.master;

  foreach (journal_t::parser_t& parser, parsers)
    if (parser.test(in))
      return parser.parse(in, *this, journal, master, &pathname);

  return 0;
}

std::size_t session_t::read_journal(journal_t&	journal,
				    const path& pathname,
				    account_t * master)
{
  journal.sources.push_back(pathname);

  if (! exists(pathname))
    throw_(std::logic_error, "Cannot read file" << pathname);

  ifstream stream(pathname);
  return read_journal(journal, stream, pathname, master);
}

void session_t::read_init()
{
  if (! init_file)
    return;

  if (! exists(*init_file))
    throw_(std::logic_error, "Cannot read init file" << *init_file);

  TRACE_START(init, 1, "Read initialization file");

  ifstream init(*init_file);

  journal_t temp;
  if (read_journal(temp, *init_file) > 0 ||
      temp.auto_entries.size() > 0 || temp.period_entries.size() > 0) {
    throw_(parse_error,
	   "Entries found in initialization file '" << init_file << "'");
  }

  TRACE_FINISH(init, 1);
}

std::size_t session_t::read_data(journal_t&    journal,
				 const string& master_account)
{
  if (data_files.empty())
    throw_(parse_error, "No journal file was specified (please use -f)");

  TRACE_START(session_parser, 1, "Parsed journal file");

  std::size_t entry_count = 0;

  DEBUG("ledger.cache", "3. use_cache = " << use_cache);

  if (use_cache && cache_file) {
    DEBUG("ledger.cache", "using_cache " << cache_file->string());
    cache_dirty = true;
    if (exists(*cache_file)) {
      push_variable<optional<path> >
	save_price_db(journal.price_db, price_db);

      entry_count += read_journal(journal, *cache_file);
      if (entry_count > 0)
	cache_dirty = false;
    }
  }

  if (entry_count == 0) {
    account_t * acct = NULL;
    if (! master_account.empty())
      acct = journal.find_account(master_account);

    journal.price_db = price_db;
    if (journal.price_db && exists(*journal.price_db)) {
      if (read_journal(journal, *journal.price_db)) {
	throw_(parse_error, "Entries not allowed in price history file");
      } else {
	DEBUG("ledger.cache",
	      "read price database " << journal.price_db->string());
	journal.sources.pop_back();
      }
    }


    foreach (const path& pathname, data_files) {
      DEBUG("ledger.cache", "rejected cache, parsing " << pathname.string());
      if (pathname == "-") {
	use_cache = false;
	journal.sources.push_back("/dev/stdin");

	// To avoid problems with stdin and pipes, etc., we read the entire
	// file in beforehand into a memory buffer, and then parcel it out
	// from there.
	std::ostringstream buffer;

	while (std::cin.good() && ! std::cin.eof()) {
	  static char line[8192];
	  std::cin.read(line, 8192);
	  std::streamsize count = std::cin.gcount();
	  buffer.write(line, count);
	}
	buffer.flush();

	std::istringstream buf_in(buffer.str());

	entry_count += read_journal(journal, buf_in, "/dev/stdin", acct);
      }
      else if (exists(pathname)) {
	entry_count += read_journal(journal, pathname, acct);
	if (journal.price_db)
	  journal.sources.push_back(*journal.price_db);
	clean_accounts();
      }
      else {
	throw_(parse_error, "Could not open journal file '" << pathname << "'");
      }
    }
  }

  VERIFY(journal.valid());

  TRACE_STOP(session_parser, 1);

  return entry_count;
}

namespace {
  account_t * find_account_re_(account_t * account, const mask_t& regexp)
  {
    if (regexp.match(account->fullname()))
      return account;

    foreach (accounts_map::value_type& pair, account->accounts)
      if (account_t * a = find_account_re_(pair.second, regexp))
	return a;

    return NULL;
  }
}

account_t * session_t::find_account_re(const string& regexp)
{
  return find_account_re_(master.get(), mask_t(regexp));
}

void session_t::clean_xacts()
{
  session_xacts_iterator walker(*this);
  pass_down_xacts
    (xact_handler_ptr(new clear_xact_xdata), walker);
}

void session_t::clean_xacts(entry_t& entry)
{
  entry_xacts_iterator walker(entry);
  pass_down_xacts(xact_handler_ptr(new clear_xact_xdata), walker);
}

void session_t::clean_accounts()
{
  basic_accounts_iterator acct_walker(*master);
  pass_down_accounts(acct_handler_ptr(new clear_account_xdata),
		     acct_walker);
}

#if 0
value_t session_t::resolve(const string& name, expr_t::scope_t& locals)
{
  const char * p = name.c_str();
  switch (*p) {
  case 'd':
#if 0
    if (name == "date_format") {
      // jww (2007-04-18): What to do here?
      return string_value(moment_t::output_format);
    }
#endif
    break;

  case 'n':
    switch (*++p) {
    case 'o':
      if (name == "now")
	return value_t(now);
      break;
    }
    break;

  case 'r':
    if (name == "register_format")
      return string_value(register_format);
    break;
  }
  return expr_t::scope_t::resolve(name, locals);
}
#endif

expr_t::ptr_op_t session_t::lookup(const string& name)
{
  const char * p = name.c_str();
  switch (*p) {
  case 'b':
    if (std::strcmp(p, "balance_format") == 0)
      return expr_t::op_t::wrap_value(string_value(balance_format));
    break;

  case 'e':
    if (std::strcmp(p, "equity_format") == 0)
      return expr_t::op_t::wrap_value(string_value(equity_format));
    break;

  case 'p':
    if (std::strcmp(p, "plot_amount_format") == 0)
      return expr_t::op_t::wrap_value(string_value(plot_amount_format));
    else if (std::strcmp(p, "plot_total_format") == 0)
      return expr_t::op_t::wrap_value(string_value(plot_total_format));
    else if (std::strcmp(p, "prices_format") == 0)
      return expr_t::op_t::wrap_value(string_value(prices_format));
    else if (std::strcmp(p, "pricesdb_format") == 0)
      return expr_t::op_t::wrap_value(string_value(pricesdb_format));
    else if (std::strcmp(p, "print_format") == 0)
      return expr_t::op_t::wrap_value(string_value(print_format));
    break;

  case 'r':
    if (std::strcmp(p, "register_format") == 0)
      return expr_t::op_t::wrap_value(string_value(register_format));
    break;

  case 'w':
    if (std::strcmp(p, "wide_register_format") == 0)
      return expr_t::op_t::wrap_value(string_value(wide_register_format));
    else if (std::strcmp(p, "write_hdr_format") == 0)
      return expr_t::op_t::wrap_value(string_value(write_hdr_format));
    else if (std::strcmp(p, "write_xact_format") == 0)
      return expr_t::op_t::wrap_value(string_value(write_xact_format));
    break;

  case 'o':
    if (std::strncmp(p, "opt_", 4) == 0) {
      p = p + 4;
      switch (*p) {
      case 'd':
	if (std::strcmp(p, "debug_") == 0)
	  return MAKE_FUNCTOR(session_t::option_debug_);
	break;

      case 'f':
	if ((*(p + 1) == '_' && ! *(p + 2)) ||
	    std::strcmp(p, "file_") == 0)
	  return MAKE_FUNCTOR(session_t::option_file_);
	break;

      case 't':
	if (std::strcmp(p, "trace_") == 0)
	  return MAKE_FUNCTOR(session_t::option_trace_);
	break;

      case 'v':
	if (! *(p + 1) || std::strcmp(p, "verbose") == 0)
	  return MAKE_FUNCTOR(session_t::option_verbose);
	else if (std::strcmp(p, "version") == 0)
	  return MAKE_FUNCTOR(session_t::option_version);
	else if (std::strcmp(p, "verify") == 0)
	  return MAKE_FUNCTOR(session_t::option_verify);
	break;
      }
    }
    break;
  }

  return expr_t::ptr_op_t();
}

// jww (2007-04-26): All of Ledger should be accessed through a
// session_t object
void session_t::initialize()
{
  amount_t::initialize();
  value_t::initialize();
  expr_t::initialize();
}

void session_t::shutdown()
{
  expr_t::shutdown();
  value_t::shutdown();
  amount_t::shutdown();
}

} // namespace ledger