1252884aeSStefan Eßer /* 2252884aeSStefan Eßer * ***************************************************************************** 3252884aeSStefan Eßer * 43aa99676SStefan Eßer * SPDX-License-Identifier: BSD-2-Clause 5252884aeSStefan Eßer * 610328f8bSStefan Eßer * Copyright (c) 2018-2021 Gavin D. Howard and contributors. 7252884aeSStefan Eßer * 8252884aeSStefan Eßer * Redistribution and use in source and binary forms, with or without 9252884aeSStefan Eßer * modification, are permitted provided that the following conditions are met: 10252884aeSStefan Eßer * 11252884aeSStefan Eßer * * Redistributions of source code must retain the above copyright notice, this 12252884aeSStefan Eßer * list of conditions and the following disclaimer. 13252884aeSStefan Eßer * 14252884aeSStefan Eßer * * Redistributions in binary form must reproduce the above copyright notice, 15252884aeSStefan Eßer * this list of conditions and the following disclaimer in the documentation 16252884aeSStefan Eßer * and/or other materials provided with the distribution. 17252884aeSStefan Eßer * 18252884aeSStefan Eßer * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19252884aeSStefan Eßer * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20252884aeSStefan Eßer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21252884aeSStefan Eßer * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22252884aeSStefan Eßer * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23252884aeSStefan Eßer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24252884aeSStefan Eßer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25252884aeSStefan Eßer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26252884aeSStefan Eßer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27252884aeSStefan Eßer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28252884aeSStefan Eßer * POSSIBILITY OF SUCH DAMAGE. 29252884aeSStefan Eßer * 30252884aeSStefan Eßer * ***************************************************************************** 31252884aeSStefan Eßer * 32252884aeSStefan Eßer * The entry point for bc. 33252884aeSStefan Eßer * 34252884aeSStefan Eßer */ 35252884aeSStefan Eßer 36252884aeSStefan Eßer #include <assert.h> 37252884aeSStefan Eßer #include <stdlib.h> 38252884aeSStefan Eßer #include <string.h> 39252884aeSStefan Eßer 4000698711SStefan Eßer #if BC_ENABLE_NLS 41252884aeSStefan Eßer #include <locale.h> 4200698711SStefan Eßer #endif // BC_ENABLE_NLS 437e5c51e5SStefan Eßer 447e5c51e5SStefan Eßer #ifndef _WIN32 45252884aeSStefan Eßer #include <libgen.h> 467e5c51e5SStefan Eßer #endif // _WIN32 47252884aeSStefan Eßer 48252884aeSStefan Eßer #include <setjmp.h> 49252884aeSStefan Eßer 507e5c51e5SStefan Eßer #include <version.h> 51252884aeSStefan Eßer #include <status.h> 52252884aeSStefan Eßer #include <vm.h> 53252884aeSStefan Eßer #include <bc.h> 54252884aeSStefan Eßer #include <dc.h> 55252884aeSStefan Eßer 56*78bc019dSStefan Eßer int 57*78bc019dSStefan Eßer main(int argc, char* argv[]) 58*78bc019dSStefan Eßer { 59252884aeSStefan Eßer char* name; 60252884aeSStefan Eßer size_t len = strlen(BC_EXECPREFIX); 61252884aeSStefan Eßer 6200698711SStefan Eßer #if BC_ENABLE_NLS 6344d4804dSStefan Eßer // Must set the locale properly in order to have the right error messages. 64252884aeSStefan Eßer vm.locale = setlocale(LC_ALL, ""); 6500698711SStefan Eßer #endif // BC_ENABLE_NLS 66252884aeSStefan Eßer 6744d4804dSStefan Eßer // Set the start pledge(). 6844d4804dSStefan Eßer bc_pledge(bc_pledge_start, NULL); 6944d4804dSStefan Eßer 7000698711SStefan Eßer // Sometimes, argv[0] can be NULL. Better make sure to be robust against it. 71*78bc019dSStefan Eßer if (argv[0] != NULL) 72*78bc019dSStefan Eßer { 7300698711SStefan Eßer // Figure out the name of the calculator we are using. We can't use 7400698711SStefan Eßer // basename because it's not portable, but yes, this is stripping off 7500698711SStefan Eßer // the directory. 767e5c51e5SStefan Eßer name = strrchr(argv[0], BC_FILE_SEP); 77252884aeSStefan Eßer vm.name = (name == NULL) ? argv[0] : name + 1; 7800698711SStefan Eßer } 7900698711SStefan Eßer else 8000698711SStefan Eßer { 8100698711SStefan Eßer #if !DC_ENABLED 8200698711SStefan Eßer vm.name = "bc"; 8300698711SStefan Eßer #elif !BC_ENABLED 8400698711SStefan Eßer vm.name = "dc"; 8500698711SStefan Eßer #else 8600698711SStefan Eßer // Just default to bc in that case. 8700698711SStefan Eßer vm.name = "bc"; 8800698711SStefan Eßer #endif 8900698711SStefan Eßer } 90252884aeSStefan Eßer 9144d4804dSStefan Eßer // If the name is longer than the length of the prefix, skip the prefix. 92252884aeSStefan Eßer if (strlen(vm.name) > len) vm.name += len; 93252884aeSStefan Eßer 94252884aeSStefan Eßer BC_SIG_LOCK; 95252884aeSStefan Eßer 9644d4804dSStefan Eßer // We *must* do this here. Otherwise, other code could not jump out all of 9744d4804dSStefan Eßer // the way. 9844d4804dSStefan Eßer bc_vec_init(&vm.jmp_bufs, sizeof(sigjmp_buf), BC_DTOR_NONE); 99252884aeSStefan Eßer 100252884aeSStefan Eßer BC_SETJMP_LOCKED(exit); 101252884aeSStefan Eßer 102252884aeSStefan Eßer #if !DC_ENABLED 103252884aeSStefan Eßer bc_main(argc, argv); 104252884aeSStefan Eßer #elif !BC_ENABLED 105252884aeSStefan Eßer dc_main(argc, argv); 106252884aeSStefan Eßer #else 10744d4804dSStefan Eßer // BC_IS_BC uses vm.name, which was set above. So we're good. 108252884aeSStefan Eßer if (BC_IS_BC) bc_main(argc, argv); 109252884aeSStefan Eßer else dc_main(argc, argv); 110252884aeSStefan Eßer #endif 111252884aeSStefan Eßer 112252884aeSStefan Eßer exit: 113252884aeSStefan Eßer BC_SIG_MAYLOCK; 114252884aeSStefan Eßer 11544d4804dSStefan Eßer // Ensure we exit appropriately. 11610328f8bSStefan Eßer return bc_vm_atexit((int) vm.status); 117252884aeSStefan Eßer } 118