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
|
/*
* Miscellaneous routines for Mathomatic.
*
* Copyright (C) 1987-2006 George Gesslein II.
*/
#include "includes.h"
/*
* Check if a floating point math function flagged an error.
*/
check_err()
{
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(_("Overflow error in constant."));
longjmp(jmp_save, 2);
break;
}
}
/*
* Erase all equation spaces.
* Similar to a restart.
*/
clear_all()
{
int i;
cur_equation = 0;
for (i = 0; i < n_equations; i++) {
n_lhs[i] = 0;
n_rhs[i] = 0;
}
for (i = 0; var_names[i]; i++) {
free(var_names[i]);
var_names[i] = NULL;
}
clear_sign_array();
}
/*
* Allocate the needed temp storage.
*
* Return true if successful.
*/
int
init_mem()
{
if ((scratch = (token_type *) malloc(((n_tokens * 3) / 2) * 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;
}
return true;
}
/*
* Initialize the global variables.
*/
init_gvars()
{
domain_check = false;
high_prec = false;
partial_flag = true;
symb_flag = false;
sign_flag = false;
approximate_roots = false;
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;
}
/*
* Clean up when processing is unexpectedly
* interrupted or terminated.
*/
clean_up()
{
init_gvars();
if (gfp != stdout) {
#if !SECURE
fclose(gfp);
#endif
gfp = stdout;
}
}
/*
* Restart sign array.
*/
clear_sign_array()
{
int i;
for (i = 0; i < ARR_CNT(sign_array); i++) {
sign_array[i] = false;
}
}
/*
* Register all sign variables.
*/
set_sign_array()
{
int i, j;
clear_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 = 2;; 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;
}
/*
* Allocate or reuse an equation space.
*
* Returns new equation space number or -1 on error.
*/
int
alloc_next_espace()
{
int i, n;
for (n = cur_equation, i = 1;; n = (n + 1) % n_equations, i++) {
if (i > n_equations) {
i = n_equations;
if (i < N_EQUATIONS) {
lhs[i] = (token_type *) malloc(n_tokens * sizeof(token_type));
if (lhs[i]) {
rhs[i] = (token_type *) malloc(n_tokens * sizeof(token_type));
if (rhs[i] == NULL) {
free(lhs[i]);
lhs[i] = NULL;
}
}
}
if (i >= N_EQUATIONS || lhs[i] == NULL) {
return -1;
}
n_equations++;
return i;
}
if (n_lhs[n] == 0)
break;
}
n_rhs[n] = 0;
return n;
}
/*
* Return the number of the next empty equation space.
*/
int
next_espace()
{
int i;
i = alloc_next_espace();
if (i < 0) {
error(_("Out of free equation spaces."));
#if !SILENT
printf(_("Please use the clear command on unnecessary equations and try again.\n"));
#endif
longjmp(jmp_save, 3);
}
return i;
}
/*
* Return true if equation number "i" is solved for a normal variable.
*/
int
solved_equation(i)
int i;
{
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 true if variable "v" exists in expression.
*/
int
found_var(equation, n, v)
token_type *equation;
int n;
long v;
{
int j;
for (j = 0; j < n; j += 2) {
if (equation[j].kind == VARIABLE && equation[j].token.variable == v) {
return true;
}
}
return false;
}
/*
* Return true if variable "v" exists in equation number "i".
*/
int
var_in_equation(i, v)
int i;
long v;
{
if (n_lhs[i] <= 0)
return false;
if (found_var(lhs[i], n_lhs[i], v))
return true;
if (found_var(rhs[i], n_rhs[i], v))
return true;
return false;
}
/*
* Substitute every instance of "v" in "equation" with "expression".
*/
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;
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();
}
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;
}
}
}
/*
* Return the minimum level encountered in a sub-expression.
*/
int
min_level(equation, n)
token_type *equation; /* expression pointer */
int n; /* expression length */
{
int min1;
token_type *p1, *ep;
if (n <= 1) {
if (n <= 0) {
return 1;
} else {
return equation[0].level;
}
}
min1 = equation[1].level;
ep = &equation[n];
for (p1 = &equation[3]; p1 < ep; p1 += 2) {
if (p1->level < min1)
min1 = p1->level;
}
return min1;
}
/*
* This is called when the max expression size has been exceeded.
*
* There is no return.
*/
error_huge()
{
longjmp(jmp_save, 14);
}
/*
* Get default equation number from a command parameter string.
* The equation number must be the only parameter.
*
* 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_garbage(cp))
return -1;
}
if (not_defined(i)) {
return -1;
}
return i;
}
/*
* Parse an expression with equation space pull if the line starts with "#".
*
* Returns the new string position or NULL on error.
*/
char *
parse_expr(equation, np, cp)
token_type *equation; /* where the parsed expression is stored (equation side) */
int *np; /* pointer to the returned parsed expression length */
char *cp; /* string to parse */
{
int i;
char *cp1, *cp2;
cp1 = skip_space(cp);
if (*cp1 == '#') {
cp1++;
switch (*cp1) {
case '+':
case '-':
i = decstrtol(cp1, &cp2);
i = cur_equation + i;
break;
default:
i = decstrtol(cp1, &cp2) - 1;
break;
}
if (cp1 == cp2 || *cp2) {
error(_("Error parsing equation space number."));
return NULL;
}
if (i < 0 || i >= n_equations || n_lhs[i] <= 0) {
error(_("Equation not defined."));
return NULL;
}
if (n_rhs[i]) {
blt(equation, rhs[i], n_rhs[i] * sizeof(token_type));
*np = n_rhs[i];
} else {
blt(equation, lhs[i], n_lhs[i] * sizeof(token_type));
*np = n_lhs[i];
}
return cp2;
}
return parse_section(equation, np, cp);
}
/*
* 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;
for (;;) {
if ((cp = get_string(buf, sizeof(buf))) == NULL) {
return false;
}
if (!case_sensitive_flag) {
str_tolower(cp);
}
cp = parse_expr(equation, np, cp);
if (cp && !extra_garbage(cp)) {
break;
}
}
return(*np > 0);
}
/*
* Prompt for a variable 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_garbage(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) {
error(_("Equation space empty."));
return true;
}
return false;
}
/*
* Return true and display a message if current equation is undefined.
*/
int
current_not_defined()
{
int i;
i = cur_equation;
if (i < 0 || i >= n_equations || 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.
*
* 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(_("Missing command line argument."));
return NULL;
#else
#if READLINE
char *cp;
#endif
if (quiet_mode) {
prompt_str[0] = '\0'; /* don't display a prompt */
}
input_column = strlen(prompt_str);
#if READLINE
if (readline_enabled) {
cp = readline(prompt_str);
if (cp == NULL)
exit_program(0);
my_strlcpy(string, cp, n);
if (string[0]) {
add_history(cp);
} else {
free(cp);
}
} else {
printf("%s", prompt_str);
if (fgets(string, n, stdin) == NULL)
exit_program(0);
}
#else
printf("%s", prompt_str);
if (fgets(string, n, stdin) == NULL)
exit_program(0);
#endif
set_error_level(string);
return string;
#endif
}
/*
* Display the prompt in prompt_str[] and wait for "y" or "n".
*
* Return true if "y".
*/
int
get_yes_no()
{
char *cp;
char buf[MAX_CMD_LEN];
for (;;) {
if ((cp = get_string(buf, sizeof(buf))) == NULL) {
return false;
}
str_tolower(cp);
switch (*cp) {
case 'n':
return false;
case 'y':
return true;
}
}
}
/*
* Store or display the result of a command.
*
* Return true if successful.
*/
int
return_result(en)
int en; /* equation space number */
{
#if LIBRARY
if (gfp == stdout) {
if (display2d) {
group_sub(en);
}
if (factor_int_flag) {
factor_int_sub(en);
}
result_str = list_equation(en, false);
return(result_str != NULL);
}
#endif
return list_sub(en);
}
/*
* Return true if the first word in the passed string is "all".
*/
int
is_all(cp)
char *cp;
{
return(strcmp_tospace(cp, "all") == 0);
}
/*
* Process an equation number range given in text string *cpp.
* Skip past all spaces and update *cpp to point to the next argument.
*
* Return true if successful,
* with the starting equation number in "*ip"
* and ending equation number in "*jp".
*/
int
get_range(cpp, ip, jp)
char **cpp;
int *ip, *jp;
{
int i;
*cpp = skip_space(*cpp);
#if LIBRARY /* only allow a single equation number when using as a library */
if (isdigit(**cpp)) {
i = decstrtol(*cpp, cpp) - 1;
} else {
i = cur_equation;
}
if (not_defined(i)) {
return false;
}
*ip = i;
*jp = i;
return true;
#else
if (is_all(*cpp)) {
*cpp = skip_param(*cpp);
*ip = 0;
*jp = n_equations - 1;
} else {
if (isdigit(**cpp)) {
*ip = strtol(*cpp, cpp, 10) - 1;
} else {
*ip = cur_equation;
}
if (*ip < 0 || *ip >= n_equations) {
error(_("Invalid equation number."));
return false;
}
if (**cpp != '-') {
if (not_defined(*ip)) {
return false;
}
*jp = *ip;
*cpp = skip_space(*cpp);
return true;
}
(*cpp)++;
if (isdigit(**cpp)) {
*jp = strtol(*cpp, cpp, 10) - 1;
} else {
*jp = cur_equation;
}
if (*jp < 0 || *jp >= n_equations) {
error(_("Invalid equation number."));
return false;
}
if (*jp < *ip) {
i = *ip;
*ip = *jp;
*jp = i;
}
}
*cpp = skip_space(*cpp);
for (i = *ip; i <= *jp; i++) {
if (n_lhs[i] > 0) {
return true;
}
}
error(_("No equations defined in specified range."));
return false;
#endif
}
/*
* This function is provided to make sure there is nothing else
* on a command line.
*
* Returns false if OK.
* Returns true if non-space characters were encountered
* and an error message was printed.
*/
int
extra_garbage(cp)
char *cp; /* command line */
{
if (cp) {
cp = skip_space(cp);
if (*cp) {
error(_("Extra characters encountered."));
return true;
}
}
return false;
}
/*
* get_range() if it is the last possible option on the command line.
*/
int
get_range_eol(cpp, ip, jp)
char **cpp;
int *ip, *jp;
{
if (!get_range(cpp, ip, jp)) {
return false;
}
if (extra_garbage(*cpp)) {
return false;
}
return true;
}
/*
* Skip over space characters.
*/
char *
skip_space(cp)
char *cp; /* character pointer */
{
while (*cp && isspace(*cp))
cp++;
return cp;
}
/*
* Enhanced decimal strtol().
* Skips trailing spaces.
*/
long
decstrtol(cp, cpp)
char *cp, **cpp;
{
long l;
l = strtol(cp, cpp, 10);
if (cp != *cpp) {
*cpp = skip_space(*cpp);
}
return l;
}
/*
* Skip over a parameter (command line argument) in a character string.
* Parameters are separated with spaces.
*
* Returns a string pointer to the next parameter.
*/
char *
skip_param(cp)
char *cp; /* character pointer */
{
while (*cp && (!isascii(*cp) || (!isspace(*cp) && *cp != '='))) {
cp++;
}
cp = skip_space(cp);
if (*cp == '=') {
cp = skip_space(cp + 1);
}
return(cp);
}
/*
* Compare strings up to the first space.
* Must be an exact match.
* Compare is alphabetic case insensitive.
*
* Returns zero on match, non-zero if different.
*/
int
strcmp_tospace(cp1, cp2)
char *cp1, *cp2;
{
char *cp1a, *cp2a;
for (cp1a = cp1; *cp1a && !isspace(*cp1a); cp1a++)
;
for (cp2a = cp2; *cp2a && !isspace(*cp2a); cp2a++)
;
return strncasecmp(cp1, cp2, max(cp1a - cp1, cp2a - cp2));
}
/*
* Return the number of "level" additive type operators.
*/
int
level_plus_count(p1, n1, level)
token_type *p1; /* expression pointer */
int n1; /* expression length */
int level; /* parentheses level number */
{
int i;
int count;
count = 0;
for (i = 1; i < n1; i += 2) {
if (p1[i].level == level) {
switch (p1[i].token.operatr) {
case PLUS:
case MINUS:
count++;
}
}
}
return count;
}
/*
* Return the number of level 1 additive type operators.
*/
int
level1_plus_count(p1, n1)
token_type *p1; /* expression pointer */
int n1; /* expression length */
{
return level_plus_count(p1, n1, min_level(p1, n1));
}
/*
* Return the count of variables in an expression.
*/
int
var_count(p1, n1)
token_type *p1; /* expression pointer */
int n1; /* expression length */
{
int i;
int count;
count = 0;
for (i = 0; i < n1; i += 2) {
if (p1[i].kind == VARIABLE) {
count++;
}
}
return count;
}
/*
* Set *vp if single variable expression.
*
* Return true if expression contains no variables.
*/
int
no_vars(source, n, vp)
token_type *source; /* expression pointer */
int n; /* expression length */
long *vp; /* variable pointer */
{
int j;
int found = false;
if (*vp) {
return(var_count(source, n) == 0);
}
for (j = 0; j < n; j += 2) {
if (source[j].kind == VARIABLE) {
if ((source[j].token.variable & VAR_MASK) <= SIGN)
continue;
if (*vp) {
if (*vp != source[j].token.variable) {
*vp = 0;
break;
}
} else {
found = true;
*vp = source[j].token.variable;
}
}
}
return(!found);
}
/*
* Return true if expression contains infinity or nan.
*/
int
exp_contains_infinity(p1, n1)
token_type *p1; /* expression pointer */
int n1; /* expression length */
{
int i;
for (i = 0; i < n1; i += 2) {
if (p1[i].kind == CONSTANT && !isfinite(p1[i].token.constant)) {
return true;
}
}
return false;
}
|