diff --git a/contrib/bc/.gitignore b/contrib/bc/.gitignore deleted file mode 100644 index 31e43aa61efc..000000000000 --- a/contrib/bc/.gitignore +++ /dev/null @@ -1,80 +0,0 @@ -*.config -*.creator -*.files -*.includes -*.creator.user* -*.cflags -*.cxxflags -bin/*bc -bin/*bc.exe -bin/*dc -bin/*dc.exe -bin/bcl -bc.old -*.o -*.a -.log_*.txt -.test.txt -.math.txt -.results.txt -.ops.txt -manuals/bc.1 -manuals/bc.1.ronn -manuals/bc.1.md -manuals/dc.1 -manuals/dc.1.ronn -manuals/dc.1.md -gen/strgen -lib.c -lib2.c -lib3.c -bc_help.c -dc_help.c -config.mak -timeconst.bc -Makefile - -tests/fuzzing/bc_outputs1/* -tests/fuzzing/bc_outputs2/* -tests/fuzzing/bc_outputs3/* -tests/fuzzing/dc_outputs/* -tests/bc_outputs/* -tests/dc_outputs/* - -.gdb_history - -# Ignore the generated test files -parse.txt -parse_results.txt -print.txt -print_results.txt -bessel.txt -bessel_results.txt -prime.txt -strings2.txt -strings2_results.txt -tests/bc/scripts/add.txt -tests/bc/scripts/divide.txt -tests/bc/scripts/multiply.txt -tests/bc/scripts/subtract.txt -tests/bc/scripts/strings2.txt -benchmarks/bc/*.txt -benchmarks/dc/*.txt -scripts/ministat -scripts/bitgen -perf.data -perf.data.old -*.gcda -*.gcno -*.gcov -*.html -*.profraw - -core.* - -cscope*.out -tags - -*.vcxproj.user -Debug/* -Release/* diff --git a/contrib/bc/LICENSE.md b/contrib/bc/LICENSE.md index 8ab2e6069881..8e6b7e0b9051 100644 --- a/contrib/bc/LICENSE.md +++ b/contrib/bc/LICENSE.md @@ -1,113 +1,113 @@ # License -Copyright (c) 2018-2021 Gavin D. Howard +Copyright (c) 2018-2023 Gavin D. Howard Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ## History The files `src/history.c` and `include/history.h` are under the following copyrights and license: Copyright (c) 2010-2014, Salvatore Sanfilippo
Copyright (c) 2010-2013, Pieter Noordhuis
Copyright (c) 2018 rain-1
-Copyright (c) 2018-2021, Gavin D. Howard +Copyright (c) 2018-2023, Gavin D. Howard Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ## Rand The files `src/rand.c` and `include/rand.h` are under the following copyrights and license: Copyright (c) 2014-2017 Melissa O'Neill and PCG Project contributors -Copyright (c) 2018-2021 Gavin D. Howard +Copyright (c) 2018-2023 Gavin D. Howard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ## `scripts/safe-install.sh` The file `scripts/safe-install.sh` is under the following copyright and license: Copyright (c) 2021 Rich Felker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ## `scripts/ministat.c` The file `scripts/ministat.c` is under the following license: "THE BEER-WARE LICENSE" (Revision 42): wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp diff --git a/contrib/bc/MEMORY_BUGS.md b/contrib/bc/MEMORY_BUGS.md new file mode 100644 index 000000000000..d675b28b342a --- /dev/null +++ b/contrib/bc/MEMORY_BUGS.md @@ -0,0 +1,53 @@ +# Memory Bugs + +This is a list of all of the memory bugs that were found in *released* versions +of `bc`, `dc`, or `bcl`. (Non-released commits with memory bugs do not count.) + +I made this list for two reasons: first, so users can know what versions of +`bc`, `dc`, and `bcl` have vulnerabilities, and two, I once had a perfect record +and then found a couple, but forgot and claimed I still had a perfect record +right after, which was embarrassing. + +This list is sorted by the first version a bug exists in, not the last it +existed in. + +* In versions `1.1.0` until `6.2.0` (inclusive) of `bc` and `dc`, there is a + out of bounds read and write in history when pressing ctrl+r (or any other + unused letter) then inserting two characters. + + The first version without this bug is `6.2.1`. + +* In versions `3.0.0` until `6.0.1` (inclusive) of `bc` and `dc`, there is a + double-free on `SIGINT` when using command-line expressions with `-e` and + `-f`. This was caused by not properly ending a jump series. + + The first version without this bug is `6.0.2`. + +* In versions `5.0.0` until `6.0.4` (inclusive) of `bc`, there is an + out-of-bounds access if a non-local (non-`auto`) variable is set to a string + with `asciify()`, then the function is redefined with a use of the same + non-local variable. + + This happened because strings were stored per-function, and the non-local + variable now had a reference to the string in the old function, which could be + at a higher index than exists in the new function. Strings are stored globally + now, and they are *not* freed once not used. + + The first version without this bug is `6.1.0`. + +* In versions `5.0.0` until `6.0.4` (inclusive) of `bc`, there is another + out-of-bounds access if an array is passed to the `asciify()` built-in + function as the only argument. This happened because arrays are allowed as + function arguments, which allowed them to be used as arguments to `asciify()`, + but they should not have been allowed. However, since they were, the + `asciify()` code tried to access an argument that was not there. + + The first version without this bug is `6.1.0`. + +* In version `6.0.0` of `bcl`, there are several uses of initialized data that + have the same root cause: I forgot to call `memset()` on the per-thread global + data. This is because the data used to be *actually* global, which meant that + it was initialized to zero by the system. This happened because I thought I + had properly hooked Valgrind into my `bcl` tests, but I had not. + + The first version without this bug is `6.0.1`. diff --git a/contrib/bc/Makefile.in b/contrib/bc/Makefile.in index b9136a57aa92..83417a333826 100644 --- a/contrib/bc/Makefile.in +++ b/contrib/bc/Makefile.in @@ -1,643 +1,642 @@ # # SPDX-License-Identifier: BSD-2-Clause # -# Copyright (c) 2018-2021 Gavin D. Howard and contributors. +# Copyright (c) 2018-2023 Gavin D. Howard and contributors. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # %%WARNING%% # .POSIX: ROOTDIR = %%ROOTDIR%% INCDIR = $(ROOTDIR)/include SRCDIR = $(ROOTDIR)/src TESTSDIR = $(ROOTDIR)/tests SCRIPTSDIR = $(ROOTDIR)/scripts GENDIR = $(ROOTDIR)/gen BUILDDIR = %%BUILDDIR%% SRC = %%SRC%% OBJ = %%OBJ%% GCDA = %%GCDA%% GCNO = %%GCNO%% BC_ENABLED_NAME = BC_ENABLED BC_ENABLED = %%BC_ENABLED%% DC_ENABLED_NAME = DC_ENABLED DC_ENABLED = %%DC_ENABLED%% HEADERS = $(INCDIR)/args.h $(INCDIR)/file.h $(INCDIR)/lang.h $(INCDIR)/lex.h $(INCDIR)/num.h $(INCDIR)/opt.h $(INCDIR)/parse.h $(INCDIR)/program.h $(INCDIR)/read.h $(INCDIR)/status.h $(INCDIR)/vector.h $(INCDIR)/vm.h BC_HEADERS = $(INCDIR)/bc.h DC_HEADERS = $(INCDIR)/dc.h HISTORY_HEADERS = $(INCDIR)/history.h EXTRA_MATH_HEADERS = $(INCDIR)/rand.h LIBRARY_HEADERS = $(INCDIR)/bcl.h $(INCDIR)/library.h -GEN_DIR = gen +GEN_DIR = %%GEN_DIR%% GEN = %%GEN%% GEN_EXEC = $(GEN_DIR)/$(GEN) GEN_C = $(GENDIR)/$(GEN).c GEN_EMU = %%GEN_EMU%% BC_LIB = $(GENDIR)/lib.bc BC_LIB_C = $(GEN_DIR)/lib.c BC_LIB_O = %%BC_LIB_O%% BC_LIB_GCDA = $(GEN_DIR)/lib.gcda BC_LIB_GCNO = $(GEN_DIR)/lib.gcno BC_LIB2 = $(GENDIR)/lib2.bc BC_LIB2_C = $(GEN_DIR)/lib2.c BC_LIB2_O = %%BC_LIB2_O%% BC_LIB2_GCDA = $(GEN_DIR)/lib2.gcda BC_LIB2_GCNO = $(GEN_DIR)/lib2.gcno BC_HELP = $(GENDIR)/bc_help.txt BC_HELP_C = $(GEN_DIR)/bc_help.c BC_HELP_O = %%BC_HELP_O%% BC_HELP_GCDA = $(GEN_DIR)/bc_help.gcda BC_HELP_GCNO = $(GEN_DIR)/bc_help.gcno DC_HELP = $(GENDIR)/dc_help.txt DC_HELP_C = $(GEN_DIR)/dc_help.c DC_HELP_O = %%DC_HELP_O%% DC_HELP_GCDA = $(GEN_DIR)/dc_help.gcda DC_HELP_GCNO = $(GEN_DIR)/dc_help.gcno BIN = bin EXEC_SUFFIX = %%EXECSUFFIX%% EXEC_PREFIX = %%EXECPREFIX%% BC = bc DC = dc BC_EXEC = $(BIN)/$(EXEC_PREFIX)$(BC) DC_EXEC = $(BIN)/$(EXEC_PREFIX)$(DC) BC_TEST_OUTPUTS = tests/bc_outputs BC_FUZZ_OUTPUTS = tests/fuzzing/bc_outputs1 tests/fuzzing/bc_outputs2 tests/fuzzing/bc_outputs3 DC_TEST_OUTPUTS = tests/dc_outputs DC_FUZZ_OUTPUTS = tests/fuzzing/dc_outputs LIB = libbcl LIB_NAME = $(LIB).a LIBBC = $(BIN)/$(LIB_NAME) BCL = bcl BCL_TEST = $(BIN)/$(BCL) BCL_TEST_C = $(TESTSDIR)/$(BCL).c MANUALS = manuals BC_MANPAGE_NAME = $(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX).1 BC_MANPAGE = $(MANUALS)/$(BC).1 BC_MD = $(BC_MANPAGE).md DC_MANPAGE_NAME = $(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX).1 DC_MANPAGE = $(MANUALS)/$(DC).1 DC_MD = $(DC_MANPAGE).md BCL_MANPAGE_NAME = bcl.3 BCL_MANPAGE = $(MANUALS)/$(BCL_MANPAGE_NAME) BCL_MD = $(BCL_MANPAGE).md MANPAGE_INSTALL_ARGS = -Dm644 BINARY_INSTALL_ARGS = -Dm755 PC_INSTALL_ARGS = $(MANPAGE_INSTALL_ARGS) BCL_PC = $(BCL).pc PC_PATH = %%PC_PATH%% BCL_HEADER_NAME = bcl.h BCL_HEADER = $(INCDIR)/$(BCL_HEADER_NAME) %%DESTDIR%% BINDIR = %%BINDIR%% INCLUDEDIR = %%INCLUDEDIR%% LIBDIR = %%LIBDIR%% MAN1DIR = %%MAN1DIR%% MAN3DIR = %%MAN3DIR%% MAIN_EXEC = $(EXEC_PREFIX)$(%%MAIN_EXEC%%)$(EXEC_SUFFIX) EXEC = $(%%EXEC%%) NLSPATH = %%NLSPATH%% BC_BUILD_TYPE = %%BUILD_TYPE%% BC_ENABLE_LIBRARY = %%LIBRARY%% BC_ENABLE_HISTORY = %%HISTORY%% BC_ENABLE_EXTRA_MATH_NAME = BC_ENABLE_EXTRA_MATH BC_ENABLE_EXTRA_MATH = %%EXTRA_MATH%% BC_ENABLE_NLS = %%NLS%% -BC_LONG_BIT = %%LONG_BIT%% +BC_EXCLUDE_EXTRA_MATH = %%EXCLUDE_EXTRA_MATH%% BC_ENABLE_AFL = %%FUZZ%% BC_ENABLE_MEMCHECK = %%MEMCHECK%% BC_DEFAULT_BANNER = %%BC_DEFAULT_BANNER%% BC_DEFAULT_SIGINT_RESET = %%BC_DEFAULT_SIGINT_RESET%% DC_DEFAULT_SIGINT_RESET = %%DC_DEFAULT_SIGINT_RESET%% BC_DEFAULT_TTY_MODE = %%BC_DEFAULT_TTY_MODE%% DC_DEFAULT_TTY_MODE = %%DC_DEFAULT_TTY_MODE%% BC_DEFAULT_PROMPT = %%BC_DEFAULT_PROMPT%% DC_DEFAULT_PROMPT = %%DC_DEFAULT_PROMPT%% BC_DEFAULT_EXPR_EXIT = %%BC_DEFAULT_EXPR_EXIT%% DC_DEFAULT_EXPR_EXIT = %%DC_DEFAULT_EXPR_EXIT%% +BC_DEFAULT_DIGIT_CLAMP = %%BC_DEFAULT_DIGIT_CLAMP%% +DC_DEFAULT_DIGIT_CLAMP = %%DC_DEFAULT_DIGIT_CLAMP%% RM = rm MKDIR = mkdir SCRIPTS = ./scripts MINISTAT = ministat MINISTAT_EXEC = $(SCRIPTS)/$(MINISTAT) BITFUNCGEN = bitfuncgen BITFUNCGEN_EXEC = $(SCRIPTS)/$(BITFUNCGEN) INSTALL = $(SCRIPTSDIR)/exec-install.sh SAFE_INSTALL = $(SCRIPTSDIR)/safe-install.sh LINK = $(SCRIPTSDIR)/link.sh MANPAGE = $(SCRIPTSDIR)/manpage.sh KARATSUBA = $(SCRIPTSDIR)/karatsuba.py LOCALE_INSTALL = $(SCRIPTSDIR)/locale_install.sh LOCALE_UNINSTALL = $(SCRIPTSDIR)/locale_uninstall.sh VALGRIND_ARGS = --error-exitcode=100 --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all TEST_STARS = *********************************************************************** BC_NUM_KARATSUBA_LEN = %%KARATSUBA_LEN%% BC_DEFS0 = -DBC_DEFAULT_BANNER=$(BC_DEFAULT_BANNER) BC_DEFS1 = -DBC_DEFAULT_SIGINT_RESET=$(BC_DEFAULT_SIGINT_RESET) BC_DEFS2 = -DBC_DEFAULT_TTY_MODE=$(BC_DEFAULT_TTY_MODE) BC_DEFS3 = -DBC_DEFAULT_PROMPT=$(BC_DEFAULT_PROMPT) BC_DEFS4 = -DBC_DEFAULT_EXPR_EXIT=$(BC_DEFAULT_EXPR_EXIT) -BC_DEFS = $(BC_DEFS0) $(BC_DEFS1) $(BC_DEFS2) $(BC_DEFS3) $(BC_DEFS4) +BC_DEFS5 = -DBC_DEFAULT_DIGIT_CLAMP=$(BC_DEFAULT_DIGIT_CLAMP) +BC_DEFS = $(BC_DEFS0) $(BC_DEFS1) $(BC_DEFS2) $(BC_DEFS3) $(BC_DEFS4) $(BC_DEFS5) DC_DEFS1 = -DDC_DEFAULT_SIGINT_RESET=$(DC_DEFAULT_SIGINT_RESET) DC_DEFS2 = -DDC_DEFAULT_TTY_MODE=$(DC_DEFAULT_TTY_MODE) DC_DEFS3 = -DDC_DEFAULT_PROMPT=$(DC_DEFAULT_PROMPT) DC_DEFS4 = -DDC_DEFAULT_EXPR_EXIT=$(DC_DEFAULT_EXPR_EXIT) -DC_DEFS = $(DC_DEFS1) $(DC_DEFS2) $(DC_DEFS3) $(DC_DEFS4) +DC_DEFS5 = -DDC_DEFAULT_DIGIT_CLAMP=$(DC_DEFAULT_DIGIT_CLAMP) +DC_DEFS = $(DC_DEFS1) $(DC_DEFS2) $(DC_DEFS3) $(DC_DEFS4) $(DC_DEFS5) CPPFLAGS1 = -D$(BC_ENABLED_NAME)=$(BC_ENABLED) -D$(DC_ENABLED_NAME)=$(DC_ENABLED) CPPFLAGS2 = $(CPPFLAGS1) -I$(INCDIR)/ -DBUILD_TYPE=$(BC_BUILD_TYPE) %%LONG_BIT_DEFINE%% CPPFLAGS3 = $(CPPFLAGS2) -DEXECPREFIX=$(EXEC_PREFIX) -DMAINEXEC=$(MAIN_EXEC) -CPPFLAGS4 = $(CPPFLAGS3) -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 %%BSD%% +CPPFLAGS4 = $(CPPFLAGS3) %%BSD%% CPPFLAGS5 = $(CPPFLAGS4) -DBC_NUM_KARATSUBA_LEN=$(BC_NUM_KARATSUBA_LEN) CPPFLAGS6 = $(CPPFLAGS5) -DBC_ENABLE_NLS=$(BC_ENABLE_NLS) CPPFLAGS7 = $(CPPFLAGS6) -D$(BC_ENABLE_EXTRA_MATH_NAME)=$(BC_ENABLE_EXTRA_MATH) CPPFLAGS8 = $(CPPFLAGS7) -DBC_ENABLE_HISTORY=$(BC_ENABLE_HISTORY) -DBC_ENABLE_LIBRARY=$(BC_ENABLE_LIBRARY) CPPFLAGS = $(CPPFLAGS8) -DBC_ENABLE_MEMCHECK=$(BC_ENABLE_MEMCHECK) -DBC_ENABLE_AFL=$(BC_ENABLE_AFL) CFLAGS = $(CPPFLAGS) $(BC_DEFS) $(DC_DEFS) %%CPPFLAGS%% %%CFLAGS%% LDFLAGS = %%LDFLAGS%% HOSTCFLAGS = %%HOSTCFLAGS%% CC = %%CC%% HOSTCC = %%HOSTCC%% BC_LIB_C_ARGS = bc_lib bc_lib_name $(BC_ENABLED_NAME) 1 BC_LIB2_C_ARGS = bc_lib2 bc_lib2_name "$(BC_ENABLED_NAME) && $(BC_ENABLE_EXTRA_MATH_NAME)" 1 OBJS = $(DC_HELP_O) $(BC_HELP_O) $(BC_LIB_O) $(BC_LIB2_O) $(OBJ) all: %%DEFAULT_TARGET%% %%DEFAULT_TARGET%%: %%DEFAULT_TARGET_PREREQS%% %%DEFAULT_TARGET_CMD%% %%SECOND_TARGET%%: %%SECOND_TARGET_PREREQS%% %%SECOND_TARGET_CMD%% $(GEN_DIR): mkdir -p $(GEN_DIR) $(GEN_EXEC): $(GEN_DIR) %%GEN_EXEC_TARGET%% $(BC_LIB_C): $(GEN_EXEC) $(BC_LIB) - $(GEN_EMU) $(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) $(BC_LIB_C_ARGS) + $(GEN_EMU) $(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) $(BC_EXCLUDE_EXTRA_MATH) $(BC_LIB_C_ARGS) $(BC_LIB_O): $(BC_LIB_C) $(CC) $(CFLAGS) -o $@ -c $< $(BC_LIB2_C): $(GEN_EXEC) $(BC_LIB2) - $(GEN_EMU) $(GEN_EXEC) $(BC_LIB2) $(BC_LIB2_C) $(BC_LIB2_C_ARGS) + $(GEN_EMU) $(GEN_EXEC) $(BC_LIB2) $(BC_LIB2_C) $(BC_EXCLUDE_EXTRA_MATH) $(BC_LIB2_C_ARGS) $(BC_LIB2_O): $(BC_LIB2_C) $(CC) $(CFLAGS) -o $@ -c $< $(BC_HELP_C): $(GEN_EXEC) $(BC_HELP) - $(GEN_EMU) $(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) bc_help "" $(BC_ENABLED_NAME) + $(GEN_EMU) $(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) $(BC_EXCLUDE_EXTRA_MATH) bc_help "" $(BC_ENABLED_NAME) $(BC_HELP_O): $(BC_HELP_C) $(CC) $(CFLAGS) -o $@ -c $< $(DC_HELP_C): $(GEN_EXEC) $(DC_HELP) - $(GEN_EMU) $(GEN_EXEC) $(DC_HELP) $(DC_HELP_C) dc_help "" $(DC_ENABLED_NAME) + $(GEN_EMU) $(GEN_EXEC) $(DC_HELP) $(DC_HELP_C) $(BC_EXCLUDE_EXTRA_MATH) dc_help "" $(DC_ENABLED_NAME) $(DC_HELP_O): $(DC_HELP_C) $(CC) $(CFLAGS) -o $@ -c $< $(BIN): $(MKDIR) -p $(BIN) src: $(MKDIR) -p src headers: %%HEADERS%% $(MINISTAT): mkdir -p $(SCRIPTS) $(HOSTCC) $(HOSTCFLAGS) -lm -o $(MINISTAT_EXEC) $(ROOTDIR)/scripts/ministat.c $(BITFUNCGEN): mkdir -p $(SCRIPTS) $(HOSTCC) $(HOSTCFLAGS) -lm -o $(BITFUNCGEN_EXEC) $(ROOTDIR)/scripts/bitfuncgen.c help: @printf 'available targets:\n' @printf '\n' @printf ' all (default) builds %%EXECUTABLES%%\n' @printf ' check alias for `make test`\n' @printf ' clean removes all build files\n' @printf ' clean_config removes all build files as well as the generated Makefile\n' @printf ' clean_tests removes all build files, the generated Makefile,\n' @printf ' and generated tests\n' @printf ' install installs binaries to "%s%s"\n' "$(DESTDIR)" "$(BINDIR)" @printf ' and (if enabled) manpages to "%s%s"\n' "$(DESTDIR)" "$(MAN1DIR)" @printf ' karatsuba runs the karatsuba script (requires Python 3)\n' @printf ' karatsuba_test runs the karatsuba script while running tests\n' @printf ' (requires Python 3)\n' @printf ' uninstall uninstalls binaries from "%s%s"\n' "$(DESTDIR)" "$(BINDIR)" @printf ' and (if enabled) manpages from "%s%s"\n' "$(DESTDIR)" "$(MAN1DIR)" @printf ' test runs the test suite\n' @printf ' test_bc runs the bc test suite, if bc has been built\n' @printf ' test_dc runs the dc test suite, if dc has been built\n' @printf ' time_test runs the test suite, displaying times for some things\n' @printf ' time_test_bc runs the bc test suite, displaying times for some things\n' @printf ' time_test_dc runs the dc test suite, displaying times for some things\n' @printf ' timeconst runs the test on the Linux timeconst.bc script,\n' @printf ' if it exists and bc has been built\n' - @printf ' valgrind runs the test suite through valgrind\n' - @printf ' valgrind_bc runs the bc test suite, if bc has been built,\n' - @printf ' through valgrind\n' - @printf ' valgrind_dc runs the dc test suite, if dc has been built,\n' - @printf ' through valgrind\n' run_all_tests: bc_all_tests timeconst_all_tests dc_all_tests run_all_tests_np: bc_all_tests_np timeconst_all_tests dc_all_tests_np bc_all_tests: %%BC_ALL_TESTS%% bc_all_tests_np: %%BC_ALL_TESTS_NP%% timeconst_all_tests: %%TIMECONST_ALL_TESTS%% dc_all_tests: %%DC_ALL_TESTS%% dc_all_tests_np: %%DC_ALL_TESTS_NP%% history_all_tests: %%HISTORY_TESTS%% check: test test: %%TESTS%% test_bc: test_bc_header test_bc_tests test_bc_scripts test_bc_errors test_bc_stdin test_bc_read test_bc_other @printf '\nAll bc tests passed.\n\n$(TEST_STARS)\n' test_bc_tests:%%BC_TESTS%% test_bc_scripts:%%BC_SCRIPT_TESTS%% test_bc_stdin: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/stdin.sh bc %%BC_TEST_EXEC%% test_bc_read: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/read.sh bc %%BC_TEST_EXEC%% test_bc_errors: test_bc_error_lines%%BC_ERROR_TESTS%% test_bc_error_lines: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/errors.sh bc %%BC_TEST_EXEC%% test_bc_other: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/other.sh bc $(BC_ENABLE_EXTRA_MATH) %%BC_TEST_EXEC%% test_bc_header: @printf '$(TEST_STARS)\n\nRunning bc tests...\n\n' test_dc: test_dc_header test_dc_tests test_dc_scripts test_dc_errors test_dc_stdin test_dc_read test_dc_other @printf '\nAll dc tests passed.\n\n$(TEST_STARS)\n' test_dc_tests:%%DC_TESTS%% test_dc_scripts:%%DC_SCRIPT_TESTS%% test_dc_stdin: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/stdin.sh dc %%DC_TEST_EXEC%% test_dc_read: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/read.sh dc %%DC_TEST_EXEC%% test_dc_errors: test_dc_error_lines%%DC_ERROR_TESTS%% test_dc_error_lines: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/errors.sh dc %%DC_TEST_EXEC%% test_dc_other: @export BC_TEST_OUTPUT_DIR="$(BUILDDIR)/tests"; sh $(TESTSDIR)/other.sh dc $(BC_ENABLE_EXTRA_MATH) %%DC_TEST_EXEC%% test_dc_header: @printf '$(TEST_STARS)\n\nRunning dc tests...\n\n' timeconst: %%TIMECONST%% test_history: test_history_header test_bc_history test_dc_history @printf '\nAll history tests passed.\n\n$(TEST_STARS)\n' test_bc_history:%%BC_HISTORY_TEST_PREREQS%% test_bc_history_all: test_bc_history0 test_bc_history1 test_bc_history2 test_bc_history3 test_bc_history4 test_bc_history5 test_bc_history6 test_bc_history7 test_bc_history8 test_bc_history9 test_bc_history10 test_bc_history11 test_bc_history12 test_bc_history13 test_bc_history14 test_bc_history15 test_bc_history16 test_bc_history17 test_bc_history18 test_bc_history19 test_bc_history20 test_bc_history21 test_bc_history_skip: @printf 'No bc history tests to run\n' test_bc_history0: @sh $(TESTSDIR)/history.sh bc 0 %%BC_TEST_EXEC%% test_bc_history1: @sh $(TESTSDIR)/history.sh bc 1 %%BC_TEST_EXEC%% test_bc_history2: @sh $(TESTSDIR)/history.sh bc 2 %%BC_TEST_EXEC%% test_bc_history3: @sh $(TESTSDIR)/history.sh bc 3 %%BC_TEST_EXEC%% test_bc_history4: @sh $(TESTSDIR)/history.sh bc 4 %%BC_TEST_EXEC%% test_bc_history5: @sh $(TESTSDIR)/history.sh bc 5 %%BC_TEST_EXEC%% test_bc_history6: @sh $(TESTSDIR)/history.sh bc 6 %%BC_TEST_EXEC%% test_bc_history7: @sh $(TESTSDIR)/history.sh bc 7 %%BC_TEST_EXEC%% test_bc_history8: @sh $(TESTSDIR)/history.sh bc 8 %%BC_TEST_EXEC%% test_bc_history9: @sh $(TESTSDIR)/history.sh bc 9 %%BC_TEST_EXEC%% test_bc_history10: @sh $(TESTSDIR)/history.sh bc 10 %%BC_TEST_EXEC%% test_bc_history11: @sh $(TESTSDIR)/history.sh bc 11 %%BC_TEST_EXEC%% test_bc_history12: @sh $(TESTSDIR)/history.sh bc 12 %%BC_TEST_EXEC%% test_bc_history13: @sh $(TESTSDIR)/history.sh bc 13 %%BC_TEST_EXEC%% test_bc_history14: @sh $(TESTSDIR)/history.sh bc 14 %%BC_TEST_EXEC%% test_bc_history15: @sh $(TESTSDIR)/history.sh bc 15 %%BC_TEST_EXEC%% test_bc_history16: @sh $(TESTSDIR)/history.sh bc 16 %%BC_TEST_EXEC%% test_bc_history17: @sh $(TESTSDIR)/history.sh bc 17 %%BC_TEST_EXEC%% test_bc_history18: @sh $(TESTSDIR)/history.sh bc 18 %%BC_TEST_EXEC%% test_bc_history19: @sh $(TESTSDIR)/history.sh bc 19 %%BC_TEST_EXEC%% test_bc_history20: @sh $(TESTSDIR)/history.sh bc 20 %%BC_TEST_EXEC%% test_bc_history21: @sh $(TESTSDIR)/history.sh bc 21 %%BC_TEST_EXEC%% test_dc_history:%%DC_HISTORY_TEST_PREREQS%% test_dc_history_all: test_dc_history0 test_dc_history1 test_dc_history2 test_dc_history3 test_dc_history4 test_dc_history5 test_dc_history6 test_dc_history7 test_dc_history8 test_dc_history9 test_dc_history10 test_dc_history_skip: @printf 'No dc history tests to run\n' test_dc_history0: @sh $(TESTSDIR)/history.sh dc 0 %%DC_TEST_EXEC%% test_dc_history1: @sh $(TESTSDIR)/history.sh dc 1 %%DC_TEST_EXEC%% test_dc_history2: @sh $(TESTSDIR)/history.sh dc 2 %%DC_TEST_EXEC%% test_dc_history3: @sh $(TESTSDIR)/history.sh dc 3 %%DC_TEST_EXEC%% test_dc_history4: @sh $(TESTSDIR)/history.sh dc 4 %%DC_TEST_EXEC%% test_dc_history5: @sh $(TESTSDIR)/history.sh dc 5 %%DC_TEST_EXEC%% test_dc_history6: @sh $(TESTSDIR)/history.sh dc 6 %%DC_TEST_EXEC%% test_dc_history7: @sh $(TESTSDIR)/history.sh dc 7 %%DC_TEST_EXEC%% test_dc_history8: @sh $(TESTSDIR)/history.sh dc 8 %%DC_TEST_EXEC%% test_dc_history9: @sh $(TESTSDIR)/history.sh dc 9 %%DC_TEST_EXEC%% test_dc_history10: @sh $(TESTSDIR)/history.sh dc 10 %%DC_TEST_EXEC%% test_history_header: @printf '$(TEST_STARS)\n\nRunning history tests...\n\n' library_test: $(LIBBC) - $(CC) $(CFLAGS) $(BCL_TEST_C) $(LIBBC) -o $(BCL_TEST) + $(CC) $(CFLAGS) -lpthread $(BCL_TEST_C) $(LIBBC) -o $(BCL_TEST) test_library: library_test - $(BCL_TEST) + %%BCL_TEST_EXEC%% karatsuba: %%KARATSUBA%% karatsuba_test: %%KARATSUBA_TEST%% coverage_output: %%COVERAGE_OUTPUT%% coverage:%%COVERAGE_PREREQS%% manpages: $(MANPAGE) bc $(MANPAGE) dc $(MANPAGE) bcl clean_gen: @$(RM) -f $(GEN_EXEC) clean:%%CLEAN_PREREQS%% @printf 'Cleaning files...\n' @$(RM) -f src/*.tmp gen/*.tmp @$(RM) -f $(OBJ) @$(RM) -f $(BC_EXEC) @$(RM) -f $(DC_EXEC) @$(RM) -fr $(BIN) @$(RM) -f $(BC_LIB_C) $(BC_LIB_O) @$(RM) -f $(BC_LIB2_C) $(BC_LIB2_O) @$(RM) -f $(BC_HELP_C) $(BC_HELP_O) @$(RM) -f $(DC_HELP_C) $(DC_HELP_O) @$(RM) -fr vs/bin/ vs/lib/ clean_benchmarks: @printf 'Cleaning benchmarks...\n' @$(RM) -f $(MINISTAT_EXEC) @$(RM) -f $(ROOTDIR)/benchmarks/bc/*.txt @$(RM) -f $(ROOTDIR)/benchmarks/dc/*.txt clean_config: clean clean_benchmarks @printf 'Cleaning config...\n' @$(RM) -f Makefile @$(RM) -f $(BC_MD) $(BC_MANPAGE) @$(RM) -f $(DC_MD) $(DC_MANPAGE) clean_coverage: @printf 'Cleaning coverage files...\n' @$(RM) -f *.gcov @$(RM) -f *.html @$(RM) -f *.gcda *.gcno @$(RM) -f *.profraw @$(RM) -f $(GCDA) $(GCNO) @$(RM) -f $(BC_GCDA) $(BC_GCNO) @$(RM) -f $(DC_GCDA) $(DC_GCNO) @$(RM) -f $(HISTORY_GCDA) $(HISTORY_GCNO) @$(RM) -f $(RAND_GCDA) $(RAND_GCNO) @$(RM) -f $(BC_LIB_GCDA) $(BC_LIB_GCNO) @$(RM) -f $(BC_LIB2_GCDA) $(BC_LIB2_GCNO) @$(RM) -f $(BC_HELP_GCDA) $(BC_HELP_GCNO) @$(RM) -f $(DC_HELP_GCDA) $(DC_HELP_GCNO) clean_tests: clean clean_config clean_coverage @printf 'Cleaning test files...\n' @$(RM) -fr $(BC_TEST_OUTPUTS) $(DC_TEST_OUTPUTS) @$(RM) -fr $(BC_FUZZ_OUTPUTS) $(DC_FUZZ_OUTPUTS) @$(RM) -f $(TESTSDIR)/bc/parse.txt $(TESTSDIR)/bc/parse_results.txt @$(RM) -f $(TESTSDIR)/bc/print.txt $(TESTSDIR)/bc/print_results.txt @$(RM) -f $(TESTSDIR)/bc/bessel.txt $(TESTSDIR)/bc/bessel_results.txt @$(RM) -f $(TESTSDIR)/bc/strings2.txt $(TESTSDIR)/bc/strings2_results.txt @$(RM) -f $(TESTSDIR)/bc/scripts/bessel.txt @$(RM) -f $(TESTSDIR)/bc/scripts/parse.txt @$(RM) -f $(TESTSDIR)/bc/scripts/print.txt @$(RM) -f $(TESTSDIR)/bc/scripts/add.txt @$(RM) -f $(TESTSDIR)/bc/scripts/divide.txt @$(RM) -f $(TESTSDIR)/bc/scripts/multiply.txt @$(RM) -f $(TESTSDIR)/bc/scripts/subtract.txt @$(RM) -f $(TESTSDIR)/bc/scripts/strings2.txt @$(RM) -f $(TESTSDIR)/dc/scripts/prime.txt @$(RM) -f .log_*.txt @$(RM) -f .math.txt .results.txt .ops.txt @$(RM) -f .test.txt @$(RM) -f tags .gdbbreakpoints .gdb_history .gdbsetup @$(RM) -f cscope.* @$(RM) -f bc.old @$(RM) -f $(BITFUNCGEN_EXEC) install_locales: %%INSTALL_LOCALES%% install_bc_manpage: $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME) install_dc_manpage: $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(DC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME) install_bcl_manpage: $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BCL_MANPAGE) $(DESTDIR)$(MAN3DIR)/$(BCL_MANPAGE_NAME) install_bcl_header: $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BCL_HEADER) $(DESTDIR)$(INCLUDEDIR)/$(BCL_HEADER_NAME) install_execs: $(INSTALL) $(DESTDIR)$(BINDIR) "$(EXEC_SUFFIX)" "$(BUILDDIR)/bin" install_library: install_bcl_header $(SAFE_INSTALL) $(BINARY_INSTALL_ARGS) $(LIBBC) $(DESTDIR)$(LIBDIR)/$(LIB_NAME) %%PKG_CONFIG_INSTALL%% install:%%INSTALL_LOCALES_PREREQS%%%%INSTALL_MAN_PREREQS%%%%INSTALL_PREREQS%% uninstall_locales: $(LOCALE_UNINSTALL) $(NLSPATH) $(MAIN_EXEC) $(DESTDIR) uninstall_bc_manpage: $(RM) -f $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME) uninstall_bc: $(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX) uninstall_dc_manpage: $(RM) -f $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME) uninstall_dc: $(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX) uninstall_library: uninstall_bcl_header $(RM) -f $(DESTDIR)$(LIBDIR)/$(LIB_NAME) %%PKG_CONFIG_UNINSTALL%% uninstall_bcl_header: $(RM) -f $(DESTDIR)$(INCLUDEDIR)/$(BCL_HEADER_NAME) uninstall_bcl_manpage: $(RM) -f $(DESTDIR)$(MAN3DIR)/$(BCL_MANPAGE_NAME) uninstall:%%UNINSTALL_LOCALES_PREREQS%%%%UNINSTALL_MAN_PREREQS%%%%UNINSTALL_PREREQS%% diff --git a/contrib/bc/NEWS.md b/contrib/bc/NEWS.md index 9a354e537d9f..36952fdaf582 100644 --- a/contrib/bc/NEWS.md +++ b/contrib/bc/NEWS.md @@ -1,1239 +1,1432 @@ # News +## 6.2.2 + +This is a production release that fixes a bug. + +The bug was that if an array element was used as a parameter, and then a later +parameter had the same name as the array whose element was used, `bc` would grab +the element from the new array parameter, not the actual element from before the +function call. + +## 6.2.1 + +This is a production release with one bug fix for a memory bug in history. + +## 6.2.0 + +This is a production release with a new feature and a few bug fixes. + +The bug fixes include: + +* A crash when `bc` and `dc` are built using editline, but history is not + activated. +* A missing local in the `uint*()` family of functions in the extended math + library. +* A failure to clear the tail call list in `dc` on error. +* A crash when attempting to swap characters in command-line history when no + characters exist. +* `SIGWINCH` was activated even when history was not. + +The new feature is that stack traces are now given for runtime errors. In debug +mode, the C source file and line of errors are given as well. + +## 6.1.1 + +This is a production release that fixes a build issue with predefined builds and +generated tests. + +## 6.1.0 + +This is a production release that fixes a discrepancy from the `bc` standard, +a couple of memory bugs, and adds new features. + +The discrepancy from the `bc` standard was with regards to the behavior of the +`quit` command. This `bc` used to quit whenever it encountered `quit` during +parsing, even if it was parsing a full file. Now, `bc` only quits when +encountering `quit` *after* it has executed all executable statements up to that +point. + +This behavior is slightly different from GNU `bc`, but users will only notice +the difference if they put `quit` on the same line as other statements. + +The first memory bug could be reproduced by assigning a string to a non-local +variable in a function, then redefining the function with use of the same +non-local variable, which would still refer to a string in the previous version +of the function. + +The second memory bug was caused by passing an array argument to the `asciify()` +built-in function. In certain cases, that was wrongly allowed, and the +interpreter just assumed everything was correct and accessed memory. Now that +arrays are allowed as arguments (see below), this is not an issue. + +The first feature was the addition of the `is_number()` built-in function (`u` +in `dc`) that returns 1 if the runtime argument is a number and 0 otherwise. + +The second feature was the addition of the `is_string()` built-in function (`t` +in `dc`) that returns 1 if the runtime argument is a string and 0 otherwise. + +These features were added because I realized that type-checking is necessary now +that strings can be assigned to variables in `bc` and because they've always +been assignable to variables in `dc`. + +The last added feature is the ability of the `asciify()` built-in function in +`bc` to convert a full array of numbers into a string. This means that +character-by-character printing will not be necessary, and more strings than +just single-character ones will be able to be created. + +## 6.0.4 + +This is a production release that most users will not need to upgrade to. + +This fixes a build bug for `bcl` only on OpenBSD. Users that do not need `bcl` +or have not run into build errors with `bcl` do ***NOT*** need to upgrade. + +## 6.0.3 + +This is a production release that fixes a build bug for cross-compilation. + +Users that do not need cross-compilation do ***NOT*** need to upgrade. + +## 6.0.2 + +This is a production release that fixes two bugs: + +* The `-l` option overrode the `-S` option. +* A double-free and crash when sending a `SIGINT` while executing expressions + given on the command-line. + +## 6.0.1 + +This is a production release that fixes memory bugs and memory leaks in `bcl`. + +Users that do not use `bcl` (use only `bc` and/or `dc`) do ***NOT*** need to +upgrade. + +These happened because I was unaware that the `bcl` test was not hooked into the +Valgrind test infrastructure. Then, when I ran the release script, which tests +everything under Valgrind (or so I thought), it caught nothing, and I thought it +was safe. + +But it was not. + +Nevertheless, I have now run it under Valgrind and fixed all of the memory bugs +(caused by not using `memset()` where I should have but previously didn't have +to) and memory leaks. + +## 6.0.0 + +This is a production release that fixes an oversight in the `bc` parser (that +sometimes caused the wrong error message) and adds a feature for compatibility +with the BSD `bc` and `dc`: turning off digit clamping when parsing numbers. + +The default for clamping can be set during the build (see the [build +manual][13]), it can be set with the `BC_DIGIT_CLAMP` and `DC_DIGIT_CLAMP` +environment variables, and it can be set with the `-c` and `-C` command-line +options. + +Turning off clamping was also added to the `bcl` library. + +In addition, signal handling was removed from the `bcl` library in order to add +the capability for multi-threading. This required a major version bump. I +apologize to all library users (I don't know of any), but signals and threads do +not play well together. + +To help with building, a convenience option (`-p`) to `configure.sh` was added +to build a `bc` and `dc` that is by default compatible with either the BSD `bc` +and `dc` or the GNU `bc` and `dc`. + +## 5.3.3 + +This is a production release that fixes a build problem in the FreeBSD base +system. + +All other users do **NOT** need to upgrade. + +## 5.3.2 + +This is a production release that fixes prompt bugs with editline and readline +where the `BC_PROMPT` environment variable was not being respected. + +This also fixes editline and readline output on `EOF`. + +## 5.3.1 + +This is a production release that fixes a build problem in the FreeBSD base +system, as well as a problem in the `en_US` locale. If you don't have problems +with either, you do not need to upgrade. + +## 5.3.0 + +This is a production release that adds features and has a few bug fixes. + +First, support for editline and readline history has been added. To use +editline, pass `-e` to `configure.sh`, and to use readline, pass `-r`. + +Second, history support for Windows has been fixed and re-enabled. + +Third, command-line options to set `scale`, `ibase`, `obase`, and `seed` were +added. This was requested long ago, and I originally disagreed with the idea. + +Fourth, the manuals had typos and were missing information. That has been fixed. + +Fifth, the manuals received different formatting to be more readable as +manpages. + +## 5.2.5 + +This is a production release that fixes this `bc`'s behavior on `^D` to match +GNU `bc`. + +## 5.2.4 + +This is a production release that fixes two bugs in history: + +* Without prompt, the cursor could not be placed on the first character in a + line. +* Home and End key handling in `tmux` was fixed. + +Any users that do not care about these improvements do not need to upgrade. + +## 5.2.3 + +This is a production release that fixes one bug, a parse error when passing a +file to `bc` using `-f` if that file had a multiline comment or string in it. + ## 5.2.2 This is a production release that fixes one bug, a segmentation fault if `argv[0]` equals `NULL`. This is not a critical bug; there will be no vulnerability as far as I can tell. There is no need to update if you do not wish to. ## 5.2.1 This is a production release that fixes two parse bugs when in POSIX standard mode. One of these bugs was due to a quirk of the POSIX grammar, and the other was because `bc` was too strict. ## 5.2.0 This is a production release that adds a new feature, fixes some bugs, and adds out-of-source builds and a `pkg-config` file for `bcl`. The new feature is the ability to turn off exiting on expressions. It is also possible to set the default using `configure.sh`. This behavior used to exist with the `BC_EXPR_EXIT` environment variable, which is now used again. Bugs fixed include: * Some possible race conditions with error handling. * Install and uninstall targets for `bcl` did not work. ## 5.1.1 This is a production release that completes a bug fix from `5.1.0`. The bug exists in all versions of `bc`. The bug was that `if` statements without `else` statements would not be handled correctly at the end of files or right before a function definition. ## 5.1.0 This is a production release with some fixes and new features. * Fixed a bug where an `if` statement without an `else` before defining a function caused an error. * Fixed a bug with the `bc` banner and `-q`. * Fixed a bug on Windows where files were not read correctly. * Added a command-line flag (`-z`) to make `bc` and `dc` print leading zeroes on numbers `-1 < x < 1`. * Added four functions to `lib2.bc` (`plz()`, `plznl()`, `pnlz()`, and `pnlznl()`) to allow printing numbers with or without leading zeros, despite the use of `-z` or not. * Added builtin functions to query global state like line length, global stacks, and leading zeroes. * Added a command-line flag (`-L`) to disable wrapping when printing numbers. * Improved builds on Windows. ## 5.0.2 This is a production release with one fix for a flaky test. If you have not experienced problems with the test suite, you do ***NOT*** need to upgrade. The test was one that tested whether `bc` fails gracefully when it can't allocate memory. Unfortunately, there are cases when Linux and FreeBSD lie and pretend to allocate the memory. The reason they do this is because a lot of programs don't use all of the memory they allocate, so those OS's usually get away with it. However, this `bc` uses all of the memory it allocates (at least at page granularity), so when it tries to use the memory, FreeBSD and Linux kill it. This only happens sometimes, however. Other times (on my machine), they do, in fact, refuse the request. So I changed the test to not test for that because I think the graceful failure code won't really change much. ## 5.0.1 This is a production release with two fixes: * Fix for the build on Mac OSX. * Fix for the build on Android. Users that do not use those platforms do ***NOT*** need to update. ## 5.0.0 This is a major production release with several changes: * Added support for OpenBSD's `pledge()` and `unveil()`. * Fixed print bug where a backslash newline combo was printed even if only one digit was left, something I blindly copied from GNU `bc`, like a fool. * Fixed bugs in the manuals. * Fixed a possible multiplication overflow in power. * Temporary numbers are garbage collected if allocation fails, and the allocation is retried. This is to make `bc` and `dc` more resilient to running out of memory. * Limited the number of temporary numbers and made the space for them static so that allocating more space for them cannot fail. * Allowed integers with non-zero `scale` to be used with power, places, and shift operators. * Added greatest common divisor and least common multiple to `lib2.bc`. * Added `SIGQUIT` handling to history. * Added a command to `dc` (`y`) to get the length of register stacks. * Fixed multi-digit bugs in `lib2.bc`. * Removed the no prompt build option. * Created settings that builders can set defaults for and users can set their preferences for. This includes the `bc` banner, resetting on `SIGINT`, TTY mode, and prompt. * Added history support to Windows. * Fixed bugs with the handling of register names in `dc`. * Fixed bugs with multi-line comments and strings in both calculators. * Added a new error type and message for `dc` when register stacks don't have enough items. * Optimized string allocation. * Made `bc` and `dc` UTF-8 capable. * Fixed a bug with `void` functions. * Fixed a misspelled symbol in `bcl`. This is technically a breaking change, which requires this to be `5.0.0`. * Added the ability for users to get the copyright banner back. * Added the ability for users to have `bc` and `dc` quit on `SIGINT`. * Added the ability for users to disable prompt and TTY mode by environment variables. * Added the ability for users to redefine keywords. This is another reason this is `5.0.0`. * Added `dc`'s modular exponentiation and divmod to `bc`. * Added the ability to assign strings to variables and array elements and pass them to functions in `bc`. * Added `dc`'s asciify command and stream printing to `bc`. * Added a command to `dc` (`Y`) to get the length of an array. * Added a command to `dc` (`,`) to get the depth of the execution stack. * Added bitwise and, or, xor, left shift, right shift, reverse, left rotate, right rotate, and mod functions to `lib2.bc`. * Added the functions `s2u(x)` and `s2un(x,n)`, to `lib2.bc`. ## 4.0.2 This is a production release that fixes two bugs: 1. If no files are used and the first statement on `stdin` is invalid, `scale` would not be set to `20` even if `-l` was used. 2. When using history, `bc` failed to respond properly to `SIGSTOP` and `SIGTSTP`. ## 4.0.1 This is a production release that only adds one thing: flushing output when it is printed with a print statement. ## 4.0.0 This is a production release with many fixes, a new command-line option, and a big surprise: * A bug was fixed in `dc`'s `P` command where the item on the stack was *not* popped. * Various bugs in the manuals have been fixed. * A known bug was fixed where history did not interact well with prompts printed by user code without newlines. * A new command-line option, `-R` and `--no-read-prompt` was added to disable just the prompt when using `read()` (`bc`) or `?` (`dc`). * And finally, **official support for Windows was added**. The last item is why this is a major version bump. Currently, only one set of build options (extra math and prompt enabled, history and NLS/locale support disabled, both calculators enabled) is supported on Windows. However, both debug and release builds are supported. In addition, Windows builds are supported for the the library (`bcl`). For more details about how to build on Windows, see the [README][5] or the [build manual][13]. ## 3.3.4 This is a production release that fixes a small bug. The bug was that output was not flushed before a `read()` call, so prompts without a newline on the end were not flushed before the `read()` call. This is such a tiny bug that users only need to upgrade if they are affected. ## 3.3.3 This is a production release with one tweak and fixes for manuals. The tweak is that `length(0)` returns `1` instead of `0`. In `3.3.1`, I changed it so `length(0.x)`, where `x` could be any number of digits, returned the `scale`, but `length(0)` still returned `0` because I believe that `0` has `0` significant digits. After request of FreeBSD and considering the arguments of a mathematician, compatibility with other `bc`'s, and the expectations of users, I decided to make the change. The fixes for manuals fixed a bug where `--` was rendered as `-`. ## 3.3.2 This is a production release that fixes a divide-by-zero bug in `root()` in the [extended math library][16]. All previous versions with `root()` have the bug. ## 3.3.1 This is a production release that fixes a bug. The bug was in the reporting of number length when the value was 0. ## 3.3.0 This is a production release that changes one behavior and fixes documentation bugs. The changed behavior is the treatment of `-e` and `-f` when given through `BC_ENV_ARGS` or `DC_ENV_ARGS`. Now `bc` and `dc` do not exit when those options (or their equivalents) are given through those environment variables. However, `bc` and `dc` still exit when they or their equivalents are given on the command-line. ## 3.2.7 This is a production release that removes a small non-portable shell operation in `configure.sh`. This problem was only noticed on OpenBSD, not FreeBSD or Linux. Non-OpenBSD users do ***NOT*** need to upgrade, although NetBSD users may also need to upgrade. ## 3.2.6 This is a production release that fixes the build on FreeBSD. There was a syntax error in `configure.sh` that the Linux shell did not catch, and FreeBSD depends on the existence of `tests/all.sh`. All users that already upgraded to `3.2.5` should update to this release, with my apologies for the poor release of `3.2.5`. Other users should skip `3.2.5` in favor of this version. ## 3.2.5 This is a production release that fixes several bugs and adds a couple small things. The two most important bugs were bugs that causes `dc` to access memory out-of-bounds (crash in debug builds). This was found by upgrading to `afl++` from `afl`. Both were caused by a failure to distinguish between the same two cases. Another bug was the failure to put all of the licenses in the `LICENSE.md` file. Third, some warnings by `scan-build` were found and eliminated. This needed one big change: `bc` and `dc` now bail out as fast as possible on fatal errors instead of unwinding the stack. Fourth, the pseudo-random number now attempts to seed itself with `/dev/random` if `/dev/urandom` fails. Finally, this release has a few quality-of-life changes to the build system. The usage should not change at all; the only thing that changed was making sure the `Makefile.in` was written to rebuild properly when headers changed and to not rebuild when not necessary. ## 3.2.4 This is a production release that fixes a warning on `gcc` 6 or older, which does not have an attribute that is used. Users do ***NOT*** need to upgrade if they don't use `gcc` 6 or older. ## 3.2.3 This is a production release that fixes a bug in `gen/strgen.sh`. I recently changed `gen/strgen.c`, but I did not change `gen/strgen.sh`. Users that do not use `gen/strgen.sh` do not need to upgrade. ## 3.2.2 This is a production release that fixes a portability bug in `configure.sh`. The bug was using the GNU `find` extension `-wholename`. ## 3.2.1 This is a production release that has one fix for `bcl(3)`. It is technically not a bug fix since the behavior is undefined, but the `BclNumber`s that `bcl_divmod()` returns will be set to `BCL_ERROR_INVALID_NUM` if there is an error. Previously, they were not set. ## 3.2.0 This is a production release that has one bug fix and a major addition. The bug fix was a missing `auto` variable in the bessel `j()` function in the math library. The major addition is a way to build a version of `bc`'s math code as a library. This is done with the `-a` option to `configure.sh`. The API for the library can be read in `./manuals/bcl.3.md` or `man bcl` once the library is installed with `make install`. This library was requested by developers before I even finished version 1.0, but I could not figure out how to do it until now. If the library has API breaking changes, the major version of `bc` will be incremented. ## 3.1.6 This is a production release that fixes a new warning from Clang 12 for FreeBSD and also removes some possible undefined behavior found by UBSan that compilers did not seem to take advantage of. Users do ***NOT*** need to upgrade, if they do not want to. ## 3.1.5 This is a production release that fixes the Chinese locales (which caused `bc` to crash) and a crash caused by `bc` executing code when it should not have been able to. ***ALL USERS SHOULD UPGRADE.*** ## 3.1.4 This is a production release that fixes one bug, changes two behaviors, and removes one environment variable. The bug is like the one in the last release except it applies if files are being executed. I also made the fix more general. The behavior that was changed is that `bc` now exits when given `-e`, `-f`, `--expression` or `--file`. However, if the last one of those is `-f-` (using `stdin` as the file), `bc` does not exit. If `-f-` exists and is not the last of the `-e` and `-f` options (and equivalents), `bc` gives a fatal error and exits. Next, I removed the `BC_EXPR_EXIT` and `DC_EXPR_EXIT` environment variables since their use is not needed with the behavior change. Finally, I made it so `bc` does not print the header, though the `-q` and `--quiet` options were kept for compatibility with GNU `bc`. ## 3.1.3 This is a production release that fixes one minor bug: if `bc` was invoked like the following, it would error: ``` echo "if (1 < 3) 1" | bc ``` Unless users run into this bug, they do not need to upgrade, but it is suggested that they do. ## 3.1.2 This is a production release that adds a way to install *all* locales. Users do ***NOT*** need to upgrade. For package maintainers wishing to make use of the change, just pass `-l` to `configure.sh`. ## 3.1.1 This is a production release that adds two Spanish locales. Users do ***NOT*** need to upgrade, unless they want those locales. ## 3.1.0 This is a production release that adjusts one behavior, fixes eight bugs, and improves manpages for FreeBSD. Because this release fixes bugs, **users and package maintainers should update to this version as soon as possible**. The behavior that was adjusted was how code from the `-e` and `-f` arguments (and equivalents) were executed. They used to be executed as one big chunk, but in this release, they are now executed line-by-line. The first bug fix in how output to `stdout` was handled in `SIGINT`. If a `SIGINT` came in, the `stdout` buffer was not correctly flushed. In fact, a clean-up function was not getting called. This release fixes that bug. The second bug is in how `dc` handled input from `stdin`. This affected `bc` as well since it was a mishandling of the `stdin` buffer. The third fixed bug was that `bc` and `dc` could `abort()` (in debug mode) when receiving a `SIGTERM`. This one was a race condition with pushing and popping items onto and out of vectors. The fourth bug fixed was that `bc` could leave extra items on the stack and thus, not properly clean up some memory. (The memory would still get `free()`'ed, but it would not be `free()`'ed when it could have been.) The next two bugs were bugs in `bc`'s parser that caused crashes when executing the resulting code. The last two bugs were crashes in `dc` that resulted from mishandling of strings. The manpage improvement was done by switching from [ronn][20] to [Pandoc][21] to generate manpages. Pandoc generates much cleaner manpages and doesn't leave blank lines where they shouldn't be. ## 3.0.3 This is a production release that adds one new feature: specific manpages. Before this release, `bc` and `dc` only used one manpage each that referred to various build options. This release changes it so there is one manpage set per relevant build type. Each manual only has information about its particular build, and `configure.sh` selects the correct set for install. ## 3.0.2 This is a production release that adds `utf8` locale symlinks and removes an unused `auto` variable from the `ceil()` function in the [extended math library][16]. Users do ***NOT*** need to update unless they want the locales. ## 3.0.1 This is a production release with two small changes. Users do ***NOT*** need to upgrade to this release; however, if they haven't upgraded to `3.0.0` yet, it may be worthwhile to upgrade to this release. The first change is fixing a compiler warning on FreeBSD with strict warnings on. The second change is to make the new implementation of `ceil()` in `lib2.bc` much more efficient. ## 3.0.0 *Notes for package maintainers:* *First, the `2.7.0` release series saw a change in the option parsing. This made me change one error message and add a few others. The error message that was changed removed one format specifier. This means that `printf()` will seqfault on old locale files. Unfortunately, `bc` cannot use any locale files except the global ones that are already installed, so it will use the previous ones while running tests during install. **If `bc` segfaults while running arg tests when updating, it is because the global locale files have not been replaced. Make sure to either prevent the test suite from running on update or remove the old locale files before updating.** (Removing the locale files can be done with `make uninstall` or by running the [`locale_uninstall.sh`][22] script.) Once this is done, `bc` should install without problems.* *Second, **the option to build without signal support has been removed**. See below for the reasons why.* This is a production release with some small bug fixes, a few improvements, three major bug fixes, and a complete redesign of `bc`'s error and signal handling. **Users and package maintainers should update to this version as soon as possible.** The first major bug fix was in how `bc` executed files. Previously, a whole file was parsed before it was executed, but if a function is defined *after* code, especially if the function definition was actually a redefinition, and the code before the definition referred to the previous function, this `bc` would replace the function before executing any code. The fix was to make sure that all code that existed before a function definition was executed. The second major bug fix was in `bc`'s `lib2.bc`. The `ceil()` function had a bug where a `0` in the decimal place after the truncation position, caused it to output the wrong numbers if there was any non-zero digit after. The third major bug is that when passing parameters to functions, if an expression included an array (not an array element) as a parameter, it was accepted, when it should have been rejected. It is now correctly rejected. Beyond that, this `bc` got several improvements that both sped it up, improved the handling of signals, and improved the error handling. First, the requirements for `bc` were pushed back to POSIX 2008. `bc` uses one function, `strdup()`, which is not in POSIX 2001, and it is in the X/Open System Interfaces group 2001. It is, however, in POSIX 2008, and since POSIX 2008 is old enough to be supported anywhere that I care, that should be the requirement. Second, the BcVm global variable was put into `bss`. This actually slightly reduces the size of the executable from a massive code shrink, and it will stop `bc` from allocating a large set of memory when `bc` starts. Third, the default Karatsuba length was updated from 64 to 32 after making the optimization changes below, since 32 is going to be better than 64 after the changes. Fourth, Spanish translations were added. Fifth, the interpreter received a speedup to make performance on non-math-heavy scripts more competitive with GNU `bc`. While improvements did, in fact, get it much closer (see the [benchmarks][19]), it isn't quite there. There were several things done to speed up the interpreter: First, several small inefficiencies were removed. These inefficiencies included calling the function `bc_vec_pop(v)` twice instead of calling `bc_vec_npop(v, 2)`. They also included an extra function call for checking the size of the stack and checking the size of the stack more than once on several operations. Second, since the current `bc` function is the one that stores constants and strings, the program caches pointers to the current function's vectors of constants and strings to prevent needing to grab the current function in order to grab a constant or a string. Third, `bc` tries to reuse `BcNum`'s (the internal representation of arbitary-precision numbers). If a `BcNum` has the default capacity of `BC_NUM_DEF_SIZE` (32 on 64-bit and 16 on 32-bit) when it is freed, it is added to a list of available `BcNum`'s. And then, when a `BcNum` is allocated with a capacity of `BC_NUM_DEF_SIZE` and any `BcNum`'s exist on the list of reusable ones, one of those ones is grabbed instead. In order to support these changes, the `BC_NUM_DEF_SIZE` was changed. It used to be 16 bytes on all systems, but it was changed to more closely align with the minimum allocation size on Linux, which is either 32 bytes (64-bit musl), 24 bytes (64-bit glibc), 16 bytes (32-bit musl), or 12 bytes (32-bit glibc). Since these are the minimum allocation sizes, these are the sizes that would be allocated anyway, making it worth it to just use the whole space, so the value of `BC_NUM_DEF_SIZE` on 64-bit systems was changed to 32 bytes. On top of that, at least on 64-bit, `BC_NUM_DEF_SIZE` supports numbers with either 72 integer digits or 45 integer digits and 27 fractional digits. This should be more than enough for most cases since `bc`'s default `scale` values are 0 or 20, meaning that, by default, it has at most 20 fractional digits. And 45 integer digits are *a lot*; it's enough to calculate the amount of mass in the Milky Way galaxy in kilograms. Also, 72 digits is enough to calculate the diameter of the universe in Planck lengths. (For 32-bit, these numbers are either 32 integer digits or 12 integer digits and 20 fractional digits. These are also quite big, and going much bigger on a 32-bit system seems a little pointless since 12 digits is just under a trillion and 20 fractional digits is still enough for about any use since `10^-20` light years is just under a millimeter.) All of this together means that for ordinary uses, and even uses in scientific work, the default number size will be all that is needed, which means that nearly all, if not all, numbers will be reused, relieving pressure on the system allocator. I did several experiments to find the changes that had the most impact, especially with regard to reusing `BcNum`'s. One was putting `BcNum`'s into buckets according to their capacity in powers of 2 up to 512. That performed worse than `bc` did in `2.7.2`. Another was putting any `BcNum` on the reuse list that had a capacity of `BC_NUM_DEF_SIZE * 2` and reusing them for `BcNum`'s that requested `BC_NUM_DEF_SIZE`. This did reduce the amount of time spent, but it also spent a lot of time in the system allocator for an unknown reason. (When using `strace`, a bunch more `brk` calls showed up.) Just reusing `BcNum`'s that had exactly `BC_NUM_DEF_SIZE` capacity spent the smallest amount of time in both user and system time. This makes sense, especially with the changes to make `BC_NUM_DEF_SIZE` bigger on 64-bit systems, since the vast majority of numbers will only ever use numbers with a size less than or equal to `BC_NUM_DEF_SIZE`. Last of all, `bc`'s signal handling underwent a complete redesign. (This is the reason that this version is `3.0.0` and not `2.8.0`.) The change was to move from a polling approach to signal handling to an interrupt-based approach. Previously, every single loop condition had a check for signals. I suspect that this could be expensive when in tight loops. Now, the signal handler just uses `longjmp()` (actually `siglongjmp()`) to start an unwinding of the stack until it is stopped or the stack is unwound to `main()`, which just returns. If `bc` is currently executing code that cannot be safely interrupted (according to POSIX), then signals are "locked." The signal handler checks if the lock is taken, and if it is, it just sets the status to indicate that a signal arrived. Later, when the signal lock is released, the status is checked to see if a signal came in. If so, the stack unwinding starts. This design eliminates polling in favor of maintaining a stack of `jmp_buf`'s. This has its own performance implications, but it gives better interaction. And the cost of pushing and popping a `jmp_buf` in a function is paid at most twice. Most functions do not pay that price, and most of the rest only pay it once. (There are only some 3 functions in `bc` that push and pop a `jmp_buf` twice.) As a side effect of this change, I had to eliminate the use of `stdio.h` in `bc` because `stdio` does not play nice with signals and `longjmp()`. I implemented custom I/O buffer code that takes a fraction of the size. This means that static builds will be smaller, but non-static builds will be bigger, though they will have less linking time. This change is also good because my history implementation was already bypassing `stdio` for good reasons, and unifying the architecture was a win. Another reason for this change is that my `bc` should *always* behave correctly in the presence of signals like `SIGINT`, `SIGTERM`, and `SIGQUIT`. With the addition of my own I/O buffering, I needed to also make sure that the buffers were correctly flushed even when such signals happened. For this reason, I **removed the option to build without signal support**. As a nice side effect of this change, the error handling code could be changed to take advantage of the stack unwinding that signals used. This means that signals and error handling use the same code paths, which means that the stack unwinding is well-tested. (Errors are tested heavily in the test suite.) It also means that functions do not need to return a status code that ***every*** caller needs to check. This eliminated over 100 branches that simply checked return codes and then passed that return code up the stack if necessary. The code bloat savings from this is at least 1700 bytes on `x86_64`, *before* taking into account the extra code from removing `stdio.h`. ## 2.7.2 This is a production release with one major bug fix. The `length()` built-in function can take either a number or an array. If it takes an array, it returns the length of the array. Arrays can be passed by reference. The bug is that the `length()` function would not properly dereference arrays that were references. This is a bug that affects all users. **ALL USERS SHOULD UPDATE `bc`**. ## 2.7.1 This is a production release with fixes for new locales and fixes for compiler warnings on FreeBSD. ## 2.7.0 This is a production release with a bug fix for Linux, new translations, and new features. Bug fixes: * Option parsing in `BC_ENV_ARGS` was broken on Linux in 2.6.1 because `glibc`'s `getopt_long()` is broken. To get around that, and to support long options on every platform, an adapted version of [`optparse`][17] was added. Now, `bc` does not even use `getopt()`. * Parsing `BC_ENV_ARGS` with quotes now works. It isn't the smartest, but it does the job if there are spaces in file names. The following new languages are supported: * Dutch * Polish * Russian * Japanes * Simplified Chinese All of these translations were generated using [DeepL][18], so improvements are welcome. There is only one new feature: **`bc` now has a built-in pseudo-random number generator** (PRNG). The PRNG is seeded, making it useful for applications where `/dev/urandom` does not work because output needs to be reproducible. However, it also uses `/dev/urandom` to seed itself by default, so it will start with a good seed by default. It also outputs 32 bits on 32-bit platforms and 64 bits on 64-bit platforms, far better than the 15 bits of C's `rand()` and `bash`'s `$RANDOM`. In addition, the PRNG can take a bound, and when it gets a bound, it automatically adjusts to remove bias. It can also generate numbers of arbitrary size. (As of the time of release, the largest pseudo-random number generated by this `bc` was generated with a bound of `2^(2^20)`.) ***IMPORTANT: read the [`bc` manual][9] and the [`dc` manual][10] to find out exactly what guarantees the PRNG provides. The underlying implementation is not guaranteed to stay the same, but the guarantees that it provides are guaranteed to stay the same regardless of the implementation.*** On top of that, four functions were added to `bc`'s [extended math library][16] to make using the PRNG easier: * `frand(p)`: Generates a number between `[0,1)` to `p` decimal places. * `ifrand(i, p)`: Generates an integer with bound `i` and adds it to `frand(p)`. * `srand(x)`: Randomizes the sign of `x`. In other words, it flips the sign of `x` with probability `0.5`. * `brand()`: Returns a random boolean value (either `0` or `1`). ## 2.6.1 This is a production release with a bug fix for FreeBSD. The bug was that when `bc` was built without long options, it would give a fatal error on every run. This was caused by a mishandling of `optind`. ## 2.6.0 This release is a production release ***with no bugfixes***. If you do not want to upgrade, you don't have to. No source code changed; the only thing that changed was `lib2.bc`. This release adds one function to the [extended math library][16]: `p(x, y)`, which calculates `x` to the power of `y`, whether or not `y` is an integer. (The `^` operator can only accept integer powers.) This release also includes a couple of small tweaks to the [extended math library][16], mostly to fix returning numbers with too high of `scale`. ## 2.5.3 This release is a production release which addresses inconsistencies in the Portuguese locales. No `bc` code was changed. The issues were that the ISO files used different naming, and also that the files that should have been symlinks were not. I did not catch that because GitHub rendered them the exact same way. ## 2.5.2 This release is a production release. No code was changed, but the build system was changed to allow `CFLAGS` to be given to `CC`, like this: ``` CC="gcc -O3 -march=native" ./configure.sh ``` If this happens, the flags are automatically put into `CFLAGS`, and the compiler is set appropriately. In the example above this means that `CC` will be "gcc" and `CFLAGS` will be "-O3 -march=native". This behavior was added to conform to GNU autotools practices. ## 2.5.1 This is a production release which addresses portability concerns discovered in the `bc` build system. No `bc` code was changed. * Support for Solaris SPARC and AIX were added. * Minor documentations edits were performed. * An option for `configure.sh` was added to disable long options if `getopt_long()` is missing. ## 2.5.0 This is a production release with new translations. No code changed. The translations were contributed by [bugcrazy][15], and they are for Portuguese, both Portugal and Brazil locales. ## 2.4.0 This is a production release primarily aimed at improving `dc`. * A couple of copy and paste errors in the [`dc` manual][10] were fixed. * `dc` startup was optimized by making sure it didn't have to set up `bc`-only things. * The `bc` `&&` and `||` operators were made available to `dc` through the `M` and `m` commands, respectively. * `dc` macros were changed to be tail call-optimized. The last item, tail call optimization, means that if the last thing in a macro is a call to another macro, then the old macro is popped before executing the new macro. This change was made to stop `dc` from consuming more and more memory as macros are executed in a loop. The `q` and `Q` commands still respect the "hidden" macros by way of recording how many macros were removed by tail call optimization. ## 2.3.2 This is a production release meant to fix warnings in the Gentoo `ebuild` by making it possible to disable binary stripping. Other users do *not* need to upgrade. ## 2.3.1 This is a production release. It fixes a bug that caused `-1000000000 < -1` to return `0`. This only happened with negative numbers and only if the value on the left was more negative by a certain amount. That said, this bug *is* a bad bug, and needs to be fixed. **ALL USERS SHOULD UPDATE `bc`**. ## 2.3.0 This is a production release with changes to the build system. ## 2.2.0 This release is a production release. It only has new features and performance improvements. 1. The performance of `sqrt(x)` was improved. 2. The new function `root(x, n)` was added to the extended math library to calculate `n`th roots. 3. The new function `cbrt(x)` was added to the extended math library to calculate cube roots. ## 2.1.3 This is a non-critical release; it just changes the build system, and in non-breaking ways: 1. Linked locale files were changed to link to their sources with a relative link. 2. A bug in `configure.sh` that caused long option parsing to fail under `bash` was fixed. ## 2.1.2 This release is not a critical release. 1. A few codes were added to history. 2. Multiplication was optimized a bit more. 3. Addition and subtraction were both optimized a bit more. ## 2.1.1 This release contains a fix for the test suite made for Linux from Scratch: now the test suite prints `pass` when a test is passed. Other than that, there is no change in this release, so distros and other users do not need to upgrade. ## 2.1.0 This release is a production release. The following bugs were fixed: 1. A `dc` bug that caused stack mishandling was fixed. 2. A warning on OpenBSD was fixed. 3. Bugs in `ctrl+arrow` operations in history were fixed. 4. The ability to paste multiple lines in history was added. 5. A `bc` bug, mishandling of array arguments to functions, was fixed. 6. A crash caused by freeing the wrong pointer was fixed. 7. A `dc` bug where strings, in a rare case, were mishandled in parsing was fixed. In addition, the following changes were made: 1. Division was slightly optimized. 2. An option was added to the build to disable printing of prompts. 3. The special case of empty arguments is now handled. This is to prevent errors in scripts that end up passing empty arguments. 4. A harmless bug was fixed. This bug was that, with the pop instructions (mostly) removed (see below), `bc` would leave extra values on its stack for `void` functions and in a few other cases. These extra items would not affect anything put on the stack and would not cause any sort of crash or even buggy behavior, but they would cause `bc` to take more memory than it needed. On top of the above changes, the following optimizations were added: 1. The need for pop instructions in `bc` was removed. 2. Extra tests on every iteration of the interpreter loop were removed. 3. Updating function and code pointers on every iteration of the interpreter loop was changed to only updating them when necessary. 4. Extra assignments to pointers were removed. Altogether, these changes sped up the interpreter by around 2x. ***NOTE***: This is the last release with new features because this `bc` is now considered complete. From now on, only bug fixes and new translations will be added to this `bc`. ## 2.0.3 This is a production, bug-fix release. Two bugs were fixed in this release: 1. A rare and subtle signal handling bug was fixed. 2. A misbehavior on `0` to a negative power was fixed. The last bug bears some mentioning. When I originally wrote power, I did not thoroughly check its error cases; instead, I had it check if the first number was `0` and then if so, just return `0`. However, `0` to a negative power means that `1` will be divided by `0`, which is an error. I caught this, but only after I stopped being cocky. You see, sometime later, I had noticed that GNU `bc` returned an error, correctly, but I thought it was wrong simply because that's not what my `bc` did. I saw it again later and had a double take. I checked for real, finally, and found out that my `bc` was wrong all along. That was bad on me. But the bug was easy to fix, so it is fixed now. There are two other things in this release: 1. Subtraction was optimized by [Stefan Eßer][14]. 2. Division was also optimized, also by Stefan Eßer. ## 2.0.2 This release contains a fix for a possible overflow in the signal handling. I would be surprised if any users ran into it because it would only happen after 2 billion (`2^31-1`) `SIGINT`'s, but I saw it and had to fix it. ## 2.0.1 This release contains very few things that will apply to any users. 1. A slight bug in `dc`'s interactive mode was fixed. 2. A bug in the test suite that was only triggered on NetBSD was fixed. 3. **The `-P`/`--no-prompt` option** was added for users that do not want a prompt. 4. A `make check` target was added as an alias for `make test`. 5. `dc` got its own read prompt: `?> `. ## 2.0.0 This release is a production release. This release is also a little different from previous releases. From here on out, I do not plan on adding any more features to this `bc`; I believe that it is complete. However, there may be bug fix releases in the future, if I or any others manage to find bugs. This release has only a few new features: 1. `atan2(y, x)` was added to the extended math library as both `a2(y, x)` and `atan2(y, x)`. 2. Locales were fixed. 3. A **POSIX shell-compatible script was added as an alternative to compiling `gen/strgen.c`** on a host machine. More details about making the choice between the two can be found by running `./configure.sh --help` or reading the [build manual][13]. 4. Multiplication was optimized by using **diagonal multiplication**, rather than straight brute force. 5. The `locale_install.sh` script was fixed. 6. `dc` was given the ability to **use the environment variable `DC_ENV_ARGS`**. 7. `dc` was also given the ability to **use the `-i` or `--interactive`** options. 8. Printing the prompt was fixed so that it did not print when it shouldn't. 9. Signal handling was fixed. 10. **Handling of `SIGTERM` and `SIGQUIT`** was fixed. 11. The **built-in functions `maxibase()`, `maxobase()`, and `maxscale()`** (the commands `T`, `U`, `V` in `dc`, respectively) were added to allow scripts to query for the max allowable values of those globals. 12. Some incompatibilities with POSIX were fixed. In addition, this release is `2.0.0` for a big reason: the internal format for numbers changed. They used to be a `char` array. Now, they are an array of larger integers, packing more decimal digits into each integer. This has delivered ***HUGE*** performance improvements, especially for multiplication, division, and power. This `bc` should now be the fastest `bc` available, but I may be wrong. ## 1.2.8 This release contains a fix for a harmless bug (it is harmless in that it still works, but it just copies extra data) in the [`locale_install.sh`][12] script. ## 1.2.7 This version contains fixes for the build on Arch Linux. ## 1.2.6 This release removes the use of `local` in shell scripts because it's not POSIX shell-compatible, and also updates a man page that should have been updated a long time ago but was missed. ## 1.2.5 This release contains some missing locale `*.msg` files. ## 1.2.4 This release contains a few bug fixes and new French translations. ## 1.2.3 This release contains a fix for a bug: use of uninitialized data. Such data was only used when outputting an error message, but I am striving for perfection. As Michelangelo said, "Trifles make perfection, and perfection is no trifle." ## 1.2.2 This release contains fixes for OpenBSD. ## 1.2.1 This release contains bug fixes for some rare bugs. ## 1.2.0 This is a production release. There have been several changes since `1.1.0`: 1. The build system had some changes. 2. Locale support has been added. (Patches welcome for translations.) 3. **The ability to turn `ibase`, `obase`, and `scale` into stacks** was added with the `-g` command-line option. (See the [`bc` manual][9] for more details.) 4. Support for compiling on Mac OSX out of the box was added. 5. The extended math library got `t(x)`, `ceil(x)`, and some aliases. 6. The extended math library also got `r2d(x)` (for converting from radians to degrees) and `d2r(x)` (for converting from degrees to radians). This is to allow using degrees with the standard library. 7. Both calculators now accept numbers in **scientific notation**. See the [`bc` manual][9] and the [`dc` manual][10] for details. 8. Both calculators can **output in either scientific or engineering notation**. See the [`bc` manual][9] and the [`dc` manual][10] for details. 9. Some inefficiencies were removed. 10. Some bugs were fixed. 11. Some bugs in the extended library were fixed. 12. Some defects from [Coverity Scan][11] were fixed. ## 1.1.4 This release contains a fix to the build system that allows it to build on older versions of `glibc`. ## 1.1.3 This release contains a fix for a bug in the test suite where `bc` tests and `dc` tests could not be run in parallel. ## 1.1.2 This release has a fix for a history bug; the down arrow did not work. ## 1.1.1 This release fixes a bug in the `1.1.0` build system. The source is exactly the same. The bug that was fixed was a failure to install if no `EXECSUFFIX` was used. ## 1.1.0 This is a production release. However, many new features were added since `1.0`. 1. **The build system has been changed** to use a custom, POSIX shell-compatible configure script ([`configure.sh`][6]) to generate a POSIX make-compatible `Makefile`, which means that `bc` and `dc` now build out of the box on any POSIX-compatible system. 2. Out-of-memory and output errors now cause the `bc` to report the error, clean up, and die, rather than just reporting and trying to continue. 3. **Strings and constants are now garbage collected** when possible. 4. Signal handling and checking has been made more simple and more thorough. 5. `BcGlobals` was refactored into `BcVm` and `BcVm` was made global. Some procedure names were changed to reflect its difference to everything else. 6. Addition got a speed improvement. 7. Some common code for addition and multiplication was refactored into its own procedure. 8. A bug was removed where `dc` could have been selected, but the internal `#define` that returned `true` for a query about `dc` would not have returned `true`. 9. Useless calls to `bc_num_zero()` were removed. 10. **History support was added.** The history support is based off of a [UTF-8 aware fork][7] of [`linenoise`][8], which has been customized with `bc`'s own data structures and signal handling. 11. Generating C source from the math library now removes tabs from the library, shrinking the size of the executable. 12. The math library was shrunk. 13. Error handling and reporting was improved. 14. Reallocations were reduced by giving access to the request size for each operation. 15. **`abs()` (`b` command for `dc`) was added as a builtin.** 16. Both calculators were tested on FreeBSD. 17. Many obscure parse bugs were fixed. 18. Markdown and man page manuals were added, and the man pages are installed by `make install`. 19. Executable size was reduced, though the added features probably made the executable end up bigger. 20. **GNU-style array references were added as a supported feature.** 21. Allocations were reduced. 22. **New operators were added**: `$` (`$` for `dc`), `@` (`@` for `dc`), `@=`, `<<` (`H` for `dc`), `<<=`, `>>` (`h` for `dc`), and `>>=`. See the [`bc` manual][9] and the [`dc` manual][10] for more details. 23. **An extended math library was added.** This library contains code that makes it so I can replace my desktop calculator with this `bc`. See the [`bc` manual][3] for more details. 24. Support for all capital letters as numbers was added. 25. **Support for GNU-style void functions was added.** 26. A bug fix for improper handling of function parameters was added. 27. Precedence for the or (`||`) operator was changed to match GNU `bc`. 28. `dc` was given an explicit negation command. 29. `dc` was changed to be able to handle strings in arrays. ## 1.1 Release Candidate 3 This release is the eighth release candidate for 1.1, though it is the third release candidate meant as a general release candidate. The new code has not been tested as thoroughly as it should for release. ## 1.1 Release Candidate 2 This release is the seventh release candidate for 1.1, though it is the second release candidate meant as a general release candidate. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 5 This release is the sixth release candidate for 1.1, though it is the fifth release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 4 This release is the fifth release candidate for 1.1, though it is the fourth release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 3 This release is the fourth release candidate for 1.1, though it is the third release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 2 This release is the third release candidate for 1.1, though it is the second release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 1 This release is the second release candidate for 1.1, though it is meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 Release Candidate 1 This is the first release candidate for 1.1. The new code has not been tested as thoroughly as it should for release. ## 1.0 This is the first non-beta release. `bc` is ready for production use. As such, a lot has changed since 0.5. 1. `dc` has been added. It has been tested even more thoroughly than `bc` was for `0.5`. It does not have the `!` command, and for security reasons, it never will, so it is complete. 2. `bc` has been more thoroughly tested. An entire section of the test suite (for both programs) has been added to test for errors. 3. A prompt (`>>> `) has been added for interactive mode, making it easier to see inputs and outputs. 4. Interrupt handling has been improved, including elimination of race conditions (as much as possible). 5. MinGW and [Windows Subsystem for Linux][1] support has been added (see [xstatic][2] for binaries). 6. Memory leaks and errors have been eliminated (as far as ASan and Valgrind can tell). 7. Crashes have been eliminated (as far as [afl][3] can tell). 8. Karatsuba multiplication was added (and thoroughly) tested, speeding up multiplication and power by orders of magnitude. 9. Performance was further enhanced by using a "divmod" function to reduce redundant divisions and by removing superfluous `memset()` calls. 10. To switch between Karatsuba and `O(n^2)` multiplication, the config variable `BC_NUM_KARATSUBA_LEN` was added. It is set to a sane default, but the optimal number can be found with [`karatsuba.py`][4] (requires Python 3) and then configured through `make`. 11. The random math test generator script was changed to Python 3 and improved. `bc` and `dc` have together been run through 30+ million random tests. 12. All known math bugs have been fixed, including out of control memory allocations in `sine` and `cosine` (that was actually a parse bug), certain cases of infinite loop on square root, and slight inaccuracies (as much as possible; see the [README][5]) in transcendental functions. 13. Parsing has been fixed as much as possible. 14. Test coverage was improved to 94.8%. The only paths not covered are ones that happen when `malloc()` or `realloc()` fails. 15. An extension to get the length of an array was added. 16. The boolean not (`!`) had its precedence change to match negation. 17. Data input was hardened. 18. `bc` was made fully compliant with POSIX when the `-s` flag is used or `POSIXLY_CORRECT` is defined. 19. Error handling was improved. 20. `bc` now checks that files it is given are not directories. ## 1.0 Release Candidate 7 This is the seventh release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 6. ## 1.0 Release Candidate 6 This is the sixth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 5. ## 1.0 Release Candidate 5 This is the fifth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 4. ## 1.0 Release Candidate 4 This is the fourth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 3. ## 1.0 Release Candidate 3 This is the third release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 2. ## 1.0 Release Candidate 2 This is the second release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 1. ## 1.0 Release Candidate 1 This is the first Release Candidate for 1.0. `bc` is complete, with `dc`, but it is not tested. ## 0.5 This beta release completes more features, but it is still not complete nor tested as thoroughly as necessary. ## 0.4.1 This beta release fixes a few bugs in 0.4. ## 0.4 This is a beta release. It does not have the complete set of features, and it is not thoroughly tested. [1]: https://docs.microsoft.com/en-us/windows/wsl/install-win10 [2]: https://pkg.musl.cc/bc/ [3]: http://lcamtuf.coredump.cx/afl/ [4]: ./scripts/karatsuba.py [5]: ./README.md [6]: ./configure.sh [7]: https://github.com/rain-1/linenoise-mob [8]: https://github.com/antirez/linenoise [9]: ./manuals/bc/A.1.md [10]: ./manuals/dc/A.1.md [11]: https://scan.coverity.com/projects/gavinhoward-bc [12]: ./scripts/locale_install.sh [13]: ./manuals/build.md [14]: https://github.com/stesser [15]: https://github.com/bugcrazy [16]: ./manuals/bc/A.1.md#extended-library [17]: https://github.com/skeeto/optparse [18]: https://www.deepl.com/translator [19]: ./manuals/benchmarks.md [20]: https://github.com/apjanke/ronn-ng [21]: https://pandoc.org/ [22]: ./scripts/locale_uninstall.sh diff --git a/contrib/bc/NOTICE.md b/contrib/bc/NOTICE.md index 56d2935ab4b3..c0d3ded5797a 100644 --- a/contrib/bc/NOTICE.md +++ b/contrib/bc/NOTICE.md @@ -1,14 +1,14 @@ # Notice -Copyright 2018-2021 Gavin D. Howard and contributors. +Copyright 2018-2023 Gavin D. Howard and contributors. ## Contributors The contributors to `bc` (besides Gavin) are listed below in alphabetical order. * Laurent Bercot (skarnet) * Stefan Eßer (stesser) * Michael Forney (michaelforney) * John Regan (jprjr) * rofl0r * Zach van Rijn (me@zv.io) diff --git a/contrib/bc/README.md b/contrib/bc/README.md index 259ab923bfc4..438cab0d1694 100644 --- a/contrib/bc/README.md +++ b/contrib/bc/README.md @@ -1,441 +1,498 @@ # `bc` +***WARNING: New user registration for https://git.yzena.com/ is disabled because +of spam. If you need to report a bug with `bc`, email gavin at this site minus +the `git.` part for an account, and I will create one for you. Or you can report +an issue at [GitHub][29].*** + ***WARNING: This project has moved to [https://git.yzena.com/][20] for [these reasons][21], though GitHub will remain a mirror.*** This is an implementation of the [POSIX `bc` calculator][12] that implements [GNU `bc`][1] extensions, as well as the period (`.`) extension for the BSD flavor of `bc`. For more information, see this `bc`'s full manual. This `bc` also includes an implementation of `dc` in the same binary, accessible via a symbolic link, which implements all FreeBSD and GNU extensions. (If a standalone `dc` binary is desired, `bc` can be copied and renamed to `dc`.) The `!` command is omitted; I believe this poses security concerns and that such functionality is unnecessary. For more information, see the `dc`'s full manual. This `bc` also provides `bc`'s math as a library with C bindings, called `bcl`. For more information, see the full manual for `bcl`. ## License This `bc` is Free and Open Source Software (FOSS). It is offered under the BSD 2-clause License. Full license text may be found in the [`LICENSE.md`][4] file. ## Prerequisites This `bc` only requires either: 1. Windows 10 or later, or 2. A C99-compatible compiler and a (mostly) POSIX 2008-compatible system with the XSI (X/Open System Interfaces) option group. Since POSIX 2008 with XSI requires the existence of a C99 compiler as `c99`, any POSIX and XSI-compatible system will have everything needed. POSIX-compatible systems that are known to work: * Linux * FreeBSD * OpenBSD * NetBSD * Mac OSX * Solaris* (as long as the Solaris version supports POSIX 2008) * AIX * HP-UX* (except for history) In addition, there is compatibility code to make this `bc` work on Windows. Please submit bug reports if this `bc` does not build out of the box on any system. ## Build This `bc` should build unmodified on any POSIX-compliant system or on Windows starting with Windows 10 (though earlier versions may work). For more complex build requirements than the ones below, see the [build manual][5]. ### Windows There is no guarantee that this `bc` will work on any version of Windows earlier than Windows 10 (I cannot test on earlier versions), but it is guaranteed to work on Windows 10 at least. Also, if building with MSBuild, the MSBuild bundled with Visual Studio is required. **Note**: Unlike the POSIX-compatible platforms, only one build configuration is -supported on Windows: extra math and prompt enabled, history and NLS (locale -support) disabled, with both calculators built. +supported on Windows: extra math and history enabled, NLS (locale support) +disabled, with both calculators built. #### `bc` To build `bc`, you can open the `vs/bc.sln` file in Visual Studio, select the configuration, and build. You can also build using MSBuild with the following from the root directory: ``` msbuild -property:Configuration= vs/bc.sln ``` where `` is either one of `Debug` or `Release`. On Windows, the calculators are built as `vs/bin///bc.exe` and `vs/bin///dc.exe`, where `` can be either `Win32` or `x64`, and `` can be `Debug` or `Release`. **Note**: On Windows, `dc.exe` is just copied from `bc.exe`; it is not linked. Patches are welcome for a way to do that. #### `bcl` (Library) To build the library, you can open the `vs/bcl.sln` file in Visual Studio, select the configuration, and build. You can also build using MSBuild with the following from the root directory: ``` msbuild -property:Configuration= vs/bcl.sln ``` where `` is either one of `Debug`, `ReleaseMD`, or `ReleaseMT`. On Windows, the library is built as `vs/lib///bcl.lib`, where `` can be either `Win32` or `x64`, and `` can be `Debug`, `ReleaseMD`, or `ReleaseMT`. ### POSIX-Compatible Systems On POSIX-compatible systems, `bc` is built as `bin/bc` and `dc` is built as `bin/dc` by default. #### Default For the default build with optimization, use the following commands in the root directory: ``` ./configure.sh -O3 make ``` #### One Calculator To only build `bc`, use the following commands: ``` ./configure.sh --disable-dc make ``` To only build `dc`, use the following commands: ``` ./configure.sh --disable-bc make ``` #### Debug For debug builds, use the following commands in the root directory: ``` ./configure.sh -g make ``` #### Install To install, use the following command: ``` make install ``` By default, `bc` and `dc` will be installed in `/usr/local`. For installing in other locations, use the `PREFIX` environment variable when running `configure.sh` or pass the `--prefix=` option to `configure.sh`. See the [build manual][5], or run `./configure.sh --help`, for more details. #### Library -This `bc` does provide a way to build a math library with C bindings. This is -done by the `-a` or `--library` options to `configure.sh`: +To build the math library, pass the `-a` or `--library` options to +`configure.sh`: ``` ./configure.sh -a ``` When building the library, the executables are not built. For more information, see the [build manual][5]. The library API can be found in [`manuals/bcl.3.md`][26] or `man bcl` once the library is installed. -The library is built as `bin/libbcl.a` on POSIX-compatible systems or as -`Release/bcl/bcl.lib` on Windows. - #### Package and Distro Maintainers This section is for package and distro maintainers. ##### Out-of-Source Builds Out-of-source builds are supported; just call `configure.sh` from the directory where the actual build will happen. For example, if the source is in `bc`, the build should happen in `build`, then call `configure.sh` and `make` like so: ``` ../bc/configure.sh make ``` ***WARNING***: The path to `configure.sh` from the build directory must not have spaces because `make` does not support target names with spaces. ##### Recommended Compiler When I ran benchmarks with my `bc` compiled under `clang`, it performed much better than when compiled under `gcc`. I recommend compiling this `bc` with `clang`. I also recommend building this `bc` with C11 if you can because `bc` will detect a C11 compiler and add `_Noreturn` to any relevant function(s). ##### Recommended Optimizations I wrote this `bc` with Separation of Concerns, which means that there are many small functions that could be inlined. However, they are often called across file boundaries, and the default optimizer can only look at the current file, which means that they are not inlined. Thus, because of the way this `bc` is built, it will automatically be slower than other `bc` implementations when running scripts with no math. (My `bc`'s math is *much* faster, so any non-trivial script should run faster in my `bc`.) Some, or all, of the difference can be made up with the right optimizations. The optimizations I recommend are: 1. `-O3` 2. `-flto` (link-time optimization) in that order. Link-time optimization, in particular, speeds up the `bc` a lot. This is because when link-time optimization is turned on, the optimizer can look across files and inline *much* more heavily. However, I recommend ***NOT*** using `-march=native`. Doing so will reduce this `bc`'s performance, at least when building with link-time optimization. See the [benchmarks][19] for more details. ##### Stripping Binaries By default, non-debug binaries are stripped, but stripping can be disabled with the `-T` option to `configure.sh`. ##### Using This `bc` as an Alternative If this `bc` is packaged as an alternative to an already existing `bc` package, it is possible to rename it in the build to prevent name collision. To prepend to the name, just run the following: ``` EXECPREFIX= ./configure.sh ``` To append to the name, just run the following: ``` EXECSUFFIX= ./configure.sh ``` If a package maintainer wishes to add both a prefix and a suffix, that is allowed. **Note**: The suggested name (and package name) when `bc` is not available is `bc-gh`. ##### Karatsuba Number Package and distro maintainers have one tool at their disposal to build this `bc` in the optimal configuration: `scripts/karatsuba.py`. This script is not a compile-time or runtime prerequisite; it is for package and distro maintainers to run once when a package is being created. It finds the optimal Karatsuba number (see the [algorithms manual][7] for more information) for the machine that it is running on. The easiest way to run this script is with `make karatsuba`. If desired, maintainers can also skip running this script because there is a sane default for the Karatsuba number. ## Status This `bc` is robust. It is well-tested, fuzzed, and fully standards-compliant (though not certified) with POSIX `bc`. The math has been tested with 40+ million random problems, so it is as correct as I can make it. This `bc` can be used as a drop-in replacement for any existing `bc`. This `bc` -is also compatible with MinGW toolchains, though history is not supported on -Windows. +is also compatible with MinGW toolchains. In addition, this `bc` is considered complete; i.e., there will be no more releases with additional features. However, it *is* actively maintained, so if any bugs are found, they will be fixed in new releases. Also, additional translations will also be added as they are provided. ### Development If I (Gavin D. Howard) get [hit by a bus][27] and future programmers need to handle work themselves, the best place to start is the [Development manual][28]. ## Vim Syntax I have developed (using other people's code to start) [`vim` syntax files][17] for this `bc` and `dc`, including the extensions. ## `bc` Libs I have gathered some excellent [`bc` and `dc` libraries][18]. These libraries may prove useful to any serious users. ## Comparison to GNU `bc` This `bc` compares favorably to GNU `bc`. * This `bc` builds natively on Windows. -* It has more extensions, which make this `bc` more useful for scripting. +* It has more extensions, which make this `bc` more useful for scripting. (See + [Extensions](#extensions).) * This `bc` is a bit more POSIX compliant. * It has a much less buggy parser. The GNU `bc` will give parse errors for what is actually valid `bc` code, or should be. For example, putting an `else` on a new line after a brace can cause GNU `bc` to give a parse error. * This `bc` has fewer crashes. * GNU `bc` calculates the wrong number of significant digits for `length(x)`. * GNU `bc` will sometimes print numbers incorrectly. For example, when running it on the file `tests/bc/power.txt` in this repo, GNU `bc` gets all the right answers, but it fails to wrap the numbers at the proper place when outputting to a file. * This `bc` is faster. (See [Performance](#performance).) ### Performance Because this `bc` packs more than `1` decimal digit per hardware integer, this `bc` is faster than GNU `bc` and can be *much* faster. Full benchmarks can be found at [manuals/benchmarks.md][19]. There is one instance where this `bc` is slower: if scripts are light on math. This is because this `bc`'s intepreter is slightly slower than GNU `bc`, but that is because it is more robust. See the [benchmarks][19]. +### Extensions + +Below is a non-comprehensive list of extensions that this `bc` and `dc` have +that all others do not. + +* An extended math library. (See [here][30] for more information.) +* A command-line prompt. +* Turning on and off digit clamping. (Digit clamping is about how to treat + "invalid" digits for a particular base. GNU `bc` uses it, and the BSD `bc` + does not. Mine does both.) +* A pseudo-random number generator. This includes the ability to set the seed + and get reproducible streams of random numbers. +* The ability to use stacks for the globals `scale`, `ibase`, and `obase` + instead of needing to restore them in *every* function. +* The ability to *not* use non-standard keywords. For example, `abs` is a + keyword (a built-in function), but if some script actually defines a function + called that, it's possible to tell my `bc` to not treat it as a keyword, which + will make the script parses correctly. +* The ability to turn on and off printing leading zeroes on numbers greater than + `-1` and less than `1`. +* Outputting in scientific and engineering notation. +* Accepting input in scientific and engineering notation. +* Passing strings and arrays to the `length()` built-in function. (In `dc`, the + `Y` command will do this for arrays, and the `Z` command will do this for both + numbers and strings.) +* The `abs()` built-in function. (This is the `b` command in `dc`.) +* The `is_number()` and `is_string()` built-in functions. (These tell whether a + variable is holding a string or a number, for runtime type checking. The + commands are `u` and `t` in `dc`.) +* For `bc` only, the `divmod()` built-in function for computing a quotient and + remainder at the same time. +* For `bc` only, the `asciify()` built-in function for converting an array to a + string. +* The `$` truncation operator. (It's the same in `bc` and `dc`.) +* The `@` "set scale" operator. (It's the same in `bc` and `dc`.) +* The decimal shift operators. (`<<` and `>>` in `bc`, `H` and `h` in `dc`.) +* Built-in functions or commands to get the max of `scale`, `ibase`, and + `obase`. +* The ability to put strings into variables in `bc`. (This always existed in + `dc`.) +* The `'` command in `dc` for the depth of the execution stack. +* The `y` command in `dc` for the depth of register stacks. +* Built-in functions or commands to get the value of certain environment + variables that might affect execution. +* The `stream` keyword to do the same thing as the `P` command in `dc`. +* Defined order of evaluation. +* Defined exit statuses. +* All environment variables other than `POSIXLY_CORRECT`, `BC_ENV_ARGS`, and + `BC_LINE_LENGTH`. +* The ability for users to define their own defaults for various options during + build. (See [here][31] for more information.) + ## Algorithms To see what algorithms this `bc` uses, see the [algorithms manual][7]. ## Locales Currently, there is no locale support on Windows. Additionally, this `bc` only has support for English (and US English), French, German, Portuguese, Dutch, Polish, Russian, Japanese, and Chinese locales. Patches are welcome for translations; use the existing `*.msg` files in `locales/` as a starting point. In addition, patches for improvements are welcome; the last two messages in Portuguese were made with Google Translate, and the Dutch, Polish, Russian, Japanese, and Chinese locales were all generated with [DeepL][22]. The message files provided assume that locales apply to all regions where a language is used, but this might not be true for, e.g., `fr_CA` and `fr_CH`. Any corrections or a confirmation that the current texts are acceptable for those regions would be appreciated, too. ## Other Projects Other projects based on this bc are: * [busybox `bc`][8]. The busybox maintainers have made their own changes, so any bugs in the busybox `bc` should be reported to them. * [toybox `bc`][9]. The maintainer has also made his own changes, so bugs in the toybox `bc` should be reported there. * [FreeBSD `bc`][23]. While the `bc` in FreeBSD is kept up-to-date, it is better to [report bugs there][24], as well as [submit patches][25], and the maintainers of the package will contact me if necessary. ## Language This `bc` is written in pure ISO C99, using POSIX 2008 APIs with custom Windows compatibility code. ## Commit Messages This `bc` uses the commit message guidelines laid out in [this blog post][10]. ## Semantic Versioning This `bc` uses [semantic versioning][11]. ## Contents Items labeled with `(maintainer use only)` are not included in release source tarballs. Files: .gitignore The git ignore file (maintainer use only). .gitattributes The git attributes file (maintainer use only). bcl.pc.in A template pkg-config file for bcl. configure A symlink to configure.sh to make packaging easier. configure.sh The configure script. LICENSE.md A Markdown form of the BSD 2-clause License. Makefile.in The Makefile template. NEWS.md The changelog. NOTICE.md List of contributors and copyright owners. - RELEASE.md A checklist for making a release (maintainer use only). Folders: - gen The bc math library, help texts, and code to generate C source. - include All header files. - locales Locale files, in .msg format. Patches welcome for translations. - manuals Manuals for both programs. - src All source code. - scripts A bunch of shell scripts to help with development and building. - tests All tests. - vs Files needed for the build on Windows. + benchmarks A folder of benchmarks for various aspects of bc performance. + gen The bc math library, help texts, and code to generate C source. + include All header files. + locales Locale files, in .msg format. Patches welcome for translations. + manuals Manuals for both programs. + src All source code. + scripts A bunch of shell scripts to help with development and building. + tests All tests. + vs Files needed for the build on Windows. [1]: https://www.gnu.org/software/bc/ [4]: ./LICENSE.md [5]: ./manuals/build.md [7]: ./manuals/algorithms.md [8]: https://git.busybox.net/busybox/tree/miscutils/bc.c [9]: https://github.com/landley/toybox/blob/master/toys/pending/bc.c [10]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html [11]: http://semver.org/ [12]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html [17]: https://git.yzena.com/gavin/vim-bc [18]: https://git.yzena.com/gavin/bc_libs [19]: ./manuals/benchmarks.md [20]: https://git.yzena.com/gavin/bc [21]: https://gavinhoward.com/2020/04/i-am-moving-away-from-github/ [22]: https://www.deepl.com/translator [23]: https://cgit.freebsd.org/src/tree/contrib/bc [24]: https://bugs.freebsd.org/ [25]: https://reviews.freebsd.org/ [26]: ./manuals/bcl.3.md [27]: https://en.wikipedia.org/wiki/Bus_factor [28]: ./manuals/development.md +[29]: https://github.com/gavinhoward/bc +[30]: ./manuals/bc/A.1.md#extended-library +[31]: ./manuals/build.md#settings diff --git a/contrib/bc/configure.sh b/contrib/bc/configure.sh index fc66ffc51066..3ada5298e9ed 100755 --- a/contrib/bc/configure.sh +++ b/contrib/bc/configure.sh @@ -1,1764 +1,2111 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # -# Copyright (c) 2018-2021 Gavin D. Howard and contributors. +# Copyright (c) 2018-2023 Gavin D. Howard and contributors. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # script="$0" scriptdir=$(dirname "$script") script=$(basename "$script") builddir=$(pwd) . "$scriptdir/scripts/functions.sh" # Simply prints the help message and quits based on the argument. # @param msg The help message to print. usage() { if [ $# -gt 0 ]; then _usage_val=1 - printf "%s\n\n" "$1" + printf '%s\n\n' "$1" else _usage_val=0 fi printf 'usage:\n' printf ' %s -h\n' "$script" printf ' %s --help\n' "$script" - printf ' %s [-a|-bD|-dB|-c] [-CEfgGHlmMNtTvz] [-O OPT_LEVEL] [-k KARATSUBA_LEN]\\\n' "$script" - printf ' [-s SETTING] [-S SETTING]\n' + printf ' %s [-a|-bD|-dB|-c] [-CeEfgGHilmMNPrtTvz] [-O OPT_LEVEL] [-k KARATSUBA_LEN]\\\n' "$script" + printf ' [-s SETTING] [-S SETTING] [-p TYPE]\n' printf ' %s \\\n' "$script" printf ' [--library|--bc-only --disable-dc|--dc-only --disable-bc|--coverage] \\\n' printf ' [--force --debug --disable-extra-math --disable-generated-tests] \\\n' printf ' [--disable-history --disable-man-pages --disable-nls --disable-strip] \\\n' - printf ' [--install-all-locales] [--opt=OPT_LEVEL] \\\n' - printf ' [--karatsuba-len=KARATSUBA_LEN] \\\n' + printf ' [--enable-editline] [--enable-readline] [--enable-internal-history] \\\n' + printf ' [--disable-problematic-tests] [--install-all-locales] \\\n' + printf ' [--opt=OPT_LEVEL] [--karatsuba-len=KARATSUBA_LEN] \\\n' printf ' [--set-default-on=SETTING] [--set-default-off=SETTING] \\\n' + printf ' [--predefined-build-type=TYPE] \\\n' printf ' [--prefix=PREFIX] [--bindir=BINDIR] [--datarootdir=DATAROOTDIR] \\\n' printf ' [--datadir=DATADIR] [--mandir=MANDIR] [--man1dir=MAN1DIR] \\\n' + printf ' [--man3dir=MAN3DIR]\n' + + if [ "$_usage_val" -ne 0 ]; then + exit + fi + printf '\n' printf ' -a, --library\n' printf ' Build the libbcl instead of the programs. This is meant to be used with\n' printf ' Other software like programming languages that want to make use of the\n' printf ' parsing and math capabilities. This option will install headers using\n' printf ' `make install`.\n' printf ' -b, --bc-only\n' printf ' Build bc only. It is an error if "-d", "--dc-only", "-B", or\n' printf ' "--disable-bc" are specified too.\n' printf ' -B, --disable-bc\n' printf ' Disable bc. It is an error if "-b", "--bc-only", "-D", or "--disable-dc"\n' printf ' are specified too.\n' printf ' -c, --coverage\n' printf ' Generate test coverage code. Requires gcov and gcovr.\n' printf ' It is an error if either "-b" ("-D") or "-d" ("-B") is specified.\n' printf ' Requires a compiler that use gcc-compatible coverage options\n' printf ' -C, --disable-clean\n' printf ' Disable the clean that configure.sh does before configure.\n' printf ' -d, --dc-only\n' printf ' Build dc only. It is an error if "-b", "--bc-only", "-D", or\n' printf ' "--disable-dc" are specified too.\n' printf ' -D, --disable-dc\n' printf ' Disable dc. It is an error if "-d", "--dc-only", "-B", or "--disable-bc"\n' printf ' are specified too.\n' + printf ' -e, --enable-editline\n' + printf ' Enable the use of libedit/editline. This is meant for those users that\n' + printf ' want vi-like or Emacs-like behavior in history. This option is ignored\n' + printf ' if history is disabled. If the -r or -i options are given with this\n' + printf ' option, the last occurrence of all of the three is used.\n' printf ' -E, --disable-extra-math\n' printf ' Disable extra math. This includes: "$" operator (truncate to integer),\n' printf ' "@" operator (set number of decimal places), and r(x, p) (rounding\n' printf ' function). Additionally, this option disables the extra printing\n' printf ' functions in the math library.\n' printf ' -f, --force\n' printf ' Force use of all enabled options, even if they do not work. This\n' printf ' option is to allow the maintainer a way to test that certain options\n' printf ' are not failing invisibly. (Development only.)\n' printf ' -g, --debug\n' printf ' Build in debug mode. Adds the "-g" flag, and if there are no\n' printf ' other CFLAGS, and "-O" was not given, this also adds the "-O0"\n' printf ' flag. If this flag is *not* given, "-DNDEBUG" is added to CPPFLAGS\n' printf ' and a strip flag is added to the link stage.\n' printf ' -G, --disable-generated-tests\n' printf ' Disable generating tests. This is for platforms that do not have a\n' printf ' GNU bc-compatible bc to generate tests.\n' printf ' -h, --help\n' printf ' Print this help message and exit.\n' printf ' -H, --disable-history\n' printf ' Disable history.\n' + printf ' -i, --enable-internal-history\n' + printf ' Enable the internal history implementation and do not depend on either\n' + printf ' editline or readline. This option is ignored if history is disabled.\n' + printf ' If this option is given along with -e and -r, the last occurrence of\n' + printf ' all of the three is used.\n' printf ' -k KARATSUBA_LEN, --karatsuba-len KARATSUBA_LEN\n' printf ' Set the karatsuba length to KARATSUBA_LEN (default is 64).\n' printf ' It is an error if KARATSUBA_LEN is not a number or is less than 16.\n' printf ' -l, --install-all-locales\n' printf ' Installs all locales, regardless of how many are on the system. This\n' printf ' option is useful for package maintainers who want to make sure that\n' printf ' a package contains all of the locales that end users might need.\n' printf ' -m, --enable-memcheck\n' printf ' Enable memcheck mode, to ensure no memory leaks. For development only.\n' printf ' -M, --disable-man-pages\n' printf ' Disable installing manpages.\n' printf ' -N, --disable-nls\n' printf ' Disable POSIX locale (NLS) support.\n' + printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n' + printf ' installed at a fixed location to work at all. If you do not want that\n' + printf ' to happen, you must disable locales (NLS) completely.\n' printf ' -O OPT_LEVEL, --opt OPT_LEVEL\n' printf ' Set the optimization level. This can also be included in the CFLAGS,\n' printf ' but it is provided, so maintainers can build optimized debug builds.\n' printf ' This is passed through to the compiler, so it must be supported.\n' + printf ' -p TYPE, --predefined-build-type=TYPE\n' + printf ' Sets a given predefined build type with specific defaults. This is for\n' + printf ' easy setting of predefined builds. For example, to get a build that\n' + printf ' acts like the GNU bc by default, TYPE should be "GNU" (without the\n' + printf ' quotes) This option *must* come before any others that might change the\n' + printf ' build options. Currently supported values for TYPE include: "BSD" (for\n' + printf ' matching the BSD bc and BSD dc), "GNU" (for matching the GNU bc and\n' + printf ' dc), "GDH" (for the preferred build of the author, Gavin D. Howard),\n' + printf ' and "DBG" (for the preferred debug build of the author). This will\n' + printf ' also automatically enable a release build (except for "DBG").\n' + printf ' -P, --disable-problematic-tests\n' + printf ' Disables problematic tests. These tests usually include tests that\n' + printf ' can cause a SIGKILL because of too much memory usage.\n' + printf ' -r, --enable-readline\n' + printf ' Enable the use of libreadline/readline. This is meant for those users\n' + printf ' that want vi-like or Emacs-like behavior in history. This option is\n' + printf ' ignored if history is disabled. If this option is given along with -e\n' + printf ' and -r, the last occurrence of all of the three is used.\n' printf ' -s SETTING, --set-default-on SETTING\n' printf ' Set the default named by SETTING to on. See below for possible values\n' printf ' for SETTING. For multiple instances of the -s or -S for the the same\n' printf ' setting, the last one is used.\n' printf ' -S SETTING, --set-default-off SETTING\n' printf ' Set the default named by SETTING to off. See below for possible values\n' printf ' for SETTING. For multiple instances of the -s or -S for the the same\n' printf ' setting, the last one is used.\n' printf ' -t, --enable-test-timing\n' printf ' Enable the timing of tests. This is for development only.\n' printf ' -T, --disable-strip\n' printf ' Disable stripping symbols from the compiled binary or binaries.\n' printf ' Stripping symbols only happens when debug mode is off.\n' printf ' -v, --enable-valgrind\n' printf ' Enable a build appropriate for valgrind. For development only.\n' printf ' -z, --enable-fuzz-mode\n' printf ' Enable fuzzing mode. THIS IS FOR DEVELOPMENT ONLY.\n' printf ' --prefix PREFIX\n' printf ' The prefix to install to. Overrides "$PREFIX" if it exists.\n' printf ' If PREFIX is "/usr", install path will be "/usr/bin".\n' printf ' Default is "/usr/local".\n' + printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n' + printf ' installed at a fixed location to work at all. If you do not want that to\n' + printf ' happen, you must disable locales (NLS) completely.\n' printf ' --bindir BINDIR\n' printf ' The directory to install binaries in. Overrides "$BINDIR" if it exists.\n' printf ' Default is "$PREFIX/bin".\n' printf ' --includedir INCLUDEDIR\n' printf ' The directory to install headers in. Overrides "$INCLUDEDIR" if it\n' printf ' exists. Default is "$PREFIX/include".\n' printf ' --libdir LIBDIR\n' printf ' The directory to install libraries in. Overrides "$LIBDIR" if it exists.\n' printf ' Default is "$PREFIX/lib".\n' printf ' --datarootdir DATAROOTDIR\n' printf ' The root location for data files. Overrides "$DATAROOTDIR" if it exists.\n' printf ' Default is "$PREFIX/share".\n' printf ' --datadir DATADIR\n' printf ' The location for data files. Overrides "$DATADIR" if it exists.\n' printf ' Default is "$DATAROOTDIR".\n' printf ' --mandir MANDIR\n' printf ' The location to install manpages to. Overrides "$MANDIR" if it exists.\n' printf ' Default is "$DATADIR/man".\n' printf ' --man1dir MAN1DIR\n' printf ' The location to install Section 1 manpages to. Overrides "$MAN1DIR" if\n' printf ' it exists. Default is "$MANDIR/man1".\n' printf ' --man3dir MAN3DIR\n' printf ' The location to install Section 3 manpages to. Overrides "$MAN3DIR" if\n' printf ' it exists. Default is "$MANDIR/man3".\n' printf '\n' printf 'In addition, the following environment variables are used:\n' printf '\n' printf ' CC C compiler. Must be compatible with POSIX c99. If there is a\n' printf ' space in the basename of the compiler, the items after the\n' printf ' first space are assumed to be compiler flags, and in that case,\n' printf ' the flags are automatically moved into CFLAGS. Default is\n' printf ' "c99".\n' printf ' HOSTCC Host C compiler. Must be compatible with POSIX c99. If there is\n' printf ' a space in the basename of the compiler, the items after the\n' printf ' first space are assumed to be compiler flags, and in the case,\n' printf ' the flags are automatically moved into HOSTCFLAGS. Default is\n' printf ' "$CC".\n' printf ' HOST_CC Same as HOSTCC. If HOSTCC also exists, it is used.\n' printf ' CFLAGS C compiler flags.\n' printf ' HOSTCFLAGS CFLAGS for HOSTCC. Default is "$CFLAGS".\n' printf ' HOST_CFLAGS Same as HOST_CFLAGS. If HOST_CFLAGS also exists, it is used.\n' printf ' CPPFLAGS C preprocessor flags. Default is "".\n' printf ' LDFLAGS Linker flags. Default is "".\n' printf ' PREFIX The prefix to install to. Default is "/usr/local".\n' printf ' If PREFIX is "/usr", install path will be "/usr/bin".\n' + printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n' + printf ' installed at a fixed location to work at all. If you do not\n' + printf ' want that to happen, you must disable locales (NLS) completely.\n' printf ' BINDIR The directory to install binaries in. Default is "$PREFIX/bin".\n' printf ' INCLUDEDIR The directory to install header files in. Default is\n' printf ' "$PREFIX/include".\n' printf ' LIBDIR The directory to install libraries in. Default is\n' printf ' "$PREFIX/lib".\n' printf ' DATAROOTDIR The root location for data files. Default is "$PREFIX/share".\n' printf ' DATADIR The location for data files. Default is "$DATAROOTDIR".\n' printf ' MANDIR The location to install manpages to. Default is "$DATADIR/man".\n' printf ' MAN1DIR The location to install Section 1 manpages to. Default is\n' printf ' "$MANDIR/man1".\n' printf ' MAN3DIR The location to install Section 3 manpages to. Default is\n' printf ' "$MANDIR/man3".\n' printf ' NLSPATH The location to install locale catalogs to. Must be an absolute\n' printf ' path (or contain one). This is treated the same as the POSIX\n' printf ' definition of $NLSPATH (see POSIX environment variables for\n' printf ' more information). Default is "/usr/share/locale/%%L/%%N".\n' printf ' PC_PATH The location to install pkg-config files to. Must be an\n' printf ' path or contain one. Default is the first path given by the\n' printf ' output of `pkg-config --variable=pc_path pkg-config`.\n' printf ' EXECSUFFIX The suffix to append to the executable names, used to not\n' printf ' interfere with other installed bc executables. Default is "".\n' printf ' EXECPREFIX The prefix to append to the executable names, used to not\n' printf ' interfere with other installed bc executables. Default is "".\n' printf ' DESTDIR For package creation. Default is "". If it is empty when\n' printf ' `%s` is run, it can also be passed to `make install`\n' "$script" printf ' later as an environment variable. If both are specified,\n' printf ' the one given to `%s` takes precedence.\n' "$script" printf ' LONG_BIT The number of bits in a C `long` type. This is mostly for the\n' printf ' embedded space since this `bc` uses `long`s internally for\n' printf ' overflow checking. In C99, a `long` is required to be 32 bits.\n' printf ' For most normal desktop systems, setting this is unnecessary,\n' printf ' except that 32-bit platforms with 64-bit longs may want to set\n' printf ' it to `32`. Default is the default of `LONG_BIT` for the target\n' printf ' platform. Minimum allowed is `32`. It is a build time error if\n' printf ' the specified value of `LONG_BIT` is greater than the default\n' printf ' value of `LONG_BIT` for the target platform.\n' printf ' GEN_HOST Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to\n' printf ' produce the C files that contain the help texts as well as the\n' printf ' math libraries. By default, `gen/strgen.c` is used, compiled by\n' printf ' "$HOSTCC" and run on the host machine. Using `gen/strgen.sh`\n' printf ' removes the need to compile and run an executable on the host\n' printf ' machine since `gen/strgen.sh` is a POSIX shell script. However,\n' - printf ' `gen/lib2.bc` is perilously close to 4095 characters, the max\n' - printf ' supported length of a string literal in C99 (and it could be\n' - printf ' added to in the future), and `gen/strgen.sh` generates a string\n' - printf ' literal instead of an array, as `gen/strgen.c` does. For most\n' - printf ' production-ready compilers, this limit probably is not\n' + printf ' `gen/lib2.bc` is over 4095 characters, the max supported length\n' + printf ' of a string literal in C99, and `gen/strgen.sh` generates a\n' + printf ' string literal instead of an array, as `gen/strgen.c` does. For\n' + printf ' most production-ready compilers, this limit probably is not\n' printf ' enforced, but it could be. Both options are still available for\n' printf ' this reason. If you are sure your compiler does not have the\n' printf ' limit and do not want to compile and run a binary on the host\n' printf ' machine, set this variable to "0". Any other value, or a\n' printf ' non-existent value, will cause the build system to compile and\n' printf ' run `gen/strgen.c`. Default is "".\n' printf ' GEN_EMU Emulator to run string generator code under (leave empty if not\n' printf ' necessary). This is not necessary when using `gen/strgen.sh`.\n' printf ' Default is "".\n' printf '\n' printf 'WARNING: even though `configure.sh` supports both option types, short and\n' printf 'long, it does not support handling both at the same time. Use only one type.\n' printf '\n' printf 'Settings\n' printf '========\n' printf '\n' printf 'bc and dc have some settings that, while they cannot be removed by build time\n' printf 'options, can have their defaults changed at build time by packagers. Users are\n' printf 'also able to change each setting with environment variables.\n' printf '\n' printf 'The following is a table of settings, along with their default values and the\n' printf 'environment variables users can use to change them. (For the defaults, non-zero\n' printf 'means on, and zero means off.)\n' printf '\n' printf '| Setting | Description | Default | Env Variable |\n' printf '| =============== | ==================== | ============ | ==================== |\n' printf '| bc.banner | Whether to display | 0 | BC_BANNER |\n' printf '| | the bc version | | |\n' printf '| | banner when in | | |\n' printf '| | interactive mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.sigint_reset | Whether SIGINT will | 1 | BC_SIGINT_RESET |\n' printf '| | reset bc, instead of | | |\n' printf '| | exiting, when in | | |\n' printf '| | interactive mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.sigint_reset | Whether SIGINT will | 1 | DC_SIGINT_RESET |\n' printf '| | reset dc, instead of | | |\n' printf '| | exiting, when in | | |\n' printf '| | interactive mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.tty_mode | Whether TTY mode for | 1 | BC_TTY_MODE |\n' printf '| | bc should be on when | | |\n' printf '| | available. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.tty_mode | Whether TTY mode for | 0 | BC_TTY_MODE |\n' printf '| | dc should be on when | | |\n' printf '| | available. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.prompt | Whether the prompt | $BC_TTY_MODE | BC_PROMPT |\n' printf '| | for bc should be on | | |\n' printf '| | in tty mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.prompt | Whether the prompt | $DC_TTY_MODE | DC_PROMPT |\n' printf '| | for dc should be on | | |\n' printf '| | in tty mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.expr_exit | Whether to exit bc | 1 | BC_EXPR_EXIT |\n' printf '| | if an expression or | | |\n' printf '| | expression file is | | |\n' printf '| | given with the -e or | | |\n' printf '| | -f options. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.expr_exit | Whether to exit dc | 1 | DC_EXPR_EXIT |\n' printf '| | if an expression or | | |\n' printf '| | expression file is | | |\n' printf '| | given with the -e or | | |\n' printf '| | -f options. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' + printf '| bc.digit_clamp | Whether to have bc | 0 | BC_DIGIT_CLAMP |\n' + printf '| | clamp digits that | | |\n' + printf '| | are greater than or | | |\n' + printf '| | equal to the current | | |\n' + printf '| | ibase when parsing | | |\n' + printf '| | numbers. | | |\n' + printf '| --------------- | -------------------- | ------------ | -------------------- |\n' + printf '| dc.digit_clamp | Whether to have dc | 0 | DC_DIGIT_CLAMP |\n' + printf '| | clamp digits that | | |\n' + printf '| | are greater than or | | |\n' + printf '| | equal to the current | | |\n' + printf '| | ibase when parsing | | |\n' + printf '| | numbers. | | |\n' + printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '\n' printf 'These settings are not meant to be changed on a whim. They are meant to ensure\n' printf 'that this bc and dc will conform to the expectations of the user on each\n' printf 'platform.\n' exit "$_usage_val" } # Replaces a file extension in a filename. This is used mostly to turn filenames # like `src/num.c` into `src/num.o`. In other words, it helps to link targets to # the files they depend on. # # @param file The filename. # @param ext1 The extension to replace. # @param ext2 The new extension. replace_ext() { if [ "$#" -ne 3 ]; then err_exit "Invalid number of args to $0" fi _replace_ext_file="$1" _replace_ext_ext1="$2" _replace_ext_ext2="$3" _replace_ext_result="${_replace_ext_file%.$_replace_ext_ext1}.$_replace_ext_ext2" printf '%s\n' "$_replace_ext_result" } # Replaces a file extension in every filename given in a list. The list is just # a space-separated list of words, so filenames are expected to *not* have # spaces in them. See the documentation for `replace_ext()`. # # @param files The list of space-separated filenames to replace extensions for. # @param ext1 The extension to replace. # @param ext2 The new extension. replace_exts() { if [ "$#" -ne 3 ]; then err_exit "Invalid number of args to $0" fi _replace_exts_files="$1" _replace_exts_ext1="$2" _replace_exts_ext2="$3" for _replace_exts_file in $_replace_exts_files; do _replace_exts_new_name=$(replace_ext "$_replace_exts_file" "$_replace_exts_ext1" "$_replace_exts_ext2") _replace_exts_result="$_replace_exts_result $_replace_exts_new_name" done printf '%s\n' "$_replace_exts_result" } # Finds a placeholder in @a str and replaces it. This is the workhorse of # configure.sh. It's what replaces placeholders in Makefile.in with the data # needed for the chosen build. Below, you will see a lot of calls to this # function. # # Note that needle can never contain an exclamation point. For more information, # see substring_replace() in scripts/functions.sh. # # @param str The string to find and replace placeholders in. # @param needle The placeholder name. # @param replacement The string to use to replace the placeholder. replace() { if [ "$#" -ne 3 ]; then err_exit "Invalid number of args to $0" fi _replace_str="$1" _replace_needle="$2" _replace_replacement="$3" substring_replace "$_replace_str" "%%$_replace_needle%%" "$_replace_replacement" } # This function finds all the source files that need to be built. If there is # only one argument and it is empty, then all source files are built. Otherwise, # the arguments are all assumed to be source files that should *not* be built. find_src_files() { _find_src_files_args="" if [ "$#" -ge 1 ] && [ "$1" != "" ]; then while [ "$#" -ge 1 ]; do _find_src_files_a="${1## }" shift _find_src_files_args=$(printf '%s\n%s/src/%s\n' "$_find_src_files_args" "$scriptdir" "${_find_src_files_a}") done fi - _find_src_files_files=$(find "$scriptdir/src/" -depth -name "*.c" -print) + _find_src_files_files=$(find "$scriptdir/src/" -depth -name "*.c" -print | LC_ALL=C sort) _find_src_files_result="" for _find_src_files_f in $_find_src_files_files; do # If this is true, the file is part of args, and therefore, unneeded. if [ "${_find_src_files_args##*$_find_src_files_f}" != "${_find_src_files_args}" ]; then continue fi _find_src_files_result=$(printf '%s\n%s\n' "$_find_src_files_result" "$_find_src_files_f") done printf '%s\n' "$_find_src_files_result" } # This function generates a list of files to go into the Makefile. It generates # the list of object files, as well as the list of test coverage files. # # @param contents The contents of the Makefile template to put the list of # files into. gen_file_list() { if [ "$#" -lt 1 ]; then err_exit "Invalid number of args to $0" fi _gen_file_list_contents="$1" shift if [ "$#" -ge 1 ]; then _gen_file_list_unneeded="$@" else _gen_file_list_unneeded="" fi _gen_file_list_needle_src="SRC" _gen_file_list_needle_obj="OBJ" _gen_file_list_needle_gcda="GCDA" _gen_file_list_needle_gcno="GCNO" _gen_file_list_replacement=$(find_src_files $_gen_file_list_unneeded | tr '\n' ' ') _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_src" "$_gen_file_list_replacement") _gen_file_list_cbases="" for _gen_file_list_f in $_gen_file_list_replacement; do _gen_file_list_b=$(basename "$_gen_file_list_f") _gen_file_list_cbases="$_gen_file_list_cbases src/$_gen_file_list_b" done _gen_file_list_replacement=$(replace_exts "$_gen_file_list_cbases" "c" "o") _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_obj" "$_gen_file_list_replacement") _gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "o" "gcda") _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_gcda" "$_gen_file_list_replacement") _gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "gcda" "gcno") _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_gcno" "$_gen_file_list_replacement") printf '%s\n' "$_gen_file_list_contents" } # Generates the proper test targets for each test to have its own target. This # allows `make test` to run in parallel. # # @param name Which calculator to generate tests for. # @param extra_math An integer that, if non-zero, activates extra math tests. # @param time_tests An integer that, if non-zero, tells the test suite to time # the execution of each test. gen_std_tests() { _gen_std_tests_name="$1" shift _gen_std_tests_extra_math="$1" shift _gen_std_tests_time_tests="$1" shift _gen_std_tests_extra_required=$(cat "$scriptdir/tests/extra_required.txt") for _gen_std_tests_t in $(cat "$scriptdir/tests/$_gen_std_tests_name/all.txt"); do if [ "$_gen_std_tests_extra_math" -eq 0 ]; then if [ -z "${_gen_std_tests_extra_required##*$_gen_std_tests_t*}" ]; then printf 'test_%s_%s:\n\t@printf "Skipping %s %s\\n"\n\n' \ "$_gen_std_tests_name" "$_gen_std_tests_t" "$_gen_std_tests_name" \ "$_gen_std_tests_t" >> "Makefile" continue fi fi - printf 'test_%s_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh \$(TESTSDIR)/test.sh %s %s %s %s %s\n\n' \ + printf 'test_%s_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/test.sh %s %s %s %s %s\n\n' \ "$_gen_std_tests_name" "$_gen_std_tests_t" "$builddir" "$_gen_std_tests_name" \ "$_gen_std_tests_t" "$generate_tests" "$time_tests" \ "$*" >> "Makefile" done } # Generates a list of test targets that will be used as prerequisites for other # targets. # # @param name The name of the calculator to generate test targets for. gen_std_test_targets() { _gen_std_test_targets_name="$1" shift _gen_std_test_targets_tests=$(cat "$scriptdir/tests/${_gen_std_test_targets_name}/all.txt") for _gen_std_test_targets_t in $_gen_std_test_targets_tests; do printf ' test_%s_%s' "$_gen_std_test_targets_name" "$_gen_std_test_targets_t" done printf '\n' } # Generates the proper test targets for each error test to have its own target. # This allows `make test_bc_errors` and `make test_dc_errors` to run in # parallel. # # @param name Which calculator to generate tests for. gen_err_tests() { _gen_err_tests_name="$1" shift _gen_err_tests_fs=$(ls "$scriptdir/tests/$_gen_err_tests_name/errors/") for _gen_err_tests_t in $_gen_err_tests_fs; do - printf 'test_%s_error_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh \$(TESTSDIR)/error.sh %s %s %s\n\n' \ + printf 'test_%s_error_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/error.sh %s %s %s %s\n\n' \ "$_gen_err_tests_name" "$_gen_err_tests_t" "$builddir" "$_gen_err_tests_name" \ - "$_gen_err_tests_t" "$*" >> "Makefile" + "$_gen_err_tests_t" "$problematic_tests" "$*" >> "Makefile" done } # Generates a list of error test targets that will be used as prerequisites for # other targets. # # @param name The name of the calculator to generate test targets for. gen_err_test_targets() { _gen_err_test_targets_name="$1" shift _gen_err_test_targets_tests=$(ls "$scriptdir/tests/$_gen_err_test_targets_name/errors/") for _gen_err_test_targets_t in $_gen_err_test_targets_tests; do printf ' test_%s_error_%s' "$_gen_err_test_targets_name" "$_gen_err_test_targets_t" done printf '\n' } # Generates the proper script test targets for each script test to have its own # target. This allows `make test` to run in parallel. # # @param name Which calculator to generate tests for. # @param extra_math An integer that, if non-zero, activates extra math tests. # @param generate An integer that, if non-zero, activates generated tests. # @param time_tests An integer that, if non-zero, tells the test suite to time # the execution of each test. gen_script_tests() { _gen_script_tests_name="$1" shift _gen_script_tests_extra_math="$1" shift _gen_script_tests_generate="$1" shift _gen_script_tests_time="$1" shift _gen_script_tests_tests=$(cat "$scriptdir/tests/$_gen_script_tests_name/scripts/all.txt") for _gen_script_tests_f in $_gen_script_tests_tests; do _gen_script_tests_b=$(basename "$_gen_script_tests_f" ".${_gen_script_tests_name}") - printf 'test_%s_script_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh \$(TESTSDIR)/script.sh %s %s %s 1 %s %s %s\n\n' \ + printf 'test_%s_script_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/script.sh %s %s %s 1 %s %s %s\n\n' \ "$_gen_script_tests_name" "$_gen_script_tests_b" "$builddir" "$_gen_script_tests_name" \ "$_gen_script_tests_f" "$_gen_script_tests_extra_math" "$_gen_script_tests_generate" \ "$_gen_script_tests_time" "$*" >> "Makefile" done } set_default() { _set_default_on="$1" shift _set_default_name="$1" shift # The reason that the variables that are being set do not have the same # non-collision avoidance that the other variables do is that we *do* want # the settings of these variables to leak out of the function. They adjust # the settings outside of the function. case "$_set_default_name" in bc.banner) bc_default_banner="$_set_default_on" ;; bc.sigint_reset) bc_default_sigint_reset="$_set_default_on" ;; dc.sigint_reset) dc_default_sigint_reset="$_set_default_on" ;; bc.tty_mode) bc_default_tty_mode="$_set_default_on" ;; dc.tty_mode) dc_default_tty_mode="$_set_default_on" ;; bc.prompt) bc_default_prompt="$_set_default_on" ;; dc.prompt) dc_default_prompt="$_set_default_on" ;; bc.expr_exit) bc_default_expr_exit="$_set_default_on";; dc.expr_exit) dc_default_expr_exit="$_set_default_on";; + bc.digit_clamp) bc_default_digit_clamp="$_set_default_on";; + dc.digit_clamp) dc_default_digit_clamp="$_set_default_on";; ?) usage "Invalid setting: $_set_default_name" ;; esac } +predefined_build() { + + _predefined_build_type="$1" + shift + + # The reason that the variables that are being set do not have the same + # non-collision avoidance that the other variables do is that we *do* want + # the settings of these variables to leak out of the function. They adjust + # the settings outside of the function. + case "$_predefined_build_type" in + + BSD) + bc_only=0 + dc_only=0 + coverage=0 + debug=0 + optimization="3" + hist=1 + hist_impl="editline" + extra_math=1 + generate_tests=$generate_tests + install_manpages=0 + nls=1 + force=0 + strip_bin=1 + all_locales=0 + library=0 + fuzz=0 + time_tests=0 + vg=0 + memcheck=0 + clean=1 + bc_default_banner=0 + bc_default_sigint_reset=1 + dc_default_sigint_reset=1 + bc_default_tty_mode=1 + dc_default_tty_mode=0 + bc_default_prompt="" + dc_default_prompt="" + bc_default_expr_exit=1 + dc_default_expr_exit=1 + bc_default_digit_clamp=0 + dc_default_digit_clamp=0;; + + GNU) + bc_only=0 + dc_only=0 + coverage=0 + debug=0 + optimization="3" + hist=1 + hist_impl="internal" + extra_math=1 + generate_tests=$generate_tests + install_manpages=1 + nls=1 + force=0 + strip_bin=1 + all_locales=0 + library=0 + fuzz=0 + time_tests=0 + vg=0 + memcheck=0 + clean=1 + bc_default_banner=1 + bc_default_sigint_reset=1 + dc_default_sigint_reset=0 + bc_default_tty_mode=1 + dc_default_tty_mode=0 + bc_default_prompt="" + dc_default_prompt="" + bc_default_expr_exit=1 + dc_default_expr_exit=1 + bc_default_digit_clamp=1 + dc_default_digit_clamp=0;; + + GDH) + CFLAGS="-flto -Weverything -Wno-padded -Werror -pedantic -std=c11" + bc_only=0 + dc_only=0 + coverage=0 + debug=0 + optimization="3" + hist=1 + hist_impl="internal" + extra_math=1 + generate_tests=1 + install_manpages=1 + nls=0 + force=0 + strip_bin=1 + all_locales=0 + library=0 + fuzz=0 + time_tests=0 + vg=0 + memcheck=0 + clean=1 + bc_default_banner=1 + bc_default_sigint_reset=1 + dc_default_sigint_reset=1 + bc_default_tty_mode=1 + dc_default_tty_mode=1 + bc_default_prompt="" + dc_default_prompt="" + bc_default_expr_exit=0 + dc_default_expr_exit=0 + bc_default_digit_clamp=1 + dc_default_digit_clamp=1;; + + DBG) + CFLAGS="-Weverything -Wno-padded -Werror -pedantic -std=c11" + bc_only=0 + dc_only=0 + coverage=0 + debug=1 + optimization="0" + hist=1 + hist_impl="internal" + extra_math=1 + generate_tests=1 + install_manpages=1 + nls=1 + force=0 + strip_bin=1 + all_locales=0 + library=0 + fuzz=0 + time_tests=0 + vg=0 + memcheck=1 + clean=1 + bc_default_banner=1 + bc_default_sigint_reset=1 + dc_default_sigint_reset=1 + bc_default_tty_mode=1 + dc_default_tty_mode=1 + bc_default_prompt="" + dc_default_prompt="" + bc_default_expr_exit=0 + dc_default_expr_exit=0 + bc_default_digit_clamp=1 + dc_default_digit_clamp=1;; + + ?|'') usage "Invalid user build: \"$_predefined_build_type\". Accepted types are BSD, GNU, GDH, DBG.";; + + esac +} + # Generates a list of script test targets that will be used as prerequisites for # other targets. # # @param name The name of the calculator to generate script test targets for. gen_script_test_targets() { _gen_script_test_targets_name="$1" shift _gen_script_test_targets_tests=$(cat "$scriptdir/tests/$_gen_script_test_targets_name/scripts/all.txt") for _gen_script_test_targets_f in $_gen_script_test_targets_tests; do _gen_script_test_targets_b=$(basename "$_gen_script_test_targets_f" \ ".$_gen_script_test_targets_name") printf ' test_%s_script_%s' "$_gen_script_test_targets_name" \ "$_gen_script_test_targets_b" done printf '\n' } # This is a list of defaults, but it is also the list of possible options for # users to change. # # The development options are: force (force options even if they fail), valgrind # (build in a way suitable for valgrind testing), memcheck (same as valgrind), # and fuzzing (build in a way suitable for fuzzing). bc_only=0 dc_only=0 coverage=0 karatsuba_len=32 debug=0 hist=1 +hist_impl="internal" extra_math=1 optimization="" generate_tests=1 install_manpages=1 nls=1 force=0 strip_bin=1 all_locales=0 library=0 fuzz=0 time_tests=0 vg=0 memcheck=0 clean=1 +problematic_tests=1 # The empty strings are because they depend on TTY mode. If they are directly # set, though, they will be integers. We test for empty strings later. bc_default_banner=0 bc_default_sigint_reset=1 dc_default_sigint_reset=1 bc_default_tty_mode=1 dc_default_tty_mode=0 bc_default_prompt="" dc_default_prompt="" bc_default_expr_exit=1 dc_default_expr_exit=1 +bc_default_digit_clamp=0 +dc_default_digit_clamp=0 # getopts is a POSIX utility, but it cannot handle long options. Thus, the # handling of long options is done by hand, and that's the reason that short and # long options cannot be mixed. -while getopts "abBcdDEfgGhHk:lMmNO:S:s:tTvz-" opt; do +while getopts "abBcdDeEfgGhHik:lMmNO:p:PrS:s:tTvz-" opt; do case "$opt" in a) library=1 ;; b) bc_only=1 ;; B) dc_only=1 ;; c) coverage=1 ;; C) clean=0 ;; d) dc_only=1 ;; D) bc_only=1 ;; + e) hist_impl="editline" ;; E) extra_math=0 ;; f) force=1 ;; g) debug=1 ;; G) generate_tests=0 ;; h) usage ;; H) hist=0 ;; + i) hist_impl="internal" ;; k) karatsuba_len="$OPTARG" ;; l) all_locales=1 ;; m) memcheck=1 ;; M) install_manpages=0 ;; N) nls=0 ;; O) optimization="$OPTARG" ;; + p) predefined_build "$OPTARG" ;; + P) problematic_tests=0 ;; + r) hist_impl="readline" ;; S) set_default 0 "$OPTARG" ;; s) set_default 1 "$OPTARG" ;; t) time_tests=1 ;; T) strip_bin=0 ;; v) vg=1 ;; z) fuzz=1 ;; -) arg="$1" arg="${arg#--}" LONG_OPTARG="${arg#*=}" case $arg in help) usage ;; library) library=1 ;; bc-only) bc_only=1 ;; dc-only) dc_only=1 ;; coverage) coverage=1 ;; debug) debug=1 ;; force) force=1 ;; prefix=?*) PREFIX="$LONG_OPTARG" ;; prefix) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi PREFIX="$2" shift ;; bindir=?*) BINDIR="$LONG_OPTARG" ;; bindir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi BINDIR="$2" shift ;; includedir=?*) INCLUDEDIR="$LONG_OPTARG" ;; includedir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi INCLUDEDIR="$2" shift ;; libdir=?*) LIBDIR="$LONG_OPTARG" ;; libdir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi LIBDIR="$2" shift ;; datarootdir=?*) DATAROOTDIR="$LONG_OPTARG" ;; datarootdir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi DATAROOTDIR="$2" shift ;; datadir=?*) DATADIR="$LONG_OPTARG" ;; datadir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi DATADIR="$2" shift ;; mandir=?*) MANDIR="$LONG_OPTARG" ;; mandir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi MANDIR="$2" shift ;; man1dir=?*) MAN1DIR="$LONG_OPTARG" ;; man1dir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi MAN1DIR="$2" shift ;; man3dir=?*) MAN3DIR="$LONG_OPTARG" ;; man3dir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi MAN3DIR="$2" shift ;; - localedir=?*) LOCALEDIR="$LONG_OPTARG" ;; - localedir) - if [ "$#" -lt 2 ]; then - usage "No argument given for '--$arg' option" - fi - LOCALEDIR="$2" - shift ;; karatsuba-len=?*) karatsuba_len="$LONG_OPTARG" ;; karatsuba-len) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi karatsuba_len="$1" shift ;; opt=?*) optimization="$LONG_OPTARG" ;; opt) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi optimization="$1" shift ;; set-default-on=?*) set_default 1 "$LONG_OPTARG" ;; set-default-on) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi set_default 1 "$1" shift ;; set-default-off=?*) set_default 0 "$LONG_OPTARG" ;; set-default-off) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi set_default 0 "$1" shift ;; + predefined-build-type=?*) predefined_build "$LONG_OPTARG" ;; + predefined-build-type) + if [ "$#" -lt 2 ]; then + usage "No argument given for '--$arg' option" + fi + predefined_build "$1" + shift ;; disable-bc) dc_only=1 ;; disable-dc) bc_only=1 ;; disable-clean) clean=0 ;; disable-extra-math) extra_math=0 ;; disable-generated-tests) generate_tests=0 ;; disable-history) hist=0 ;; disable-man-pages) install_manpages=0 ;; disable-nls) nls=0 ;; disable-strip) strip_bin=0 ;; + disable-problematic-tests) problematic_tests=0 ;; + enable-editline) hist_impl="editline" ;; + enable-readline) hist_impl="readline" ;; + enable-internal-history) hist_impl="internal" ;; enable-test-timing) time_tests=1 ;; enable-valgrind) vg=1 ;; enable-fuzz-mode) fuzz=1 ;; enable-memcheck) memcheck=1 ;; install-all-locales) all_locales=1 ;; help* | bc-only* | dc-only* | coverage* | debug*) usage "No arg allowed for --$arg option" ;; disable-bc* | disable-dc* | disable-clean*) usage "No arg allowed for --$arg option" ;; disable-extra-math*) usage "No arg allowed for --$arg option" ;; disable-generated-tests* | disable-history*) usage "No arg allowed for --$arg option" ;; disable-man-pages* | disable-nls* | disable-strip*) usage "No arg allowed for --$arg option" ;; + disable-problematic-tests*) + usage "No arg allowed for --$arg option" ;; enable-fuzz-mode* | enable-test-timing* | enable-valgrind*) usage "No arg allowed for --$arg option" ;; enable-memcheck* | install-all-locales*) usage "No arg allowed for --$arg option" ;; + enable-editline* | enable-readline*) + usage "No arg allowed for --$arg option" ;; + enable-internal-history*) + usage "No arg allowed for --$arg option" ;; '') break ;; # "--" terminates argument processing * ) usage "Invalid option $LONG_OPTARG" ;; esac shift OPTIND=1 ;; ?) usage "Invalid option: $opt" ;; esac done # Sometimes, developers don't want configure.sh to do a config clean. But # sometimes they do. if [ "$clean" -ne 0 ]; then if [ -f ./Makefile ]; then make clean_config > /dev/null fi fi # It is an error to say that bc only should be built and likewise for dc. if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then usage "Can only specify one of -b(-D) or -d(-B)" fi # The library is mutually exclusive to the calculators, so it's an error to # give an option for either of them. if [ "$library" -ne 0 ]; then if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then usage "Must not specify -b(-D) or -d(-B) when building the library" fi fi # KARATSUBA_LEN must be an integer and must be 16 or greater. case $karatsuba_len in (*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;; (*) ;; esac if [ "$karatsuba_len" -lt 16 ]; then usage "KARATSUBA_LEN is less than 16" fi set -e if [ -z "${LONG_BIT+set}" ]; then LONG_BIT_DEFINE="" elif [ "$LONG_BIT" -lt 32 ]; then usage "LONG_BIT is less than 32" else - LONG_BIT_DEFINE="-DBC_LONG_BIT=\$(BC_LONG_BIT)" + LONG_BIT_DEFINE="-DBC_LONG_BIT=$LONG_BIT" fi if [ -z "$CC" ]; then CC="c99" else # I had users complain that, if they gave CFLAGS as part of CC, which # autotools allows in its braindead way, the build would fail with an error. # I don't like adjusting for autotools, but oh well. These lines puts the # stuff after the first space into CFLAGS. ccbase=$(basename "$CC") suffix=" *" prefix="* " if [ "${ccbase%%$suffix}" != "$ccbase" ]; then ccflags="${ccbase#$prefix}" cc="${ccbase%%$suffix}" ccdir=$(dirname "$CC") if [ "$ccdir" = "." ] && [ "${CC#.}" = "$CC" ]; then ccdir="" else ccdir="$ccdir/" fi CC="${ccdir}${cc}" CFLAGS="$CFLAGS $ccflags" fi fi if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then HOSTCC="$CC" elif [ -z "$HOSTCC" ]; then HOSTCC="$HOST_CC" fi if [ "$HOSTCC" != "$CC" ]; then # Like above, this splits HOSTCC and HOSTCFLAGS. ccbase=$(basename "$HOSTCC") suffix=" *" prefix="* " if [ "${ccbase%%$suffix}" != "$ccbase" ]; then ccflags="${ccbase#$prefix}" cc="${ccbase%%$suffix}" ccdir=$(dirname "$HOSTCC") if [ "$ccdir" = "." ] && [ "${HOSTCC#.}" = "$HOSTCC" ]; then ccdir="" else ccdir="$ccdir/" fi HOSTCC="${ccdir}${cc}" HOSTCFLAGS="$HOSTCFLAGS $ccflags" fi fi if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then HOSTCFLAGS="$CFLAGS" elif [ -z "${HOSTCFLAGS+set}" ]; then HOSTCFLAGS="$HOST_CFLAGS" fi # Store these for the cross compilation detection later. OLDCFLAGS="$CFLAGS" OLDHOSTCFLAGS="$HOSTCFLAGS" link="@printf 'No link necessary\\\\n'" main_exec="BC" executable="BC_EXEC" tests="test_bc timeconst test_dc" -bc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh bc $extra_math 1 $generate_tests $time_tests \$(BC_EXEC)" -bc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n bc $extra_math 1 $generate_tests $time_tests \$(BC_EXEC)" -dc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh dc $extra_math 1 $generate_tests $time_tests \$(DC_EXEC)" -dc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n dc $extra_math 1 $generate_tests $time_tests \$(DC_EXEC)" +bc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh bc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(BC_EXEC)" +bc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n bc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(BC_EXEC)" +dc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh dc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(DC_EXEC)" +dc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n dc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(DC_EXEC)" timeconst="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/bc/timeconst.sh \$(TESTSDIR)/bc/scripts/timeconst.bc \$(BC_EXEC)" # In order to have cleanup at exit, we need to be in # debug mode, so don't run valgrind without that. if [ "$vg" -ne 0 ]; then debug=1 bc_test_exec='valgrind $(VALGRIND_ARGS) $(BC_EXEC)' dc_test_exec='valgrind $(VALGRIND_ARGS) $(DC_EXEC)' + bcl_test_exec='valgrind $(VALGRIND_ARGS) $(BCL_TEST)' else bc_test_exec='$(BC_EXEC)' dc_test_exec='$(DC_EXEC)' + bcl_test_exec='$(BCL_TEST)' fi test_bc_history_prereqs="test_bc_history_all" test_dc_history_prereqs="test_dc_history_all" karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'" karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'" bc_lib="\$(GEN_DIR)/lib.o" bc_help="\$(GEN_DIR)/bc_help.o" dc_help="\$(GEN_DIR)/dc_help.o" default_target_prereqs="\$(BIN) \$(OBJS)" default_target_cmd="\$(CC) \$(CFLAGS) \$(OBJS) \$(LDFLAGS) -o \$(EXEC)" default_target="\$(DC_EXEC)" second_target_prereqs="" second_target_cmd="$default_target_cmd" second_target="\$(BC_EXEC)" # This if/else if chain is for setting the defaults that change based on whether # the library is being built, bc only, dc only, or both calculators. if [ "$library" -ne 0 ]; then extra_math=1 nls=0 hist=0 bc=1 dc=1 default_target_prereqs="\$(BIN) \$(OBJ)" default_target_cmd="ar -r -cu \$(LIBBC) \$(OBJ)" default_target="\$(LIBBC)" tests="test_library" test_bc_history_prereqs=" test_bc_history_skip" test_dc_history_prereqs=" test_dc_history_skip" install_prereqs=" install_library" uninstall_prereqs=" uninstall_library" install_man_prereqs=" install_bcl_manpage" uninstall_man_prereqs=" uninstall_bcl_manpage" elif [ "$bc_only" -eq 1 ]; then bc=1 dc=0 dc_help="" executables="bc" dc_test="@printf 'No dc tests to run\\\\n'" dc_test_np="@printf 'No dc tests to run\\\\n'" test_dc_history_prereqs=" test_dc_history_skip" install_prereqs=" install_execs" install_man_prereqs=" install_bc_manpage" uninstall_prereqs=" uninstall_bc" uninstall_man_prereqs=" uninstall_bc_manpage" default_target="\$(BC_EXEC)" second_target="\$(DC_EXEC)" tests="test_bc timeconst" elif [ "$dc_only" -eq 1 ]; then bc=0 dc=1 bc_lib="" bc_help="" executables="dc" main_exec="DC" executable="DC_EXEC" bc_test="@printf 'No bc tests to run\\\\n'" bc_test_np="@printf 'No bc tests to run\\\\n'" test_bc_history_prereqs=" test_bc_history_skip" timeconst="@printf 'timeconst cannot be run because bc is not built\\\\n'" install_prereqs=" install_execs" install_man_prereqs=" install_dc_manpage" uninstall_prereqs=" uninstall_dc" uninstall_man_prereqs=" uninstall_dc_manpage" tests="test_dc" else bc=1 dc=1 executables="bc and dc" karatsuba="@\$(KARATSUBA) 30 0 \$(BC_EXEC)" karatsuba_test="@\$(KARATSUBA) 1 100 \$(BC_EXEC)" if [ "$library" -eq 0 ]; then install_prereqs=" install_execs" install_man_prereqs=" install_bc_manpage install_dc_manpage" uninstall_prereqs=" uninstall_bc uninstall_dc" uninstall_man_prereqs=" uninstall_bc_manpage uninstall_dc_manpage" else install_prereqs=" install_library install_bcl_header" install_man_prereqs=" install_bcl_manpage" uninstall_prereqs=" uninstall_library uninstall_bcl_header" uninstall_man_prereqs=" uninstall_bcl_manpage" tests="test_library" fi second_target_prereqs="$default_target_prereqs" default_target_prereqs="$second_target" default_target_cmd="\$(LINK) \$(BIN) \$(EXEC_PREFIX)\$(DC)" fi # We need specific stuff for fuzzing. if [ "$fuzz" -ne 0 ]; then debug=1 hist=0 nls=0 optimization="3" fi # This sets some necessary things for debug mode. if [ "$debug" -eq 1 ]; then if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then CFLAGS="-O0" fi - CFLAGS="-g $CFLAGS" + ccbase=$(basename "$CC") + + if [ "$ccbase" = "clang" ]; then + CFLAGS="-gdwarf-4 $CFLAGS" + else + CFLAGS="-g $CFLAGS" + fi else CPPFLAGS="-DNDEBUG $CPPFLAGS" if [ "$strip_bin" -ne 0 ]; then LDFLAGS="-s $LDFLAGS" fi fi # Set optimization CFLAGS. if [ -n "$optimization" ]; then CFLAGS="-O$optimization $CFLAGS" fi # Set test coverage defaults. if [ "$coverage" -eq 1 ]; then if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then usage "Can only specify -c without -b or -d" fi CFLAGS="-fprofile-arcs -ftest-coverage -g -O0 $CFLAGS" CPPFLAGS="-DNDEBUG $CPPFLAGS" COVERAGE_OUTPUT="@gcov -pabcdf \$(GCDA) \$(BC_GCDA) \$(DC_GCDA) \$(HISTORY_GCDA) \$(RAND_GCDA)" COVERAGE_OUTPUT="$COVERAGE_OUTPUT;\$(RM) -f \$(GEN)*.gc*" COVERAGE_OUTPUT="$COVERAGE_OUTPUT;gcovr --exclude-unreachable-branches --exclude-throw-branches --html-details --output index.html" COVERAGE_PREREQS=" test coverage_output" else COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'" COVERAGE_PREREQS="" fi # Set some defaults. if [ -z "${DESTDIR+set}" ]; then destdir="" else destdir="DESTDIR = $DESTDIR" fi +# defprefix is for a warning about locales later. if [ -z "${PREFIX+set}" ]; then PREFIX="/usr/local" + defprefix=1 +else + defprefix=0 fi if [ -z "${BINDIR+set}" ]; then BINDIR="$PREFIX/bin" fi if [ -z "${INCLUDEDIR+set}" ]; then INCLUDEDIR="$PREFIX/include" fi if [ -z "${LIBDIR+set}" ]; then LIBDIR="$PREFIX/lib" fi if [ -z "${PC_PATH+set}" ]; then set +e command -v pkg-config > /dev/null err=$? set -e if [ "$err" -eq 0 ]; then PC_PATH=$(pkg-config --variable=pc_path pkg-config) PC_PATH="${PC_PATH%%:*}" else PC_PATH="" fi fi # Set a default for the DATAROOTDIR. This is done if either manpages will be -# installed, or locales are enabled because that's probably where NLS_PATH +# installed, or locales are enabled because that's probably where NLSPATH # points. if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then if [ -z "${DATAROOTDIR+set}" ]; then DATAROOTDIR="$PREFIX/share" fi fi # Set defaults for manpage environment variables. if [ "$install_manpages" -ne 0 ]; then if [ -z "${DATADIR+set}" ]; then DATADIR="$DATAROOTDIR" fi if [ -z "${MANDIR+set}" ]; then MANDIR="$DATADIR/man" fi if [ -z "${MAN1DIR+set}" ]; then MAN1DIR="$MANDIR/man1" fi if [ -z "${MAN3DIR+set}" ]; then MAN3DIR="$MANDIR/man3" fi else install_man_prereqs="" uninstall_man_prereqs="" fi # Here is where we test NLS (the locale system). This is done by trying to # compile src/vm.c, which has the relevant code. If it fails, then it is # disabled. if [ "$nls" -ne 0 ]; then set +e printf 'Testing NLS...\n' flags="-DBC_ENABLE_NLS=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc" flags="$flags -DBC_ENABLE_HISTORY=$hist -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0" flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/" flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700" + ccbase=$(basename "$CC") + + if [ "$ccbase" = "clang" ]; then + flags="$flags -Wno-unreachable-code" + fi + "$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/vm.c" -o "./vm.o" > /dev/null 2>&1 err="$?" rm -rf "./vm.o" # If this errors, it is probably because of building on Windows, # and NLS is not supported on Windows, so disable it. if [ "$err" -ne 0 ]; then printf 'NLS does not work.\n' if [ $force -eq 0 ]; then printf 'Disabling NLS...\n\n' nls=0 else printf 'Forcing NLS...\n\n' fi else printf 'NLS works.\n\n' printf 'Testing gencat...\n' gencat "./en_US.cat" "$scriptdir/locales/en_US.msg" > /dev/null 2>&1 err="$?" rm -rf "./en_US.cat" if [ "$err" -ne 0 ]; then printf 'gencat does not work.\n' if [ $force -eq 0 ]; then printf 'Disabling NLS...\n\n' nls=0 else printf 'Forcing NLS...\n\n' fi else printf 'gencat works.\n\n' # It turns out that POSIX locales are really terrible, and running # gencat on one machine is not guaranteed to make those cat files # portable to another machine, so we had better warn the user here. if [ "$HOSTCC" != "$CC" ] || [ "$OLDHOSTCFLAGS" != "$OLDCFLAGS" ]; then printf 'Cross-compile detected.\n\n' printf 'WARNING: Catalog files generated with gencat may not be portable\n' printf ' across different architectures.\n\n' fi if [ -z "$NLSPATH" ]; then NLSPATH="/usr/share/locale/%L/%N" fi install_locales_prereqs=" install_locales" uninstall_locales_prereqs=" uninstall_locales" fi fi set -e else install_locales_prereqs="" uninstall_locales_prereqs="" all_locales=0 fi if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)" else install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)" fi # Like the above tested locale support, this tests history. if [ "$hist" -eq 1 ]; then + if [ "$hist_impl" = "editline" ]; then + editline=1 + readline=0 + elif [ "$hist_impl" = "readline" ]; then + editline=0 + readline=1 + else + editline=0 + readline=0 + fi + set +e printf 'Testing history...\n' flags="-DBC_ENABLE_HISTORY=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc" flags="$flags -DBC_ENABLE_NLS=$nls -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0" + flags="$flags -DBC_ENABLE_EDITLINE=$editline -DBC_ENABLE_READLINE=$readline" flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/" flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700" "$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/history.c" -o "./history.o" > /dev/null 2>&1 err="$?" rm -rf "./history.o" # If this errors, it is probably because of building on Windows, # and history is not supported on Windows, so disable it. if [ "$err" -ne 0 ]; then printf 'History does not work.\n' if [ $force -eq 0 ]; then printf 'Disabling history...\n\n' hist=0 else printf 'Forcing history...\n\n' fi else printf 'History works.\n\n' fi set -e +else + + editline=0 + readline=0 + fi -# We have to disable the history tests if it is disabled or valgrind is on. +# We have to disable the history tests if it is disabled or valgrind is on. Or +# if we are using editline or readline. if [ "$hist" -eq 0 ] || [ "$vg" -ne 0 ]; then test_bc_history_prereqs=" test_bc_history_skip" test_dc_history_prereqs=" test_dc_history_skip" history_tests="@printf 'Skipping history tests...\\\\n'" + CFLAGS="$CFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=0" +else + + if [ "$editline" -eq 0 ] && [ "$readline" -eq 0 ]; then + history_tests="@printf '\$(TEST_STARS)\\\\n\\\\nRunning history tests...\\\\n\\\\n'" + history_tests="$history_tests \&\& \$(TESTSDIR)/history.sh bc -a \&\&" + history_tests="$history_tests \$(TESTSDIR)/history.sh dc -a \&\& printf" + history_tests="$history_tests '\\\\nAll history tests passed.\\\\n\\\\n\$(TEST_STARS)\\\\n'" + else + test_bc_history_prereqs=" test_bc_history_skip" + test_dc_history_prereqs=" test_dc_history_skip" + history_tests="@printf 'Skipping history tests...\\\\n'" + fi + + # We are also setting the CFLAGS and LDFLAGS here. + if [ "$editline" -ne 0 ]; then + LDFLAGS="$LDFLAGS -ledit" + CFLAGS="$CFLAGS -DBC_ENABLE_EDITLINE=1 -DBC_ENABLE_READLINE=0" + elif [ "$readline" -ne 0 ]; then + LDFLAGS="$LDFLAGS -lreadline" + CFLAGS="$CFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=1" + else + CFLAGS="$CFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=0" + fi + +fi + +# Test FreeBSD. This is not in an if statement because regardless of whatever +# the user says, we need to know if we are on FreeBSD. If we are, we cannot set +# _POSIX_C_SOURCE and _XOPEN_SOURCE. The FreeBSD headers turn *off* stuff when +# that is done. +set +e +printf 'Testing for FreeBSD...\n' + +flags="-DBC_TEST_FREEBSD -DBC_ENABLE_AFL=0" +"$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1 + +err="$?" + +if [ "$err" -ne 0 ]; then + printf 'On FreeBSD. Not using _POSIX_C_SOURCE and _XOPEN_SOURCE.\n\n' else - history_tests="@printf '\$(TEST_STARS)\\\\n\\\\nRunning history tests...\\\\n\\\\n' \&\& \$(TESTSDIR)/history.sh bc -a \&\& \$(TESTSDIR)/history.sh dc -a \&\& printf '\\\\nAll history tests passed.\\\\n\\\\n\$(TEST_STARS)\\\\n'" + printf 'Not on FreeBSD. Using _POSIX_C_SOURCE and _XOPEN_SOURCE.\n\n' + CPPFLAGS="$CPPFLAGS -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700" fi # Test OpenBSD. This is not in an if statement because regardless of whatever # the user says, we need to know if we are on OpenBSD to activate _BSD_SOURCE. # No, I cannot `#define _BSD_SOURCE` in a header because OpenBSD's patched GCC # and Clang complain that that is only allowed for system headers. Sigh....So we # have to check at configure time and set it on the compiler command-line. And # we have to set it because we also set _POSIX_C_SOURCE, which OpenBSD headers # detect, and when they detect it, they turn off _BSD_SOURCE unless it is # specifically requested. set +e printf 'Testing for OpenBSD...\n' flags="-DBC_TEST_OPENBSD -DBC_ENABLE_AFL=0" -"$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/include/status.h" > /dev/null 2>&1 +"$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1 err="$?" if [ "$err" -ne 0 ]; then + printf 'On OpenBSD. Using _BSD_SOURCE.\n\n' bsd="-D_BSD_SOURCE" + + # Readline errors on OpenBSD, for some weird reason. + if [ "$readline" -ne 0 ]; then + usage "Cannot use readline on OpenBSD" + fi + else printf 'Not on OpenBSD.\n\n' bsd="" fi if [ "$library" -eq 1 ]; then bc_lib="" fi if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ] && [ "$library" -eq 0 ]; then BC_LIB2_O="\$(GEN_DIR)/lib2.o" else BC_LIB2_O="" fi +GEN_DIR="$scriptdir/gen" + # These lines set the appropriate targets based on whether `gen/strgen.c` or # `gen/strgen.sh` is used. GEN="strgen" -GEN_EXEC_TARGET="\$(HOSTCC) \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)" +GEN_EXEC_TARGET="\$(HOSTCC) -DBC_ENABLE_AFL=0 -I$scriptdir/include/ \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)" CLEAN_PREREQS=" clean_gen clean_coverage" if [ -z "${GEN_HOST+set}" ]; then GEN_HOST=1 else if [ "$GEN_HOST" -eq 0 ]; then GEN="strgen.sh" GEN_EXEC_TARGET="@printf 'Do not need to build gen/strgen.c\\\\n'" CLEAN_PREREQS=" clean_coverage" fi fi manpage_args="" unneeded="" headers="\$(HEADERS)" # This series of if statements figure out what source files are *not* needed. if [ "$extra_math" -eq 0 ]; then + exclude_extra_math=1 manpage_args="E" unneeded="$unneeded rand.c" else + exclude_extra_math=0 headers="$headers \$(EXTRA_MATH_HEADERS)" fi # All of these next if statements set the build type and mark certain source # files as unneeded so that they won't have targets generated for them. if [ "$hist" -eq 0 ]; then manpage_args="${manpage_args}H" unneeded="$unneeded history.c" else headers="$headers \$(HISTORY_HEADERS)" fi if [ "$nls" -eq 0 ]; then manpage_args="${manpage_args}N" fi if [ "$bc" -eq 0 ]; then unneeded="$unneeded bc.c bc_lex.c bc_parse.c" else headers="$headers \$(BC_HEADERS)" fi if [ "$dc" -eq 0 ]; then unneeded="$unneeded dc.c dc_lex.c dc_parse.c" else headers="$headers \$(DC_HEADERS)" fi # This convoluted mess does pull the version out. If you change the format of # include/version.h, you may have to change this line. version=$(cat "$scriptdir/include/version.h" | grep "VERSION " - | awk '{ print $3 }' -) if [ "$library" -ne 0 ]; then unneeded="$unneeded args.c opt.c read.c file.c main.c" unneeded="$unneeded lang.c lex.c parse.c program.c" unneeded="$unneeded bc.c bc_lex.c bc_parse.c" unneeded="$unneeded dc.c dc_lex.c dc_parse.c" headers="$headers \$(LIBRARY_HEADERS)" if [ "$PC_PATH" != "" ]; then contents=$(cat "$scriptdir/bcl.pc.in") contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR") contents=$(replace "$contents" "LIBDIR" "$LIBDIR") contents=$(replace "$contents" "VERSION" "$version") printf '%s\n' "$contents" > "./bcl.pc" pkg_config_install="\$(SAFE_INSTALL) \$(PC_INSTALL_ARGS) \"\$(BCL_PC)\" \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\"" pkg_config_uninstall="\$(RM) -f \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\"" else pkg_config_install="" pkg_config_uninstall="" fi else unneeded="$unneeded library.c" PC_PATH="" pkg_config_install="" pkg_config_uninstall="" fi # library.c is not needed under normal circumstances. if [ "$unneeded" = "" ]; then unneeded="library.c" fi # This sets the appropriate manpage for a full build. if [ "$manpage_args" = "" ]; then manpage_args="A" fi if [ "$vg" -ne 0 ]; then memcheck=1 fi if [ "$bc_default_prompt" = "" ]; then bc_default_prompt="$bc_default_tty_mode" fi if [ "$dc_default_prompt" = "" ]; then dc_default_prompt="$dc_default_tty_mode" fi # Generate the test targets and prerequisites. bc_tests=$(gen_std_test_targets bc) bc_script_tests=$(gen_script_test_targets bc) bc_err_tests=$(gen_err_test_targets bc) dc_tests=$(gen_std_test_targets dc) dc_script_tests=$(gen_script_test_targets dc) dc_err_tests=$(gen_err_test_targets dc) # Print out the values; this is for debugging. printf 'Version: %s\n' "$version" if [ "$bc" -ne 0 ]; then printf 'Building bc\n' else printf 'Not building bc\n' fi if [ "$dc" -ne 0 ]; then printf 'Building dc\n' else printf 'Not building dc\n' fi printf '\n' printf 'BC_ENABLE_LIBRARY=%s\n\n' "$library" printf 'BC_ENABLE_HISTORY=%s\n' "$hist" printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math" -printf 'BC_ENABLE_NLS=%s\n' "$nls" +printf 'BC_ENABLE_NLS=%s\n\n' "$nls" printf 'BC_ENABLE_AFL=%s\n' "$fuzz" printf '\n' printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len" printf '\n' printf 'CC=%s\n' "$CC" printf 'CFLAGS=%s\n' "$CFLAGS" printf 'HOSTCC=%s\n' "$HOSTCC" printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS" printf 'CPPFLAGS=%s\n' "$CPPFLAGS" printf 'LDFLAGS=%s\n' "$LDFLAGS" printf 'PREFIX=%s\n' "$PREFIX" printf 'BINDIR=%s\n' "$BINDIR" printf 'INCLUDEDIR=%s\n' "$INCLUDEDIR" printf 'LIBDIR=%s\n' "$LIBDIR" printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR" printf 'DATADIR=%s\n' "$DATADIR" printf 'MANDIR=%s\n' "$MANDIR" printf 'MAN1DIR=%s\n' "$MAN1DIR" printf 'MAN3DIR=%s\n' "$MAN3DIR" printf 'NLSPATH=%s\n' "$NLSPATH" printf 'PC_PATH=%s\n' "$PC_PATH" printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX" printf 'EXECPREFIX=%s\n' "$EXECPREFIX" printf 'DESTDIR=%s\n' "$DESTDIR" printf 'LONG_BIT=%s\n' "$LONG_BIT" printf 'GEN_HOST=%s\n' "$GEN_HOST" printf 'GEN_EMU=%s\n' "$GEN_EMU" printf '\n' printf 'Setting Defaults\n' printf '================\n' printf 'bc.banner=%s\n' "$bc_default_banner" printf 'bc.sigint_reset=%s\n' "$bc_default_sigint_reset" printf 'dc.sigint_reset=%s\n' "$dc_default_sigint_reset" printf 'bc.tty_mode=%s\n' "$bc_default_tty_mode" printf 'dc.tty_mode=%s\n' "$dc_default_tty_mode" printf 'bc.prompt=%s\n' "$bc_default_prompt" printf 'dc.prompt=%s\n' "$dc_default_prompt" printf 'bc.expr_exit=%s\n' "$bc_default_expr_exit" printf 'dc.expr_exit=%s\n' "$dc_default_expr_exit" +printf 'bc.digit_clamp=%s\n' "$bc_default_digit_clamp" +printf 'dc.digit_clamp=%s\n' "$dc_default_digit_clamp" + +# This code outputs a warning. The warning is to not surprise users when locales +# are installed outside of the prefix. This warning is suppressed when the +# default prefix is used, as well, so as not to panic users just installing by +# hand. I believe this will be okay because NLSPATH is usually in /usr and the +# default prefix is /usr/local, so they'll be close that way. +if [ "$nls" -ne 0 ] && [ "${NLSPATH#$PREFIX}" = "${NLSPATH}" ] && [ "$defprefix" -eq 0 ]; then + printf '\n********************************************************************************\n\n' + printf 'WARNING: Locales will *NOT* be installed in $PREFIX (%s).\n' "$PREFIX" + printf '\n' + printf ' This is because they *MUST* be installed at a fixed location to even\n' + printf ' work, and that fixed location is $NLSPATH (%s).\n' "$NLSPATH" + printf '\n' + printf ' This location is *outside* of $PREFIX. If you do not wish to install\n' + printf ' locales outside of $PREFIX, you must disable NLS with the -N or the\n' + printf ' --disable-nls options.\n' + printf '\n' + printf ' The author apologizes for the inconvenience, but the need to install\n' + printf ' the locales at a fixed location is mandated by POSIX, and it is not\n' + printf ' possible for the author to change that requirement.\n' + printf '\n********************************************************************************\n' +fi # This is where the real work begins. This is the point at which the Makefile.in # template is edited and output to the Makefile. contents=$(cat "$scriptdir/Makefile.in") needle="WARNING" replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***' contents=$(replace "$contents" "$needle" "$replacement") # The contents are edited to have the list of files to build. contents=$(gen_file_list "$contents" $unneeded) SRC_TARGETS="" # This line and loop generates the individual targets for source files. I used # to just use an implicit target, but that was found to be inadequate when I # added the library. src_files=$(find_src_files $unneeded) for f in $src_files; do o=$(replace_ext "$f" "c" "o") o=$(basename "$o") SRC_TARGETS=$(printf '%s\n\nsrc/%s: src %s %s\n\t$(CC) $(CFLAGS) -o src/%s -c %s\n' \ "$SRC_TARGETS" "$o" "$headers" "$f" "$o" "$f") done # Replace all the placeholders. contents=$(replace "$contents" "ROOTDIR" "$scriptdir") contents=$(replace "$contents" "BUILDDIR" "$builddir") contents=$(replace "$contents" "HEADERS" "$headers") contents=$(replace "$contents" "BC_ENABLED" "$bc") contents=$(replace "$contents" "DC_ENABLED" "$dc") contents=$(replace "$contents" "BC_ALL_TESTS" "$bc_test") contents=$(replace "$contents" "BC_ALL_TESTS_NP" "$bc_test_np") contents=$(replace "$contents" "BC_TESTS" "$bc_tests") contents=$(replace "$contents" "BC_SCRIPT_TESTS" "$bc_script_tests") contents=$(replace "$contents" "BC_ERROR_TESTS" "$bc_err_tests") contents=$(replace "$contents" "BC_TEST_EXEC" "$bc_test_exec") contents=$(replace "$contents" "TIMECONST_ALL_TESTS" "$timeconst") contents=$(replace "$contents" "DC_ALL_TESTS" "$dc_test") contents=$(replace "$contents" "DC_ALL_TESTS_NP" "$dc_test_np") contents=$(replace "$contents" "DC_TESTS" "$dc_tests") contents=$(replace "$contents" "DC_SCRIPT_TESTS" "$dc_script_tests") contents=$(replace "$contents" "DC_ERROR_TESTS" "$dc_err_tests") contents=$(replace "$contents" "DC_TEST_EXEC" "$dc_test_exec") +contents=$(replace "$contents" "BCL_TEST_EXEC" "$bcl_test_exec") + contents=$(replace "$contents" "BUILD_TYPE" "$manpage_args") +contents=$(replace "$contents" "EXCLUDE_EXTRA_MATH" "$exclude_extra_math") contents=$(replace "$contents" "LIBRARY" "$library") contents=$(replace "$contents" "HISTORY" "$hist") contents=$(replace "$contents" "EXTRA_MATH" "$extra_math") contents=$(replace "$contents" "NLS" "$nls") contents=$(replace "$contents" "FUZZ" "$fuzz") contents=$(replace "$contents" "MEMCHECK" "$memcheck") contents=$(replace "$contents" "BC_LIB_O" "$bc_lib") contents=$(replace "$contents" "BC_HELP_O" "$bc_help") contents=$(replace "$contents" "DC_HELP_O" "$dc_help") contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O") contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len") contents=$(replace "$contents" "NLSPATH" "$NLSPATH") contents=$(replace "$contents" "DESTDIR" "$destdir") contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX") contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX") contents=$(replace "$contents" "BINDIR" "$BINDIR") contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR") contents=$(replace "$contents" "LIBDIR" "$LIBDIR") contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR") contents=$(replace "$contents" "MAN3DIR" "$MAN3DIR") contents=$(replace "$contents" "CFLAGS" "$CFLAGS") contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS") contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS") contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS") contents=$(replace "$contents" "CC" "$CC") contents=$(replace "$contents" "HOSTCC" "$HOSTCC") contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT") contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS") contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs") contents=$(replace "$contents" "INSTALL_MAN_PREREQS" "$install_man_prereqs") contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales") contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs") contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs") contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs") contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs") contents=$(replace "$contents" "PC_PATH" "$PC_PATH") contents=$(replace "$contents" "PKG_CONFIG_INSTALL" "$pkg_config_install") contents=$(replace "$contents" "PKG_CONFIG_UNINSTALL" "$pkg_config_uninstall") contents=$(replace "$contents" "DEFAULT_TARGET" "$default_target") contents=$(replace "$contents" "DEFAULT_TARGET_PREREQS" "$default_target_prereqs") contents=$(replace "$contents" "DEFAULT_TARGET_CMD" "$default_target_cmd") contents=$(replace "$contents" "SECOND_TARGET" "$second_target") contents=$(replace "$contents" "SECOND_TARGET_PREREQS" "$second_target_prereqs") contents=$(replace "$contents" "SECOND_TARGET_CMD" "$second_target_cmd") contents=$(replace "$contents" "ALL_PREREQ" "$ALL_PREREQ") contents=$(replace "$contents" "BC_EXEC_PREREQ" "$bc_exec_prereq") contents=$(replace "$contents" "BC_EXEC_CMD" "$bc_exec_cmd") contents=$(replace "$contents" "DC_EXEC_PREREQ" "$dc_exec_prereq") contents=$(replace "$contents" "DC_EXEC_CMD" "$dc_exec_cmd") contents=$(replace "$contents" "EXECUTABLES" "$executables") contents=$(replace "$contents" "MAIN_EXEC" "$main_exec") contents=$(replace "$contents" "EXEC" "$executable") contents=$(replace "$contents" "TESTS" "$tests") contents=$(replace "$contents" "BC_HISTORY_TEST_PREREQS" "$test_bc_history_prereqs") contents=$(replace "$contents" "DC_HISTORY_TEST_PREREQS" "$test_dc_history_prereqs") contents=$(replace "$contents" "HISTORY_TESTS" "$history_tests") contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test") contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test") contents=$(replace "$contents" "TIMECONST" "$timeconst") contents=$(replace "$contents" "KARATSUBA" "$karatsuba") contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test") -contents=$(replace "$contents" "LONG_BIT" "$LONG_BIT") contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE") +contents=$(replace "$contents" "GEN_DIR" "$GEN_DIR") contents=$(replace "$contents" "GEN" "$GEN") contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET") contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS") contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU") contents=$(replace "$contents" "BSD" "$bsd") contents=$(replace "$contents" "BC_DEFAULT_BANNER" "$bc_default_banner") contents=$(replace "$contents" "BC_DEFAULT_SIGINT_RESET" "$bc_default_sigint_reset") contents=$(replace "$contents" "DC_DEFAULT_SIGINT_RESET" "$dc_default_sigint_reset") contents=$(replace "$contents" "BC_DEFAULT_TTY_MODE" "$bc_default_tty_mode") contents=$(replace "$contents" "DC_DEFAULT_TTY_MODE" "$dc_default_tty_mode") contents=$(replace "$contents" "BC_DEFAULT_PROMPT" "$bc_default_prompt") contents=$(replace "$contents" "DC_DEFAULT_PROMPT" "$dc_default_prompt") contents=$(replace "$contents" "BC_DEFAULT_EXPR_EXIT" "$bc_default_expr_exit") contents=$(replace "$contents" "DC_DEFAULT_EXPR_EXIT" "$dc_default_expr_exit") +contents=$(replace "$contents" "BC_DEFAULT_DIGIT_CLAMP" "$bc_default_digit_clamp") +contents=$(replace "$contents" "DC_DEFAULT_DIGIT_CLAMP" "$dc_default_digit_clamp") # Do the first print to the Makefile. printf '%s\n%s\n\n' "$contents" "$SRC_TARGETS" > "Makefile" # Generate the individual test targets. if [ "$bc" -ne 0 ]; then gen_std_tests bc "$extra_math" "$time_tests" $bc_test_exec gen_script_tests bc "$extra_math" "$generate_tests" "$time_tests" $bc_test_exec gen_err_tests bc $bc_test_exec fi if [ "$dc" -ne 0 ]; then gen_std_tests dc "$extra_math" "$time_tests" $dc_test_exec gen_script_tests dc "$extra_math" "$generate_tests" "$time_tests" $dc_test_exec gen_err_tests dc $dc_test_exec fi # Copy the correct manuals to the expected places. mkdir -p manuals cp -f "$scriptdir/manuals/bc/$manpage_args.1.md" manuals/bc.1.md cp -f "$scriptdir/manuals/bc/$manpage_args.1" manuals/bc.1 cp -f "$scriptdir/manuals/dc/$manpage_args.1.md" manuals/dc.1.md cp -f "$scriptdir/manuals/dc/$manpage_args.1" manuals/dc.1 make clean > /dev/null diff --git a/contrib/bc/gen/bc_help.txt b/contrib/bc/gen/bc_help.txt index 36329b1d7aaf..f78ba9e71dd3 100644 --- a/contrib/bc/gen/bc_help.txt +++ b/contrib/bc/gen/bc_help.txt @@ -1,192 +1,256 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * The bc help text. * */ usage: %s [options] [file...] bc is a command-line, arbitrary-precision calculator with a Turing-complete language. For details, use `man %s` or see the online documentation at https://git.yzena.com/gavin/bc/src/tag/%s/manuals/bc/%s.1.md. This bc is compatible with both the GNU bc and the POSIX bc spec. See the GNU bc manual (https://www.gnu.org/software/bc/manual/bc.html) and bc spec (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) for details. This bc has three differences to the GNU bc: 1) Arrays can be passed to the builtin "length" function to get the number of elements currently in the array. The following example prints "1": a[0] = 0 length(a[]) 2) The precedence of the boolean "not" operator (!) is equal to that of the unary minus (-), or negation, operator. This still allows POSIX-compliant scripts to work while somewhat preserving expected behavior (versus C) and making parsing easier. 3) This bc has many more extensions than the GNU bc does. For details, see the man page or online documentation. This bc also implements the dot (.) extension of the BSD bc. Options: + -C --no-digit-clamp + + Disables clamping of digits that are larger than or equal to the current + ibase when parsing numbers. + + This means that the value added to a number from a digit is always that + digit's value multiplied by the value of ibase raised to the power of the + digit's position, which starts from 0 at the least significant digit. + + If multiple of this option and the -c option are given, the last is used. + + -c --digit-clamp + + Enables clamping of digits that are larger than or equal to the current + ibase when parsing numbers. + + This means that digits that the value added to a number from a digit that + is greater than or equal to the ibase is the value of ibase minus 1 all + multiplied by the value of ibase raised to the power of the digit's + position, which starts from 0 at the least significant digit. + + If multiple of this option and the -C option are given, the last is used. +{{ A H N HN }} + + -E seed --seed=seed + + Sets the builtin variable seed to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. +{{ end }} + -e expr --expression=expr Run "expr" and quit. If multiple expressions or files (see below) are given, they are all run before executing from stdin. -f file --file=file Run the bc code in "file" and exit. See above as well. -g --global-stacks Turn scale, ibase, and obase into stacks. This makes the value of each be be restored on returning from functions. See the man page or online documentation for more details. -h --help Print this usage message and exit. + -I ibase --ibase=ibase + + Sets the builtin variable ibase to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. + -i --interactive Force interactive mode. -L --no-line-length Disable line length checking. -l --mathlib Use predefined math routines: s(expr) = sine of expr in radians c(expr) = cosine of expr in radians a(expr) = arctangent of expr, returning radians l(expr) = natural log of expr e(expr) = raises e to the power of expr j(n, x) = Bessel function of integer order n of x This bc may load more functions with these options. See the manpage or online documentation for details. + -O obase --obase=obase + + Sets the builtin variable obase to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. + -P --no-prompt Disable the prompts in interactive mode. -R --no-read-prompt Disable the read prompt in interactive mode. -r keyword --redefine=keyword Redefines "keyword" and allows it to be used as a function, variable, and array name. This is useful when this bc gives parse errors on scripts meant for other bc implementations. Only keywords that are not in the POSIX bc spec may be redefined. It is a fatal error to attempt to redefine a keyword that cannot be redefined or does not exist. -q --quiet Don't print version and copyright. + -S scale --scale=scale + + Sets the builtin variable scale to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. + -s --standard Error if any non-POSIX extensions are used. -w --warn Warn if any non-POSIX extensions are used. -v --version Print version information and copyright and exit. -z --leading-zeroes Enable leading zeroes on numbers greater than -1 and less than 1. Environment variables: POSIXLY_CORRECT Error if any non-POSIX extensions are used. BC_ENV_ARGS Command-line arguments to use on every run. BC_LINE_LENGTH If an integer, the number of characters to print on a line before wrapping. Using 0 will disable line length checking. BC_BANNER If an integer and non-zero, display the copyright banner in interactive mode. + If zero, disable the banner. + Overrides the default, which is %s print the banner. BC_SIGINT_RESET If an integer and non-zero, reset on SIGINT, rather than exit, when in interactive mode. + If zero, do not reset on SIGINT in all cases, but exit instead. + Overrides the default, which is %s. BC_TTY_MODE If an integer and non-zero, enable TTY mode when it is available. + If zero, disable TTY mode in all cases. + Overrides the default, which is TTY mode %s. BC_PROMPT If an integer and non-zero, enable prompt when TTY mode is possible. + If zero, disable prompt in all cases. + Overrides the default, which is prompt %s. BC_EXPR_EXIT If an integer and non-zero, exit when expressions or expression files are given on the command-line, and does not exit when an integer and zero. Overrides the default, which is %s. + + BC_DIGIT_CLAMP + + If an integer and non-zero, clamp digits larger than or equal to the + current ibase when parsing numbers. + + Overrides the default, which is %s. diff --git a/contrib/bc/gen/dc_help.txt b/contrib/bc/gen/dc_help.txt index a0f275b60b64..8b313f8d9e07 100644 --- a/contrib/bc/gen/dc_help.txt +++ b/contrib/bc/gen/dc_help.txt @@ -1,151 +1,213 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * The dc help text. * */ usage: %s [options] [file...] dc is a reverse-polish notation command-line calculator which supports unlimited precision arithmetic. For details, use `man %s` or see the online documentation at https://git.yzena.com/gavin/bc/src/tag/%s/manuals/bc/%s.1.md. This dc is (mostly) compatible with the OpenBSD dc and the GNU dc. See the OpenBSD man page (http://man.openbsd.org/OpenBSD-current/man1/dc.1) and the GNU dc manual (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html) for details. This dc has a few differences from the two above: 1) When printing a byte stream (command "P"), this bc follows what the FreeBSD dc does. 2) This dc implements the GNU extensions for divmod ("~") and modular exponentiation ("|"). 3) This dc implements all FreeBSD extensions, except for "J" and "M". 4) This dc does not implement the run command ("!"), for security reasons. 5) Like the FreeBSD dc, this dc supports extended registers. However, they are implemented differently. When it encounters whitespace where a register should be, it skips the whitespace. If the character following is not a lowercase letter, an error is issued. Otherwise, the register name is parsed by the following regex: [a-z][a-z0-9_]* This generally means that register names will be surrounded by whitespace. Examples: l idx s temp L index S temp2 < do_thing Also note that, unlike the FreeBSD dc, extended registers are not even parsed unless the "-x" option is given. Instead, the space after a command that requires a register name is taken as the register name. Options: + -C --no-digit-clamp + + Disables clamping of digits that are larger than or equal to the current + ibase when parsing numbers. + + This means that the value added to a number from a digit is always that + digit's value multiplied by the value of ibase raised to the power of the + digit's position, which starts from 0 at the least significant digit. + + If multiple of this option and the -c option are given, the last is used. + + -c --digit-clamp + + Enables clamping of digits that are larger than or equal to the current + ibase when parsing numbers. + + This means that digits that the value added to a number from a digit that + is greater than or equal to the ibase is the value of ibase minus 1 all + multiplied by the value of ibase raised to the power of the digit's + position, which starts from 0 at the least significant digit. + + If multiple of this option and the -C option are given, the last is used. +{{ A H N HN }} + + -E seed --seed=seed + + Sets the builtin variable seed to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. +{{ end }} + -e expr --expression=expr Run "expr" and quit. If multiple expressions or files (see below) are given, they are all run. After running, dc will exit. -f file --file=file Run the dc code in "file" and exit. See above. -h --help Print this usage message and exit. + -I ibase --ibase=ibase + + Sets the builtin variable ibase to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. + -i --interactive Put dc into interactive mode. See the man page for more details. -L --no-line-length Disable line length checking. + -O obase --obase=obase + + Sets the builtin variable obase to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. + -P --no-prompt Disable the prompts in interactive mode. -R --no-read-prompt Disable the read prompt in interactive mode. + -S scale --scale=scale + + Sets the builtin variable scale to the given value assuming that the given + value is in base 10. It is a fatal error if the given value is not a valid + number. + -V --version Print version and copyright and exit. -x --extended-register Enable extended register mode. -z --leading-zeroes Enable leading zeroes on numbers greater than -1 and less than 1. Environment variables: DC_ENV_ARGS Command-line arguments to use on every run. DC_LINE_LENGTH If an integer, the number of characters to print on a line before wrapping. Using 0 will disable line length checking. DC_SIGINT_RESET If an integer and non-zero, reset on SIGINT, rather than exit, when in interactive mode. + If zero, do not reset on SIGINT in all cases, but exit instead. + Overrides the default, which is %s. DC_TTY_MODE If an integer and non-zero, enable TTY mode when it is available. + If zero, disable TTY mode in all cases. + Overrides the default, which is TTY mode %s. DC_PROMPT If an integer and non-zero, enable prompt when TTY mode is possible. + If zero, disable prompt in all cases. + Overrides the default, which is prompt %s. DC_EXPR_EXIT If an integer and non-zero, exit when expressions or expression files are given on the command-line, and does not exit when an integer and zero. Overrides the default, which is %s. + + DC_DIGIT_CLAMP + + If an integer and non-zero, clamp digits larger than or equal to the + current ibase when parsing numbers. + + Overrides the default, which is %s. diff --git a/contrib/bc/gen/lib.bc b/contrib/bc/gen/lib.bc index c0cd7f7dc8d4..b12b23176790 100644 --- a/contrib/bc/gen/lib.bc +++ b/contrib/bc/gen/lib.bc @@ -1,201 +1,200 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * The bc math library. * */ -scale=2*A define e(x){ auto b,s,n,r,d,i,p,f,v b=ibase ibase=A if(x<0){ n=1 x=-x } s=scale r=6+s+.44*x scale=scale(x)+1 while(x>1){ d+=1 x/=2 scale+=1 } scale=r r=x+1 p=x f=v=1 for(i=2;v;++i){ p*=x f*=i v=p/f r+=v } while(d--)r*=r scale=s ibase=b if(n)return(1/r) return(r/1) } define l(x){ auto b,s,r,p,a,q,i,v if(x<=0)return((1-A^scale)/1) b=ibase ibase=A s=scale scale+=6 p=2 while(x>=2){ p*=2 x=sqrt(x) } while(x<=.5){ p*=2 x=sqrt(x) } r=a=(x-1)/(x+1) q=a*a v=1 for(i=3;v;i+=2){ a*=q v=a/i r+=v } r*=p scale=s ibase=b return(r/1) } define s(x){ auto b,s,r,a,q,i if(x<0)return(-s(-x)) b=ibase ibase=A s=scale scale=1.1*s+2 a=a(1) scale=0 q=(x/a+2)/4 x-=4*q*a if(q%2)x=-x scale=s+2 r=a=x q=-x*x for(i=3;a;i+=2){ a*=q/(i*(i-1)) r+=a } scale=s ibase=b return(r/1) } define c(x){ auto b,s b=ibase ibase=A s=scale scale*=1.2 x=s(2*a(1)+x) scale=s ibase=b return(x/1) } define a(x){ auto b,s,r,n,a,m,t,f,i,u b=ibase ibase=A n=1 if(x<0){ n=-1 x=-x } if(scale<65){ if(x==1){ r=.7853981633974483096156608458198757210492923498437764552437361480/n ibase=b return(r) } if(x==.2){ r=.1973955598498807583700497651947902934475851037878521015176889402/n ibase=b return(r) } } s=scale if(x>.2){ scale+=5 a=a(.2) } scale=s+3 while(x>.2){ m+=1 x=(x-.2)/(1+.2*x) } r=u=x f=-x*x t=1 for(i=3;t;i+=2){ u*=f t=u/i r+=t } scale=s ibase=b return((m*a+r)/n) } define j(n,x){ auto b,s,o,a,i,r,v,f b=ibase ibase=A s=scale scale=0 n/=1 if(n<0){ n=-n o=n%2 } a=1 for(i=2;i<=n;++i)a*=i scale=1.5*s a=(x^n)/2^n/a r=v=1 f=-x*x/4 scale+=length(a)-scale(a) for(i=1;v;++i){ v=v*f/i/(n+i) r+=v } scale=s ibase=b if(o)a=-a return(a*r/1) } diff --git a/contrib/bc/gen/lib2.bc b/contrib/bc/gen/lib2.bc index 23cbec104d02..826f8a430cea 100644 --- a/contrib/bc/gen/lib2.bc +++ b/contrib/bc/gen/lib2.bc @@ -1,564 +1,561 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * The second bc math library. * */ define p(x,y){ auto a a=y$ if(y==a)return (x^a)@scale return e(y*l(x)) } define r(x,p){ auto t,n if(x==0)return x p=abs(p)$ n=(x<0) x=abs(x) t=x@p if(p=5>>p+1)t+=1>>p if(n)t=-t return t } define ceil(x,p){ auto t,n if(x==0)return x p=abs(p)$ n=(x<0) x=abs(x) t=(x+((x@p>p))@p if(n)t=-t return t } define f(n){ auto r n=abs(n)$ for(r=1;n>1;--n)r*=n return r } define perm(n,k){ auto f,g,s if(k>n)return 0 n=abs(n)$ k=abs(k)$ f=f(n) g=f(n-k) s=scale scale=0 f/=g scale=s return f } define comb(n,r){ auto s,f,g,h if(r>n)return 0 n=abs(n)$ r=abs(r)$ s=scale scale=0 f=f(n) h=f(r) g=f(n-r) f/=h*g scale=s return f } define log(x,b){ auto p,s s=scale if(scalescale)scale=scale(x) scale*=2 p=l(x)/l(b) scale=s return p@s } define l2(x){return log(x,2)} define l10(x){return log(x,A)} define root(x,n){ auto s,m,r,q,p if(n<0)sqrt(n) n=n$ if(n==0)x/n if(x==0||n==1)return x if(n==2)return sqrt(x) s=scale scale=0 if(x<0&&n%2==0)sqrt(x) scale=s+2 m=(x<0) x=abs(x) p=n-1 q=A^ceil((length(x$)/n)$,0) while(r!=q){ r=q q=(p*r+x/r^p)/n } if(m)r=-r scale=s return r@s } define cbrt(x){return root(x,3)} define gcd(a,b){ auto g,s if(!b)return a s=scale scale=0 a=abs(a)$ b=abs(b)$ if(a>p } define ifrand(i,p){return irand(abs(i)$)+frand(p)} define srand(x){ if(irand(2))return -x return x } define brand(){return irand(2)} define void output(x,b){ auto c c=obase obase=b x obase=c } define void hex(x){output(x,G)} define void binary(x){output(x,2)} define ubytes(x){ auto p,i x=abs(x)$ i=2^8 for(p=1;i-1p||(!z&&x==p))n*=2 return n } define s2un(x,n){ auto t,u,s x=x$ if(x<0){ x=abs(x) s=scale scale=0 t=n*8 u=2^(t-1) if(x==u)return x else if(x>u)x%=u scale=s return 2^(t)-x } return x } define s2u(x){return s2un(x,sbytes(x))} define void plz(x){ if(leading_zero())print x else{ if(x>-1&&x<1&&x!=0){ if(x<0)print"-" print 0,abs(x) } else print x } } define void plznl(x){ plz(x) print"\n" } define void pnlz(x){ auto s,i if(leading_zero()){ if(x>-1&&x<1&&x!=0){ s=scale(x) if(x<0)print"-" print"." x=abs(x) for(i=0;i1)p=log(b,obase)+1 else p=b for(i=y-p;i>0;--i)print 0 if(b)print b scale=s - ibase=j } define void output_uint(x,n){ auto i for(i=n-1;i>=0;--i){ output_byte(x,i) if(i)print" " else print"\n" } } define void hex_uint(x,n){ auto o o=obase obase=G output_uint(x,n) obase=o } define void binary_uint(x,n){ auto o o=obase obase=2 output_uint(x,n) obase=o } define void uintn(x,n){ if(scale(x)){ print"Error: ",x," is not an integer.\n" return } if(x<0){ print"Error: ",x," is negative.\n" return } if(x>=2^(n*8)){ print"Error: ",x," cannot fit into ",n," unsigned byte(s).\n" return } binary_uint(x,n) hex_uint(x,n) } define void intn(x,n){ auto t if(scale(x)){ print"Error: ",x," is not an integer.\n" return } t=2^(n*8-1) if(abs(x)>=t&&(x>0||x!=-t)){ print "Error: ",x," cannot fit into ",n," signed byte(s).\n" return } x=s2un(x,n) binary_uint(x,n) hex_uint(x,n) } define void uint8(x){uintn(x,1)} define void int8(x){intn(x,1)} define void uint16(x){uintn(x,2)} define void int16(x){intn(x,2)} define void uint32(x){uintn(x,4)} define void int32(x){intn(x,4)} define void uint64(x){uintn(x,8)} define void int64(x){intn(x,8)} define void uint(x){uintn(x,ubytes(x))} define void int(x){intn(x,sbytes(x))} define bunrev(t){ auto a,s,m[] s=scale scale=0 t=abs(t)$ while(t!=1){ t=divmod(t,2,m[]) a*=2 a+=m[0] } scale=s return a } define band(a,b){ auto s,t,m[],n[] a=abs(a)$ b=abs(b)$ if(b>a){ t=b b=a a=t } s=scale scale=0 t=1 while(b){ a=divmod(a,2,m[]) b=divmod(b,2,n[]) t*=2 t+=(m[0]&&n[0]) } scale=s return bunrev(t) } define bor(a,b){ auto s,t,m[],n[] a=abs(a)$ b=abs(b)$ if(b>a){ t=b b=a a=t } s=scale scale=0 t=1 while(b){ a=divmod(a,2,m[]) b=divmod(b,2,n[]) t*=2 t+=(m[0]||n[0]) } while(a){ a=divmod(a,2,m[]) t*=2 t+=m[0] } scale=s return bunrev(t) } define bxor(a,b){ auto s,t,m[],n[] a=abs(a)$ b=abs(b)$ if(b>a){ t=b b=a a=t } s=scale scale=0 t=1 while(b){ a=divmod(a,2,m[]) b=divmod(b,2,n[]) t*=2 t+=(m[0]+n[0]==1) } while(a){ a=divmod(a,2,m[]) t*=2 t+=m[0] } scale=s return bunrev(t) } define bshl(a,b){return abs(a)$*2^abs(b)$} define bshr(a,b){return (abs(a)$/2^abs(b)$)$} define bnotn(x,n){ auto s,t,m[] s=scale scale=0 t=2^(abs(n)$*8) x=abs(x)$%t+t t=1 while(x!=1){ x=divmod(x,2,m[]) t*=2 t+=!m[0] } scale=s return bunrev(t) } define bnot8(x){return bnotn(x,1)} define bnot16(x){return bnotn(x,2)} define bnot32(x){return bnotn(x,4)} define bnot64(x){return bnotn(x,8)} define bnot(x){return bnotn(x,ubytes(x))} define brevn(x,n){ auto s,t,m[] s=scale scale=0 t=2^(abs(n)$*8) x=abs(x)$%t+t scale=s return bunrev(x) } define brev8(x){return brevn(x,1)} define brev16(x){return brevn(x,2)} define brev32(x){return brevn(x,4)} define brev64(x){return brevn(x,8)} define brev(x){return brevn(x,ubytes(x))} define broln(x,p,n){ auto s,t,m[] s=scale scale=0 n=abs(n)$*8 p=abs(p)$%n t=2^n x=abs(x)$%t if(!p)return x x=divmod(x,2^(n-p),m[]) x+=m[0]*2^p%t scale=s return x } define brol8(x,p){return broln(x,p,1)} define brol16(x,p){return broln(x,p,2)} define brol32(x,p){return broln(x,p,4)} define brol64(x,p){return broln(x,p,8)} define brol(x,p){return broln(x,p,ubytes(x))} define brorn(x,p,n){ auto s,t,m[] s=scale scale=0 n=abs(n)$*8 p=abs(p)$%n t=2^n x=abs(x)$%t if(!p)return x x=divmod(x,2^p,m[]) x+=m[0]*2^(n-p)%t scale=s return x } define bror8(x,p){return brorn(x,p,1)} define bror16(x,p){return brorn(x,p,2)} define bror32(x,p){return brorn(x,p,4)} define bror64(x,p){return brorn(x,p,8)} define brol(x,p){return brorn(x,p,ubytes(x))} define bmodn(x,n){ auto s s=scale scale=0 x=abs(x)$%2^(abs(n)$*8) scale=s return x } define bmod8(x){return bmodn(x,1)} define bmod16(x){return bmodn(x,2)} define bmod32(x){return bmodn(x,4)} define bmod64(x){return bmodn(x,8)} diff --git a/contrib/bc/gen/strgen.c b/contrib/bc/gen/strgen.c index 63faf1ec3472..dbea0212f617 100644 --- a/contrib/bc/gen/strgen.c +++ b/contrib/bc/gen/strgen.c @@ -1,294 +1,506 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * Generates a const array from a bc script. * */ +#include #include #include #include #include #include -// For some reason, Windows needs this header. +#include +#include + +#ifndef _WIN32 +#include +#endif // _WIN32 + +// For some reason, Windows can't have this header. #ifndef _WIN32 #include #endif // _WIN32 +// This pulls in cross-platform stuff. +#include + +// clang-format off + +// The usage help. +static const char* const bc_gen_usage = + "usage: %s input output exclude name [label [define [remove_tabs]]]\n"; + +static const char* const bc_gen_ex_start = "{{ A H N HN }}"; +static const char* const bc_gen_ex_end = "{{ end }}"; + // This is exactly what it looks like. It just slaps a simple license header on // the generated C source file. static const char* const bc_gen_header = - "// Copyright (c) 2018-2021 Gavin D. Howard and contributors.\n" + "// Copyright (c) 2018-2023 Gavin D. Howard and contributors.\n" "// Licensed under the 2-clause BSD license.\n" "// *** AUTOMATICALLY GENERATED FROM %s. DO NOT MODIFY. ***\n\n"; +// clang-format on // These are just format strings used to generate the C source. static const char* const bc_gen_label = "const char *%s = \"%s\";\n\n"; static const char* const bc_gen_label_extern = "extern const char *%s;\n\n"; static const char* const bc_gen_ifdef = "#if %s\n"; static const char* const bc_gen_endif = "#endif // %s\n"; static const char* const bc_gen_name = "const char %s[] = {\n"; static const char* const bc_gen_name_extern = "extern const char %s[];\n\n"; // Error codes. We can't use 0 because these are used as exit statuses, and 0 // as an exit status is not an error. #define IO_ERR (1) #define INVALID_INPUT_FILE (2) #define INVALID_PARAMS (3) // This is the max width to print characters to the screen. This is to ensure // that lines don't go much over 80 characters. #define MAX_WIDTH (72) /** * Open a file. This function is to smooth over differences between POSIX and * Windows. * @param f A pointer to the FILE pointer that will be initialized. * @param filename The name of the file. * @param mode The mode to open the file in. */ -static void open_file(FILE** f, const char* filename, const char* mode) { - +static void +open_file(FILE** f, const char* filename, const char* mode) +{ #ifndef _WIN32 *f = fopen(filename, mode); #else // _WIN32 // We want the file pointer to be NULL on failure, but fopen_s() is not // guaranteed to set it. *f = NULL; fopen_s(f, filename, mode); #endif // _WIN32 } +/** + * A portability file open function. This is copied from src/read.c. Make sure + * to update that if this changes. + * @param path The path to the file to open. + * @param mode The mode to open in. + */ +static int +bc_read_open(const char* path, int mode) +{ + int fd; + +#ifndef _WIN32 + fd = open(path, mode); +#else // _WIN32 + fd = -1; + open(&fd, path, mode); +#endif + + return fd; +} + +/** + * Reads a file and returns the file as a string. This has been copied from + * src/read.c. Make sure to change that if this changes. + * @param path The path to the file. + * @return The contents of the file as a string. + */ +static char* +bc_read_file(const char* path) +{ + int e = IO_ERR; + size_t size, to_read; + struct stat pstat; + int fd; + char* buf; + char* buf2; + + // This has been copied from src/read.c. Make sure to change that if this + // changes. + + assert(path != NULL); + +#ifndef NDEBUG + // Need this to quiet MSan. + // NOLINTNEXTLINE + memset(&pstat, 0, sizeof(struct stat)); +#endif // NDEBUG + + fd = bc_read_open(path, O_RDONLY); + + // If we can't read a file, we just barf. + if (BC_ERR(fd < 0)) + { + fprintf(stderr, "Could not open file: %s\n", path); + exit(INVALID_INPUT_FILE); + } + + // The reason we call fstat is to eliminate TOCTOU race conditions. This + // way, we have an open file, so it's not going anywhere. + if (BC_ERR(fstat(fd, &pstat) == -1)) + { + fprintf(stderr, "Could not stat file: %s\n", path); + exit(INVALID_INPUT_FILE); + } + + // Make sure it's not a directory. + if (BC_ERR(S_ISDIR(pstat.st_mode))) + { + fprintf(stderr, "Path is directory: %s\n", path); + exit(INVALID_INPUT_FILE); + } + + // Get the size of the file and allocate that much. + size = (size_t) pstat.st_size; + buf = (char*) malloc(size + 1); + if (buf == NULL) + { + fprintf(stderr, "Could not malloc\n"); + exit(INVALID_INPUT_FILE); + } + buf2 = buf; + to_read = size; + + do + { + // Read the file. We just bail if a signal interrupts. This is so that + // users can interrupt the reading of big files if they want. + ssize_t r = read(fd, buf2, to_read); + if (BC_ERR(r < 0)) exit(e); + to_read -= (size_t) r; + buf2 += (size_t) r; + } + while (to_read); + + // Got to have a nul byte. + buf[size] = '\0'; + + close(fd); + + return buf; +} + /** * Outputs a label, which is a string literal that the code can use as a name * for the file that is being turned into a string. This is important for the * math libraries because the parse and lex code expects a filename. The label * becomes the filename for the purposes of lexing and parsing. * * The label is generated from bc_gen_label (above). It has the form: * * const char * =