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 #if BC_ENABLE_EXTRA_MATH 564 565 /// The PRNG. 566 BcRNG rng; 567 568 #endif // BC_ENABLE_EXTRA_MATH 569 570 /// The current error. 571 BclError err; 572 573 /// Whether or not bcl should abort on fatal errors. 574 bool abrt; 575 576 /// Whether or not to print leading zeros. 577 bool leading_zeroes; 578 579 /// Whether or not to clamp digits that are greater than or equal to the 580 /// current ibase. 581 bool digit_clamp; 582 583 /// The number of "references," or times that the library was initialized. 584 unsigned int refs; 585 586 #else // BC_ENABLE_LIBRARY 587 588 /// A pointer to the filename of the current file. This is not owned by the 589 /// BcVm struct. 590 const char* file; 591 592 /// The message printed when SIGINT happens. 593 const char* sigmsg; 594 595 /// Non-zero when signals are "locked." This is volatile sig_atomic_t 596 /// because it is also used in the signal handler. See the development 597 /// manual (manuals/development.md#async-signal-safe-signal-handling) for 598 /// more information. 599 volatile sig_atomic_t sig_lock; 600 601 /// Non-zero when a signal has been received, but not acted on. This is 602 /// volatile sig_atomic_t because it is also used in the signal handler. See 603 /// the development manual 604 /// (manuals/development.md#async-signal-safe-signal-handling) for more 605 /// information. 606 volatile sig_atomic_t sig; 607 608 /// The length of sigmsg. 609 uchar siglen; 610 611 /// The instruction used for returning from a read() call. 612 uchar read_ret; 613 614 /// The flags field used by most macros above. 615 uint16_t flags; 616 617 /// The number of characters printed in the current line. This is used 618 /// because bc has a limit of the number of characters it can print per 619 /// line. 620 uint16_t nchars; 621 622 /// The length of the line we can print. The user can set this if they wish. 623 uint16_t line_len; 624 625 /// True if bc should error if expressions are encountered during option 626 /// parsing, false otherwise. 627 bool no_exprs; 628 629 /// True if bc should exit if expresions are encountered. 630 bool exit_exprs; 631 632 /// True if EOF was encountered. 633 bool eof; 634 635 /// The mode that the program is in. 636 uchar mode; 637 638 #if BC_ENABLED 639 640 /// True if keywords should not be redefined. This is only true for the 641 /// builtin math libraries for bc. 642 bool no_redefine; 643 644 #endif // BC_ENABLED 645 646 /// A vector of filenames to process. 647 BcVec files; 648 649 /// A vector of expressions to process. 650 BcVec exprs; 651 652 /// The name of the calculator under use. This is used by BC_IS_BC and 653 /// BC_IS_DC. 654 const char* name; 655 656 /// The help text for the calculator. 657 const char* help; 658 659 #if BC_ENABLE_HISTORY 660 661 /// The history data. 662 BcHistory history; 663 664 #endif // BC_ENABLE_HISTORY 665 666 /// The function to call to get the next lex token. 667 BcLexNext next; 668 669 /// The function to call to parse. 670 BcParseParse parse; 671 672 /// The function to call to parse expressions. 673 BcParseExpr expr; 674 675 /// The names of the categories of errors. 676 const char* err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED]; 677 678 /// The messages for each error. 679 const char* err_msgs[BC_ERR_NELEMS]; 680 681 #if BC_ENABLE_NLS 682 /// The locale. 683 const char* locale; 684 #endif // BC_ENABLE_NLS 685 686 #endif // BC_ENABLE_LIBRARY 687 688 /// An array of maxes for the globals. 689 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH]; 690 691 /// The last base used to parse. 692 BcBigDig last_base; 693 694 /// The last power of last_base used to parse. 695 BcBigDig last_pow; 696 697 /// The last exponent of base that equals last_pow. 698 BcBigDig last_exp; 699 700 /// BC_BASE_POW - last_pow. 701 BcBigDig last_rem; 702 703 #if !BC_ENABLE_LIBRARY 704 705 /// A buffer of environment arguments. This is the actual value of the 706 /// environment variable. 707 char* env_args_buffer; 708 709 /// A vector for environment arguments after parsing. 710 BcVec env_args; 711 712 /// A BcNum set to constant 0. 713 BcNum zero; 714 715 #endif // !BC_ENABLE_LIBRARY 716 717 /// A BcNum set to constant 1. 718 BcNum one; 719 720 /// A BcNum holding the max number held by a BcBigDig plus 1. 721 BcNum max; 722 723 /// A BcNum holding the max number held by a BcBigDig times 2 plus 1. 724 BcNum max2; 725 726 /// The BcDig array for max. 727 BcDig max_num[BC_NUM_BIGDIG_LOG10]; 728 729 /// The BcDig array for max2. 730 BcDig max2_num[BC_NUM_BIGDIG_LOG10]; 731 732 // The BcDig array for the one BcNum. 733 BcDig one_num[BC_VM_ONE_CAP]; 734 735 #if !BC_ENABLE_LIBRARY 736 737 // The BcDig array for the zero BcNum. 738 BcDig zero_num[BC_VM_ONE_CAP]; 739 740 /// The stdout file. 741 BcFile fout; 742 743 /// The stderr file. 744 BcFile ferr; 745 746 #if BC_ENABLE_NLS 747 748 /// The locale catalog. 749 nl_catd catalog; 750 751 #endif // BC_ENABLE_NLS 752 753 /// A pointer to the stdin buffer. 754 char* buf; 755 756 /// The number of items in the input buffer. 757 size_t buf_len; 758 759 /// The slabs vector for constants, strings, function names, and other 760 /// string-like things. 761 BcVec slabs; 762 763 #if BC_ENABLED 764 765 /// An array of booleans for which bc keywords have been redefined if 766 /// BC_REDEFINE_KEYWORDS is non-zero. 767 bool redefined_kws[BC_LEX_NKWS]; 768 769 #endif // BC_ENABLED 770 #endif // !BC_ENABLE_LIBRARY 771 772 BcDig* temps_buf[BC_VM_MAX_TEMPS]; 773 774 #if BC_DEBUG_CODE 775 776 /// The depth for BC_FUNC_ENTER and BC_FUNC_EXIT. 777 size_t func_depth; 778 779 #endif // BC_DEBUG_CODE 780 781 } BcVm; 782 783 /** 784 * Print the copyright banner and help if it's non-NULL. 785 * @param help The help message to print if it's non-NULL. 786 */ 787 void 788 bc_vm_info(const char* const help); 789 790 /** 791 * The entrance point for bc/dc together. 792 * @param argc The count of arguments. 793 * @param argv The argument array. 794 */ 795 void 796 bc_vm_boot(int argc, char* argv[]); 797 798 /** 799 * Initializes some of the BcVm global. This is separate to make things easier 800 * on the library code. 801 */ 802 void 803 bc_vm_init(void); 804 805 /** 806 * Frees the BcVm global. 807 */ 808 void 809 bc_vm_shutdown(void); 810 811 /** 812 * Add a temp to the temp array. 813 * @param num The BcDig array to add to the temp array. 814 */ 815 void 816 bc_vm_addTemp(BcDig* num); 817 818 /** 819 * Return the temp on the top of the temp stack, or NULL if there are none. 820 * @return A temp, or NULL if none exist. 821 */ 822 BcDig* 823 bc_vm_takeTemp(void); 824 825 /** 826 * Gets the top temp of the temp stack. This is separate from bc_vm_takeTemp() 827 * to quiet a GCC warning about longjmp() clobbering in bc_num_init(). 828 * @return A temp, or NULL if none exist. 829 */ 830 BcDig* 831 bc_vm_getTemp(void); 832 833 /** 834 * Frees all temporaries. 835 */ 836 void 837 bc_vm_freeTemps(void); 838 839 #if !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY 840 841 /** 842 * Erases the flush argument if history does not exist because it does not 843 * matter if history does not exist. 844 */ 845 #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c) 846 847 #else // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY 848 849 // This is here to satisfy a clang warning about recursive macros. 850 #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c, t) 851 852 #endif // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY 853 854 /** 855 * Print to stdout with limited formating. 856 * @param fmt The format string. 857 */ 858 void 859 bc_vm_printf(const char* fmt, ...); 860 861 /** 862 * Puts a char into the stdout buffer. 863 * @param c The character to put on the stdout buffer. 864 * @param type The flush type. 865 */ 866 void 867 bc_vm_putchar(int c, BcFlushType type); 868 869 /** 870 * Multiplies @a n and @a size and throws an allocation error if overflow 871 * occurs. 872 * @param n The number of elements. 873 * @param size The size of each element. 874 * @return The product of @a n and @a size. 875 */ 876 size_t 877 bc_vm_arraySize(size_t n, size_t size); 878 879 /** 880 * Adds @a a and @a b and throws an error if overflow occurs. 881 * @param a The first operand. 882 * @param b The second operand. 883 * @return The sum of @a a and @a b. 884 */ 885 size_t 886 bc_vm_growSize(size_t a, size_t b); 887 888 /** 889 * Allocate @a n bytes and throw an allocation error if allocation fails. 890 * @param n The bytes to allocate. 891 * @return A pointer to the allocated memory. 892 */ 893 void* 894 bc_vm_malloc(size_t n); 895 896 /** 897 * Reallocate @a ptr to be @a n bytes and throw an allocation error if 898 * reallocation fails. 899 * @param ptr The pointer to a memory allocation to reallocate. 900 * @param n The bytes to allocate. 901 * @return A pointer to the reallocated memory. 902 */ 903 void* 904 bc_vm_realloc(void* ptr, size_t n); 905 906 /** 907 * Allocates space for, and duplicates, @a str. 908 * @param str The string to allocate. 909 * @return The allocated string. 910 */ 911 char* 912 bc_vm_strdup(const char* str); 913 914 /** 915 * Reads a line from stdin into BcVm's buffer field. 916 * @param clear True if the buffer should be cleared first, false otherwise. 917 * @return True if a line was read, false otherwise. 918 */ 919 bool 920 bc_vm_readLine(bool clear); 921 922 /** 923 * Reads a line from the command-line expressions into BcVm's buffer field. 924 * @param clear True if the buffer should be cleared first, false otherwise. 925 * @return True if a line was read, false otherwise. 926 */ 927 bool 928 bc_vm_readBuf(bool clear); 929 930 /** 931 * A convenience and portability function for OpenBSD's pledge(). 932 * @param promises The promises to pledge(). 933 * @param execpromises The exec promises to pledge(). 934 */ 935 void 936 bc_pledge(const char* promises, const char* execpromises); 937 938 /** 939 * Returns the value of an environment variable. 940 * @param var The environment variable. 941 * @return The value of the environment variable. 942 */ 943 char* 944 bc_vm_getenv(const char* var); 945 946 /** 947 * Frees an environment variable value. 948 * @param val The value to free. 949 */ 950 void 951 bc_vm_getenvFree(char* val); 952 953 #if BC_DEBUG_CODE 954 955 /** 956 * Start executing a jump series. 957 * @param f The name of the function that started the jump series. 958 */ 959 void 960 bc_vm_jmp(const char* f); 961 962 #else // BC_DEBUG_CODE 963 964 /** 965 * Start executing a jump series. 966 */ 967 void 968 bc_vm_jmp(void); 969 970 #endif // BC_DEBUG_CODE 971 972 #if BC_ENABLE_LIBRARY 973 974 /** 975 * Handle an error. This is the true error handler. It will start a jump series 976 * if an error occurred. POSIX errors will not cause jumps when warnings are on 977 * or no POSIX errors are enabled. 978 * @param e The error. 979 */ 980 void 981 bc_vm_handleError(BcErr e); 982 983 /** 984 * Handle a fatal error. 985 * @param e The error. 986 */ 987 void 988 bc_vm_fatalError(BcErr e); 989 990 /** 991 * A function to call at exit. 992 */ 993 void 994 bc_vm_atexit(void); 995 996 #else // BC_ENABLE_LIBRARY 997 998 /** 999 * Calculates the number of decimal digits in the argument. 1000 * @param val The value to calculate the number of decimal digits in. 1001 * @return The number of decimal digits in @a val. 1002 */ 1003 size_t 1004 bc_vm_numDigits(size_t val); 1005 1006 #if BC_DEBUG 1007 1008 /** 1009 * Handle an error. This is the true error handler. It will start a jump series 1010 * if an error occurred. POSIX errors will not cause jumps when warnings are on 1011 * or no POSIX errors are enabled. 1012 * @param e The error. 1013 * @param file The source file where the error occurred. 1014 * @param fline The line in the source file where the error occurred. 1015 * @param line The bc source line where the error occurred. 1016 */ 1017 void 1018 bc_vm_handleError(BcErr e, const char* file, int fline, size_t line, ...); 1019 1020 #else // BC_DEBUG 1021 1022 /** 1023 * Handle an error. This is the true error handler. It will start a jump series 1024 * if an error occurred. POSIX errors will not cause jumps when warnings are on 1025 * or no POSIX errors are enabled. 1026 * @param e The error. 1027 * @param line The bc source line where the error occurred. 1028 */ 1029 void 1030 bc_vm_handleError(BcErr e, size_t line, ...); 1031 1032 #endif // BC_DEBUG 1033 1034 /** 1035 * Handle a fatal error. 1036 * @param e The error. 1037 */ 1038 #if !BC_ENABLE_MEMCHECK 1039 BC_NORETURN 1040 #endif // !BC_ENABLE_MEMCHECK 1041 void 1042 bc_vm_fatalError(BcErr e); 1043 1044 /** 1045 * A function to call at exit. 1046 * @param status The exit status. 1047 */ 1048 int 1049 bc_vm_atexit(int status); 1050 #endif // BC_ENABLE_LIBRARY 1051 1052 /// A reference to the copyright header. 1053 extern const char bc_copyright[]; 1054 1055 /// A reference to the array of default error category names. 1056 extern const char* bc_errs[]; 1057 1058 /// A reference to the array of error category indices for each error. 1059 extern const uchar bc_err_ids[]; 1060 1061 /// A reference to the array of default error messages. 1062 extern const char* const bc_err_msgs[]; 1063 1064 /// A reference to the pledge() promises at start. 1065 extern const char bc_pledge_start[]; 1066 1067 #if BC_ENABLE_HISTORY 1068 1069 /// A reference to the end pledge() promises when using history. 1070 extern const char bc_pledge_end_history[]; 1071 1072 #endif // BC_ENABLE_HISTORY 1073 1074 /// A reference to the end pledge() promises when *not* using history. 1075 extern const char bc_pledge_end[]; 1076 1077 #if !BC_ENABLE_LIBRARY 1078 1079 /// A reference to the global data. 1080 extern BcVm* vm; 1081 1082 /// The global data. 1083 extern BcVm vm_data; 1084 1085 /// A reference to the global output buffers. 1086 extern char output_bufs[BC_VM_BUF_SIZE]; 1087 1088 #endif // !BC_ENABLE_LIBRARY 1089 1090 #endif // BC_VM_H 1091