diff --git a/NEWS.md b/NEWS.md index 3b1477cafb26..f35d593b807a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,1155 +1,1185 @@ # News +## 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/include/status.h b/include/status.h index 781248ad4020..662f2b89c04d 100644 --- a/include/status.h +++ b/include/status.h @@ -1,789 +1,793 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2021 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. * * ***************************************************************************** * * All bc status codes and cross-platform portability. * */ #ifndef BC_STATUS_H #define BC_STATUS_H #include // This is used by configure.sh to test for OpenBSD. #ifdef BC_TEST_OPENBSD #ifdef __OpenBSD__ #error On OpenBSD without _BSD_SOURCE #endif // __OpenBSD__ #endif // BC_TEST_OPENBSD #ifndef BC_ENABLED #define BC_ENABLED (1) #endif // BC_ENABLED #ifndef DC_ENABLED #define DC_ENABLED (1) #endif // DC_ENABLED +#ifndef BC_ENABLE_LIBRARY +#define BC_ENABLE_LIBRARY (0) +#endif // BC_ENABLE_LIBRARY + // This is error checking for fuzz builds. #if BC_ENABLE_AFL #ifndef __AFL_HAVE_MANUAL_CONTROL #error Must compile with afl-clang-fast or afl-clang-lto for fuzzing #endif // __AFL_HAVE_MANUAL_CONTROL #endif // BC_ENABLE_AFL #ifndef BC_ENABLE_MEMCHECK #define BC_ENABLE_MEMCHECK (0) #endif // BC_ENABLE_MEMCHECK /** * Mark a variable as unused. * @param e The variable to mark as unused. */ #define BC_UNUSED(e) ((void) (e)) // If users want, they can define this to something like __builtin_expect(e, 1). // It might give a performance improvement. #ifndef BC_LIKELY /** * Mark a branch expression as likely. * @param e The expression to mark as likely. */ #define BC_LIKELY(e) (e) #endif // BC_LIKELY // If users want, they can define this to something like __builtin_expect(e, 0). // It might give a performance improvement. #ifndef BC_UNLIKELY /** * Mark a branch expression as unlikely. * @param e The expression to mark as unlikely. */ #define BC_UNLIKELY(e) (e) #endif // BC_UNLIKELY /** * Mark a branch expression as an error, if true. * @param e The expression to mark as an error, if true. */ #define BC_ERR(e) BC_UNLIKELY(e) /** * Mark a branch expression as not an error, if true. * @param e The expression to mark as not an error, if true. */ #define BC_NO_ERR(s) BC_LIKELY(s) // Disable extra debug code by default. #ifndef BC_DEBUG_CODE #define BC_DEBUG_CODE (0) #endif // BC_DEBUG_CODE // We want to be able to use _Noreturn on C11 compilers. #if __STDC_VERSION__ >= 201100L #include #define BC_NORETURN _Noreturn #define BC_C11 (1) #else // __STDC_VERSION__ #define BC_NORETURN #define BC_MUST_RETURN #define BC_C11 (0) #endif // __STDC_VERSION__ #define BC_HAS_UNREACHABLE (0) #define BC_HAS_COMPUTED_GOTO (0) // GCC and Clang complain if fallthroughs are not marked with their special // attribute. Jerks. This creates a define for marking the fallthroughs that is // nothing on other compilers. #if defined(__clang__) || defined(__GNUC__) #if defined(__has_attribute) #if __has_attribute(fallthrough) #define BC_FALLTHROUGH __attribute__((fallthrough)); #else // __has_attribute(fallthrough) #define BC_FALLTHROUGH #endif // __has_attribute(fallthrough) #ifdef __GNUC__ #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) #undef BC_HAS_UNREACHABLE #define BC_HAS_UNREACHABLE (1) #endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) #else // __GNUC__ #if __clang_major__ >= 4 #undef BC_HAS_UNREACHABLE #define BC_HAS_UNREACHABLE (1) #endif // __clang_major__ >= 4 #endif // __GNUC__ #else // defined(__has_attribute) #define BC_FALLTHROUGH #endif // defined(__has_attribute) #else // defined(__clang__) || defined(__GNUC__) #define BC_FALLTHROUGH #endif // defined(__clang__) || defined(__GNUC__) #if BC_HAS_UNREACHABLE #define BC_UNREACHABLE __builtin_unreachable(); #else // BC_HAS_UNREACHABLE #ifdef _WIN32 #define BC_UNREACHABLE __assume(0); #else // _WIN32 #define BC_UNREACHABLE #endif // _WIN32 #endif // BC_HAS_UNREACHABLE #ifdef __GNUC__ #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) #undef BC_HAS_COMPUTED_GOTO #define BC_HAS_COMPUTED_GOTO (1) #endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) #endif // __GNUC__ #ifdef __clang__ #if __clang_major__ >= 4 #undef BC_HAS_COMPUTED_GOTO #define BC_HAS_COMPUTED_GOTO (1) #endif // __clang_major__ >= 4 #endif // __GNUC__ #ifdef BC_NO_COMPUTED_GOTO #undef BC_HAS_COMPUTED_GOTO #define BC_HAS_COMPUTED_GOTO (0) #endif // BC_NO_COMPUTED_GOTO #ifdef __GNUC__ #ifdef __OpenBSD__ // The OpenBSD GCC doesn't like inline. #define inline #endif // __OpenBSD__ #endif // __GNUC__ // Workarounds for AIX's POSIX incompatibility. #ifndef SIZE_MAX #define SIZE_MAX __SIZE_MAX__ #endif // SIZE_MAX #ifndef UINTMAX_C #define UINTMAX_C __UINTMAX_C #endif // UINTMAX_C #ifndef UINT32_C #define UINT32_C __UINT32_C #endif // UINT32_C #ifndef UINT_FAST32_MAX #define UINT_FAST32_MAX __UINT_FAST32_MAX__ #endif // UINT_FAST32_MAX #ifndef UINT16_MAX #define UINT16_MAX __UINT16_MAX__ #endif // UINT16_MAX #ifndef SIG_ATOMIC_MAX #define SIG_ATOMIC_MAX __SIG_ATOMIC_MAX__ #endif // SIG_ATOMIC_MAX // Yes, this has to be here. #include // All of these set defaults for settings. #if BC_ENABLED #ifndef BC_DEFAULT_BANNER #define BC_DEFAULT_BANNER (0) #endif // BC_DEFAULT_BANNER #endif // BC_ENABLED #ifndef BC_DEFAULT_SIGINT_RESET #define BC_DEFAULT_SIGINT_RESET (1) #endif // BC_DEFAULT_SIGINT_RESET #ifndef BC_DEFAULT_TTY_MODE #define BC_DEFAULT_TTY_MODE (1) #endif // BC_DEFAULT_TTY_MODE #ifndef BC_DEFAULT_PROMPT #define BC_DEFAULT_PROMPT BC_DEFAULT_TTY_MODE #endif // BC_DEFAULT_PROMPT // All of these set defaults for settings. #ifndef DC_DEFAULT_SIGINT_RESET #define DC_DEFAULT_SIGINT_RESET (1) #endif // DC_DEFAULT_SIGINT_RESET #ifndef DC_DEFAULT_TTY_MODE #define DC_DEFAULT_TTY_MODE (0) #endif // DC_DEFAULT_TTY_MODE #ifndef DC_DEFAULT_HISTORY #define DC_DEFAULT_HISTORY DC_DEFAULT_TTY_MODE #endif // DC_DEFAULT_HISTORY #ifndef DC_DEFAULT_PROMPT #define DC_DEFAULT_PROMPT DC_DEFAULT_TTY_MODE #endif // DC_DEFAULT_PROMPT /// Statuses, which mark either which category of error happened, or some other /// status that matters. typedef enum BcStatus { /// Normal status. BC_STATUS_SUCCESS = 0, /// Math error. BC_STATUS_ERROR_MATH, /// Parse (and lex) error. BC_STATUS_ERROR_PARSE, /// Runtime error. BC_STATUS_ERROR_EXEC, /// Fatal error. BC_STATUS_ERROR_FATAL, /// EOF status. BC_STATUS_EOF, /// Quit status. This means that bc/dc is in the process of quitting. BC_STATUS_QUIT, } BcStatus; /// Errors, which are more specific errors. typedef enum BcErr { // Math errors. /// Negative number used when not allowed. BC_ERR_MATH_NEGATIVE, /// Non-integer used when not allowed. BC_ERR_MATH_NON_INTEGER, /// Conversion to a hardware integer would overflow. BC_ERR_MATH_OVERFLOW, /// Divide by zero. BC_ERR_MATH_DIVIDE_BY_ZERO, // Fatal errors. /// An allocation or reallocation failed. BC_ERR_FATAL_ALLOC_ERR, /// I/O failure. BC_ERR_FATAL_IO_ERR, /// File error, such as permissions or file does not exist. BC_ERR_FATAL_FILE_ERR, /// File is binary, not text, error. BC_ERR_FATAL_BIN_FILE, /// Attempted to read a directory as a file error. BC_ERR_FATAL_PATH_DIR, /// Invalid option error. BC_ERR_FATAL_OPTION, /// Option with required argument not given an argument. BC_ERR_FATAL_OPTION_NO_ARG, /// Option with no argument given an argument. BC_ERR_FATAL_OPTION_ARG, /// Option argument is invalid. BC_ERR_FATAL_ARG, // Runtime errors. /// Invalid ibase value. BC_ERR_EXEC_IBASE, /// Invalid obase value. BC_ERR_EXEC_OBASE, /// Invalid scale value. BC_ERR_EXEC_SCALE, /// Invalid expression parsed by read(). BC_ERR_EXEC_READ_EXPR, /// read() used within an expression given to a read() call. BC_ERR_EXEC_REC_READ, /// Type error. BC_ERR_EXEC_TYPE, /// Stack has too few elements error. BC_ERR_EXEC_STACK, /// Register stack has too few elements error. BC_ERR_EXEC_STACK_REGISTER, /// Wrong number of arguments error. BC_ERR_EXEC_PARAMS, /// Undefined function error. BC_ERR_EXEC_UNDEF_FUNC, /// Void value used in an expression error. BC_ERR_EXEC_VOID_VAL, // Parse (and lex errors). /// EOF encountered when not expected error. BC_ERR_PARSE_EOF, /// Invalid character error. BC_ERR_PARSE_CHAR, /// Invalid string (no ending quote) error. BC_ERR_PARSE_STRING, /// Invalid comment (no end found) error. BC_ERR_PARSE_COMMENT, /// Invalid token encountered error. BC_ERR_PARSE_TOKEN, #if BC_ENABLED /// Invalid expression error. BC_ERR_PARSE_EXPR, /// Expression is empty error. BC_ERR_PARSE_EMPTY_EXPR, /// Print statement is invalid error. BC_ERR_PARSE_PRINT, /// Function definition is invalid error. BC_ERR_PARSE_FUNC, /// Assignment is invalid error. BC_ERR_PARSE_ASSIGN, /// No auto identifiers given for an auto statement error. BC_ERR_PARSE_NO_AUTO, /// Duplicate local (parameter or auto) error. BC_ERR_PARSE_DUP_LOCAL, /// Invalid block (within braces) error. BC_ERR_PARSE_BLOCK, /// Invalid return statement for void functions. BC_ERR_PARSE_RET_VOID, /// Reference attached to a variable, not an array, error. BC_ERR_PARSE_REF_VAR, // POSIX-only errors. /// Name length greater than 1 error. BC_ERR_POSIX_NAME_LEN, /// Non-POSIX comment used error. BC_ERR_POSIX_COMMENT, /// Non-POSIX keyword error. BC_ERR_POSIX_KW, /// Non-POSIX . (last) error. BC_ERR_POSIX_DOT, /// Non-POSIX return error. BC_ERR_POSIX_RET, /// Non-POSIX boolean operator used error. BC_ERR_POSIX_BOOL, /// POSIX relation operator used outside if, while, or for statements error. BC_ERR_POSIX_REL_POS, /// Multiple POSIX relation operators used in an if, while, or for statement /// error. BC_ERR_POSIX_MULTIREL, /// Empty statements in POSIX for loop error. BC_ERR_POSIX_FOR, /// Non-POSIX exponential (scientific or engineering) number used error. BC_ERR_POSIX_EXP_NUM, /// Non-POSIX array reference error. BC_ERR_POSIX_REF, /// Non-POSIX void error. BC_ERR_POSIX_VOID, /// Non-POSIX brace position used error. BC_ERR_POSIX_BRACE, /// String used in expression. BC_ERR_POSIX_EXPR_STRING, #endif // BC_ENABLED // Number of elements. BC_ERR_NELEMS, #if BC_ENABLED /// A marker for the start of POSIX errors. BC_ERR_POSIX_START = BC_ERR_POSIX_NAME_LEN, /// A marker for the end of POSIX errors. BC_ERR_POSIX_END = BC_ERR_POSIX_EXPR_STRING, #endif // BC_ENABLED } BcErr; // The indices of each category of error in bc_errs[], and used in bc_err_ids[] // to associate actual errors with their categories. /// Math error category. #define BC_ERR_IDX_MATH (0) /// Parse (and lex) error category. #define BC_ERR_IDX_PARSE (1) /// Runtime error category. #define BC_ERR_IDX_EXEC (2) /// Fatal error category. #define BC_ERR_IDX_FATAL (3) /// Number of categories. #define BC_ERR_IDX_NELEMS (4) // If bc is enabled, we add an extra category for POSIX warnings. #if BC_ENABLED /// POSIX warning category. #define BC_ERR_IDX_WARN (BC_ERR_IDX_NELEMS) #endif // BC_ENABLED /// Do a longjmp(). This is what to use when activating an "exception", i.e., a /// longjmp(). With debug code, it will print the name of the function it jumped /// from. #if BC_DEBUG_CODE #define BC_JMP bc_vm_jmp(__func__) #else // BC_DEBUG_CODE #define BC_JMP bc_vm_jmp() #endif // BC_DEBUG_CODE /// Returns true if an exception is in flight, false otherwise. #define BC_SIG_EXC \ BC_UNLIKELY(vm.status != (sig_atomic_t) BC_STATUS_SUCCESS || vm.sig) /// Returns true if there is *no* exception in flight, false otherwise. #define BC_NO_SIG_EXC \ BC_LIKELY(vm.status == (sig_atomic_t) BC_STATUS_SUCCESS && !vm.sig) #ifndef NDEBUG /// Assert that signals are locked. There are non-async-signal-safe functions in /// bc, and they *must* have signals locked. Other functions are expected to /// *not* have signals locked, for reasons. So this is a pre-built assert /// (no-op in non-debug mode) that check that signals are locked. #define BC_SIG_ASSERT_LOCKED do { assert(vm.sig_lock); } while (0) /// Assert that signals are unlocked. There are non-async-signal-safe functions /// in bc, and they *must* have signals locked. Other functions are expected to /// *not* have signals locked, for reasons. So this is a pre-built assert /// (no-op in non-debug mode) that check that signals are unlocked. #define BC_SIG_ASSERT_NOT_LOCKED do { assert(vm.sig_lock == 0); } while (0) #else // NDEBUG /// Assert that signals are locked. There are non-async-signal-safe functions in /// bc, and they *must* have signals locked. Other functions are expected to /// *not* have signals locked, for reasons. So this is a pre-built assert /// (no-op in non-debug mode) that check that signals are locked. #define BC_SIG_ASSERT_LOCKED /// Assert that signals are unlocked. There are non-async-signal-safe functions /// in bc, and they *must* have signals locked. Other functions are expected to /// *not* have signals locked, for reasons. So this is a pre-built assert /// (no-op in non-debug mode) that check that signals are unlocked. #define BC_SIG_ASSERT_NOT_LOCKED #endif // NDEBUG /// Locks signals. #define BC_SIG_LOCK \ do { \ BC_SIG_ASSERT_NOT_LOCKED; \ vm.sig_lock = 1; \ } while (0) /// Unlocks signals. If a signal happened, then this will cause a jump. #define BC_SIG_UNLOCK \ do { \ BC_SIG_ASSERT_LOCKED; \ vm.sig_lock = 0; \ if (vm.sig) BC_JMP; \ } while (0) /// Locks signals, regardless of if they are already locked. This is really only /// used after labels that longjmp() goes to after the jump because the cleanup /// code must have signals locked, and BC_LONGJMP_CONT will unlock signals if it /// doesn't jump. #define BC_SIG_MAYLOCK \ do { \ vm.sig_lock = 1; \ } while (0) /// Unlocks signals, regardless of if they were already unlocked. If a signal /// happened, then this will cause a jump. #define BC_SIG_MAYUNLOCK \ do { \ vm.sig_lock = 0; \ if (vm.sig) BC_JMP; \ } while (0) /* * Locks signals, but stores the old lock state, to be restored later by * BC_SIG_TRYUNLOCK. * @param v The variable to store the old lock state to. */ #define BC_SIG_TRYLOCK(v) \ do { \ v = vm.sig_lock; \ vm.sig_lock = 1; \ } while (0) /* Restores the previous state of a signal lock, and if it is now unlocked, * initiates an exception/jump. * @param v The old lock state. */ #define BC_SIG_TRYUNLOCK(v) \ do { \ vm.sig_lock = (v); \ if (!(v) && vm.sig) BC_JMP; \ } while (0) /** * Sets a jump, and sets it up as well so that if a longjmp() happens, bc will * immediately goto a label where some cleanup code is. This one assumes that * signals are not locked and will lock them, set the jump, and unlock them. * Setting the jump also includes pushing the jmp_buf onto the jmp_buf stack. * This grows the jmp_bufs vector first to prevent a fatal error from happening * after the setjmp(). This is done because BC_SETJMP(l) is assumed to be used * *before* the actual initialization calls that need the setjmp(). * param l The label to jump to on a longjmp(). */ #define BC_SETJMP(l) \ do { \ sigjmp_buf sjb; \ BC_SIG_LOCK; \ bc_vec_grow(&vm.jmp_bufs, 1); \ if (sigsetjmp(sjb, 0)) { \ assert(BC_SIG_EXC); \ goto l; \ } \ bc_vec_push(&vm.jmp_bufs, &sjb); \ BC_SIG_UNLOCK; \ } while (0) /** * Sets a jump like BC_SETJMP, but unlike BC_SETJMP, it assumes signals are * locked and will just set the jump. This does *not* have a call to * bc_vec_grow() because it is assumed that BC_SETJMP_LOCKED(l) is used *after* * the initializations that need the setjmp(). * param l The label to jump to on a longjmp(). */ #define BC_SETJMP_LOCKED(l) \ do { \ sigjmp_buf sjb; \ BC_SIG_ASSERT_LOCKED; \ if (sigsetjmp(sjb, 0)) { \ assert(BC_SIG_EXC); \ goto l; \ } \ bc_vec_push(&vm.jmp_bufs, &sjb); \ } while (0) /// Used after cleanup labels set by BC_SETJMP and BC_SETJMP_LOCKED to jump to /// the next place. This is what continues the stack unwinding. This basically /// copies BC_SIG_UNLOCK into itself, but that is because its condition for /// jumping is BC_SIG_EXC, not just that a signal happened. #define BC_LONGJMP_CONT \ do { \ BC_SIG_ASSERT_LOCKED; \ if (!vm.sig_pop) bc_vec_pop(&vm.jmp_bufs); \ vm.sig_lock = 0; \ if (BC_SIG_EXC) BC_JMP; \ } while (0) /// Unsets a jump. It always assumes signals are locked. This basically just /// pops a jmp_buf off of the stack of jmp_bufs, and since the jump mechanism /// always jumps to the location at the top of the stack, this effectively /// undoes a setjmp(). #define BC_UNSETJMP \ do { \ BC_SIG_ASSERT_LOCKED; \ bc_vec_pop(&vm.jmp_bufs); \ } while (0) /// Stops a stack unwinding. Technically, a stack unwinding needs to be done /// manually, but it will always be done unless certain flags are cleared. This /// clears the flags. #define BC_LONGJMP_STOP \ do { \ vm.sig_pop = 0; \ vm.sig = 0; \ } while (0) // Various convenience macros for calling the bc's error handling routine. #if BC_ENABLE_LIBRARY /** * Call bc's error handling routine. * @param e The error. * @param l The line of the script that the error happened. * @param ... Extra arguments for error messages as necessary. */ #define bc_error(e, l, ...) (bc_vm_handleError((e))) /** * Call bc's error handling routine. * @param e The error. */ #define bc_err(e) (bc_vm_handleError((e))) /** * Call bc's error handling routine. * @param e The error. */ #define bc_verr(e, ...) (bc_vm_handleError((e))) #else // BC_ENABLE_LIBRARY /** * Call bc's error handling routine. * @param e The error. * @param l The line of the script that the error happened. * @param ... Extra arguments for error messages as necessary. */ #define bc_error(e, l, ...) (bc_vm_handleError((e), (l), __VA_ARGS__)) /** * Call bc's error handling routine. * @param e The error. */ #define bc_err(e) (bc_vm_handleError((e), 0)) /** * Call bc's error handling routine. * @param e The error. */ #define bc_verr(e, ...) (bc_vm_handleError((e), 0, __VA_ARGS__)) #endif // BC_ENABLE_LIBRARY /** * Returns true if status @a s is an error, false otherwise. * @param s The status to test. * @return True if @a s is an error, false otherwise. */ #define BC_STATUS_IS_ERROR(s) \ ((s) >= BC_STATUS_ERROR_MATH && (s) <= BC_STATUS_ERROR_FATAL) // Convenience macros that can be placed at the beginning and exits of functions // for easy marking of where functions are entered and exited. #if BC_DEBUG_CODE #define BC_FUNC_ENTER \ do { \ size_t bc_func_enter_i; \ for (bc_func_enter_i = 0; bc_func_enter_i < vm.func_depth; \ ++bc_func_enter_i) \ { \ bc_file_puts(&vm.ferr, bc_flush_none, " "); \ } \ vm.func_depth += 1; \ bc_file_printf(&vm.ferr, "Entering %s\n", __func__); \ bc_file_flush(&vm.ferr, bc_flush_none); \ } while (0); #define BC_FUNC_EXIT \ do { \ size_t bc_func_enter_i; \ vm.func_depth -= 1; \ for (bc_func_enter_i = 0; bc_func_enter_i < vm.func_depth; \ ++bc_func_enter_i) \ { \ bc_file_puts(&vm.ferr, bc_flush_none, " "); \ } \ bc_file_printf(&vm.ferr, "Leaving %s\n", __func__); \ bc_file_flush(&vm.ferr, bc_flush_none); \ } while (0); #else // BC_DEBUG_CODE #define BC_FUNC_ENTER #define BC_FUNC_EXIT #endif // BC_DEBUG_CODE #endif // BC_STATUS_H diff --git a/include/vector.h b/include/vector.h index 8f7cbbcc2b50..c35d22c9eff7 100644 --- a/include/vector.h +++ b/include/vector.h @@ -1,461 +1,461 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2021 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. * * ***************************************************************************** * * Definitions for bc vectors (resizable arrays). * */ #ifndef BC_VECTOR_H #define BC_VECTOR_H #include #include #include #include /// An invalid index for a map to mark when an item does not exist. #define BC_VEC_INVALID_IDX (SIZE_MAX) /// The starting capacity for vectors. This is based on the minimum allocation /// for 64-bit systems. #define BC_VEC_START_CAP (UINTMAX_C(1)<<5) /// An alias. typedef unsigned char uchar; /** * A destructor. Frees the object that @a ptr points to. This is used by vectors * to free the memory they own. * @param ptr Pointer to the data to free. */ typedef void (*BcVecFree)(void *ptr); // Forward declaration. struct BcId; #if BC_LONG_BIT >= 64 /// An integer to shrink the size of a vector by using these instead of size_t. typedef uint32_t BcSize; #else // BC_LONG_BIT >= 64 /// An integer to shrink the size of a vector by using these instead of size_t. typedef uint16_t BcSize; #endif // BC_LONG_BIT >= 64 /// An enum of all of the destructors. We use an enum to save space. typedef enum BcDtorType { /// No destructor needed. BC_DTOR_NONE, /// Vector destructor. BC_DTOR_VEC, /// BcNum destructor. BC_DTOR_NUM, #if !BC_ENABLE_LIBRARY #ifndef NDEBUG /// BcFunc destructor. BC_DTOR_FUNC, #endif // NDEBUG /// BcSlab destructor. BC_DTOR_SLAB, /// BcConst destructor. BC_DTOR_CONST, /// BcResult destructor. BC_DTOR_RESULT, #if BC_ENABLE_HISTORY /// String destructor for history, which is *special*. BC_DTOR_HISTORY_STRING, #endif // BC_ENABLE_HISTORY #else // !BC_ENABLE_LIBRARY /// Destructor for bcl numbers. BC_DTOR_BCL_NUM, #endif // !BC_ENABLE_LIBRARY } BcDtorType; /// The actual vector struct. typedef struct BcVec { /// The vector array itself. This uses a char* because it is compatible with /// pointers of all other types, and I can do pointer arithmetic on it. char *restrict v; /// The length of the vector, which is how many items actually exist. size_t len; /// The capacity of the vector, which is how many items can fit in the /// current allocation. size_t cap; /// The size of the items in the vector, as returned by sizeof(). BcSize size; /// The destructor as a BcDtorType enum. BcSize dtor; } BcVec; /** * Initializes a vector. * @param v The vector to initialize. * @param esize The size of the elements, as returned by sizeof(). * @param dtor The destructor of the elements, as a BcDtorType enum. */ void bc_vec_init(BcVec *restrict v, size_t esize, BcDtorType dtor); /** * Expands the vector to have a capacity of @a req items, if it doesn't have * enough already. * @param v The vector to expand. * @param req The requested capacity. */ void bc_vec_expand(BcVec *restrict v, size_t req); /** * Grow a vector by at least @a n elements. * @param v The vector to grow. * @param n The number of elements to grow the vector by. */ void bc_vec_grow(BcVec *restrict v, size_t n); /** * Pops @a n items off the back of the vector. The vector must have at least * @a n elements. * @param v The vector to pop off of. * @param n The number of elements to pop off. */ void bc_vec_npop(BcVec *restrict v, size_t n); /** * Pops @a n items, starting at index @a idx, off the vector. The vector must * have at least @a n elements after the @a idx index. Any remaining elements at * the end are moved up to fill the hole. * @param v The vector to pop off of. * @param n The number of elements to pop off. * @param idx The index to start popping at. */ void bc_vec_npopAt(BcVec *restrict v, size_t n, size_t idx); /** * Pushes one item on the back of the vector. It does a memcpy(), but it assumes * that the vector takes ownership of the data. * @param v The vector to push onto. * @param data A pointer to the data to push. */ void bc_vec_push(BcVec *restrict v, const void *data); /** * Pushes @a n items on the back of the vector. It does a memcpy(), but it * assumes that the vector takes ownership of the data. * @param v The vector to push onto. * @param data A pointer to the elements of data to push. */ void bc_vec_npush(BcVec *restrict v, size_t n, const void *data); /** * Push an empty element and return a pointer to it. This is done as an * optimization where initializing an item needs a pointer anyway. It removes an * extra memcpy(). * @param v The vector to push onto. * @return A pointer to the newly-pushed element. */ void* bc_vec_pushEmpty(BcVec *restrict v); /** * Pushes a byte onto a bytecode vector. This is a convenience function for the * parsers pushing instructions. The vector must be a bytecode vector. * @param v The vector to push onto. * @param data The byte to push. */ void bc_vec_pushByte(BcVec *restrict v, uchar data); /** * Pushes and index onto a bytecode vector. The vector must be a bytecode * vector. For more info about why and how this is done, see the development * manual (manuals/development#bytecode-indices). * @param v The vector to push onto. * @param idx The index to push. */ void bc_vec_pushIndex(BcVec *restrict v, size_t idx); /** * Push an item onto the vector at a certain index. The index must be valid * (either exists or is equal to the length of the vector). The elements at that * index and after are moved back one element and kept in the same order. This * is how the map vectors are kept sorted. * @param v The vector to push onto. * @param data A pointer to the data to push. * @param idx The index to push at. */ void bc_vec_pushAt(BcVec *restrict v, const void *data, size_t idx); /** * Empties the vector and sets it to the string. The vector must be a valid * vector and must have chars as its elements. * @param v The vector to set to the string. * @param len The length of the string. This can be less than the actual length * of the string, but must never be more. * @param str The string to push. */ void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str); /** * Appends the string to the end of the vector, which must be holding a string * (nul byte-terminated) already. * @param v The vector to append to. * @param str The string to append (by copying). */ void bc_vec_concat(BcVec *restrict v, const char *restrict str); /** * Empties a vector and pushes a nul-byte at the first index. The vector must be * a char vector. */ void bc_vec_empty(BcVec *restrict v); #if BC_ENABLE_HISTORY /** * Replaces an item at a particular index. No elements are moved. The index must * exist. * @param v The vector to replace an item on. * @param idx The index of the item to replace. * @param data The data to replace the item with. */ void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data); #endif // BC_ENABLE_HISTORY /** * Returns a pointer to the item in the vector at the index. This is the key * function for vectors. The index must exist. * @param v The vector. * @param idx The index to the item to get a pointer to. * @return A pointer to the item at @a idx. */ void* bc_vec_item(const BcVec *restrict v, size_t idx); /** * Returns a pointer to the item in the vector at the index, reversed. This is * another key function for vectors. The index must exist. * @param v The vector. * @param idx The index to the item to get a pointer to. * @return A pointer to the item at len - @a idx - 1. */ void* bc_vec_item_rev(const BcVec *restrict v, size_t idx); /** * Zeros a vector. The vector must not be allocated. * @param v The vector to clear. */ void bc_vec_clear(BcVec *restrict v); /** * Frees a vector and its elements. This is a destructor. * @param vec A vector as a void pointer. */ void bc_vec_free(void *vec); /** * Attempts to insert an item into a map and returns true if it succeeded, false * if the item already exists. * @param v The map vector to insert into. * @param name The name of the item to insert. This name is assumed to be owned * by another entity. * @param idx The index of the partner array where the actual item is. * @param i A pointer to an index that will be set to the index of the item * in the map. * @return True if the item was inserted, false if the item already exists. */ bool bc_map_insert(BcVec *restrict v, const char *name, size_t idx, size_t *restrict i); /** * Returns the index of the item with @a name in the map, or BC_VEC_INVALID_IDX * if it doesn't exist. * @param v The map vector. * @param name The name of the item to find. * @return The index in the map of the item with @a name, or * BC_VEC_INVALID_IDX if the item does not exist. */ size_t bc_map_index(const BcVec *restrict v, const char *name); #if DC_ENABLED /** * Returns the name of the item at index @a idx in the map. * @param v The map vector. * @param idx The index. * @return The name of the item at @a idx. */ const char* bc_map_name(const BcVec *restrict v, size_t idx); #endif // DC_ENABLED /** * Pops one item off of the vector. * @param v The vector to pop one item off of. */ #define bc_vec_pop(v) (bc_vec_npop((v), 1)) /** * Pops all items off of the vector. * @param v The vector to pop all items off of. */ #define bc_vec_popAll(v) (bc_vec_npop((v), (v)->len)) /** * Return a pointer to the last item in the vector, or first if it's being * treated as a stack. * @param v The vector to get the top of stack of. */ #define bc_vec_top(v) (bc_vec_item_rev((v), 0)) /** * Initializes a vector to serve as a map. * @param v The vector to initialize. */ #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), BC_DTOR_NONE)) /// A reference to the array of destructors. extern const BcVecFree bc_vec_dtors[]; #if !BC_ENABLE_LIBRARY /// The allocated size of slabs. #define BC_SLAB_SIZE (4096) /// A slab for allocating strings. typedef struct BcSlab { /// The actual allocation. char *s; /// How many bytes of the slab are taken. size_t len; } BcSlab; /** * Frees a slab. This is a destructor. * @param slab The slab as a void pointer. */ void bc_slab_free(void *slab); /** * Initializes a slab vector. * @param v The vector to initialize. */ void bc_slabvec_init(BcVec *restrict v); /** * Duplicates the string using slabs in the slab vector. * @param v The slab vector. * @param str The string to duplicate. * @return A pointer to the duplicated string, owned by the slab vector. */ char* bc_slabvec_strdup(BcVec *restrict v, const char *str); #if BC_ENABLED /** * Undoes the last allocation on the slab vector. This allows bc to have a * heap-based stacks for strings. This is used by the bc parser. */ void bc_slabvec_undo(BcVec *restrict v, size_t len); #endif // BC_ENABLED /** * Clears a slab vector. This deallocates all but the first slab and clears the * first slab. * @param v The slab vector to clear. */ void bc_slabvec_clear(BcVec *restrict v); #if BC_DEBUG_CODE /** * Prints all of the items in a slab vector, in order. * @param v The vector whose items will be printed. */ void bc_slabvec_print(BcVec *v, const char *func); #endif // BC_DEBUG_CODE /// A convenience macro for freeing a vector of slabs. #define bc_slabvec_free bc_vec_free #ifndef _WIN32 /** * A macro to get rid of a warning on Windows. * @param d The destination string. * @param l The length of the destination string. This has to be big enough to * contain @a s. * @param s The source string. */ -#define strcpy(d, l, s) strcpy(d, s) +#define bc_strcpy(d, l, s) strcpy(d, s) #else // _WIN32 /** * A macro to get rid of a warning on Windows. * @param d The destination string. * @param l The length of the destination string. This has to be big enough to * contain @a s. * @param s The source string. */ -#define strcpy(d, l, s) strcpy_s(d, l, s) +#define bc_strcpy(d, l, s) strcpy_s(d, l, s) #endif // _WIN32 #endif // !BC_ENABLE_LIBRARY #endif // BC_VECTOR_H diff --git a/include/version.h b/include/version.h index 5127c28e2b4a..071b123cccf1 100644 --- a/include/version.h +++ b/include/version.h @@ -1,42 +1,42 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2021 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 version of bc. * */ #ifndef BC_VERSION_H #define BC_VERSION_H /// The current version. -#define VERSION 5.0.0 +#define VERSION 5.0.2 #endif // BC_VERSION_H diff --git a/src/vector.c b/src/vector.c index 1cd90f729956..ebc2e76ca8c8 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,586 +1,586 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2021 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. * * ***************************************************************************** * * Code to manipulate vectors (resizable arrays). * */ #include #include #include #include #include #include #include void bc_vec_grow(BcVec *restrict v, size_t n) { size_t cap, len; sig_atomic_t lock; cap = v->cap; len = v->len + n; // If this is true, we might overflow. if (len > SIZE_MAX / 2) cap = len; else { // Keep doubling until larger. while (cap < len) cap += cap; } BC_SIG_TRYLOCK(lock); v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size)); v->cap = cap; BC_SIG_TRYUNLOCK(lock); } void bc_vec_init(BcVec *restrict v, size_t esize, BcDtorType dtor) { BC_SIG_ASSERT_LOCKED; assert(v != NULL && esize); v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize)); v->size = (BcSize) esize; v->cap = BC_VEC_START_CAP; v->len = 0; v->dtor = (BcSize) dtor; } void bc_vec_expand(BcVec *restrict v, size_t req) { assert(v != NULL); // Only expand if necessary. if (v->cap < req) { sig_atomic_t lock; BC_SIG_TRYLOCK(lock); v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size)); v->cap = req; BC_SIG_TRYUNLOCK(lock); } } void bc_vec_npop(BcVec *restrict v, size_t n) { sig_atomic_t lock; assert(v != NULL && n <= v->len); BC_SIG_TRYLOCK(lock); if (!v->dtor) v->len -= n; else { const BcVecFree d = bc_vec_dtors[v->dtor]; size_t esize = v->size; size_t len = v->len - n; // Loop through and manually destruct every element. while (v->len > len) d(v->v + (esize * --v->len)); } BC_SIG_TRYUNLOCK(lock); } void bc_vec_npopAt(BcVec *restrict v, size_t n, size_t idx) { char* ptr, *data; sig_atomic_t lock; assert(v != NULL); assert(idx + n < v->len); // Grab start and end pointers. ptr = bc_vec_item(v, idx); data = bc_vec_item(v, idx + n); BC_SIG_TRYLOCK(lock); if (v->dtor) { size_t i; const BcVecFree d = bc_vec_dtors[v->dtor]; // Destroy every popped item. for (i = 0; i < n; ++i) d(bc_vec_item(v, idx + i)); } v->len -= n; memmove(ptr, data, (v->len - idx) * v->size); BC_SIG_TRYUNLOCK(lock); } void bc_vec_npush(BcVec *restrict v, size_t n, const void *data) { sig_atomic_t lock; size_t esize; assert(v != NULL && data != NULL); BC_SIG_TRYLOCK(lock); // Grow if necessary. if (v->len + n > v->cap) bc_vec_grow(v, n); esize = v->size; // Copy the elements in. memcpy(v->v + (esize * v->len), data, esize * n); v->len += n; BC_SIG_TRYUNLOCK(lock); } inline void bc_vec_push(BcVec *restrict v, const void *data) { bc_vec_npush(v, 1, data); } void* bc_vec_pushEmpty(BcVec *restrict v) { sig_atomic_t lock; void *ptr; assert(v != NULL); BC_SIG_TRYLOCK(lock); // Grow if necessary. if (v->len + 1 > v->cap) bc_vec_grow(v, 1); ptr = v->v + v->size * v->len; v->len += 1; BC_SIG_TRYUNLOCK(lock); return ptr; } inline void bc_vec_pushByte(BcVec *restrict v, uchar data) { assert(v != NULL && v->size == sizeof(uchar)); bc_vec_npush(v, 1, &data); } void bc_vec_pushIndex(BcVec *restrict v, size_t idx) { uchar amt, nums[sizeof(size_t) + 1]; assert(v != NULL); assert(v->size == sizeof(uchar)); // Encode the index. for (amt = 0; idx; ++amt) { nums[amt + 1] = (uchar) idx; idx &= ((size_t) ~(UCHAR_MAX)); idx >>= sizeof(uchar) * CHAR_BIT; } nums[0] = amt; // Push the index onto the vector. bc_vec_npush(v, amt + 1, nums); } void bc_vec_pushAt(BcVec *restrict v, const void *data, size_t idx) { assert(v != NULL && data != NULL && idx <= v->len); BC_SIG_ASSERT_LOCKED; // Do the easy case. if (idx == v->len) bc_vec_push(v, data); else { char *ptr; size_t esize; // Grow if necessary. if (v->len == v->cap) bc_vec_grow(v, 1); esize = v->size; ptr = v->v + esize * idx; memmove(ptr + esize, ptr, esize * (v->len++ - idx)); memcpy(ptr, data, esize); } } void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str) { sig_atomic_t lock; assert(v != NULL && v->size == sizeof(char)); assert(!v->dtor); assert(!v->len || !v->v[v->len - 1]); assert(v->v != str); BC_SIG_TRYLOCK(lock); bc_vec_popAll(v); bc_vec_expand(v, bc_vm_growSize(len, 1)); memcpy(v->v, str, len); v->len = len; bc_vec_pushByte(v, '\0'); BC_SIG_TRYUNLOCK(lock); } void bc_vec_concat(BcVec *restrict v, const char *restrict str) { sig_atomic_t lock; assert(v != NULL && v->size == sizeof(char)); assert(!v->dtor); assert(!v->len || !v->v[v->len - 1]); assert(v->v != str); BC_SIG_TRYLOCK(lock); // If there is already a string, erase its nul byte. if (v->len) v->len -= 1; bc_vec_npush(v, strlen(str) + 1, str); BC_SIG_TRYUNLOCK(lock); } void bc_vec_empty(BcVec *restrict v) { sig_atomic_t lock; assert(v != NULL && v->size == sizeof(char)); assert(!v->dtor); BC_SIG_TRYLOCK(lock); bc_vec_popAll(v); bc_vec_pushByte(v, '\0'); BC_SIG_TRYUNLOCK(lock); } #if BC_ENABLE_HISTORY void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data) { char *ptr; BC_SIG_ASSERT_LOCKED; assert(v != NULL); ptr = bc_vec_item(v, idx); if (v->dtor) bc_vec_dtors[v->dtor](ptr); memcpy(ptr, data, v->size); } #endif // BC_ENABLE_HISTORY inline void* bc_vec_item(const BcVec *restrict v, size_t idx) { assert(v != NULL && v->len && idx < v->len); return v->v + v->size * idx; } inline void* bc_vec_item_rev(const BcVec *restrict v, size_t idx) { assert(v != NULL && v->len && idx < v->len); return v->v + v->size * (v->len - idx - 1); } inline void bc_vec_clear(BcVec *restrict v) { BC_SIG_ASSERT_LOCKED; v->v = NULL; v->len = 0; v->dtor = BC_DTOR_NONE; } void bc_vec_free(void *vec) { BcVec *v = (BcVec*) vec; BC_SIG_ASSERT_LOCKED; bc_vec_popAll(v); free(v->v); } #if !BC_ENABLE_LIBRARY /** * Finds a name in a map by binary search. Returns the index where the item * *would* be if it doesn't exist. Callers are responsible for checking that the * item exists at the index. * @param v The map. * @param name The name to find. * @return The index of the item with @a name, or where the item would be * if it does not exist. */ static size_t bc_map_find(const BcVec *restrict v, const char *name) { size_t low = 0, high = v->len; while (low < high) { size_t mid = (low + high) / 2; const BcId *id = bc_vec_item(v, mid); int result = strcmp(name, id->name); if (!result) return mid; else if (result < 0) high = mid; else low = mid + 1; } return low; } bool bc_map_insert(BcVec *restrict v, const char *name, size_t idx, size_t *restrict i) { BcId id; BcVec *slabs; BC_SIG_ASSERT_LOCKED; assert(v != NULL && name != NULL && i != NULL); *i = bc_map_find(v, name); assert(*i <= v->len); if (*i != v->len && !strcmp(name, ((BcId*) bc_vec_item(v, *i))->name)) return false; #if BC_ENABLED slabs = BC_IS_DC ? &vm.main_slabs : &vm.other_slabs; #else // BC_ENABLED slabs = &vm.main_slabs; #endif // BC_ENABLED id.name = bc_slabvec_strdup(slabs, name); id.idx = idx; bc_vec_pushAt(v, &id, *i); return true; } size_t bc_map_index(const BcVec *restrict v, const char *name) { size_t i; assert(v != NULL && name != NULL); i = bc_map_find(v, name); // If out of range, return invalid. if (i >= v->len) return BC_VEC_INVALID_IDX; // Make sure the item exists. return strcmp(name, ((BcId*) bc_vec_item(v, i))->name) ? BC_VEC_INVALID_IDX : i; } #if DC_ENABLED const char* bc_map_name(const BcVec *restrict v, size_t idx) { size_t i, len = v->len; for (i = 0; i < len; ++i) { BcId* id = (BcId*) bc_vec_item(v, i); if (id->idx == idx) return id->name; } BC_UNREACHABLE return ""; } #endif // DC_ENABLED /** * Initializes a single slab. * @param s The slab to initialize. */ static void bc_slab_init(BcSlab *s) { s->s = bc_vm_malloc(BC_SLAB_SIZE); s->len = 0; } /** * Adds a string to a slab and returns a pointer to it, or NULL if it could not * be added. * @param s The slab to add to. * @param str The string to add. * @param len The length of the string, including its nul byte. * @return A pointer to the new string in the slab, or NULL if it could not * be added. */ static char* bc_slab_add(BcSlab *s, const char *str, size_t len) { char *ptr; assert(s != NULL); assert(str != NULL); assert(len == strlen(str) + 1); if (s->len + len > BC_SLAB_SIZE) return NULL; ptr = (char*) (s->s + s->len); - strcpy(ptr, len, str); + bc_strcpy(ptr, len, str); s->len += len; return ptr; } void bc_slab_free(void *slab) { free(((BcSlab*) slab)->s); } void bc_slabvec_init(BcVec* v) { BcSlab *slab; assert(v != NULL); bc_vec_init(v, sizeof(BcSlab), BC_DTOR_SLAB); // We always want to have at least one slab. slab = bc_vec_pushEmpty(v); bc_slab_init(slab); } char* bc_slabvec_strdup(BcVec *v, const char *str) { char *s; size_t len; BcSlab slab; BcSlab *slab_ptr; BC_SIG_ASSERT_LOCKED; assert(v != NULL && v->len); assert(str != NULL); len = strlen(str) + 1; // If the len is greater than 128, then just allocate it with malloc. if (BC_UNLIKELY(len >= BC_SLAB_SIZE)) { // SIZE_MAX is a marker for these standalone allocations. slab.len = SIZE_MAX; slab.s = bc_vm_strdup(str); // Push the standalone slab. bc_vec_pushAt(v, &slab, v->len - 1); return slab.s; } // Add to a slab. slab_ptr = bc_vec_top(v); s = bc_slab_add(slab_ptr, str, len); // If it couldn't be added, add a slab and try again. if (BC_UNLIKELY(s == NULL)) { slab_ptr = bc_vec_pushEmpty(v); bc_slab_init(slab_ptr); s = bc_slab_add(slab_ptr, str, len); assert(s != NULL); } return s; } void bc_slabvec_clear(BcVec *v) { BcSlab *s; bool again; // This complicated loop exists because of standalone allocations over 128 // bytes. do { // Get the first slab. s = bc_vec_item(v, 0); // Either the slab must be valid (not standalone), or there must be // another slab. assert(s->len != SIZE_MAX || v->len > 1); // Do we have to loop again? We do if it's a standalone allocation. again = (s->len == SIZE_MAX); // Pop the standalone allocation, not the one after it. if (again) bc_vec_npopAt(v, 1, 0); } while(again); // If we get here, we know that the first slab is a valid slab. We want to // pop all of the other slabs. if (v->len > 1) bc_vec_npop(v, v->len - 1); // Empty the first slab. s->len = 0; } #endif // !BC_ENABLE_LIBRARY #if BC_DEBUG_CODE void bc_slabvec_print(BcVec *v, const char *func) { size_t i; BcSlab *s; bc_file_printf(&vm.ferr, "%s\n", func); for (i = 0; i < v->len; ++i) { s = bc_vec_item(v, i); bc_file_printf(&vm.ferr, "%zu { s = %zu, len = %zu }\n", i, (uintptr_t) s->s, s->len); } bc_file_puts(&vm.ferr, bc_flush_none, "\n"); bc_file_flush(&vm.ferr, bc_flush_none); } #endif // BC_DEBUG_CODE diff --git a/tests/all.sh b/tests/all.sh index 04afdb391d0e..a4a9c8bc8936 100755 --- a/tests/all.sh +++ b/tests/all.sh @@ -1,134 +1,137 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # set -e script="$0" testdir=$(dirname "$script") . "$testdir/../scripts/functions.sh" # Command-line processing. if [ "$#" -ge 1 ]; then d="$1" shift else err_exit "usage: $script dir [run_extra_tests] [run_stack_tests] [gen_tests] [time_tests] [exec args...]" 1 fi if [ "$#" -lt 1 ]; then extra=1 else extra="$1" shift fi if [ "$#" -lt 1 ]; then run_stack_tests=1 else run_stack_tests="$1" shift fi if [ "$#" -lt 1 ]; then generate_tests=1 else generate_tests="$1" shift fi if [ "$#" -lt 1 ]; then time_tests=0 else time_tests="$1" shift fi if [ "$#" -lt 1 ]; then exe="$testdir/../bin/$d" else exe="$1" shift fi stars="***********************************************************************" printf '%s\n' "$stars" # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then halt="quit" else halt="q" fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH # Get the list of tests that require extra math. extra_required=$(cat "$testdir/extra_required.txt") printf '\nRunning %s tests...\n\n' "$d" # Run the tests one at a time. while read t; do # If it requires extra, then skip if we don't have it. if [ "$extra" -eq 0 ]; then if [ -z "${extra_required##*$t*}" ]; then printf 'Skipping %s %s\n' "$d" "$t" continue fi fi sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@" done < "$testdir/$d/all.txt" # stdin tests. sh "$testdir/stdin.sh" "$d" "$exe" "$@" # Script tests. sh "$testdir/scripts.sh" "$d" "$extra" "$run_stack_tests" "$generate_tests" \ "$time_tests" "$exe" "$@" # Read tests. sh "$testdir/read.sh" "$d" "$exe" "$@" # Error tests. sh "$testdir/errors.sh" "$d" "$exe" "$@" # Other tests. sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@" +# History tests. +sh "$testdir/history.sh" "$d" -a + printf '\nAll %s tests passed.\n' "$d" printf '\n%s\n' "$stars" diff --git a/tests/bc/timeconst.sh b/tests/bc/timeconst.sh index 5c5ec3806ab8..45e10c77bdf4 100755 --- a/tests/bc/timeconst.sh +++ b/tests/bc/timeconst.sh @@ -1,114 +1,116 @@ #! /bin/sh # # Copyright (c) 2018-2021 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. # # Tests the timeconst.bc script from the Linux kernel build. # You can find the script at kernel/time/timeconst.bc in any Linux repo. # One such repo is: https://github.com/torvalds/linux script="$0" testdir=$(dirname "$script") +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir/..} + # Gets the timeconst script, which could be a command-line argument. if [ "$#" -gt 0 ]; then timeconst="$1" shift else timeconst="$testdir/scripts/timeconst.bc" fi # Gets the executable, which could also be a command-line argument. if [ "$#" -gt 0 ]; then bc="$1" shift else bc="$testdir/../../bin/bc" fi # -out1="$testdir/bc_outputs/bc_timeconst.txt" -out2="$testdir/bc_outputs/bc_timeconst_results.txt" +out1="$outputdir/bc_outputs/bc_timeconst.txt" +out2="$outputdir/bc_outputs/bc_timeconst_results.txt" outdir=$(dirname "$out1") # Make sure the directory exists. if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi base=$(basename "$timeconst") # If the script does not exist, just skip. Running this test is not necessary. if [ ! -f "$timeconst" ]; then printf 'Warning: %s does not exist\n' "$timeconst" printf 'Skipping...\n' exit 0 fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH printf 'Running %s...' "$base" # Get a list of numbers. Funny how bc can help with that. nums=$(printf 'for (i = 0; i <= 1000; ++i) { i }\n' | bc) # Run each number through the script. for i in $nums; do # Run the GNU bc on the test. printf '%s\n' "$i" | bc -q "$timeconst" > "$out1" err="$?" # If the other bc failed, it's not GNU bc, or this bc. if [ "$err" -ne 0 ]; then printf '\nOther bc is not GNU compatible. Skipping...\n' exit 0 fi # Run the built bc on the test. printf '%s\n' "$i" | "$bc" "$@" -q "$timeconst" > "$out2" diff "$out1" "$out2" error="$?" # If fail, bail. if [ "$error" -ne 0 ]; then printf '\nFailed on input: %s\n' "$i" exit "$error" fi done rm -f "$out1" rm -f "$out2" exec printf 'pass\n' diff --git a/tests/dc/errors/33.txt b/tests/dc/errors/33.txt index 524b548e6f4d..7d01c535c665 100644 Binary files a/tests/dc/errors/33.txt and b/tests/dc/errors/33.txt differ diff --git a/tests/errors.sh b/tests/errors.sh index d6c120aab4e7..c8c82cfe356f 100755 --- a/tests/errors.sh +++ b/tests/errors.sh @@ -1,194 +1,174 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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: Test files cannot have empty lines! script="$0" testdir=$(dirname "$script") . "$testdir/../scripts/functions.sh" +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} + # Command-line processing. if [ "$#" -eq 0 ]; then printf 'usage: %s dir [exec args...]\n' "$script" exit 1 else d="$1" shift fi if [ "$#" -lt 1 ]; then exe="$testdir/../bin/$d" else exe="$1" shift fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH -out="$testdir/${d}_outputs/errors_results.txt" +out="$outputdir/${d}_outputs/errors_results.txt" outdir=$(dirname "$out") # Make sure the directory exists. if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi exebase=$(basename "$exe") # These are the filenames for the extra tests. posix="posix_errors" read_errors="read_errors" # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then opts="-l" halt="halt" read_call="read()" read_expr="${read_call}\n5+5;" else opts="-x" halt="q" fi printf 'Running %s command-line error tests...' "$d" printf '%s\n' "$halt" | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "command-line -e test" "$out" "$exebase" printf '%s\n' "$halt" | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "command-line -f test" "$out" "$exebase" printf 'pass\n' # Now test the error files in the standard tests directory. for testfile in $testdir/$d/*errors.txt; do if [ -z "${testfile##*$read_errors*}" ]; then # We don't test read errors here. Skip. continue fi # Test bc POSIX errors and warnings. if [ -z "${testfile##*$posix*}" ]; then # Just test warnings. line="last" printf '%s\n' "$line" | "$exe" "$@" "-lw" 2> "$out" > /dev/null err="$?" if [ "$err" -ne 0 ]; then die "$d" "returned an error ($err)" "POSIX warning" 1 fi checkerrtest "$d" "1" "$line" "$out" "$exebase" # Set the options for standard mode. options="-ls" else options="$opts" fi # Output something pretty. base=$(basename "$testfile") base="${base%.*}" printf 'Running %s %s...' "$d" "$base" # Test errors on each line of the file. Yes, each line has a separate error # case. while read -r line; do rm -f "$out" printf '%s\n' "$line" | "$exe" "$@" "$options" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "$line" "$out" "$exebase" done < "$testfile" printf 'pass\n' done -# I need to skip a test here on FreeBSD. -os=$(uname) - -# The list of files we need to skip. -skip_files=" -33.txt -" - # Test all the files in the errors directory. While the loop above does one test # for every line, this does one test per file, but it runs the file through # stdin and as a file on the command-line. for testfile in $testdir/$d/errors/*.txt; do - # If we are on FreeBSD... - if [ "$os" = "FreeBSD" ] && [ "$d" = "dc" ]; then - - b=$(basename "$testfile") - - # If the file is one of the skip files... - if [ -z "${skip_files##*$b*}" ]; then - - printf 'On FreeBSD; skipping %s...\n' "$testfile" - continue - - fi - fi - printf 'Running %s error file %s...' "$d" "$testfile" printf '%s\n' "$halt" | "$exe" "$@" $opts "$testfile" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "$testfile" "$out" "$exebase" printf 'pass\n' printf 'Running %s error file %s through cat...' "$d" "$testfile" cat "$testfile" | "$exe" "$@" $opts 2> "$out" > /dev/null err="$?" checkcrash "$d" "$err" "$testfile" printf 'pass\n' done diff --git a/tests/history.py b/tests/history.py index ae25c7cf2854..fff531652b1b 100755 --- a/tests/history.py +++ b/tests/history.py @@ -1,1087 +1,1082 @@ #! /usr/bin/python # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # import os, sys import time import signal try: import pexpect except ImportError: print("Could not find pexpect. Skipping...") sys.exit(0) # Housekeeping. script = sys.argv[0] testdir = os.path.dirname(script) +if "BC_TEST_OUTPUT_DIR" in os.environ: + outputdir = os.environ["BC_TEST_OUTPUT_DIR"] +else: + outputdir = testdir + prompt = ">>> " # This array is for escaping characters that are necessary to escape when # outputting to pexpect. Since pexpect takes regexes, these characters confuse # it unless we escape them. escapes = [ ']', '[', '+', ] # UTF-8 stress tests. utf8_stress1 = "ᆬḰ䋔䗅㜲ತ咡䒢岤䳰稨⣡嶣㷡嶏ⵐ䄺嵕ਅ奰痚㆜䊛拂䅙૩➋䛿ቬ竳Ϳᅠ❄产翷䮊௷Ỉ䷒䳜㛠➕傎ᗋᏯਕ䆐悙癐㺨" utf8_stress2 = "韠싧돳넨큚ꉿ뮴픷ꉲ긌�최릙걆鳬낽ꪁ퍼鈴핐黙헶ꪈ뮩쭀锻끥鉗겉욞며뛯꬐�ﻼ�度錐�" utf8_stress3 = "곻�䣹昲蜴Ὓ桢㎏⚦珢畣갴ﭱ鶶ๅ⶛뀁彻ꖒ䔾ꢚﱤ햔햞㐹�鼳뵡▿ⶾ꠩�纞⊐佧�ⵟ霘紳㱔籠뎼⊓搧硤" utf8_stress4 = "ᄀ𖢾🏴��" # An easy array for UTF-8 tests. utf8_stress_strs = [ utf8_stress1, utf8_stress2, utf8_stress3, utf8_stress4, ] -def spawn(exe, args, env, encoding=None, codec_errors='strict'): - if do_test: - f = open(testdir + "/" + exedir + "_outputs/history_test.txt", "wb") - return pexpect.popen_spawn.PopenSpawn([ exe ] + args, env=env, - encoding=encoding, codec_errors=codec_errors, stderr=f) - else: - return pexpect.spawn(exe, args, env=env, encoding=encoding, - codec_errors=codec_errors) - - # Check that the child output the expected line. If history is false, then # the output should change. def check_line(child, expected, prompt=">>> ", history=True): child.send("\n") prefix = "\r\n" if history else "" child.expect(prefix + expected + "\r\n" + prompt) # Write a string to output, checking all of the characters are output, # one-by-one. def write_str(child, s): for c in s: child.send(c) if c in escapes: child.expect("\\{}".format(c)) else: child.expect(c) # Check the bc banner. # @param child The child process. def bc_banner(child): bc_banner1 = "bc [0-9]+\.[0-9]+\.[0-9]+\r\n" bc_banner2 = "Copyright \(c\) 2018-[2-9][0-9][0-9][0-9] Gavin D. Howard and contributors\r\n" bc_banner3 = "Report bugs at: https://git.yzena.com/gavin/bc\r\n\r\n" bc_banner4 = "This is free software with ABSOLUTELY NO WARRANTY.\r\n\r\n" child.expect(bc_banner1) child.expect(bc_banner2) child.expect(bc_banner3) child.expect(bc_banner4) child.expect(prompt) # Common UTF-8 testing function. The index is the index into utf8_stress_strs # for which stress string to use. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. # @param idx The index of the UTF-8 stress string. def test_utf8(exe, args, env, idx, bc=True): # Because both bc and dc use this, make sure the banner doesn't pop. env["BC_BANNER"] = "0" child = pexpect.spawn(exe, args=args, env=env, encoding='utf-8', codec_errors='ignore') try: # Write the stress string. child.send(utf8_stress_strs[idx]) child.send("\n") child.expect("Parse error: bad character") if bc: child.send("quit") else: child.send("q") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # A random UTF-8 test with insert. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_utf8_0(exe, args, env, bc=True): # Because both bc and dc use this, make sure the banner doesn't pop. env["BC_BANNER"] = "0" child = pexpect.spawn(exe, args=args, env=env, encoding='utf-8', codec_errors='ignore') try: # Just random UTF-8 I generated somewhow, plus ensuring that insert works. write_str(child, "ﴪáá̵̗🈐ã") child.send("\x1b[D\x1b[D\x1b[D\x1b\x1b[Aℐ") child.send("\n") child.expect("Parse error: bad character") if bc: child.send("quit") else: child.send("q") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child def test_utf8_1(exe, args, env, bc=True): return test_utf8(exe, args, env, 0, bc) def test_utf8_2(exe, args, env, bc=True): return test_utf8(exe, args, env, 1, bc) def test_utf8_3(exe, args, env, bc=True): return test_utf8(exe, args, env, 2, bc) def test_utf8_4(exe, args, env, bc=True): return test_utf8(exe, args, env, 3, bc) # This tests a SIGINT with reset followed by a SIGQUIT. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_sigint_sigquit(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: child.send("\t") child.expect(" ") child.send("\x03") child.send("\x1c") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Test for EOF. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_eof(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: child.send("\t") child.expect(" ") child.send("\x04") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Test for quiting SIGINT. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_sigint(exe, args, env): env["BC_SIGINT_RESET"] = "0" env["DC_SIGINT_RESET"] = "0" child = pexpect.spawn(exe, args=args, env=env) try: child.send("\t") child.expect(" ") child.send("\x03") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Test for SIGTSTP. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_sigtstp(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: child.send("\t") child.expect(" ") child.send("\x13") time.sleep(1) if not child.isalive(): print("child exited early") print(str(child)) print(str(child.buffer)) sys.exit(1) child.kill(signal.SIGCONT) write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Test for SIGSTOP. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_sigstop(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: child.send("\t") child.expect(" ") child.send("\x14") time.sleep(1) if not child.isalive(): print("child exited early") print(str(child)) print(str(child.buffer)) sys.exit(1) child.send("\x13") time.sleep(1) if not child.isalive(): print("child exited early") print(str(child)) print(str(child.buffer)) sys.exit(1) child.kill(signal.SIGCONT) child.send("quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child def test_bc_utf8_0(exe, args, env): return test_utf8_0(exe, args, env, True) def test_bc_utf8_1(exe, args, env): return test_utf8_1(exe, args, env, True) def test_bc_utf8_2(exe, args, env): return test_utf8_2(exe, args, env, True) def test_bc_utf8_3(exe, args, env): return test_utf8_3(exe, args, env, True) def test_bc_utf8_4(exe, args, env): return test_utf8_4(exe, args, env, True) # Basic bc test. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc1(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) write_str(child, "1") check_line(child, "1") write_str(child, "1") check_line(child, "1") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # SIGINT with no history. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc2(exe, args, env): env["TERM"] = "dumb" child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.sendline("1") check_line(child, "1", history=False) time.sleep(1) child.sendintr() child.sendline("quit") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Left and right arrows. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc3(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x1b[D\x1b[D\x1b[C\x1b[C") child.send("\n") child.expect(prompt) child.send("12\x1b[D3\x1b[C4\x1bOD5\x1bOC6") child.send("\n") check_line(child, "132546") child.send("12\x023\x064") child.send("\n") check_line(child, "1324") child.send("12\x1b[H3\x1bOH\x01\x1b[H45\x1bOF6\x05\x1b[F7\x1bOH8") child.send("\n") check_line(child, "84531267") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Up and down arrows. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc4(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x1b[A\x1bOA\x1b[B\x1bOB") child.send("\n") child.expect(prompt) write_str(child, "15") check_line(child, "15") write_str(child, "2^16") check_line(child, "65536") child.send("\x1b[A\x1bOA") child.send("\n") check_line(child, "15") child.send("\x1b[A\x1bOA\x1b[A\x1b[B") check_line(child, "65536") child.send("\x1b[A\x1bOA\x0e\x1b[A\x1b[A\x1b[A\x1b[B\x10\x1b[B\x1b[B\x1bOB\x1b[B\x1bOA") child.send("\n") check_line(child, "65536") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Clear screen. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc5(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x0c") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Printed material without a newline. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc6(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("print \"Enter number: \"") child.send("\n") child.expect("Enter number: ") child.send("4\x1b[A\x1b[A") child.send("\n") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Word start and word end. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc7(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x1bb\x1bb\x1bf\x1bf") child.send("\n") child.expect(prompt) child.send("\x1b[0~\x1b[3a") child.send("\n") child.expect(prompt) child.send("\x1b[0;4\x1b[0A") child.send("\n") child.expect(prompt) child.send(" ") child.send("\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb") child.send("\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf") child.send("\n") child.expect(prompt) write_str(child, "12 + 34 + 56 + 78 + 90") check_line(child, "270") child.send("\x1b[A") child.send("\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb\x1bb") child.send("\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf\x1bf") check_line(child, "270") child.send("\x1b[A") child.send("\x1bh\x1bh\x1bf + 14 ") child.send("\n") check_line(child, "284") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Backspace. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc8(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("12\x1b[D3\x1b[C4\x08\x7f") child.send("\n") check_line(child, "13") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Backspace and delete words. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc9(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x1b[0;5D\x1b[0;5D\x1b[0;5D\x1b[0;5C\x1b[0;5D\x1bd\x1b[3~\x1b[d\x1b[d\x1b[d\x1b[d\x7f\x7f\x7f") child.send("\n") child.expect(prompt) write_str(child, "12 + 34 + 56 + 78 + 90") check_line(child, "270") child.send("\x1b[A") child.send("\x1b[0;5D\x1b[0;5D\x1b[0;5D\x1b[0;5C\x1b[0;5D\x1bd\x1b[3~\x1b[d\x1b[d\x1b[d\x1b[d\x7f\x7f\x7f") child.send("\n") check_line(child, "102") child.send("\x1b[A") child.send("\x17\x17") child.send("\n") check_line(child, "46") child.send("\x17\x17") child.send("\n") child.expect(prompt) write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Backspace and delete words 2. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc10(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x1b[3~\x1b[3~") child.send("\n") child.expect(prompt) child.send(" \x1b[3~\x1b[3~") child.send("\n") child.expect(prompt) write_str(child, "12 + 34 + 56 + 78 + 90") check_line(child, "270") child.send("\x1b[A\x1b[A\x1b[A\x1b[B\x1b[B\x1b[B\x1b[A") child.send("\n") check_line(child, "270") child.send("\x1b[A\x1b[0;5D\x1b[0;5D\x0b") child.send("\n") check_line(child, "180") child.send("\x1b[A\x1521") check_line(child, "21") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Swap. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc11(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("\x1b[A\x02\x14") child.send("\n") child.expect(prompt) write_str(child, "12 + 34 + 56 + 78") check_line(child, "180") child.send("\x1b[A\x02\x14") check_line(child, "189") write_str(child, "quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Non-fatal error. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_bc12(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: bc_banner(child) child.send("12 +") child.send("\n") time.sleep(1) if not child.isalive(): print("child exited early") print(str(child)) print(str(child.buffer)) sys.exit(1) child.send("quit") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child def test_dc_utf8_0(exe, args, env): return test_utf8_0(exe, args, env, False) def test_dc_utf8_1(exe, args, env): return test_utf8_1(exe, args, env, False) def test_dc_utf8_2(exe, args, env): return test_utf8_2(exe, args, env, False) def test_dc_utf8_3(exe, args, env): return test_utf8_3(exe, args, env, False) def test_dc_utf8_4(exe, args, env): return test_utf8_4(exe, args, env, False) # Basic dc test. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_dc1(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: write_str(child, "1pR") check_line(child, "1") write_str(child, "1pR") check_line(child, "1") write_str(child, "q") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # SIGINT with quit. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_dc2(exe, args, env): env["TERM"] = "dumb" child = pexpect.spawn(exe, args=args, env=env) try: child.sendline("1pR") check_line(child, "1", history=False) time.sleep(1) child.sendintr() child.sendline("q") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # Execute string. # @param exe The executable. # @param args The arguments to pass to the executable. # @param env The environment. def test_dc3(exe, args, env): child = pexpect.spawn(exe, args=args, env=env) try: write_str(child, "[1 15+pR]x") check_line(child, "16") write_str(child, "1pR") check_line(child, "1") write_str(child, "q") child.send("\n") child.wait() except pexpect.TIMEOUT: print("timed out") print(str(child)) sys.exit(2) except pexpect.EOF: print("EOF") print(str(child)) print(str(child.buffer)) print(str(child.before)) sys.exit(2) return child # The array of bc tests. bc_tests = [ test_bc_utf8_0, test_bc_utf8_1, test_bc_utf8_2, test_bc_utf8_3, test_bc_utf8_4, test_sigint_sigquit, test_eof, test_sigint, test_sigtstp, test_sigstop, test_bc1, test_bc2, test_bc3, test_bc4, test_bc5, test_bc6, test_bc7, test_bc8, test_bc9, test_bc10, test_bc11, test_bc12, ] # The array of dc tests. dc_tests = [ test_dc_utf8_0, test_dc_utf8_1, test_dc_utf8_2, test_dc_utf8_3, test_sigint_sigquit, test_eof, test_sigint, test_dc1, test_dc2, test_dc3, ] # Print the usage and exit with an error. def usage(): print("usage: {} [-t] dir [-a] test_idx [exe options...]".format(script)) print(" The valid values for dir are: 'bc' and 'dc'.") print(" The max test_idx for bc is {}.".format(len(bc_tests) - 1)) print(" The max test_idx for dc is {}.".format(len(dc_tests) - 1)) print(" If -a is given, the number of test for dir is printed.") print(" No tests are run.") sys.exit(1) # Must run this script alone. if __name__ != "__main__": usage() if len(sys.argv) < 2: usage() idx = 1 exedir = sys.argv[idx] idx += 1 if exedir == "-t": do_test = True exedir = sys.argv[idx] idx += 1 else: do_test = False test_idx = sys.argv[idx] idx += 1 if test_idx == "-a": if exedir == "bc": l = len(bc_tests) else: l = len(dc_tests) print("{}".format(l)) sys.exit(0) test_idx = int(test_idx) # Set a default executable unless we have one. if len(sys.argv) >= idx + 1: exe = sys.argv[idx] else: exe = testdir + "/../bin/" + exedir exebase = os.path.basename(exe) # Use the correct options. if exebase == "bc": halt = "halt\n" options = "-lq" test_array = bc_tests else: halt = "q\n" options = "-x" test_array = dc_tests # More command-line processing. if len(sys.argv) > idx + 1: exe = [ exe, sys.argv[idx + 1:], options ] else: exe = [ exe, options ] # This is the environment necessary for most tests. env = { "BC_BANNER": "1", "BC_PROMPT": "1", "DC_PROMPT": "1", "BC_TTY_MODE": "1", "DC_TTY_MODE": "1", "BC_SIGINT_RESET": "1", "DC_SIGINT_RESET": "1", } # Make sure to include the outside environment. env.update(os.environ) env.pop("BC_ENV_ARGS", None) env.pop("BC_LINE_LENGTH", None) env.pop("DC_ENV_ARGS", None) env.pop("DC_LINE_LENGTH", None) # Run the correct test. child = test_array[test_idx](exe[0], exe[1:], env) child.close() exit = child.exitstatus if exit != 0: print("child failed; expected exit code 0, got {}".format(exit)) print(str(child)) sys.exit(1) diff --git a/tests/other.sh b/tests/other.sh index 0a856f4a6e22..4a26582457e3 100755 --- a/tests/other.sh +++ b/tests/other.sh @@ -1,371 +1,373 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # set -e script="$0" testdir=$(dirname "$script") . "$testdir/../scripts/functions.sh" +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} + # Command-line processing. if [ "$#" -ge 2 ]; then d="$1" shift extra_math="$1" shift else err_exit "usage: $script dir extra_math [exec args...]" 1 fi if [ "$#" -lt 1 ]; then exe="$testdir/../bin/$d" else exe="$1" shift fi if [ "$d" = "bc" ]; then halt="quit" else halt="q" fi # For tests later. num=100000000000000000000000000000000000000000000000000000000000000000000000000000 numres="$num" num70="10000000000000000000000000000000000000000000000000000000000000000000\\ 0000000000" # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then halt="halt" opt="x" lopt="extended-register" line_var="BC_LINE_LENGTH" else halt="q" opt="l" lopt="mathlib" line_var="DC_LINE_LENGTH" num="$num pR" fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH set +e printf '\nRunning %s quit test...' "$d" printf '%s\n' "$halt" | "$exe" "$@" > /dev/null 2>&1 checktest_retcode "$d" "$?" "quit" # bc has two halt or quit commands, so test the second as well. if [ "$d" = bc ]; then printf '%s\n' "quit" | "$exe" "$@" > /dev/null 2>&1 checktest_retcode "$d" "$?" quit two=$("$exe" "$@" -e 1+1 -e quit) checktest_retcode "$d" "$?" quit if [ "$two" != "2" ]; then err_exit "$d failed test quit" 1 fi fi printf 'pass\n' base=$(basename "$exe") printf 'Running %s environment var tests...' "$d" if [ "$d" = "bc" ]; then export BC_ENV_ARGS=" '-l' '' -q" printf 's(.02893)\n' | "$exe" "$@" > /dev/null checktest_retcode "$d" "$?" "environment var" "$exe" "$@" -e 4 > /dev/null err="$?" checktest_retcode "$d" "$?" "environment var" printf 'pass\n' printf 'Running keyword redefinition test...' unset BC_ENV_ARGS - redefine_res="$testdir/bc_outputs/redefine.txt" - redefine_out="$testdir/bc_outputs/redefine_results.txt" + redefine_res="$outputdir/bc_outputs/redefine.txt" + redefine_out="$outputdir/bc_outputs/redefine_results.txt" outdir=$(dirname "$easter_out") if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi printf '5\n0\n' > "$redefine_res" "$exe" "$@" --redefine=print -e 'define print(x) { x }' -e 'print(5)' > "$redefine_out" checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out" "$exe" "$@" -r "abs" -r "else" -e 'abs = 5;else = 0' -e 'abs;else' > "$redefine_out" checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out" if [ "$extra_math" -ne 0 ]; then "$exe" "$@" -lr abs -e "perm(5, 1)" -e "0" > "$redefine_out" checktest "$d" "$err" "keyword not redefined in builtin library" "$redefine_res" "$redefine_out" fi "$exe" "$@" -r "break" -e 'define break(x) { x }' 2> "$redefine_out" err="$?" checkerrtest "$d" "$err" "keyword redefinition error" "$redefine_out" "$d" "$exe" "$@" -e 'define read(x) { x }' 2> "$redefine_out" err="$?" checkerrtest "$d" "$err" "Keyword redefinition error without BC_REDEFINE_KEYWORDS" "$redefine_out" "$d" printf 'pass\n' else export DC_ENV_ARGS="'-x'" export DC_EXPR_EXIT="1" printf '4s stuff\n' | "$exe" "$@" > /dev/null checktest_retcode "$d" "$?" "environment var" "$exe" "$@" -e 4pR > /dev/null checktest_retcode "$d" "$?" "environment var" printf 'pass\n' set +e # dc has an extra test for a case that someone found running this easter.dc # script. It went into an infinite loop, so we want to check that we did not # regress. printf 'three\n' | cut -c1-3 > /dev/null err=$? if [ "$err" -eq 0 ]; then printf 'Running dc Easter script...' - easter_res="$testdir/dc_outputs/easter.txt" - easter_out="$testdir/dc_outputs/easter_results.txt" + easter_res="$outputdir/dc_outputs/easter.txt" + easter_out="$outputdir/dc_outputs/easter_results.txt" outdir=$(dirname "$easter_out") if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi printf '4 April 2021\n' > "$easter_res" "$testdir/dc/scripts/easter.sh" "$exe" 2021 "$@" | cut -c1-12 > "$easter_out" err="$?" checktest "$d" "$err" "Easter script" "$easter_res" "$easter_out" printf 'pass\n' fi fi -out1="$testdir/../.log_$d.txt" -out2="$testdir/../.log_${d}_test.txt" +out1="$outputdir/${d}_outputs/${d}_other.txt" +out2="$outputdir/${d}_outputs/${d}_other_test.txt" printf 'Running %s line length tests...' "$d" printf '%s\n' "$numres" > "$out1" export "$line_var"=80 printf '%s\n' "$num" | "$exe" "$@" > "$out2" checktest "$d" "$?" "environment var" "$out1" "$out2" printf '%s\n' "$num70" > "$out1" export "$line_var"=2147483647 printf '%s\n' "$num" | "$exe" "$@" > "$out2" checktest "$d" "$?" "environment var" "$out1" "$out2" printf 'pass\n' printf 'Running %s arg tests...' "$d" f="$testdir/$d/add.txt" exprs=$(cat "$f") results=$(cat "$testdir/$d/add_results.txt") printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1" "$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2" checktest "$d" "$?" "arg" "$out1" "$out2" printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2" checktest "$d" "$?" "arg" "$out1" "$out2" if [ "$d" = "bc" ]; then printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1 fi printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null checktest_retcode "$d" "$?" "arg" printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null checktest_retcode "$d" "$?" "arg" printf '%s\n' "$halt" | "$exe" "$@" -R > /dev/null checktest_retcode "$d" "$?" "arg" printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null checktest_retcode "$d" "$?" "arg" printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null checktest_retcode "$d" "$?" "arg" "$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d" "$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d" "$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d" "$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d" "$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d" "$exe" "$@" -f > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d" "$exe" "$@" --file > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d" "$exe" "$@" --version=5 > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d" "$exe" "$@" -: > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "colon short option" "$out2" "$d" "$exe" "$@" --: > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "colon long option" "$out2" "$d" printf 'pass\n' printf 'Running %s directory test...' "$d" "$exe" "$@" "$testdir" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "directory" "$out2" "$d" printf 'pass\n' printf 'Running %s binary file test...' "$d" bin="/bin/sh" "$exe" "$@" "$bin" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "binary file" "$out2" "$d" printf 'pass\n' printf 'Running %s binary stdin test...' "$d" cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2" err="$?" checkerrtest "$d" "$err" "binary stdin" "$out2" "$d" printf 'pass\n' if [ "$d" = "bc" ]; then printf 'Running %s limits tests...' "$d" printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1 checktest_retcode "$d" "$?" "limits" if [ ! -s "$out2" ]; then err_exit "$d did not produce output on the limits test" 1 fi exec printf 'pass\n' fi diff --git a/tests/read.sh b/tests/read.sh index 1186a19c99bd..a1915eb271ac 100755 --- a/tests/read.sh +++ b/tests/read.sh @@ -1,138 +1,140 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # set -e script="$0" testdir=$(dirname "$script") . "$testdir/../scripts/functions.sh" +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} + # Command-line processing. if [ "$#" -lt 1 ]; then printf 'usage: %s dir [exe [args...]]\n' "$0" printf 'valid dirs are:\n' printf '\n' cat "$testdir/all.txt" printf '\n' exit 1 fi d="$1" shift if [ "$#" -gt 0 ]; then exe="$1" shift else exe="$testdir/../bin/$d" fi name="$testdir/$d/read.txt" results="$testdir/$d/read_results.txt" errors="$testdir/$d/read_errors.txt" -out="$testdir/${d}_outputs/read_results.txt" +out="$outputdir/${d}_outputs/read_results.txt" outdir=$(dirname "$out") # Make sure the directory exists. if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi exebase=$(basename "$exe") # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then options="-lq" halt="halt" read_call="read()" read_expr="${read_call}\n5+5;" else options="-x" halt="q" read_call="?" read_expr="${read_call}" fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH printf 'Running %s read...' "$d" set +e # Run read() on every line. while read line; do printf '%s\n%s\n' "$read_call" "$line" | "$exe" "$@" "$options" > "$out" checktest "$d" "$?" 'read' "$results" "$out" done < "$name" printf 'pass\n' printf 'Running %s read errors...' "$d" # Run read on every line. while read line; do printf '%s\n%s\n' "$read_call" "$line" | "$exe" "$@" "$options" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "$line" "$out" "$exebase" done < "$errors" printf 'pass\n' printf 'Running %s empty read...' "$d" read_test=$(printf '%s\n' "$read_call") printf '%s\n' "$read_test" | "$exe" "$@" "$opts" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "$read_test" "$out" "$exebase" printf 'pass\n' printf 'Running %s read EOF...' "$d" read_test=$(printf '%s' "$read_call") printf '%s' "$read_test" | "$exe" "$@" "$opts" 2> "$out" > /dev/null err="$?" checkerrtest "$d" "$err" "$read_test" "$out" "$exebase" exec printf 'pass\n' diff --git a/tests/script.sh b/tests/script.sh index f8fdd67ee137..162437af8f22 100755 --- a/tests/script.sh +++ b/tests/script.sh @@ -1,183 +1,185 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # set -e script="$0" testdir=$(dirname "${script}") . "$testdir/../scripts/functions.sh" +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} + # Command-line processing. if [ "$#" -lt 2 ]; then printf 'usage: %s dir script [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script" exit 1 fi d="$1" shift f="$1" shift if [ "$#" -gt 0 ]; then run_extra_tests="$1" shift else run_extra_tests=1 fi if [ "$#" -gt 0 ]; then run_stack_tests="$1" shift else run_stack_tests=1 fi if [ "$#" -gt 0 ]; then generate="$1" shift else generate=1 fi if [ "$#" -gt 0 ]; then time_tests="$1" shift else time_tests=0 fi if [ "$#" -gt 0 ]; then exe="$1" shift else exe="$testdir/../bin/$d" fi # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then if [ "$run_stack_tests" -ne 0 ]; then options="-lgq" else options="-lq" fi halt="halt" else options="-x" halt="q" fi scriptdir="$testdir/$d/scripts" name="${f%.*}" # We specifically want to skip this because it is handled specially. if [ "$f" = "timeconst.bc" ]; then exit 0 fi # Skip the tests that require extra math if we don't have it. if [ "$run_extra_tests" -eq 0 ]; then if [ "$f" = "rand.bc" ]; then printf 'Skipping %s script: %s\n' "$d" "$f" exit 0 fi fi # Skip the tests that require global stacks flag if we are not allowed to run # them. if [ "$run_stack_tests" -eq 0 ]; then if [ "$f" = "globals.bc" ] || [ "$f" = "references.bc" ] || [ "$f" = "rand.bc" ]; then printf 'Skipping %s script: %s\n' "$d" "$f" exit 0 fi fi -out="$testdir/${d}_outputs/${name}_script_results.txt" +out="$outputdir/${d}_outputs/${name}_script_results.txt" outdir=$(dirname "$out") # Make sure the directory exists. if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH s="$scriptdir/$f" orig="$testdir/$name.txt" results="$scriptdir/$name.txt" if [ -f "$orig" ]; then res="$orig" elif [ -f "$results" ]; then res="$results" elif [ "$generate" -eq 0 ]; then printf 'Skipping %s script %s\n' "$d" "$f" exit 0 else # This sed, and the script, are to remove an incompatibility with GNU bc, # where GNU bc is wrong. See the development manual # (manuals/development.md#script-tests) for more information. printf 'Generating %s results...' "$f" printf '%s\n' "$halt" | "$d" "$s" | sed -n -f "$testdir/script.sed" > "$results" printf 'done\n' res="$results" fi set +e printf 'Running %s script %s...' "$d" "$f" # Yes this is poor timing, but it works. if [ "$time_tests" -ne 0 ]; then printf '\n' printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$s" > "$out" err="$?" printf '\n' else printf '%s\n' "$halt" | "$exe" "$@" $options "$s" > "$out" err="$?" fi checktest "$d" "$err" "script $f" "$res" "$out" rm -f "$out" exec printf 'pass\n' diff --git a/tests/stdin.sh b/tests/stdin.sh index 56bd1aae227b..c9e02253c30a 100755 --- a/tests/stdin.sh +++ b/tests/stdin.sh @@ -1,101 +1,103 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # set -e script="$0" testdir=$(dirname "$script") . "$testdir/../scripts/functions.sh" +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} + # Command-line processing. if [ "$#" -lt 1 ]; then printf 'usage: %s dir [exe [args...]]\n' "$0" printf 'valid dirs are:\n' printf '\n' cat "$testdir/all.txt" printf '\n' exit 1 fi d="$1" shift if [ "$#" -gt 0 ]; then exe="$1" shift else exe="$testdir/../bin/$d" fi -out="$testdir/${d}_outputs/stdin_results.txt" +out="$outputdir/${d}_outputs/stdin_results.txt" outdir=$(dirname "$out") # Make sure the directory exists. if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then options="-lq" else options="-x" fi rm -f "$out" # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH set +e printf 'Running %s stdin tests...' "$d" # Run the file through stdin. cat "$testdir/$d/stdin.txt" | "$exe" "$@" "$options" > "$out" 2> /dev/null checktest "$d" "$?" "stdin" "$testdir/$d/stdin_results.txt" "$out" # bc has some more tests; run those. if [ "$d" = "bc" ]; then cat "$testdir/$d/stdin1.txt" | "$exe" "$@" "$options" > "$out" 2> /dev/null checktest "$d" "$?" "stdin" "$testdir/$d/stdin1_results.txt" "$out" cat "$testdir/$d/stdin2.txt" | "$exe" "$@" "$options" > "$out" 2> /dev/null checktest "$d" "$?" "stdin" "$testdir/$d/stdin2_results.txt" "$out" fi rm -f "$out" exec printf 'pass\n' diff --git a/tests/test.sh b/tests/test.sh index ec7f6ba920c9..9d557a715dc0 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,149 +1,151 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2021 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. # set -e script="$0" testdir=$(dirname "$script") . "$testdir/../scripts/functions.sh" +outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} + # Command-line processing. if [ "$#" -lt 2 ]; then printf 'usage: %s dir test [generate_tests] [time_tests] [exe [args...]]\n' "$0" printf 'valid dirs are:\n' printf '\n' cat "$testdir/all.txt" printf '\n' exit 1 fi d="$1" shift t="$1" name="$testdir/$d/$t.txt" results="$testdir/$d/${t}_results.txt" shift if [ "$#" -gt 0 ]; then generate_tests="$1" shift else generate_tests=1 fi if [ "$#" -gt 0 ]; then time_tests="$1" shift else time_tests=0 fi if [ "$#" -gt 0 ]; then exe="$1" shift else exe="$testdir/../bin/$d" fi -out="$testdir/${d}_outputs/${t}_results.txt" +out="$outputdir/${d}_outputs/${t}_results.txt" outdir=$(dirname "$out") # Make sure the directory exists. if [ ! -d "$outdir" ]; then mkdir -p "$outdir" fi # I use these, so unset them to make the tests work. unset BC_ENV_ARGS unset BC_LINE_LENGTH unset DC_ENV_ARGS unset DC_LINE_LENGTH # Set stuff for the correct calculator. if [ "$d" = "bc" ]; then options="-lq" var="BC_LINE_LENGTH" halt="halt" else options="" var="DC_LINE_LENGTH" halt="q" fi # If the test does not exist... if [ ! -f "$name" ]; then # Skip if we can't generate. if [ "$generate_tests" -eq 0 ]; then printf 'Skipping %s %s test\n' "$d" "$t" exit 0 fi # Generate. printf 'Generating %s %s...' "$d" "$t" "$d" "$testdir/$d/scripts/$t.$d" > "$name" printf 'done\n' fi # If the results do not exist, generate.. if [ ! -f "$results" ]; then printf 'Generating %s %s results...' "$d" "$t" printf '%s\n' "$halt" | "$d" $options "$name" > "$results" printf 'done\n' fi # We set this here because GNU dc does not have it. if [ "$d" = "dc" ]; then options="-x" fi export $var=string set +e printf 'Running %s %s...' "$d" "$t" if [ "$time_tests" -ne 0 ]; then printf '\n' printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$name" > "$out" err="$?" printf '\n' else printf '%s\n' "$halt" | "$exe" "$@" $options "$name" > "$out" err="$?" fi checktest "$d" "$err" "$t" "$results" "$out" rm -f "$out" exec printf 'pass\n'