Loading...
Searching...
No Matches
global.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
38#pragma once
39
40#include <ledger.hh>
41#include "option.h"
42#include "report.h"
43
44namespace ledger {
45
46class session_t;
47class report_t;
48
49extern std::string _init_file;
50
51class global_scope_t : public noncopyable, public scope_t
52{
53 shared_ptr<session_t> session_ptr;
54 ptr_list<report_t> report_stack;
55 empty_scope_t empty_scope;
56
57public:
60
61 void quick_close() {
62 if (! report_stack.empty())
63 report_stack.front().quick_close();
64 }
65
66 virtual string description() {
67 return _("global scope");
68 }
69
71 void read_init();
77
78 char * prompt_string();
79
81 return *session_ptr.get();
82 }
84 return report_stack.front();
85 }
86
87 void push_report() {
88 report_stack.push_front(new report_t(report_stack.front()));
90 }
91 void pop_report() {
92 assert(! report_stack.empty());
93 report_stack.pop_front();
94
95 // There should always be the "default report" waiting on the stack.
96 assert(! report_stack.empty());
98 }
99
100 void report_error(const std::exception& err);
101
108
110 // Make a copy at position 2, because the topmost report object has an
111 // open output stream at this point. We want it to get popped off as
112 // soon as this command terminate so that the stream is closed cleanly.
113 report_stack.insert(++report_stack.begin(),
114 new report_t(report_stack.front()));
115 return true;
116 }
118 pop_report();
119 return true;
120 }
121
122 void show_version_info(std::ostream& out) {
123 out <<
124 "Ledger " << Ledger_VERSION_MAJOR << '.' << Ledger_VERSION_MINOR << '.'
128 if (std::strlen(Ledger_VERSION_DATE) > 0)
129 out << '-' << Ledger_VERSION_DATE;
130 out << _(", the command-line accounting tool");
131 out << _("\nwith");
132#if !HAVE_GPGME
133 out << _("out");
134#endif
135 out << _(" support for gpg encrypted journals and with");
136#if !HAVE_BOOST_PYTHON
137 out <<_("out");
138#endif
139 out << _(" Python support");
140 out <<
141 _("\n\nCopyright (c) 2003-2023, John Wiegley. All rights reserved.\n\n\
142This program is made available under the terms of the BSD Public License.\n\
143See LICENSE file included with the distribution for details and disclaimer.");
144 out << std::endl;
145 }
146
147 void report_options(report_t& report, std::ostream& out);
148
150
152 const string& name);
153
156
157 void visit_man_page() const;
158
159 OPTION_(global_scope_t, help, DO() { parent->visit_man_page(); }); // -h
160
164 if (!_init_file.empty())
165 // _init_file is filled during handle_debug_options
166 on(none, _init_file);
167 });
168
175
176 OPTION_(global_scope_t, version, DO() { // -v
177 parent->show_version_info(std::cout);
178 throw error_count(0, ""); // exit immediately
179 });
180};
181
182void handle_debug_options(int argc, char * argv[]);
183
184} // namespace ledger
#define assert(x)
Definition utils.h:92
Basic type and macros for handling command-line options.
#define OPTION(type, name)
Definition option.h:266
#define CTOR(type, name)
Definition option.h:210
#define OPTION_(type, name, body)
Definition option.h:273
#define DO()
Definition option.h:218
#define OPTION__(type, name, body)
Definition option.h:281
std::string _init_file
boost::filesystem::path path
Definition utils.h:68
std::list< string > strings_list
Definition utils.h:60
T & downcast(U &object)
Definition utils.h:468
void handle_debug_options(int argc, char *argv[])
const string version
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57
function< result_type(call_scope_t &) func_t)
Definition exprbase.h:75
session_t & session()
Definition global.h:80
void report_options(report_t &report, std::ostream &out)
strings_list read_command_arguments(scope_t &scope, strings_list args)
expr_t::func_t look_for_command(scope_t &scope, const string &verb)
void show_version_info(std::ostream &out)
Definition global.h:122
void parse_init(path init_file)
global_scope_t(char **envp)
void visit_man_page() const
value_t push_command(call_scope_t &)
Definition global.h:109
value_t pop_command(call_scope_t &)
Definition global.h:117
void read_environment_settings(char *envp[])
option_t< global_scope_t > * lookup_option(const char *p)
expr_t::func_t look_for_precommand(scope_t &scope, const string &verb)
void execute_command(strings_list args, bool at_repl)
virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind, const string &name)
int execute_command_wrapper(strings_list args, bool at_repl)
report_t & report()
Definition global.h:83
void report_error(const std::exception &err)
virtual string description()
Definition global.h:66
static scope_t * default_scope
Definition scope.h:92
Dynamic type representing various numeric types.
Definition value.h:83