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
|
/*
* 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 <ledger.h> // Read this file for a top-level overview
#include "work.h" // This is where the meat of main() is, which
// was moved there for the sake of clarity
using namespace ledger;
namespace {
class global_scope_t : public noncopyable, public scope_t
{
scoped_ptr<session_t> session_ptr;
ptr_list<report_t> report_stack;
public:
path script_file;
global_scope_t() {
session_ptr.reset(new LEDGER_SESSION_T);
set_session_context(session_ptr.get());
// Create the report object, which maintains state relating to each
// command invocation. Because we're running from main(), the
// distinction between session and report doesn't really matter, but if
// a GUI were calling into Ledger it would have one session object per
// open document, with a separate report_t object for each report it
// generated.
report_stack.push_front(new report_t(*session_ptr.get()));
}
~global_scope_t() {
// If memory verification is being performed (which can be very slow),
// clean up everything by closing the session and deleting the session
// object, and then shutting down the memory tracing subsystem.
// Otherwise, let it all leak because we're about to exit anyway.
IF_VERIFY() {
set_session_context(NULL);
INFO("Ledger ended (Boost/libstdc++ may still hold memory)");
shutdown_memory_tracing();
} else {
// Don't free anything, just let it all leak.
INFO("Ledger ended");
}
}
char * prompt_string()
{
static char prompt[32];
std::size_t i;
for (i = 0; i < report_stack.size(); i++)
prompt[i] = ']';
prompt[i++] = ' ';
prompt[i] = '\0';
return prompt;
}
session_t& session() {
return *session_ptr.get();
}
report_t& report() {
return report_stack.front();
}
void push_report() {
report_stack.push_front(new report_t(report_stack.front()));
}
void pop_report() {
if (! report_stack.empty())
report_stack.pop_front();
}
value_t push_report_cmd(call_scope_t&) {
// Make a copy at position 2, because the topmost report object has an
// open output stream at this point. We want it to get popped off as
// soon as this command terminate so that the stream is closed cleanly.
report_stack.insert(++report_stack.begin(),
new report_t(report_stack.front()));
return true;
}
value_t pop_report_cmd(call_scope_t&) {
pop_report();
return true;
}
value_t option_script_(call_scope_t& args) {
script_file = args[0].as_string();
return true;
}
value_t ignore(call_scope_t&) {
return true;
}
virtual expr_t::ptr_op_t lookup(const string& name)
{
const char * p = name.c_str();
switch (*p) {
case 'l':
if (std::strncmp(p, "ledger_precmd_", 14) == 0) {
p = p + 14;
switch (*p) {
case 'p':
if (std::strcmp(p, "push") == 0)
return MAKE_FUNCTOR(global_scope_t::push_report_cmd);
else if (std::strcmp(p, "pop") == 0)
return MAKE_FUNCTOR(global_scope_t::pop_report_cmd);
break;
}
}
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(global_scope_t::ignore);
break;
case 's':
if (std::strcmp(p, "script_") == 0)
return MAKE_FUNCTOR(global_scope_t::option_script_);
break;
case 't':
if (std::strcmp(p, "trace_") == 0)
return MAKE_FUNCTOR(global_scope_t::ignore);
break;
case 'v':
if (! *(p + 1) || std::strcmp(p, "verbose") == 0)
return MAKE_FUNCTOR(global_scope_t::ignore);
else if (std::strcmp(p, "verify") == 0)
return MAKE_FUNCTOR(global_scope_t::ignore);
break;
}
}
}
// If you're wondering how symbols from report() will be found, it's
// because of the bind_scope_t object in execute_command() below.
return expr_t::ptr_op_t();
}
};
strings_list split_arguments(char * line)
{
strings_list args;
// jww (2009-02-04): This is too naive
for (char * p = std::strtok(line, " \t");
p;
p = std::strtok(NULL, " \t"))
args.push_back(p);
return args;
}
void report_error(const std::exception& err)
{
std::cout.flush(); // first display anything that was pending
if (caught_signal == NONE_CAUGHT) {
// Display any pending error context information
string context = error_context();
if (! context.empty())
std::cerr << context << std::endl;
std::cerr << "Error: " << err.what() << std::endl;
} else {
caught_signal = NONE_CAUGHT;
}
}
/**
* @return \c true if a command was actually executed; otherwise, it probably
* just resulted in setting some options.
*/
void execute_command(global_scope_t& global_scope,
strings_list args,
bool at_repl)
{
// Process the command verb, arguments and options
args = read_command_arguments(global_scope.report(), args);
if (args.empty())
return;
string_iterator arg = args.begin();
string verb = *arg++;
// Look for a precommand first, which is defined as any defined function
// whose name starts with "ledger_precmd_". The difference between a
// precommand and a regular command is that precommands ignore the journal
// data file completely, nor is the user's init file read.
//
// Here are some examples of pre-commands:
//
// parse STRING ; show how a value expression is parsed
// eval STRING ; simply evaluate a value expression
// format STRING ; show how a format string is parsed
//
// If such a command is found, create the output stream for the result and
// then invoke the command.
function_t command;
bool is_precommand = false;
bind_scope_t bound_scope(global_scope, global_scope.report());
if (bool(command = look_for_precommand(bound_scope, verb)))
is_precommand = true;
else if (! bool(command = look_for_command(bound_scope, verb)))
throw_(std::logic_error, "Unrecognized command '" << verb << "'");
// If it is not a pre-command, then parse the user's ledger data at this
// time if not done alreday (i.e., if not at a REPL). Then patch up the
// report options based on the command verb.
if (! is_precommand) {
if (! at_repl)
read_journal_files(global_scope.session(),
global_scope.report().account);
// jww (2009-02-02): This is a complete hack, and a leftover from long,
// long ago. The question is, how best to remove its necessity...
normalize_report_options(global_scope.report(), verb);
}
// Create the output stream (it might be a file, the console or a PAGER
// subprocess) and invoke the report command. The output stream is closed
// by the caller of this function.
global_scope.report()
.output_stream.initialize(global_scope.report().output_file,
global_scope.session().pager_path);
// Create an argument scope containing the report command's arguments, and
// then invoke the command. The bound scope causes lookups to happen
// first in the global scope, and then in the report scope.
call_scope_t command_args(bound_scope);
for (string_iterator i = arg; i != args.end(); i++)
command_args.push_back(string_value(*i));
INFO_START(command, "Finished executing command");
command(command_args);
INFO_FINISH(command);
}
int execute_command_wrapper(global_scope_t& global_scope,
strings_list args,
bool at_repl)
{
int status = 1;
try {
global_scope.push_report();
execute_command(global_scope, args, at_repl);
global_scope.pop_report();
// If we've reached this point, everything succeeded fine. Ledger uses
// exceptions to notify of error conditions, so if you're using gdb,
// just type "catch throw" to find the source point of any error.
status = 0;
}
catch (const std::exception& err) {
global_scope.pop_report();
report_error(err);
}
return status;
}
}
int main(int argc, char * argv[], char * envp[])
{
int status;
// The very first thing we do is handle some very special command-line
// options, since they affect how the environment is setup:
//
// --verify ; turns on memory tracing
// --verbose ; turns on logging
// --debug CATEGORY ; turns on debug logging
// --trace LEVEL ; turns on trace logging
handle_debug_options(argc, argv);
IF_VERIFY() initialize_memory_tracing();
INFO("Ledger starting");
// Initialize global Boost/C++ environment
std::ios::sync_with_stdio(false);
filesystem::path::default_name_check(filesystem::portable_posix_name);
std::signal(SIGINT, sigint_handler);
std::signal(SIGPIPE, sigpipe_handler);
// Create the session object, which maintains nearly all state relating to
// this invocation of Ledger; and register all known journal parsers.
std::auto_ptr<global_scope_t> global_scope(new global_scope_t);
try {
// Read the user's options, in the following order:
//
// 1. environment variables (LEDGER_<option>)
// 2. initialization file (~/.ledgerrc)
// 3. command-line (--option or -o)
//
// Before processing command-line options, we must notify the session object
// that such options are beginning, since options like -f cause a complete
// override of files found anywhere else.
global_scope->session().now_at_command_line(false);
read_environment_settings(global_scope->report(), envp);
global_scope->session().read_init();
global_scope->session().now_at_command_line(true);
// Construct an STL-style argument list from the process command arguments
strings_list args;
for (int i = 1; i < argc; i++)
args.push_back(argv[i]);
// Look for options and a command verb in the command-line arguments
bind_scope_t bound_scope(*global_scope.get(), global_scope->report());
args = read_command_arguments(bound_scope, args);
if (! global_scope->script_file.empty() &&
exists(global_scope->script_file)) {
read_journal_files(global_scope->session(),
global_scope->report().account);
ifstream in(global_scope->script_file);
while (! in.eof()) {
char line[1024];
in.getline(line, 1023);
char * p = skip_ws(line);
if (*p && *p != '#')
execute_command_wrapper(*global_scope, split_arguments(p), true);
}
status = 0;
}
else if (! args.empty()) {
// User has invoke a verb at the interactive command-line
status = execute_command_wrapper(*global_scope, args, false);
}
else {
// Commence the REPL by displaying the current Ledger version
global_scope->session().option_version(global_scope->session());
read_journal_files(global_scope->session(),
global_scope->report().account);
bool exit_loop = false;
#ifdef HAVE_LIBEDIT
rl_readline_name = const_cast<char *>("Ledger");
#if 0
// jww (2009-02-05): NYI
rl_attempted_completion_function = ledger_completion;
#endif
while (char * p = readline(global_scope->prompt_string())) {
char * expansion = NULL;
int result;
result = history_expand(skip_ws(p), &expansion);
if (result < 0 || result == 2) {
if (expansion)
std::free(expansion);
std::free(p);
throw_(std::logic_error,
"Failed to expand history reference '" << p << "'");
}
else if (expansion) {
add_history(expansion);
}
#else // HAVE_LIBEDIT
while (! std::cin.eof()) {
std::cout << global_scope->prompt_string();
char line[1024];
std::cin.getline(line, 1023);
char * p = skip_ws(line);
#endif // HAVE_LIBEDIT
bool do_command = true;
check_for_signal();
if (*p && *p != '#') {
if (std::strncmp(p, "quit", 4) == 0)
exit_loop = true;
else
execute_command_wrapper(*global_scope, split_arguments(p), true);
}
#ifdef HAVE_LIBEDIT
if (expansion)
std::free(expansion);
std::free(p);
#endif // HAVE_LIBEDIT
if (exit_loop)
break;
}
status = 0; // report success
}
}
catch (const std::exception& err) {
report_error(err);
}
catch (int _status) {
status = _status; // used for a "quick" exit, and is used only
// if help text (such as --help) was displayed
}
// If memory verification is being performed (which can be very slow), clean
// up everything by closing the session and deleting the session object, and
// then shutting down the memory tracing subsystem. Otherwise, let it all
// leak because we're about to exit anyway.
IF_VERIFY() {
global_scope.reset();
shutdown_memory_tracing();
INFO("Ledger ended (Boost/libstdc++ may still hold memory)");
} else {
// Don't free anything, just let it all leak.
global_scope.release();
INFO("Ledger ended");
}
// Return the final status to the operating system, either 1 for error or 0
// for a successful completion.
return status;
}
// main.cc ends here.
|