xref: /aosp_15_r20/external/bc/src/main.c (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker /*
2*5a6e8488SAndroid Build Coastguard Worker  * *****************************************************************************
3*5a6e8488SAndroid Build Coastguard Worker  *
4*5a6e8488SAndroid Build Coastguard Worker  * SPDX-License-Identifier: BSD-2-Clause
5*5a6e8488SAndroid Build Coastguard Worker  *
6*5a6e8488SAndroid Build Coastguard Worker  * Copyright (c) 2018-2024 Gavin D. Howard and contributors.
7*5a6e8488SAndroid Build Coastguard Worker  *
8*5a6e8488SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
9*5a6e8488SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions are met:
10*5a6e8488SAndroid Build Coastguard Worker  *
11*5a6e8488SAndroid Build Coastguard Worker  * * Redistributions of source code must retain the above copyright notice, this
12*5a6e8488SAndroid Build Coastguard Worker  *   list of conditions and the following disclaimer.
13*5a6e8488SAndroid Build Coastguard Worker  *
14*5a6e8488SAndroid Build Coastguard Worker  * * Redistributions in binary form must reproduce the above copyright notice,
15*5a6e8488SAndroid Build Coastguard Worker  *   this list of conditions and the following disclaimer in the documentation
16*5a6e8488SAndroid Build Coastguard Worker  *   and/or other materials provided with the distribution.
17*5a6e8488SAndroid Build Coastguard Worker  *
18*5a6e8488SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*5a6e8488SAndroid Build Coastguard Worker  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*5a6e8488SAndroid Build Coastguard Worker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*5a6e8488SAndroid Build Coastguard Worker  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*5a6e8488SAndroid Build Coastguard Worker  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*5a6e8488SAndroid Build Coastguard Worker  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*5a6e8488SAndroid Build Coastguard Worker  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*5a6e8488SAndroid Build Coastguard Worker  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*5a6e8488SAndroid Build Coastguard Worker  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*5a6e8488SAndroid Build Coastguard Worker  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*5a6e8488SAndroid Build Coastguard Worker  * POSSIBILITY OF SUCH DAMAGE.
29*5a6e8488SAndroid Build Coastguard Worker  *
30*5a6e8488SAndroid Build Coastguard Worker  * *****************************************************************************
31*5a6e8488SAndroid Build Coastguard Worker  *
32*5a6e8488SAndroid Build Coastguard Worker  * The entry point for bc.
33*5a6e8488SAndroid Build Coastguard Worker  *
34*5a6e8488SAndroid Build Coastguard Worker  */
35*5a6e8488SAndroid Build Coastguard Worker 
36*5a6e8488SAndroid Build Coastguard Worker #include <assert.h>
37*5a6e8488SAndroid Build Coastguard Worker #include <stdlib.h>
38*5a6e8488SAndroid Build Coastguard Worker #include <string.h>
39*5a6e8488SAndroid Build Coastguard Worker 
40*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_NLS
41*5a6e8488SAndroid Build Coastguard Worker #include <locale.h>
42*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_NLS
43*5a6e8488SAndroid Build Coastguard Worker 
44*5a6e8488SAndroid Build Coastguard Worker #ifndef _WIN32
45*5a6e8488SAndroid Build Coastguard Worker #include <libgen.h>
46*5a6e8488SAndroid Build Coastguard Worker #endif // _WIN32
47*5a6e8488SAndroid Build Coastguard Worker 
48*5a6e8488SAndroid Build Coastguard Worker #include <setjmp.h>
49*5a6e8488SAndroid Build Coastguard Worker 
50*5a6e8488SAndroid Build Coastguard Worker #include <version.h>
51*5a6e8488SAndroid Build Coastguard Worker #include <status.h>
52*5a6e8488SAndroid Build Coastguard Worker #include <vm.h>
53*5a6e8488SAndroid Build Coastguard Worker #include <bc.h>
54*5a6e8488SAndroid Build Coastguard Worker #include <dc.h>
55*5a6e8488SAndroid Build Coastguard Worker 
56*5a6e8488SAndroid Build Coastguard Worker int
main(int argc,const char * argv[])57*5a6e8488SAndroid Build Coastguard Worker main(int argc, const char* argv[])
58*5a6e8488SAndroid Build Coastguard Worker {
59*5a6e8488SAndroid Build Coastguard Worker 	BcStatus s;
60*5a6e8488SAndroid Build Coastguard Worker 	char* name;
61*5a6e8488SAndroid Build Coastguard Worker 	size_t len = strlen(BC_EXECPREFIX);
62*5a6e8488SAndroid Build Coastguard Worker 
63*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_NLS
64*5a6e8488SAndroid Build Coastguard Worker 	// Must set the locale properly in order to have the right error messages.
65*5a6e8488SAndroid Build Coastguard Worker 	vm->locale = setlocale(LC_ALL, "");
66*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_NLS
67*5a6e8488SAndroid Build Coastguard Worker 
68*5a6e8488SAndroid Build Coastguard Worker 	// Set the start pledge().
69*5a6e8488SAndroid Build Coastguard Worker 	bc_pledge(bc_pledge_start, NULL);
70*5a6e8488SAndroid Build Coastguard Worker 
71*5a6e8488SAndroid Build Coastguard Worker 	// Sometimes, argv[0] can be NULL. Better make sure to be robust against it.
72*5a6e8488SAndroid Build Coastguard Worker 	if (argv[0] != NULL)
73*5a6e8488SAndroid Build Coastguard Worker 	{
74*5a6e8488SAndroid Build Coastguard Worker 		// Figure out the name of the calculator we are using. We can't use
75*5a6e8488SAndroid Build Coastguard Worker 		// basename because it's not portable, but yes, this is stripping off
76*5a6e8488SAndroid Build Coastguard Worker 		// the directory.
77*5a6e8488SAndroid Build Coastguard Worker 		name = strrchr(argv[0], BC_FILE_SEP);
78*5a6e8488SAndroid Build Coastguard Worker 		vm->name = (name == NULL) ? argv[0] : name + 1;
79*5a6e8488SAndroid Build Coastguard Worker 	}
80*5a6e8488SAndroid Build Coastguard Worker 	else
81*5a6e8488SAndroid Build Coastguard Worker 	{
82*5a6e8488SAndroid Build Coastguard Worker #if !DC_ENABLED
83*5a6e8488SAndroid Build Coastguard Worker 		vm->name = "bc";
84*5a6e8488SAndroid Build Coastguard Worker #elif !BC_ENABLED
85*5a6e8488SAndroid Build Coastguard Worker 		vm->name = "dc";
86*5a6e8488SAndroid Build Coastguard Worker #else
87*5a6e8488SAndroid Build Coastguard Worker 		// Just default to bc in that case.
88*5a6e8488SAndroid Build Coastguard Worker 		vm->name = "bc";
89*5a6e8488SAndroid Build Coastguard Worker #endif
90*5a6e8488SAndroid Build Coastguard Worker 	}
91*5a6e8488SAndroid Build Coastguard Worker 
92*5a6e8488SAndroid Build Coastguard Worker 	// If the name is longer than the length of the prefix, skip the prefix.
93*5a6e8488SAndroid Build Coastguard Worker 	if (strlen(vm->name) > len) vm->name += len;
94*5a6e8488SAndroid Build Coastguard Worker 
95*5a6e8488SAndroid Build Coastguard Worker 	BC_SIG_LOCK;
96*5a6e8488SAndroid Build Coastguard Worker 
97*5a6e8488SAndroid Build Coastguard Worker 	// We *must* do this here. Otherwise, other code could not jump out all of
98*5a6e8488SAndroid Build Coastguard Worker 	// the way.
99*5a6e8488SAndroid Build Coastguard Worker 	bc_vec_init(&vm->jmp_bufs, sizeof(sigjmp_buf), BC_DTOR_NONE);
100*5a6e8488SAndroid Build Coastguard Worker 
101*5a6e8488SAndroid Build Coastguard Worker 	BC_SETJMP_LOCKED(vm, exit);
102*5a6e8488SAndroid Build Coastguard Worker 
103*5a6e8488SAndroid Build Coastguard Worker #if BC_CLANG
104*5a6e8488SAndroid Build Coastguard Worker #pragma clang diagnostic push
105*5a6e8488SAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wcast-qual"
106*5a6e8488SAndroid Build Coastguard Worker #endif // BC_CLANG
107*5a6e8488SAndroid Build Coastguard Worker #if !DC_ENABLED
108*5a6e8488SAndroid Build Coastguard Worker 	s = bc_main(argc, (const char**) argv);
109*5a6e8488SAndroid Build Coastguard Worker #elif !BC_ENABLED
110*5a6e8488SAndroid Build Coastguard Worker 	s = dc_main(argc, (const char**) argv);
111*5a6e8488SAndroid Build Coastguard Worker #else
112*5a6e8488SAndroid Build Coastguard Worker 	// BC_IS_BC uses vm->name, which was set above. So we're good.
113*5a6e8488SAndroid Build Coastguard Worker 	if (BC_IS_BC) s = bc_main(argc, (const char**) argv);
114*5a6e8488SAndroid Build Coastguard Worker 	else s = dc_main(argc, (const char**) argv);
115*5a6e8488SAndroid Build Coastguard Worker #endif
116*5a6e8488SAndroid Build Coastguard Worker #if BC_CLANG
117*5a6e8488SAndroid Build Coastguard Worker #pragma clang diagnostic pop
118*5a6e8488SAndroid Build Coastguard Worker #endif // BC_CLANG
119*5a6e8488SAndroid Build Coastguard Worker 
120*5a6e8488SAndroid Build Coastguard Worker 	vm->status = (sig_atomic_t) s;
121*5a6e8488SAndroid Build Coastguard Worker 
122*5a6e8488SAndroid Build Coastguard Worker exit:
123*5a6e8488SAndroid Build Coastguard Worker 	BC_SIG_MAYLOCK;
124*5a6e8488SAndroid Build Coastguard Worker 
125*5a6e8488SAndroid Build Coastguard Worker 	s = bc_vm_atexit((BcStatus) vm->status);
126*5a6e8488SAndroid Build Coastguard Worker 
127*5a6e8488SAndroid Build Coastguard Worker 	return (int) s;
128*5a6e8488SAndroid Build Coastguard Worker }
129