2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 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 * Code for processing command-line arguments.
52 * Adds @a str to the list of expressions to execute later.
53 * @param str The string to add to the list of expressions.
55 static void bc_args_exprs(const char *str) {
57 if (vm.exprs.v == NULL) bc_vec_init(&vm.exprs, sizeof(uchar), BC_DTOR_NONE);
58 bc_vec_concat(&vm.exprs, str);
59 bc_vec_concat(&vm.exprs, "\n");
63 * Adds the contents of @a file to the list of expressions to execute later.
64 * @param file The name of the file whose contents should be added to the list
65 * of expressions to execute.
67 static void bc_args_file(const char *file) {
75 buf = bc_read_file(file);
86 * Redefines a keyword, if it exists and is not a POSIX keyword. Otherwise, it
87 * throws a fatal error.
88 * @param keyword The keyword to redefine.
90 static void bc_args_redefine(const char *keyword) {
94 for (i = 0; i < bc_lex_kws_len; ++i) {
96 const BcLexKeyword *kw = bc_lex_kws + i;
98 if (!strcmp(keyword, kw->name)) {
100 if (BC_LEX_KW_POSIX(kw)) break;
102 vm.redefined_kws[i] = true;
108 bc_error(BC_ERR_FATAL_ARG, 0, keyword);
113 void bc_args(int argc, char *argv[], bool exit_exprs) {
117 bool do_exit = false, version = false;
120 BC_SIG_ASSERT_LOCKED;
122 bc_opt_init(&opts, argv);
124 // This loop should look familiar to anyone who has used getopt() or
125 // getopt_long() in C.
126 while ((c = bc_opt_parse(&opts, bc_args_lopt)) != -1) {
132 // Barf if not allowed.
134 bc_verr(BC_ERR_FATAL_OPTION, "-e (--expression)");
136 // Add the expressions and set exit.
137 bc_args_exprs(opts.optarg);
138 vm.exit_exprs = (exit_exprs || vm.exit_exprs);
145 // Figure out if exiting on expressions is disabled.
146 if (!strcmp(opts.optarg, "-")) vm.no_exprs = true;
149 // Barf if not allowed.
151 bc_verr(BC_ERR_FATAL_OPTION, "-f (--file)");
153 // Add the expressions and set exit.
154 bc_args_file(opts.optarg);
155 vm.exit_exprs = (exit_exprs || vm.exit_exprs);
170 vm.flags |= BC_FLAG_I;
176 vm.flags |= BC_FLAG_Z;
188 vm.flags &= ~(BC_FLAG_P);
194 vm.flags &= ~(BC_FLAG_R);
202 vm.flags |= BC_FLAG_G;
209 vm.flags |= BC_FLAG_L;
216 vm.flags &= ~(BC_FLAG_Q);
222 bc_args_redefine(opts.optarg);
229 vm.flags |= BC_FLAG_S;
236 vm.flags |= BC_FLAG_W;
244 do_exit = version = true;
252 vm.flags |= DC_FLAG_X;
258 // We shouldn't get here because bc_opt_error()/bc_error() should
271 if (version) bc_vm_info(NULL);
273 vm.status = (sig_atomic_t) BC_STATUS_QUIT;
277 // We do not print the banner if expressions are used or dc is used.
278 if (!BC_IS_BC || vm.exprs.len > 1) vm.flags &= ~(BC_FLAG_Q);
280 // We need to make sure the files list is initialized. We don't want to
281 // initialize it if there are no files because it's just a waste of memory.
282 if (opts.optind < (size_t) argc && vm.files.v == NULL)
283 bc_vec_init(&vm.files, sizeof(char*), BC_DTOR_NONE);
285 // Add all the files to the vector.
286 for (i = opts.optind; i < (size_t) argc; ++i)
287 bc_vec_push(&vm.files, argv + i);