aboutsummaryrefslogtreecommitdiff
path: root/src/qalculate_helper.cpp
blob: c997a95c8d2ad95366e7f41ff2e2924e5a0c1293 (plain) (blame)
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
//SPDX-License-Identifier: GPL-3.0
/*
 * qalculate-helper.cpp
 * Copyright (C) 2024 Marko Zajc
 *
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this
 * program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <libqalculate/Calculator.h>
#include <libqalculate/DataSet.h>
#include <libqalculate/includes.h>
#include <libqalculate/MathStructure.h>
#include <security_util.h>
#include <timeout_exception.h>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using std::string;
using std::stringstream;
using std::getline;
using std::vector;
using std::string_view;
using std::size_t;
using std::pair;

#if __cplusplus >= 201703L
#include <string_view>

static bool ends_with(string_view str, string_view suffix) {
	return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}

#endif

const char TYPE_MESSAGE = 1;
const char TYPE_RESULT = 2;

const char LEVEL_INFO = 1;
const char LEVEL_WARNING = 2;
const char LEVEL_ERROR = 3;
const char LEVEL_UNKNOWN = 4;
const char SEPARATOR = 0;

const unsigned long MODE_PRECISION = 1 << 0;
const unsigned long MODE_EXACT = 1 << 1;
const unsigned long MODE_NOCOLOR = 1 << 2;

struct MathResult {
		MathStructure input;
		MathStructure output;
};

static void print_messages(unsigned long line_number, Calculator &calc) {
	const CalculatorMessage *message;
	while ((message = calc.message())) {
		putchar(TYPE_MESSAGE);
		switch (message->type()) {
			case MESSAGE_INFORMATION:
				putchar(LEVEL_INFO);
				break;
			case MESSAGE_WARNING:
				putchar(LEVEL_WARNING);
				break;
			case MESSAGE_ERROR:
				putchar(LEVEL_ERROR);
				break;
			default:
				putchar(LEVEL_UNKNOWN);
				break;
		}
		printf("line %lu: ", line_number);
		fputs(message->c_message(), stdout);
		putchar(SEPARATOR);
		calc.nextMessage();
	}
}

static MathStructure evaluate_single(Calculator &calc, const EvaluationOptions &eo, unsigned long line_number,
									 const string &expression, MathStructure *out_parsed = nullptr) {
	MathStructure result;
	if (!calc.calculate(&result, calc.unlocalizeExpression(expression), TIMEOUT_CALC, eo, out_parsed))
		throw timeout_exception();
	print_messages(line_number, calc);
	return result;
}

static bool mode_set(unsigned long mode, unsigned long test) {
	return mode & test;
}

static void set_precision(Calculator &calc, unsigned long mode, EvaluationOptions &eo, PrintOptions &po) {
	int precision = PRECISION_DEFAULT;

	if (mode_set(mode, MODE_EXACT)) {
		eo.approximation = APPROXIMATION_EXACT;
		po.number_fraction_format = FRACTION_DECIMAL_EXACT;

	} else if (mode_set(mode, MODE_PRECISION)) {
		precision = PRECISION_HIGH;
		po.indicate_infinite_series = false;
	}

	calc.setPrecision(precision);
}

static MathResult evaluate_all(Calculator &calc, const vector<string> &expressions, const EvaluationOptions &eo) {
	for (size_t i = 0; i < expressions.size() - 1; ++i)
		evaluate_single(calc, eo, i + 1, expressions[i]);

	MathStructure parsed;
	MathStructure evaluated = evaluate_single(calc, eo, expressions.size(), expressions.back(), &parsed);
	return {parsed, evaluated};
}

static bool has_unknown(const MathStructure &ms) {
	if (ms.size() == 0)
		return ms.isUnknown();
	else
		return has_unknown(ms[0]);
}

static void replace_booleans(Calculator &calc, MathResult &result) {
	bool inputBoolean = result.input.isLogicalAnd() || result.input.isLogicalNot() || result.input.isLogicalOr()
			|| result.input.isLogicalXor() || result.input.isComparison();

	bool outputBoolean = !has_unknown(result.output);

	if (inputBoolean && outputBoolean && result.output.representsBoolean()) {
		auto *replacement = result.output.isZero() ? calc.getActiveVariable("false") : calc.getActiveVariable("true");

		if (replacement)
			result.output.set(replacement);
	}
}

static void print_result(Calculator &calc, MathResult result_struct, const PrintOptions &po, int mode) {
	replace_booleans(calc, result_struct);
	string result = calc.print(result_struct.output, TIMEOUT_PRINT, po, false, mode_set(mode, MODE_NOCOLOR) ? 0 : 1,
			TAG_TYPE_TERMINAL);

	if (ends_with(result, calc.timedOutString()))
		throw timeout_exception();

	if (!result_struct.output.isComparison()) // comparisons (eg. "x = 1") already have a comparison sign
		result = (result_struct.output.isApproximate() ? "≈ " : "= ") + result;

	if (!mode_set(mode, MODE_NOCOLOR) && ends_with(result, "\033[0m"))
		result.erase(result.length() - 4);

	putchar(TYPE_RESULT);
	fputs(result.c_str(), stdout);
	putchar(SEPARATOR);
}

static EvaluationOptions get_evaluationoptions() {
	EvaluationOptions eo;
	eo.approximation = APPROXIMATION_TRY_EXACT;
	eo.parse_options.unknowns_enabled = false;
	// eo.sync_units = false; // causes issues with monetary conversion, eg x usd > 1 eur. no idea why I
	// enabled this in the first place
	return eo;
}

static PrintOptions get_printoptions(int base) {
	PrintOptions po;
	po.base = base;
	po.number_fraction_format = FRACTION_DECIMAL;
	po.interval_display = INTERVAL_DISPLAY_PLUSMINUS;
	po.use_unicode_signs = true;
	po.time_zone = TIME_ZONE_UTC;
	po.abbreviate_names = true;
	po.spell_out_logical_operators = true;
	po.allow_non_usable = true;
	po.show_ending_zeroes = false;
	return po;
}

static void load_calculator(Calculator &calc) {
	do_defang_calculator(calc);
	calc.loadExchangeRates();
	calc.loadGlobalDefinitions();

#ifdef LIBQALCULATE_PRELOAD_DATASETS
	DataSet *set = calc.getDataSet(1); // getDataSet() is 1-indexed for some reason
	size_t i = 2;
	while (set) {
		set->loadObjects();
		set = calc.getDataSet(i++);
	}
#endif
}

static void evaluate(Calculator &calc, const vector<string> &expressions, unsigned int mode, int base) {
	PrintOptions po = get_printoptions(base);
	EvaluationOptions eo = get_evaluationoptions();
	set_precision(calc, mode, eo, po);

	calc.setMessagePrintOptions(po);

	do_seccomp();

	auto result_struct = evaluate_all(calc, expressions.empty() ? vector<string> {"0"} : expressions, eo);

	print_result(calc, result_struct, po, mode);
}

static vector<string> parseExpressions(stringstream input) {
	vector<string> result;
	string expression;
	while (std::getline(input, expression, '\n'))
		result.push_back(expression);

	return result;
}

int main(int argc, char **argv) {
	do_setuid();

	if (argc != 4)
		return 1;

	Calculator calc(true);
	try {
		load_calculator(calc);
		evaluate(calc, parseExpressions(stringstream(argv[1])), std::strtoul(argv[2], nullptr, 10),
				std::strtol(argv[3], nullptr, 10));

	} catch (const qalculate_exception &e) {
		return e.getCode();
	}
}