/*1* *****************************************************************************2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2018-2025 Gavin D. Howard and contributors.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are met:9*10* * Redistributions of source code must retain the above copyright notice, this11* list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright notice,14* this list of conditions and the following disclaimer in the documentation15* and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*29* *****************************************************************************30*31* Code for processing command-line arguments.32*33*/3435#include <assert.h>36#include <ctype.h>37#include <stdbool.h>38#include <stdlib.h>39#include <string.h>4041#ifndef _WIN3242#include <unistd.h>43#endif // _WIN324445#include <vector.h>46#include <read.h>47#include <args.h>48#include <opt.h>49#include <num.h>50#include <vm.h>5152/**53* Adds @a str to the list of expressions to execute later.54* @param str The string to add to the list of expressions.55*/56static void57bc_args_exprs(const char* str)58{59BC_SIG_ASSERT_LOCKED;6061if (vm->exprs.v == NULL)62{63bc_vec_init(&vm->exprs, sizeof(uchar), BC_DTOR_NONE);64}6566bc_vec_concat(&vm->exprs, str);67bc_vec_concat(&vm->exprs, "\n");68}6970/**71* Adds the contents of @a file to the list of expressions to execute later.72* @param file The name of the file whose contents should be added to the list73* of expressions to execute.74*/75static void76bc_args_file(const char* file)77{78char* buf;7980BC_SIG_ASSERT_LOCKED;8182vm->file = file;8384buf = bc_read_file(file);8586assert(buf != NULL);8788bc_args_exprs(buf);89free(buf);90}9192static BcBigDig93bc_args_builtin(const char* arg)94{95bool strvalid;96BcNum n;97BcBigDig res;9899strvalid = bc_num_strValid(arg);100101if (BC_ERR(!strvalid))102{103bc_verr(BC_ERR_FATAL_ARG, arg);104}105106bc_num_init(&n, 0);107108bc_num_parse(&n, arg, 10);109110res = bc_num_bigdig(&n);111112bc_num_free(&n);113114return res;115}116117#if BC_ENABLED118119/**120* Redefines a keyword, if it exists and is not a POSIX keyword. Otherwise, it121* throws a fatal error.122* @param keyword The keyword to redefine.123*/124static void125bc_args_redefine(const char* keyword)126{127size_t i;128129BC_SIG_ASSERT_LOCKED;130131for (i = 0; i < bc_lex_kws_len; ++i)132{133const BcLexKeyword* kw = bc_lex_kws + i;134135if (!strcmp(keyword, kw->name))136{137if (BC_LEX_KW_POSIX(kw)) break;138139vm->redefined_kws[i] = true;140141return;142}143}144145bc_error(BC_ERR_FATAL_ARG, 0, keyword);146}147148#endif // BC_ENABLED149150void151bc_args(int argc, const char* argv[], bool exit_exprs, BcBigDig* scale,152BcBigDig* ibase, BcBigDig* obase)153{154int c;155size_t i;156bool do_exit = false, version = false;157BcOpt opts;158#if BC_ENABLE_EXTRA_MATH159const char* seed = NULL;160#endif // BC_ENABLE_EXTRA_MATH161162BC_SIG_ASSERT_LOCKED;163164bc_opt_init(&opts, argv);165166// This loop should look familiar to anyone who has used getopt() or167// getopt_long() in C.168while ((c = bc_opt_parse(&opts, bc_args_lopt)) != -1)169{170switch (c)171{172case 'c':173{174vm->flags |= BC_FLAG_DIGIT_CLAMP;175break;176}177178case 'C':179{180vm->flags &= ~BC_FLAG_DIGIT_CLAMP;181break;182}183184case 'e':185{186// Barf if not allowed.187if (vm->no_exprs)188{189bc_verr(BC_ERR_FATAL_OPTION, "-e (--expression)");190}191192// Add the expressions and set exit.193bc_args_exprs(opts.optarg);194vm->exit_exprs = (exit_exprs || vm->exit_exprs);195196break;197}198199case 'f':200{201// Figure out if exiting on expressions is disabled.202if (!strcmp(opts.optarg, "-")) vm->no_exprs = true;203else204{205// Barf if not allowed.206if (vm->no_exprs)207{208bc_verr(BC_ERR_FATAL_OPTION, "-f (--file)");209}210211// Add the expressions and set exit.212bc_args_file(opts.optarg);213vm->exit_exprs = (exit_exprs || vm->exit_exprs);214}215216break;217}218219case 'h':220{221bc_vm_info(vm->help);222do_exit = true;223break;224}225226case 'i':227{228vm->flags |= BC_FLAG_I;229break;230}231232case 'I':233{234*ibase = bc_args_builtin(opts.optarg);235break;236}237238case 'z':239{240vm->flags |= BC_FLAG_Z;241break;242}243244case 'L':245{246vm->line_len = 0;247break;248}249250case 'O':251{252*obase = bc_args_builtin(opts.optarg);253break;254}255256case 'P':257{258vm->flags &= ~(BC_FLAG_P);259break;260}261262case 'R':263{264vm->flags &= ~(BC_FLAG_R);265break;266}267268case 'S':269{270*scale = bc_args_builtin(opts.optarg);271break;272}273274#if BC_ENABLE_EXTRA_MATH275case 'E':276{277if (BC_ERR(!bc_num_strValid(opts.optarg)))278{279bc_verr(BC_ERR_FATAL_ARG, opts.optarg);280}281282seed = opts.optarg;283284break;285}286#endif // BC_ENABLE_EXTRA_MATH287288#if BC_ENABLED289case 'g':290{291assert(BC_IS_BC);292vm->flags |= BC_FLAG_G;293break;294}295296case 'l':297{298assert(BC_IS_BC);299vm->flags |= BC_FLAG_L;300break;301}302303case 'q':304{305assert(BC_IS_BC);306vm->flags &= ~(BC_FLAG_Q);307break;308}309310case 'r':311{312bc_args_redefine(opts.optarg);313break;314}315316case 's':317{318assert(BC_IS_BC);319vm->flags |= BC_FLAG_S;320break;321}322323case 'w':324{325assert(BC_IS_BC);326vm->flags |= BC_FLAG_W;327break;328}329#endif // BC_ENABLED330331case 'V':332case 'v':333{334do_exit = version = true;335break;336}337338#if DC_ENABLED339case 'x':340{341assert(BC_IS_DC);342vm->flags |= DC_FLAG_X;343break;344}345#endif // DC_ENABLED346347#if BC_DEBUG348// We shouldn't get here because bc_opt_error()/bc_error() should349// longjmp() out.350case '?':351case ':':352default:353{354BC_UNREACHABLE355#if !BC_CLANG356abort();357#endif // !BC_CLANG358}359#endif // BC_DEBUG360}361}362363if (version) bc_vm_info(NULL);364if (do_exit)365{366vm->status = (sig_atomic_t) BC_STATUS_QUIT;367BC_JMP;368}369370// We do not print the banner if expressions are used or dc is used.371if (BC_ARGS_SHOULD_BE_QUIET) vm->flags &= ~(BC_FLAG_Q);372373// We need to make sure the files list is initialized. We don't want to374// initialize it if there are no files because it's just a waste of memory.375if (opts.optind < (size_t) argc && vm->files.v == NULL)376{377bc_vec_init(&vm->files, sizeof(char*), BC_DTOR_NONE);378}379380// Add all the files to the vector.381for (i = opts.optind; i < (size_t) argc; ++i)382{383bc_vec_push(&vm->files, argv + i);384}385386#if BC_ENABLE_EXTRA_MATH387if (seed != NULL)388{389BcNum n;390391bc_num_init(&n, strlen(seed));392393BC_SIG_UNLOCK;394395bc_num_parse(&n, seed, BC_BASE);396397bc_program_assignSeed(&vm->prog, &n);398399BC_SIG_LOCK;400401bc_num_free(&n);402}403#endif // BC_ENABLE_EXTRA_MATH404}405406407