-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathhelp.c
1313 lines (1250 loc) · 44.5 KB
/
help.c
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
/*
* Mathomatic help command and parsing routines.
*
* Everything that depends on the command table goes here.
*
* Copyright (C) 1987-2012 George Gesslein II.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
The chief copyright holder can be contacted at gesslein@mathomatic.org, or
George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/
#include "includes.h"
#include "license.h" /* the current software license for Mathomatic */
#define CMD_REQUIRED_NCHARS 4 /* Only type this many characters to run a Mathomatic command. */
/* Set this to a high number like 50 to require all letters of a command to be typed. */
/*
* The following structure is used for each Mathomatic command.
*/
typedef struct {
char *name; /* command name to be typed by user (must not contain any spaces) */
char *secondary_name; /* another name for this command */
int (*func)(); /* function that handles this command */
/* function is passed a char pointer and returns true if successful */
char *usage; /* command syntax text */
char *info; /* one line description of command */
char *extra; /* one line extra info on command */
} com_type;
/*
* The Mathomatic command table follows. It should be in alphabetical order.
*/
static com_type com_list[] = {
/* command name, alternate name, function, usage, information */
{ "approximate", NULL, approximate_cmd, "[equation-number-ranges]", "Approximate all numerical values in equation spaces.", "\"repeat approximate\" approximates more, like calculate." },
#if !LIBRARY
{ "calculate", NULL, calculate_cmd, "[\"factor\"] [equation-number-range] [variable iterations]", "Temporarily plug in values for variables and approximate well.", "\"repeat calculate\" repeatedly prompts for any input." },
#endif
{ "clear", NULL, clear_cmd, "[equation-number-ranges]", "Delete expressions stored in memory so equation spaces can be reused.", "Tip: Use \"clear all\" to quickly restart Mathomatic." },
{ "code", NULL, code_cmd, "[\"c\" or \"java\" or \"python\" or \"integer\"] [equation-number-ranges]", "Output C, Java, or Python code for the specified equations.", "Related commands: simplify, optimize, and variables" },
{ "compare", NULL, compare_cmd, "[\"symbolic\" \"approx\"] equation-number [\"with\" equation-number]", "Compare two equation spaces for mathematical equivalence.", "This command may be preceded with \"repeat\" for full simplify." },
{ "copy", NULL, copy_cmd, "[\"select\"] [equation-number-ranges]", "Duplicate the contents of the specified equation spaces.", "With select, the first copy is made the current equation." },
{ "derivative", "differentiate", derivative_cmd, "[\"nosimplify\"] variable or \"all\" [order]", "Symbolically differentiate and simplify, order times." },
{ "display", NULL, display_cmd, "[\"factor\"] [\"simple\" or \"mixed\"] [equation-number-ranges]", "Display expressions in pretty, 2D multi-line fraction format." },
{ "divide", NULL, divide_cmd, "[base-variable] [dividend divisor]", "Divide 2 numbers or polynomials. Give detailed result and GCD.", "\"repeat divide\" repeatedly prompts for any input." },
{ "echo", NULL, echo_cmd, "[text]", "Output a line of text, followed by a newline.", "This command may be preceded with \"repeat\"." },
#if SHELL_OUT
{ "edit", NULL, edit_cmd, "[file-name]", "Edit all equation spaces or an input file, then read them in.", "Editor name in EDITOR environment variable." },
#endif
{ "eliminate", NULL, eliminate_cmd, "variables or \"all\" [\"using\" equation-number]", "Substitute the specified variables with solved equations.", "This command may be preceded with \"repeat\"." },
{ "extrema", NULL, extrema_cmd, "[variable] [order]", "Show where the slope of the current equation equals zero.", "Helps with finding the minimums and maximums." },
{ "factor", "collect", factor_cmd, "[\"number\" [integers]] or [\"power\"] [equation-number-range] [variables]", "Factor variables in equation spaces or factorize given integers." },
{ "for", NULL, for_cmd, "variable start end [step-size]", "Evaluate and display the current expression for each value of variable.", "Same syntax as the sum and product commands." },
{ "fraction", NULL, fraction_cmd, "[\"numerator\" \"denominator\"] [equation-number-range]", "Convert expression to a single simple algebraic fraction.", "This command may be preceded with \"repeat\"." },
#if HELP
{ "help", "?", help_cmd, "[topics or command-names]", "Short, built-in help and reference." },
#endif
{ "imaginary", NULL, imaginary_cmd, "[variable]", "Fully expand and copy the imaginary part of the current expression.", "Related command: real" },
{ "integrate", "integral", integrate_cmd, "[\"constant\" or \"definite\"] variable [order [lower and upper-bounds]]", "Symbolically integrate polynomials order times, then simplify." },
{ "laplace", NULL, laplace_cmd, "[\"inverse\"] variable", "Compute the Laplace or inverse Laplace transform of polynomials.", "This command only works with polynomials." },
{ "limit", NULL, limit_cmd, "variable expression", "Take the limit as variable goes to expression.", "This limit command is experimental." },
{ "list", NULL, list_cmd, "[\"export\" or \"maxima\" or \"gnuplot\" or \"hex\"] [equation-number-ranges]", "Display equation spaces in single-line (one-dimensional) format.", "Options to export expressions to other math programs." },
{ "nintegrate", NULL, nintegrate_cmd, "[\"trapezoid\"] variable [partitions [lower and upper-bounds]]", "Do numerical definite integration using Simpson's rule.", "This command cannot integrate over singularities." },
{ "optimize", NULL, optimize_cmd, "[equation-number-range]", "Split up equations into smaller, more efficient equations." },
{ "pause", NULL, pause_cmd, "[text]", "Display a line of text and wait for user to press the Enter key." },
#if SHELL_OUT
{ "plot", NULL, plot_cmd, "[equation-number-ranges] [xyz-ranges] [gnuplot-expressions,]", "Automatically plot multiple expressions in 2D or 3D with gnuplot.", "Plots variable x; if expression contains y, do a 3D surface plot." },
#endif
{ "product", NULL, product_cmd, "variable start end [step-size]", "Compute the product as variable goes from start to end.", "Related command: sum" },
#if READLINE || EDITLINE
{ "push", NULL, push_cmd, "[equation-number-ranges or text-to-push]", "Push equation spaces or text into readline history for editing.", "Available only if readline is enabled." },
#endif
#if !LIBRARY
{ "quit", "exit", quit_cmd, "[exit-value]", "Terminate this program without saving." },
#endif
#if !SECURE
{ "read", NULL, read_cmd, "[file-name or directory]", "Display/change directory, or read in a text file as if it was typed in.", "\"repeat read\" will read in a file repeatedly until failure." },
#endif
{ "real", NULL, real_cmd, "[variable]", "Fully expand and copy the real part of the current expression.", "Related command: imaginary" },
{ "replace", NULL, replace_cmd, "[variables [\"with\" expression]]", "Substitute variables in the current equation with expressions.", "This command may be preceded with \"repeat\"." },
{ "roots", NULL, roots_cmd, "root real-part imaginary-part", "Display all the roots of a complex number.", "\"repeat roots\" repeatedly prompts for any input." },
#if !SECURE
{ "save", NULL, save_cmd, "file-name", "Save all equation spaces in a text file.", "Related command: read" },
#endif
{ "set", NULL, set_cmd, "[[\"no\"] option [value]] ...", "Display, set, or \"save\" current session options.", "\"set\" by itself will show all current option settings." },
{ "simplify", NULL, simplify_cmd, "[\"sign\" \"symbolic\" \"quick[est]\" \"fraction\"] [equation-number-ranges]", "Completely simplify expressions.", "This command may be preceded with \"repeat\" for full simplify." },
{ "solve", NULL, solve_cmd, "[\"verify\" or \"verifiable\"] [equation-number-range] [\"for\"] expression", "Solve the specified equations for a variable or for zero.", "The verify options check all returned solutions for correctness." },
{ "sum", NULL, sum_cmd, "variable start end [step-size]", "Compute the summation as variable goes from start to end.", "Related command: product" },
#if !LIBRARY
{ "tally", NULL, tally_cmd, "[\"average\"] [equation-number-ranges]", "Add entries, specified and prompted for, showing total." },
#endif
{ "taylor", NULL, taylor_cmd, "[\"nosimplify\"] variable order point", "Compute the Taylor series expansion of the current expression." },
{ "unfactor", "expand", unfactor_cmd, "[\"count\" \"fraction\" \"quick\" \"power\"] [equation-number-range]", "Algebraically expand (multiply out) expressions." },
{ "variables", NULL, variables_cmd, "[\"c\" \"java\" \"integer\" \"count\"] [equation-number-ranges]", "Show all variable names used within the specified expressions.", "Related command: code" },
{ "version", NULL, version_cmd, "[\"status\"]", "Display Mathomatic version, status, and compiler information." }
};
#if HELP
char *example_strings[] = {
"; Example 1:\n",
"; Here the derivative of the absolute value function is computed.\n",
"; Expressions are entered by just typing them in:\n",
"|x| ; The absolute value of x\n",
"derivative ; The result gives the sign of x:\n",
"pause\n",
"repeat echo -\n",
"; Example 2:\n",
#if !LIBRARY
"; Here the calculate command is used to plug values into a solved formula.\n",
"; A common temperature conversion formula (from \"help conversions\"):\n",
"fahrenheit = (9*celsius/5) + 32\n",
"repeat calculate ; plug in values until an empty line is entered\n",
"\n",
"; Solve for the other variable and simplify the result:\n",
"solve for celsius\n",
"simplify\n",
"repeat calculate ; plug in values until an empty line is entered\n",
"\n",
"variables count; count all variables that occur in expressions\n",
"pause\n",
"repeat echo -\n",
"; Example 3:\n",
#endif
"; Expand the following to polynomial form, then refactor and differentiate:\n",
"(x+y+z)^3\n",
"expand count ; Expand and count the resulting number of terms:\n",
"pause\n",
"simplify ; refactor:\n",
"derivative x ; here is the derivative, with respect to x:\n",
"expand count ; and its term count, when expanded:\n",
NULL
};
#endif
#if HELP
char *geometry_strings[] = {
"; Triangle area, \"b\" is the \"base\" side:\n",
"triangle_area = b*height/2\n",
"; Here is Heron's formula for the area of any triangle\n",
"; given all three side lengths (\"a\", \"b\", and \"c\"):\n",
"triangle_area = (((a + b + c)*(a - b + c)*(a + b - c)*(b - a + c))^(1/2))/4\n",
"\n",
"; Rectangle of length \"l\" and width \"w\":\n",
"rectangle_area = l*w\n",
"rectangle_perimeter = 2*l + 2*w\n",
"\n",
"; Trapezoid of parallel sides \"a\" and \"b\",\n",
"; and the \"distance\" between them:\n",
"trapezoid_area = distance*(a + b)/2\n",
"\n",
"; Circle of radius \"r\":\n",
"circle_area = pi*r^2\n",
"circle_perimeter = 2*pi*r\n",
"\n",
"; 3D rectangular solid of length \"l\", width \"w\", and height \"h\":\n",
"brick_volume = l*w*h\n",
"brick_surface_area = 2*l*w + 2*l*h + 2*w*h\n",
"\n",
"; 3D sphere of radius \"r\":\n",
"sphere_volume = 4/3*pi*r^3\n",
"sphere_surface_area = 4*pi*r^2\n",
"\n",
"; Convex 2D polygon with straight sides,\n",
"; sum of all interior angles formula in degree, radian, and gradian units:\n",
"sum_degrees = (sides - 2)*180\n",
"sum_radians = (sides - 2)*pi\n",
"sum_grads = (sides - 2)*180*10/9 ; Rarely used gradian formula.\n",
"; \"sides\" is the number of sides of any convex 2D polygon.\n",
"; Convex means that all interior angles are less than 180 degrees.\n",
"; Type \"elim sides\" to get the radians/degrees/grads conversion formulas.\n",
NULL
};
char *conversion_strings[] = {
"; Temperature\n",
"fahrenheit = (9*celsius/5) + 32\n",
"kelvin = celsius + 273.15\n",
"; Distance\n",
"inches = centimeters/2.54\n",
"miles = kilometers/1.609344\n",
"; Weight\n",
"pounds = kilograms/0.45359237\n",
NULL
};
#endif
/*
* Process mathematical expression input in Mathomatic,
* with no solving and no automatic calculation.
*
* Simply parse the equation or expression text in "cp"
* and place the result in equation space "n".
*
* Return true if successful.
*/
int
parse(n, cp)
int n;
char *cp;
{
if (parse_equation(n, cp)) {
if (n_lhs[n] == 0 && n_rhs[n] == 0)
return true;
if (n_lhs[n] == 0) {
/* RHS expression only, set equal to zero */
n_lhs[n] = 1;
lhs[n][0] = zero_token;
}
cur_equation = n;
return return_result(cur_equation);
}
return false;
}
/*
* Process main prompt equation and expression input in Mathomatic;
* either swapping equation sides, selecting an equation space (autoselect),
* solving the current equation (autosolve), calculating a numerical expression (autocalc),
* or storing a new or modified equation or expression.
* Here we can apply identical operations to both sides of an equation, too.
*
* Parse the mathematical expression text in "cp" and perform one of the above operations,
* or store it in equation space "n" and display.
*
* Return true if successful.
*/
int
process_parse(n, cp)
int n;
char *cp;
{
int i;
char *cp1, *ep;
int equals_flag = false;
#if LIBRARY
int previous_repeat_flag;
#endif
int rv;
int op = 0;
static int last_autocalc_en = -1;
long answer_v = 0; /* Mathomatic answer variable */
if (cp == NULL)
return false;
if ((cp1 = strchr(cp, '=')) != NULL) {
if (strrchr(cp, '=') == cp1 && is_mathomatic_operator(cp[0]) && cp[0] != '='
&& is_mathomatic_operator(cp[1])) {
if (cp[1] == '=') {
switch (cp[0]) {
case '+':
op = PLUS;
break;
case '-':
op = MINUS;
break;
case '*':
op = TIMES;
break;
case '/':
op = DIVIDE;
break;
case '^':
op = POWER;
break;
case '%':
op = MODULUS;
break;
}
} else if (cp[2] == '=') {
if (cp[0] == '*' && cp[1] == '*') {
op = POWER;
} else if (cp[0] == '/' && cp[1] == '/') {
op = IDIVIDE;
}
}
}
if (op) {
if (cur_equation == n || empty_equation_space(cur_equation)) {
error(_("No current equation to manipulate."));
return false;
}
input_column += (cp1 + 1) - cp;
if (parse_equation(n, cp1 + 1) == NULL) {
return false;
}
if (n_lhs[n] <= 0 || n_rhs[n] != 0) {
error(_("Syntax error."));
n_lhs[n] = 0;
n_rhs[n] = 0;
return false;
}
if (n_lhs[cur_equation] + 1 + n_lhs[n] > n_tokens
|| n_rhs[cur_equation] + 1 + n_lhs[n] > n_tokens) {
n_lhs[n] = 0;
n_rhs[n] = 0;
error_huge();
}
for (i = 0; i < n_lhs[cur_equation]; i++) {
lhs[cur_equation][i].level++;
}
lhs[cur_equation][i].kind = OPERATOR;
lhs[cur_equation][i].level = 1;
lhs[cur_equation][i].token.operatr = op;
i++;
blt(&lhs[cur_equation][i], lhs[n], n_lhs[n] * sizeof(token_type));
n_lhs[cur_equation] += 1 + n_lhs[n];
for (; i < n_lhs[cur_equation]; i++) {
lhs[cur_equation][i].level++;
}
if (n_rhs[cur_equation] > 0) {
for (i = 0; i < n_rhs[cur_equation]; i++) {
rhs[cur_equation][i].level++;
}
rhs[cur_equation][i].kind = OPERATOR;
rhs[cur_equation][i].level = 1;
rhs[cur_equation][i].token.operatr = op;
i++;
blt(&rhs[cur_equation][i], lhs[n], n_lhs[n] * sizeof(token_type));
n_rhs[cur_equation] += 1 + n_lhs[n];
for (; i < n_rhs[cur_equation]; i++) {
rhs[cur_equation][i].level++;
}
}
n_lhs[n] = 0;
n_rhs[n] = 0;
simp_equation(cur_equation);
return return_result(cur_equation);
}
}
if ((ep = parse_equation(n, cp))) {
for (cp1 = cp; cp1 < ep; cp1++) {
if (*cp1 == '=') {
equals_flag = true;
break;
}
}
if (n_lhs[n] == 0 && n_rhs[n] == 0) {
if (strcmp(cp, "=") == 0 && cur_equation != n && equation_space_is_equation(cur_equation)) {
debug_string(0, _("Swapping both sides of the current equation..."));
n = cur_equation;
i = n_lhs[n];
blt(scratch, lhs[n], i * sizeof(token_type));
n_lhs[n] = n_rhs[n];
blt(lhs[n], rhs[n], n_rhs[n] * sizeof(token_type));
n_rhs[n] = i;
blt(rhs[n], scratch, i * sizeof(token_type));
return return_result(cur_equation);
}
return true;
}
if (n_lhs[n] == 0 || n_rhs[n] == 0) {
if (equals_flag && cur_equation != n && !empty_equation_space(cur_equation) && n_rhs[cur_equation] == 0) {
debug_string(0, _("Combining to make an equation out of the current non-equation."));
if (n_lhs[n]) {
/* copy the LHS to the RHS */
blt(rhs[cur_equation], lhs[cur_equation], n_lhs[cur_equation] * sizeof(token_type));
n_rhs[cur_equation] = n_lhs[cur_equation];
blt(lhs[cur_equation], lhs[n], n_lhs[n] * sizeof(token_type));
n_lhs[cur_equation] = n_lhs[n];
} else if (n_rhs[n]) {
blt(rhs[cur_equation], rhs[n], n_rhs[n] * sizeof(token_type));
n_rhs[cur_equation] = n_rhs[n];
}
n_lhs[n] = 0;
n_rhs[n] = 0;
return return_result(cur_equation);
}
if (autosolve || equals_flag) {
if ((n_lhs[n] == 1 && ((lhs[n][0].kind == CONSTANT && lhs[n][0].token.constant == 0.0)
|| (lhs[n][0].kind == VARIABLE && ((lhs[n][0].token.variable & VAR_MASK) > SIGN || equals_flag))))
|| (n_rhs[n] == 1 && ((rhs[n][0].kind == CONSTANT && rhs[n][0].token.constant == 0.0)
|| rhs[n][0].kind == VARIABLE))) {
rv = solve_espace(n, cur_equation);
n_lhs[n] = 0;
n_rhs[n] = 0;
if (rv) {
return return_result(cur_equation);
} else {
return false;
}
}
}
if (!equals_flag && autoselect && n_lhs[n] == 1 && lhs[n][0].kind == CONSTANT && fmod(lhs[n][0].token.constant, 1.0) == 0.0
&& lhs[n][0].token.constant > 0.0 && lhs[n][0].token.constant <= n_equations) {
/* easy selecting of equation spaces by just typing in the equation number */
cur_equation = lhs[n][0].token.constant - 1;
n_lhs[n] = 0;
return_result(cur_equation);
return true;
}
if (autocalc) {
/* the numerical input calculation */
if (n_lhs[n]) {
if (!exp_is_numeric(lhs[n], n_lhs[n])) {
goto set_equal_to_zero; /* not numerical (contains a variable) */
}
/* copy the LHS to the RHS */
blt(rhs[n], lhs[n], n_lhs[n] * sizeof(token_type));
n_rhs[n] = n_lhs[n];
}
if (exp_is_numeric(rhs[n], n_rhs[n])) {
/* make the expression an equation by making the LHS the "answer" variable */
lhs[n][0].level = 1;
lhs[n][0].kind = VARIABLE;
parse_var(&answer_v, "answer"); /* convert to a Mathomatic variable */
lhs[n][0].token.variable = answer_v;
n_lhs[n] = 1;
/* make it the current equation and run the calculate command on it */
cur_equation = n;
#if LIBRARY
previous_repeat_flag = repeat_flag;
repeat_flag = true; /* act like the calculate command does */
rv = approximate_cmd(""); /* display an approximation even when using the Symbolic Math Library, unless autocalc is false */
repeat_flag = previous_repeat_flag;
#else
debug_string(0, _("Calculating..."));
rv = calculate_cmd(""); /* display the approximation */
#endif
/* Keep the current input until next autocalc, then delete if "set autodelete". */
i = last_autocalc_en;
if (autodelete && i >= 0 && i < n_equations) {
if (i != n && n_lhs[i] == 1 && lhs[i][0].kind == VARIABLE
&& lhs[i][0].token.variable == answer_v) {
/* delete previous answer from memory */
n_lhs[i] = 0;
n_rhs[i] = 0;
}
}
last_autocalc_en = n;
return rv;
}
}
set_equal_to_zero:
if (equals_flag) {
debug_string(0, _("Setting new algebraic expression equal to zero."));
if (n_rhs[n]) {
/* RHS expression only with equals sign; set equal to zero */
n_lhs[n] = 1;
lhs[n][0] = zero_token;
} else if (n_lhs[n]) {
/* LHS expression only with equals sign; set equal to zero */
n_rhs[n] = 1;
rhs[n][0] = zero_token;
}
}
}
cur_equation = n;
return return_result(cur_equation);
}
return false;
}
/*
* Run a line of main prompt input to Mathomatic.
* It may be a command, an expression or equation to store, etc.
*
* Return true if line starts with a colon (:) or if successful.
*/
int
process(cp)
char *cp;
{
if (cp && cp[0] == ':') {
input_column++;
previous_return_value = process_rv(cp + 1);
return true;
} else {
previous_return_value = process_rv(cp);
if (!previous_return_value) {
debug_string(1, "Error return.");
}
return previous_return_value;
}
}
/*
* Run a line of main prompt input to Mathomatic.
* It may be a command, an expression or equation to store, etc.
*
* Return true if successful.
*/
int
process_rv(cp)
char *cp;
{
char *cp1 = NULL;
char *cp_start;
int i;
int len;
int rv;
char buf2[MAX_CMD_LEN]; /* do not make this static! */
int our_repeat_flag = false;
long v; /* Mathomatic variable */
#if !SECURE
FILE *fp;
int i1;
#endif
#if DEBUG
check_gvars();
#endif
init_gvars(); /* make sure we are in the default state */
set_sign_array(); /* register all sign variables so that the next ones will be unique */
if (cp == NULL) {
return false;
}
cp_start = cp;
cp = skip_space(cp);
/* handle search forward */
if (*cp == '/' && isvarchar(cp[1])) {
cp++;
debug_string(0, ("Searching forwards for variable."));
if ((cp1 = parse_var(&v, cp)) == NULL) {
return false;
}
if (extra_characters(cp1)) {
return false;
}
if (search_all_for_var(v, true)) {
return return_result(cur_equation);
} else {
error(_("Variable not found in any equation space."));
return false;
}
}
/* handle the equation selector */
if (*cp == '#') {
cp++;
if (isvarchar(*cp)) {
debug_string(0, ("Searching backwards for variable."));
if ((cp1 = parse_var(&v, cp)) == NULL) {
return false;
}
if (extra_characters(cp1)) {
return false;
}
if (search_all_for_var(v, false)) {
return return_result(cur_equation);
} else {
error(_("Variable not found in any equation space."));
return false;
}
}
i = cur_equation;
cp1 = cp;
switch (*cp) {
case '+':
case '-':
i += strtol(cp, &cp1, 10);
break;
default:
if (isdigit(*cp)) {
i = strtol(cp, &cp1, 10) - 1;
}
break;
}
if (cp1 == NULL || cp == cp1) {
return true; /* treat as comment */
}
if (*cp1 == '\0' || *cp1 == ':' || isspace(*cp1)) {
if (!alloc_to_espace(i)) {
put_up_arrow(cp - cp_start, _("Equation number out of range."));
return false;
}
cp = cp1;
if (*cp == ':') {
cp++;
}
cp = skip_space(cp);
if (*cp) {
input_column += (cp - cp_start);
return parse(i, cp);
}
cur_equation = i;
return_result(cur_equation);
return true;
} else {
cp--;
}
}
#if SHELL_OUT
/* handle shell escape */
if (*cp == '!') {
if (security_level > 0) {
error(_("Shelling out disabled by security level."));
return false;
}
cp = skip_space(cp + 1);
if (*cp == '\0' && security_level < 0) {
error(_("Running an interactive shell is not possible with m4."));
return false;
}
#if MINGW
cp1 = "cmd";
#else
cp1 = getenv("SHELL");
if (cp1 == NULL) {
cp1 = "/bin/sh";
}
#endif
#if 0
if (*cp1 == '/' && access(cp1, X_OK)) {
perror(cp1);
error(_("Shell not found or not executable, check SHELL environment variable."));
return false;
}
#endif
rv = shell_out(*cp ? cp : cp1);
return !rv;
}
#endif
#if HELP
/* a quick way to get help */
if (*cp == '?') {
cp = skip_space(cp + 1);
input_column += (cp - cp_start);
return(help_cmd(cp));
}
#endif
/* See if the string pointed to by cp is a command. */
/* If so, execute it. */
do_repeat:
cp1 = cp;
while (*cp1 && !isspace(*cp1))
cp1++;
len = cp1 - cp; /* length of possible command name in cp */
#define COMPARE_COMMAND_NAME(str) (len >= min(CMD_REQUIRED_NCHARS, strlen(str)) \
&& len <= strlen(str) && strncasecmp(cp, str, len) == 0)
if (COMPARE_COMMAND_NAME("repeat")) {
our_repeat_flag = true;
cp = skip_space(cp1);
goto do_repeat;
}
for (i = 0; i < ARR_CNT(com_list); i++) {
if (COMPARE_COMMAND_NAME(com_list[i].name)
|| (com_list[i].secondary_name && COMPARE_COMMAND_NAME(com_list[i].secondary_name))) {
cp1 = skip_space(cp1);
input_column += (cp1 - cp_start);
/* Copy the command-line to buf2 and use it, because the original string may be overwritten. */
if (my_strlcpy(buf2, cp1, sizeof(buf2)) >= sizeof(buf2)) {
error(_("Command-line too long."));
return false;
}
#if !SECURE
fp = NULL;
if (security_level < 2) {
/* handle output redirection */
gfp_append_flag = false;
gfp_filename = NULL;
for (i1 = strlen(buf2) - 1; i1 >= 0; i1--) {
if (buf2[i1] == '>') {
gfp_filename = skip_space(&buf2[i1+1]);
if (i1 && buf2[i1-1] == '>') {
i1--;
gfp_append_flag = true;
}
buf2[i1] = '\0';
break;
}
}
if (gfp_filename) {
if (gfp_append_flag) {
fp = fopen(gfp_filename, "a");
} else {
fp = fopen(gfp_filename, "w");
}
if (fp == NULL) {
perror(gfp_filename);
error(_("Can't open redirected output file for writing."));
gfp_filename = NULL;
return false;
}
if (gfp != stdout && gfp != stderr && gfp != default_out) {
fclose(gfp); /* make sure previous redirection file is closed */
}
gfp = fp;
}
}
#endif
remove_trailing_spaces(buf2);
pull_number = 1;
show_usage = true;
repeat_flag = our_repeat_flag;
/* execute the command by calling the command function */
rv = (*com_list[i].func)(buf2);
repeat_flag = false;
#if !SECURE
if (fp && gfp != default_out) {
if (gfp != stdout && gfp != stderr)
fclose(gfp);
gfp = default_out;
}
gfp_filename = NULL;
#endif
#if !SILENT && !LIBRARY
if (!rv) {
if (show_usage && debug_level >= 0) {
printf("Command usage: %s %s\n", com_list[i].name, com_list[i].usage);
#if DEBUG
} else if (!test_mode && !demo_mode) {
printf(_("Command returned with error.\n"));
#endif
}
}
#endif
return rv;
}
}
if (our_repeat_flag) {
error(_("Follow \"repeat\" with a command to automatically repeat."));
return false;
}
/* cp is not a command, so parse the expression */
i = next_espace();
input_column += (cp - cp_start);
return process_parse(i, cp);
}
/*
* Display and process Mathomatic main prompt input.
* Used by the read command and "help examples", "help conversions", and "help geometry".
* The input string will be shortened by set_error_level().
*
* Return true if successful.
*/
int
display_process(cp)
char *cp; /* String to process; will be modified, so do not use constant strings. */
{
int len;
int nlt; /* true if cp is newline terminated */
if (cp == NULL)
return false;
#if !LIBRARY
error_str = NULL;
warning_str = NULL;
#endif
len = strlen(cp);
if (len > 0)
len--;
nlt = (cp[len] == '\n');
input_column = 0;
#if !SILENT || !LIBRARY
if (!quiet_mode) {
set_color(3); /* blue prompt */
input_column = printf("%d%s", cur_equation + 1, html_flag ? HTML_PROMPT_STR : PROMPT_STR);
default_color(false);
if (html_flag) {
input_column -= (strlen(HTML_PROMPT_STR) - strlen(PROMPT_STR));
printf("<b>%s</b>", cp); /* make input bold */
} else {
printf("%s", cp);
}
if (!nlt)
printf("\n");
}
#endif
if (gfp != stdout && gfp != stderr) {
if (html_flag == 2) {
set_color(3);
input_column = fprintf(gfp, "%d%s", cur_equation + 1, HTML_PROMPT_STR);
default_color(false);
input_column -= (strlen(HTML_PROMPT_STR) - strlen(PROMPT_STR));
fprintf(gfp, "<b>%s</b>", cp);
} else {
input_column = fprintf(gfp, "%d%s", cur_equation + 1, PROMPT_STR);
fprintf(gfp, "%s", cp);
}
if (!nlt)
fprintf(gfp, "\n");
}
set_error_level(cp);
return process(cp);
}
#if SHELL_OUT
/*
* Execute a shell command. Note that system(3) requires "/bin/sh".
*
* Returns exit status of command (0 if no error).
*/
int
shell_out(cp)
char *cp; /* shell command string */
{
int rv;
if (security_level > 0) {
error(_("Shelling out disabled by security level."));
return -1;
}
#if !SILENT
if (debug_level > 0) {
fprintf(gfp, _("Running shell command-line: %s\n"), cp);
}
#endif
reset_attr();
errno = 0;
rv = system(cp);
if (rv < 0) {
perror("system(3) call failed");
}
printf("\n");
default_color(false);
if (rv) {
show_usage = false; /* already shows enough */
}
return rv;
}
#endif
/*
* Parse a variable name with before and after space and comma character skipping.
*
* Return new position in string or NULL if error.
*/
char *
parse_var2(vp, cp)
long *vp; /* pointer to returned variable in Mathomatic internal format */
char *cp; /* pointer to variable name string */
{
cp = skip_comma_space(cp);
cp = parse_var(vp, cp);
if (cp == NULL) {
return NULL;
}
return skip_comma_space(cp);
}
#if HELP
/*
* Display command usage info in color.
*
* Return number of lines displayed.
*/
int
display_usage(pstr, i)
char *pstr; /* prefix string */
int i;
{
int len = 0;
len += fprintf(gfp, "%s", pstr);
set_color(0);
len += fprintf(gfp, "%s", com_list[i].name);
default_color(false);
len += fprintf(gfp, " %s\n", com_list[i].usage);
if (screen_columns && len > screen_columns) {
return 2;
} else {
return 1;
}
}
/*
* Output command info and usage.
*
* Return the number of lines output.
*/
int
display_command(i)
int i; /* command table index of command */
{
int rows = 2;
fprintf(gfp, "%s - %s\n", com_list[i].name, com_list[i].info);
rows += display_usage("Usage: ", i);
if (com_list[i].secondary_name) {
fprintf(gfp, "Alternate name for this command: %s\n", com_list[i].secondary_name);
rows++;
}
if (com_list[i].extra) {
fprintf(gfp, "%s\n", com_list[i].extra);
rows++;
}
fprintf(gfp, "\n");
#if DEBUG
if (com_list[i].secondary_name && com_list[i].extra) {
error_bug("Alternate name and extra info fields both set for this command, only one or the other is currently allowed.");
}
#endif
return rows;
}
/*
* Output repeat command info and usage.
*
* Return the number of lines output.
*/
int
display_repeat_command(void)
{
EP("repeat - Automatically repeat the following command over and over.");
fprintf(gfp, "Usage: ");
set_color(0);
fprintf(gfp, "repeat");
default_color(false);
fprintf(gfp, " command arguments\n");
EP("Not all commands are repeatable.\n");
return 3;
}
int
read_examples(cpp)
char **cpp;
{
int i;
char *cp;
for (i = 0; cpp[i]; i++) {
cp = strdup(cpp[i]);
if (cp == NULL)
return false;
if (!display_process(cp)) {
free(cp);
return false;
}
free(cp);
}
return true;
}
/*
* Display a row of dashes to underline a title.
*/
void
underline_title(count)
int count; /* length of title, including newline */
{
#if !NOT80COLUMNS
int i;
for (i = 1; i < count; i++) {
fprintf(gfp, "-");
}
#endif
fprintf(gfp, "\n");
}
/*
* The help command.
*/
int
help_cmd(cp)
char *cp;
{
int i, j;
char *cp1;
int flag, html_out;
int row;
html_out = ((html_flag == 2) || (html_flag && gfp == stdout));
if (*cp == '\0') {
intro:
/* default help text: */
SP("Mathomatic is a Computer Algebra System (CAS) and calculator program.");
#if !LIBRARY
SP("Type \"help options\" for a list of shell command-line startup options.");
#endif
SP("For helpful interactive examples, \"help examples\". For news, \"help news\".");
SP("Type \"help equations\" for help with entering expressions and equations.");
SP("Type \"help all\" for a summary of all commands or \"help usage\" just for syntax.");
SP("Other help topics: constants, color, license, bugs, geometry, or conversions.");
SP("\"help\" or \"?\" followed by a command name will give info on that command.");
fprintf(gfp, "These are the %d commands for this version of Mathomatic:\n", ARR_CNT(com_list));
set_color(0);
for (i = 0; i < ARR_CNT(com_list); i++) {
if ((i % 5) == 0)
fprintf(gfp, "\n");
j = 15 - fprintf(gfp, "%s", com_list[i].name);
for (; j > 0; j--)
fprintf(gfp, " ");
}
default_color(false);
SP("\n\nTo see what is allowed at the main prompt, type \"help main\".");
EP("For more help, go to the official website: www.mathomatic.org");