2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 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.
83 #define BC_ENABLE_NLS (0)
84 #endif // BC_ENABLE_NLS
99 * Generate a string from text.
100 * @parm V The text to generate a string for.
102 #define GEN_STR(V) #V
105 * Help generate a string from text. The preprocessor requires this two-step
107 * @parm V The text to generate a string for.
109 #define GEN_STR2(V) GEN_STR(V)
111 /// The version as a string. VERSION must be defined previously, usually by the
113 #define BC_VERSION GEN_STR2(VERSION)
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)
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)
123 // We only allow an empty executable prefix on Windows.
125 #define BC_EXECPREFIX GEN_STR2(EXECPREFIX)
127 #define BC_EXECPREFIX ""
130 #if !BC_ENABLE_LIBRARY
134 /// The flag for the extended register option.
135 #define DC_FLAG_X (UINTMAX_C(1)<<0)
141 /// The flag for the POSIX warning option.
142 #define BC_FLAG_W (UINTMAX_C(1)<<1)
144 /// The flag for the POSIX error option.
145 #define BC_FLAG_S (UINTMAX_C(1)<<2)
147 /// The flag for the math library option.
148 #define BC_FLAG_L (UINTMAX_C(1)<<3)
150 /// The flag for the global stacks option.
151 #define BC_FLAG_G (UINTMAX_C(1)<<4)
155 /// The flag for quiet, though this one is reversed; the option clears the flag.
156 #define BC_FLAG_Q (UINTMAX_C(1)<<5)
158 /// The flag for interactive.
159 #define BC_FLAG_I (UINTMAX_C(1)<<6)
161 /// The flag for prompt. This is also reversed; the option clears the flag.
162 #define BC_FLAG_P (UINTMAX_C(1)<<7)
164 /// The flag for read prompt. This is also reversed; the option clears the flag.
165 #define BC_FLAG_R (UINTMAX_C(1)<<8)
167 /// The flag for a leading zero.
168 #define BC_FLAG_Z (UINTMAX_C(1)<<9)
170 /// The flag for stdin being a TTY.
171 #define BC_FLAG_TTYIN (UINTMAX_C(1)<<10)
173 /// The flag for TTY mode.
174 #define BC_FLAG_TTY (UINTMAX_C(1)<<11)
176 /// The flag for reset on SIGINT.
177 #define BC_FLAG_SIGINT (UINTMAX_C(1)<<12)
179 /// A convenience macro for getting the TTYIN flag.
180 #define BC_TTYIN (vm.flags & BC_FLAG_TTYIN)
182 /// A convenience macro for getting the TTY flag.
183 #define BC_TTY (vm.flags & BC_FLAG_TTY)
185 /// A convenience macro for getting the SIGINT flag.
186 #define BC_SIGINT (vm.flags & BC_FLAG_SIGINT)
190 /// A convenience macro for getting the POSIX error flag.
191 #define BC_S (vm.flags & BC_FLAG_S)
193 /// A convenience macro for getting the POSIX warning flag.
194 #define BC_W (vm.flags & BC_FLAG_W)
196 /// A convenience macro for getting the math library flag.
197 #define BC_L (vm.flags & BC_FLAG_L)
199 /// A convenience macro for getting the global stacks flag.
200 #define BC_G (vm.flags & BC_FLAG_G)
206 /// A convenience macro for getting the extended register flag.
207 #define DC_X (vm.flags & DC_FLAG_X)
211 /// A convenience macro for getting the interactive flag.
212 #define BC_I (vm.flags & BC_FLAG_I)
214 /// A convenience macro for getting the prompt flag.
215 #define BC_P (vm.flags & BC_FLAG_P)
217 /// A convenience macro for getting the read prompt flag.
218 #define BC_R (vm.flags & BC_FLAG_R)
220 /// A convenience macro for getting the leading zero flag.
221 #define BC_Z (vm.flags & BC_FLAG_Z)
225 /// A convenience macro for checking if bc is in POSIX mode.
226 #define BC_IS_POSIX (BC_S || BC_W)
230 /// Returns true if bc is running.
231 #define BC_IS_BC (vm.name[0] != 'd')
233 /// Returns true if dc is running.
234 #define BC_IS_DC (vm.name[0] == 'd')
238 /// Returns true if bc is running.
241 /// Returns true if dc is running.
248 /// A convenience macro for checking if bc is in POSIX mode.
249 #define BC_IS_POSIX (0)
251 /// Returns true if bc is running.
254 /// Returns true if dc is running.
259 /// A convenience macro for checking if the prompt is enabled.
260 #define BC_PROMPT (BC_P)
262 #else // !BC_ENABLE_LIBRARY
264 #define BC_Z (vm.leading_zeroes)
266 #endif // !BC_ENABLE_LIBRARY
269 * Returns the max of its two arguments. This evaluates arguments twice, so be
270 * careful what args you give it.
271 * @param a The first argument.
272 * @param b The second argument.
273 * @return The max of the two arguments.
275 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
278 * Returns the min of its two arguments. This evaluates arguments twice, so be
279 * careful what args you give it.
280 * @param a The first argument.
281 * @param b The second argument.
282 * @return The min of the two arguments.
284 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
286 /// Returns the max obase that is allowed.
287 #define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW))
289 /// Returns the max array size that is allowed.
290 #define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1))
292 /// Returns the max scale that is allowed.
293 #define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
295 /// Returns the max string length that is allowed.
296 #define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
298 /// Returns the max identifier length that is allowed.
299 #define BC_MAX_NAME BC_MAX_STRING
301 /// Returns the max number size that is allowed.
302 #define BC_MAX_NUM BC_MAX_SCALE
304 #if BC_ENABLE_EXTRA_MATH
306 /// Returns the max random integer that can be returned.
307 #define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1))
309 #endif // BC_ENABLE_EXTRA_MATH
311 /// Returns the max exponent that is allowed.
312 #define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX))
314 /// Returns the max number of variables that is allowed.
315 #define BC_MAX_VARS ((ulong) (SIZE_MAX - 1))
317 /// The size of the global buffer.
318 #define BC_VM_BUF_SIZE (1<<12)
320 /// The amount of the global buffer allocated to stdout.
321 #define BC_VM_STDOUT_BUF_SIZE (1<<11)
323 /// The amount of the global buffer allocated to stderr.
324 #define BC_VM_STDERR_BUF_SIZE (1<<10)
326 /// The amount of the global buffer allocated to stdin.
327 #define BC_VM_STDIN_BUF_SIZE (BC_VM_STDERR_BUF_SIZE - 1)
329 /// The max number of temporary BcNums that can be kept.
330 #define BC_VM_MAX_TEMPS (1 << 9)
332 /// The capacity of the one BcNum, which is a constant.
333 #define BC_VM_ONE_CAP (1)
336 * Returns true if a BcResult is safe for garbage collection.
337 * @param r The BcResult to test.
338 * @return True if @a r is safe to garbage collect.
340 #define BC_VM_SAFE_RESULT(r) ((r)->t >= BC_RESULT_TEMP)
342 /// The invalid locale catalog return value.
343 #define BC_VM_INVALID_CATALOG ((nl_catd) -1)
346 * Returns true if the *unsigned* multiplication overflows.
347 * @param a The first operand.
348 * @param b The second operand.
349 * @param r The product.
350 * @return True if the multiplication of @a a and @a b overflows.
352 #define BC_VM_MUL_OVERFLOW(a, b, r) \
353 ((r) >= SIZE_MAX || ((a) != 0 && (r) / (a) != (b)))
355 /// The global vm struct. This holds all of the global data besides the file
357 typedef struct BcVm {
359 /// The current status. This is volatile sig_atomic_t because it is also
360 /// used in the signal handler. See the development manual
361 /// (manuals/development.md#async-signal-safe-signal-handling) for more
363 volatile sig_atomic_t status;
365 /// Non-zero if a jump series is in progress and items should be popped off
366 /// the jmp_bufs vector. This is volatile sig_atomic_t because it is also
367 /// used in the signal handler. See the development manual
368 /// (manuals/development.md#async-signal-safe-signal-handling) for more
370 volatile sig_atomic_t sig_pop;
372 #if !BC_ENABLE_LIBRARY
380 /// A buffer for lines for stdin.
383 /// A buffer to hold a series of lines from stdin. Sometimes, multiple lines
384 /// are necessary for parsing, such as a comment that spans multiple lines.
387 /// A parser to parse read expressions.
390 /// A buffer for read expressions.
393 #endif // !BC_ENABLE_LIBRARY
395 /// A vector of jmp_bufs for doing a jump series. This allows exception-type
396 /// error handling, while allowing me to do cleanup on the way.
399 /// The number of temps in the temps array.
402 #if BC_ENABLE_LIBRARY
404 /// The vector of contexts for the library.
407 /// The vector for creating strings to pass to the client.
413 /// The current error.
416 /// Whether or not bcl should abort on fatal errors.
419 /// Whether or not to print leading zeros.
422 /// The number of "references," or times that the library was initialized.
425 /// Non-zero if bcl is running. This is volatile sig_atomic_t because it is
426 /// also used in the signal handler. See the development manual
427 /// (manuals/development.md#async-signal-safe-signal-handling) for more
429 volatile sig_atomic_t running;
431 #endif // BC_ENABLE_LIBRARY
433 #if !BC_ENABLE_LIBRARY
435 /// A pointer to the filename of the current file. This is not owned by the
439 /// The message printed when SIGINT happens.
442 #endif // !BC_ENABLE_LIBRARY
444 /// Non-zero when signals are "locked." This is volatile sig_atomic_t
445 /// because it is also used in the signal handler. See the development
446 /// manual (manuals/development.md#async-signal-safe-signal-handling) for
447 /// more information.
448 volatile sig_atomic_t sig_lock;
450 /// Non-zero when a signal has been received, but not acted on. This is
451 /// volatile sig_atomic_t because it is also used in the signal handler. See
452 /// the development manual
453 /// (manuals/development.md#async-signal-safe-signal-handling) for more
455 volatile sig_atomic_t sig;
457 #if !BC_ENABLE_LIBRARY
459 /// The length of sigmsg.
462 /// The instruction used for returning from a read() call.
465 /// The flags field used by most macros above.
468 /// The number of characters printed in the current line. This is used
469 /// because bc has a limit of the number of characters it can print per
473 /// The length of the line we can print. The user can set this if they wish.
476 /// True if bc should error if expressions are encountered during option
477 /// parsing, false otherwise.
480 /// True if bc should exit if expresions are encountered.
483 /// True if EOF was encountered.
486 /// True if bc is currently reading from stdin.
491 /// True if keywords should not be redefined. This is only true for the
492 /// builtin math libraries for bc.
497 #endif // !BC_ENABLE_LIBRARY
499 /// An array of maxes for the globals.
500 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH];
502 #if !BC_ENABLE_LIBRARY
504 /// A vector of filenames to process.
507 /// A vector of expressions to process.
510 /// The name of the calculator under use. This is used by BC_IS_BC and
514 /// The help text for the calculator.
517 #if BC_ENABLE_HISTORY
519 /// The history data.
522 #endif // BC_ENABLE_HISTORY
524 /// The function to call to get the next lex token.
527 /// The function to call to parse.
530 /// The function to call to parse expressions.
533 /// The text to display to label functions in error messages.
534 const char *func_header;
536 /// The names of the categories of errors.
537 const char *err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED];
539 /// The messages for each error.
540 const char *err_msgs[BC_ERR_NELEMS];
545 #endif // !BC_ENABLE_LIBRARY
547 /// The last base used to parse.
550 /// The last power of last_base used to parse.
553 /// The last exponent of base that equals last_pow.
556 /// BC_BASE_POW - last_pow.
559 #if !BC_ENABLE_LIBRARY
561 /// A buffer of environment arguments. This is the actual value of the
562 /// environment variable.
563 char *env_args_buffer;
565 /// A vector for environment arguments after parsing.
568 /// A BcNum set to constant 0.
571 #endif // !BC_ENABLE_LIBRARY
573 /// A BcNum set to constant 1.
576 /// A BcNum holding the max number held by a BcBigDig plus 1.
579 /// A BcNum holding the max number held by a BcBigDig times 2 plus 1.
582 /// The BcDig array for max.
583 BcDig max_num[BC_NUM_BIGDIG_LOG10];
585 /// The BcDig array for max2.
586 BcDig max2_num[BC_NUM_BIGDIG_LOG10];
588 // The BcDig array for the one BcNum.
589 BcDig one_num[BC_VM_ONE_CAP];
591 #if !BC_ENABLE_LIBRARY
593 // The BcDig array for the zero BcNum.
594 BcDig zero_num[BC_VM_ONE_CAP];
604 /// The locale catalog.
607 #endif // BC_ENABLE_NLS
609 /// A pointer to the stdin buffer.
612 /// The number of items in the input buffer.
615 /// The slab for constants in the main function. This is separate for
616 /// garbage collection reasons.
617 BcVec main_const_slab;
619 //// The slab for all other strings for the main function.
622 /// The slab for function names, strings in other functions, and constants
623 /// in other functions.
628 /// An array of booleans for which bc keywords have been redefined if
629 /// BC_REDEFINE_KEYWORDS is non-zero.
630 bool redefined_kws[BC_LEX_NKWS];
633 #endif // !BC_ENABLE_LIBRARY
637 /// The depth for BC_FUNC_ENTER and BC_FUNC_EXIT.
640 #endif // BC_DEBUG_CODE
645 * Print the copyright banner and help if it's non-NULL.
646 * @param help The help message to print if it's non-NULL.
648 void bc_vm_info(const char* const help);
651 * The entrance point for bc/dc together.
652 * @param argc The count of arguments.
653 * @param argv The argument array.
655 void bc_vm_boot(int argc, char *argv[]);
658 * Initializes some of the BcVm global. This is separate to make things easier
659 * on the library code.
661 void bc_vm_init(void);
664 * Frees the BcVm global.
666 void bc_vm_shutdown(void);
669 * Add a temp to the temp array.
670 * @param num The BcDig array to add to the temp array.
672 void bc_vm_addTemp(BcDig *num);
675 * Dish out a temp, or NULL if there are none.
676 * @return A temp, or NULL if none exist.
678 BcDig* bc_vm_takeTemp(void);
681 * Frees all temporaries.
683 void bc_vm_freeTemps(void);
685 #if !BC_ENABLE_HISTORY
688 * Erases the flush argument if history does not exist because it does not
689 * matter if history does not exist.
691 #define bc_vm_putchar(c, t) bc_vm_putchar(c)
693 #endif // !BC_ENABLE_HISTORY
696 * Print to stdout with limited formating.
697 * @param fmt The format string.
699 void bc_vm_printf(const char *fmt, ...);
702 * Puts a char into the stdout buffer.
703 * @param c The character to put on the stdout buffer.
704 * @param type The flush type.
706 void bc_vm_putchar(int c, BcFlushType type);
709 * Multiplies @a n and @a size and throws an allocation error if overflow
711 * @param n The number of elements.
712 * @param size The size of each element.
713 * @return The product of @a n and @a size.
715 size_t bc_vm_arraySize(size_t n, size_t size);
718 * Adds @a a and @a b and throws an error if overflow occurs.
719 * @param a The first operand.
720 * @param b The second operand.
721 * @return The sum of @a a and @a b.
723 size_t bc_vm_growSize(size_t a, size_t b);
726 * Allocate @a n bytes and throw an allocation error if allocation fails.
727 * @param n The bytes to allocate.
728 * @return A pointer to the allocated memory.
730 void* bc_vm_malloc(size_t n);
733 * Reallocate @a ptr to be @a n bytes and throw an allocation error if
734 * reallocation fails.
735 * @param ptr The pointer to a memory allocation to reallocate.
736 * @param n The bytes to allocate.
737 * @return A pointer to the reallocated memory.
739 void* bc_vm_realloc(void *ptr, size_t n);
742 * Allocates space for, and duplicates, @a str.
743 * @param str The string to allocate.
744 * @return The allocated string.
746 char* bc_vm_strdup(const char *str);
749 * Reads a line into BcVm's buffer field.
750 * @param clear True if the buffer should be cleared first, false otherwise.
751 * @return True if a line was read, false otherwise.
753 bool bc_vm_readLine(bool clear);
756 * A convenience and portability function for OpenBSD's pledge().
757 * @param promises The promises to pledge().
758 * @param execpromises The exec promises to pledge().
760 void bc_pledge(const char *promises, const char *execpromises);
763 * Returns the value of an environment variable.
764 * @param var The environment variable.
765 * @return The value of the environment variable.
767 char* bc_vm_getenv(const char* var);
770 * Frees an environment variable value.
771 * @param val The value to free.
773 void bc_vm_getenvFree(char* val);
778 * Start executing a jump series.
779 * @param f The name of the function that started the jump series.
781 void bc_vm_jmp(const char *f);
782 #else // BC_DEBUG_CODE
785 * Start executing a jump series.
787 void bc_vm_jmp(void);
789 #endif // BC_DEBUG_CODE
791 #if BC_ENABLE_LIBRARY
794 * Handle an error. This is the true error handler. It will start a jump series
795 * if an error occurred. POSIX errors will not cause jumps when warnings are on
796 * or no POSIX errors are enabled.
797 * @param e The error.
799 void bc_vm_handleError(BcErr e);
802 * Handle a fatal error.
803 * @param e The error.
805 void bc_vm_fatalError(BcErr e);
808 * A function to call at exit.
810 void bc_vm_atexit(void);
812 #else // BC_ENABLE_LIBRARY
815 * Handle an error. This is the true error handler. It will start a jump series
816 * if an error occurred. POSIX errors will not cause jumps when warnings are on
817 * or no POSIX errors are enabled.
818 * @param e The error.
819 * @param line The source line where the error occurred.
821 void bc_vm_handleError(BcErr e, size_t line, ...);
824 * Handle a fatal error.
825 * @param e The error.
827 #if !BC_ENABLE_MEMCHECK
829 #endif // !BC_ENABLE_MEMCHECK
830 void bc_vm_fatalError(BcErr e);
833 * A function to call at exit.
834 * @param status The exit status.
836 int bc_vm_atexit(int status);
837 #endif // BC_ENABLE_LIBRARY
839 /// A reference to the copyright header.
840 extern const char bc_copyright[];
842 /// A reference to the format string for source code line printing.
843 extern const char* const bc_err_line;
845 /// A reference to the format string for source code function printing.
846 extern const char* const bc_err_func_header;
848 /// A reference to the array of default error category names.
849 extern const char *bc_errs[];
851 /// A reference to the array of error category indices for each error.
852 extern const uchar bc_err_ids[];
854 /// A reference to the array of default error messages.
855 extern const char* const bc_err_msgs[];
857 /// A reference to the pledge() promises at start.
858 extern const char bc_pledge_start[];
860 #if BC_ENABLE_HISTORY
862 /// A reference to the end pledge() promises when using history.
863 extern const char bc_pledge_end_history[];
865 #endif // BC_ENABLE_HISTORY
867 /// A reference to the end pledge() promises when *not* using history.
868 extern const char bc_pledge_end[];
870 /// A reference to the global data.
873 /// A reference to the global output buffers.
874 extern char output_bufs[BC_VM_BUF_SIZE];