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.
49 static const BcOptLong bc_args_lopt[] = {
51 { "expression", BC_OPT_REQUIRED, 'e' },
52 { "file", BC_OPT_REQUIRED, 'f' },
53 { "help", BC_OPT_NONE, 'h' },
54 { "interactive", BC_OPT_NONE, 'i' },
55 { "no-prompt", BC_OPT_NONE, 'P' },
57 { "global-stacks", BC_OPT_BC_ONLY, 'g' },
58 { "mathlib", BC_OPT_BC_ONLY, 'l' },
59 { "quiet", BC_OPT_BC_ONLY, 'q' },
60 { "standard", BC_OPT_BC_ONLY, 's' },
61 { "warn", BC_OPT_BC_ONLY, 'w' },
63 { "version", BC_OPT_NONE, 'v' },
64 { "version", BC_OPT_NONE, 'V' },
66 { "extended-register", BC_OPT_DC_ONLY, 'x' },
72 static void bc_args_exprs(const char *str) {
74 if (vm.exprs.v == NULL) bc_vec_init(&vm.exprs, sizeof(uchar), NULL);
75 bc_vec_concat(&vm.exprs, str);
76 bc_vec_concat(&vm.exprs, "\n");
79 static void bc_args_file(const char *file) {
87 bc_read_file(file, &buf);
92 void bc_args(int argc, char *argv[], bool exit_exprs) {
96 bool do_exit = false, version = false;
101 bc_opt_init(&opts, argv);
103 while ((c = bc_opt_parse(&opts, bc_args_lopt)) != -1) {
109 if (vm.no_exit_exprs)
110 bc_vm_verr(BC_ERR_FATAL_OPTION, "-e (--expression)");
111 bc_args_exprs(opts.optarg);
112 vm.exit_exprs = (exit_exprs || vm.exit_exprs);
118 if (!strcmp(opts.optarg, "-")) vm.no_exit_exprs = true;
120 if (vm.no_exit_exprs)
121 bc_vm_verr(BC_ERR_FATAL_OPTION, "-f (--file)");
122 bc_args_file(opts.optarg);
123 vm.exit_exprs = (exit_exprs || vm.exit_exprs);
137 vm.flags |= BC_FLAG_I;
143 vm.flags |= BC_FLAG_P;
151 vm.flags |= BC_FLAG_G;
158 vm.flags |= BC_FLAG_L;
172 vm.flags |= BC_FLAG_S;
179 vm.flags |= BC_FLAG_W;
187 do_exit = version = true;
195 vm.flags |= DC_FLAG_X;
201 // We shouldn't get here because bc_opt_error()/bc_vm_error() should
213 if (version) bc_vm_info(NULL);
214 if (do_exit) exit((int) vm.status);
216 if (opts.optind < (size_t) argc && vm.files.v == NULL)
217 bc_vec_init(&vm.files, sizeof(char*), NULL);
219 for (i = opts.optind; i < (size_t) argc; ++i)
220 bc_vec_push(&vm.files, argv + i);