2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 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
61 #if !BC_ENABLE_LIBRARY
63 #endif // !BC_ENABLE_LIBRARY
65 #if !BC_ENABLED && !DC_ENABLED
66 #error Must define BC_ENABLED, DC_ENABLED, or both
69 // CHAR_BIT must be at least 6.
71 #error CHAR_BIT must be at least 6.
75 #define BC_ENABLE_NLS (0)
76 #endif // BC_ENABLE_NLS
87 #define GEN_STR2(V) GEN_STR(V)
89 #define BC_VERSION GEN_STR2(VERSION)
90 #define BC_EXECPREFIX GEN_STR2(EXECPREFIX)
91 #define BC_MAINEXEC GEN_STR2(MAINEXEC)
93 // Windows has deprecated isatty().
95 #define isatty _isatty
98 #if !BC_ENABLE_LIBRARY
101 #define DC_FLAG_X (UINTMAX_C(1)<<0)
105 #define BC_FLAG_W (UINTMAX_C(1)<<1)
106 #define BC_FLAG_S (UINTMAX_C(1)<<2)
107 #define BC_FLAG_L (UINTMAX_C(1)<<3)
108 #define BC_FLAG_G (UINTMAX_C(1)<<4)
111 #define BC_FLAG_I (UINTMAX_C(1)<<5)
112 #define BC_FLAG_P (UINTMAX_C(1)<<6)
113 #define BC_FLAG_TTYIN (UINTMAX_C(1)<<7)
114 #define BC_FLAG_TTY (UINTMAX_C(1)<<8)
115 #define BC_TTYIN (vm.flags & BC_FLAG_TTYIN)
116 #define BC_TTY (vm.flags & BC_FLAG_TTY)
120 #define BC_S (vm.flags & BC_FLAG_S)
121 #define BC_W (vm.flags & BC_FLAG_W)
122 #define BC_L (vm.flags & BC_FLAG_L)
123 #define BC_G (vm.flags & BC_FLAG_G)
128 #define DC_X (vm.flags & DC_FLAG_X)
131 #define BC_I (vm.flags & BC_FLAG_I)
132 #define BC_P (vm.flags & BC_FLAG_P)
136 #define BC_IS_POSIX (BC_S || BC_W)
139 #define BC_IS_BC (vm.name[0] != 'd')
140 #define BC_IS_DC (vm.name[0] == 'd')
147 #define BC_IS_POSIX (0)
153 #define BC_USE_PROMPT (!BC_P && BC_TTY && !BC_IS_POSIX)
155 #define BC_USE_PROMPT (!BC_P && BC_TTY)
158 #endif // !BC_ENABLE_LIBRARY
160 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
161 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
163 #define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW))
164 #define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1))
165 #define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
166 #define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
167 #define BC_MAX_NAME BC_MAX_STRING
168 #define BC_MAX_NUM BC_MAX_SCALE
170 #if BC_ENABLE_EXTRA_MATH
171 #define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1))
172 #endif // BC_ENABLE_EXTRA_MATH
174 #define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX))
175 #define BC_MAX_VARS ((ulong) (SIZE_MAX - 1))
178 #define BC_VM_JMP bc_vm_jmp(__func__)
179 #else // BC_DEBUG_CODE
180 #define BC_VM_JMP bc_vm_jmp()
181 #endif // BC_DEBUG_CODE
184 BC_UNLIKELY(vm.status != (sig_atomic_t) BC_STATUS_SUCCESS || vm.sig)
185 #define BC_NO_SIG_EXC \
186 BC_LIKELY(vm.status == (sig_atomic_t) BC_STATUS_SUCCESS && !vm.sig)
189 #define BC_SIG_ASSERT_LOCKED do { assert(vm.sig_lock); } while (0)
190 #define BC_SIG_ASSERT_NOT_LOCKED do { assert(vm.sig_lock == 0); } while (0)
192 #define BC_SIG_ASSERT_LOCKED
193 #define BC_SIG_ASSERT_NOT_LOCKED
196 #define BC_SIG_LOCK \
198 BC_SIG_ASSERT_NOT_LOCKED; \
202 #define BC_SIG_UNLOCK \
204 BC_SIG_ASSERT_LOCKED; \
206 if (BC_SIG_EXC) BC_VM_JMP; \
209 #define BC_SIG_MAYLOCK \
214 #define BC_SIG_MAYUNLOCK \
217 if (BC_SIG_EXC) BC_VM_JMP; \
220 #define BC_SIG_TRYLOCK(v) \
226 #define BC_SIG_TRYUNLOCK(v) \
229 if (!(v) && BC_SIG_EXC) BC_VM_JMP; \
232 #define BC_SETJMP(l) \
236 if (sigsetjmp(sjb, 0)) { \
237 assert(BC_SIG_EXC); \
240 bc_vec_push(&vm.jmp_bufs, &sjb); \
244 #define BC_SETJMP_LOCKED(l) \
247 BC_SIG_ASSERT_LOCKED; \
248 if (sigsetjmp(sjb, 0)) { \
249 assert(BC_SIG_EXC); \
252 bc_vec_push(&vm.jmp_bufs, &sjb); \
255 #define BC_LONGJMP_CONT \
257 BC_SIG_ASSERT_LOCKED; \
258 if (!vm.sig_pop) bc_vec_pop(&vm.jmp_bufs); \
262 #define BC_UNSETJMP \
264 BC_SIG_ASSERT_LOCKED; \
265 bc_vec_pop(&vm.jmp_bufs); \
268 #define BC_LONGJMP_STOP \
274 #define BC_VM_BUF_SIZE (1<<12)
275 #define BC_VM_STDOUT_BUF_SIZE (1<<11)
276 #define BC_VM_STDERR_BUF_SIZE (1<<10)
277 #define BC_VM_STDIN_BUF_SIZE (BC_VM_STDERR_BUF_SIZE - 1)
279 #define BC_VM_SAFE_RESULT(r) ((r)->t >= BC_RESULT_TEMP)
281 #if BC_ENABLE_LIBRARY
282 #define bc_vm_error(e, l, ...) (bc_vm_handleError((e)))
283 #define bc_vm_err(e) (bc_vm_handleError((e)))
284 #define bc_vm_verr(e, ...) (bc_vm_handleError((e)))
285 #else // BC_ENABLE_LIBRARY
286 #define bc_vm_error(e, l, ...) (bc_vm_handleError((e), (l), __VA_ARGS__))
287 #define bc_vm_err(e) (bc_vm_handleError((e), 0))
288 #define bc_vm_verr(e, ...) (bc_vm_handleError((e), 0, __VA_ARGS__))
289 #endif // BC_ENABLE_LIBRARY
291 #define BC_STATUS_IS_ERROR(s) \
292 ((s) >= BC_STATUS_ERROR_MATH && (s) <= BC_STATUS_ERROR_FATAL)
294 #define BC_VM_INVALID_CATALOG ((nl_catd) -1)
297 #define BC_VM_FUNC_ENTER \
299 bc_file_printf(&vm.ferr, "Entering %s\n", __func__); \
300 bc_file_flush(&vm.ferr); \
303 #define BC_VM_FUNC_EXIT \
305 bc_file_printf(&vm.ferr, "Leaving %s\n", __func__); \
306 bc_file_flush(&vm.ferr); \
308 #else // BC_DEBUG_CODE
309 #define BC_VM_FUNC_ENTER
310 #define BC_VM_FUNC_EXIT
311 #endif // BC_DEBUG_CODE
313 typedef struct BcVm {
315 volatile sig_atomic_t status;
316 volatile sig_atomic_t sig_pop;
318 #if !BC_ENABLE_LIBRARY
321 #endif // BC_ENABLE_LIBRARY
327 #if BC_ENABLE_LIBRARY
339 volatile sig_atomic_t running;
340 #endif // BC_ENABLE_LIBRARY
342 #if !BC_ENABLE_LIBRARY
346 #endif // BC_ENABLE_LIBRARY
347 volatile sig_atomic_t sig_lock;
348 volatile sig_atomic_t sig;
349 #if !BC_ENABLE_LIBRARY
360 #endif // BC_ENABLE_LIBRARY
362 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH];
364 #if !BC_ENABLE_LIBRARY
371 #if BC_ENABLE_HISTORY
373 #endif // BC_ENABLE_HISTORY
379 const char *func_header;
381 const char *err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED];
382 const char *err_msgs[BC_ERR_NELEMS];
385 #endif // BC_ENABLE_LIBRARY
392 #if !BC_ENABLE_LIBRARY
393 char *env_args_buffer;
395 #endif // BC_ENABLE_LIBRARY
399 BcDig max_num[BC_NUM_BIGDIG_LOG10];
400 BcDig max2_num[BC_NUM_BIGDIG_LOG10];
402 #if !BC_ENABLE_LIBRARY
408 #endif // BC_ENABLE_NLS
412 #endif // !BC_ENABLE_LIBRARY
416 void bc_vm_info(const char* const help);
417 void bc_vm_boot(int argc, char *argv[], const char *env_len,
418 const char* const env_args);
419 void bc_vm_init(void);
420 void bc_vm_shutdown(void);
421 void bc_vm_freeTemps(void);
423 void bc_vm_printf(const char *fmt, ...);
424 void bc_vm_putchar(int c);
425 size_t bc_vm_arraySize(size_t n, size_t size);
426 size_t bc_vm_growSize(size_t a, size_t b);
427 void* bc_vm_malloc(size_t n);
428 void* bc_vm_realloc(void *ptr, size_t n);
429 char* bc_vm_strdup(const char *str);
432 void bc_vm_jmp(const char *f);
433 #else // BC_DEBUG_CODE
434 void bc_vm_jmp(void);
435 #endif // BC_DEBUG_CODE
437 #if BC_ENABLE_LIBRARY
438 void bc_vm_handleError(BcErr e);
439 #else // BC_ENABLE_LIBRARY
440 void bc_vm_handleError(BcErr e, size_t line, ...);
441 #endif // BC_ENABLE_LIBRARY
443 extern const char bc_copyright[];
444 extern const char* const bc_err_line;
445 extern const char* const bc_err_func_header;
446 extern const char *bc_errs[];
447 extern const uchar bc_err_ids[];
448 extern const char* const bc_err_msgs[];
451 extern char output_bufs[BC_VM_BUF_SIZE];