1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2021 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 BC_ENABLE_NLS 83 #define BC_ENABLE_NLS (0) 84 #endif // BC_ENABLE_NLS 85 86 #ifndef MAINEXEC 87 #define MAINEXEC bc 88 #endif // MAINEXEC 89 90 #ifndef _WIN32 91 #ifndef EXECPREFIX 92 #define EXECPREFIX 93 #endif // EXECPREFIX 94 #else // _WIN32 95 #undef EXECPREFIX 96 #endif // _WIN32 97 98 /** 99 * Generate a string from text. 100 * @parm V The text to generate a string for. 101 */ 102 #define GEN_STR(V) #V 103 104 /** 105 * Help generate a string from text. The preprocessor requires this two-step 106 * process. Trust me. 107 * @parm V The text to generate a string for. 108 */ 109 #define GEN_STR2(V) GEN_STR(V) 110 111 /// The version as a string. VERSION must be defined previously, usually by the 112 /// build system. 113 #define BC_VERSION GEN_STR2(VERSION) 114 115 /// The main executable name as a string. MAINEXEC must be defined previously, 116 /// usually by the build system. 117 #define BC_MAINEXEC GEN_STR2(MAINEXEC) 118 119 /// The build type as a string. BUILD_TYPE must be defined previously, usually 120 /// by the build system. 121 #define BC_BUILD_TYPE GEN_STR2(BUILD_TYPE) 122 123 // We only allow an empty executable prefix on Windows. 124 #ifndef _WIN32 125 #define BC_EXECPREFIX GEN_STR2(EXECPREFIX) 126 #else // _WIN32 127 #define BC_EXECPREFIX "" 128 #endif // _WIN32 129 130 #if !BC_ENABLE_LIBRARY 131 132 #if DC_ENABLED 133 134 /// The flag for the extended register option. 135 #define DC_FLAG_X (UINTMAX_C(1)<<0) 136 137 #endif // DC_ENABLED 138 139 #if BC_ENABLED 140 141 /// The flag for the POSIX warning option. 142 #define BC_FLAG_W (UINTMAX_C(1)<<1) 143 144 /// The flag for the POSIX error option. 145 #define BC_FLAG_S (UINTMAX_C(1)<<2) 146 147 /// The flag for the math library option. 148 #define BC_FLAG_L (UINTMAX_C(1)<<3) 149 150 /// The flag for the global stacks option. 151 #define BC_FLAG_G (UINTMAX_C(1)<<4) 152 153 #endif // BC_ENABLED 154 155 /// The flag for quiet, though this one is reversed; the option clears the flag. 156 #define BC_FLAG_Q (UINTMAX_C(1)<<5) 157 158 /// The flag for interactive. 159 #define BC_FLAG_I (UINTMAX_C(1)<<6) 160 161 /// The flag for prompt. This is also reversed; the option clears the flag. 162 #define BC_FLAG_P (UINTMAX_C(1)<<7) 163 164 /// The flag for read prompt. This is also reversed; the option clears the flag. 165 #define BC_FLAG_R (UINTMAX_C(1)<<8) 166 167 /// The flag for stdin being a TTY. 168 #define BC_FLAG_TTYIN (UINTMAX_C(1)<<9) 169 170 /// The flag for TTY mode. 171 #define BC_FLAG_TTY (UINTMAX_C(1)<<10) 172 173 /// The flag for reset on SIGINT. 174 #define BC_FLAG_SIGINT (UINTMAX_C(1)<<11) 175 176 /// A convenience macro for getting the TTYIN flag. 177 #define BC_TTYIN (vm.flags & BC_FLAG_TTYIN) 178 179 /// A convenience macro for getting the TTY flag. 180 #define BC_TTY (vm.flags & BC_FLAG_TTY) 181 182 /// A convenience macro for getting the SIGINT flag. 183 #define BC_SIGINT (vm.flags & BC_FLAG_SIGINT) 184 185 #if BC_ENABLED 186 187 /// A convenience macro for getting the POSIX error flag. 188 #define BC_S (vm.flags & BC_FLAG_S) 189 190 /// A convenience macro for getting the POSIX warning flag. 191 #define BC_W (vm.flags & BC_FLAG_W) 192 193 /// A convenience macro for getting the math library flag. 194 #define BC_L (vm.flags & BC_FLAG_L) 195 196 /// A convenience macro for getting the global stacks flag. 197 #define BC_G (vm.flags & BC_FLAG_G) 198 199 #endif // BC_ENABLED 200 201 #if DC_ENABLED 202 203 /// A convenience macro for getting the extended register flag. 204 #define DC_X (vm.flags & DC_FLAG_X) 205 206 #endif // DC_ENABLED 207 208 /// A convenience macro for getting the interactive flag. 209 #define BC_I (vm.flags & BC_FLAG_I) 210 211 /// A convenience macro for getting the prompt flag. 212 #define BC_P (vm.flags & BC_FLAG_P) 213 214 /// A convenience macro for getting the read prompt flag. 215 #define BC_R (vm.flags & BC_FLAG_R) 216 217 #if BC_ENABLED 218 219 /// A convenience macro for checking if bc is in POSIX mode. 220 #define BC_IS_POSIX (BC_S || BC_W) 221 222 #if DC_ENABLED 223 224 /// Returns true if bc is running. 225 #define BC_IS_BC (vm.name[0] != 'd') 226 227 /// Returns true if dc is running. 228 #define BC_IS_DC (vm.name[0] == 'd') 229 230 #else // DC_ENABLED 231 232 /// Returns true if bc is running. 233 #define BC_IS_BC (1) 234 235 /// Returns true if dc is running. 236 #define BC_IS_DC (0) 237 238 #endif // DC_ENABLED 239 240 #else // BC_ENABLED 241 242 /// A convenience macro for checking if bc is in POSIX mode. 243 #define BC_IS_POSIX (0) 244 245 /// Returns true if bc is running. 246 #define BC_IS_BC (0) 247 248 /// Returns true if dc is running. 249 #define BC_IS_DC (1) 250 251 #endif // BC_ENABLED 252 253 /// A convenience macro for checking if the prompt is enabled. 254 #define BC_PROMPT (BC_P) 255 256 #endif // !BC_ENABLE_LIBRARY 257 258 /** 259 * Returns the max of its two arguments. This evaluates arguments twice, so be 260 * careful what args you give it. 261 * @param a The first argument. 262 * @param b The second argument. 263 * @return The max of the two arguments. 264 */ 265 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) 266 267 /** 268 * Returns the min of its two arguments. This evaluates arguments twice, so be 269 * careful what args you give it. 270 * @param a The first argument. 271 * @param b The second argument. 272 * @return The min of the two arguments. 273 */ 274 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) 275 276 /// Returns the max obase that is allowed. 277 #define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW)) 278 279 /// Returns the max array size that is allowed. 280 #define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1)) 281 282 /// Returns the max scale that is allowed. 283 #define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1)) 284 285 /// Returns the max string length that is allowed. 286 #define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1)) 287 288 /// Returns the max identifier length that is allowed. 289 #define BC_MAX_NAME BC_MAX_STRING 290 291 /// Returns the max number size that is allowed. 292 #define BC_MAX_NUM BC_MAX_SCALE 293 294 #if BC_ENABLE_EXTRA_MATH 295 296 /// Returns the max random integer that can be returned. 297 #define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1)) 298 299 #endif // BC_ENABLE_EXTRA_MATH 300 301 /// Returns the max exponent that is allowed. 302 #define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX)) 303 304 /// Returns the max number of variables that is allowed. 305 #define BC_MAX_VARS ((ulong) (SIZE_MAX - 1)) 306 307 /// The size of the global buffer. 308 #define BC_VM_BUF_SIZE (1<<12) 309 310 /// The amount of the global buffer allocated to stdout. 311 #define BC_VM_STDOUT_BUF_SIZE (1<<11) 312 313 /// The amount of the global buffer allocated to stderr. 314 #define BC_VM_STDERR_BUF_SIZE (1<<10) 315 316 /// The amount of the global buffer allocated to stdin. 317 #define BC_VM_STDIN_BUF_SIZE (BC_VM_STDERR_BUF_SIZE - 1) 318 319 /// The max number of temporary BcNums that can be kept. 320 #define BC_VM_MAX_TEMPS (1 << 9) 321 322 /// The capacity of the one BcNum, which is a constant. 323 #define BC_VM_ONE_CAP (1) 324 325 /** 326 * Returns true if a BcResult is safe for garbage collection. 327 * @param r The BcResult to test. 328 * @return True if @a r is safe to garbage collect. 329 */ 330 #define BC_VM_SAFE_RESULT(r) ((r)->t >= BC_RESULT_TEMP) 331 332 /// The invalid locale catalog return value. 333 #define BC_VM_INVALID_CATALOG ((nl_catd) -1) 334 335 /** 336 * Returns true if the *unsigned* multiplication overflows. 337 * @param a The first operand. 338 * @param b The second operand. 339 * @param r The product. 340 * @return True if the multiplication of @a a and @a b overflows. 341 */ 342 #define BC_VM_MUL_OVERFLOW(a, b, r) \ 343 ((r) >= SIZE_MAX || ((a) != 0 && (r) / (a) != (b))) 344 345 /// The global vm struct. This holds all of the global data besides the file 346 /// buffers. 347 typedef struct BcVm { 348 349 /// The current status. This is volatile sig_atomic_t because it is also 350 /// used in the signal handler. See the development manual 351 /// (manuals/development.md#async-signal-safe-signal-handling) for more 352 /// information. 353 volatile sig_atomic_t status; 354 355 /// Non-zero if a jump series is in progress and items should be popped off 356 /// the jmp_bufs vector. This is volatile sig_atomic_t because it is also 357 /// used in the signal handler. See the development manual 358 /// (manuals/development.md#async-signal-safe-signal-handling) for more 359 /// information. 360 volatile sig_atomic_t sig_pop; 361 362 #if !BC_ENABLE_LIBRARY 363 364 /// The parser. 365 BcParse prs; 366 367 /// The program. 368 BcProgram prog; 369 370 /// A buffer for lines for stdin. 371 BcVec line_buf; 372 373 /// A buffer to hold a series of lines from stdin. Sometimes, multiple lines 374 /// are necessary for parsing, such as a comment that spans multiple lines. 375 BcVec buffer; 376 377 /// A parser to parse read expressions. 378 BcParse read_prs; 379 380 /// A buffer for read expressions. 381 BcVec read_buf; 382 383 #endif // !BC_ENABLE_LIBRARY 384 385 /// A vector of jmp_bufs for doing a jump series. This allows exception-type 386 /// error handling, while allowing me to do cleanup on the way. 387 BcVec jmp_bufs; 388 389 /// The number of temps in the temps array. 390 size_t temps_len; 391 392 #if BC_ENABLE_LIBRARY 393 394 /// The vector of contexts for the library. 395 BcVec ctxts; 396 397 /// The vector for creating strings to pass to the client. 398 BcVec out; 399 400 /// The PRNG. 401 BcRNG rng; 402 403 /// The current error. 404 BclError err; 405 406 /// Whether or not bcl should abort on fatal errors. 407 bool abrt; 408 409 /// The number of "references," or times that the library was initialized. 410 unsigned int refs; 411 412 /// Non-zero if bcl is running. This is volatile sig_atomic_t because it is 413 /// also used in the signal handler. See the development manual 414 /// (manuals/development.md#async-signal-safe-signal-handling) for more 415 /// information. 416 volatile sig_atomic_t running; 417 418 #endif // BC_ENABLE_LIBRARY 419 420 #if !BC_ENABLE_LIBRARY 421 422 /// A pointer to the filename of the current file. This is not owned by the 423 /// BcVm struct. 424 const char* file; 425 426 /// The message printed when SIGINT happens. 427 const char *sigmsg; 428 429 #endif // !BC_ENABLE_LIBRARY 430 431 /// Non-zero when signals are "locked." This is volatile sig_atomic_t 432 /// because it is also used in the signal handler. See the development 433 /// manual (manuals/development.md#async-signal-safe-signal-handling) for 434 /// more information. 435 volatile sig_atomic_t sig_lock; 436 437 /// Non-zero when a signal has been received, but not acted on. This is 438 /// volatile sig_atomic_t because it is also used in the signal handler. See 439 /// the development manual 440 /// (manuals/development.md#async-signal-safe-signal-handling) for more 441 /// information. 442 volatile sig_atomic_t sig; 443 444 #if !BC_ENABLE_LIBRARY 445 446 /// The length of sigmsg. 447 uchar siglen; 448 449 /// The instruction used for returning from a read() call. 450 uchar read_ret; 451 452 /// The flags field used by most macros above. 453 uint16_t flags; 454 455 /// The number of characters printed in the current line. This is used 456 /// because bc has a limit of the number of characters it can print per 457 /// line. 458 uint16_t nchars; 459 460 /// The length of the line we can print. The user can set this if they wish. 461 uint16_t line_len; 462 463 /// True if bc should error if expressions are encountered during option 464 /// parsing, false otherwise. 465 bool no_exprs; 466 467 /// True if bc should exit if expresions are encountered. 468 bool exit_exprs; 469 470 /// True if EOF was encountered. 471 bool eof; 472 473 /// True if bc is currently reading from stdin. 474 bool is_stdin; 475 476 #if BC_ENABLED 477 478 /// True if keywords should not be redefined. This is only true for the 479 /// builtin math libraries for bc. 480 bool no_redefine; 481 482 #endif // BC_ENABLED 483 484 #endif // !BC_ENABLE_LIBRARY 485 486 /// An array of maxes for the globals. 487 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH]; 488 489 #if !BC_ENABLE_LIBRARY 490 491 /// A vector of filenames to process. 492 BcVec files; 493 494 /// A vector of expressions to process. 495 BcVec exprs; 496 497 /// The name of the calculator under use. This is used by BC_IS_BC and 498 /// BC_IS_DC. 499 const char *name; 500 501 /// The help text for the calculator. 502 const char *help; 503 504 #if BC_ENABLE_HISTORY 505 506 /// The history data. 507 BcHistory history; 508 509 #endif // BC_ENABLE_HISTORY 510 511 /// The function to call to get the next lex token. 512 BcLexNext next; 513 514 /// The function to call to parse. 515 BcParseParse parse; 516 517 /// The function to call to parse expressions. 518 BcParseExpr expr; 519 520 /// The text to display to label functions in error messages. 521 const char *func_header; 522 523 /// The names of the categories of errors. 524 const char *err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED]; 525 526 /// The messages for each error. 527 const char *err_msgs[BC_ERR_NELEMS]; 528 529 /// The locale. 530 const char *locale; 531 532 #endif // !BC_ENABLE_LIBRARY 533 534 /// The last base used to parse. 535 BcBigDig last_base; 536 537 /// The last power of last_base used to parse. 538 BcBigDig last_pow; 539 540 /// The last exponent of base that equals last_pow. 541 BcBigDig last_exp; 542 543 /// BC_BASE_POW - last_pow. 544 BcBigDig last_rem; 545 546 #if !BC_ENABLE_LIBRARY 547 548 /// A buffer of environment arguments. This is the actual value of the 549 /// environment variable. 550 char *env_args_buffer; 551 552 /// A vector for environment arguments after parsing. 553 BcVec env_args; 554 555 /// A BcNum set to constant 0. 556 BcNum zero; 557 558 #endif // !BC_ENABLE_LIBRARY 559 560 /// A BcNum set to constant 1. 561 BcNum one; 562 563 /// A BcNum holding the max number held by a BcBigDig plus 1. 564 BcNum max; 565 566 /// A BcNum holding the max number held by a BcBigDig times 2 plus 1. 567 BcNum max2; 568 569 /// The BcDig array for max. 570 BcDig max_num[BC_NUM_BIGDIG_LOG10]; 571 572 /// The BcDig array for max2. 573 BcDig max2_num[BC_NUM_BIGDIG_LOG10]; 574 575 // The BcDig array for the one BcNum. 576 BcDig one_num[BC_VM_ONE_CAP]; 577 578 #if !BC_ENABLE_LIBRARY 579 580 // The BcDig array for the zero BcNum. 581 BcDig zero_num[BC_VM_ONE_CAP]; 582 583 /// The stdout file. 584 BcFile fout; 585 586 /// The stderr file. 587 BcFile ferr; 588 589 #if BC_ENABLE_NLS 590 591 /// The locale catalog. 592 nl_catd catalog; 593 594 #endif // BC_ENABLE_NLS 595 596 /// A pointer to the stdin buffer. 597 char *buf; 598 599 /// The number of items in the input buffer. 600 size_t buf_len; 601 602 /// The slab for constants in the main function. This is separate for 603 /// garbage collection reasons. 604 BcVec main_const_slab; 605 606 //// The slab for all other strings for the main function. 607 BcVec main_slabs; 608 609 /// The slab for function names, strings in other functions, and constants 610 /// in other functions. 611 BcVec other_slabs; 612 613 #if BC_ENABLED 614 615 /// An array of booleans for which bc keywords have been redefined if 616 /// BC_REDEFINE_KEYWORDS is non-zero. 617 bool redefined_kws[BC_LEX_NKWS]; 618 619 #endif // BC_ENABLED 620 #endif // !BC_ENABLE_LIBRARY 621 622 #if BC_DEBUG_CODE 623 624 /// The depth for BC_FUNC_ENTER and BC_FUNC_EXIT. 625 size_t func_depth; 626 627 #endif // BC_DEBUG_CODE 628 629 } BcVm; 630 631 /** 632 * Print the copyright banner and help if it's non-NULL. 633 * @param help The help message to print if it's non-NULL. 634 */ 635 void bc_vm_info(const char* const help); 636 637 /** 638 * The entrance point for bc/dc together. 639 * @param argc The count of arguments. 640 * @param argv The argument array. 641 */ 642 void bc_vm_boot(int argc, char *argv[]); 643 644 /** 645 * Initializes some of the BcVm global. This is separate to make things easier 646 * on the library code. 647 */ 648 void bc_vm_init(void); 649 650 /** 651 * Frees the BcVm global. 652 */ 653 void bc_vm_shutdown(void); 654 655 /** 656 * Add a temp to the temp array. 657 * @param num The BcDig array to add to the temp array. 658 */ 659 void bc_vm_addTemp(BcDig *num); 660 661 /** 662 * Dish out a temp, or NULL if there are none. 663 * @return A temp, or NULL if none exist. 664 */ 665 BcDig* bc_vm_takeTemp(void); 666 667 /** 668 * Frees all temporaries. 669 */ 670 void bc_vm_freeTemps(void); 671 672 #if !BC_ENABLE_HISTORY 673 674 /** 675 * Erases the flush argument if history does not exist because it does not 676 * matter if history does not exist. 677 */ 678 #define bc_vm_putchar(c, t) bc_vm_putchar(c) 679 680 #endif // !BC_ENABLE_HISTORY 681 682 /** 683 * Print to stdout with limited formating. 684 * @param fmt The format string. 685 */ 686 void bc_vm_printf(const char *fmt, ...); 687 688 /** 689 * Puts a char into the stdout buffer. 690 * @param c The character to put on the stdout buffer. 691 * @param type The flush type. 692 */ 693 void bc_vm_putchar(int c, BcFlushType type); 694 695 /** 696 * Multiplies @a n and @a size and throws an allocation error if overflow 697 * occurs. 698 * @param n The number of elements. 699 * @param size The size of each element. 700 * @return The product of @a n and @a size. 701 */ 702 size_t bc_vm_arraySize(size_t n, size_t size); 703 704 /** 705 * Adds @a a and @a b and throws an error if overflow occurs. 706 * @param a The first operand. 707 * @param b The second operand. 708 * @return The sum of @a a and @a b. 709 */ 710 size_t bc_vm_growSize(size_t a, size_t b); 711 712 /** 713 * Allocate @a n bytes and throw an allocation error if allocation fails. 714 * @param n The bytes to allocate. 715 * @return A pointer to the allocated memory. 716 */ 717 void* bc_vm_malloc(size_t n); 718 719 /** 720 * Reallocate @a ptr to be @a n bytes and throw an allocation error if 721 * reallocation fails. 722 * @param ptr The pointer to a memory allocation to reallocate. 723 * @param n The bytes to allocate. 724 * @return A pointer to the reallocated memory. 725 */ 726 void* bc_vm_realloc(void *ptr, size_t n); 727 728 /** 729 * Allocates space for, and duplicates, @a str. 730 * @param str The string to allocate. 731 * @return The allocated string. 732 */ 733 char* bc_vm_strdup(const char *str); 734 735 /** 736 * Reads a line into BcVm's buffer field. 737 * @param clear True if the buffer should be cleared first, false otherwise. 738 * @return True if a line was read, false otherwise. 739 */ 740 bool bc_vm_readLine(bool clear); 741 742 /** 743 * A convenience and portability function for OpenBSD's pledge(). 744 * @param promises The promises to pledge(). 745 * @param execpromises The exec promises to pledge(). 746 */ 747 void bc_pledge(const char *promises, const char *execpromises); 748 749 /** 750 * Returns the value of an environment variable. 751 * @param var The environment variable. 752 * @return The value of the environment variable. 753 */ 754 char* bc_vm_getenv(const char* var); 755 756 /** 757 * Frees an environment variable value. 758 * @param val The value to free. 759 */ 760 void bc_vm_getenvFree(char* val); 761 762 #if BC_DEBUG_CODE 763 764 /** 765 * Start executing a jump series. 766 * @param f The name of the function that started the jump series. 767 */ 768 void bc_vm_jmp(const char *f); 769 #else // BC_DEBUG_CODE 770 771 /** 772 * Start executing a jump series. 773 */ 774 void bc_vm_jmp(void); 775 776 #endif // BC_DEBUG_CODE 777 778 #if BC_ENABLE_LIBRARY 779 780 /** 781 * Handle an error. This is the true error handler. It will start a jump series 782 * if an error occurred. POSIX errors will not cause jumps when warnings are on 783 * or no POSIX errors are enabled. 784 * @param e The error. 785 */ 786 void bc_vm_handleError(BcErr e); 787 788 /** 789 * Handle a fatal error. 790 * @param e The error. 791 */ 792 void bc_vm_fatalError(BcErr e); 793 794 /** 795 * A function to call at exit. 796 */ 797 void bc_vm_atexit(void); 798 799 #else // BC_ENABLE_LIBRARY 800 801 /** 802 * Handle an error. This is the true error handler. It will start a jump series 803 * if an error occurred. POSIX errors will not cause jumps when warnings are on 804 * or no POSIX errors are enabled. 805 * @param e The error. 806 * @param line The source line where the error occurred. 807 */ 808 void bc_vm_handleError(BcErr e, size_t line, ...); 809 810 /** 811 * Handle a fatal error. 812 * @param e The error. 813 */ 814 #if !BC_ENABLE_MEMCHECK 815 BC_NORETURN 816 #endif // !BC_ENABLE_MEMCHECK 817 void bc_vm_fatalError(BcErr e); 818 819 /** 820 * A function to call at exit. 821 * @param status The exit status. 822 */ 823 int bc_vm_atexit(int status); 824 #endif // BC_ENABLE_LIBRARY 825 826 /// A reference to the copyright header. 827 extern const char bc_copyright[]; 828 829 /// A reference to the format string for source code line printing. 830 extern const char* const bc_err_line; 831 832 /// A reference to the format string for source code function printing. 833 extern const char* const bc_err_func_header; 834 835 /// A reference to the array of default error category names. 836 extern const char *bc_errs[]; 837 838 /// A reference to the array of error category indices for each error. 839 extern const uchar bc_err_ids[]; 840 841 /// A reference to the array of default error messages. 842 extern const char* const bc_err_msgs[]; 843 844 /// A reference to the pledge() promises at start. 845 extern const char bc_pledge_start[]; 846 847 #if BC_ENABLE_HISTORY 848 849 /// A reference to the end pledge() promises when using history. 850 extern const char bc_pledge_end_history[]; 851 852 #endif // BC_ENABLE_HISTORY 853 854 /// A reference to the end pledge() promises when *not* using history. 855 extern const char bc_pledge_end[]; 856 857 /// A reference to the global data. 858 extern BcVm vm; 859 860 /// A reference to the global output buffers. 861 extern char output_bufs[BC_VM_BUF_SIZE]; 862 863 #endif // BC_VM_H 864