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