1252884aeSStefan Eßer /* 2252884aeSStefan Eßer * ***************************************************************************** 3252884aeSStefan Eßer * 43aa99676SStefan Eßer * SPDX-License-Identifier: BSD-2-Clause 5252884aeSStefan Eßer * 6*a970610aSStefan Eßer * Copyright (c) 2018-2024 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 5678bc019dSStefan Eßer int 5778bc019dSStefan Eßer main(int argc, char* argv[]) 5878bc019dSStefan Eßer { 59*a970610aSStefan Eßer BcStatus s; 60252884aeSStefan Eßer char* name; 61252884aeSStefan Eßer size_t len = strlen(BC_EXECPREFIX); 62252884aeSStefan Eßer 6300698711SStefan Eßer #if BC_ENABLE_NLS 6444d4804dSStefan Eßer // Must set the locale properly in order to have the right error messages. 65d101cdd6SStefan Eßer vm->locale = setlocale(LC_ALL, ""); 6600698711SStefan Eßer #endif // BC_ENABLE_NLS 67252884aeSStefan Eßer 6844d4804dSStefan Eßer // Set the start pledge(). 6944d4804dSStefan Eßer bc_pledge(bc_pledge_start, NULL); 7044d4804dSStefan Eßer 7100698711SStefan Eßer // Sometimes, argv[0] can be NULL. Better make sure to be robust against it. 7278bc019dSStefan Eßer if (argv[0] != NULL) 7378bc019dSStefan Eßer { 7400698711SStefan Eßer // Figure out the name of the calculator we are using. We can't use 7500698711SStefan Eßer // basename because it's not portable, but yes, this is stripping off 7600698711SStefan Eßer // the directory. 777e5c51e5SStefan Eßer name = strrchr(argv[0], BC_FILE_SEP); 78d101cdd6SStefan Eßer vm->name = (name == NULL) ? argv[0] : name + 1; 7900698711SStefan Eßer } 8000698711SStefan Eßer else 8100698711SStefan Eßer { 8200698711SStefan Eßer #if !DC_ENABLED 83d101cdd6SStefan Eßer vm->name = "bc"; 8400698711SStefan Eßer #elif !BC_ENABLED 85d101cdd6SStefan Eßer vm->name = "dc"; 8600698711SStefan Eßer #else 8700698711SStefan Eßer // Just default to bc in that case. 88d101cdd6SStefan Eßer vm->name = "bc"; 8900698711SStefan Eßer #endif 9000698711SStefan Eßer } 91252884aeSStefan Eßer 9244d4804dSStefan Eßer // If the name is longer than the length of the prefix, skip the prefix. 93d101cdd6SStefan Eßer if (strlen(vm->name) > len) vm->name += len; 94252884aeSStefan Eßer 95252884aeSStefan Eßer BC_SIG_LOCK; 96252884aeSStefan Eßer 9744d4804dSStefan Eßer // We *must* do this here. Otherwise, other code could not jump out all of 9844d4804dSStefan Eßer // the way. 99d101cdd6SStefan Eßer bc_vec_init(&vm->jmp_bufs, sizeof(sigjmp_buf), BC_DTOR_NONE); 100252884aeSStefan Eßer 101d101cdd6SStefan Eßer BC_SETJMP_LOCKED(vm, exit); 102252884aeSStefan Eßer 103252884aeSStefan Eßer #if !DC_ENABLED 104*a970610aSStefan Eßer s = bc_main(argc, argv); 105252884aeSStefan Eßer #elif !BC_ENABLED 106*a970610aSStefan Eßer s = dc_main(argc, argv); 107252884aeSStefan Eßer #else 108d101cdd6SStefan Eßer // BC_IS_BC uses vm->name, which was set above. So we're good. 109*a970610aSStefan Eßer if (BC_IS_BC) s = bc_main(argc, argv); 110*a970610aSStefan Eßer else s = dc_main(argc, argv); 111252884aeSStefan Eßer #endif 112252884aeSStefan Eßer 113*a970610aSStefan Eßer vm->status = (int) s; 114*a970610aSStefan Eßer 115252884aeSStefan Eßer exit: 116252884aeSStefan Eßer BC_SIG_MAYLOCK; 117252884aeSStefan Eßer 118*a970610aSStefan Eßer return vm->status == BC_STATUS_QUIT ? BC_STATUS_SUCCESS : vm->status; 119252884aeSStefan Eßer } 120