forked from Qalculate/libqalculate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qalc.cc
12034 lines (11768 loc) · 460 KB
/
qalc.cc
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Qalculate (CLI)
Copyright (C) 2003-2007, 2008, 2016-2021 Hanna Knutsson
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; either version 2 of the License, or
(at your option) any later version.
*/
#include <libqalculate/support.h>
// #include <dirent.h>
#include <libqalculate/qalculate.h>
#include <limits.h>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <vector>
#ifdef HAVE_LIBREADLINE
#include <readline/history.h>
#include <readline/readline.h>
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <VersionHelpers.h>
#include <windows.h>
#define STDIN_FILENO _fileno(stdin)
#else
#include <unistd.h>
#endif
#ifndef _WIN32
#include <signal.h>
#endif
#include <libqalculate/MathStructure-support.h>
using std::cerr;
using std::cout;
using std::endl;
using std::iterator;
using std::list;
using std::string;
using std::vector;
class ViewThread : public Thread {
protected:
virtual void run();
};
class CommandThread : public Thread {
protected:
virtual void run();
};
MathStructure *mstruct, *parsed_mstruct, mstruct_exact, prepend_mstruct;
KnownVariable *vans[5], *v_memory;
string result_text, parsed_text, original_expression;
vector<string> alt_results;
bool load_global_defs, fetch_exchange_rates_at_startup, first_time,
save_mode_on_exit, save_defs_on_exit, clear_history_on_exit,
load_defaults = false;
int auto_update_exchange_rates;
PrintOptions printops, saved_printops;
bool saved_concise_uncertainty_input = false;
bool complex_angle_form = false, saved_caf = false;
EvaluationOptions evalops, saved_evalops;
bool dot_question_asked = false, implicit_question_asked = false;
Number saved_custom_output_base, saved_custom_input_base;
AssumptionType saved_assumption_type;
AssumptionSign saved_assumption_sign;
int saved_precision;
int saved_binary_prefixes;
bool saved_interval, saved_adaptive_interval_display,
saved_variable_units_enabled;
bool adaptive_interval_display;
Thread *view_thread, *command_thread;
bool command_aborted = false;
volatile bool b_busy = false;
string expression_str;
bool expression_executed = false;
bool avoid_recalculation = false;
bool hide_parse_errors = false;
ParsingMode nonrpn_parsing_mode = PARSING_MODE_ADAPTIVE, saved_parsing_mode;
bool saved_percent;
bool rpn_mode = false, saved_rpn_mode = false;
bool caret_as_xor = false, saved_caret_as_xor = false;
string custom_angle_unit, saved_custom_angle_unit;
bool use_readline = true;
bool interactive_mode;
int colorize = 0;
int force_color = -1;
bool ask_questions = false;
bool canfetch = true;
bool programmers_mode = false;
int b_decimal_comma = -1;
long int i_maxtime = 0;
struct timeval t_end;
int dual_fraction = -1, saved_dual_fraction = -1;
int dual_approximation = -1, saved_dual_approximation = -1;
bool tc_set = false, sinc_set = false;
bool ignore_locale = false;
bool result_only = false, vertical_space = true;
bool do_imaginary_j = false;
int sigint_action = 1;
bool unittest = false;
int rounding_mode = 0, saved_rounding = 0;
bool simplified_percentage = true;
int defs_edited = 0;
bool use_duo_syms = false;
int unicode_exponents = 1;
bool had_to_expression = false;
bool had_errors = false;
static char buffer[100000];
void setResult(Prefix *prefix = NULL, bool update_parse = false,
bool goto_input = true, size_t stack_index = 0,
bool register_moved = false, bool noprint = false);
void execute_expression(bool goto_input = true, bool do_mathoperation = false,
MathOperation op = OPERATION_ADD,
MathFunction *f = NULL, bool do_stack = false,
size_t stack_index = 0, bool check_exrates = true);
void execute_command(int command_type, bool show_result = true);
void load_preferences();
void save_history();
bool save_preferences(bool mode = false);
bool save_mode();
void set_saved_mode();
bool save_defs();
void result_display_updated();
void result_format_updated();
void result_action_executed();
void result_prefix_changed(Prefix *prefix = NULL);
void expression_format_updated(bool reparse);
void expression_calculation_updated();
bool display_errors(bool goto_input = false, int cols = 0,
bool *implicit_warning = NULL);
void replace_result_cis(string &resstr);
FILE *cfile;
enum {
COMMAND_FACTORIZE,
COMMAND_EXPAND,
COMMAND_EXPAND_PARTIAL_FRACTIONS,
COMMAND_EVAL
};
#define RESET_TZ \
printops.custom_time_zone = (rounding_mode == 2 ? TZ_TRUNCATE : 0); \
if (use_duo_syms) \
printops.custom_time_zone += TZ_DOZENAL; \
printops.time_zone = TIME_ZONE_LOCAL;
#define EQUALS_IGNORECASE_AND_LOCAL(x, y, z) \
(equalsIgnoreCase(x, y) || equalsIgnoreCase(x, z))
#define EQUALS_IGNORECASE_AND_LOCAL_NR(x, y, z, a) \
(equalsIgnoreCase(x, y a) || \
(x.length() == strlen(z) + strlen(a) && \
equalsIgnoreCase(x.substr(0, x.length() - strlen(a)), z) && \
equalsIgnoreCase(x.substr(x.length() - strlen(a)), a)))
#define DO_WIN_FORMAT IsWindows10OrGreater()
#ifdef _WIN32
#define DO_FORMAT \
(force_color > 0 || \
(force_color != 0 && !cfile && colorize && interactive_mode))
#else
#define DO_FORMAT \
(force_color > 0 || (force_color != 0 && !cfile && interactive_mode))
#endif
#define DO_COLOR \
(force_color >= 0 ? force_color \
: (!cfile && colorize && interactive_mode ? colorize : 0))
#define PRINT_COLOR \
(DO_COLOR * (unicode_exponents == 2 ? -1 : (unicode_exponents > 0 ? 1 : -10)))
bool contains_unicode_char(const char *str) {
for (int i = strlen(str) - 1; i >= 0; i--) {
if ((signed char)str[i] < 0)
return true;
}
return false;
}
#ifdef _WIN32
LPWSTR utf8wchar(const char *str) {
size_t len = strlen(str) + 1;
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
LPWSTR wstr = (LPWSTR)LocalAlloc(LPTR, sizeof(WCHAR) * size_needed);
MultiByteToWideChar(CP_UTF8, 0, str, len, wstr, size_needed);
return wstr;
}
#define PUTS_UNICODE(x) \
if (!contains_unicode_char(x)) { \
puts(x); \
} else if (printops.use_unicode_signs) { \
fputws(utf8wchar(x), stdout); \
puts(""); \
} else { \
char *gstr = locale_from_utf8(x); \
if (gstr) { \
puts(gstr); \
free(gstr); \
} else { \
puts(x); \
} \
}
#define FPUTS_UNICODE(x, y) \
if (!contains_unicode_char(x)) { \
fputs(x, y); \
} else if (printops.use_unicode_signs) { \
fputws(utf8wchar(x), y); \
} else { \
char *gstr = locale_from_utf8(x); \
if (gstr) { \
fputs(gstr, y); \
free(gstr); \
} else { \
fputs(x, y); \
} \
}
#else
#define PUTS_UNICODE(x) \
if (printops.use_unicode_signs || !contains_unicode_char(x)) { \
puts(x); \
} else { \
char *gstr = locale_from_utf8(x); \
if (gstr) { \
puts(gstr); \
free(gstr); \
} else { \
puts(x); \
} \
}
#define FPUTS_UNICODE(x, y) \
if (printops.use_unicode_signs || !contains_unicode_char(x)) { \
fputs(x, y); \
} else { \
char *gstr = locale_from_utf8(x); \
if (gstr) { \
fputs(gstr, y); \
free(gstr); \
} else { \
fputs(x, y); \
} \
}
#endif
#define MIN_EXP(x) \
((x.min_exp < EXP_NO_POWER_OF_10 / 2) \
? x.min_exp - EXP_NO_POWER_OF_10 \
: ((x.min_exp > EXP_POWER_OF_10 / 2) ? x.min_exp - EXP_POWER_OF_10 \
: x.min_exp))
void update_message_print_options() {
PrintOptions message_printoptions = printops;
message_printoptions.interval_display = INTERVAL_DISPLAY_PLUSMINUS;
message_printoptions.show_ending_zeroes = false;
message_printoptions.base = 10;
int min_exp = MIN_EXP(printops);
if (min_exp < -10 || min_exp > 10 ||
((min_exp == EXP_PRECISION || min_exp == EXP_NONE) && PRECISION > 10))
message_printoptions.min_exp =
(printops.min_exp > EXP_POWER_OF_10 / 2 ? EXP_POWER_OF_10 + 10 : 10);
else if (min_exp == EXP_NONE)
message_printoptions.min_exp = (printops.min_exp > EXP_POWER_OF_10 / 2
? EXP_POWER_OF_10 + EXP_PRECISION
: EXP_PRECISION);
if (PRECISION > 10) {
message_printoptions.use_max_decimals = true;
message_printoptions.max_decimals = 10;
}
CALCULATOR->setMessagePrintOptions(message_printoptions);
}
size_t unicode_length_check(const char *str) {
size_t l = strlen(str), l2 = 0;
for (size_t i = 0; i < l; i++) {
if (str[i] == '\033') {
do {
i++;
} while (i < l && str[i] != 'm');
} else if ((signed char)str[i] > 0 || (unsigned char)str[i] >= 0xC0) {
l2++;
}
}
return l2;
}
int s2b(const string &str) {
if (str.empty())
return -1;
if (EQUALS_IGNORECASE_AND_LOCAL(str, "yes", _("yes")))
return 1;
if (EQUALS_IGNORECASE_AND_LOCAL(str, "no", _("no")))
return 0;
if (EQUALS_IGNORECASE_AND_LOCAL(str, "true", _("true")))
return 1;
if (EQUALS_IGNORECASE_AND_LOCAL(str, "false", _("false")))
return 0;
if (EQUALS_IGNORECASE_AND_LOCAL(str, "on", _("on")))
return 1;
if (EQUALS_IGNORECASE_AND_LOCAL(str, "off", _("off")))
return 0;
if (str.find_first_not_of(SPACES NUMBERS) != string::npos)
return -1;
int i = s2i(str);
if (i > 0)
return 1;
return 0;
}
bool is_answer_variable(Variable *v) {
return v == vans[0] || v == vans[1] || v == vans[2] || v == vans[3] ||
v == vans[4];
}
bool equalsIgnoreCaseFirst(const string &str1, const char *str2) {
if (str1.length() < 1 || strlen(str2) < 1)
return false;
if (((signed char)str1[0] < 0 && str1.length() > 1) ||
((signed char)str2[0] < 0 && strlen(str2) > 1)) {
size_t iu1 = 1, iu2 = 1;
if ((signed char)str1[0] < 0) {
while (iu1 < str1.length() && (signed char)str1[iu1] < 0 &&
(unsigned char)str1[iu1] < 0xC0) {
iu1++;
}
}
if ((signed char)str2[0] < 0) {
while (iu2 < strlen(str2) && (signed char)str2[iu2] < 0 &&
(unsigned char)str1[iu2] < 0xC0) {
iu2++;
}
}
bool isequal = (iu1 == iu2);
if (isequal) {
for (size_t i = 0; i < iu1; i++) {
if (str1[i] != str2[i]) {
isequal = false;
break;
}
}
}
if (!isequal) {
char *gstr1 = utf8_strdown(str1.c_str(), iu1);
if (!gstr1)
return false;
char *gstr2 = utf8_strdown(str2, iu2);
if (!gstr2) {
free(gstr1);
return false;
}
bool b = strcmp(gstr1, gstr2) == 0;
free(gstr1);
free(gstr2);
return b;
}
} else if (str1.length() != 1 || (str1[0] != str2[0] &&
!((str1[0] >= 'a' && str1[0] <= 'z') &&
str1[0] - 32 == str2[0]) &&
!((str1[0] <= 'Z' && str1[0] >= 'A') &&
str1[0] + 32 == str2[0]))) {
return false;
}
return true;
}
bool ask_question(const char *question, bool default_answer = false) {
FPUTS_UNICODE(question, stdout);
while (true) {
#ifdef HAVE_LIBREADLINE
char *rlbuffer = readline(" ");
if (!rlbuffer)
return false;
string str = rlbuffer;
free(rlbuffer);
#else
fputs(" ", stdout);
if (!fgets(buffer, 1000, stdin))
return false;
string str = buffer;
#endif
remove_blank_ends(str);
if (equalsIgnoreCaseFirst(str, "yes") ||
equalsIgnoreCaseFirst(str, _("yes")) ||
EQUALS_IGNORECASE_AND_LOCAL(str, "yes", _("yes"))) {
return true;
} else if (equalsIgnoreCaseFirst(str, "no") ||
equalsIgnoreCaseFirst(str, _("no")) ||
EQUALS_IGNORECASE_AND_LOCAL(str, "no", _("no"))) {
return false;
} else if (str.empty()) {
return default_answer;
} else {
FPUTS_UNICODE(_("Please answer yes or no"), stdout);
FPUTS_UNICODE(":", stdout);
}
}
}
void set_assumption(const string &str, bool last_of_two = false) {
// assumptions
if (EQUALS_IGNORECASE_AND_LOCAL(str, "none", _("none")) || str == "0") {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_NUMBER);
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_UNKNOWN);
// assumptions
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "unknown", _("unknown"))) {
if (!last_of_two) {
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_UNKNOWN);
} else {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_NUMBER);
}
// real number
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "real", _("real"))) {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_REAL);
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "number", _("number")) ||
str == "num") {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_NUMBER);
// complex number
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "complex", _("complex")) ||
str == "cplx") {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_NUMBER);
// rational number
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "rational", _("rational")) ||
str == "rat") {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_RATIONAL);
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "integer", _("integer")) ||
str == "int") {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_INTEGER);
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "boolean", _("boolean")) ||
str == "bool") {
CALCULATOR->defaultAssumptions()->setType(ASSUMPTION_TYPE_BOOLEAN);
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "non-zero", _("non-zero")) ||
str == "nz") {
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_NONZERO);
// positive number
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "positive", _("positive")) ||
str == "pos") {
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_POSITIVE);
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "non-negative",
_("non-negative")) ||
str == "nneg") {
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_NONNEGATIVE);
// negative number
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "negative", _("negative")) ||
str == "neg") {
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_NEGATIVE);
} else if (EQUALS_IGNORECASE_AND_LOCAL(str, "non-positive",
_("non-positive")) ||
str == "npos") {
CALCULATOR->defaultAssumptions()->setSign(ASSUMPTION_SIGN_NONPOSITIVE);
} else {
PUTS_UNICODE(_("Unrecognized assumption."));
return;
}
}
void replace_subscripts(string &str) {
if (str.find("\xe2\x82", 1) != string::npos) {
bool in_cit1 = false, in_cit2 = false;
for (size_t i = 1; i < str.length() - 2; i++) {
switch (str[i]) {
case '\"': {
if (in_cit1)
in_cit1 = false;
else if (!in_cit2)
in_cit1 = true;
break;
}
case '\'': {
if (in_cit2)
in_cit2 = false;
else if (!in_cit1)
in_cit1 = true;
break;
}
case '\xe2': {
if (is_not_in(NOT_IN_NAMES, str[i - 1]) && str[i + 1] == '\x82' &&
(unsigned char)str[i + 2] >= 0x80 &&
(unsigned char)str[i + 2] <= 0x89) {
str.replace(i, 3, 1, '0' + ((unsigned char)str[i + 2] - 0x80));
}
break;
}
}
}
}
}
vector<string> matches;
#ifdef __cplusplus
extern "C" {
#endif
bool name_has_formatting(const ExpressionName *ename) {
if (ename->name.length() < 2)
return false;
if (ename->suffix)
return true;
if (ename->completion_only || ename->case_sensitive ||
ename->name.length() <= 4)
return false;
size_t i = ename->name.find('_');
if (i == string::npos)
return false;
return unicode_length(ename->name, i) >= 3;
}
#ifdef HAVE_LIBREADLINE
void completion_match_item(ExpressionItem *item, const char *text, size_t l) {
const ExpressionName *ename = NULL;
bool b_match = false, b_formatted = false;
string strcmp;
for (size_t name_i = 1; name_i <= item->countNames() && !b_match; name_i++) {
ename = &item->getName(name_i);
if (ename && l <= ename->name.length()) {
b_match = true;
b_formatted = false;
for (size_t i2 = 0; i2 < l; i2++) {
if (ename->name[i2] != text[i2]) {
b_match = false;
break;
}
}
}
if (!b_match && name_has_formatting(ename)) {
strcmp = ename->formattedName(item->type(), true);
if (l <= strcmp.length()) {
b_match = true;
b_formatted = true;
for (size_t i2 = 0; i2 < l; i2++) {
if (strcmp[i2] != text[i2]) {
b_match = false;
break;
}
}
}
}
}
if (b_match && ename) {
if (ename->completion_only) {
ename = &item->preferredInputName(ename->abbreviation,
printops.use_unicode_signs);
matches.push_back(ename->formattedName(item->type(), true, false,
printops.use_unicode_signs));
} else if (b_formatted) {
matches.push_back(ename->formattedName(item->type(), true, false,
printops.use_unicode_signs));
} else {
matches.push_back(ename->name);
}
}
}
char *qalc_completion(const char *text, int index) {
if (index == 0) {
if (strlen(text) < 1)
return NULL;
matches.clear();
size_t l = strlen(text);
for (size_t i = 0; i < CALCULATOR->functions.size(); i++) {
if (CALCULATOR->functions[i]->isActive()) {
completion_match_item(CALCULATOR->functions[i], text, l);
}
}
for (size_t i = 0; i < CALCULATOR->variables.size(); i++) {
if (CALCULATOR->variables[i]->isActive()) {
completion_match_item(CALCULATOR->variables[i], text, l);
}
}
for (size_t i = 0; i < CALCULATOR->units.size(); i++) {
if (CALCULATOR->units[i]->isActive() &&
CALCULATOR->units[i]->subtype() != SUBTYPE_COMPOSITE_UNIT) {
completion_match_item(CALCULATOR->units[i], text, l);
}
}
}
if (index >= 0 && index < (int)matches.size()) {
char *cstr = (char *)malloc(sizeof(char) * matches[index].length() + 1);
strcpy(cstr, matches[index].c_str());
return cstr;
}
return NULL;
}
#endif
#ifdef __cplusplus
}
#endif
int enable_unicode = -1;
void handle_exit() {
CALCULATOR->abort();
if (enable_unicode >= 0) {
printops.use_unicode_signs = !enable_unicode;
}
bool b_savedefs = defs_edited > 0;
if (save_defs_on_exit &&
(CALCULATOR->checkSaveFunctionCalled() || defs_edited < 0)) {
for (size_t i = 0; !b_savedefs && i < CALCULATOR->variables.size(); i++) {
if (CALCULATOR->variables[i]->hasChanged() &&
CALCULATOR->variables[i]->category() !=
CALCULATOR->temporaryCategory() &&
CALCULATOR->variables[i]->category() != "Temporary") {
b_savedefs = true;
}
}
for (size_t i = 0; !b_savedefs && i < CALCULATOR->functions.size(); i++) {
if (CALCULATOR->functions[i]->hasChanged() &&
CALCULATOR->functions[i]->category() !=
CALCULATOR->temporaryCategory() &&
CALCULATOR->functions[i]->category() != "Temporary") {
b_savedefs = true;
}
}
for (size_t i = 0; !b_savedefs && i < CALCULATOR->units.size(); i++) {
if (CALCULATOR->units[i]->hasChanged() &&
CALCULATOR->units[i]->category() != CALCULATOR->temporaryCategory() &&
CALCULATOR->units[i]->category() != "Temporary") {
b_savedefs = true;
}
}
}
if (interactive_mode) {
if (load_defaults) {
save_history();
} else if (save_mode_on_exit) {
save_mode();
} else {
save_preferences();
}
}
if (b_savedefs)
save_defs();
if (!view_thread->write(NULL))
view_thread->cancel();
if (command_thread->running &&
(!command_thread->write((int)0) || !command_thread->write(NULL)))
command_thread->cancel();
CALCULATOR->terminateThreads();
}
#ifndef _WIN32
void sigint_handler(int) {
if (CALCULATOR->busy()) {
CALCULATOR->abort();
return;
}
bool b_interrupt = (sigint_action == 2);
#ifdef HAVE_LIBREADLINE
if (rl_end > 0) {
b_interrupt = true;
}
#endif
if (b_interrupt) {
#ifdef HAVE_LIBREADLINE
puts("> ");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
#else
puts("");
fputs("> ", stdout);
fflush(stdout);
#endif
} else {
handle_exit();
exit(0);
}
}
#endif
#ifdef HAVE_LIBREADLINE
int rlcom_tab(int, int) {
rl_complete_internal('!');
return 0;
}
#endif
int countRows(const char *str, int cols) {
int l = strlen(str);
if (l == 0)
return 1;
int r = 1, c = 0;
for (int i = 0; i < l; i++) {
if (str[i] == '\033') {
do {
i++;
} while (i < l && str[i] != 'm');
} else if ((signed char)str[i] > 0 || (unsigned char)str[i] >= 0xC0) {
if (str[i] == '\n') {
r++;
c = 0;
} else {
if (c == cols) {
r++;
c = 0;
}
c++;
}
}
}
return r;
}
int addLineBreaks(string &str, int cols, bool expr = false, size_t indent = 0,
size_t result_start = 0) {
if (cols <= 0)
return 1;
int r = 1;
size_t c = 0;
size_t lb_point = string::npos;
size_t or_point = string::npos;
int b_or = 0;
if (expr && str.find("||") != string::npos)
b_or = 2;
else if (expr && str.find(_("or")) != string::npos)
b_or = 1;
for (size_t i = 0; i < str.length(); i++) {
if (r != 1 && c == indent) {
if (str[i] == ' ') {
str.erase(i, 1);
if (i < result_start)
result_start--;
if (i >= str.length())
break;
} else if (expr && printops.digit_grouping != DIGIT_GROUPING_NONE &&
(signed char)str[i] <= 0 && (unsigned char)str[i] >= 0xC0) {
size_t l = 1;
while (i + l < str.length() && (signed char)str[i + l] <= 0 &&
(unsigned char)str[i + l] < 0xC0)
l++;
if (str.substr(i, l) == THIN_SPACE || str.substr(i, l) == NNBSP) {
str.erase(i, l);
if (i < result_start)
result_start--;
if (i >= str.length())
break;
}
}
}
while (str[i] == '\033') {
do {
i++;
} while (i < str.length() && str[i] != 'm');
i++;
if (i >= str.length())
break;
}
if ((signed char)str[i] > 0 || (unsigned char)str[i] >= 0xC0) {
if (str[i] == '\n') {
r++;
c = 0;
lb_point = string::npos;
} else {
if (c > indent) {
if (is_in(" \t", str[i])) {
if (!expr || printops.digit_grouping == DIGIT_GROUPING_NONE ||
i + 1 == str.length() || is_not_in("0123456789", str[i + 1]) ||
is_not_in("0123456789", str[i - 1])) {
if (expr || (c - indent) > (cols - indent) / 2)
lb_point = i;
if (i > result_start && b_or == 1 &&
str.length() > i + strlen("or") + 2 &&
str.substr(i + 1, strlen(_("or"))) == _("or") &&
str[i + strlen(_("or")) + 1] == ' ')
or_point = i + strlen(_("or")) + 1;
else if (i > result_start && b_or == 2 &&
str.length() > i + 2 + 2 &&
str.substr(i + 1, 2) == "||" && str[i + 2 + 1] == ' ')
or_point = i + 2 + 1;
}
} else if (expr && !printops.spacious &&
(c - indent) > (cols - indent) / 2 && c < (size_t)cols &&
is_in("+-*", str[i]) && i + 1 != str.length() &&
str[i - 1] != '^' && str[i - 1] != ' ' &&
str[i - 1] != '=' && is_not_in(" \t.;,", str[i + 1])) {
lb_point = i + 1;
}
}
if (c == (size_t)cols || or_point != string::npos) {
if (or_point != string::npos)
lb_point = or_point;
if (lb_point == string::npos) {
if (expr && printops.digit_grouping != DIGIT_GROUPING_NONE) {
if (i > 3 && str[i] <= '9' && str[i] >= '0' &&
str[i - 1] <= '9' && str[i - 1] >= '0') {
if (str[i - 2] == ' ' && str[i - 3] <= '9' && str[i - 3] >= '0')
i -= 2;
else if (str[i - 3] == ' ' && str[i - 4] <= '9' &&
str[i - 4] >= '0')
i -= 3;
else if ((str[i - 2] == '.' || str[i - 2] == ',') &&
str[i - 3] <= '9' && str[i - 3] >= '0')
i--;
else if ((str[i - 3] == '.' || str[i - 3] == ',') &&
str[i - 4] <= '9' && str[i - 4] >= '0')
i -= 2;
} else if (i > 4 && (str[i] == '.' || str[i] == ',') &&
str[i - 1] <= '9' && str[i - 1] >= '0' &&
str[i - 4] == str[i] && str[i - 5] <= '9' &&
str[i - 5] >= '0') {
i -= 3;
}
}
str.insert(i, "\n");
result_start++;
for (size_t i2 = 0; i2 < indent; i2++) {
i++;
if (i < result_start)
result_start++;
str.insert(i, " ");
}
c = indent;
} else if (str[lb_point] == ' ' || str[lb_point] == '\t') {
str[lb_point] = '\n';
for (size_t i2 = 0; i2 < indent; i2++) {
lb_point++;
if (i < result_start)
result_start++;
str.insert(lb_point, " ");
}
i = lb_point;
c = indent;
} else {
str.insert(lb_point, "\n");
result_start++;
for (size_t i2 = 0; i2 < indent; i2++) {
lb_point++;
if (i < result_start)
result_start++;
str.insert(lb_point, " ");
}
i = lb_point;
c = indent;
}
lb_point = string::npos;
or_point = string::npos;
r++;
} else {
if (str[i] == '\t')
c += 8;
else
c++;
}
}
} else if (expr && !printops.spacious && printops.use_unicode_signs &&
i + 1 < str.length() &&
((signed char)str[i + 1] > 0 ||
(unsigned char)str[i + 1] >= 0xC0)) {
if (c > indent && (c - indent) > (cols - indent) / 2 &&
is_not_in(" \t.;,", str[i + 1])) {
size_t index = i;
while (index > 0 && (signed char)str[index - 1] <= 0) {
index--;
if ((unsigned char)str[index] >= 0xC0)
break;
}
if (index > 0 && str[index - 1] != '^' && str[index - 1] != ' ' &&
str[index - 1] != '=') {
string unichar = str.substr(index, i - index + 1);
if (unichar == SIGN_MULTIPLICATION || unichar == SIGN_MULTIDOT ||
unichar == SIGN_MIDDLEDOT || unichar == SIGN_MULTIBULLET ||
unichar == SIGN_SMALLCIRCLE || unichar == SIGN_DIVISION_SLASH ||
unichar == SIGN_DIVISION || unichar == SIGN_MINUS ||
unichar == SIGN_PLUS)
lb_point = i + 1;
}
}
}
}
return r;
}
bool check_exchange_rates() {
int i = CALCULATOR->exchangeRatesUsed();
if (i == 0)
return false;
if (CALCULATOR->checkExchangeRatesDate(
auto_update_exchange_rates > 0 ? auto_update_exchange_rates : 7,
false,
auto_update_exchange_rates == 0 ||
(auto_update_exchange_rates < 0 && !ask_questions),
i))
return false;
if (auto_update_exchange_rates == 0 ||
(auto_update_exchange_rates < 0 && !ask_questions))
return false;
bool b = false;
if (auto_update_exchange_rates < 0) {
string ask_str;
int days = (int)floor(
difftime(time(NULL), CALCULATOR->getExchangeRatesTime(i)) / 86400);
int cx = snprintf(
buffer, 1000,
_n("It has been %s day since the exchange rates last were updated.",
"It has been %s days since the exchange rates last were updated.",
days),
i2s(days).c_str());
if (cx >= 0 && cx < 1000) {
ask_str = buffer;
ask_str += "\n";
}
ask_str += _("Do you wish to update the exchange rates now?");
b = ask_question(ask_str.c_str());
}
if (b || auto_update_exchange_rates > 0) {
if (auto_update_exchange_rates <= 0)
i = -1;
CALCULATOR->fetchExchangeRates(15, i);
CALCULATOR->loadExchangeRates();
return true;
}
return false;
}
#ifdef HAVE_LIBREADLINE
#define CHECK_IF_SCREEN_FILLED \
if (check_sf) { \
rcount++; \
if (rcount + 2 >= rows) { \
FPUTS_UNICODE(_("\nPress Enter to continue."), stdout); \
fflush(stdout); \
sf_c = rl_read_key(); \
if (sf_c != '\n' && sf_c != '\r') { \
check_sf = false; \
} else { \
puts(""); \
if (sf_c == '\r') { \
puts(""); \
} \
rcount = 1; \
} \
} \
}
#define CHECK_IF_SCREEN_FILLED_PUTS_RP(x, rplus) \
{ \
str_lb = x; \
int cr = 0; \
if (!cfile) { \
cr = addLineBreaks(str_lb, cols); \
} \
if (check_sf) { \
if (rcount + cr + 1 + rplus >= rows) { \
rcount += 2; \
while (rcount < rows) { \
puts(""); \
rcount++; \
} \
FPUTS_UNICODE(_("\nPress Enter to continue."), stdout); \
fflush(stdout); \
sf_c = rl_read_key(); \
if (sf_c != '\n' && sf_c != '\r') { \
check_sf = false; \
} else { \
rcount = 0; \
if (str_lb.empty() || str_lb[0] != '\n') { \
puts(""); \
if (sf_c == '\r') { \
puts(""); \
} \
rcount++; \
} \
} \
} \
if (check_sf) { \
rcount += cr; \
} \
} \
PUTS_UNICODE(str_lb.c_str()); \
}
#define CHECK_IF_SCREEN_FILLED_PUTS(x) CHECK_IF_SCREEN_FILLED_PUTS_RP(x, 0)
#define INIT_SCREEN_CHECK \
int rows = 0, cols = 0, rcount = 0; \
bool check_sf = (cfile == NULL); \
char sf_c; \
string str_lb; \
if (!cfile) \
rl_get_screen_size(&rows, &cols);
#define INIT_COLS \
int rows = 0, cols = 0; \
if (!cfile) \
rl_get_screen_size(&rows, &cols);
#define CHECK_IF_SCREEN_FILLED_HEADING_S(x) \
if (set_option.empty()) { \
str = "\n"; \
BEGIN_UNDERLINED(str); \