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.
47 # error NLS is not supported on Windows.
52 #endif // BC_ENABLE_NLS
61 #if !BC_ENABLED && !DC_ENABLED
62 #error Must define BC_ENABLED, DC_ENABLED, or both
65 // CHAR_BIT must be at least 6.
67 #error CHAR_BIT must be at least 6.
71 #define BC_ENABLE_NLS (0)
72 #endif // BC_ENABLE_NLS
83 #define GEN_STR2(V) GEN_STR(V)
85 #define BC_VERSION GEN_STR2(VERSION)
86 #define BC_EXECPREFIX GEN_STR2(EXECPREFIX)
87 #define BC_MAINEXEC GEN_STR2(MAINEXEC)
89 // Windows has deprecated isatty().
91 #define isatty _isatty
95 #define DC_FLAG_X (UINTMAX_C(1)<<0)
99 #define BC_FLAG_W (UINTMAX_C(1)<<1)
100 #define BC_FLAG_S (UINTMAX_C(1)<<2)
101 #define BC_FLAG_L (UINTMAX_C(1)<<3)
102 #define BC_FLAG_G (UINTMAX_C(1)<<4)
105 #define BC_FLAG_Q (UINTMAX_C(1)<<5)
106 #define BC_FLAG_I (UINTMAX_C(1)<<6)
107 #define BC_FLAG_P (UINTMAX_C(1)<<7)
108 #define BC_FLAG_TTYIN (UINTMAX_C(1)<<8)
109 #define BC_FLAG_TTY (UINTMAX_C(1)<<9)
110 #define BC_TTYIN (vm.flags & BC_FLAG_TTYIN)
111 #define BC_TTY (vm.flags & BC_FLAG_TTY)
115 #define BC_S (vm.flags & BC_FLAG_S)
116 #define BC_W (vm.flags & BC_FLAG_W)
117 #define BC_L (vm.flags & BC_FLAG_L)
118 #define BC_G (vm.flags & BC_FLAG_G)
123 #define DC_X (vm.flags & DC_FLAG_X)
126 #define BC_I (vm.flags & BC_FLAG_I)
127 #define BC_P (vm.flags & BC_FLAG_P)
131 #define BC_IS_POSIX (BC_S || BC_W)
134 #define BC_IS_BC (vm.name[0] != 'd')
135 #define BC_IS_DC (vm.name[0] == 'd')
142 #define BC_IS_POSIX (0)
148 #define BC_USE_PROMPT (!BC_P && BC_TTY && !BC_IS_POSIX)
150 #define BC_USE_PROMPT (!BC_P && BC_TTY)
153 #define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
154 #define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
156 #define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW))
157 #define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1))
158 #define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
159 #define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
160 #define BC_MAX_NAME BC_MAX_STRING
161 #define BC_MAX_NUM BC_MAX_SCALE
163 #if BC_ENABLE_EXTRA_MATH
164 #define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1))
165 #endif // BC_ENABLE_EXTRA_MATH
167 #define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX))
168 #define BC_MAX_VARS ((ulong) (SIZE_MAX - 1))
171 #define BC_VM_JMP bc_vm_jmp(__func__)
172 #else // BC_DEBUG_CODE
173 #define BC_VM_JMP bc_vm_jmp()
174 #endif // BC_DEBUG_CODE
177 BC_UNLIKELY(vm.status != (sig_atomic_t) BC_STATUS_SUCCESS || vm.sig)
178 #define BC_NO_SIG_EXC \
179 BC_LIKELY(vm.status == (sig_atomic_t) BC_STATUS_SUCCESS && !vm.sig)
182 #define BC_SIG_ASSERT_LOCKED do { assert(vm.sig_lock); } while (0)
183 #define BC_SIG_ASSERT_NOT_LOCKED do { assert(vm.sig_lock == 0); } while (0)
185 #define BC_SIG_ASSERT_LOCKED
186 #define BC_SIG_ASSERT_NOT_LOCKED
189 #define BC_SIG_LOCK \
191 BC_SIG_ASSERT_NOT_LOCKED; \
195 #define BC_SIG_UNLOCK \
197 BC_SIG_ASSERT_LOCKED; \
199 if (BC_SIG_EXC) BC_VM_JMP; \
202 #define BC_SIG_MAYLOCK \
207 #define BC_SIG_MAYUNLOCK \
210 if (BC_SIG_EXC) BC_VM_JMP; \
213 #define BC_SIG_TRYLOCK(v) \
219 #define BC_SIG_TRYUNLOCK(v) \
222 if (!(v) && BC_SIG_EXC) BC_VM_JMP; \
225 #define BC_SETJMP(l) \
229 if (sigsetjmp(sjb, 0)) { \
230 assert(BC_SIG_EXC); \
233 bc_vec_push(&vm.jmp_bufs, &sjb); \
237 #define BC_SETJMP_LOCKED(l) \
240 BC_SIG_ASSERT_LOCKED; \
241 if (sigsetjmp(sjb, 0)) { \
242 assert(BC_SIG_EXC); \
245 bc_vec_push(&vm.jmp_bufs, &sjb); \
248 #define BC_LONGJMP_CONT \
250 BC_SIG_ASSERT_LOCKED; \
251 if (!vm.sig_pop) bc_vec_pop(&vm.jmp_bufs); \
255 #define BC_UNSETJMP \
257 BC_SIG_ASSERT_LOCKED; \
258 bc_vec_pop(&vm.jmp_bufs); \
261 #define BC_LONGJMP_STOP \
267 #define BC_VM_BUF_SIZE (1<<12)
268 #define BC_VM_STDOUT_BUF_SIZE (1<<11)
269 #define BC_VM_STDERR_BUF_SIZE (1<<10)
270 #define BC_VM_STDIN_BUF_SIZE (BC_VM_STDERR_BUF_SIZE - 1)
272 #define BC_VM_SAFE_RESULT(r) ((r)->t >= BC_RESULT_TEMP)
274 #define bc_vm_err(e) (bc_vm_error((e), 0))
275 #define bc_vm_verr(e, ...) (bc_vm_error((e), 0, __VA_ARGS__))
277 #define BC_STATUS_IS_ERROR(s) \
278 ((s) >= BC_STATUS_ERROR_MATH && (s) <= BC_STATUS_ERROR_FATAL)
280 #define BC_VM_INVALID_CATALOG ((nl_catd) -1)
282 // dc does not use is_stdin.
284 #define bc_vm_process(text, is_stdin) bc_vm_process(text)
288 typedef struct BcVm {
290 volatile sig_atomic_t status;
291 volatile sig_atomic_t sig_pop;
303 volatile sig_atomic_t sig_lock;
304 volatile sig_atomic_t sig;
315 BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH];
323 #if BC_ENABLE_HISTORY
325 #endif // BC_ENABLE_HISTORY
331 const char *func_header;
333 const char *err_ids[BC_ERR_IDX_NELEMS + BC_ENABLED];
334 const char *err_msgs[BC_ERROR_NELEMS];
343 char *env_args_buffer;
347 BcDig max_num[BC_NUM_BIGDIG_LOG10];
354 #endif // BC_ENABLE_NLS
361 void bc_vm_info(const char* const help);
362 void bc_vm_boot(int argc, char *argv[], const char *env_len,
363 const char* const env_args, const char* env_exp_quit);
364 void bc_vm_shutdown(void);
366 void bc_vm_printf(const char *fmt, ...);
367 void bc_vm_putchar(int c);
368 size_t bc_vm_arraySize(size_t n, size_t size);
369 size_t bc_vm_growSize(size_t a, size_t b);
370 void* bc_vm_malloc(size_t n);
371 void* bc_vm_realloc(void *ptr, size_t n);
372 char* bc_vm_strdup(const char *str);
375 void bc_vm_jmp(const char *f);
376 #else // BC_DEBUG_CODE
377 void bc_vm_jmp(void);
378 #endif // BC_DEBUG_CODE
380 void bc_vm_error(BcError e, size_t line, ...);
382 extern const char bc_copyright[];
383 extern const char* const bc_err_line;
384 extern const char* const bc_err_func_header;
385 extern const char *bc_errs[];
386 extern const uchar bc_err_ids[];
387 extern const char* const bc_err_msgs[];
390 extern char output_bufs[BC_VM_BUF_SIZE];