2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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.
30 * *****************************************************************************
32 * Definitions for bc's VM.
48 #error NLS is not supported on Windows.
53 #endif // BC_ENABLE_NLS
64 // We don't want to include this file for the library because it's unused.
65 #if !BC_ENABLE_LIBRARY
67 #endif // !BC_ENABLE_LIBRARY
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
74 // CHAR_BIT must be at least 6, for various reasons. I might want to bump this
75 // to 8 in the future.
77 #error CHAR_BIT must be at least 6.
95 * Generate a string from text.
96 * @parm V The text to generate a string for.
101 * Help generate a string from text. The preprocessor requires this two-step
103 * @parm V The text to generate a string for.
105 #define GEN_STR2(V) GEN_STR(V)
107 /// The version as a string. VERSION must be defined previously, usually by the
109 #define BC_VERSION GEN_STR2(VERSION)
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)
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)
119 // We only allow an empty executable prefix on Windows.
121 #define BC_EXECPREFIX GEN_STR2(EXECPREFIX)
123 #define BC_EXECPREFIX ""
126 #if !BC_ENABLE_LIBRARY
130 /// The flag for the extended register option.
131 #define DC_FLAG_X (UINTMAX_C(1) << 0)
137 /// The flag for the POSIX warning option.
138 #define BC_FLAG_W (UINTMAX_C(1) << 1)
140 /// The flag for the POSIX error option.
141 #define BC_FLAG_S (UINTMAX_C(1) << 2)
143 /// The flag for the math library option.
144 #define BC_FLAG_L (UINTMAX_C(1) << 3)
146 /// The flag for the global stacks option.
147 #define BC_FLAG_G (UINTMAX_C(1) << 4)
151 /// The flag for quiet, though this one is reversed; the option clears the flag.
152 #define BC_FLAG_Q (UINTMAX_C(1) << 5)
154 /// The flag for interactive.
155 #define BC_FLAG_I (UINTMAX_C(1) << 6)
157 /// The flag for prompt. This is also reversed; the option clears the flag.
158 #define BC_FLAG_P (UINTMAX_C(1) << 7)
160 /// The flag for read prompt. This is also reversed; the option clears the flag.
161 #define BC_FLAG_R (UINTMAX_C(1) << 8)
163 /// The flag for a leading zero.
164 #define BC_FLAG_Z (UINTMAX_C(1) << 9)
166 /// The flag for stdin being a TTY.
167 #define BC_FLAG_TTYIN (UINTMAX_C(1) << 10)
169 /// The flag for TTY mode.
170 #define BC_FLAG_TTY (UINTMAX_C(1) << 11)
172 /// The flag for reset on SIGINT.
173 #define BC_FLAG_SIGINT (UINTMAX_C(1) << 12)
175 /// The flag for exiting with expressions.
176 #define BC_FLAG_EXPR_EXIT (UINTMAX_C(1) << 13)
178 /// The flag for digit clamping.
179 #define BC_FLAG_DIGIT_CLAMP (UINTMAX_C(1) << 14)
181 /// A convenience macro for getting the TTYIN flag.
182 #define BC_TTYIN (vm->flags & BC_FLAG_TTYIN)
184 /// A convenience macro for getting the TTY flag.
185 #define BC_TTY (vm->flags & BC_FLAG_TTY)
187 /// A convenience macro for getting the SIGINT flag.
188 #define BC_SIGINT (vm->flags & BC_FLAG_SIGINT)
192 /// A convenience macro for getting the POSIX error flag.
193 #define BC_S (vm->flags & BC_FLAG_S)
195 /// A convenience macro for getting the POSIX warning flag.
196 #define BC_W (vm->flags & BC_FLAG_W)
198 /// A convenience macro for getting the math library flag.
199 #define BC_L (vm->flags & BC_FLAG_L)
201 /// A convenience macro for getting the global stacks flag.
202 #define BC_G (vm->flags & BC_FLAG_G)
208 /// A convenience macro for getting the extended register flag.
209 #define DC_X (vm->flags & DC_FLAG_X)
213 /// A convenience macro for getting the interactive flag.
214 #define BC_I (vm->flags & BC_FLAG_I)
216 /// A convenience macro for getting the prompt flag.
217 #define BC_P (vm->flags & BC_FLAG_P)
219 /// A convenience macro for getting the read prompt flag.
220 #define BC_R (vm->flags & BC_FLAG_R)
222 /// A convenience macro for getting the leading zero flag.
223 #define BC_Z (vm->flags & BC_FLAG_Z)
225 /// A convenience macro for getting the expression exit flag.
226 #define BC_EXPR_EXIT (vm->flags & BC_FLAG_EXPR_EXIT)
228 /// A convenience macro for getting the digit clamp flag.
229 #define BC_DIGIT_CLAMP (vm->flags & BC_FLAG_DIGIT_CLAMP)
233 /// A convenience macro for checking if bc is in POSIX mode.
234 #define BC_IS_POSIX (BC_S || BC_W)
238 /// Returns true if bc is running.
239 #define BC_IS_BC (vm->name[0] != 'd')
241 /// Returns true if dc is running.
242 #define BC_IS_DC (vm->name[0] == 'd')
244 /// Returns the correct read prompt.
245 #define BC_VM_READ_PROMPT (BC_IS_BC ? "read> " : "?> ")
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")
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")
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")
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)
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")
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)
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")
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)
274 /// Returns the string for the prompt environment variable.
275 #define BC_VM_PROMPT_STR (BC_IS_BC ? "BC_PROMPT" : "DC_PROMPT")
277 /// Returns the default for the prompt environment variable.
278 #define BC_VM_PROMPT_DEF (BC_IS_BC ? BC_DEFAULT_PROMPT : DC_DEFAULT_PROMPT)
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")
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)
288 /// Returns true if the calculator should run stdin.
289 #define BC_VM_RUN_STDIN(has_file) (BC_IS_BC || !(has_file))
293 /// Returns true if bc is running.
296 /// Returns true if dc is running.
299 /// Returns the correct read prompt.
300 #define BC_VM_READ_PROMPT ("read> ")
302 /// Returns the string for the line length environment variable.
303 #define BC_VM_LINE_LENGTH_STR ("BC_LINE_LENGTH")
305 /// Returns the string for the environment args environment variable.
306 #define BC_VM_ENV_ARGS_STR ("BC_ENV_ARGS")
308 /// Returns the string for the expression exit environment variable.
309 #define BC_VM_EXPR_EXIT_STR ("BC_EXPR_EXIT")
311 /// Returns the default for the expression exit environment variable.
312 #define BC_VM_EXPR_EXIT_DEF (BC_DEFAULT_EXPR_EXIT)
314 /// Returns the string for the digit clamp environment variable.
315 #define BC_VM_DIGIT_CLAMP_STR ("BC_DIGIT_CLAMP")
317 /// Returns the default for the digit clamp environment variable.
318 #define BC_VM_DIGIT_CLAMP_DEF (BC_DEFAULT_DIGIT_CLAMP)
320 /// Returns the string for the TTY mode environment variable.
321 #define BC_VM_TTY_MODE_STR ("BC_TTY_MODE")
323 /// Returns the default for the TTY mode environment variable.
324 #define BC_VM_TTY_MODE_DEF (BC_DEFAULT_TTY_MODE)
326 /// Returns the string for the prompt environment variable.
327 #define BC_VM_PROMPT_STR ("BC_PROMPT")
329 /// Returns the default for the SIGINT reset environment variable.
330 #define BC_VM_PROMPT_DEF (BC_DEFAULT_PROMPT)
332 /// Returns the string for the SIGINT reset environment variable.
333 #define BC_VM_SIGINT_RESET_STR ("BC_SIGINT_RESET")
335 /// Returns the string for the SIGINT reset environment variable.
336 #define BC_VM_SIGINT_RESET_DEF (BC_DEFAULT_SIGINT_RESET)
338 /// Returns true if the calculator should run stdin.
339 #define BC_VM_RUN_STDIN(has_file) (BC_IS_BC)
345 /// A convenience macro for checking if bc is in POSIX mode.
346 #define BC_IS_POSIX (0)
348 /// Returns true if bc is running.
351 /// Returns true if dc is running.
354 /// Returns the correct read prompt.
355 #define BC_VM_READ_PROMPT ("?> ")
357 /// Returns the string for the line length environment variable.
358 #define BC_VM_LINE_LENGTH_STR ("DC_LINE_LENGTH")
360 /// Returns the string for the environment args environment variable.
361 #define BC_VM_ENV_ARGS_STR ("DC_ENV_ARGS")
363 /// Returns the string for the expression exit environment variable.
364 #define BC_VM_EXPR_EXIT_STR ("DC_EXPR_EXIT")
366 /// Returns the default for the expression exit environment variable.
367 #define BC_VM_EXPR_EXIT_DEF (DC_DEFAULT_EXPR_EXIT)
369 /// Returns the string for the digit clamp environment variable.
370 #define BC_VM_DIGIT_CLAMP_STR ("DC_DIGIT_CLAMP")
372 /// Returns the default for the digit clamp environment variable.
373 #define BC_VM_DIGIT_CLAMP_DEF (DC_DEFAULT_DIGIT_CLAMP)
375 /// Returns the string for the TTY mode environment variable.
376 #define BC_VM_TTY_MODE_STR ("DC_TTY_MODE")
378 /// Returns the default for the TTY mode environment variable.
379 #define BC_VM_TTY_MODE_DEF (DC_DEFAULT_TTY_MODE)
381 /// Returns the string for the prompt environment variable.
382 #define BC_VM_PROMPT_STR ("DC_PROMPT")
384 /// Returns the default for the SIGINT reset environment variable.
385 #define BC_VM_PROMPT_DEF (DC_DEFAULT_PROMPT)
387 /// Returns the string for the SIGINT reset environment variable.
388 #define BC_VM_SIGINT_RESET_STR ("DC_SIGINT_RESET")
390 /// Returns the string for the SIGINT reset environment variable.
391 #define BC_VM_SIGINT_RESET_DEF (DC_DEFAULT_SIGINT_RESET)
393 /// Returns true if the calculator should run stdin.
394 #define BC_VM_RUN_STDIN(has_file) (!(has_file))
398 /// A convenience macro for checking if the prompt is enabled.
399 #define BC_PROMPT (BC_P)
401 #else // !BC_ENABLE_LIBRARY
403 #define BC_Z (vm->leading_zeroes)
405 #define BC_DIGIT_CLAMP (vm->digit_clamp)
407 #endif // !BC_ENABLE_LIBRARY
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.
416 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
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.
425 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
427 /// Returns the max obase that is allowed.
428 #define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW))
430 /// Returns the max array size that is allowed.
431 #define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1))
433 /// Returns the max scale that is allowed.
434 #define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
436 /// Returns the max string length that is allowed.
437 #define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
439 /// Returns the max identifier length that is allowed.
440 #define BC_MAX_NAME BC_MAX_STRING
442 /// Returns the max number size that is allowed.
443 #define BC_MAX_NUM BC_MAX_SCALE
445 #if BC_ENABLE_EXTRA_MATH
447 /// Returns the max random integer that can be returned.
448 #define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1))
450 #endif // BC_ENABLE_EXTRA_MATH
452 /// Returns the max exponent that is allowed.
453 #define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX))
455 /// Returns the max number of variables that is allowed.
456 #define BC_MAX_VARS ((ulong) (SIZE_MAX - 1))
458 #if BC_ENABLE_LINE_LIB
460 /// The size of the global buffer.
461 #define BC_VM_BUF_SIZE (1 << 10)
463 /// The amount of the global buffer allocated to stdin.
464 #define BC_VM_STDIN_BUF_SIZE (BC_VM_BUF_SIZE - 1)
466 #else // BC_ENABLE_LINE_LIB
468 /// The size of the global buffer.
469 #define BC_VM_BUF_SIZE (1 << 12)
471 /// The amount of the global buffer allocated to stdout.
472 #define BC_VM_STDOUT_BUF_SIZE (1 << 11)
474 /// The amount of the global buffer allocated to stderr.
475 #define BC_VM_STDERR_BUF_SIZE (1 << 10)
477 /// The amount of the global buffer allocated to stdin.
478 #define BC_VM_STDIN_BUF_SIZE (BC_VM_STDERR_BUF_SIZE - 1)
480 #endif // BC_ENABLE_LINE_LIB
482 /// The max number of temporary BcNums that can be kept.
483 #define BC_VM_MAX_TEMPS (1 << 9)
485 /// The capacity of the one BcNum, which is a constant.
486 #define BC_VM_ONE_CAP (1)
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.
493 #define BC_VM_SAFE_RESULT(r) ((r)->t >= BC_RESULT_TEMP)
495 /// The invalid locale catalog return value.
496 #define BC_VM_INVALID_CATALOG ((nl_catd) -1)
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.
505 #define BC_VM_MUL_OVERFLOW(a, b, r) \
506 ((r) >= SIZE_MAX || ((a) != 0 && (r) / (a) != (b)))
508 /// The global vm struct. This holds all of the global data besides the file
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
516 volatile sig_atomic_t status;
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
523 volatile sig_atomic_t sig_pop;
525 #if !BC_ENABLE_LIBRARY
533 /// A buffer for lines for stdin.
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.
540 /// A parser to parse read expressions.
543 /// A buffer for read expressions.
546 #endif // !BC_ENABLE_LIBRARY
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.
552 /// The number of temps in the temps array.
555 #if BC_ENABLE_LIBRARY
557 /// The vector of contexts for the library.
560 /// The vector for creating strings to pass to the client.
563 #if BC_ENABLE_EXTRA_MATH
568 #endif // BC_ENABLE_EXTRA_MATH
570 /// The current error.
573 /// Whether or not bcl should abort on fatal errors.
576 /// Whether or not to print leading zeros.
579 /// Whether or not to clamp digits that are greater than or equal to the
583 /// The number of "references," or times that the library was initialized.
586 #else // BC_ENABLE_LIBRARY
588 /// A pointer to the filename of the current file. This is not owned by the
592 /// The message printed when SIGINT happens.
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;
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
606 volatile sig_atomic_t sig;
608 /// The length of sigmsg.
611 /// The instruction used for returning from a read() call.
614 /// The flags field used by most macros above.
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
622 /// The length of the line we can print. The user can set this if they wish.
625 /// True if bc should error if expressions are encountered during option
626 /// parsing, false otherwise.
629 /// True if bc should exit if expresions are encountered.
632 /// True if EOF was encountered.
635 /// The mode that the program is in.
640 /// True if keywords should not be redefined. This is only true for the
641 /// builtin math libraries for bc.
646 /// A vector of filenames to process.
649 /// A vector of expressions to process.
652 /// The name of the calculator under use. This is used by BC_IS_BC and
656 /// The help text for the calculator.
659 #if BC_ENABLE_HISTORY
661 /// The history data.
664 #endif // BC_ENABLE_HISTORY
666 /// The function to call to get the next lex token.
669 /// The function to call to parse.
672 /// The function to call to parse expressions.
675 /// The names of the categories of errors.
676 const char* err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED];
678 /// The messages for each error.
679 const char* err_msgs[BC_ERR_NELEMS];
684 #endif // BC_ENABLE_NLS
686 #endif // BC_ENABLE_LIBRARY
688 /// An array of maxes for the globals.
689 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH];
691 /// The last base used to parse.
694 /// The last power of last_base used to parse.
697 /// The last exponent of base that equals last_pow.
700 /// BC_BASE_POW - last_pow.
703 #if !BC_ENABLE_LIBRARY
705 /// A buffer of environment arguments. This is the actual value of the
706 /// environment variable.
707 char* env_args_buffer;
709 /// A vector for environment arguments after parsing.
712 /// A BcNum set to constant 0.
715 #endif // !BC_ENABLE_LIBRARY
717 /// A BcNum set to constant 1.
720 /// A BcNum holding the max number held by a BcBigDig plus 1.
723 /// A BcNum holding the max number held by a BcBigDig times 2 plus 1.
726 /// The BcDig array for max.
727 BcDig max_num[BC_NUM_BIGDIG_LOG10];
729 /// The BcDig array for max2.
730 BcDig max2_num[BC_NUM_BIGDIG_LOG10];
732 // The BcDig array for the one BcNum.
733 BcDig one_num[BC_VM_ONE_CAP];
735 #if !BC_ENABLE_LIBRARY
737 // The BcDig array for the zero BcNum.
738 BcDig zero_num[BC_VM_ONE_CAP];
748 /// The locale catalog.
751 #endif // BC_ENABLE_NLS
753 /// A pointer to the stdin buffer.
756 /// The number of items in the input buffer.
759 /// The slabs vector for constants, strings, function names, and other
760 /// string-like things.
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];
770 #endif // !BC_ENABLE_LIBRARY
772 BcDig* temps_buf[BC_VM_MAX_TEMPS];
776 /// The depth for BC_FUNC_ENTER and BC_FUNC_EXIT.
779 #endif // BC_DEBUG_CODE
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.
788 bc_vm_info(const char* const help);
791 * The entrance point for bc/dc together.
792 * @param argc The count of arguments.
793 * @param argv The argument array.
796 bc_vm_boot(int argc, char* argv[]);
799 * Initializes some of the BcVm global. This is separate to make things easier
800 * on the library code.
806 * Frees the BcVm global.
809 bc_vm_shutdown(void);
812 * Add a temp to the temp array.
813 * @param num The BcDig array to add to the temp array.
816 bc_vm_addTemp(BcDig* num);
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.
823 bc_vm_takeTemp(void);
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.
834 * Frees all temporaries.
837 bc_vm_freeTemps(void);
839 #if !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY
842 * Erases the flush argument if history does not exist because it does not
843 * matter if history does not exist.
845 #define bc_vm_putchar(c, t) bc_vm_putchar_impl(c)
847 #else // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY
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)
852 #endif // !BC_ENABLE_HISTORY || BC_ENABLE_LINE_LIB || BC_ENABLE_LIBRARY
855 * Print to stdout with limited formating.
856 * @param fmt The format string.
859 bc_vm_printf(const char* fmt, ...);
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.
867 bc_vm_putchar(int c, BcFlushType type);
870 * Multiplies @a n and @a size and throws an allocation error if overflow
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.
877 bc_vm_arraySize(size_t n, size_t size);
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.
886 bc_vm_growSize(size_t a, size_t b);
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.
894 bc_vm_malloc(size_t n);
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.
904 bc_vm_realloc(void* ptr, size_t n);
907 * Allocates space for, and duplicates, @a str.
908 * @param str The string to allocate.
909 * @return The allocated string.
912 bc_vm_strdup(const char* str);
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.
920 bc_vm_readLine(bool clear);
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.
928 bc_vm_readBuf(bool clear);
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().
936 bc_pledge(const char* promises, const char* execpromises);
939 * Returns the value of an environment variable.
940 * @param var The environment variable.
941 * @return The value of the environment variable.
944 bc_vm_getenv(const char* var);
947 * Frees an environment variable value.
948 * @param val The value to free.
951 bc_vm_getenvFree(char* val);
956 * Start executing a jump series.
957 * @param f The name of the function that started the jump series.
960 bc_vm_jmp(const char* f);
962 #else // BC_DEBUG_CODE
965 * Start executing a jump series.
970 #endif // BC_DEBUG_CODE
972 #if BC_ENABLE_LIBRARY
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.
981 bc_vm_handleError(BcErr e);
984 * Handle a fatal error.
985 * @param e The error.
988 bc_vm_fatalError(BcErr e);
991 * A function to call at exit.
996 #else // BC_ENABLE_LIBRARY
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.
1004 bc_vm_numDigits(size_t val);
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.
1018 bc_vm_handleError(BcErr e, const char* file, int fline, size_t line, ...);
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.
1030 bc_vm_handleError(BcErr e, size_t line, ...);
1035 * Handle a fatal error.
1036 * @param e The error.
1038 #if !BC_ENABLE_MEMCHECK
1040 #endif // !BC_ENABLE_MEMCHECK
1042 bc_vm_fatalError(BcErr e);
1045 * A function to call at exit.
1046 * @param status The exit status.
1049 bc_vm_atexit(int status);
1050 #endif // BC_ENABLE_LIBRARY
1052 /// A reference to the copyright header.
1053 extern const char bc_copyright[];
1055 /// A reference to the array of default error category names.
1056 extern const char* bc_errs[];
1058 /// A reference to the array of error category indices for each error.
1059 extern const uchar bc_err_ids[];
1061 /// A reference to the array of default error messages.
1062 extern const char* const bc_err_msgs[];
1064 /// A reference to the pledge() promises at start.
1065 extern const char bc_pledge_start[];
1067 #if BC_ENABLE_HISTORY
1069 /// A reference to the end pledge() promises when using history.
1070 extern const char bc_pledge_end_history[];
1072 #endif // BC_ENABLE_HISTORY
1074 /// A reference to the end pledge() promises when *not* using history.
1075 extern const char bc_pledge_end[];
1077 #if !BC_ENABLE_LIBRARY
1079 /// A reference to the global data.
1082 /// The global data.
1083 extern BcVm vm_data;
1085 /// A reference to the global output buffers.
1086 extern char output_bufs[BC_VM_BUF_SIZE];
1088 #endif // !BC_ENABLE_LIBRARY