1 /*
2  * Copyright (c) 2003-2018, 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 
32 /**
33  * @defgroup report Reporting
34  */
35 
36 /**
37  * @file   session.h
38  * @author John Wiegley
39  *
40  * @ingroup report
41  */
42 #ifndef _SESSION_H
43 #define _SESSION_H
44 
45 #include "account.h"
46 #include "journal.h"
47 #include "context.h"
48 #include "option.h"
49 #include "commodity.h"
50 
51 namespace ledger {
52 
53 class xact_t;
54 
55 struct ComparePaths
56 {
operatorComparePaths57   bool operator()(const path& p1, const path& p2) const
58   {
59     return p1 < p2 && !boost::filesystem::equivalent(p1, p2);
60   }
61 };
62 
63 #define COMMA ,
64 
65 class session_t : public symbol_scope_t
66 {
67   friend void set_session_context(session_t * session);
68 
69 public:
70   bool flush_on_next_data_file;
71 
72   unique_ptr<journal_t> journal;
73   parse_context_stack_t parsing_context;
74   optional<expr_t>      value_expr;
75 
76   explicit session_t();
~session_t()77   virtual ~session_t() {
78     TRACE_DTOR(session_t);
79     parsing_context.pop();
80   }
81 
description()82   virtual string description() {
83     return _("current session");
84   }
85 
set_flush_on_next_data_file(const bool truth)86   void set_flush_on_next_data_file(const bool truth) {
87     flush_on_next_data_file = truth;
88   }
89 
90   journal_t * read_journal(const path& pathname);
91   journal_t * read_journal_from_string(const string& data);
92   std::size_t read_data(const string& master_account = "");
93 
94   journal_t * read_journal_files();
95   void close_journal_files();
96 
97   journal_t * get_journal();
98 
99   value_t fn_account(call_scope_t& scope);
100   value_t fn_min(call_scope_t& scope);
101   value_t fn_max(call_scope_t& scope);
102   value_t fn_int(call_scope_t& scope);
103   value_t fn_str(call_scope_t& scope);
104   value_t fn_lot_price(call_scope_t& scope);
105   value_t fn_lot_date(call_scope_t& scope);
106   value_t fn_lot_tag(call_scope_t& scope);
107 
report_options(std::ostream & out)108   void report_options(std::ostream& out)
109   {
110     HANDLER(check_payees).report(out);
111     HANDLER(day_break).report(out);
112     HANDLER(download).report(out);
113     HANDLER(decimal_comma).report(out);
114     HANDLER(time_colon).report(out);
115     HANDLER(file_).report(out);
116     HANDLER(input_date_format_).report(out);
117     HANDLER(explicit).report(out);
118     HANDLER(master_account_).report(out);
119     HANDLER(pedantic).report(out);
120     HANDLER(permissive).report(out);
121     HANDLER(price_db_).report(out);
122     HANDLER(price_exp_).report(out);
123     HANDLER(recursive_aliases).report(out);
124     HANDLER(no_aliases).report(out);
125     HANDLER(strict).report(out);
126     HANDLER(value_expr_).report(out);
127   }
128 
129   option_t<session_t> * lookup_option(const char * p);
130 
131   virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
132                                   const string& name);
133 
134   /**
135    * Option handlers
136    */
137 
138   OPTION(session_t, check_payees);
139   OPTION(session_t, day_break);
140   OPTION(session_t, download); // -Q
141 
142   OPTION_(session_t, decimal_comma, DO() {
143       commodity_t::decimal_comma_by_default = true;
144     });
145 
146   OPTION_(session_t, time_colon, DO() {
147       commodity_t::time_colon_by_default = true;
148     });
149 
150   OPTION__
151   (session_t, price_exp_, // -Z
152    CTOR(session_t, price_exp_) { value = "24"; });
153 
154   OPTION__
155   (session_t, file_, // -f
156    std::list<path> data_files;
157    CTOR(session_t, file_) {}
158    DO_(str) {
159      if (parent->flush_on_next_data_file) {
160        data_files.clear();
161        parent->flush_on_next_data_file = false;
162      }
163      data_files.push_back(str);
164    });
165 
166   OPTION_(session_t, input_date_format_, DO_(str) {
167       // This changes static variables inside times.h, which affects the
168       // basic date parser.
169       set_input_date_format(str.c_str());
170     });
171 
172   OPTION(session_t, explicit);
173   OPTION(session_t, master_account_);
174   OPTION(session_t, pedantic);
175   OPTION(session_t, permissive);
176   OPTION(session_t, price_db_);
177   OPTION(session_t, strict);
178   OPTION(session_t, value_expr_);
179   OPTION(session_t, recursive_aliases);
180   OPTION(session_t, no_aliases);
181 };
182 
183 /**
184  * Set the current session context, transferring all static globals to point
185  * at the data structures related to this session.  Although Ledger itself is
186  * not thread-safe, by locking, switching session context, then unlocking
187  * after an operation is done, multiple threads can sequentially make use of
188  * the library.  Thus, a session_t maintains all of the information relating
189  * to a single usage of the Ledger library.
190  */
191 void set_session_context(session_t * session);
192 
193 } // namespace ledger
194 
195 #endif // _SESSION_H
196