-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy patham.c
1656 lines (1547 loc) · 34.8 KB
/
am.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
/*
* Standard routines for Mathomatic.
*
* 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 [email protected], or
George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/
#include "includes.h"
#if UNIX || CYGWIN
#include <sys/ioctl.h>
#include <termios.h>
#endif
/*
* Display the main Mathomatic startup message.
* fp is the output file stream pointer it goes to, and should be stdout or gfp,
* but really can go to any file you wish.
*/
void
display_startup_message(fp)
FILE *fp; /* output file pointer */
{
long es_size;
#if SECURE
fprintf(fp, _("Secure "));
#else
if (security_level >= 2)
fprintf(fp, _("Secure "));
else if (security_level == -1)
fprintf(fp, "m4 ");
#endif
fprintf(fp, "Mathomatic version %s\n", VERSION);
if (html_flag)
fprintf(fp, "Copyright © 1987-2012 George Gesslein II.\n");
else
fprintf(fp, "Copyright (C) 1987-2012 George Gesslein II.\n");
es_size = (long) n_tokens * sizeof(token_type) * 2L / 1000L;
if (es_size >= 1000) {
fprintf(fp, _("%d equation spaces available in RAM; %ld megabytes per equation space.\n"),
N_EQUATIONS, (es_size + 500L) / 1000L);
} else {
fprintf(fp, _("%d equation spaces available in RAM; %ld kilobytes per equation space.\n"),
N_EQUATIONS, es_size);
}
}
/*
* Standard function to report an error to the user.
*/
void
error(str)
const char *str; /* constant string to display */
{
error_str = str; /* save reference to str, must be a constant string, temporary strings don't work */
#if !SILENT && !LIBRARY
set_color(2); /* set color to red */
printf("%s\n", str);
default_color(false); /* restore to default color */
#endif
}
/*
* Reset last call to error(), as if it didn't happen.
*/
void
reset_error(void)
{
#if !SILENT && !LIBRARY
if (error_str)
printf(_("Forgetting previous error.\n"));
#endif
error_str = NULL;
}
/*
* Standard function to report a warning only once to the user.
* A warning is less serious than an error.
*/
void
warning(str)
const char *str; /* constant string to display */
{
int already_warned = false;
if (warning_str) {
if (strcmp(str, warning_str) == 0)
already_warned = true;
}
warning_str = str; /* save reference to str, must be a constant string, temporary strings don't work */
#if !SILENT && !LIBRARY
if (!already_warned && debug_level >= -1) {
set_color(1); /* set color to yellow */
printf("Warning: %s\n", str);
default_color(false); /* restore to default color */
}
#endif
}
/*
* This is called when the maximum expression size has been exceeded.
*
* There is no return.
*/
void
error_huge(void)
{
longjmp(jmp_save, 14);
}
/*
* This is called when a bug test result is positive.
*
* There is no return.
*/
void
error_bug(str)
const char *str; /* constant string to display */
{
/* Return and display the passed error message in str. */
error(str); /* str must be a constant string, temporary strings don't work */
#if SILENT || LIBRARY
printf("%s\n", str);
#endif
printf(_("Please report this bug to the maintainers,\n"));
printf(_("along with the entry sequence that caused it.\n"));
#if !LIBRARY
printf(_("Type \"help bugs\" for info on how to report bugs found in this program.\n"));
#endif
longjmp(jmp_save, 13); /* Abort the current operation with the critical error number 13. */
}
/*
* Check if a floating point math function flagged an error.
*
* There is no return if an error message is displayed.
*/
void
check_err(void)
{
switch (errno) {
case EDOM:
errno = 0;
if (domain_check) {
domain_check = false;
} else {
error(_("Domain error in constant."));
longjmp(jmp_save, 2);
}
break;
case ERANGE:
errno = 0;
error(_("Floating point constant out of range."));
longjmp(jmp_save, 2);
break;
}
}
/*
* Get the current screen (window) width and height from the operating system.
*
* Return true if the global integers screen_columns and/or screen_rows were set.
*/
int
get_screen_size(void)
{
int rv = false;
#if UNIX || CYGWIN
struct winsize ws;
ws.ws_col = 0;
ws.ws_row = 0;
if (ioctl(1, TIOCGWINSZ, &ws) >= 0) {
if (ws.ws_col > 0) {
screen_columns = ws.ws_col;
rv = true;
}
if (ws.ws_row > 0) {
screen_rows = ws.ws_row;
rv = true;
}
}
#else
screen_columns = STANDARD_SCREEN_COLUMNS;
screen_rows = STANDARD_SCREEN_ROWS;
rv = true;
#endif
return rv;
}
/*
* Allocate the display lines in the vscreen[] array.
* Call this before using vscreen[].
*
* Return true with vscreen[] allocated to TEXT_ROWS*current_columns characters if successful.
*/
int
malloc_vscreen(void)
{
int i;
if (current_columns == 0 || ((screen_columns > 0) ? (current_columns != screen_columns) : (current_columns != TEXT_COLUMNS))) {
if (screen_columns > 0) {
current_columns = screen_columns;
} else {
current_columns = TEXT_COLUMNS;
}
for (i = 0; i < TEXT_ROWS; i++) {
if (vscreen[i]) {
free(vscreen[i]);
}
vscreen[i] = malloc(current_columns + 1);
if (vscreen[i] == NULL) {
error(_("Out of memory (can't malloc(3))."));
current_columns = 0;
return false;
}
}
}
return true;
}
/*
* Allocate the needed global expression storage arrays.
* Each is static and can hold n_tokens elements.
* n_tokens must not change until Mathomatic terminates.
*
* init_mem() is called only once upon Mathomatic startup
* before using the symbolic math engine,
* and can be undone by calling free_mem() below.
*
* Returns true if successful, otherwise Mathomatic cannot be used.
*/
int
init_mem(void)
{
if (n_tokens <= 0)
return false;
if ((scratch = (token_type *) malloc(((n_tokens * 3) / 2) * sizeof(token_type))) == NULL
|| (tes = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL
|| (tlhs = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL
|| (trhs = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL) {
return false;
}
if (alloc_next_espace() < 0) { /* make sure there is at least 1 equation space */
return false;
}
clear_all();
return true;
}
#if LIBRARY || VALGRIND
/*
* Free the global expression storage arrays and other known memory buffers.
* After calling this, memory usage is reset and Mathomatic becomes unusable,
* so do not call unless finished with using the Mathomatic code.
*
* This routine is usually not needed, because when a program exits,
* all the memory it allocated is released by the operating system.
* Inclusion of this routine was requested by Tam Hanna for use with Symbian OS.
*/
void
free_mem(void)
{
int i;
clear_all();
free(scratch);
free(tes);
free(tlhs);
free(trhs);
for (i = 0; i < N_EQUATIONS; i++) {
if (lhs[i]) {
free(lhs[i]);
lhs[i] = NULL;
}
if (rhs[i]) {
free(rhs[i]);
rhs[i] = NULL;
}
}
n_equations = 0;
for (i = 0; i < TEXT_ROWS; i++) {
if (vscreen[i]) {
free(vscreen[i]);
vscreen[i] = NULL;
}
}
current_columns = 0;
}
#endif
#if DEBUG
/*
* Use this function to check for any erroneous global conditions.
* Use of this function is rather paranoid, but helpful.
*
* Always returns true, or doesn't return on error.
*/
int
check_gvars(void)
{
if (!(domain_check == false &&
high_prec == false &&
partial_flag == true &&
symb_flag == false &&
sign_cmp_flag == false &&
approximate_roots == false))
error_bug("Global vars got changed!");
if (!(zero_token.level == 1 &&
zero_token.kind == CONSTANT &&
zero_token.token.constant == 0.0 &&
one_token.level == 1 &&
one_token.kind == CONSTANT &&
one_token.token.constant == 1.0))
error_bug("Global constants got changed!");
return true;
}
#endif
/*
* Initialize some important global variables to their defaults.
* This is called on startup and by process() to reset the global flags to the default state.
* This is also called when processing is aborted with a longjmp(3).
*/
void
init_gvars(void)
{
domain_check = false;
high_prec = false;
partial_flag = true;
symb_flag = false;
sign_cmp_flag = false;
approximate_roots = false;
repeat_flag = false;
/* initialize the universal and often used constant "0" expression */
zero_token.level = 1;
zero_token.kind = CONSTANT;
zero_token.token.constant = 0.0;
/* initialize the universal and often used constant "1" expression */
one_token.level = 1;
one_token.kind = CONSTANT;
one_token.token.constant = 1.0;
}
/*
* Clean up when processing is unexpectedly interrupted or terminated.
*/
void
clean_up(void)
{
int i;
init_gvars(); /* reset the global variables to the default */
if (gfp != default_out) { /* reset the output file to default */
#if !SECURE
if (gfp != stdout && gfp != stderr)
fclose(gfp);
#endif
gfp = default_out;
}
gfp_filename = NULL;
for (i = 0; i < n_equations; i++) {
if (n_lhs[i] <= 0) {
n_lhs[i] = 0;
n_rhs[i] = 0;
}
}
}
/*
* Register all sign variables in all equation spaces
* so that the next sign variables returned by next_sign() will be unique.
*/
void
set_sign_array(void)
{
int i, j;
CLEAR_ARRAY(sign_array);
for (i = 0; i < n_equations; i++) {
if (n_lhs[i] > 0) {
for (j = 0; j < n_lhs[i]; j += 2) {
if (lhs[i][j].kind == VARIABLE && (lhs[i][j].token.variable & VAR_MASK) == SIGN) {
sign_array[(lhs[i][j].token.variable >> VAR_SHIFT) & SUBSCRIPT_MASK] = true;
}
}
for (j = 0; j < n_rhs[i]; j += 2) {
if (rhs[i][j].kind == VARIABLE && (rhs[i][j].token.variable & VAR_MASK) == SIGN) {
sign_array[(rhs[i][j].token.variable >> VAR_SHIFT) & SUBSCRIPT_MASK] = true;
}
}
}
}
}
/*
* Return next unused sign variable in "*vp".
* Mark it used.
*/
int
next_sign(vp)
long *vp;
{
int i;
for (i = 0;; i++) {
if (i >= ARR_CNT(sign_array)) {
/* out of unique sign variables */
*vp = SIGN;
return false;
}
if (!sign_array[i]) {
*vp = SIGN + (((long) i) << VAR_SHIFT);
sign_array[i] = true;
break;
}
}
return true;
}
/*
* Erase all equation spaces and initialize the global variables.
* Similar to a restart.
*/
void
clear_all(void)
{
int i;
/* select first equation space */
cur_equation = 0;
/* erase all equation spaces by setting their length to zero */
CLEAR_ARRAY(n_lhs);
CLEAR_ARRAY(n_rhs);
/* forget all variables names */
for (i = 0; var_names[i]; i++) {
free(var_names[i]);
var_names[i] = NULL;
}
/* reset everything to a known state */
CLEAR_ARRAY(sign_array);
init_gvars();
}
/*
* Return true if the specified equation space is available,
* zeroing and allocating if necessary.
*/
int
alloc_espace(i)
int i; /* equation space number */
{
if (i < 0 || i >= N_EQUATIONS)
return false;
n_lhs[i] = 0;
n_rhs[i] = 0;
if (lhs[i] && rhs[i])
return true; /* already allocated */
if (lhs[i] || rhs[i])
return false; /* something is wrong */
lhs[i] = (token_type *) malloc(n_tokens * sizeof(token_type));
if (lhs[i] == NULL)
return false;
rhs[i] = (token_type *) malloc(n_tokens * sizeof(token_type));
if (rhs[i] == NULL) {
free(lhs[i]);
lhs[i] = NULL;
return false;
}
return true;
}
/*
* Allocate all equation spaces up to and including an equation space number,
* making sure the specified equation number is valid and usable.
*
* Returns true if successful.
*/
int
alloc_to_espace(en)
int en; /* equation space number */
{
if (en < 0 || en >= N_EQUATIONS)
return false;
for (;;) {
if (en < n_equations)
return true;
if (n_equations >= N_EQUATIONS)
return false;
if (!alloc_espace(n_equations)) {
warning(_("Memory is exhausted."));
return false;
}
n_equations++;
}
}
/*
* Allocate or reuse an empty equation space.
*
* Returns empty equation space number ready for use or -1 on error.
*/
int
alloc_next_espace(void)
{
int i, n;
for (n = cur_equation, i = 0;; n = (n + 1) % N_EQUATIONS, i++) {
if (i >= N_EQUATIONS)
return -1;
if (n >= n_equations) {
n = n_equations;
if (!alloc_espace(n)) {
warning(_("Memory is exhausted."));
for (n = 0; n < n_equations; n++) {
if (n_lhs[n] == 0) {
n_rhs[n] = 0;
return n;
}
}
return -1;
}
n_equations++;
return n;
}
if (n_lhs[n] == 0)
break;
}
n_rhs[n] = 0;
return n;
}
/*
* Return the number of the next empty equation space, otherwise don't return.
*/
int
next_espace(void)
{
int i, j;
long answer_v = 0; /* Mathomatic answer variable */
i = alloc_next_espace();
if (i < 0) {
#if !SILENT
printf(_("Deleting old numeric calculations to free up equation spaces.\n"));
#endif
parse_var(&answer_v, "answer"); /* convert to a Mathomatic variable */
for (j = 0; j < n_equations; j++) {
if (n_lhs[j] == 1 && lhs[j][0].kind == VARIABLE
&& lhs[j][0].token.variable == answer_v) {
/* delete calculation from memory */
n_lhs[j] = 0;
n_rhs[j] = 0;
}
}
i = alloc_next_espace();
if (i < 0) {
error(_("Out of free equation spaces."));
#if !SILENT
printf(_("Use the clear command on unnecessary equations and try again.\n"));
#endif
longjmp(jmp_save, 3); /* do not return */
}
}
return i;
}
/*
* Copy equation space "src" to equation space "dest".
* "dest" is overwritten.
*/
void
copy_espace(src, dest)
int src, dest; /* equation space numbers */
{
if (src == dest) {
#if DEBUG
error_bug("Internal error: copy_espace() source and destination the same.");
#endif
return;
}
blt(lhs[dest], lhs[src], n_lhs[src] * sizeof(token_type));
n_lhs[dest] = n_lhs[src];
blt(rhs[dest], rhs[src], n_rhs[src] * sizeof(token_type));
n_rhs[dest] = n_rhs[src];
}
/*
* Return true if equation space "i" is a valid equation solved for a normal variable.
*/
int
solved_equation(i)
int i;
{
if (empty_equation_space(i))
return false;
if (n_rhs[i] <= 0)
return false;
if (n_lhs[i] != 1 || lhs[i][0].kind != VARIABLE || (lhs[i][0].token.variable & VAR_MASK) <= SIGN)
return false;
if (found_var(rhs[i], n_rhs[i], lhs[i][0].token.variable))
return false;
return true;
}
/*
* Return the number of times variable "v" is found in an expression.
*/
int
found_var(p1, n, v)
token_type *p1; /* expression pointer */
int n; /* expression length */
long v; /* standard Mathomatic variable */
{
int j;
int count = 0;
if (v) {
for (j = 0; j < n; j++) {
if (p1[j].kind == VARIABLE && p1[j].token.variable == v) {
count++;
}
}
}
return count;
}
/*
* Return true if variable "v" exists in equation space "i".
*/
int
var_in_equation(i, v)
int i; /* equation space number */
long v; /* standard Mathomatic variable */
{
if (empty_equation_space(i))
return false;
if (found_var(lhs[i], n_lhs[i], v))
return true;
if (n_rhs[i] <= 0)
return false;
if (found_var(rhs[i], n_rhs[i], v))
return true;
return false;
}
/*
* Return true if variable "v" exists in any equation space.
*
* Search forward starting at the next equation space if forward_direction is true,
* otherwise search backwards starting at the previous equation space.
* If found, return true with cur_equation set to the equation space the variable is found in.
*/
int
search_all_for_var(v, forward_direction)
long v;
int forward_direction;
{
int i, n;
i = cur_equation;
for (n = 0; n < n_equations; n++) {
if (forward_direction) {
if (i >= (n_equations - 1))
i = 0;
else
i++;
} else {
if (i <= 0)
i = n_equations - 1;
else
i--;
}
if (var_in_equation(i, v)) {
cur_equation = i;
return true;
}
}
return false;
}
/*
* Replace all occurrences of variable from_v with to_v in an equation space.
*/
void
rename_var_in_es(en, from_v, to_v)
int en; /* equation space number */
long from_v, to_v; /* Mathomatic variables */
{
int i;
if (empty_equation_space(en)) {
#if DEBUG
error_bug("Invalid or empty equation number given to rename_var_in_es().");
#else
return;
#endif
}
for (i = 0; i < n_lhs[en]; i += 2)
if (lhs[en][i].kind == VARIABLE
&& lhs[en][i].token.variable == from_v)
lhs[en][i].token.variable = to_v;
for (i = 0; i < n_rhs[en]; i += 2)
if (rhs[en][i].kind == VARIABLE
&& rhs[en][i].token.variable == from_v)
rhs[en][i].token.variable = to_v;
}
/*
* Substitute every instance of "v" in "equation" with "expression".
*
* Return true if something was substituted.
*/
int
subst_var_with_exp(equation, np, expression, len, v)
token_type *equation; /* equation side pointer */
int *np; /* pointer to equation side length */
token_type *expression; /* expression pointer */
int len; /* expression length */
long v; /* variable to substitute with expression */
{
int j, k;
int level;
int substituted = false;
if (v == 0 || len <= 0)
return false;
for (j = *np - 1; j >= 0; j--) {
if (equation[j].kind == VARIABLE && equation[j].token.variable == v) {
level = equation[j].level;
if (*np + len - 1 > n_tokens) {
error_huge();
}
if (len > 1) {
blt(&equation[j+len], &equation[j+1], (*np - (j + 1)) * sizeof(token_type));
*np += len - 1;
}
blt(&equation[j], expression, len * sizeof(token_type));
for (k = j; k < j + len; k++)
equation[k].level += level;
substituted = true;
}
}
if (substituted) {
if (is_integer_var(v) && !is_integer_expr(expression, len)) {
warning(_("Substituting integer variable with non-integer expression."));
}
}
return substituted;
}
/*
* Return the base (minimum) parentheses level encountered in a Mathomatic "expression".
*/
int
min_level(expression, n)
token_type *expression; /* expression pointer */
int n; /* expression length */
{
int min1;
token_type *p1, *ep;
#if DEBUG
if (expression == NULL)
error_bug("NULL pointer passed to min_level().");
#endif
switch (n) {
case 1:
return expression[0].level;
case 3:
return expression[1].level;
default:
if (n <= 0 || (n & 1) != 1)
error_bug("Invalid expression length in call to min_level().");
break;
}
min1 = expression[1].level;
ep = &expression[n];
for (p1 = &expression[3]; p1 < ep; p1 += 2) {
if (p1->level < min1)
min1 = p1->level;
}
return min1;
}
/*
* Get default equation number from a command parameter string.
* The equation number must be the only parameter.
* If no equation number is specified, default to the current equation.
*
* Return -1 on error.
*/
int
get_default_en(cp)
char *cp;
{
int i;
if (*cp == '\0') {
i = cur_equation;
} else {
i = decstrtol(cp, &cp) - 1;
if (extra_characters(cp))
return -1;
}
if (not_defined(i)) {
return -1;
}
return i;
}
/*
* Get an expression from the user.
* The prompt must be previously copied into the global prompt_str[].
*
* Return true if successful.
*/
int
get_expr(equation, np)
token_type *equation; /* where the parsed expression is stored (equation side) */
int *np; /* pointer to the returned parsed expression length */
{
char buf[DEFAULT_N_TOKENS];
char *cp;
#if LIBRARY
snprintf(buf, sizeof(buf), "#%+d", pull_number);
pull_number++;
cp = parse_expr(equation, np, buf, true);
if (extra_characters(cp))
return false;
return(cp && *np > 0);
#else
for (;;) {
if ((cp = get_string(buf, sizeof(buf))) == NULL) {
return false;
}
cp = parse_expr(equation, np, cp, true);
if (cp && !extra_characters(cp)) {
break;
}
}
return(*np > 0);
#endif
}
/*
* Prompt for a variable name from the user.
*
* Return true if successful.
*/
int
prompt_var(vp)
long *vp; /* pointer to the returned variable */
{
char buf[MAX_CMD_LEN];
char *cp;
for (;;) {
my_strlcpy(prompt_str, _("Enter variable: "), sizeof(prompt_str));
if ((cp = get_string(buf, sizeof(buf))) == NULL) {
return false;
}
if (*cp == '\0') {
return false;
}
cp = parse_var2(vp, cp);
if (cp == NULL || extra_characters(cp)) {
continue;
}
return true;
}
}
/*
* Return true and display a message if equation "i" is undefined.
*/
int
not_defined(i)
int i; /* equation space number */
{
if (i < 0 || i >= n_equations) {
error(_("Invalid equation number."));
return true;
}
if (n_lhs[i] <= 0) {
if (i == cur_equation) {
error(_("Current equation space is empty."));
} else {
error(_("Equation space is empty."));
}
return true;
}
return false;
}
/*
* Verify that a current equation or expression is loaded.
*
* Return true and display a message if the current equation space is empty or not defined.
*/
int
current_not_defined(void)
{
int i;
i = cur_equation;
if (i < 0 || i >= n_equations) {
error(_("Current equation number out of range; reset to 1."));
i = cur_equation = 0;
}
if (n_lhs[i] <= 0) {
error(_("No current equation or expression."));
return true;
}
return false;
}
/*
* Common routine to output the prompt in prompt_str[] and get a line of input from stdin.
* All Mathomatic input comes from this routine, except for file reading.
* If there is no more input (EOF), exit this program with no error.
*
* Returns "string" if successful or NULL on error.
*/
char *
get_string(string, n)
char *string; /* storage for input string */
int n; /* maximum size of "string" in bytes */
{
#if LIBRARY
error(_("Library usage error. Input requested, possibly due to missing command-line argument."));
return NULL;
#else
int i;
#if READLINE || EDITLINE
char *cp;
#endif
#if DEBUG
if (string == NULL)
error_bug("NULL pointer passed to get_string().");
#endif
if (quiet_mode) {
prompt_str[0] = '\0'; /* don't display a prompt */
}
input_column = strlen(prompt_str);
fflush(NULL); /* flush everything before gathering input */
#if READLINE || EDITLINE
if (readline_enabled) {
cp = readline(prompt_str);
if (cp == NULL) {
if (!quiet_mode)
printf(_("\nEnd of input.\n"));
exit_program(0);
}
my_strlcpy(string, cp, n);
if (skip_space(cp)[0] && (last_history_string == NULL || strcmp(last_history_string, cp))) {
add_history(cp);
last_history_string = cp;
} else {
free(cp);
}
} else {
printf("%s", prompt_str);
fflush(stdout);
if (fgets(string, n, stdin) == NULL) {
if (!quiet_mode)
printf(_("\nEnd of input.\n"));
exit_program(0);
}
}
#else
printf("%s", prompt_str);