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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
|
/*
* config - configuration routines
*
* Copyright (C) 1999-2006 David I. Bell and Landon Curt Noll
*
* Primary author: David I. Bell
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc 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.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Revision: 29.22 $
* @(#) $Id: config.c,v 29.22 2006/06/25 22:05:38 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/RCS/config.c,v $
*
* Under source code control: 1991/07/20 00:21:56
* File existed as early as: 1991
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include "calc.h"
#include "token.h"
#include "zrand.h"
#include "block.h"
#include "nametype.h"
#include "config.h"
#include "string.h"
#include "custom.h"
#include "have_strdup.h"
#if !defined(HAVE_STRDUP)
# define strdup(x) calc_strdup((CONST char *)(x))
#endif /* HAVE_STRDUP */
/*
* Table of configuration types that can be set or read.
*/
NAMETYPE configs[] = {
{"all", CONFIG_ALL},
{"mode", CONFIG_MODE},
{"mode2", CONFIG_MODE2},
{"display", CONFIG_DISPLAY},
{"epsilon", CONFIG_EPSILON},
/*epsilonprec -- tied to epsilon not a configuration type*/
{"trace", CONFIG_TRACE},
{"maxprint", CONFIG_MAXPRINT},
{"mul2", CONFIG_MUL2},
{"sq2", CONFIG_SQ2},
{"pow2", CONFIG_POW2},
{"redc2", CONFIG_REDC2},
{"tilde", CONFIG_TILDE},
{"tab", CONFIG_TAB},
{"quomod", CONFIG_QUOMOD},
{"quo", CONFIG_QUO},
{"mod", CONFIG_MOD},
{"sqrt", CONFIG_SQRT},
{"appr", CONFIG_APPR},
{"cfappr", CONFIG_CFAPPR},
{"cfsim", CONFIG_CFSIM},
{"outround", CONFIG_OUTROUND},
{"round", CONFIG_ROUND},
{"leadzero", CONFIG_LEADZERO},
{"fullzero", CONFIG_FULLZERO},
{"maxscan", CONFIG_MAXSCAN},
{"maxerr", CONFIG_MAXSCAN}, /* old name for maxscan */
{"prompt", CONFIG_PROMPT},
{"more", CONFIG_MORE},
{"blkmaxprint", CONFIG_BLKMAXPRINT},
{"blkverbose", CONFIG_BLKVERBOSE},
{"blkbase", CONFIG_BLKBASE},
{"blkfmt", CONFIG_BLKFMT},
{"resource_debug", CONFIG_RESOURCE_DEBUG},
{"lib_debug", CONFIG_RESOURCE_DEBUG},
{"calc_debug", CONFIG_CALC_DEBUG},
{"user_debug", CONFIG_USER_DEBUG},
{"verbose_quit",CONFIG_VERBOSE_QUIT},
{"ctrl_d", CONFIG_CTRL_D},
{"ctrl-d", CONFIG_CTRL_D}, /* alias for ctrl_d */
{"program", CONFIG_PROGRAM},
{"basename", CONFIG_BASENAME},
{"windows", CONFIG_WINDOWS},
{"cygwin", CONFIG_CYGWIN},
{"compile_custom", CONFIG_COMPILE_CUSTOM},
{"allow_custom", CONFIG_ALLOW_CUSTOM},
{"version", CONFIG_VERSION},
{"baseb", CONFIG_BASEB},
{"redecl_warn", CONFIG_REDECL_WARN},
{"dupvar_warn", CONFIG_DUPVAR_WARN},
{NULL, 0}
};
/*
* configurations
*/
CONFIG oldstd = { /* backward compatible standard configuration */
MODE_INITIAL, /* current output mode */
MODE2_INITIAL, /* current secondary output mode */
20, /* current output digits for float or exp */
NULL, /* loaded in at startup - default error for real functions */
EPSILONPREC_DEFAULT, /* binary precision of epsilon */
FALSE, /* tracing flags */
MAXPRINT_DEFAULT, /* number of elements to print */
MUL_ALG2, /* size of number to use multiply alg 2 */
SQ_ALG2, /* size of number to use square alg 2 */
POW_ALG2, /* size of modulus to use REDC for powers */
REDC_ALG2, /* size of modulus to use REDC algorithm 2 */
TRUE, /* ok to print a tilde on approximations */
TRUE, /* ok to print tab before numeric values */
0, /* quomod() default rounding mode */
2, /* quotient // default rounding mode */
0, /* mod % default rounding mode */
24, /* sqrt() default rounding mode */
24, /* appr() default rounding mode */
0, /* cfappr() default rounding mode */
8, /* cfsim() default rounding mode */
2, /* output default rounding mode */
24, /* round()/bround() default rounding mode */
FALSE, /* ok to print leading 0 before decimal pt */
0, /* ok to print trailing 0's */
MAXSCANCOUNT, /* max scan errors before abort */
PROMPT1, /* normal prompt */
PROMPT2, /* prompt when inside multi-line input */
BLK_DEF_MAXPRINT, /* number of octets of a block to print */
FALSE, /* skip duplicate block output lines */
BLK_BASE_HEX, /* block octet print base */
BLK_FMT_HD_STYLE, /* block output format */
0, /* internal calc debug level */
3, /* calc resource file debug level */
0, /* user defined debug level */
FALSE, /* print Quit or abort executed messages */
CTRL_D_VIRGIN_EOF, /* ^D only exits on virgin lines */
NULL, /* our name */
NULL, /* basename of our name */
#if defined(_WIN32)
TRUE, /* running under windows */
#else
FALSE, /* congrats, you are not using windows */
#endif
#if defined(__CYGWIN__)
TRUE, /* compiled under cygwin */
#else
FALSE, /* not compiled with cygwin */
#endif
#if defined(CUSTOM)
TRUE, /* compiled with -DCUSTOM */
#else
FALSE, /* compiled without -DCUSTOM */
#endif
&allow_custom, /* *TRUE=> custom functions are enabled */
NULL, /* version */
BASEB, /* base for calculations */
TRUE, /* warn when redeclaring */
TRUE, /* warn when variable names collide */
};
CONFIG newstd = { /* new non-backward compatible configuration */
MODE_INITIAL, /* current output mode */
MODE2_INITIAL, /* current output mode */
20, /* current output digits for float or exp */
NULL, /* loaded in at startup - default error for real functions */
EPSILONPREC_DEFAULT, /* binary precision of epsilon */
FALSE, /* tracing flags */
MAXPRINT_DEFAULT, /* number of elements to print */
MUL_ALG2, /* size of number to use multiply alg 2 */
SQ_ALG2, /* size of number to use square alg 2 */
POW_ALG2, /* size of modulus to use REDC for powers */
REDC_ALG2, /* size of modulus to use REDC algorithm 2 */
TRUE, /* ok to print a tilde on approximations */
TRUE, /* ok to print tab before numeric values */
0, /* quomod() default rounding mode */
2, /* quotient // default rounding mode */
0, /* mod % default rounding mode */
24, /* sqrt() default rounding mode */
24, /* appr() default rounding mode */
0, /* cfappr() default rounding mode */
8, /* cfsim() default rounding mode */
24, /* output default rounding mode */
24, /* round()/bround() default rounding mode */
TRUE, /* ok to print leading 0 before decimal pt */
0, /* ok to print trailing 0's */
MAXSCANCOUNT, /* max scan errors before abort */
"; ", /* normal prompt */
";; ", /* prompt when inside multi-line input */
BLK_DEF_MAXPRINT, /* number of octets of a block to print */
FALSE, /* skip duplicate block output lines */
BLK_BASE_HEX, /* block octet print base */
BLK_FMT_HD_STYLE, /* block output format */
0, /* internal calc debug level */
3, /* calc resource file debug level */
0, /* user defined debug level */
FALSE, /* print Quit or abort executed messages */
CTRL_D_VIRGIN_EOF, /* ^D only exits on virgin lines */
NULL, /* our name */
NULL, /* basename of our name */
#if defined(_WIN32)
TRUE, /* running under windows */
#else
FALSE, /* congrats, you are not using windows */
#endif
#if defined(__CYGWIN__)
TRUE, /* compiled under cygwin */
#else
FALSE, /* not compiled with cygwin */
#endif
#if defined(CUSTOM)
TRUE, /* compiled with -DCUSTOM */
#else
FALSE, /* compiled without -DCUSTOM */
#endif
&allow_custom, /* *TRUE=> custom functions are enabled */
NULL, /* version */
BASEB, /* base for calculations */
TRUE, /* warn when redeclaring */
TRUE, /* warn when variable names collide */
};
CONFIG *conf = NULL; /* loaded in at startup - current configuration */
/*
* Possible output modes.
*/
static NAMETYPE modes[] = {
{"fraction", MODE_FRAC},
{"frac", MODE_FRAC},
{"integer", MODE_INT},
{"int", MODE_INT},
{"real", MODE_REAL},
{"float", MODE_REAL},
{"default", MODE_INITIAL}, /* MODE_REAL */
{"scientific", MODE_EXP},
{"sci", MODE_EXP},
{"exp", MODE_EXP},
{"hexadecimal", MODE_HEX},
{"hex", MODE_HEX},
{"octal", MODE_OCTAL},
{"oct", MODE_OCTAL},
{"binary", MODE_BINARY},
{"bin", MODE_BINARY},
{"off", MODE2_OFF},
{NULL, 0}
};
/*
* Possible block base output modes
*/
static NAMETYPE blk_base[] = {
{"hexadecimal", BLK_BASE_HEX},
{"hex", BLK_BASE_HEX},
{"default", BLK_BASE_HEX},
{"octal", BLK_BASE_OCT},
{"oct", BLK_BASE_OCT},
{"character", BLK_BASE_CHAR},
{"char", BLK_BASE_CHAR},
{"binary", BLK_BASE_BINARY},
{"bin", BLK_BASE_BINARY},
{"raw", BLK_BASE_RAW},
{"none", BLK_BASE_RAW},
{NULL, 0}
};
/*
* Possible block output formats
*/
static NAMETYPE blk_fmt[] = {
{"lines", BLK_FMT_LINE},
{"line", BLK_FMT_LINE},
{"strings", BLK_FMT_STRING},
{"string", BLK_FMT_STRING},
{"str", BLK_FMT_STRING},
{"od_style", BLK_FMT_OD_STYLE},
{"odstyle", BLK_FMT_OD_STYLE},
{"od", BLK_FMT_OD_STYLE},
{"hd_style", BLK_FMT_HD_STYLE},
{"hdstyle", BLK_FMT_HD_STYLE},
{"hd", BLK_FMT_HD_STYLE},
{"default", BLK_FMT_HD_STYLE},
{NULL, 0}
};
/*
* Possible ctrl_d styles
*/
static NAMETYPE ctrl_d[] = {
{"virgin_eof", CTRL_D_VIRGIN_EOF},
{"virgineof", CTRL_D_VIRGIN_EOF},
{"virgin", CTRL_D_VIRGIN_EOF},
{"default", CTRL_D_VIRGIN_EOF},
{"never_eof", CTRL_D_NEVER_EOF},
{"nevereof", CTRL_D_NEVER_EOF},
{"never", CTRL_D_NEVER_EOF},
{"empty_eof", CTRL_D_EMPTY_EOF},
{"emptyeof", CTRL_D_EMPTY_EOF},
{"empty", CTRL_D_EMPTY_EOF},
{NULL, 0}
};
/*
* Possible binary config state values
*/
#define TRUE_STRING "true"
#define FALSE_STRING "false"
static NAMETYPE truth[] = {
{TRUE_STRING, TRUE},
{"t", TRUE},
{"on", TRUE},
{"yes", TRUE},
{"y", TRUE},
{"set", TRUE},
{"1", TRUE},
{FALSE_STRING, FALSE},
{"f", FALSE},
{"off", FALSE},
{"no", FALSE},
{"n", FALSE},
{"unset", FALSE},
{"0", FALSE},
{NULL, 0}
};
/*
* declare static functions
*/
static long lookup_long(NAMETYPE *set, char *name);
static char *lookup_name(NAMETYPE *set, long val);
static int getlen(VALUE *vp, LEN *lp);
/*
* Given a string value which represents a configuration name, return
* the configuration type for that string. Returns negative type if
* the string is unknown.
*
* given:
* name configuration name
*/
int
configtype(char *name)
{
NAMETYPE *cp; /* current config pointer */
for (cp = configs; cp->name; cp++) {
if (strcmp(cp->name, name) == 0)
return cp->type;
}
return -1;
}
/*
* lookup_long - given a name and a NAMETYPE, return the int
*
* given:
* set the NAMESET array of name/int pairs
* name mode name
*
* returns:
* numeric value of the name or -1 if not found
*/
static long
lookup_long(NAMETYPE *set, char *name)
{
NAMETYPE *cp; /* current config pointer */
for (cp = set; cp->name; cp++) {
if (strcmp(cp->name, name) == 0)
return cp->type;
}
return -1;
}
/*
* lookup_name - given numeric value and a NAMETYPE, return the name
*
* given:
* set the NAMESET array of name/int pairs
* val numeric value to lookup
*
* returns:
* name of the value found of NULL
*/
static char *
lookup_name(NAMETYPE *set, long val)
{
NAMETYPE *cp; /* current config pointer */
for (cp = set; cp->name; cp++) {
if (val == cp->type)
return cp->name;
}
return NULL;
}
/*
* Check whether VALUE at vp is a LEN (32-bit signed integer) and if so,
* copy that integer to lp.
*
* Return: 1 ==> not an integer, 2 ==> int > 2^31, 0 ==> OK, -1 ==> error
*/
static int
getlen(VALUE *vp, LEN *lp)
{
if (vp->v_type != V_NUM || !qisint(vp->v_num))
return 1;
if (zge31b(vp->v_num->num))
return 2;
*lp = ztoi(vp->v_num->num);
if (*lp < 0)
return -1;
return 0;
}
/*
* Set the specified configuration type to the specified value.
* An error is generated if the type number or value is illegal.
*/
void
setconfig(int type, VALUE *vp)
{
NUMBER *q;
CONFIG *newconf; /* new configuration to set */
long temp;
LEN len = 0;
char *p;
switch (type) {
case CONFIG_ALL:
newconf = NULL; /* firewall */
if (vp->v_type == V_STR) {
if (strcmp(vp->v_str->s_str, "oldstd") == 0) {
newconf = &oldstd;
} else if (strcmp(vp->v_str->s_str, "newstd") == 0) {
newconf = &newstd;
} else {
math_error("CONFIG alias not oldstd or newstd");
/*NOTREACHED*/
}
} else if (vp->v_type != V_CONFIG) {
math_error("non-CONFIG for all");
/*NOTREACHED*/
} else {
newconf = vp->v_config;
}
/* free the current configuration */
config_free(conf);
/* set the new configuration */
conf = config_copy(newconf);
break;
case CONFIG_TRACE:
if (vp->v_type != V_NUM) {
math_error("Non-numeric for trace");
/*NOTREACHED*/
}
q = vp->v_num;
temp = qtoi(q);
if (qisfrac(q) || !zistiny(q->num) ||
((unsigned long) temp > TRACE_MAX)) {
math_error("Bad trace value");
/*NOTREACHED*/
}
conf->traceflags = (FLAG)temp;
break;
case CONFIG_DISPLAY:
if (getlen(vp, &len)) {
math_error("Bad value for display");
/*NOTREACHED*/
}
math_setdigits(len);
break;
case CONFIG_MODE:
if (vp->v_type != V_STR) {
math_error("Non-string for mode");
/*NOTREACHED*/
}
temp = lookup_long(modes, vp->v_str->s_str);
if (temp < 0) {
math_error("Unknown mode \"%s\"", vp->v_str);
/*NOTREACHED*/
}
math_setmode((int) temp);
break;
case CONFIG_MODE2:
if (vp->v_type != V_STR) {
math_error("Non-string for mode");
/*NOTREACHED*/
}
temp = lookup_long(modes, vp->v_str->s_str);
if (temp < 0) {
math_error("Unknown mode \"%s\"", vp->v_str);
/*NOTREACHED*/
}
math_setmode2((int) temp);
break;
case CONFIG_EPSILON:
if (vp->v_type != V_NUM) {
math_error("Non-numeric for epsilon");
/*NOTREACHED*/
}
setepsilon(vp->v_num);
break;
case CONFIG_MAXPRINT:
if (getlen(vp, &len)) {
math_error("Bad value for maxprint");
/*NOTREACHED*/
}
conf->maxprint = len;
break;
case CONFIG_MUL2:
if (getlen(vp, &len) || len < 0 || len == 1) {
math_error("Bad value for mul2");
/*NOTREACHED*/
}
if (len == 0)
len = MUL_ALG2;
conf->mul2 = len;
break;
case CONFIG_SQ2:
if (getlen(vp, &len) || len < 0 || len == 1) {
math_error("Bad value for sq2");
/*NOTREACHED*/
}
if (len == 0)
len = SQ_ALG2;
conf->sq2 = len;
break;
case CONFIG_POW2:
if (getlen(vp, &len) || len < 0 || len == 1) {
math_error("Bad value for pow2");
/*NOTREACHED*/
}
if (len == 0)
len = POW_ALG2;
conf->pow2 = len;
break;
case CONFIG_REDC2:
if (getlen(vp, &len) || len < 0 || len == 1) {
math_error("Bad value for redc2");
/*NOTREACHED*/
}
if (len == 0)
len = REDC_ALG2;
conf->redc2 = len;
break;
case CONFIG_TILDE:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->tilde_ok = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value for tilde");
/*NOTREACHED*/
}
conf->tilde_ok = (int)temp;
}
break;
case CONFIG_TAB:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->tab_ok = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value for tab");
/*NOTREACHED*/
}
conf->tab_ok = (int)temp;
}
break;
case CONFIG_QUOMOD:
if (getlen(vp, &len)) {
math_error("Illegal value for quomod");
/*NOTREACHED*/
}
conf->quomod = len;
break;
case CONFIG_QUO:
if (getlen(vp, &len)) {
math_error("Illegal value for quo");
/*NOTREACHED*/
}
conf->quo = len;
break;
case CONFIG_MOD:
if (getlen(vp, &len)) {
math_error("Illegal value for mod");
/*NOTREACHED*/
}
conf->mod = len;
break;
case CONFIG_SQRT:
if (getlen(vp, &len)) {
math_error("Illegal value for sqrt");
/*NOTREACHED*/
}
conf->sqrt = len;
break;
case CONFIG_APPR:
if (getlen(vp, &len)) {
math_error("Illegal value for appr");
/*NOTREACHED*/
}
conf->appr = len;
break;
case CONFIG_CFAPPR:
if (getlen(vp, &len)) {
math_error("Illegal value for cfappr");
/*NOTREACHED*/
}
conf->cfappr = len;
break;
case CONFIG_CFSIM:
if (getlen(vp, &len)) {
math_error("Illegal value for cfsim");
/*NOTREACHED*/
}
conf->cfsim = len;
break;
case CONFIG_OUTROUND:
if (getlen(vp, &len)) {
math_error("Illegal value for outround");
/*NOTREACHED*/
}
conf->outround = len;
break;
case CONFIG_ROUND:
if (getlen(vp, &len)) {
math_error("Illegal value for round");
/*NOTREACHED*/
}
conf->round = len;
break;
case CONFIG_LEADZERO:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->leadzero = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) { {
math_error("Illegal truth value for leadzero");
/*NOTREACHED*/
}
}
conf->leadzero = (int)temp;
}
break;
case CONFIG_FULLZERO:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->fullzero = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) { {
math_error("Illegal truth value for fullzero");
/*NOTREACHED*/
}
}
conf->fullzero = (int)temp;
}
break;
case CONFIG_MAXSCAN:
if (vp->v_type != V_NUM) {
math_error("Non-numeric for maxscancount");
/*NOTREACHED*/
}
q = vp->v_num;
temp = qtoi(q);
if (qisfrac(q) || qisneg(q) || !zistiny(q->num))
temp = -1;
if (temp < 0) {
math_error("Maxscan value is out of range");
/*NOTREACHED*/
}
conf->maxscancount = temp;
break;
case CONFIG_PROMPT:
if (vp->v_type != V_STR) {
math_error("Non-string for prompt");
/*NOTREACHED*/
}
p = (char *)malloc(vp->v_str->s_len + 1);
if (p == NULL) {
math_error("Cannot duplicate new prompt");
/*NOTREACHED*/
}
strncpy(p, vp->v_str->s_str, vp->v_str->s_len + 1);
free(conf->prompt1);
conf->prompt1 = p;
break;
case CONFIG_MORE:
if (vp->v_type != V_STR) {
math_error("Non-string for more prompt");
/*NOTREACHED*/
}
p = (char *)malloc(vp->v_str->s_len + 1);
if (p == NULL) {
math_error("Cannot duplicate new more prompt");
/*NOTREACHED*/
}
strncpy(p, vp->v_str->s_str, vp->v_str->s_len + 1);
free(conf->prompt2);
conf->prompt2 = p;
break;
case CONFIG_BLKMAXPRINT:
if (vp->v_type != V_NUM) {
math_error("Non-numeric for blkmaxprint");
/*NOTREACHED*/
}
q = vp->v_num;
temp = qtoi(q);
if (qisfrac(q) || qisneg(q) || !zistiny(q->num))
temp = -1;
if (temp < 0) {
math_error("Blkmaxprint value is out of range");
/*NOTREACHED*/
}
conf->blkmaxprint = temp;
break;
case CONFIG_BLKVERBOSE:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->blkverbose = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value for blkverbose");
/*NOTREACHED*/
}
conf->blkverbose = (int)temp;
}
break;
case CONFIG_BLKBASE:
if (vp->v_type != V_STR) {
math_error("Non-string for blkbase");
/*NOTREACHED*/
}
temp = lookup_long(blk_base, vp->v_str->s_str);
if (temp < 0) {
math_error("Unknown mode \"%s\" for blkbase",
vp->v_str->s_str);
/*NOTREACHED*/
}
conf->blkbase = temp;
break;
case CONFIG_BLKFMT:
if (vp->v_type != V_STR) {
math_error("Non-string for blkfmt");
/*NOTREACHED*/
}
temp = lookup_long(blk_fmt, vp->v_str->s_str);
if (temp < 0) {
math_error("Unknown mode \"%s\" for blkfmt",
vp->v_str->s_str);
/*NOTREACHED*/
}
conf->blkfmt = temp;
break;
case CONFIG_CALC_DEBUG:
if (vp->v_type != V_NUM) {
math_error("Non numeric for calc_debug");
/*NOTREACHED*/
}
q = vp->v_num;
temp = qtoi(q);
if (qisfrac(q) || !zistiny(q->num)) {
math_error("Illegal calc_debug parameter value");
/*NOTREACHED*/
}
conf->calc_debug = temp;
break;
case CONFIG_RESOURCE_DEBUG:
if (vp->v_type != V_NUM) {
math_error("Non numeric for resource_debug");
/*NOTREACHED*/
}
q = vp->v_num;
temp = qtoi(q);
if (qisfrac(q) || !zistiny(q->num)) {
math_error("Illegal resource_debug parameter value");
/*NOTREACHED*/
}
conf->resource_debug = temp;
break;
case CONFIG_USER_DEBUG:
if (vp->v_type != V_NUM) {
math_error("Non numeric for user_debug");
/*NOTREACHED*/
}
q = vp->v_num;
temp = qtoi(q);
if (qisfrac(q) || !zistiny(q->num)) {
math_error("Illegal user_debug parameter value");
/*NOTREACHED*/
}
conf->user_debug = temp;
break;
case CONFIG_VERBOSE_QUIT:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->verbose_quit = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value "
"for verbose_quit");
/*NOTREACHED*/
}
conf->verbose_quit = (int)temp;
}
break;
case CONFIG_CTRL_D:
if (vp->v_type != V_STR) {
math_error("Non-string for ctrl_d");
/*NOTREACHED*/
}
temp = lookup_long(ctrl_d, vp->v_str->s_str);
if (temp < 0) {
math_error("Unknown mode \"%s\" for ctrl_d",
vp->v_str->s_str);
/*NOTREACHED*/
}
conf->ctrl_d = temp;
break;
case CONFIG_PROGRAM:
math_error("The program config parameter is read-only");
/*NOTREACHED*/
case CONFIG_BASENAME:
math_error("The basename config parameter is read-only");
/*NOTREACHED*/
case CONFIG_WINDOWS:
math_error("The windows config parameter is read-only");
/*NOTREACHED*/
case CONFIG_CYGWIN:
math_error("The cygwin config parameter is read-only");
/*NOTREACHED*/
case CONFIG_COMPILE_CUSTOM:
math_error("The custom config parameter is read-only");
/*NOTREACHED*/
case CONFIG_ALLOW_CUSTOM:
math_error("The allow_custom config parameter is read-only");
/*NOTREACHED*/
case CONFIG_VERSION:
math_error("The version config parameter is read-only");
/*NOTREACHED*/
case CONFIG_BASEB:
math_error("The baseb config parameter is read-only");
/*NOTREACHED*/
case CONFIG_REDECL_WARN:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->redecl_warn = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value for redecl_warn");
/*NOTREACHED*/
}
conf->redecl_warn = (int)temp;
}
break;
case CONFIG_DUPVAR_WARN:
if (vp->v_type == V_NUM) {
q = vp->v_num;
conf->dupvar_warn = !qiszero(q);
} else if (vp->v_type == V_STR) {
temp = lookup_long(truth, vp->v_str->s_str);
if (temp < 0) {
math_error("Illegal truth value for dupvar_warn");
/*NOTREACHED*/
}
conf->dupvar_warn = (int)temp;
}
break;
default:
math_error("Setting illegal config parameter");
/*NOTREACHED*/
}
}
/*
* config_copy - copy the configuration from one value to another
*
* given:
* src copy this configuration
*
* returns:
* pointer to the configuration copy
*/
CONFIG *
config_copy(CONFIG *src)
{
CONFIG *dest; /* the new CONFIG to return */
/*
* firewall
*/
if (src == NULL || src->epsilon == NULL || src->prompt1 == NULL ||
src->prompt2 == NULL) {
math_error("bad CONFIG value");
/*NOTREACHED*/
}
/*
* malloc the storage
*/
dest = (CONFIG *)malloc(sizeof(CONFIG));
if (dest == NULL) {
math_error("malloc of CONFIG failed");
/*NOTREACHED*/
}
/*
* copy over the values
*/
memcpy((void *)dest, (void *)src, sizeof(CONFIG));
/*
* clone the pointer values
*/
dest->epsilon = qlink(src->epsilon);
dest->prompt1 = strdup(src->prompt1);
dest->prompt2 = strdup(src->prompt2);
if (src->program == NULL) {
dest->program = strdup(program);
} else {
dest->program = strdup(src->program);
}
if (src->base_name == NULL) {
dest->base_name = strdup(base_name);
} else {
dest->base_name = strdup(src->base_name);
}
/* NOTE: allow_custom points to a global variable, so do not clone it */
if (src->version == NULL) {
dest->version = strdup(version());
} else {
dest->version = strdup(src->version);
}
/*
* return the new value
*/
return dest;
}
/*
* config_free - free a CONFIG value
*
* given:
* cfg the CONFIG value to free
*/
void
config_free(CONFIG *cfg)
{
/*
* firewall
*/
if (cfg == NULL) {
return;
}
/*
* free pointer values
*/
if (cfg->epsilon != NULL) {
qfree(cfg->epsilon);
}
if (cfg->prompt1 != NULL) {
free(cfg->prompt1);
}
if (cfg->prompt2 != NULL) {
free(cfg->prompt2);
}
if (cfg->program != NULL) {
free(cfg->program);
}
if (cfg->base_name != NULL) {
free(cfg->base_name);
}
/* NOTE: allow_custom points to a global variable, so do not free it */
if (cfg->version != NULL) {
free(cfg->version);
}
/*
* free the CONFIG value itself
*/
free(cfg);
return;
}
/*
* config_value - return a CONFIG element as a value
*
* given:
* cfg CONFIG from which an element will be returned
* type the type of CONFIG element to print
* ret where to return the value
*
* returns:
* ret points to the VALUE returned
*/
void
config_value(CONFIG *cfg, int type, VALUE *vp)
{
long i=0;
char *p;
/*
* firewall
*/
if (cfg == NULL || cfg->epsilon == NULL || cfg->prompt1 == NULL ||
cfg->prompt2 == NULL) {
math_error("bad CONFIG value");
/*NOTREACHED*/
}
/*
* convert element to value
*/
vp->v_type = V_NUM;
vp->v_subtype = V_NOSUBTYPE;
switch (type) {
case CONFIG_ALL:
vp->v_type = V_CONFIG;
vp->v_config = config_copy(conf);
return;
case CONFIG_TRACE:
i = cfg->traceflags;
break;
case CONFIG_DISPLAY:
i = cfg->outdigits;
break;
case CONFIG_MODE:
vp->v_type = V_STR;
p = lookup_name(modes, cfg->outmode);
if (p == NULL) {
math_error("invalid output mode: %d", cfg->outmode);
/*NOTREACHED*/
}
vp->v_str = makenewstring(p);
return;
case CONFIG_MODE2:
vp->v_type = V_STR;
p = lookup_name(modes, cfg->outmode2);
if (p == NULL) {
math_error("invalid secondary output mode: %d", cfg->outmode2);
/*NOTREACHED*/
}
vp->v_str = makenewstring(p);
return;
case CONFIG_EPSILON:
vp->v_num = qlink(cfg->epsilon);
return;
case CONFIG_MAXPRINT:
i = cfg->maxprint;
break;
case CONFIG_MUL2:
i = cfg->mul2;
break;
case CONFIG_SQ2:
i = cfg->sq2;
break;
case CONFIG_POW2:
i = cfg->pow2;
break;
case CONFIG_REDC2:
i = cfg->redc2;
break;
case CONFIG_TILDE:
i = (cfg->tilde_ok ? 1 : 0);
break;
case CONFIG_TAB:
i = (cfg->tab_ok ? 1 : 0);
break;
case CONFIG_QUOMOD:
i = cfg->quomod;
break;
case CONFIG_QUO:
i = cfg->quo;
break;
case CONFIG_MOD:
i = cfg->mod;
break;
case CONFIG_SQRT:
i = cfg->sqrt;
break;
case CONFIG_APPR:
i = cfg->appr;
break;
case CONFIG_CFAPPR:
i = cfg->cfappr;
break;
case CONFIG_CFSIM:
i = cfg->cfsim;
break;
case CONFIG_OUTROUND:
i = cfg->outround;
break;
case CONFIG_ROUND:
i = cfg->round;
break;
case CONFIG_LEADZERO:
i = (cfg->leadzero ? 1 : 0);
break;
case CONFIG_FULLZERO:
i = (cfg->fullzero ? 1 : 0);
break;
case CONFIG_MAXSCAN:
i = cfg->maxscancount;
break;
case CONFIG_PROMPT:
vp->v_type = V_STR;
vp->v_str = makenewstring(cfg->prompt1);
return;
case CONFIG_MORE:
vp->v_type = V_STR;
vp->v_str = makenewstring(cfg->prompt2);
return;
case CONFIG_BLKMAXPRINT:
i = cfg->blkmaxprint;
break;
case CONFIG_BLKVERBOSE:
i = (cfg->blkverbose ? 1 : 0);
break;
case CONFIG_BLKBASE:
vp->v_type = V_STR;
p = lookup_name(blk_base, cfg->blkbase);
if (p == NULL) {
math_error("invalid block base: %d", cfg->blkbase);
/*NOTREACHED*/
}
vp->v_str = makenewstring(p);
return;
case CONFIG_BLKFMT:
vp->v_type = V_STR;
p = lookup_name(blk_fmt, cfg->blkfmt);
if (p == NULL) {
math_error("invalid block format: %d", cfg->blkfmt);
/*NOTREACHED*/
}
vp->v_str = makenewstring(p);
return;
case CONFIG_CALC_DEBUG:
i = cfg->calc_debug;
break;
case CONFIG_RESOURCE_DEBUG:
i = cfg->resource_debug;
break;
case CONFIG_USER_DEBUG:
i = cfg->user_debug;
break;
case CONFIG_VERBOSE_QUIT:
i = (cfg->verbose_quit ? 1 : 0);
break;
case CONFIG_CTRL_D:
vp->v_type = V_STR;
p = lookup_name(ctrl_d, cfg->ctrl_d);
if (p == NULL) {
math_error("invalid Control-D: %d", cfg->ctrl_d);
/*NOTREACHED*/
}
vp->v_str = makenewstring(p);
return;
case CONFIG_PROGRAM:
vp->v_type = V_STR;
if (cfg->base_name == NULL) {
vp->v_str = makestring(strdup(program));
} else {
vp->v_str = makenewstring(cfg->program);
}
return;
case CONFIG_BASENAME:
vp->v_type = V_STR;
if (cfg->base_name == NULL) {
vp->v_str = makestring(strdup(base_name));
} else {
vp->v_str = makenewstring(cfg->base_name);
}
return;
case CONFIG_WINDOWS:
i = (cfg->windows ? 1 : 0);
break;
case CONFIG_CYGWIN:
i = (cfg->cygwin ? 1 : 0);
break;
case CONFIG_COMPILE_CUSTOM:
i = (cfg->compile_custom ? 1 : 0);
break;
case CONFIG_ALLOW_CUSTOM:
/* firewall */
if (cfg->allow_custom == NULL) {
cfg->allow_custom = &allow_custom;
}
i = (*(cfg->allow_custom) ? 1 : 0);
break;
case CONFIG_VERSION:
vp->v_type = V_STR;
if (cfg->version == NULL) {
vp->v_str = makestring(strdup(version()));
} else {
vp->v_str = makenewstring(cfg->version);
}
return;
case CONFIG_BASEB:
i = BASEB;
break;
case CONFIG_REDECL_WARN:
i = (cfg->redecl_warn ? 1 : 0);
break;
case CONFIG_DUPVAR_WARN:
i = (cfg->dupvar_warn ? 1 : 0);
break;
default:
math_error("Getting illegal CONFIG element");
/*NOTREACHED*/
}
/*
* if we got this far, we have a V_NUM in i
*/
vp->v_num = itoq(i);
return;
}
/*
* config_cmp - compare two CONFIG states
*
* given:
* cfg1 - 1st CONFIG to compare
* cfg2 - 2nd CONFIG to compare
*
* return:
* TRUE if configurations differ
*/
BOOL
config_cmp(CONFIG *cfg1, CONFIG *cfg2)
{
/*
* firewall
*/
if (cfg1 == NULL || cfg1->epsilon == NULL || cfg1->prompt1 == NULL ||
cfg1->prompt2 == NULL) {
math_error("CONFIG #1 value is invalid");
/*NOTREACHED*/
}
if (cfg2 == NULL || cfg2->epsilon == NULL || cfg2->prompt1 == NULL ||
cfg2->prompt2 == NULL) {
math_error("CONFIG #2 value is invalid");
/*NOTREACHED*/
}
/*
* compare
*/
return cfg1->traceflags != cfg2->traceflags ||
cfg1->outdigits != cfg2->outdigits ||
cfg1->outmode != cfg2->outmode ||
cfg1->outmode2 != cfg2->outmode2 ||
qcmp(cfg1->epsilon, cfg2->epsilon) ||
cfg1->epsilonprec != cfg2->epsilonprec ||
cfg1->maxprint != cfg2->maxprint ||
cfg1->mul2 != cfg2->mul2 ||
cfg1->sq2 != cfg2->sq2 ||
cfg1->pow2 != cfg2->pow2 ||
cfg1->redc2 != cfg2->redc2 ||
cfg1->tilde_ok != cfg2->tilde_ok ||
cfg1->tab_ok != cfg2->tab_ok ||
cfg1->quomod != cfg2->quomod ||
cfg1->quo != cfg2->quo ||
cfg1->mod != cfg2->mod ||
cfg1->sqrt != cfg2->sqrt ||
cfg1->appr != cfg2->appr ||
cfg1->cfappr != cfg2->cfappr ||
cfg1->cfsim != cfg2->cfsim ||
cfg1->outround != cfg2->outround ||
cfg1->round != cfg2->round ||
cfg1->leadzero != cfg2->leadzero ||
cfg1->fullzero != cfg2->fullzero ||
cfg1->maxscancount != cfg2->maxscancount ||
strcmp(cfg1->prompt1, cfg2->prompt1) != 0 ||
strcmp(cfg1->prompt2, cfg2->prompt2) != 0 ||
cfg1->blkmaxprint != cfg2->blkmaxprint ||
cfg1->blkverbose != cfg2->blkverbose ||
cfg1->blkbase != cfg2->blkbase ||
cfg1->blkfmt != cfg2->blkfmt ||
cfg1->calc_debug != cfg2->calc_debug ||
cfg1->resource_debug != cfg2->resource_debug ||
cfg1->user_debug != cfg2->user_debug ||
cfg1->verbose_quit != cfg2->verbose_quit ||
cfg1->ctrl_d != cfg2->ctrl_d ||
(cfg1->program == NULL && cfg2->program != NULL) ||
(cfg1->program != NULL && cfg2->program == NULL) ||
(cfg1->program != NULL && cfg2->program != NULL &&
strcmp(cfg1->program, cfg2->program) != 0) ||
(cfg1->base_name == NULL && cfg2->base_name != NULL) ||
(cfg1->base_name != NULL && cfg2->base_name == NULL) ||
(cfg1->base_name != NULL && cfg2->base_name != NULL &&
strcmp(cfg1->base_name, cfg2->base_name) != 0) ||
cfg1->windows != cfg2->windows ||
cfg1->cygwin != cfg2->cygwin ||
cfg1->compile_custom != cfg2->compile_custom ||
(cfg1->allow_custom == NULL && cfg2->allow_custom != NULL) ||
(cfg1->allow_custom != NULL && cfg2->allow_custom == NULL) ||
(cfg1->allow_custom != NULL && cfg2->allow_custom != NULL &&
*(cfg1->allow_custom) != *(cfg2->allow_custom)) ||
(cfg1->version == NULL && cfg2->version != NULL) ||
(cfg1->version != NULL && cfg2->version == NULL) ||
(cfg1->version != NULL && cfg2->version != NULL &&
strcmp(cfg1->version, cfg2->version) != 0) ||
cfg1->baseb != cfg2->baseb;
}
|