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 * Code common to all of bc and dc.
33 *
34 */
35
36 #include <assert.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <string.h>
41
42 #include <signal.h>
43
44 #include <setjmp.h>
45
46 #ifndef _WIN32
47
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <unistd.h>
51
52 #else // _WIN32
53
54 #define WIN32_LEAN_AND_MEAN
55 #include <windows.h>
56 #include <io.h>
57
58 #endif // _WIN32
59
60 #include <status.h>
61 #include <vector.h>
62 #include <args.h>
63 #include <vm.h>
64 #include <read.h>
65 #include <bc.h>
66
67 // The actual globals.
68 static BcDig* temps_buf[BC_VM_MAX_TEMPS];
69 char output_bufs[BC_VM_BUF_SIZE];
70 BcVm vm;
71
72 #if BC_DEBUG_CODE
bc_vm_jmp(const char * f)73 BC_NORETURN void bc_vm_jmp(const char* f) {
74 #else // BC_DEBUG_CODE
75 BC_NORETURN void bc_vm_jmp(void) {
76 #endif
77
78 assert(BC_SIG_EXC);
79
80 BC_SIG_MAYLOCK;
81
82 #if BC_DEBUG_CODE
83 bc_file_puts(&vm.ferr, bc_flush_none, "Longjmp: ");
84 bc_file_puts(&vm.ferr, bc_flush_none, f);
85 bc_file_putchar(&vm.ferr, bc_flush_none, '\n');
86 bc_file_flush(&vm.ferr, bc_flush_none);
87 #endif // BC_DEBUG_CODE
88
89 #ifndef NDEBUG
90 assert(vm.jmp_bufs.len - (size_t) vm.sig_pop);
91 #endif // NDEBUG
92
93 if (vm.jmp_bufs.len == 0) abort();
94 if (vm.sig_pop) bc_vec_pop(&vm.jmp_bufs);
95 else vm.sig_pop = 1;
96
97 siglongjmp(*((sigjmp_buf*) bc_vec_top(&vm.jmp_bufs)), 1);
98 }
99
100 #if !BC_ENABLE_LIBRARY
101
102 /**
103 * Handles signals. This is the signal handler.
104 * @param sig The signal to handle.
105 */
106 static void bc_vm_sig(int sig) {
107
108 // There is already a signal in flight.
109 if (vm.status == (sig_atomic_t) BC_STATUS_QUIT || vm.sig) {
110 if (!BC_I || sig != SIGINT) vm.status = BC_STATUS_QUIT;
111 return;
112 }
113
114 // Only reset under these conditions; otherwise, quit.
115 if (sig == SIGINT && BC_SIGINT && BC_I) {
116
117 int err = errno;
118
119 // Write the message.
120 if (write(STDOUT_FILENO, vm.sigmsg, vm.siglen) != (ssize_t) vm.siglen)
121 vm.status = BC_STATUS_ERROR_FATAL;
122 else vm.sig = 1;
123
124 errno = err;
125 }
126 else vm.status = BC_STATUS_QUIT;
127
128 assert(vm.jmp_bufs.len);
129
130 // Only jump if signals are not locked. The jump will happen by whoever
131 // unlocks signals.
132 if (!vm.sig_lock) BC_JMP;
133 }
134
135 /**
136 * Sets up signal handling.
137 */
138 static void bc_vm_sigaction(void) {
139 #ifndef _WIN32
140
141 struct sigaction sa;
142
143 sigemptyset(&sa.sa_mask);
144 sa.sa_handler = bc_vm_sig;
145 sa.sa_flags = SA_NODEFER;
146
147 sigaction(SIGTERM, &sa, NULL);
148 sigaction(SIGQUIT, &sa, NULL);
149 sigaction(SIGINT, &sa, NULL);
150
151 #if BC_ENABLE_HISTORY
152 if (BC_TTY) sigaction(SIGHUP, &sa, NULL);
153 #endif // BC_ENABLE_HISTORY
154
155 #else // _WIN32
156
157 signal(SIGTERM, bc_vm_sig);
158 signal(SIGINT, bc_vm_sig);
159
160 #endif // _WIN32
161 }
162
163 void bc_vm_info(const char* const help) {
164
165 BC_SIG_ASSERT_LOCKED;
166
167 // Print the banner.
168 bc_file_puts(&vm.fout, bc_flush_none, vm.name);
169 bc_file_putchar(&vm.fout, bc_flush_none, ' ');
170 bc_file_puts(&vm.fout, bc_flush_none, BC_VERSION);
171 bc_file_putchar(&vm.fout, bc_flush_none, '\n');
172 bc_file_puts(&vm.fout, bc_flush_none, bc_copyright);
173
174 // Print the help.
175 if (help) {
176
177 bc_file_putchar(&vm.fout, bc_flush_none, '\n');
178
179 #if BC_ENABLED
180 if (BC_IS_BC) {
181
182 const char* const banner = BC_DEFAULT_BANNER ? "to" : "to not";
183 const char* const sigint = BC_DEFAULT_SIGINT_RESET ? "to reset" :
184 "to exit";
185 const char* const tty = BC_DEFAULT_TTY_MODE ? "enabled" :
186 "disabled";
187 const char* const prompt = BC_DEFAULT_PROMPT ? "enabled" :
188 "disabled";
189
190 bc_file_printf(&vm.fout, help, vm.name, vm.name, BC_VERSION,
191 BC_BUILD_TYPE, banner, sigint, tty, prompt);
192 }
193 #endif // BC_ENABLED
194
195 #if DC_ENABLED
196 if (BC_IS_DC)
197 {
198 const char* const sigint = DC_DEFAULT_SIGINT_RESET ? "to reset" :
199 "to exit";
200 const char* const tty = DC_DEFAULT_TTY_MODE ? "enabled" :
201 "disabled";
202 const char* const prompt = DC_DEFAULT_PROMPT ? "enabled" :
203 "disabled";
204
205 bc_file_printf(&vm.fout, help, vm.name, vm.name, BC_VERSION,
206 BC_BUILD_TYPE, sigint, tty, prompt);
207 }
208 #endif // DC_ENABLED
209 }
210
211 // Flush.
212 bc_file_flush(&vm.fout, bc_flush_none);
213 }
214 #endif // !BC_ENABLE_LIBRARY
215
216 #if !BC_ENABLE_LIBRARY && !BC_ENABLE_MEMCHECK
217 BC_NORETURN
218 #endif // !BC_ENABLE_LIBRARY && !BC_ENABLE_MEMCHECK
219 void bc_vm_fatalError(BcErr e) {
220 bc_err(e);
221 #if !BC_ENABLE_LIBRARY && !BC_ENABLE_MEMCHECK
222 BC_UNREACHABLE
223 abort();
224 #endif // !BC_ENABLE_LIBRARY && !BC_ENABLE_MEMCHECK
225 }
226
227 #if BC_ENABLE_LIBRARY
228 void bc_vm_handleError(BcErr e) {
229
230 assert(e < BC_ERR_NELEMS);
231 assert(!vm.sig_pop);
232
233 BC_SIG_LOCK;
234
235 // If we have a normal error...
236 if (e <= BC_ERR_MATH_DIVIDE_BY_ZERO) {
237
238 // Set the error.
239 vm.err = (BclError) (e - BC_ERR_MATH_NEGATIVE +
240 BCL_ERROR_MATH_NEGATIVE);
241 }
242 // Abort if we should.
243 else if (vm.abrt) abort();
244 else if (e == BC_ERR_FATAL_ALLOC_ERR) vm.err = BCL_ERROR_FATAL_ALLOC_ERR;
245 else vm.err = BCL_ERROR_FATAL_UNKNOWN_ERR;
246
247 BC_JMP;
248 }
249 #else // BC_ENABLE_LIBRARY
250 void bc_vm_handleError(BcErr e, size_t line, ...) {
251
252 BcStatus s;
253 va_list args;
254 uchar id = bc_err_ids[e];
255 const char* err_type = vm.err_ids[id];
256 sig_atomic_t lock;
257
258 assert(e < BC_ERR_NELEMS);
259 assert(!vm.sig_pop);
260
261 #if BC_ENABLED
262 // Figure out if the POSIX error should be an error, a warning, or nothing.
263 if (!BC_S && e >= BC_ERR_POSIX_START) {
264 if (BC_W) {
265 // Make sure to not return an error.
266 id = UCHAR_MAX;
267 err_type = vm.err_ids[BC_ERR_IDX_WARN];
268 }
269 else return;
270 }
271 #endif // BC_ENABLED
272
273 BC_SIG_TRYLOCK(lock);
274
275 // Make sure all of stdout is written first.
276 s = bc_file_flushErr(&vm.fout, bc_flush_err);
277
278 // Just jump out if the flush failed; there's nothing we can do.
279 if (BC_ERR(s == BC_STATUS_ERROR_FATAL)) {
280 vm.status = (sig_atomic_t) s;
281 BC_JMP;
282 }
283
284 // Print the error message.
285 va_start(args, line);
286 bc_file_putchar(&vm.ferr, bc_flush_none, '\n');
287 bc_file_puts(&vm.ferr, bc_flush_none, err_type);
288 bc_file_putchar(&vm.ferr, bc_flush_none, ' ');
289 bc_file_vprintf(&vm.ferr, vm.err_msgs[e], args);
290 va_end(args);
291
292 // Print the extra information if we have it.
293 if (BC_NO_ERR(vm.file != NULL)) {
294
295 // This is the condition for parsing vs runtime.
296 // If line is not 0, it is parsing.
297 if (line) {
298 bc_file_puts(&vm.ferr, bc_flush_none, "\n ");
299 bc_file_puts(&vm.ferr, bc_flush_none, vm.file);
300 bc_file_printf(&vm.ferr, bc_err_line, line);
301 }
302 else {
303
304 BcInstPtr *ip = bc_vec_item_rev(&vm.prog.stack, 0);
305 BcFunc *f = bc_vec_item(&vm.prog.fns, ip->func);
306
307 bc_file_puts(&vm.ferr, bc_flush_none, "\n ");
308 bc_file_puts(&vm.ferr, bc_flush_none, vm.func_header);
309 bc_file_putchar(&vm.ferr, bc_flush_none, ' ');
310 bc_file_puts(&vm.ferr, bc_flush_none, f->name);
311
312 #if BC_ENABLED
313 if (BC_IS_BC && ip->func != BC_PROG_MAIN &&
314 ip->func != BC_PROG_READ)
315 {
316 bc_file_puts(&vm.ferr, bc_flush_none, "()");
317 }
318 #endif // BC_ENABLED
319 }
320 }
321
322 bc_file_puts(&vm.ferr, bc_flush_none, "\n\n");
323
324 s = bc_file_flushErr(&vm.ferr, bc_flush_err);
325
326 #if !BC_ENABLE_MEMCHECK
327 // Because this function is called by a BC_NORETURN function when fatal
328 // errors happen, we need to make sure to exit on fatal errors. This will
329 // be faster anyway. This function *cannot jump when a fatal error occurs!*
330 if (BC_ERR(id == BC_ERR_IDX_FATAL || s == BC_STATUS_ERROR_FATAL))
331 exit(bc_vm_atexit((int) BC_STATUS_ERROR_FATAL));
332 #else // !BC_ENABLE_MEMCHECK
333 if (BC_ERR(s == BC_STATUS_ERROR_FATAL)) vm.status = (sig_atomic_t) s;
334 else
335 #endif // !BC_ENABLE_MEMCHECK
336 {
337 vm.status = (sig_atomic_t) (uchar) (id + 1);
338 }
339
340 // Only jump if there is an error.
341 if (BC_ERR(vm.status)) BC_JMP;
342
343 BC_SIG_TRYUNLOCK(lock);
344 }
345
346 char* bc_vm_getenv(const char* var) {
347
348 char* ret;
349
350 #ifndef _WIN32
351 ret = getenv(var);
352 #else // _WIN32
353 _dupenv_s(&ret, NULL, var);
354 #endif // _WIN32
355
356 return ret;
357 }
358
359 void bc_vm_getenvFree(char* val) {
360 BC_UNUSED(val);
361 #ifdef _WIN32
362 free(val);
363 #endif // _WIN32
364 }
365
366 /**
367 * Sets a flag from an environment variable and the default.
368 * @param var The environment variable.
369 * @param def The default.
370 * @param flag The flag to set.
371 */
372 static void bc_vm_setenvFlag(const char* const var, int def, uint16_t flag) {
373
374 // Get the value.
375 char* val = bc_vm_getenv(var);
376
377 // If there is no value...
378 if (val == NULL) {
379
380 // Set the default.
381 if (def) vm.flags |= flag;
382 else vm.flags &= ~(flag);
383 }
384 // Parse the value.
385 else if (strtoul(val, NULL, 0)) vm.flags |= flag;
386 else vm.flags &= ~(flag);
387
388 bc_vm_getenvFree(val);
389 }
390
391 /**
392 * Parses the arguments in {B,D]C_ENV_ARGS.
393 * @param env_args_name The environment variable to use.
394 */
395 static void bc_vm_envArgs(const char* const env_args_name) {
396
397 char *env_args = bc_vm_getenv(env_args_name), *buf, *start;
398 char instr = '\0';
399
400 BC_SIG_ASSERT_LOCKED;
401
402 if (env_args == NULL) return;
403
404 // Windows already allocates, so we don't need to.
405 #ifndef _WIN32
406 start = buf = vm.env_args_buffer = bc_vm_strdup(env_args);
407 #else // _WIN32
408 start = buf = vm.env_args_buffer = env_args;
409 #endif // _WIN32
410
411 assert(buf != NULL);
412
413 // Create two buffers for parsing. These need to stay throughout the entire
414 // execution of bc, unfortunately, because of filenames that might be in
415 // there.
416 bc_vec_init(&vm.env_args, sizeof(char*), BC_DTOR_NONE);
417 bc_vec_push(&vm.env_args, &env_args_name);
418
419 // While we haven't reached the end of the args...
420 while (*buf) {
421
422 // If we don't have whitespace...
423 if (!isspace(*buf)) {
424
425 // If we have the start of a string...
426 if (*buf == '"' || *buf == '\'') {
427
428 // Set stuff appropriately.
429 instr = *buf;
430 buf += 1;
431
432 // Check for the empty string.
433 if (*buf == instr) {
434 instr = '\0';
435 buf += 1;
436 continue;
437 }
438 }
439
440 // Push the pointer to the args buffer.
441 bc_vec_push(&vm.env_args, &buf);
442
443 // Parse the string.
444 while (*buf && ((!instr && !isspace(*buf)) ||
445 (instr && *buf != instr)))
446 {
447 buf += 1;
448 }
449
450 // If we did find the end of the string...
451 if (*buf) {
452
453 if (instr) instr = '\0';
454
455 // Reset stuff.
456 *buf = '\0';
457 buf += 1;
458 start = buf;
459 }
460 else if (instr) bc_error(BC_ERR_FATAL_OPTION, 0, start);
461 }
462 // If we have whitespace, eat it.
463 else buf += 1;
464 }
465
466 // Make sure to push a NULL pointer at the end.
467 buf = NULL;
468 bc_vec_push(&vm.env_args, &buf);
469
470 // Parse the arguments.
471 bc_args((int) vm.env_args.len - 1, bc_vec_item(&vm.env_args, 0), false);
472 }
473
474 /**
475 * Gets the {B,D}C_LINE_LENGTH.
476 * @param var The environment variable to pull it from.
477 * @return The line length.
478 */
479 static size_t bc_vm_envLen(const char *var) {
480
481 char *lenv = bc_vm_getenv(var);
482 size_t i, len = BC_NUM_PRINT_WIDTH;
483 int num;
484
485 // Return the default with none.
486 if (lenv == NULL) return len;
487
488 len = strlen(lenv);
489
490 // Figure out if it's a number.
491 for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]);
492
493 // If it is a number...
494 if (num) {
495
496 // Parse it and clamp it if needed.
497 len = (size_t) atoi(lenv) - 1;
498 if (len < 2 || len >= UINT16_MAX) len = BC_NUM_PRINT_WIDTH;
499 }
500 // Set the default.
501 else len = BC_NUM_PRINT_WIDTH;
502
503 bc_vm_getenvFree(lenv);
504
505 return len;
506 }
507 #endif // BC_ENABLE_LIBRARY
508
509 void bc_vm_shutdown(void) {
510
511 BC_SIG_ASSERT_LOCKED;
512
513 #if BC_ENABLE_NLS
514 if (vm.catalog != BC_VM_INVALID_CATALOG) catclose(vm.catalog);
515 #endif // BC_ENABLE_NLS
516
517 #if BC_ENABLE_HISTORY
518 // This must always run to ensure that the terminal is back to normal, i.e.,
519 // has raw mode disabled.
520 if (BC_TTY) bc_history_free(&vm.history);
521 #endif // BC_ENABLE_HISTORY
522
523 #ifndef NDEBUG
524 #if !BC_ENABLE_LIBRARY
525 bc_vec_free(&vm.env_args);
526 free(vm.env_args_buffer);
527 bc_vec_free(&vm.files);
528 bc_vec_free(&vm.exprs);
529
530 if (BC_PARSE_IS_INITED(&vm.read_prs, &vm.prog)) {
531 bc_vec_free(&vm.read_buf);
532 bc_parse_free(&vm.read_prs);
533 }
534
535 bc_parse_free(&vm.prs);
536 bc_program_free(&vm.prog);
537
538 bc_slabvec_free(&vm.other_slabs);
539 bc_slabvec_free(&vm.main_slabs);
540 bc_slabvec_free(&vm.main_const_slab);
541 #endif // !BC_ENABLE_LIBRARY
542
543 bc_vm_freeTemps();
544 #endif // NDEBUG
545
546 #if !BC_ENABLE_LIBRARY
547 // We always want to flush.
548 bc_file_free(&vm.fout);
549 bc_file_free(&vm.ferr);
550 #endif // !BC_ENABLE_LIBRARY
551 }
552
553 void bc_vm_addTemp(BcDig *num) {
554
555 // If we don't have room, just free.
556 if (vm.temps_len == BC_VM_MAX_TEMPS) free(num);
557 else {
558
559 // Add to the buffer and length.
560 temps_buf[vm.temps_len] = num;
561 vm.temps_len += 1;
562 }
563 }
564
565 BcDig* bc_vm_takeTemp(void) {
566 if (!vm.temps_len) return NULL;
567 vm.temps_len -= 1;
568 return temps_buf[vm.temps_len];
569 }
570
571 void bc_vm_freeTemps(void) {
572
573 size_t i;
574
575 BC_SIG_ASSERT_LOCKED;
576
577 if (!vm.temps_len) return;
578
579 // Free them all...
580 for (i = 0; i < vm.temps_len; ++i) free(temps_buf[i]);
581
582 vm.temps_len = 0;
583 }
584
585 inline size_t bc_vm_arraySize(size_t n, size_t size) {
586 size_t res = n * size;
587 if (BC_ERR(BC_VM_MUL_OVERFLOW(n, size, res)))
588 bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
589 return res;
590 }
591
592 inline size_t bc_vm_growSize(size_t a, size_t b) {
593 size_t res = a + b;
594 if (BC_ERR(res >= SIZE_MAX || res < a))
595 bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
596 return res;
597 }
598
599 void* bc_vm_malloc(size_t n) {
600
601 void* ptr;
602
603 BC_SIG_ASSERT_LOCKED;
604
605 ptr = malloc(n);
606
607 if (BC_ERR(ptr == NULL)) {
608
609 bc_vm_freeTemps();
610
611 ptr = malloc(n);
612
613 if (BC_ERR(ptr == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
614 }
615
616 return ptr;
617 }
618
619 void* bc_vm_realloc(void *ptr, size_t n) {
620
621 void* temp;
622
623 BC_SIG_ASSERT_LOCKED;
624
625 temp = realloc(ptr, n);
626
627 if (BC_ERR(temp == NULL)) {
628
629 bc_vm_freeTemps();
630
631 temp = realloc(ptr, n);
632
633 if (BC_ERR(temp == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
634 }
635
636 return temp;
637 }
638
639 char* bc_vm_strdup(const char *str) {
640
641 char *s;
642
643 BC_SIG_ASSERT_LOCKED;
644
645 s = strdup(str);
646
647 if (BC_ERR(s == NULL)) {
648
649 bc_vm_freeTemps();
650
651 s = strdup(str);
652
653 if (BC_ERR(s == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
654 }
655
656 return s;
657 }
658
659 #if !BC_ENABLE_LIBRARY
660 void bc_vm_printf(const char *fmt, ...) {
661
662 va_list args;
663
664 BC_SIG_LOCK;
665
666 va_start(args, fmt);
667 bc_file_vprintf(&vm.fout, fmt, args);
668 va_end(args);
669
670 vm.nchars = 0;
671
672 BC_SIG_UNLOCK;
673 }
674 #endif // !BC_ENABLE_LIBRARY
675
676 void bc_vm_putchar(int c, BcFlushType type) {
677 #if BC_ENABLE_LIBRARY
678 bc_vec_pushByte(&vm.out, (uchar) c);
679 #else // BC_ENABLE_LIBRARY
680 bc_file_putchar(&vm.fout, type, (uchar) c);
681 vm.nchars = (c == '\n' ? 0 : vm.nchars + 1);
682 #endif // BC_ENABLE_LIBRARY
683 }
684
685 #if !BC_ENABLE_LIBRARY
686
687 #ifdef __OpenBSD__
688
689 /**
690 * Aborts with a message. This should never be called because I have carefully
691 * made sure that the calls to pledge() and unveil() are correct, but it's here
692 * just in case.
693 * @param msg The message to print.
694 */
695 BC_NORETURN static void bc_abortm(const char* msg) {
696 bc_file_puts(&vm.ferr, bc_flush_none, msg);
697 bc_file_puts(&vm.ferr, bc_flush_none, "; this is a bug");
698 bc_file_flush(&vm.ferr, bc_flush_none);
699 abort();
700 }
701
702 void bc_pledge(const char *promises, const char* execpromises) {
703 int r = pledge(promises, execpromises);
704 if (r) bc_abortm("pledge() failed");
705 }
706
707 #if BC_ENABLE_EXTRA_MATH
708
709 /**
710 * A convenience and portability function for OpenBSD's unveil().
711 * @param path The path.
712 * @param permissions The permissions for the path.
713 */
714 static void bc_unveil(const char *path, const char *permissions) {
715 int r = unveil(path, permissions);
716 if (r) bc_abortm("unveil() failed");
717 }
718 #endif // BC_ENABLE_EXTRA_MATH
719
720 #else // __OpenBSD__
721
722 void bc_pledge(const char *promises, const char *execpromises) {
723 BC_UNUSED(promises);
724 BC_UNUSED(execpromises);
725 }
726
727 #if BC_ENABLE_EXTRA_MATH
728 static void bc_unveil(const char *path, const char *permissions) {
729 BC_UNUSED(path);
730 BC_UNUSED(permissions);
731 }
732 #endif // BC_ENABLE_EXTRA_MATH
733
734 #endif // __OpenBSD__
735
736 /**
737 * Cleans unneeded variables, arrays, functions, strings, and constants when
738 * done executing a line of stdin. This is to prevent memory usage growing
739 * without bound. This is an idea from busybox.
740 */
741 static void bc_vm_clean(void) {
742
743 BcVec *fns = &vm.prog.fns;
744 BcFunc *f = bc_vec_item(fns, BC_PROG_MAIN);
745 BcInstPtr *ip = bc_vec_item(&vm.prog.stack, 0);
746 bool good = ((vm.status && vm.status != BC_STATUS_QUIT) || vm.sig);
747
748 // If all is good, go ahead and reset.
749 if (good) bc_program_reset(&vm.prog);
750
751 #if BC_ENABLED
752 // bc has this extra condition. If it not satisfied, it is in the middle of
753 // a parse.
754 if (good && BC_IS_BC) good = !BC_PARSE_NO_EXEC(&vm.prs);
755 #endif // BC_ENABLED
756
757 #if DC_ENABLED
758 // For dc, it is safe only when all of the results on the results stack are
759 // safe, which means that they are temporaries or other things that don't
760 // need strings or constants.
761 if (BC_IS_DC) {
762
763 size_t i;
764
765 good = true;
766
767 for (i = 0; good && i < vm.prog.results.len; ++i) {
768 BcResult *r = (BcResult*) bc_vec_item(&vm.prog.results, i);
769 good = BC_VM_SAFE_RESULT(r);
770 }
771 }
772 #endif // DC_ENABLED
773
774 // If this condition is true, we can get rid of strings,
775 // constants, and code.
776 if (good && vm.prog.stack.len == 1 && ip->idx == f->code.len) {
777
778 #if BC_ENABLED
779 if (BC_IS_BC) {
780
781 bc_vec_popAll(&f->labels);
782 bc_vec_popAll(&f->strs);
783 bc_vec_popAll(&f->consts);
784
785 // I can't clear out the other_slabs because it has functions,
786 // consts, strings, vars, and arrays. It has strings from *other*
787 // functions, specifically.
788 bc_slabvec_clear(&vm.main_const_slab);
789 bc_slabvec_clear(&vm.main_slabs);
790 }
791 #endif // BC_ENABLED
792
793 #if DC_ENABLED
794 // Note to self: you cannot delete strings and functions. Deal with it.
795 if (BC_IS_DC) {
796 bc_vec_popAll(vm.prog.consts);
797 bc_slabvec_clear(&vm.main_const_slab);
798 }
799 #endif // DC_ENABLED
800
801 bc_vec_popAll(&f->code);
802
803 ip->idx = 0;
804 }
805 }
806
807 /**
808 * Process a bunch of text.
809 * @param text The text to process.
810 * @param is_stdin True if the text came from stdin, false otherwise.
811 */
812 static void bc_vm_process(const char *text, bool is_stdin) {
813
814 // Set up the parser.
815 bc_parse_text(&vm.prs, text, is_stdin);
816
817 do {
818
819 #if BC_ENABLED
820 // If the first token is the keyword define, then we need to do this
821 // specially because bc thinks it may not be able to parse.
822 if (vm.prs.l.t == BC_LEX_KW_DEFINE) vm.parse(&vm.prs);
823 #endif // BC_ENABLED
824
825 // Parse it all.
826 while (BC_PARSE_CAN_PARSE(vm.prs)) vm.parse(&vm.prs);
827
828 // Execute if possible.
829 if(BC_IS_DC || !BC_PARSE_NO_EXEC(&vm.prs)) bc_program_exec(&vm.prog);
830
831 assert(BC_IS_DC || vm.prog.results.len == 0);
832
833 // Flush in interactive mode.
834 if (BC_I) bc_file_flush(&vm.fout, bc_flush_save);
835
836 } while (vm.prs.l.t != BC_LEX_EOF);
837 }
838
839 #if BC_ENABLED
840
841 /**
842 * Ends an if statement that ends a file. This is to ensure that full parses
843 * happen when a file finishes. Without this, bc thinks that it cannot parse
844 * any further. But if we reach the end of a file, we know we can add an empty
845 * else clause.
846 */
847 static void bc_vm_endif(void) {
848
849 size_t i;
850 bool good;
851
852 // Not a problem if this is true.
853 if (BC_NO_ERR(!BC_PARSE_NO_EXEC(&vm.prs))) return;
854
855 good = true;
856
857 // Find an instance of a body that needs closing, i.e., a statement that did
858 // not have a right brace when it should have.
859 for (i = 0; good && i < vm.prs.flags.len; ++i) {
860 uint16_t flag = *((uint16_t*) bc_vec_item(&vm.prs.flags, i));
861 good = ((flag & BC_PARSE_FLAG_BRACE) != BC_PARSE_FLAG_BRACE);
862 }
863
864 // If we did not find such an instance...
865 if (good) {
866
867 // We set this to restore it later. We don't want the parser thinking
868 // that we are on stdin for this one because it will want more.
869 bool is_stdin = vm.is_stdin;
870
871 vm.is_stdin = false;
872
873 // Cheat and keep parsing empty else clauses until all of them are
874 // satisfied.
875 while (BC_PARSE_IF_END(&vm.prs)) bc_vm_process("else {}", false);
876
877 vm.is_stdin = is_stdin;
878 }
879 // If we reach here, a block was not properly closed, and we should error.
880 else bc_parse_err(&vm.prs, BC_ERR_PARSE_BLOCK);
881 }
882 #endif // BC_ENABLED
883
884 /**
885 * Processes a file.
886 * @param file The filename.
887 */
888 static void bc_vm_file(const char *file) {
889
890 char *data = NULL;
891
892 assert(!vm.sig_pop);
893
894 // Set up the lexer.
895 bc_lex_file(&vm.prs.l, file);
896
897 BC_SIG_LOCK;
898
899 // Read the file.
900 data = bc_read_file(file);
901
902 assert(data != NULL);
903
904 BC_SETJMP_LOCKED(err);
905
906 BC_SIG_UNLOCK;
907
908 // Process it.
909 bc_vm_process(data, false);
910
911 #if BC_ENABLED
912 // Make sure to end any open if statements.
913 if (BC_IS_BC) bc_vm_endif();
914 #endif // BC_ENABLED
915
916 err:
917 BC_SIG_MAYLOCK;
918
919 // Cleanup.
920 free(data);
921 bc_vm_clean();
922
923 // bc_program_reset(), called by bc_vm_clean(), resets the status.
924 // We want it to clear the sig_pop variable in case it was set.
925 if (vm.status == (sig_atomic_t) BC_STATUS_SUCCESS) BC_LONGJMP_STOP;
926
927 BC_LONGJMP_CONT;
928 }
929
930 bool bc_vm_readLine(bool clear) {
931
932 BcStatus s;
933 bool good;
934
935 // Clear the buffer if desired.
936 if (clear) bc_vec_empty(&vm.buffer);
937
938 // Empty the line buffer.
939 bc_vec_empty(&vm.line_buf);
940
941 if (vm.eof) return false;
942
943 do {
944 // bc_read_line() must always return either BC_STATUS_SUCCESS or
945 // BC_STATUS_EOF. Everything else, it and whatever it calls, must jump
946 // out instead.
947 s = bc_read_line(&vm.line_buf, ">>> ");
948 vm.eof = (s == BC_STATUS_EOF);
949 } while (!(s) && !vm.eof && vm.line_buf.len < 1);
950
951 good = (vm.line_buf.len > 1);
952
953 // Concat if we found something.
954 if (good) bc_vec_concat(&vm.buffer, vm.line_buf.v);
955
956 return good;
957 }
958
959 /**
960 * Processes text from stdin.
961 */
962 static void bc_vm_stdin(void) {
963
964 bool clear = true;
965
966 vm.is_stdin = true;
967
968 // Set up the lexer.
969 bc_lex_file(&vm.prs.l, bc_program_stdin_name);
970
971 // These are global so that the dc lexer can access them, but they are tied
972 // to this function, really. Well, this and bc_vm_readLine(). These are the
973 // reason that we have vm.is_stdin to tell the dc lexer if we are reading
974 // from stdin. Well, both lexers care. And the reason they care is so that
975 // if a comment or a string goes across multiple lines, the lexer can
976 // request more data from stdin until the comment or string is ended.
977 BC_SIG_LOCK;
978 bc_vec_init(&vm.buffer, sizeof(uchar), BC_DTOR_NONE);
979 bc_vec_init(&vm.line_buf, sizeof(uchar), BC_DTOR_NONE);
980 BC_SETJMP_LOCKED(err);
981 BC_SIG_UNLOCK;
982
983 // This label exists because errors can cause jumps to end up at the err label
984 // below. If that happens, and the error should be cleared and execution
985 // continue, then we need to jump back.
986 restart:
987
988 // While we still read data from stdin.
989 while (bc_vm_readLine(clear)) {
990
991 size_t len = vm.buffer.len - 1;
992 const char *str = vm.buffer.v;
993
994 // We don't want to clear the buffer when the line ends with a backslash
995 // because a backslash newline is special in bc.
996 clear = (len < 2 || str[len - 2] != '\\' || str[len - 1] != '\n');
997 if (!clear) continue;
998
999 // Process the data.
1000 bc_vm_process(vm.buffer.v, true);
1001
1002 if (vm.eof) break;
1003 else bc_vm_clean();
1004 }
1005
1006 #if BC_ENABLED
1007 // End the if statements.
1008 if (BC_IS_BC) bc_vm_endif();
1009 #endif // BC_ENABLED
1010
1011 err:
1012 BC_SIG_MAYLOCK;
1013
1014 // Cleanup.
1015 bc_vm_clean();
1016
1017 #if !BC_ENABLE_MEMCHECK
1018 assert(vm.status != BC_STATUS_ERROR_FATAL);
1019
1020 vm.status = vm.status == BC_STATUS_QUIT || !BC_I ?
1021 vm.status : BC_STATUS_SUCCESS;
1022 #else // !BC_ENABLE_MEMCHECK
1023 vm.status = vm.status == BC_STATUS_ERROR_FATAL ||
1024 vm.status == BC_STATUS_QUIT || !BC_I ?
1025 vm.status : BC_STATUS_SUCCESS;
1026 #endif // !BC_ENABLE_MEMCHECK
1027
1028 if (!vm.status && !vm.eof) {
1029 bc_vec_empty(&vm.buffer);
1030 BC_LONGJMP_STOP;
1031 BC_SIG_UNLOCK;
1032 goto restart;
1033 }
1034
1035 #ifndef NDEBUG
1036 // Since these are tied to this function, free them here.
1037 bc_vec_free(&vm.line_buf);
1038 bc_vec_free(&vm.buffer);
1039 #endif // NDEBUG
1040
1041 BC_LONGJMP_CONT;
1042 }
1043
1044 #if BC_ENABLED
1045
1046 /**
1047 * Loads a math library.
1048 * @param name The name of the library.
1049 * @param text The text of the source code.
1050 */
1051 static void bc_vm_load(const char *name, const char *text) {
1052
1053 bc_lex_file(&vm.prs.l, name);
1054 bc_parse_text(&vm.prs, text, false);
1055
1056 while (vm.prs.l.t != BC_LEX_EOF) vm.parse(&vm.prs);
1057 }
1058
1059 #endif // BC_ENABLED
1060
1061 /**
1062 * Loads the default error messages.
1063 */
1064 static void bc_vm_defaultMsgs(void) {
1065
1066 size_t i;
1067
1068 vm.func_header = bc_err_func_header;
1069
1070 // Load the error categories.
1071 for (i = 0; i < BC_ERR_IDX_NELEMS + BC_ENABLED; ++i)
1072 vm.err_ids[i] = bc_errs[i];
1073
1074 // Load the error messages.
1075 for (i = 0; i < BC_ERR_NELEMS; ++i) vm.err_msgs[i] = bc_err_msgs[i];
1076 }
1077
1078 /**
1079 * Loads the error messages for the locale. If NLS is disabled, this just loads
1080 * the default messages.
1081 */
1082 static void bc_vm_gettext(void) {
1083
1084 #if BC_ENABLE_NLS
1085 uchar id = 0;
1086 int set = 1, msg = 1;
1087 size_t i;
1088
1089 // If no locale, load the defaults.
1090 if (vm.locale == NULL) {
1091 vm.catalog = BC_VM_INVALID_CATALOG;
1092 bc_vm_defaultMsgs();
1093 return;
1094 }
1095
1096 vm.catalog = catopen(BC_MAINEXEC, NL_CAT_LOCALE);
1097
1098 // If no catalog, load the defaults.
1099 if (vm.catalog == BC_VM_INVALID_CATALOG) {
1100 bc_vm_defaultMsgs();
1101 return;
1102 }
1103
1104 // Load the function header.
1105 vm.func_header = catgets(vm.catalog, set, msg, bc_err_func_header);
1106
1107 // Load the error categories.
1108 for (set += 1; msg <= BC_ERR_IDX_NELEMS + BC_ENABLED; ++msg)
1109 vm.err_ids[msg - 1] = catgets(vm.catalog, set, msg, bc_errs[msg - 1]);
1110
1111 i = 0;
1112 id = bc_err_ids[i];
1113
1114 // Load the error messages. In order to understand this loop, you must know
1115 // the order of messages and categories in the enum and in the locale files.
1116 for (set = id + 3, msg = 1; i < BC_ERR_NELEMS; ++i, ++msg) {
1117
1118 if (id != bc_err_ids[i]) {
1119 msg = 1;
1120 id = bc_err_ids[i];
1121 set = id + 3;
1122 }
1123
1124 vm.err_msgs[i] = catgets(vm.catalog, set, msg, bc_err_msgs[i]);
1125 }
1126 #else // BC_ENABLE_NLS
1127 bc_vm_defaultMsgs();
1128 #endif // BC_ENABLE_NLS
1129 }
1130
1131 /**
1132 * Starts execution. Really, this is a function of historical accident; it could
1133 * probably be combined with bc_vm_boot(), but I don't care enough. Really, this
1134 * function starts when execution of bc or dc source code starts.
1135 */
1136 static void bc_vm_exec(void) {
1137
1138 size_t i;
1139 bool has_file = false;
1140 BcVec buf;
1141
1142 #if BC_ENABLED
1143 // Load the math libraries.
1144 if (BC_IS_BC && (vm.flags & BC_FLAG_L)) {
1145
1146 // Can't allow redefinitions in the builtin library.
1147 vm.no_redefine = true;
1148
1149 bc_vm_load(bc_lib_name, bc_lib);
1150
1151 #if BC_ENABLE_EXTRA_MATH
1152 if (!BC_IS_POSIX) bc_vm_load(bc_lib2_name, bc_lib2);
1153 #endif // BC_ENABLE_EXTRA_MATH
1154
1155 // Make sure to clear this.
1156 vm.no_redefine = false;
1157
1158 // Execute to ensure that all is hunky dory. Without this, scale can be
1159 // set improperly.
1160 bc_program_exec(&vm.prog);
1161 }
1162 #endif // BC_ENABLED
1163
1164 // If there are expressions to execute...
1165 if (vm.exprs.len) {
1166
1167 size_t len = vm.exprs.len - 1;
1168 bool more;
1169
1170 BC_SIG_LOCK;
1171
1172 // Create this as a buffer for reading into.
1173 bc_vec_init(&buf, sizeof(uchar), BC_DTOR_NONE);
1174
1175 #ifndef NDEBUG
1176 BC_SETJMP_LOCKED(err);
1177 #endif // NDEBUG
1178
1179 BC_SIG_UNLOCK;
1180
1181 // Prepare the lexer.
1182 bc_lex_file(&vm.prs.l, bc_program_exprs_name);
1183
1184 // Process the expressions one at a time.
1185 do {
1186
1187 more = bc_read_buf(&buf, vm.exprs.v, &len);
1188 bc_vec_pushByte(&buf, '\0');
1189 bc_vm_process(buf.v, false);
1190
1191 bc_vec_popAll(&buf);
1192
1193 } while (more);
1194
1195 BC_SIG_LOCK;
1196
1197 bc_vec_free(&buf);
1198
1199 #ifndef NDEBUG
1200 BC_UNSETJMP;
1201 #endif // NDEBUG
1202
1203 BC_SIG_UNLOCK;
1204
1205 // Sometimes, executing expressions means we need to quit.
1206 if (!vm.no_exprs && vm.exit_exprs) return;
1207 }
1208
1209 // Process files.
1210 for (i = 0; i < vm.files.len; ++i) {
1211 char *path = *((char**) bc_vec_item(&vm.files, i));
1212 if (!strcmp(path, "")) continue;
1213 has_file = true;
1214 bc_vm_file(path);
1215 }
1216
1217 #if BC_ENABLE_EXTRA_MATH
1218 // These are needed for the pseudo-random number generator.
1219 bc_unveil("/dev/urandom", "r");
1220 bc_unveil("/dev/random", "r");
1221 bc_unveil(NULL, NULL);
1222 #endif // BC_ENABLE_EXTRA_MATH
1223
1224 #if BC_ENABLE_HISTORY
1225
1226 // We need to keep tty if history is enabled, and we need to keep rpath for
1227 // the times when we read from /dev/urandom.
1228 if (BC_TTY && !vm.history.badTerm) {
1229 bc_pledge(bc_pledge_end_history, NULL);
1230 }
1231 else
1232 #endif // BC_ENABLE_HISTORY
1233 {
1234 bc_pledge(bc_pledge_end, NULL);
1235 }
1236
1237 #if BC_ENABLE_AFL
1238 // This is the thing that makes fuzzing with AFL++ so fast. If you move this
1239 // back, you won't cause any problems, but fuzzing will slow down. If you
1240 // move this forward, you won't fuzz anything because you will be skipping
1241 // the reading from stdin.
1242 __AFL_INIT();
1243 #endif // BC_ENABLE_AFL
1244
1245 // Execute from stdin. bc always does.
1246 if (BC_IS_BC || !has_file) bc_vm_stdin();
1247
1248 // These are all protected by ifndef NDEBUG because if these are needed, bc is
1249 // going to exit anyway, and I see no reason to include this code in a release
1250 // build when the OS is going to free all of the resources anyway.
1251 #ifndef NDEBUG
1252 return;
1253
1254 err:
1255 BC_SIG_MAYLOCK;
1256 bc_vec_free(&buf);
1257 BC_LONGJMP_CONT;
1258 #endif // NDEBUG
1259 }
1260
1261 void bc_vm_boot(int argc, char *argv[]) {
1262
1263 int ttyin, ttyout, ttyerr;
1264 bool tty;
1265 const char* const env_len = BC_IS_BC ? "BC_LINE_LENGTH" : "DC_LINE_LENGTH";
1266 const char* const env_args = BC_IS_BC ? "BC_ENV_ARGS" : "DC_ENV_ARGS";
1267
1268 // We need to know which of stdin, stdout, and stderr are tty's.
1269 ttyin = isatty(STDIN_FILENO);
1270 ttyout = isatty(STDOUT_FILENO);
1271 ttyerr = isatty(STDERR_FILENO);
1272 tty = (ttyin != 0 && ttyout != 0 && ttyerr != 0);
1273
1274 vm.flags |= ttyin ? BC_FLAG_TTYIN : 0;
1275 vm.flags |= tty ? BC_FLAG_TTY : 0;
1276 vm.flags |= ttyin && ttyout ? BC_FLAG_I : 0;
1277
1278 // Set up signals.
1279 bc_vm_sigaction();
1280
1281 // Initialize some vm stuff. This is separate to make things easier for the
1282 // library.
1283 bc_vm_init();
1284
1285 // Explicitly set this in case NULL isn't all zeroes.
1286 vm.file = NULL;
1287
1288 // Set the error messages.
1289 bc_vm_gettext();
1290
1291 // Initialize the output file buffers. They each take portions of the global
1292 // buffer. stdout gets more because it will probably have more data.
1293 bc_file_init(&vm.ferr, STDERR_FILENO, output_bufs + BC_VM_STDOUT_BUF_SIZE,
1294 BC_VM_STDERR_BUF_SIZE);
1295 bc_file_init(&vm.fout, STDOUT_FILENO, output_bufs, BC_VM_STDOUT_BUF_SIZE);
1296
1297 // Set the input buffer to the rest of the global buffer.
1298 vm.buf = output_bufs + BC_VM_STDOUT_BUF_SIZE + BC_VM_STDERR_BUF_SIZE;
1299
1300 // Set the line length by environment variable.
1301 vm.line_len = (uint16_t) bc_vm_envLen(env_len);
1302
1303 // Clear the files and expressions vectors, just in case. This marks them as
1304 // *not* allocated.
1305 bc_vec_clear(&vm.files);
1306 bc_vec_clear(&vm.exprs);
1307
1308 #if !BC_ENABLE_LIBRARY
1309
1310 // Initialize the slab vectors.
1311 bc_slabvec_init(&vm.main_const_slab);
1312 bc_slabvec_init(&vm.main_slabs);
1313 bc_slabvec_init(&vm.other_slabs);
1314
1315 #endif // !BC_ENABLE_LIBRARY
1316
1317 // Initialize the program and main parser. These have to be in this order
1318 // because the program has to be initialized first, since a pointer to it is
1319 // passed to the parser.
1320 bc_program_init(&vm.prog);
1321 bc_parse_init(&vm.prs, &vm.prog, BC_PROG_MAIN);
1322
1323 #if BC_ENABLED
1324 // bc checks this environment variable to see if it should run in standard
1325 // mode.
1326 if (BC_IS_BC) {
1327
1328 char* var = bc_vm_getenv("POSIXLY_CORRECT");
1329
1330 vm.flags |= BC_FLAG_S * (var != NULL);
1331 bc_vm_getenvFree(var);
1332 }
1333 #endif // BC_ENABLED
1334
1335 // Set defaults.
1336 vm.flags |= BC_TTY ? BC_FLAG_P | BC_FLAG_R : 0;
1337 vm.flags |= BC_I ? BC_FLAG_Q : 0;
1338
1339 // Are we in TTY mode?
1340 if (BC_TTY) {
1341
1342 const char* const env_tty = BC_IS_BC ? "BC_TTY_MODE" : "DC_TTY_MODE";
1343 int env_tty_def = BC_IS_BC ? BC_DEFAULT_TTY_MODE : DC_DEFAULT_TTY_MODE;
1344 const char* const env_prompt = BC_IS_BC ? "BC_PROMPT" : "DC_PROMPT";
1345 int env_prompt_def = BC_IS_BC ? BC_DEFAULT_PROMPT : DC_DEFAULT_PROMPT;
1346
1347 // Set flags for TTY mode and prompt.
1348 bc_vm_setenvFlag(env_tty, env_tty_def, BC_FLAG_TTY);
1349 bc_vm_setenvFlag(env_prompt, tty ? env_prompt_def : 0, BC_FLAG_P);
1350
1351 #if BC_ENABLE_HISTORY
1352 // If TTY mode is used, activate history.
1353 if (BC_TTY) bc_history_init(&vm.history);
1354 #endif // BC_ENABLE_HISTORY
1355 }
1356
1357 // Process environment and command-line arguments.
1358 bc_vm_envArgs(env_args);
1359 bc_args(argc, argv, true);
1360
1361 // If we are in interactive mode...
1362 if (BC_I) {
1363
1364 const char* const env_sigint = BC_IS_BC ? "BC_SIGINT_RESET" :
1365 "DC_SIGINT_RESET";
1366 int env_sigint_def = BC_IS_BC ? BC_DEFAULT_SIGINT_RESET :
1367 DC_DEFAULT_SIGINT_RESET;
1368
1369 // Set whether we reset on SIGINT or not.
1370 bc_vm_setenvFlag(env_sigint, env_sigint_def, BC_FLAG_SIGINT);
1371 #if BC_ENABLED
1372 // Set whether we print the banner or not.
1373 if (BC_IS_BC)
1374 bc_vm_setenvFlag("BC_BANNER", BC_DEFAULT_BANNER, BC_FLAG_Q);
1375 #endif // BC_ENABLED
1376 }
1377
1378 #if BC_ENABLED
1379 // Disable global stacks in POSIX mode.
1380 if (BC_IS_POSIX) vm.flags &= ~(BC_FLAG_G);
1381 #endif // BC_ENABLED
1382
1383 #if BC_ENABLED
1384 // Print the banner if allowed. We have to be in bc, in interactive mode,
1385 // and not be quieted by command-line option or environment variable.
1386 if (BC_IS_BC && BC_I && (vm.flags & BC_FLAG_Q)) {
1387 bc_vm_info(NULL);
1388 bc_file_putchar(&vm.fout, bc_flush_none, '\n');
1389 bc_file_flush(&vm.fout, bc_flush_none);
1390 }
1391 #endif // BC_ENABLED
1392
1393 BC_SIG_UNLOCK;
1394
1395 // Start executing.
1396 bc_vm_exec();
1397 }
1398 #endif // !BC_ENABLE_LIBRARY
1399
1400 void bc_vm_init(void) {
1401
1402 BC_SIG_ASSERT_LOCKED;
1403
1404 #if !BC_ENABLE_LIBRARY
1405 // Set up the constant zero.
1406 bc_num_setup(&vm.zero, vm.zero_num, BC_VM_ONE_CAP);
1407 #endif // !BC_ENABLE_LIBRARY
1408
1409 // Set up more constant BcNum's.
1410 bc_num_setup(&vm.one, vm.one_num, BC_VM_ONE_CAP);
1411 bc_num_one(&vm.one);
1412
1413 // Set up more constant BcNum's.
1414 memcpy(vm.max_num, bc_num_bigdigMax,
1415 bc_num_bigdigMax_size * sizeof(BcDig));
1416 memcpy(vm.max2_num, bc_num_bigdigMax2,
1417 bc_num_bigdigMax2_size * sizeof(BcDig));
1418 bc_num_setup(&vm.max, vm.max_num, BC_NUM_BIGDIG_LOG10);
1419 bc_num_setup(&vm.max2, vm.max2_num, BC_NUM_BIGDIG_LOG10);
1420 vm.max.len = bc_num_bigdigMax_size;
1421 vm.max2.len = bc_num_bigdigMax2_size;
1422
1423 // Set up the maxes for the globals.
1424 vm.maxes[BC_PROG_GLOBALS_IBASE] = BC_NUM_MAX_POSIX_IBASE;
1425 vm.maxes[BC_PROG_GLOBALS_OBASE] = BC_MAX_OBASE;
1426 vm.maxes[BC_PROG_GLOBALS_SCALE] = BC_MAX_SCALE;
1427
1428 #if BC_ENABLE_EXTRA_MATH
1429 vm.maxes[BC_PROG_MAX_RAND] = ((BcRand) 0) - 1;
1430 #endif // BC_ENABLE_EXTRA_MATH
1431
1432 #if BC_ENABLED
1433 #if !BC_ENABLE_LIBRARY
1434 // bc has a higher max ibase when it's not in POSIX mode.
1435 if (BC_IS_BC && !BC_IS_POSIX)
1436 #endif // !BC_ENABLE_LIBRARY
1437 {
1438 vm.maxes[BC_PROG_GLOBALS_IBASE] = BC_NUM_MAX_IBASE;
1439 }
1440 #endif // BC_ENABLED
1441 }
1442
1443 #if BC_ENABLE_LIBRARY
1444 void bc_vm_atexit(void) {
1445
1446 bc_vm_shutdown();
1447
1448 #ifndef NDEBUG
1449 bc_vec_free(&vm.jmp_bufs);
1450 #endif // NDEBUG
1451 }
1452 #else // BC_ENABLE_LIBRARY
1453 int bc_vm_atexit(int status) {
1454
1455 // Set the status correctly.
1456 int s = BC_STATUS_IS_ERROR(status) ? status : BC_STATUS_SUCCESS;
1457
1458 bc_vm_shutdown();
1459
1460 #ifndef NDEBUG
1461 bc_vec_free(&vm.jmp_bufs);
1462 #endif // NDEBUG
1463
1464 return s;
1465 }
1466 #endif // BC_ENABLE_LIBRARY
1467