1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * 14 * * Redistributions in binary form must reproduce the above copyright notice, 15 * this list of conditions and the following disclaimer in the documentation 16 * and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 * 30 * ***************************************************************************** 31 * 32 * Definitions for bc's VM. 33 * 34 */ 35 36 #ifndef BC_VM_H 37 #define BC_VM_H 38 39 #include <assert.h> 40 #include <stddef.h> 41 #include <limits.h> 42 43 #include <signal.h> 44 45 #if BC_ENABLE_NLS 46 47 #ifdef _WIN32 48 #error NLS is not supported on Windows. 49 #endif // _WIN32 50 51 #include <nl_types.h> 52 53 #endif // BC_ENABLE_NLS 54 55 #include <version.h> 56 #include <status.h> 57 #include <num.h> 58 #include <lex.h> 59 #include <parse.h> 60 #include <program.h> 61 #include <history.h> 62 #include <bc.h> 63 64 // We don't want to include this file for the library because it's unused. 65 #if !BC_ENABLE_LIBRARY 66 #include <file.h> 67 #endif // !BC_ENABLE_LIBRARY 68 69 // This should be obvious. If neither calculator is enabled, barf. 70 #if !BC_ENABLED && !DC_ENABLED 71 #error Must define BC_ENABLED, DC_ENABLED, or both 72 #endif 73 74 // CHAR_BIT must be at least 6, for various reasons. I might want to bump this 75 // to 8 in the future. 76 #if CHAR_BIT < 6 77 #error CHAR_BIT must be at least 6. 78 #endif 79 80 // Set defaults. 81 82 #ifndef MAINEXEC 83 #define MAINEXEC bc 84 #endif // MAINEXEC 85 86 #ifndef _WIN32 87 #ifndef EXECPREFIX 88 #define EXECPREFIX 89 #endif // EXECPREFIX 90 #else // _WIN32 91 #undef EXECPREFIX 92 #endif // _WIN32 93 94 /** 95 * Generate a string from text. 96 * @parm V The text to generate a string for. 97 */ 98 #define GEN_STR(V) #V 99 100 /** 101 * Help generate a string from text. The preprocessor requires this two-step 102 * process. Trust me. 103 * @parm V The text to generate a string for. 104 */ 105 #define GEN_STR2(V) GEN_STR(V) 106 107 /// The version as a string. VERSION must be defined previously, usually by the 108 /// build system. 109 #define BC_VERSION GEN_STR2(VERSION) 110 111 /// The main executable name as a string. MAINEXEC must be defined previously, 112 /// usually by the build system. 113 #define BC_MAINEXEC GEN_STR2(MAINEXEC) 114 115 /// The build type as a string. BUILD_TYPE must be defined previously, usually 116 /// by the build system. 117 #define BC_BUILD_TYPE GEN_STR2(BUILD_TYPE) 118 119 // We only allow an empty executable prefix on Windows. 120 #ifndef _WIN32 121 #define BC_EXECPREFIX GEN_STR2(EXECPREFIX) 122 #else // _WIN32 123 #define BC_EXECPREFIX "" 124 #endif // _WIN32 125 126 #if !BC_ENABLE_LIBRARY 127 128 #if DC_ENABLED 129 130 /// The flag for the extended register option. 131 #define DC_FLAG_X (UINTMAX_C(1) << 0) 132 133 #endif // DC_ENABLED 134 135 #if BC_ENABLED 136 137 /// The flag for the POSIX warning option. 138 #define BC_FLAG_W (UINTMAX_C(1) << 1) 139 140 /// The flag for the POSIX error option. 141 #define BC_FLAG_S (UINTMAX_C(1) << 2) 142 143 /// The flag for the math library option. 144 #define BC_FLAG_L (UINTMAX_C(1) << 3) 145 146 /// The flag for the global stacks option. 147 #define BC_FLAG_G (UINTMAX_C(1) << 4) 148 149 #endif // BC_ENABLED 150 151 /// The flag for quiet, though this one is reversed; the option clears the flag. 152 #define BC_FLAG_Q (UINTMAX_C(1) << 5) 153 154 /// The flag for interactive. 155 #define BC_FLAG_I (UINTMAX_C(1) << 6) 156 157 /// The flag for prompt. This is also reversed; the option clears the flag. 158 #define BC_FLAG_P (UINTMAX_C(1) << 7) 159 160 /// The flag for read prompt. This is also reversed; the option clears the flag. 161 #define BC_FLAG_R (UINTMAX_C(1) << 8) 162 163 /// The flag for a leading zero. 164 #define BC_FLAG_Z (UINTMAX_C(1) << 9) 165 166 /// The flag for stdin being a TTY. 167 #define BC_FLAG_TTYIN (UINTMAX_C(1) << 10) 168 169 /// The flag for TTY mode. 170 #define BC_FLAG_TTY (UINTMAX_C(1) << 11) 171 172 /// The flag for reset on SIGINT. 173 #define BC_FLAG_SIGINT (UINTMAX_C(1) << 12) 174 175 /// The flag for exiting with expressions. 176 #define BC_FLAG_EXPR_EXIT (UINTMAX_C(1) << 13) 177 178 /// The flag for digit clamping. 179 #define BC_FLAG_DIGIT_CLAMP (UINTMAX_C(1) << 14) 180 181 /// A convenience macro for getting the TTYIN flag. 182 #define BC_TTYIN (vm->flags & BC_FLAG_TTYIN) 183 184 /// A convenience macro for getting the TTY flag. 185 #define BC_TTY (vm->flags & BC_FLAG_TTY) 186 187 /// A convenience macro for getting the SIGINT flag. 188 #define BC_SIGINT (vm->flags & BC_FLAG_SIGINT) 189 190 #if BC_ENABLED 191 192 /// A convenience macro for getting the POSIX error flag. 193 #define BC_S (vm->flags & BC_FLAG_S) 194 195 /// A convenience macro for getting the POSIX warning flag. 196 #define BC_W (vm->flags & BC_FLAG_W) 197 198 /// A convenience macro for getting the math library flag. 199 #define BC_L (vm->flags & BC_FLAG_L) 200 201 /// A convenience macro for getting the global stacks flag. 202 #define BC_G (vm->flags & BC_FLAG_G) 203 204 #endif // BC_ENABLED 205 206 #if DC_ENABLED 207 208 /// A convenience macro for getting the extended register flag. 209 #define DC_X (vm->flags & DC_FLAG_X) 210 211 #endif // DC_ENABLED 212 213 /// A convenience macro for getting the interactive flag. 214 #define BC_I (vm->flags & BC_FLAG_I) 215 216 /// A convenience macro for getting the prompt flag. 217 #define BC_P (vm->flags & BC_FLAG_P) 218 219 /// A convenience macro for getting the read prompt flag. 220 #define BC_R (vm->flags & BC_FLAG_R) 221 222 /// A convenience macro for getting the leading zero flag. 223 #define BC_Z (vm->flags & BC_FLAG_Z) 224 225 /// A convenience macro for getting the expression exit flag. 226 #define BC_EXPR_EXIT (vm->flags & BC_FLAG_EXPR_EXIT) 227 228 /// A convenience macro for getting the digit clamp flag. 229 #define BC_DIGIT_CLAMP (vm->flags & BC_FLAG_DIGIT_CLAMP) 230 231 #if BC_ENABLED 232 233 /// A convenience macro for checking if bc is in POSIX mode. 234 #define BC_IS_POSIX (BC_S || BC_W) 235 236 #if DC_ENABLED 237 238 /// Returns true if bc is running. 239 #define BC_IS_BC (vm->name[0] != 'd') 240 241 /// Returns true if dc is running. 242 #define BC_IS_DC (vm->name[0] == 'd') 243 244 /// Returns the correct read prompt. 245 #define BC_VM_READ_PROMPT (BC_IS_BC ? "read> " : "?> ") 246 247 /// Returns the string for the line length environment variable. 248 #define BC_VM_LINE_LENGTH_STR (BC_IS_BC ? "BC_LINE_LENGTH" : "DC_LINE_LENGTH") 249 250 /// Returns the string for the environment args environment variable. 251 #define BC_VM_ENV_ARGS_STR (BC_IS_BC ? "BC_ENV_ARGS" : "DC_ENV_ARGS") 252 253 /// Returns the string for the expression exit environment variable. 254 #define BC_VM_EXPR_EXIT_STR (BC_IS_BC ? "BC_EXPR_EXIT" : "DC_EXPR_EXIT") 255 256 /// Returns the default for the expression exit environment variable. 257 #define BC_VM_EXPR_EXIT_DEF \ 258 (BC_IS_BC ? BC_DEFAULT_EXPR_EXIT : DC_DEFAULT_EXPR_EXIT) 259 260 /// Returns the string for the digit clamp environment variable. 261 #define BC_VM_DIGIT_CLAMP_STR (BC_IS_BC ? "BC_DIGIT_CLAMP" : "DC_DIGIT_CLAMP") 262 263 /// Returns the default for the digit clamp environment variable. 264 #define BC_VM_DIGIT_CLAMP_DEF \ 265 (BC_IS_BC ? BC_DEFAULT_DIGIT_CLAMP : DC_DEFAULT_DIGIT_CLAMP) 266 267 /// Returns the string for the TTY mode environment variable. 268 #define BC_VM_TTY_MODE_STR (BC_IS_BC ? "BC_TTY_MODE" : "DC_TTY_MODE") 269 270 /// Returns the default for the TTY mode environment variable. 271 #define BC_VM_TTY_MODE_DEF \ 272 (BC_IS_BC ? BC_DEFAULT_TTY_MODE : DC_DEFAULT_TTY_MODE) 273 274 /// Returns the string for the prompt environment variable. 275 #define BC_VM_PROMPT_STR (BC_IS_BC ? "BC_PROMPT" : "DC_PROMPT") 276 277 /// Returns the default for the prompt environment variable. 278 #define BC_VM_PROMPT_DEF (BC_IS_BC ? BC_DEFAULT_PROMPT : DC_DEFAULT_PROMPT) 279 280 /// Returns the string for the SIGINT reset environment variable. 281 #define BC_VM_SIGINT_RESET_STR \ 282 (BC_IS_BC ? "BC_SIGINT_RESET" : "DC_SIGINT_RESET") 283 284 /// Returns the string for the SIGINT reset environment variable. 285 #define BC_VM_SIGINT_RESET_DEF \ 286 (BC_IS_BC ? BC_DEFAULT_SIGINT_RESET : DC_DEFAULT_SIGINT_RESET) 287 288 /// Returns true if the calculator should run stdin. 289 #define BC_VM_RUN_STDIN(has_file) (BC_IS_BC || !(has_file)) 290 291 #else // DC_ENABLED 292 293 /// Returns true if bc is running. 294 #define BC_IS_BC (1) 295 296 /// Returns true if dc is running. 297 #define BC_IS_DC (0) 298 299 /// Returns the correct read prompt. 300 #define BC_VM_READ_PROMPT ("read> ") 301 302 /// Returns the string for the line length environment variable. 303 #define BC_VM_LINE_LENGTH_STR ("BC_LINE_LENGTH") 304 305 /// Returns the string for the environment args environment variable. 306 #define BC_VM_ENV_ARGS_STR ("BC_ENV_ARGS") 307 308 /// Returns the string for the expression exit environment variable. 309 #define BC_VM_EXPR_EXIT_STR ("BC_EXPR_EXIT") 310 311 /// Returns the default for the expression exit environment variable. 312 #define BC_VM_EXPR_EXIT_DEF (BC_DEFAULT_EXPR_EXIT) 313 314 /// Returns the string for the digit clamp environment variable. 315 #define BC_VM_DIGIT_CLAMP_STR ("BC_DIGIT_CLAMP") 316 317 /// Returns the default for the digit clamp environment variable. 318 #define BC_VM_DIGIT_CLAMP_DEF (BC_DEFAULT_DIGIT_CLAMP) 319 320 /// Returns the string for the TTY mode environment variable. 321 #define BC_VM_TTY_MODE_STR ("BC_TTY_MODE") 322 323 /// Returns the default for the TTY mode environment variable. 324 #define BC_VM_TTY_MODE_DEF (BC_DEFAULT_TTY_MODE) 325 326 /// Returns the string for the prompt environment variable. 327 #define BC_VM_PROMPT_STR ("BC_PROMPT") 328 329 /// Returns the default for the SIGINT reset environment variable. 330 #define BC_VM_PROMPT_DEF (BC_DEFAULT_PROMPT) 331 332 /// Returns the string for the SIGINT reset environment variable. 333 #define BC_VM_SIGINT_RESET_STR ("BC_SIGINT_RESET") 334 335 /// Returns the string for the SIGINT reset environment variable. 336 #define BC_VM_SIGINT_RESET_DEF (BC_DEFAULT_SIGINT_RESET) 337 338 /// Returns true if the calculator should run stdin. 339 #define BC_VM_RUN_STDIN(has_file) (BC_IS_BC) 340 341 #endif // DC_ENABLED 342 343 #else // BC_ENABLED 344 345 /// A convenience macro for checking if bc is in POSIX mode. 346 #define BC_IS_POSIX (0) 347 348 /// Returns true if bc is running. 349 #define BC_IS_BC (0) 350 351 /// Returns true if dc is running. 352 #define BC_IS_DC (1) 353 354 /// Returns the correct read prompt. 355 #define BC_VM_READ_PROMPT ("?> ") 356 357 /// Returns the string for the line length environment variable. 358 #define BC_VM_LINE_LENGTH_STR ("DC_LINE_LENGTH") 359 360 /// Returns the string for the environment args environment variable. 361 #define BC_VM_ENV_ARGS_STR ("DC_ENV_ARGS") 362 363 /// Returns the string for the expression exit environment variable. 364 #define BC_VM_EXPR_EXIT_STR ("DC_EXPR_EXIT") 365 366 /// Returns the default for the expression exit environment variable. 367 #define BC_VM_EXPR_EXIT_DEF (DC_DEFAULT_EXPR_EXIT) 368 369 /// Returns the string for the digit clamp environment variable. 370 #define BC_VM_DIGIT_CLAMP_STR ("DC_DIGIT_CLAMP") 371 372 /// Returns the default for the digit clamp environment variable. 373 #define BC_VM_DIGIT_CLAMP_DEF (DC_DEFAULT_DIGIT_CLAMP) 374 375 /// Returns the string for the TTY mode environment variable. 376 #define BC_VM_TTY_MODE_STR ("DC_TTY_MODE") 377 378 /// Returns the default for the TTY mode environment variable. 379 #define BC_VM_TTY_MODE_DEF (DC_DEFAULT_TTY_MODE) 380 381 /// Returns the string for the prompt environment variable. 382 #define BC_VM_PROMPT_STR ("DC_PROMPT") 383 384 /// Returns the default for the SIGINT reset environment variable. 385 #define BC_VM_PROMPT_DEF (DC_DEFAULT_PROMPT) 386 387 /// Returns the string for the SIGINT reset environment variable. 388 #define BC_VM_SIGINT_RESET_STR ("DC_SIGINT_RESET") 389 390 /// Returns the string for the SIGINT reset environment variable. 391 #define BC_VM_SIGINT_RESET_DEF (DC_DEFAULT_SIGINT_RESET) 392 393 /// Returns true if the calculator should run stdin. 394 #define BC_VM_RUN_STDIN(has_file) (!(has_file)) 395 396 #endif // BC_ENABLED 397 398 /// A convenience macro for checking if the prompt is enabled. 399 #define BC_PROMPT (BC_P) 400 401 #else // !BC_ENABLE_LIBRARY 402 403 #define BC_Z (vm->leading_zeroes) 404 405 #define BC_DIGIT_CLAMP (vm->digit_clamp) 406 407 #endif // !BC_ENABLE_LIBRARY 408 409 /** 410 * Returns the max of its two arguments. This evaluates arguments twice, so be 411 * careful what args you give it. 412 * @param a The first argument. 413 * @param b The second argument. 414 * @return The max of the two arguments. 415 */ 416 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) 417 418 /** 419 * Returns the min of its two arguments. This evaluates arguments twice, so be 420 * careful what args you give it. 421 * @param a The first argument. 422 * @param b The second argument. 423 * @return The min of the two arguments. 424 */ 425 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) 426 427 /// Returns the max obase that is allowed. 428 #define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW)) 429 430 /// Returns the max array size that is allowed. 431 #define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1)) 432 433 /// Returns the max scale that is allowed. 434 #define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1)) 435 436 /// Returns the max string length that is allowed. 437 #define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1)) 438 439 /// Returns the max identifier length that is allowed. 440 #define BC_MAX_NAME BC_MAX_STRING 441 442 /// Returns the max number size that is allowed. 443 #define BC_MAX_NUM BC_MAX_SCALE 444 445 #if BC_ENABLE_EXTRA_MATH 446 447 /// Returns the max random integer that can be returned. 448 #define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1)) 449 450 #endif // BC_ENABLE_EXTRA_MATH 451 452 /// Returns the max exponent that is allowed. 453 #define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX)) 454 455 /// Returns the max number of variables that is allowed. 456 #define BC_MAX_VARS ((ulong) (SIZE_MAX - 1)) 457 458 #if BC_ENABLE_LINE_LIB 459 460 /// The size of the global buffer. 461 #define BC_VM_BUF_SIZE (1 << 10) 462 463 /// The amount of the global buffer allocated to stdin. 464 #define BC_VM_STDIN_BUF_SIZE (BC_VM_BUF_SIZE - 1) 465 466 #else // BC_ENABLE_LINE_LIB 467 468 /// The size of the global buffer. 469 #define BC_VM_BUF_SIZE (1 << 12) 470 471 /// The amount of the global buffer allocated to stdout. 472 #define BC_VM_STDOUT_BUF_SIZE (1 << 11) 473 474 /// The amount of the global buffer allocated to stderr. 475 #define BC_VM_STDERR_BUF_SIZE (1 << 10) 476 477 /// The amount of the global buffer allocated to stdin. 478 #define BC_VM_STDIN_BUF_SIZE (BC_VM_STDERR_BUF_SIZE - 1) 479 480 #endif // BC_ENABLE_LINE_LIB 481 482 /// The max number of temporary BcNums that can be kept. 483 #define BC_VM_MAX_TEMPS (1 << 9) 484 485 /// The capacity of the one BcNum, which is a constant. 486 #define BC_VM_ONE_CAP (1) 487 488 /** 489 * Returns true if a BcResult is safe for garbage collection. 490 * @param r The BcResult to test. 491 * @return True if @a r is safe to garbage collect. 492 */ 493 #define BC_VM_SAFE_RESULT(r) ((r)->t >= BC_RESULT_TEMP) 494 495 /// The invalid locale catalog return value. 496 #define BC_VM_INVALID_CATALOG ((nl_catd) -1) 497 498 /** 499 * Returns true if the *unsigned* multiplication overflows. 500 * @param a The first operand. 501 * @param b The second operand. 502 * @param r The product. 503 * @return True if the multiplication of @a a and @a b overflows. 504 */ 505 #define BC_VM_MUL_OVERFLOW(a, b, r) \ 506 ((r) >= SIZE_MAX || ((a) != 0 && (r) / (a) != (b))) 507 508 /// The global vm struct. This holds all of the global data besides the file 509 /// buffers. 510 typedef struct BcVm 511 { 512 /// The current status. This is volatile sig_atomic_t because it is also 513 /// used in the signal handler. See the development manual 514 /// (manuals/development.md#async-signal-safe-signal-handling) for more 515 /// information. 516 volatile sig_atomic_t status; 517 518 /// Non-zero if a jump series is in progress and items should be popped off 519 /// the jmp_bufs vector. This is volatile sig_atomic_t because it is also 520 /// used in the signal handler. See the development manual 521 /// (manuals/development.md#async-signal-safe-signal-handling) for more 522 /// information. 523 volatile sig_atomic_t sig_pop; 524 525 #if !BC_ENABLE_LIBRARY 526 527 /// The parser. 528 BcParse prs; 529 530 /// The program. 531 BcProgram prog; 532 533 /// A buffer for lines for stdin. 534 BcVec line_buf; 535 536 /// A buffer to hold a series of lines from stdin. Sometimes, multiple lines 537 /// are necessary for parsing, such as a comment that spans multiple lines. 538 BcVec buffer; 539 540 /// A parser to parse read expressions. 541 BcParse read_prs; 542 543 /// A buffer for read expressions. 544 BcVec read_buf; 545 546 #endif // !BC_ENABLE_LIBRARY 547 548 /// A vector of jmp_bufs for doing a jump series. This allows exception-type 549 /// error handling, while allowing me to do cleanup on the way. 550 BcVec jmp_bufs; 551 552 /// The number of temps in the temps array. 553 size_t temps_len; 554 555 #if BC_ENABLE_LIBRARY 556 557 /// The vector of contexts for the library. 558 BcVec ctxts; 559 560 /// The vector for creating strings to pass to the client. 561 BcVec out; 562 563 /// The PRNG. 564 BcRNG rng; 565 566 /// The current error. 567 BclError err; 568 569 /// Whether or not bcl should abort on fatal errors. 570 bool abrt; 571 572 /// Whether or not to print leading zeros. 573 bool leading_zeroes; 574 575 /// Whether or not to clamp digits that are greater than or equal to the 576 /// current ibase. 577 bool digit_clamp; 578 579 /// The number of "references," or times that the library was initialized. 580 unsigned int refs; 581 582 #else // BC_ENABLE_LIBRARY 583 584 /// A pointer to the filename of the current file. This is not owned by the 585 /// BcVm struct. 586 const char* file; 587 588 /// The message printed when SIGINT happens. 589 const char* sigmsg; 590 591 /// Non-zero when signals are "locked." This is volatile sig_atomic_t 592 /// because it is also used in the signal handler. See the development 593 /// manual (manuals/development.md#async-signal-safe-signal-handling) for 594 /// more information. 595 volatile sig_atomic_t sig_lock; 596 597 /// Non-zero when a signal has been received, but not acted on. This is 598 /// volatile sig_atomic_t because it is also used in the signal handler. See 599 /// the development manual 600 /// (manuals/development.md#async-signal-safe-signal-handling) for more 601 /// information. 602 volatile sig_atomic_t sig; 603 604 /// The length of sigmsg. 605 uchar siglen; 606 607 /// The instruction used for returning from a read() call. 608 uchar read_ret; 609 610 /// The flags field used by most macros above. 611 uint16_t flags; 612 613 /// The number of characters printed in the current line. This is used 614 /// because bc has a limit of the number of characters it can print per 615 /// line. 616 uint16_t nchars; 617 618 /// The length of the line we can print. The user can set this if they wish. 619 uint16_t line_len; 620 621 /// True if bc should error if expressions are encountered during option 622 /// parsing, false otherwise. 623 bool no_exprs; 624 625 /// True if bc should exit if expresions are encountered. 626 bool exit_exprs; 627 628 /// True if EOF was encountered. 629 bool eof; 630 631 /// The mode that the program is in. 632 uchar mode; 633 634 #if BC_ENABLED 635 636 /// True if keywords should not be redefined. This is only true for the 637 /// builtin math libraries for bc. 638 bool no_redefine; 639 640 #endif // BC_ENABLED 641 642 /// A vector of filenames to process. 643 BcVec files; 644 645 /// A vector of expressions to process. 646 BcVec exprs; 647 648 /// The name of the calculator under use. This is used by BC_IS_BC and 649 /// BC_IS_DC. 650 const char* name; 651 652 /// The help text for the calculator. 653 const char* help; 654 655 #if BC_ENABLE_HISTORY 656 657 /// The history data. 658 BcHistory history; 659 660 #endif // BC_ENABLE_HISTORY 661 662 /// The function to call to get the next lex token. 663 BcLexNext next; 664 665 /// The function to call to parse. 666 BcParseParse parse; 667 668 /// The function to call to parse expressions. 669 BcParseExpr expr; 670 671 /// The names of the categories of errors. 672 const char* err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED]; 673 674 /// The messages for each error. 675 const char* err_msgs[BC_ERR_NELEMS]; 676 677 #if BC_ENABLE_NLS 678 /// The locale. 679 const char* locale; 680 #endif // BC_ENABLE_NLS 681 682 #endif // BC_ENABLE_LIBRARY 683 684 /// An array of maxes for the globals. 685 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH]; 686 687 /// The last base used to parse. 688 BcBigDig last_base; 689 690 /// The last power of last_base used to parse. 691 BcBigDig last_pow; 692 693 /// The last exponent of base that equals last_pow. 694 BcBigDig last_exp; 695 696 /// BC_BASE_POW - last_pow. 697 BcBigDig last_rem; 698 699 #if !BC_ENABLE_LIBRARY 700 701 /// A buffer of environment arguments. This is the actual value of the 702 /// environment variable. 703 char* env_args_buffer; 704 705 /// A vector for environment arguments after parsing. 706 BcVec env_args; 707 708 /// A BcNum set to constant 0. 709 BcNum zero; 710 711 #endif // !BC_ENABLE_LIBRARY 712 713 /// A BcNum set to constant 1. 714 BcNum one; 715 716 /// A BcNum holding the max number held by a BcBigDig plus 1. 717 BcNum max; 718 719 /// A BcNum holding the max number held by a BcBigDig times 2 plus 1. 720 BcNum max2; 721 722 /// The BcDig array for max. 723 BcDig max_num[BC_NUM_BIGDIG_LOG10]; 724 725 /// The BcDig array for max2. 726 BcDig max2_num[BC_NUM_BIGDIG_LOG10]; 727 728 // The BcDig array for the one BcNum. 729 BcDig one_num[BC_VM_ONE_CAP]; 730 731 #if !BC_ENABLE_LIBRARY 732 733 // The BcDig array for the zero BcNum. 734 BcDig zero_num[BC_VM_ONE_CAP]; 735 736 /// The stdout file. 737 BcFile fout; 738 739 /// The stderr file. 740 BcFile ferr; 741 742 #if BC_ENABLE_NLS 743 744 /// The locale catalog. 745 nl_catd catalog; 746 747 #endif // BC_ENABLE_NLS 748 749 /// A pointer to the stdin buffer. 750 char* buf; 751 752 /// The number of items in the input buffer. 753 size_t buf_len; 754 755 /// The slabs vector for constants, strings, function names, and other 756 /// string-like things. 757 BcVec slabs; 758 759 #if BC_ENABLED 760 761 /// An array of booleans for which bc keywords have been redefined if 762 /// BC_REDEFINE_KEYWORDS is non-zero. 763 bool redefined_kws[BC_LEX_NKWS]; 764 765 #endif // BC_ENABLED 766 #endif // !BC_ENABLE_LIBRARY 767 768 BcDig* temps_buf[BC_VM_MAX_TEMPS]; 769 770 #if BC_DEBUG_CODE 771 772 /// The depth for BC_FUNC_ENTER and BC_FUNC_EXIT. 773 size_t func_depth; 774 775 #endif // BC_DEBUG_CODE 776 777 } BcVm; 778 779 /** 780 * Print the copyright banner and help if it's non-NULL. 781 * @param help The help message to print if it's non-NULL. 782 */ 783 void 784 bc_vm_info(const char* const help); 785 786 /** 787 * The entrance point for bc/dc together. 788 * @param argc The count of arguments. 789 * @param argv The argument array. 790 */ 791 void 792 bc_vm_boot(int argc, char* argv[]); 793 794 /** 795 * Initializes some of the BcVm global. This is separate to make things easier 796 * on the library code. 797 */ 798 void 799 bc_vm_init(void); 800 801 /** 802 * Frees the BcVm global. 803 */ 804 void 805 bc_vm_shutdown(void); 806 807 /** 808 * Add a temp to the temp array. 809 * @param num The BcDig array to add to the temp array. 810 */ 811 void 812 bc_vm_addTemp(BcDig* num); 813 814 /** 815 * Return the temp on the top of the temp stack, or NULL if there are none. 816 * @return A temp, or NULL if none exist. 817 */ 818 BcDig* 819 bc_vm_takeTemp(void); 820 821 /** 822 * Gets the top temp of the temp stack. This is separate from bc_vm_takeTemp() 823 * to quiet a GCC warning about longjmp() clobbering in bc_num_init(). 824 * @return A temp, or NULL if none exist. 825 */ 826 BcDig* 827 bc_vm_getTemp(void); 828 829 /** 830 * Frees all temporaries. 831 */ 832 void 833 bc_vm_freeTemps(void); 834 835 #if !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY 836 837 /** 838 * Erases the flush argument if history does not exist because it does not 839 * matter if history does not exist. 840 */ 841 #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c) 842 843 #else // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY 844 845 // This is here to satisfy a clang warning about recursive macros. 846 #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c, t) 847 848 #endif // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY 849 850 /** 851 * Print to stdout with limited formating. 852 * @param fmt The format string. 853 */ 854 void 855 bc_vm_printf(const char* fmt, ...); 856 857 /** 858 * Puts a char into the stdout buffer. 859 * @param c The character to put on the stdout buffer. 860 * @param type The flush type. 861 */ 862 void 863 bc_vm_putchar(int c, BcFlushType type); 864 865 /** 866 * Multiplies @a n and @a size and throws an allocation error if overflow 867 * occurs. 868 * @param n The number of elements. 869 * @param size The size of each element. 870 * @return The product of @a n and @a size. 871 */ 872 size_t 873 bc_vm_arraySize(size_t n, size_t size); 874 875 /** 876 * Adds @a a and @a b and throws an error if overflow occurs. 877 * @param a The first operand. 878 * @param b The second operand. 879 * @return The sum of @a a and @a b. 880 */ 881 size_t 882 bc_vm_growSize(size_t a, size_t b); 883 884 /** 885 * Allocate @a n bytes and throw an allocation error if allocation fails. 886 * @param n The bytes to allocate. 887 * @return A pointer to the allocated memory. 888 */ 889 void* 890 bc_vm_malloc(size_t n); 891 892 /** 893 * Reallocate @a ptr to be @a n bytes and throw an allocation error if 894 * reallocation fails. 895 * @param ptr The pointer to a memory allocation to reallocate. 896 * @param n The bytes to allocate. 897 * @return A pointer to the reallocated memory. 898 */ 899 void* 900 bc_vm_realloc(void* ptr, size_t n); 901 902 /** 903 * Allocates space for, and duplicates, @a str. 904 * @param str The string to allocate. 905 * @return The allocated string. 906 */ 907 char* 908 bc_vm_strdup(const char* str); 909 910 /** 911 * Reads a line from stdin into BcVm's buffer field. 912 * @param clear True if the buffer should be cleared first, false otherwise. 913 * @return True if a line was read, false otherwise. 914 */ 915 bool 916 bc_vm_readLine(bool clear); 917 918 /** 919 * Reads a line from the command-line expressions into BcVm's buffer field. 920 * @param clear True if the buffer should be cleared first, false otherwise. 921 * @return True if a line was read, false otherwise. 922 */ 923 bool 924 bc_vm_readBuf(bool clear); 925 926 /** 927 * A convenience and portability function for OpenBSD's pledge(). 928 * @param promises The promises to pledge(). 929 * @param execpromises The exec promises to pledge(). 930 */ 931 void 932 bc_pledge(const char* promises, const char* execpromises); 933 934 /** 935 * Returns the value of an environment variable. 936 * @param var The environment variable. 937 * @return The value of the environment variable. 938 */ 939 char* 940 bc_vm_getenv(const char* var); 941 942 /** 943 * Frees an environment variable value. 944 * @param val The value to free. 945 */ 946 void 947 bc_vm_getenvFree(char* val); 948 949 #if BC_DEBUG_CODE 950 951 /** 952 * Start executing a jump series. 953 * @param f The name of the function that started the jump series. 954 */ 955 void 956 bc_vm_jmp(const char* f); 957 958 #else // BC_DEBUG_CODE 959 960 /** 961 * Start executing a jump series. 962 */ 963 void 964 bc_vm_jmp(void); 965 966 #endif // BC_DEBUG_CODE 967 968 #if BC_ENABLE_LIBRARY 969 970 /** 971 * Handle an error. This is the true error handler. It will start a jump series 972 * if an error occurred. POSIX errors will not cause jumps when warnings are on 973 * or no POSIX errors are enabled. 974 * @param e The error. 975 */ 976 void 977 bc_vm_handleError(BcErr e); 978 979 /** 980 * Handle a fatal error. 981 * @param e The error. 982 */ 983 void 984 bc_vm_fatalError(BcErr e); 985 986 /** 987 * A function to call at exit. 988 */ 989 void 990 bc_vm_atexit(void); 991 992 #else // BC_ENABLE_LIBRARY 993 994 /** 995 * Calculates the number of decimal digits in the argument. 996 * @param val The value to calculate the number of decimal digits in. 997 * @return The number of decimal digits in @a val. 998 */ 999 size_t 1000 bc_vm_numDigits(size_t val); 1001 1002 #ifndef NDEBUG 1003 1004 /** 1005 * Handle an error. This is the true error handler. It will start a jump series 1006 * if an error occurred. POSIX errors will not cause jumps when warnings are on 1007 * or no POSIX errors are enabled. 1008 * @param e The error. 1009 * @param file The source file where the error occurred. 1010 * @param fline The line in the source file where the error occurred. 1011 * @param line The bc source line where the error occurred. 1012 */ 1013 void 1014 bc_vm_handleError(BcErr e, const char* file, int fline, size_t line, ...); 1015 1016 #else // NDEBUG 1017 1018 /** 1019 * Handle an error. This is the true error handler. It will start a jump series 1020 * if an error occurred. POSIX errors will not cause jumps when warnings are on 1021 * or no POSIX errors are enabled. 1022 * @param e The error. 1023 * @param line The bc source line where the error occurred. 1024 */ 1025 void 1026 bc_vm_handleError(BcErr e, size_t line, ...); 1027 1028 #endif // NDEBUG 1029 1030 /** 1031 * Handle a fatal error. 1032 * @param e The error. 1033 */ 1034 #if !BC_ENABLE_MEMCHECK 1035 BC_NORETURN 1036 #endif // !BC_ENABLE_MEMCHECK 1037 void 1038 bc_vm_fatalError(BcErr e); 1039 1040 /** 1041 * A function to call at exit. 1042 * @param status The exit status. 1043 */ 1044 int 1045 bc_vm_atexit(int status); 1046 #endif // BC_ENABLE_LIBRARY 1047 1048 /// A reference to the copyright header. 1049 extern const char bc_copyright[]; 1050 1051 /// A reference to the array of default error category names. 1052 extern const char* bc_errs[]; 1053 1054 /// A reference to the array of error category indices for each error. 1055 extern const uchar bc_err_ids[]; 1056 1057 /// A reference to the array of default error messages. 1058 extern const char* const bc_err_msgs[]; 1059 1060 /// A reference to the pledge() promises at start. 1061 extern const char bc_pledge_start[]; 1062 1063 #if BC_ENABLE_HISTORY 1064 1065 /// A reference to the end pledge() promises when using history. 1066 extern const char bc_pledge_end_history[]; 1067 1068 #endif // BC_ENABLE_HISTORY 1069 1070 /// A reference to the end pledge() promises when *not* using history. 1071 extern const char bc_pledge_end[]; 1072 1073 #if !BC_ENABLE_LIBRARY 1074 1075 /// A reference to the global data. 1076 extern BcVm* vm; 1077 1078 /// The global data. 1079 extern BcVm vm_data; 1080 1081 /// A reference to the global output buffers. 1082 extern char output_bufs[BC_VM_BUF_SIZE]; 1083 1084 #endif // !BC_ENABLE_LIBRARY 1085 1086 #endif // BC_VM_H 1087