xref: /aosp_15_r20/external/bc/manuals/development.md (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker# Development
2*5a6e8488SAndroid Build Coastguard Worker
3*5a6e8488SAndroid Build Coastguard WorkerUpdated: 22 Dec 2023
4*5a6e8488SAndroid Build Coastguard Worker
5*5a6e8488SAndroid Build Coastguard WorkerThis document is meant for the day when I (Gavin D. Howard) get [hit by a
6*5a6e8488SAndroid Build Coastguard Workerbus][1]. In other words, it's meant to make the [bus factor][1] a non-issue.
7*5a6e8488SAndroid Build Coastguard Worker
8*5a6e8488SAndroid Build Coastguard WorkerThis document is supposed to contain all of the knowledge necessary to develop
9*5a6e8488SAndroid Build Coastguard Worker`bc` and `dc`.
10*5a6e8488SAndroid Build Coastguard Worker
11*5a6e8488SAndroid Build Coastguard WorkerIn addition, this document is meant to add to the [oral tradition of software
12*5a6e8488SAndroid Build Coastguard Workerengineering][118], as described by Bryan Cantrill.
13*5a6e8488SAndroid Build Coastguard Worker
14*5a6e8488SAndroid Build Coastguard WorkerThis document will reference other parts of the repository. That is so a lot of
15*5a6e8488SAndroid Build Coastguard Workerthe documentation can be closest to the part of the repo where it is actually
16*5a6e8488SAndroid Build Coastguard Workernecessary.
17*5a6e8488SAndroid Build Coastguard Worker
18*5a6e8488SAndroid Build Coastguard Worker## What Is It?
19*5a6e8488SAndroid Build Coastguard Worker
20*5a6e8488SAndroid Build Coastguard WorkerThis repository contains an implementation of both [POSIX `bc`][2] and [Unix
21*5a6e8488SAndroid Build Coastguard Worker`dc`][3].
22*5a6e8488SAndroid Build Coastguard Worker
23*5a6e8488SAndroid Build Coastguard WorkerPOSIX `bc` is a standard utility required for POSIX systems. `dc` is a
24*5a6e8488SAndroid Build Coastguard Workerhistorical utility that was included in early Unix and even predates both Unix
25*5a6e8488SAndroid Build Coastguard Workerand C. They both are arbitrary-precision command-line calculators with their own
26*5a6e8488SAndroid Build Coastguard Workerprogramming languages. `bc`'s language looks similar to C, with infix notation
27*5a6e8488SAndroid Build Coastguard Workerand including functions, while `dc` uses [Reverse Polish Notation][4] and allows
28*5a6e8488SAndroid Build Coastguard Workerthe user to execute strings as though they were functions.
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard WorkerIn addition, it is also possible to build the arbitrary-precision math as a
31*5a6e8488SAndroid Build Coastguard Workerlibrary, named [`bcl`][156].
32*5a6e8488SAndroid Build Coastguard Worker
33*5a6e8488SAndroid Build Coastguard Worker**Note**: for ease, I will refer to both programs as `bc` in this document.
34*5a6e8488SAndroid Build Coastguard WorkerHowever, if I say "just `bc`," I am referring to just `bc`, and if I say `dc`, I
35*5a6e8488SAndroid Build Coastguard Workeram referring to just `dc`.
36*5a6e8488SAndroid Build Coastguard Worker
37*5a6e8488SAndroid Build Coastguard Worker### History
38*5a6e8488SAndroid Build Coastguard Worker
39*5a6e8488SAndroid Build Coastguard WorkerThis project started in January 2018 when a certain individual on IRC, hearing
40*5a6e8488SAndroid Build Coastguard Workerthat I knew how to write parsers, asked me to write a `bc` parser for his math
41*5a6e8488SAndroid Build Coastguard Workerlibrary. I did so. I thought about writing my own math library, but he
42*5a6e8488SAndroid Build Coastguard Workerdisparaged my programming skills and made me think that I couldn't do it.
43*5a6e8488SAndroid Build Coastguard Worker
44*5a6e8488SAndroid Build Coastguard WorkerHowever, he took so long to do it that I eventually decided to give it a try and
45*5a6e8488SAndroid Build Coastguard Workerhad a working math portion in two weeks. It taught me that I should not listen
46*5a6e8488SAndroid Build Coastguard Workerto such people.
47*5a6e8488SAndroid Build Coastguard Worker
48*5a6e8488SAndroid Build Coastguard WorkerFrom that point, I decided to make it an extreme learning experience about how
49*5a6e8488SAndroid Build Coastguard Workerto write quality software.
50*5a6e8488SAndroid Build Coastguard Worker
51*5a6e8488SAndroid Build Coastguard WorkerThat individual's main goal had been to get his `bc` into [toybox][16], and I
52*5a6e8488SAndroid Build Coastguard Workermanaged to get my own `bc` in. I also got it in [busybox][17].
53*5a6e8488SAndroid Build Coastguard Worker
54*5a6e8488SAndroid Build Coastguard WorkerEventually, in late 2018, I also decided to try my hand at implementing
55*5a6e8488SAndroid Build Coastguard Worker[Karatsuba multiplication][18], an algorithm that that unnamed individual
56*5a6e8488SAndroid Build Coastguard Workerclaimed I could never implement. It took me a bit, but I did it.
57*5a6e8488SAndroid Build Coastguard Worker
58*5a6e8488SAndroid Build Coastguard WorkerThis project became a passion project for me, and I continued. In mid-2019,
59*5a6e8488SAndroid Build Coastguard WorkerStefan Eßer suggested I improve performance by putting more than 1 digit in each
60*5a6e8488SAndroid Build Coastguard Workersection of the numbers. After I showed immaturity because of some burnout, I
61*5a6e8488SAndroid Build Coastguard Workerimplemented his suggestion, and the results were incredible.
62*5a6e8488SAndroid Build Coastguard Worker
63*5a6e8488SAndroid Build Coastguard WorkerSince that time, I have gradually been improving the `bc` as I have learned more
64*5a6e8488SAndroid Build Coastguard Workerabout things like fuzzing, [`scan-build`][19], [valgrind][20],
65*5a6e8488SAndroid Build Coastguard Worker[AddressSanitizer][21] (and the other sanitizers), and many other things.
66*5a6e8488SAndroid Build Coastguard Worker
67*5a6e8488SAndroid Build Coastguard WorkerOne of my happiest moments was when my `bc` was made the default in FreeBSD.
68*5a6e8488SAndroid Build Coastguard WorkerAnother happiest moment was when I found out that my `bc` had shipped with macOS
69*5a6e8488SAndroid Build Coastguard WorkerVentura, without my knowledge.
70*5a6e8488SAndroid Build Coastguard Worker
71*5a6e8488SAndroid Build Coastguard WorkerBut since I believe in [finishing the software I write][22], I have done less
72*5a6e8488SAndroid Build Coastguard Workerwork on `bc` over time, though there are still times when I put a lot of effort
73*5a6e8488SAndroid Build Coastguard Workerin, such as now (17 June 2021), when I am attempting to convince OpenBSD to use
74*5a6e8488SAndroid Build Coastguard Workermy `bc`.
75*5a6e8488SAndroid Build Coastguard Worker
76*5a6e8488SAndroid Build Coastguard WorkerAnd that is why I am writing this document: someday, someone else is going to
77*5a6e8488SAndroid Build Coastguard Workerwant to change my code, and this document is my attempt to make it as simple as
78*5a6e8488SAndroid Build Coastguard Workerpossible.
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Worker### Values
81*5a6e8488SAndroid Build Coastguard Worker
82*5a6e8488SAndroid Build Coastguard Worker[According to Bryan Cantrill][10], all software has values. I think he's
83*5a6e8488SAndroid Build Coastguard Workercorrect, though I [added one value for programming languages in particular][11].
84*5a6e8488SAndroid Build Coastguard Worker
85*5a6e8488SAndroid Build Coastguard WorkerHowever, for `bc`, his original list will do:
86*5a6e8488SAndroid Build Coastguard Worker
87*5a6e8488SAndroid Build Coastguard Worker* Approachability
88*5a6e8488SAndroid Build Coastguard Worker* Availability
89*5a6e8488SAndroid Build Coastguard Worker* Compatibility
90*5a6e8488SAndroid Build Coastguard Worker* Composability
91*5a6e8488SAndroid Build Coastguard Worker* Debuggability
92*5a6e8488SAndroid Build Coastguard Worker* Expressiveness
93*5a6e8488SAndroid Build Coastguard Worker* Extensibility
94*5a6e8488SAndroid Build Coastguard Worker* Interoperability
95*5a6e8488SAndroid Build Coastguard Worker* Integrity
96*5a6e8488SAndroid Build Coastguard Worker* Maintainability
97*5a6e8488SAndroid Build Coastguard Worker* Measurability
98*5a6e8488SAndroid Build Coastguard Worker* Operability
99*5a6e8488SAndroid Build Coastguard Worker* Performance
100*5a6e8488SAndroid Build Coastguard Worker* Portability
101*5a6e8488SAndroid Build Coastguard Worker* Resiliency
102*5a6e8488SAndroid Build Coastguard Worker* Rigor
103*5a6e8488SAndroid Build Coastguard Worker* Robustness
104*5a6e8488SAndroid Build Coastguard Worker* Safety
105*5a6e8488SAndroid Build Coastguard Worker* Security
106*5a6e8488SAndroid Build Coastguard Worker* Simplicity
107*5a6e8488SAndroid Build Coastguard Worker* Stability
108*5a6e8488SAndroid Build Coastguard Worker* Thoroughness
109*5a6e8488SAndroid Build Coastguard Worker* Transparency
110*5a6e8488SAndroid Build Coastguard Worker* Velocity
111*5a6e8488SAndroid Build Coastguard Worker
112*5a6e8488SAndroid Build Coastguard WorkerThere are several values that don't apply. The reason they don't apply is
113*5a6e8488SAndroid Build Coastguard Workerbecause `bc` and `dc` are existing utilities; this is just another
114*5a6e8488SAndroid Build Coastguard Workerreimplementation. The designs of `bc` and `dc` are set in stone; there is
115*5a6e8488SAndroid Build Coastguard Workernothing we can do to change them, so let's get rid of those values that would
116*5a6e8488SAndroid Build Coastguard Workerapply to their design:
117*5a6e8488SAndroid Build Coastguard Worker
118*5a6e8488SAndroid Build Coastguard Worker* Compatibility
119*5a6e8488SAndroid Build Coastguard Worker* Integrity
120*5a6e8488SAndroid Build Coastguard Worker* Maintainability
121*5a6e8488SAndroid Build Coastguard Worker* Measurability
122*5a6e8488SAndroid Build Coastguard Worker* Performance
123*5a6e8488SAndroid Build Coastguard Worker* Portability
124*5a6e8488SAndroid Build Coastguard Worker* Resiliency
125*5a6e8488SAndroid Build Coastguard Worker* Rigor
126*5a6e8488SAndroid Build Coastguard Worker* Robustness
127*5a6e8488SAndroid Build Coastguard Worker* Safety
128*5a6e8488SAndroid Build Coastguard Worker* Security
129*5a6e8488SAndroid Build Coastguard Worker* Simplicity
130*5a6e8488SAndroid Build Coastguard Worker* Stability
131*5a6e8488SAndroid Build Coastguard Worker* Thoroughness
132*5a6e8488SAndroid Build Coastguard Worker* Transparency
133*5a6e8488SAndroid Build Coastguard Worker
134*5a6e8488SAndroid Build Coastguard WorkerFurthermore, some of the remaining ones don't matter to me, so let me get rid of
135*5a6e8488SAndroid Build Coastguard Workerthose and order the rest according to my *actual* values for this project:
136*5a6e8488SAndroid Build Coastguard Worker
137*5a6e8488SAndroid Build Coastguard Worker* Robustness
138*5a6e8488SAndroid Build Coastguard Worker* Stability
139*5a6e8488SAndroid Build Coastguard Worker* Portability
140*5a6e8488SAndroid Build Coastguard Worker* Compatibility
141*5a6e8488SAndroid Build Coastguard Worker* Performance
142*5a6e8488SAndroid Build Coastguard Worker* Security
143*5a6e8488SAndroid Build Coastguard Worker* Simplicity
144*5a6e8488SAndroid Build Coastguard Worker
145*5a6e8488SAndroid Build Coastguard WorkerFirst is **robustness**. This `bc` and `dc` should be robust, accepting any
146*5a6e8488SAndroid Build Coastguard Workerinput, never crashing, and instead, returning an error.
147*5a6e8488SAndroid Build Coastguard Worker
148*5a6e8488SAndroid Build Coastguard WorkerClosely related to that is **stability**. The execution of `bc` and `dc` should
149*5a6e8488SAndroid Build Coastguard Workerbe deterministic and never change for the same inputs, including the
150*5a6e8488SAndroid Build Coastguard Workerpseudo-random number generator (for the same seed).
151*5a6e8488SAndroid Build Coastguard Worker
152*5a6e8488SAndroid Build Coastguard WorkerThird is **portability**. These programs should run everywhere that POSIX
153*5a6e8488SAndroid Build Coastguard Workerexists, as well as Windows. This means that just about every person on the
154*5a6e8488SAndroid Build Coastguard Workerplanet will have access to these programs.
155*5a6e8488SAndroid Build Coastguard Worker
156*5a6e8488SAndroid Build Coastguard WorkerNext is **compatibility**. These programs should, as much as possible, be
157*5a6e8488SAndroid Build Coastguard Workercompatible with other existing implementations and standards.
158*5a6e8488SAndroid Build Coastguard Worker
159*5a6e8488SAndroid Build Coastguard WorkerThen we come to **performance**. A calculator is only usable if it's fast, so
160*5a6e8488SAndroid Build Coastguard Workerthese programs should run as fast as possible.
161*5a6e8488SAndroid Build Coastguard Worker
162*5a6e8488SAndroid Build Coastguard WorkerAfter that is **security**. These programs should *never* be the reason a user's
163*5a6e8488SAndroid Build Coastguard Workercomputer is compromised.
164*5a6e8488SAndroid Build Coastguard Worker
165*5a6e8488SAndroid Build Coastguard WorkerAnd finally, **simplicity**. Where possible, the code should be simple, while
166*5a6e8488SAndroid Build Coastguard Workerdeferring to the above values.
167*5a6e8488SAndroid Build Coastguard Worker
168*5a6e8488SAndroid Build Coastguard WorkerKeep these values in mind for the rest of this document, and for exploring any
169*5a6e8488SAndroid Build Coastguard Workerother part of this repo.
170*5a6e8488SAndroid Build Coastguard Worker
171*5a6e8488SAndroid Build Coastguard Worker#### Portability
172*5a6e8488SAndroid Build Coastguard Worker
173*5a6e8488SAndroid Build Coastguard WorkerBut before I go on, I want to talk about portability in particular.
174*5a6e8488SAndroid Build Coastguard Worker
175*5a6e8488SAndroid Build Coastguard WorkerMost of these principles just require good attention and care, but portability
176*5a6e8488SAndroid Build Coastguard Workeris different. Sometimes, it requires pulling in code from other places and
177*5a6e8488SAndroid Build Coastguard Workeradapting it. In other words, sometimes I need to duplicate and adapt code.
178*5a6e8488SAndroid Build Coastguard Worker
179*5a6e8488SAndroid Build Coastguard WorkerThis happened in a few cases:
180*5a6e8488SAndroid Build Coastguard Worker
181*5a6e8488SAndroid Build Coastguard Worker* Option parsing (see [`include/opt.h`][35]).
182*5a6e8488SAndroid Build Coastguard Worker* History (see [`include/history.h`][36]).
183*5a6e8488SAndroid Build Coastguard Worker* Pseudo-Random Number Generator (see [`include/rand.h`][37]).
184*5a6e8488SAndroid Build Coastguard Worker
185*5a6e8488SAndroid Build Coastguard WorkerThis was done because I decided to ensure that `bc`'s dependencies were
186*5a6e8488SAndroid Build Coastguard Workerbasically zero. In particular, either users have a normal install of Windows or
187*5a6e8488SAndroid Build Coastguard Workerthey have a POSIX system.
188*5a6e8488SAndroid Build Coastguard Worker
189*5a6e8488SAndroid Build Coastguard WorkerA POSIX system limited me to C99, `sh`, and zero external dependencies. That
190*5a6e8488SAndroid Build Coastguard Workerlast item is why I pull code into `bc`: if I pull it in, it's not an external
191*5a6e8488SAndroid Build Coastguard Workerdependency.
192*5a6e8488SAndroid Build Coastguard Worker
193*5a6e8488SAndroid Build Coastguard WorkerThat's why `bc` has duplicated code. Remove it, and you risk `bc` not being
194*5a6e8488SAndroid Build Coastguard Workerportable to some platforms.
195*5a6e8488SAndroid Build Coastguard Worker
196*5a6e8488SAndroid Build Coastguard Worker## Suggested Course
197*5a6e8488SAndroid Build Coastguard Worker
198*5a6e8488SAndroid Build Coastguard WorkerI do have a suggested course for programmers to follow when trying to understand
199*5a6e8488SAndroid Build Coastguard Workerthis codebase. The order is this:
200*5a6e8488SAndroid Build Coastguard Worker
201*5a6e8488SAndroid Build Coastguard Worker1.	`bc` Spec.
202*5a6e8488SAndroid Build Coastguard Worker2.	Manpages.
203*5a6e8488SAndroid Build Coastguard Worker3.	Test suite.
204*5a6e8488SAndroid Build Coastguard Worker4.	Understand the build.
205*5a6e8488SAndroid Build Coastguard Worker5.	Algorithms manual.
206*5a6e8488SAndroid Build Coastguard Worker6.	Code concepts.
207*5a6e8488SAndroid Build Coastguard Worker7.	Repo structure.
208*5a6e8488SAndroid Build Coastguard Worker8.	Headers.
209*5a6e8488SAndroid Build Coastguard Worker9.	Source code.
210*5a6e8488SAndroid Build Coastguard Worker
211*5a6e8488SAndroid Build Coastguard WorkerThis order roughly follows this order:
212*5a6e8488SAndroid Build Coastguard Worker
213*5a6e8488SAndroid Build Coastguard Worker1. High-level requirements
214*5a6e8488SAndroid Build Coastguard Worker2. Low-level requirements
215*5a6e8488SAndroid Build Coastguard Worker3. High-level implementation
216*5a6e8488SAndroid Build Coastguard Worker4. Low-level implementation
217*5a6e8488SAndroid Build Coastguard Worker
218*5a6e8488SAndroid Build Coastguard WorkerIn other words, first understand what the code is *supposed* to do, then
219*5a6e8488SAndroid Build Coastguard Workerunderstand the code itself.
220*5a6e8488SAndroid Build Coastguard Worker
221*5a6e8488SAndroid Build Coastguard Worker## Useful External Tools
222*5a6e8488SAndroid Build Coastguard Worker
223*5a6e8488SAndroid Build Coastguard WorkerI have a few tools external to `bc` that are useful:
224*5a6e8488SAndroid Build Coastguard Worker
225*5a6e8488SAndroid Build Coastguard Worker* A [Vim plugin with syntax files made specifically for my `bc` and `dc`][132].
226*5a6e8488SAndroid Build Coastguard Worker* A [repo of `bc` and `dc` scripts][133].
227*5a6e8488SAndroid Build Coastguard Worker* A set of `bash` aliases (see below).
228*5a6e8488SAndroid Build Coastguard Worker* A `.bcrc` file with items useful for my `bash` setup (see below).
229*5a6e8488SAndroid Build Coastguard Worker
230*5a6e8488SAndroid Build Coastguard WorkerMy `bash` aliases are these:
231*5a6e8488SAndroid Build Coastguard Worker
232*5a6e8488SAndroid Build Coastguard Worker```sh
233*5a6e8488SAndroid Build Coastguard Workeralias makej='make -j16'
234*5a6e8488SAndroid Build Coastguard Workeralias mcmake='make clean && make'
235*5a6e8488SAndroid Build Coastguard Workeralias mcmakej='make clean && make -j16'
236*5a6e8488SAndroid Build Coastguard Workeralias bcdebug='CPPFLAGS="-DBC_DEBUG_CODE=1" CFLAGS="-Weverything -Wno-padded \
237*5a6e8488SAndroid Build Coastguard Worker    -Wno-switch-enum -Wno-format-nonliteral -Wno-cast-align \
238*5a6e8488SAndroid Build Coastguard Worker    -Wno-unreachable-code-return -Wno-missing-noreturn \
239*5a6e8488SAndroid Build Coastguard Worker    -Wno-disabled-macro-expansion -Wno-unreachable-code -Wall -Wextra \
240*5a6e8488SAndroid Build Coastguard Worker    -pedantic -std=c99" ./configure.sh'
241*5a6e8488SAndroid Build Coastguard Workeralias bcconfig='CFLAGS="-Weverything -Wno-padded -Wno-switch-enum \
242*5a6e8488SAndroid Build Coastguard Worker    -Wno-format-nonliteral -Wno-cast-align -Wno-unreachable-code-return \
243*5a6e8488SAndroid Build Coastguard Worker    -Wno-missing-noreturn -Wno-disabled-macro-expansion -Wno-unreachable-code \
244*5a6e8488SAndroid Build Coastguard Worker    -Wall -Wextra -pedantic -std=c99" ./configure.sh'
245*5a6e8488SAndroid Build Coastguard Workeralias bcnoassert='CPPFLAGS="-DNDEBUG" CFLAGS="-Weverything -Wno-padded \
246*5a6e8488SAndroid Build Coastguard Worker    -Wno-switch-enum -Wno-format-nonliteral -Wno-cast-align \
247*5a6e8488SAndroid Build Coastguard Worker    -Wno-unreachable-code-return -Wno-missing-noreturn \
248*5a6e8488SAndroid Build Coastguard Worker    -Wno-disabled-macro-expansion -Wno-unreachable-code -Wall -Wextra \
249*5a6e8488SAndroid Build Coastguard Worker    -pedantic -std=c99" ./configure.sh'
250*5a6e8488SAndroid Build Coastguard Workeralias bcdebugnoassert='CPPFLAGS="-DNDEBUG -DBC_DEBUG_CODE=1" \
251*5a6e8488SAndroid Build Coastguard Worker    CFLAGS="-Weverything -Wno-padded -Wno-switch-enum -Wno-format-nonliteral \
252*5a6e8488SAndroid Build Coastguard Worker    -Wno-cast-align -Wno-unreachable-code-return -Wno-missing-noreturn \
253*5a6e8488SAndroid Build Coastguard Worker    -Wno-disabled-macro-expansion -Wno-unreachable-code -Wall -Wextra \
254*5a6e8488SAndroid Build Coastguard Worker    -pedantic -std=c99" ./configure.sh'
255*5a6e8488SAndroid Build Coastguard Workeralias bcunset='unset BC_LINE_LENGTH && unset BC_ENV_ARGS'
256*5a6e8488SAndroid Build Coastguard Worker```
257*5a6e8488SAndroid Build Coastguard Worker
258*5a6e8488SAndroid Build Coastguard Worker`makej` runs `make` with all of my cores.
259*5a6e8488SAndroid Build Coastguard Worker
260*5a6e8488SAndroid Build Coastguard Worker`mcmake` runs `make clean` before running `make`. It will take a target on the
261*5a6e8488SAndroid Build Coastguard Workercommand-line.
262*5a6e8488SAndroid Build Coastguard Worker
263*5a6e8488SAndroid Build Coastguard Worker`mcmakej` is a combination of `makej` and `mcmake`.
264*5a6e8488SAndroid Build Coastguard Worker
265*5a6e8488SAndroid Build Coastguard Worker`bcdebug` configures `bc` for a full debug build, including `BC_DEBUG_CODE` (see
266*5a6e8488SAndroid Build Coastguard Worker[Debugging][134] below).
267*5a6e8488SAndroid Build Coastguard Worker
268*5a6e8488SAndroid Build Coastguard Worker`bcconfig` configures `bc` with Clang (Clang is my personal default compiler)
269*5a6e8488SAndroid Build Coastguard Workerusing full warnings, with a few really loud and useless warnings turned off.
270*5a6e8488SAndroid Build Coastguard Worker
271*5a6e8488SAndroid Build Coastguard Worker`bcnoassert` configures `bc` to not have asserts built in.
272*5a6e8488SAndroid Build Coastguard Worker
273*5a6e8488SAndroid Build Coastguard Worker`bcdebugnoassert` is like `bcnoassert`, except it also configures `bc` for debug
274*5a6e8488SAndroid Build Coastguard Workermode.
275*5a6e8488SAndroid Build Coastguard Worker
276*5a6e8488SAndroid Build Coastguard Worker`bcunset` unsets my personal `bc` environment variables, which are set to:
277*5a6e8488SAndroid Build Coastguard Worker
278*5a6e8488SAndroid Build Coastguard Worker```sh
279*5a6e8488SAndroid Build Coastguard Workerexport BC_ENV_ARGS="-l $HOME/.bcrc"
280*5a6e8488SAndroid Build Coastguard Workerexport BC_LINE_LENGTH="74"
281*5a6e8488SAndroid Build Coastguard Worker```
282*5a6e8488SAndroid Build Coastguard Worker
283*5a6e8488SAndroid Build Coastguard WorkerUnsetting these environment variables are necessary for running
284*5a6e8488SAndroid Build Coastguard Worker[`scripts/release.sh`][83] because otherwise, it will error when attempting to
285*5a6e8488SAndroid Build Coastguard Workerrun `bc -s` on my `$HOME/.bcrc`.
286*5a6e8488SAndroid Build Coastguard Worker
287*5a6e8488SAndroid Build Coastguard WorkerSpeaking of which, the contents of that file are:
288*5a6e8488SAndroid Build Coastguard Worker
289*5a6e8488SAndroid Build Coastguard Worker```bc
290*5a6e8488SAndroid Build Coastguard Workerdefine void print_time_unit(t){
291*5a6e8488SAndroid Build Coastguard Worker	if(t<10)print "0"
292*5a6e8488SAndroid Build Coastguard Worker	if(t<1&&t)print "0"
293*5a6e8488SAndroid Build Coastguard Worker	print t,":"
294*5a6e8488SAndroid Build Coastguard Worker}
295*5a6e8488SAndroid Build Coastguard Workerdefine void sec2time(t){
296*5a6e8488SAndroid Build Coastguard Worker	auto s,m,h,d,r
297*5a6e8488SAndroid Build Coastguard Worker	r=scale
298*5a6e8488SAndroid Build Coastguard Worker	scale=0
299*5a6e8488SAndroid Build Coastguard Worker	t=abs(t)
300*5a6e8488SAndroid Build Coastguard Worker	s=t%60
301*5a6e8488SAndroid Build Coastguard Worker	t-=s
302*5a6e8488SAndroid Build Coastguard Worker	m=t/60%60
303*5a6e8488SAndroid Build Coastguard Worker	t-=m
304*5a6e8488SAndroid Build Coastguard Worker	h=t/3600%24
305*5a6e8488SAndroid Build Coastguard Worker	t-=h
306*5a6e8488SAndroid Build Coastguard Worker	d=t/86400
307*5a6e8488SAndroid Build Coastguard Worker	if(d)print_time_unit(d)
308*5a6e8488SAndroid Build Coastguard Worker	if(h)print_time_unit(h)
309*5a6e8488SAndroid Build Coastguard Worker	print_time_unit(m)
310*5a6e8488SAndroid Build Coastguard Worker	if(s<10)print "0"
311*5a6e8488SAndroid Build Coastguard Worker	if(s<1&&s)print "0"
312*5a6e8488SAndroid Build Coastguard Worker	s
313*5a6e8488SAndroid Build Coastguard Worker	scale=r
314*5a6e8488SAndroid Build Coastguard Worker}
315*5a6e8488SAndroid Build Coastguard Workerdefine minutes(secs){
316*5a6e8488SAndroid Build Coastguard Worker	return secs/60;
317*5a6e8488SAndroid Build Coastguard Worker}
318*5a6e8488SAndroid Build Coastguard Workerdefine hours(secs){
319*5a6e8488SAndroid Build Coastguard Worker	return secs/3600;
320*5a6e8488SAndroid Build Coastguard Worker}
321*5a6e8488SAndroid Build Coastguard Workerdefine days(secs){
322*5a6e8488SAndroid Build Coastguard Worker	return secs/3600/24;
323*5a6e8488SAndroid Build Coastguard Worker}
324*5a6e8488SAndroid Build Coastguard Workerdefine years(secs){
325*5a6e8488SAndroid Build Coastguard Worker	return secs/3600/24/365.25;
326*5a6e8488SAndroid Build Coastguard Worker}
327*5a6e8488SAndroid Build Coastguard Workerdefine fbrand(b,p){
328*5a6e8488SAndroid Build Coastguard Worker	auto l,s,t
329*5a6e8488SAndroid Build Coastguard Worker	b=abs(b)$
330*5a6e8488SAndroid Build Coastguard Worker	if(b<2)b=2
331*5a6e8488SAndroid Build Coastguard Worker	s=scale
332*5a6e8488SAndroid Build Coastguard Worker	t=b^abs(p)$
333*5a6e8488SAndroid Build Coastguard Worker	l=ceil(l2(t),0)
334*5a6e8488SAndroid Build Coastguard Worker	if(l>scale)scale=l
335*5a6e8488SAndroid Build Coastguard Worker	t=irand(t)/t
336*5a6e8488SAndroid Build Coastguard Worker	scale=s
337*5a6e8488SAndroid Build Coastguard Worker	return t
338*5a6e8488SAndroid Build Coastguard Worker}
339*5a6e8488SAndroid Build Coastguard Workerdefine ifbrand(i,b,p){return irand(abs(i)$)+fbrand(b,p)}
340*5a6e8488SAndroid Build Coastguard Worker```
341*5a6e8488SAndroid Build Coastguard Worker
342*5a6e8488SAndroid Build Coastguard WorkerThis allows me to use `bc` as part of my `bash` prompt.
343*5a6e8488SAndroid Build Coastguard Worker
344*5a6e8488SAndroid Build Coastguard Worker## Code Style
345*5a6e8488SAndroid Build Coastguard Worker
346*5a6e8488SAndroid Build Coastguard WorkerThe code style for `bc` is...weird, and that comes from historical accident.
347*5a6e8488SAndroid Build Coastguard Worker
348*5a6e8488SAndroid Build Coastguard WorkerIn [History][23], I mentioned how I got my `bc` in [toybox][16]. Well, in order
349*5a6e8488SAndroid Build Coastguard Workerto do that, my `bc` originally had toybox style. Eventually, I changed to using
350*5a6e8488SAndroid Build Coastguard Workertabs, and assuming they were 4 spaces wide, but other than that, I basically
351*5a6e8488SAndroid Build Coastguard Workerkept the same style, with some exceptions that are more or less dependent on my
352*5a6e8488SAndroid Build Coastguard Workertaste.
353*5a6e8488SAndroid Build Coastguard Worker
354*5a6e8488SAndroid Build Coastguard WorkerHowever, I later managed to get [ClangFormat][24] to work, so I changed the
355*5a6e8488SAndroid Build Coastguard Workerstyle to that.
356*5a6e8488SAndroid Build Coastguard Worker
357*5a6e8488SAndroid Build Coastguard Worker### ClangFormat
358*5a6e8488SAndroid Build Coastguard Worker
359*5a6e8488SAndroid Build Coastguard WorkerThe style is now defined as whatever [ClangFormat][24] outputs using the
360*5a6e8488SAndroid Build Coastguard Workerexisting `.clang-format` file. More precisely, the style is whatever is output
361*5a6e8488SAndroid Build Coastguard Workerwhen the following command is run in the root directory:
362*5a6e8488SAndroid Build Coastguard Worker
363*5a6e8488SAndroid Build Coastguard Worker```
364*5a6e8488SAndroid Build Coastguard Worker./scripts/format.sh
365*5a6e8488SAndroid Build Coastguard Worker```
366*5a6e8488SAndroid Build Coastguard Worker
367*5a6e8488SAndroid Build Coastguard Worker### Historical Style
368*5a6e8488SAndroid Build Coastguard Worker
369*5a6e8488SAndroid Build Coastguard WorkerThe code style used to be:
370*5a6e8488SAndroid Build Coastguard Worker
371*5a6e8488SAndroid Build Coastguard Worker* Tabs are 4 spaces.
372*5a6e8488SAndroid Build Coastguard Worker* Tabs are used at the beginning of lines for indent.
373*5a6e8488SAndroid Build Coastguard Worker* Spaces are used for alignment.
374*5a6e8488SAndroid Build Coastguard Worker* Lines are limited to 80 characters, period.
375*5a6e8488SAndroid Build Coastguard Worker* Pointer asterisk (`*`) goes with the variable (on the right), not the type,
376*5a6e8488SAndroid Build Coastguard Worker  unless it is for a pointer type returned from a function.
377*5a6e8488SAndroid Build Coastguard Worker* The opening brace is put on the same line as the header for the function,
378*5a6e8488SAndroid Build Coastguard Worker  loop, or `if` statement.
379*5a6e8488SAndroid Build Coastguard Worker* Unless the header is more than one line, in which case the opening brace is
380*5a6e8488SAndroid Build Coastguard Worker  put on its own line.
381*5a6e8488SAndroid Build Coastguard Worker* If the opening brace is put on its own line, there is no blank line after it.
382*5a6e8488SAndroid Build Coastguard Worker* If the opening brace is *not* put on its own line, there *is* a blank line
383*5a6e8488SAndroid Build Coastguard Worker  after it, *unless* the block is only one or two lines long.
384*5a6e8488SAndroid Build Coastguard Worker* Code lines are grouped into what I call "paragraphs." Basically, lines that
385*5a6e8488SAndroid Build Coastguard Worker  seem like they should go together are grouped together. This one comes down
386*5a6e8488SAndroid Build Coastguard Worker  to judgment.
387*5a6e8488SAndroid Build Coastguard Worker* Bodies of `if` statements, `else` statements, and loops that are one line
388*5a6e8488SAndroid Build Coastguard Worker  long are put on the same line as the statement, unless the header is more than
389*5a6e8488SAndroid Build Coastguard Worker  one line long, and/or, the header and body cannot fit into 80 characters with
390*5a6e8488SAndroid Build Coastguard Worker  a space inbetween them.
391*5a6e8488SAndroid Build Coastguard Worker* If single-line bodies are on a separate line from their headers, and the
392*5a6e8488SAndroid Build Coastguard Worker  headers are only a single line, then no braces are used.
393*5a6e8488SAndroid Build Coastguard Worker* However, braces are *always* used if they contain another `if` statement or
394*5a6e8488SAndroid Build Coastguard Worker  loop.
395*5a6e8488SAndroid Build Coastguard Worker* Loops with empty bodies are ended with a semicolon.
396*5a6e8488SAndroid Build Coastguard Worker* Expressions that return a boolean value are surrounded by paretheses.
397*5a6e8488SAndroid Build Coastguard Worker* Macro backslashes are aligned as far to the left as possible.
398*5a6e8488SAndroid Build Coastguard Worker* Binary operators have spaces on both sides.
399*5a6e8488SAndroid Build Coastguard Worker* If a line with binary operators overflows 80 characters, a newline is inserted
400*5a6e8488SAndroid Build Coastguard Worker  *after* binary operators.
401*5a6e8488SAndroid Build Coastguard Worker* Function modifiers and return types are on the same line as the function name.
402*5a6e8488SAndroid Build Coastguard Worker* With one exception, `goto`'s are only used to jump to the end of a function
403*5a6e8488SAndroid Build Coastguard Worker  for cleanup.
404*5a6e8488SAndroid Build Coastguard Worker* All structs, enums, and unions are `typedef`'ed.
405*5a6e8488SAndroid Build Coastguard Worker* All constant data is in one file: [`src/data.c`][131], but the corresponding
406*5a6e8488SAndroid Build Coastguard Worker  `extern` declarations are in the appropriate header file.
407*5a6e8488SAndroid Build Coastguard Worker* All local variables are declared at the beginning of the scope where they
408*5a6e8488SAndroid Build Coastguard Worker  appear. They may be initialized at that point, if it does not invoke UB or
409*5a6e8488SAndroid Build Coastguard Worker  otherwise cause bugs.
410*5a6e8488SAndroid Build Coastguard Worker* All precondition `assert()`'s (see [Asserts][135]) come *after* local variable
411*5a6e8488SAndroid Build Coastguard Worker  declarations.
412*5a6e8488SAndroid Build Coastguard Worker* Besides short `if` statements and loops, there should *never* be more than one
413*5a6e8488SAndroid Build Coastguard Worker  statement per line.
414*5a6e8488SAndroid Build Coastguard Worker
415*5a6e8488SAndroid Build Coastguard Worker## Repo Structure
416*5a6e8488SAndroid Build Coastguard Worker
417*5a6e8488SAndroid Build Coastguard WorkerFunctions are documented with Doxygen-style doc comments. Functions that appear
418*5a6e8488SAndroid Build Coastguard Workerin headers are documented in the headers, while static functions are documented
419*5a6e8488SAndroid Build Coastguard Workerwhere they are defined.
420*5a6e8488SAndroid Build Coastguard Worker
421*5a6e8488SAndroid Build Coastguard Worker### `configure`
422*5a6e8488SAndroid Build Coastguard Worker
423*5a6e8488SAndroid Build Coastguard WorkerA symlink to [`configure.sh`][69].
424*5a6e8488SAndroid Build Coastguard Worker
425*5a6e8488SAndroid Build Coastguard Worker### `configure.sh`
426*5a6e8488SAndroid Build Coastguard Worker
427*5a6e8488SAndroid Build Coastguard WorkerThis is the script to configure `bc` and [`bcl`][156] for building.
428*5a6e8488SAndroid Build Coastguard Worker
429*5a6e8488SAndroid Build Coastguard WorkerThis `bc` has a custom build system. The reason for this is because of
430*5a6e8488SAndroid Build Coastguard Worker[*portability*][136].
431*5a6e8488SAndroid Build Coastguard Worker
432*5a6e8488SAndroid Build Coastguard WorkerIf `bc` used an outside build system, that build system would be an external
433*5a6e8488SAndroid Build Coastguard Workerdependency. Thus, I had to write a build system for `bc` that used nothing but
434*5a6e8488SAndroid Build Coastguard WorkerC99 and POSIX utilities.
435*5a6e8488SAndroid Build Coastguard Worker
436*5a6e8488SAndroid Build Coastguard WorkerOne of those utilities is POSIX `sh`, which technically implements a
437*5a6e8488SAndroid Build Coastguard WorkerTuring-complete programming language. It's a terrible one, but it works.
438*5a6e8488SAndroid Build Coastguard Worker
439*5a6e8488SAndroid Build Coastguard WorkerA user that wants to build `bc` on a POSIX system (not Windows) first runs
440*5a6e8488SAndroid Build Coastguard Worker`configure.sh` with the options he wants. `configure.sh` uses those options and
441*5a6e8488SAndroid Build Coastguard Workerthe `Makefile` template ([`Makefile.in`][70]) to generate an actual valid
442*5a6e8488SAndroid Build Coastguard Worker`Makefile`. Then `make` can do the rest.
443*5a6e8488SAndroid Build Coastguard Worker
444*5a6e8488SAndroid Build Coastguard WorkerFor more information about the build process, see the [Build System][142]
445*5a6e8488SAndroid Build Coastguard Workersection and the [build manual][14].
446*5a6e8488SAndroid Build Coastguard Worker
447*5a6e8488SAndroid Build Coastguard WorkerFor more information about shell scripts, see [POSIX Shell Scripts][76].
448*5a6e8488SAndroid Build Coastguard Worker
449*5a6e8488SAndroid Build Coastguard Worker`configure.sh` does the following:
450*5a6e8488SAndroid Build Coastguard Worker
451*5a6e8488SAndroid Build Coastguard Worker1.	It processes command-line arguments and figure out what the user wants to
452*5a6e8488SAndroid Build Coastguard Worker	build.
453*5a6e8488SAndroid Build Coastguard Worker2.	It reads in [`Makefile.in`][70].
454*5a6e8488SAndroid Build Coastguard Worker3.	One-by-one, it replaces placeholders (in [`Makefile.in`][70]) of the form
455*5a6e8488SAndroid Build Coastguard Worker	`%%<placeholder_name>%%` based on the [build type][81].
456*5a6e8488SAndroid Build Coastguard Worker4.	It appends a list of file targets based on the [build type][81].
457*5a6e8488SAndroid Build Coastguard Worker5.	It appends the correct test targets.
458*5a6e8488SAndroid Build Coastguard Worker6.	It copies the correct manpage and markdown manual for `bc` and `dc` into a
459*5a6e8488SAndroid Build Coastguard Worker	location from which they can be copied for install.
460*5a6e8488SAndroid Build Coastguard Worker7.	It does a `make clean` to reset the build state.
461*5a6e8488SAndroid Build Coastguard Worker
462*5a6e8488SAndroid Build Coastguard Worker### `.gitattributes`
463*5a6e8488SAndroid Build Coastguard Worker
464*5a6e8488SAndroid Build Coastguard WorkerA `.gitattributes` file. This is needed to preserve the `crlf` line endings in
465*5a6e8488SAndroid Build Coastguard Workerthe Visual Studio files.
466*5a6e8488SAndroid Build Coastguard Worker
467*5a6e8488SAndroid Build Coastguard Worker### `.gitignore`
468*5a6e8488SAndroid Build Coastguard Worker
469*5a6e8488SAndroid Build Coastguard WorkerThe `.gitignore`
470*5a6e8488SAndroid Build Coastguard Worker
471*5a6e8488SAndroid Build Coastguard Worker### `LICENSE.md`
472*5a6e8488SAndroid Build Coastguard Worker
473*5a6e8488SAndroid Build Coastguard WorkerThis is the `LICENSE` file, including the licenses of various software that I
474*5a6e8488SAndroid Build Coastguard Workerhave borrowed.
475*5a6e8488SAndroid Build Coastguard Worker
476*5a6e8488SAndroid Build Coastguard Worker### `Makefile.in`
477*5a6e8488SAndroid Build Coastguard Worker
478*5a6e8488SAndroid Build Coastguard WorkerThis is the `Makefile` template for [`configure.sh`][69] to use for generating a
479*5a6e8488SAndroid Build Coastguard Worker`Makefile`.
480*5a6e8488SAndroid Build Coastguard Worker
481*5a6e8488SAndroid Build Coastguard WorkerFor more information, see [`configure.sh`][69], the [Build System][142] section,
482*5a6e8488SAndroid Build Coastguard Workerand the [build manual][14].
483*5a6e8488SAndroid Build Coastguard Worker
484*5a6e8488SAndroid Build Coastguard WorkerBecause of [portability][136], the generated `Makefile.in` should be a pure
485*5a6e8488SAndroid Build Coastguard Worker[POSIX `make`][74]-compatible `Makefile` (minus the placeholders). Here are a
486*5a6e8488SAndroid Build Coastguard Workerfew snares for the unwary programmer in this file:
487*5a6e8488SAndroid Build Coastguard Worker
488*5a6e8488SAndroid Build Coastguard Worker1.	No extensions allowed, including and especially GNU extensions.
489*5a6e8488SAndroid Build Coastguard Worker2.	If new headers are added, they must also be added to `Makefile.in`.
490*5a6e8488SAndroid Build Coastguard Worker3.	Don't delete the `.POSIX:` empty target at the top; that's what tells `make`
491*5a6e8488SAndroid Build Coastguard Worker	implementations that pure [POSIX `make`][74] is needed.
492*5a6e8488SAndroid Build Coastguard Worker
493*5a6e8488SAndroid Build Coastguard WorkerIn particular, there is no way to set up variables other than the `=` operator.
494*5a6e8488SAndroid Build Coastguard WorkerThere are no conditionals, so all of the conditional stuff must be in
495*5a6e8488SAndroid Build Coastguard Worker[`configure.sh`][69]. This is, in fact, why [`configure.sh`][69] exists in the
496*5a6e8488SAndroid Build Coastguard Workerfirst place: [POSIX `make`][74] is barebones and only does a build with no
497*5a6e8488SAndroid Build Coastguard Workerconfiguration.
498*5a6e8488SAndroid Build Coastguard Worker
499*5a6e8488SAndroid Build Coastguard Worker### `NEWS.md`
500*5a6e8488SAndroid Build Coastguard Worker
501*5a6e8488SAndroid Build Coastguard WorkerA running changelog with an entry for each version. This should be updated at
502*5a6e8488SAndroid Build Coastguard Workerthe same time that [`include/version.h`][75] is.
503*5a6e8488SAndroid Build Coastguard Worker
504*5a6e8488SAndroid Build Coastguard Worker### `NOTICE.md`
505*5a6e8488SAndroid Build Coastguard Worker
506*5a6e8488SAndroid Build Coastguard WorkerThe `NOTICE` file with proper attributions.
507*5a6e8488SAndroid Build Coastguard Worker
508*5a6e8488SAndroid Build Coastguard Worker### `README.md`
509*5a6e8488SAndroid Build Coastguard Worker
510*5a6e8488SAndroid Build Coastguard WorkerThe `README`. Read it.
511*5a6e8488SAndroid Build Coastguard Worker
512*5a6e8488SAndroid Build Coastguard Worker### `benchmarks/`
513*5a6e8488SAndroid Build Coastguard Worker
514*5a6e8488SAndroid Build Coastguard WorkerThe folder containing files to generate benchmarks.
515*5a6e8488SAndroid Build Coastguard Worker
516*5a6e8488SAndroid Build Coastguard WorkerEach of these files was made, at one time or another, to benchmark some
517*5a6e8488SAndroid Build Coastguard Workerexperimental feature, so if it seems there is no rhyme or reason to these
518*5a6e8488SAndroid Build Coastguard Workerbenchmarks, it is because there is none, besides historical accident.
519*5a6e8488SAndroid Build Coastguard Worker
520*5a6e8488SAndroid Build Coastguard Worker#### `bc/`
521*5a6e8488SAndroid Build Coastguard Worker
522*5a6e8488SAndroid Build Coastguard WorkerThe folder containing `bc` scripts to generate `bc` benchmarks.
523*5a6e8488SAndroid Build Coastguard Worker
524*5a6e8488SAndroid Build Coastguard Worker##### `add.bc`
525*5a6e8488SAndroid Build Coastguard Worker
526*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark addition in `bc`.
527*5a6e8488SAndroid Build Coastguard Worker
528*5a6e8488SAndroid Build Coastguard Worker##### `arrays_and_constants.bc`
529*5a6e8488SAndroid Build Coastguard Worker
530*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of array names
531*5a6e8488SAndroid Build Coastguard Workerand constants.
532*5a6e8488SAndroid Build Coastguard Worker
533*5a6e8488SAndroid Build Coastguard Worker##### `arrays.bc`
534*5a6e8488SAndroid Build Coastguard Worker
535*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of array names.
536*5a6e8488SAndroid Build Coastguard Worker
537*5a6e8488SAndroid Build Coastguard Worker##### `constants.bc`
538*5a6e8488SAndroid Build Coastguard Worker
539*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of constants.
540*5a6e8488SAndroid Build Coastguard Worker
541*5a6e8488SAndroid Build Coastguard Worker##### `divide.bc`
542*5a6e8488SAndroid Build Coastguard Worker
543*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark division in `bc`.
544*5a6e8488SAndroid Build Coastguard Worker
545*5a6e8488SAndroid Build Coastguard Worker##### `functions.bc`
546*5a6e8488SAndroid Build Coastguard Worker
547*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of functions.
548*5a6e8488SAndroid Build Coastguard Worker
549*5a6e8488SAndroid Build Coastguard Worker##### `irand_long.bc`
550*5a6e8488SAndroid Build Coastguard Worker
551*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of calls to
552*5a6e8488SAndroid Build Coastguard Worker`irand()` with large bounds.
553*5a6e8488SAndroid Build Coastguard Worker
554*5a6e8488SAndroid Build Coastguard Worker##### `irand_short.bc`
555*5a6e8488SAndroid Build Coastguard Worker
556*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of calls to
557*5a6e8488SAndroid Build Coastguard Worker`irand()` with small bounds.
558*5a6e8488SAndroid Build Coastguard Worker
559*5a6e8488SAndroid Build Coastguard Worker##### `lib.bc`
560*5a6e8488SAndroid Build Coastguard Worker
561*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of calls to
562*5a6e8488SAndroid Build Coastguard Workerheavy functions in `lib.bc`.
563*5a6e8488SAndroid Build Coastguard Worker
564*5a6e8488SAndroid Build Coastguard Worker##### `multiply.bc`
565*5a6e8488SAndroid Build Coastguard Worker
566*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark multiplication in `bc`.
567*5a6e8488SAndroid Build Coastguard Worker
568*5a6e8488SAndroid Build Coastguard Worker##### `newton_raphson_div_large.bc`
569*5a6e8488SAndroid Build Coastguard Worker
570*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark the Newton-Raphson division in
571*5a6e8488SAndroid Build Coastguard Worker[GitHub PR #72][229] with large numbers.
572*5a6e8488SAndroid Build Coastguard Worker
573*5a6e8488SAndroid Build Coastguard Worker##### `newton_raphson_div_small.bc`
574*5a6e8488SAndroid Build Coastguard Worker
575*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark the Newton-Raphson division in
576*5a6e8488SAndroid Build Coastguard Worker[GitHub PR #72][229] with small numbers.
577*5a6e8488SAndroid Build Coastguard Worker
578*5a6e8488SAndroid Build Coastguard Worker##### `newton_raphson_sqrt_large.bc`
579*5a6e8488SAndroid Build Coastguard Worker
580*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark the Newton-Raphson square root
581*5a6e8488SAndroid Build Coastguard Workerin [GitHub PR #72][229] with large numbers.
582*5a6e8488SAndroid Build Coastguard Worker
583*5a6e8488SAndroid Build Coastguard Worker##### `newton_raphson_sqrt_small.bc`
584*5a6e8488SAndroid Build Coastguard Worker
585*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark the Newton-Raphson square root
586*5a6e8488SAndroid Build Coastguard Workerin [GitHub PR #72][229] with small numbers.
587*5a6e8488SAndroid Build Coastguard Worker
588*5a6e8488SAndroid Build Coastguard Worker##### `postfix_incdec.bc`
589*5a6e8488SAndroid Build Coastguard Worker
590*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using postfix increment and
591*5a6e8488SAndroid Build Coastguard Workerdecrement operators.
592*5a6e8488SAndroid Build Coastguard Worker
593*5a6e8488SAndroid Build Coastguard Worker##### `power.bc`
594*5a6e8488SAndroid Build Coastguard Worker
595*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark power (exponentiation) in `bc`.
596*5a6e8488SAndroid Build Coastguard Worker
597*5a6e8488SAndroid Build Coastguard Worker##### `subtract.bc`
598*5a6e8488SAndroid Build Coastguard Worker
599*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark subtraction in `bc`.
600*5a6e8488SAndroid Build Coastguard Worker
601*5a6e8488SAndroid Build Coastguard Worker##### `strings.bc`
602*5a6e8488SAndroid Build Coastguard Worker
603*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark `bc` using lots of strings.
604*5a6e8488SAndroid Build Coastguard Worker
605*5a6e8488SAndroid Build Coastguard Worker#### `dc/`
606*5a6e8488SAndroid Build Coastguard Worker
607*5a6e8488SAndroid Build Coastguard WorkerThe folder containing `dc` scripts to generate `dc` benchmarks.
608*5a6e8488SAndroid Build Coastguard Worker
609*5a6e8488SAndroid Build Coastguard Worker##### `modexp.dc`
610*5a6e8488SAndroid Build Coastguard Worker
611*5a6e8488SAndroid Build Coastguard WorkerThe file to generate the benchmark to benchmark modular exponentiation in `dc`.
612*5a6e8488SAndroid Build Coastguard Worker
613*5a6e8488SAndroid Build Coastguard Worker### `gen/`
614*5a6e8488SAndroid Build Coastguard Worker
615*5a6e8488SAndroid Build Coastguard WorkerA folder containing the files necessary to generate C strings that will be
616*5a6e8488SAndroid Build Coastguard Workerembedded in the executable.
617*5a6e8488SAndroid Build Coastguard Worker
618*5a6e8488SAndroid Build Coastguard WorkerAll of the files in this folder have license headers, but the program and script
619*5a6e8488SAndroid Build Coastguard Workerthat can generate strings from them include code to strip the license header out
620*5a6e8488SAndroid Build Coastguard Workerbefore strings are generated.
621*5a6e8488SAndroid Build Coastguard Worker
622*5a6e8488SAndroid Build Coastguard Worker#### `bc_help.txt`
623*5a6e8488SAndroid Build Coastguard Worker
624*5a6e8488SAndroid Build Coastguard WorkerA text file containing the text displayed for `bc -h` or `bc --help`.
625*5a6e8488SAndroid Build Coastguard Worker
626*5a6e8488SAndroid Build Coastguard WorkerThis text just contains the command-line options and a short summary of the
627*5a6e8488SAndroid Build Coastguard Workerdifferences from GNU and BSD `bc`'s. It also directs users to the manpage.
628*5a6e8488SAndroid Build Coastguard Worker
629*5a6e8488SAndroid Build Coastguard WorkerThe reason for this is because otherwise, the help would be far too long to be
630*5a6e8488SAndroid Build Coastguard Workeruseful.
631*5a6e8488SAndroid Build Coastguard Worker
632*5a6e8488SAndroid Build Coastguard Worker**Warning**: The text has some `printf()` format specifiers. You need to make
633*5a6e8488SAndroid Build Coastguard Workersure the format specifiers match the arguments given to `bc_file_printf()`.
634*5a6e8488SAndroid Build Coastguard Worker
635*5a6e8488SAndroid Build Coastguard Worker#### `dc_help.txt`
636*5a6e8488SAndroid Build Coastguard Worker
637*5a6e8488SAndroid Build Coastguard WorkerA text file containing the text displayed for `dc -h` or `dc --help`.
638*5a6e8488SAndroid Build Coastguard Worker
639*5a6e8488SAndroid Build Coastguard WorkerThis text just contains the command-line options and a short summary of the
640*5a6e8488SAndroid Build Coastguard Workerdifferences from GNU and BSD `dc`'s. It also directs users to the manpage.
641*5a6e8488SAndroid Build Coastguard Worker
642*5a6e8488SAndroid Build Coastguard WorkerThe reason for this is because otherwise, the help would be far too long to be
643*5a6e8488SAndroid Build Coastguard Workeruseful.
644*5a6e8488SAndroid Build Coastguard Worker
645*5a6e8488SAndroid Build Coastguard Worker**Warning**: The text has some `printf()` format specifiers. You need to make
646*5a6e8488SAndroid Build Coastguard Workersure the format specifiers match the arguments given to `bc_file_printf()`.
647*5a6e8488SAndroid Build Coastguard Worker
648*5a6e8488SAndroid Build Coastguard Worker#### `lib.bc`
649*5a6e8488SAndroid Build Coastguard Worker
650*5a6e8488SAndroid Build Coastguard WorkerA `bc` script containing the [standard math library][5] required by POSIX. See
651*5a6e8488SAndroid Build Coastguard Workerthe [POSIX standard][2] for what is required.
652*5a6e8488SAndroid Build Coastguard Worker
653*5a6e8488SAndroid Build Coastguard WorkerThis file does not have any extraneous whitespace, except for tabs at the
654*5a6e8488SAndroid Build Coastguard Workerbeginning of lines. That is because this data goes directly into the binary,
655*5a6e8488SAndroid Build Coastguard Workerand whitespace is extra bytes in the binary. Thus, not having any extra
656*5a6e8488SAndroid Build Coastguard Workerwhitespace shrinks the resulting binary.
657*5a6e8488SAndroid Build Coastguard Worker
658*5a6e8488SAndroid Build Coastguard WorkerHowever, tabs at the beginning of lines are kept for two reasons:
659*5a6e8488SAndroid Build Coastguard Worker
660*5a6e8488SAndroid Build Coastguard Worker1.	Readability. (This file is still code.)
661*5a6e8488SAndroid Build Coastguard Worker2.	The program and script that generate strings from this file can remove
662*5a6e8488SAndroid Build Coastguard Worker	tabs at the beginning of lines.
663*5a6e8488SAndroid Build Coastguard Worker
664*5a6e8488SAndroid Build Coastguard WorkerFor more details about the algorithms used, see the [algorithms manual][25].
665*5a6e8488SAndroid Build Coastguard Worker
666*5a6e8488SAndroid Build Coastguard WorkerHowever, there are a few snares for unwary programmers.
667*5a6e8488SAndroid Build Coastguard Worker
668*5a6e8488SAndroid Build Coastguard WorkerFirst, all constants must be one digit. This is because otherwise, multi-digit
669*5a6e8488SAndroid Build Coastguard Workerconstants could be interpreted wrongly if the user uses a different `ibase`.
670*5a6e8488SAndroid Build Coastguard WorkerThis does not happen with single-digit numbers because they are guaranteed to be
671*5a6e8488SAndroid Build Coastguard Workerinterpreted what number they would be if the `ibase` was as high as possible.
672*5a6e8488SAndroid Build Coastguard Worker
673*5a6e8488SAndroid Build Coastguard WorkerThis is why `A` is used in the library instead of `10`, and things like `2*9*A`
674*5a6e8488SAndroid Build Coastguard Workerfor `180` in [`lib2.bc`][26].
675*5a6e8488SAndroid Build Coastguard Worker
676*5a6e8488SAndroid Build Coastguard WorkerAs an alternative, you can set `ibase` in the function, but if you do, make sure
677*5a6e8488SAndroid Build Coastguard Workerto set it with a single-digit number and beware the snare below...
678*5a6e8488SAndroid Build Coastguard Worker
679*5a6e8488SAndroid Build Coastguard WorkerSecond, `scale`, `ibase`, and `obase` must be safely restored before returning
680*5a6e8488SAndroid Build Coastguard Workerfrom any function in the library. This is because without the `-g` option,
681*5a6e8488SAndroid Build Coastguard Workerfunctions are allowed to change any of the globals.
682*5a6e8488SAndroid Build Coastguard Worker
683*5a6e8488SAndroid Build Coastguard WorkerThird, all local variables in a function must be declared in an `auto` statement
684*5a6e8488SAndroid Build Coastguard Workerbefore doing anything else. This includes arrays. However, function parameters
685*5a6e8488SAndroid Build Coastguard Workerare considered predeclared.
686*5a6e8488SAndroid Build Coastguard Worker
687*5a6e8488SAndroid Build Coastguard WorkerFourth, and this is only a snare for `lib.bc`, not [`lib2.bc`][26], the code
688*5a6e8488SAndroid Build Coastguard Workermust not use *any* extensions. It has to work when users use the `-s` or `-w`
689*5a6e8488SAndroid Build Coastguard Workerflags.
690*5a6e8488SAndroid Build Coastguard Worker
691*5a6e8488SAndroid Build Coastguard Worker#### `lib2.bc`
692*5a6e8488SAndroid Build Coastguard Worker
693*5a6e8488SAndroid Build Coastguard WorkerA `bc` script containing the [extended math library][7].
694*5a6e8488SAndroid Build Coastguard Worker
695*5a6e8488SAndroid Build Coastguard WorkerLike [`lib.bc`][8], and for the same reasons, this file should have no
696*5a6e8488SAndroid Build Coastguard Workerextraneous whitespace, except for tabs at the beginning of lines.
697*5a6e8488SAndroid Build Coastguard Worker
698*5a6e8488SAndroid Build Coastguard WorkerFor more details about the algorithms used, see the [algorithms manual][25].
699*5a6e8488SAndroid Build Coastguard Worker
700*5a6e8488SAndroid Build Coastguard WorkerAlso, be sure to check [`lib.bc`][8] for the snares that can trip up unwary
701*5a6e8488SAndroid Build Coastguard Workerprogrammers when writing code for `lib2.bc`.
702*5a6e8488SAndroid Build Coastguard Worker
703*5a6e8488SAndroid Build Coastguard Worker#### `strgen.c`
704*5a6e8488SAndroid Build Coastguard Worker
705*5a6e8488SAndroid Build Coastguard WorkerCode for the program to generate C strings from text files. This is the original
706*5a6e8488SAndroid Build Coastguard Workerprogram, although [`strgen.sh`][9] was added later.
707*5a6e8488SAndroid Build Coastguard Worker
708*5a6e8488SAndroid Build Coastguard WorkerThe reason I used C here is because even though I knew `sh` would be available
709*5a6e8488SAndroid Build Coastguard Worker(it must be available to run `configure.sh`), I didn't know how to do what I
710*5a6e8488SAndroid Build Coastguard Workerneeded to do with POSIX utilities and `sh`.
711*5a6e8488SAndroid Build Coastguard Worker
712*5a6e8488SAndroid Build Coastguard WorkerLater, [`strgen.sh`][9] was contributed by Stefan Eßer of FreeBSD, showing that
713*5a6e8488SAndroid Build Coastguard Workerit *could* be done with `sh` and POSIX utilities.
714*5a6e8488SAndroid Build Coastguard Worker
715*5a6e8488SAndroid Build Coastguard WorkerHowever, `strgen.c` exists *still* exists because the versions generated by
716*5a6e8488SAndroid Build Coastguard Worker[`strgen.sh`][9] may technically hit an environmental limit. (See the [draft C99
717*5a6e8488SAndroid Build Coastguard Workerstandard][12], page 21.) This is because [`strgen.sh`][9] generates string
718*5a6e8488SAndroid Build Coastguard Workerliterals, and in C99, string literals can be limited to 4095 characters, and
719*5a6e8488SAndroid Build Coastguard Worker`gen/lib2.bc` is above that.
720*5a6e8488SAndroid Build Coastguard Worker
721*5a6e8488SAndroid Build Coastguard WorkerFortunately, the limit for "objects," which include `char` arrays, is much
722*5a6e8488SAndroid Build Coastguard Workerbigger: 65535 bytes, so that's what `strgen.c` generates.
723*5a6e8488SAndroid Build Coastguard Worker
724*5a6e8488SAndroid Build Coastguard WorkerHowever, the existence of `strgen.c` does come with a cost: the build needs C99
725*5a6e8488SAndroid Build Coastguard Workercompiler that targets the host machine. For more information, see the ["Cross
726*5a6e8488SAndroid Build Coastguard WorkerCompiling" section][13] of the [build manual][14].
727*5a6e8488SAndroid Build Coastguard Worker
728*5a6e8488SAndroid Build Coastguard WorkerRead the comments in `strgen.c` for more detail about it, the arguments it
729*5a6e8488SAndroid Build Coastguard Workertakes, and how it works.
730*5a6e8488SAndroid Build Coastguard Worker
731*5a6e8488SAndroid Build Coastguard Worker#### `strgen.sh`
732*5a6e8488SAndroid Build Coastguard Worker
733*5a6e8488SAndroid Build Coastguard WorkerAn `sh` script that will generate C strings that uses only POSIX utilities. This
734*5a6e8488SAndroid Build Coastguard Workerexists for those situations where a host C99 compiler is not available, and the
735*5a6e8488SAndroid Build Coastguard Workerenvironment limits mentioned above in [`strgen.c`][15] don't matter.
736*5a6e8488SAndroid Build Coastguard Worker
737*5a6e8488SAndroid Build Coastguard Worker`strgen.sh` takes the same arguments as [`strgen.c`][15], and the arguments mean
738*5a6e8488SAndroid Build Coastguard Workerthe exact same things, so see the comments in [`strgen.c`][15] for more detail
739*5a6e8488SAndroid Build Coastguard Workerabout that, and see the comments in `strgen.sh` for more details about it and
740*5a6e8488SAndroid Build Coastguard Workerhow it works.
741*5a6e8488SAndroid Build Coastguard Worker
742*5a6e8488SAndroid Build Coastguard WorkerFor more information about shell scripts, see [POSIX Shell Scripts][76].
743*5a6e8488SAndroid Build Coastguard Worker
744*5a6e8488SAndroid Build Coastguard Worker### `include/`
745*5a6e8488SAndroid Build Coastguard Worker
746*5a6e8488SAndroid Build Coastguard WorkerA folder containing the headers.
747*5a6e8488SAndroid Build Coastguard Worker
748*5a6e8488SAndroid Build Coastguard WorkerThe headers are not included among the source code because I like it better that
749*5a6e8488SAndroid Build Coastguard Workerway. Also there were folders within `src/` at one point, and I did not want to
750*5a6e8488SAndroid Build Coastguard Workersee `#include "../some_header.h"` or things like that.
751*5a6e8488SAndroid Build Coastguard Worker
752*5a6e8488SAndroid Build Coastguard WorkerSo all headers are here, even though only one ([`bcl.h`][30]) is meant for end
753*5a6e8488SAndroid Build Coastguard Workerusers (to be installed in `INCLUDEDIR`).
754*5a6e8488SAndroid Build Coastguard Worker
755*5a6e8488SAndroid Build Coastguard Worker#### `args.h`
756*5a6e8488SAndroid Build Coastguard Worker
757*5a6e8488SAndroid Build Coastguard WorkerThis file is the API for processing command-line arguments.
758*5a6e8488SAndroid Build Coastguard Worker
759*5a6e8488SAndroid Build Coastguard Worker#### `bc.h`
760*5a6e8488SAndroid Build Coastguard Worker
761*5a6e8488SAndroid Build Coastguard WorkerThis header is the API for `bc`-only items. This includes the `bc_main()`
762*5a6e8488SAndroid Build Coastguard Workerfunction and the `bc`-specific lexing and parsing items.
763*5a6e8488SAndroid Build Coastguard Worker
764*5a6e8488SAndroid Build Coastguard WorkerThe `bc` parser is perhaps the most sensitive part of the entire codebase. See
765*5a6e8488SAndroid Build Coastguard Workerthe documentation in `bc.h` for more information.
766*5a6e8488SAndroid Build Coastguard Worker
767*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/bc.c`][40],
768*5a6e8488SAndroid Build Coastguard Worker[`src/bc_lex.c`][41], and [`src/bc_parse.c`][42].
769*5a6e8488SAndroid Build Coastguard Worker
770*5a6e8488SAndroid Build Coastguard Worker#### `bcl.h`
771*5a6e8488SAndroid Build Coastguard Worker
772*5a6e8488SAndroid Build Coastguard WorkerThis header is the API for the [`bcl`][156] library.
773*5a6e8488SAndroid Build Coastguard Worker
774*5a6e8488SAndroid Build Coastguard WorkerThis header is meant for distribution to end users and contains the API that end
775*5a6e8488SAndroid Build Coastguard Workerusers of [`bcl`][156] can use in their own software.
776*5a6e8488SAndroid Build Coastguard Worker
777*5a6e8488SAndroid Build Coastguard WorkerThis header, because it's the public header, is also the root header. That means
778*5a6e8488SAndroid Build Coastguard Workerthat it has platform-specific fixes for Windows. (If the fixes were not in this
779*5a6e8488SAndroid Build Coastguard Workerheader, the build would fail on Windows.)
780*5a6e8488SAndroid Build Coastguard Worker
781*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/library.c`][43].
782*5a6e8488SAndroid Build Coastguard Worker
783*5a6e8488SAndroid Build Coastguard Worker#### `dc.h`
784*5a6e8488SAndroid Build Coastguard Worker
785*5a6e8488SAndroid Build Coastguard WorkerThis header is the API for `dc`-only items. This includes the `dc_main()`
786*5a6e8488SAndroid Build Coastguard Workerfunction and the `dc`-specific lexing and parsing items.
787*5a6e8488SAndroid Build Coastguard Worker
788*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/dc.c`][44],
789*5a6e8488SAndroid Build Coastguard Worker[`src/dc_lex.c`][45], and [`src/dc_parse.c`][46].
790*5a6e8488SAndroid Build Coastguard Worker
791*5a6e8488SAndroid Build Coastguard Worker#### `file.h`
792*5a6e8488SAndroid Build Coastguard Worker
793*5a6e8488SAndroid Build Coastguard WorkerThis header is for `bc`'s internal buffered I/O API.
794*5a6e8488SAndroid Build Coastguard Worker
795*5a6e8488SAndroid Build Coastguard WorkerFor more information about `bc`'s error handling and custom buffered I/O, see
796*5a6e8488SAndroid Build Coastguard Worker[Error Handling][97] and [Custom I/O][114], along with [`status.h`][176] and the
797*5a6e8488SAndroid Build Coastguard Workernotes about version [`3.0.0`][32] in the [`NEWS`][32].
798*5a6e8488SAndroid Build Coastguard Worker
799*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/file.c`][47].
800*5a6e8488SAndroid Build Coastguard Worker
801*5a6e8488SAndroid Build Coastguard Worker#### `history.h`
802*5a6e8488SAndroid Build Coastguard Worker
803*5a6e8488SAndroid Build Coastguard WorkerThis header is for `bc`'s implementation of command-line editing/history, which
804*5a6e8488SAndroid Build Coastguard Workeris based on a [UTF-8-aware fork][28] of [`linenoise`][29].
805*5a6e8488SAndroid Build Coastguard Worker
806*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [Command-Line History][189] section.
807*5a6e8488SAndroid Build Coastguard Worker
808*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/history.c`][48].
809*5a6e8488SAndroid Build Coastguard Worker
810*5a6e8488SAndroid Build Coastguard Worker#### `lang.h`
811*5a6e8488SAndroid Build Coastguard Worker
812*5a6e8488SAndroid Build Coastguard WorkerThis header defines the data structures and bytecode used for actual execution
813*5a6e8488SAndroid Build Coastguard Workerof `bc` and `dc` code.
814*5a6e8488SAndroid Build Coastguard Worker
815*5a6e8488SAndroid Build Coastguard WorkerYes, it's misnamed; that's an accident of history where the first things I put
816*5a6e8488SAndroid Build Coastguard Workerinto it all seemed related to the `bc` language.
817*5a6e8488SAndroid Build Coastguard Worker
818*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/lang.c`][49].
819*5a6e8488SAndroid Build Coastguard Worker
820*5a6e8488SAndroid Build Coastguard Worker#### `lex.h`
821*5a6e8488SAndroid Build Coastguard Worker
822*5a6e8488SAndroid Build Coastguard WorkerThis header defines the common items that both programs need for lexing.
823*5a6e8488SAndroid Build Coastguard Worker
824*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/lex.c`][50],
825*5a6e8488SAndroid Build Coastguard Worker[`src/bc_lex.c`][41], and [`src/dc_lex.c`][45].
826*5a6e8488SAndroid Build Coastguard Worker
827*5a6e8488SAndroid Build Coastguard Worker#### `library.h`
828*5a6e8488SAndroid Build Coastguard Worker
829*5a6e8488SAndroid Build Coastguard WorkerThis header defines the things needed for [`bcl`][156] that users should *not*
830*5a6e8488SAndroid Build Coastguard Workerhave access to. In other words, [`bcl.h`][30] is the *public* header for the
831*5a6e8488SAndroid Build Coastguard Workerlibrary, and this header is the *private* header for the library.
832*5a6e8488SAndroid Build Coastguard Worker
833*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/library.c`][43].
834*5a6e8488SAndroid Build Coastguard Worker
835*5a6e8488SAndroid Build Coastguard Worker#### `num.h`
836*5a6e8488SAndroid Build Coastguard Worker
837*5a6e8488SAndroid Build Coastguard WorkerThis header is the API for numbers and math.
838*5a6e8488SAndroid Build Coastguard Worker
839*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/num.c`][39].
840*5a6e8488SAndroid Build Coastguard Worker
841*5a6e8488SAndroid Build Coastguard Worker#### `opt.h`
842*5a6e8488SAndroid Build Coastguard Worker
843*5a6e8488SAndroid Build Coastguard WorkerThis header is the API for parsing command-line arguments.
844*5a6e8488SAndroid Build Coastguard Worker
845*5a6e8488SAndroid Build Coastguard WorkerIt's different from [`args.h`][31] in that [`args.h`][31] is for the main code
846*5a6e8488SAndroid Build Coastguard Workerto process the command-line arguments into global data *after* they have already
847*5a6e8488SAndroid Build Coastguard Workerbeen parsed by `opt.h` into proper tokens. In other words, `opt.h` actually
848*5a6e8488SAndroid Build Coastguard Workerparses the command-line arguments, and [`args.h`][31] turns that parsed data
849*5a6e8488SAndroid Build Coastguard Workerinto flags (bits), strings, and expressions that will be used later.
850*5a6e8488SAndroid Build Coastguard Worker
851*5a6e8488SAndroid Build Coastguard WorkerWhy are they separate? Because originally, `bc` used `getopt_long()` for
852*5a6e8488SAndroid Build Coastguard Workerparsing, so [`args.h`][31] was the only one that existed. After it was
853*5a6e8488SAndroid Build Coastguard Workerdiscovered that `getopt_long()` has different behavior on different platforms, I
854*5a6e8488SAndroid Build Coastguard Workeradapted a [public-domain option parsing library][34] to do the job instead. And
855*5a6e8488SAndroid Build Coastguard Workerin doing so, I gave it its own header.
856*5a6e8488SAndroid Build Coastguard Worker
857*5a6e8488SAndroid Build Coastguard WorkerThey could probably be combined, but I don't really care enough at this point.
858*5a6e8488SAndroid Build Coastguard Worker
859*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/opt.c`][51].
860*5a6e8488SAndroid Build Coastguard Worker
861*5a6e8488SAndroid Build Coastguard Worker#### `parse.h`
862*5a6e8488SAndroid Build Coastguard Worker
863*5a6e8488SAndroid Build Coastguard WorkerThis header defines the common items that both programs need for parsing.
864*5a6e8488SAndroid Build Coastguard Worker
865*5a6e8488SAndroid Build Coastguard WorkerNote that the parsers don't produce abstract syntax trees (AST's) or any
866*5a6e8488SAndroid Build Coastguard Workerintermediate representations. They produce bytecode directly. In other words,
867*5a6e8488SAndroid Build Coastguard Workerthey don't have special data structures except what they need to do their job.
868*5a6e8488SAndroid Build Coastguard Worker
869*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/parse.c`][50],
870*5a6e8488SAndroid Build Coastguard Worker[`src/bc_lex.c`][42], and [`src/dc_lex.c`][46].
871*5a6e8488SAndroid Build Coastguard Worker
872*5a6e8488SAndroid Build Coastguard Worker#### `program.h`
873*5a6e8488SAndroid Build Coastguard Worker
874*5a6e8488SAndroid Build Coastguard WorkerThis header defines the items needed to manage the data structures in
875*5a6e8488SAndroid Build Coastguard Worker[`lang.h`][38] as well as any helper functions needed to generate bytecode or
876*5a6e8488SAndroid Build Coastguard Workerexecute it.
877*5a6e8488SAndroid Build Coastguard Worker
878*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/program.c`][53].
879*5a6e8488SAndroid Build Coastguard Worker
880*5a6e8488SAndroid Build Coastguard Worker#### `rand.h`
881*5a6e8488SAndroid Build Coastguard Worker
882*5a6e8488SAndroid Build Coastguard WorkerThis header defines the API for the [pseudo-random number generator
883*5a6e8488SAndroid Build Coastguard Worker(PRNG)][179].
884*5a6e8488SAndroid Build Coastguard Worker
885*5a6e8488SAndroid Build Coastguard WorkerThe PRNG only generates fixed-size integers. The magic of generating random
886*5a6e8488SAndroid Build Coastguard Workernumbers of arbitrary size is actually given to the code that does math
887*5a6e8488SAndroid Build Coastguard Worker([`src/num.c`][39]).
888*5a6e8488SAndroid Build Coastguard Worker
889*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/rand.c`][54].
890*5a6e8488SAndroid Build Coastguard Worker
891*5a6e8488SAndroid Build Coastguard Worker#### `read.h`
892*5a6e8488SAndroid Build Coastguard Worker
893*5a6e8488SAndroid Build Coastguard WorkerThis header defines the API for reading from files and `stdin`.
894*5a6e8488SAndroid Build Coastguard Worker
895*5a6e8488SAndroid Build Coastguard WorkerThus, [`file.h`][55] is really for buffered *output*, while this file is for
896*5a6e8488SAndroid Build Coastguard Worker*input*. There is no buffering needed for `bc`'s inputs.
897*5a6e8488SAndroid Build Coastguard Worker
898*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/read.c`][56].
899*5a6e8488SAndroid Build Coastguard Worker
900*5a6e8488SAndroid Build Coastguard Worker#### `status.h`
901*5a6e8488SAndroid Build Coastguard Worker
902*5a6e8488SAndroid Build Coastguard WorkerThis header has several things:
903*5a6e8488SAndroid Build Coastguard Worker
904*5a6e8488SAndroid Build Coastguard Worker* A list of possible errors that internal `bc` code can use.
905*5a6e8488SAndroid Build Coastguard Worker* Compiler-specific fixes.
906*5a6e8488SAndroid Build Coastguard Worker* Platform-specific fixes.
907*5a6e8488SAndroid Build Coastguard Worker* Macros for `bc`'s [error handling][97].
908*5a6e8488SAndroid Build Coastguard Worker
909*5a6e8488SAndroid Build Coastguard WorkerThere is no code associated with this header.
910*5a6e8488SAndroid Build Coastguard Worker
911*5a6e8488SAndroid Build Coastguard Worker#### `vector.h`
912*5a6e8488SAndroid Build Coastguard Worker
913*5a6e8488SAndroid Build Coastguard WorkerThis header defines the API for the vectors (resizable arrays) that are used for
914*5a6e8488SAndroid Build Coastguard Workerdata structures.
915*5a6e8488SAndroid Build Coastguard Worker
916*5a6e8488SAndroid Build Coastguard WorkerVectors are what do the heavy lifting in almost all of `bc`'s data structures.
917*5a6e8488SAndroid Build Coastguard WorkerEven the maps of identifiers and arrays use vectors.
918*5a6e8488SAndroid Build Coastguard Worker
919*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/vector.c`][228].
920*5a6e8488SAndroid Build Coastguard Worker
921*5a6e8488SAndroid Build Coastguard Worker#### `version.h`
922*5a6e8488SAndroid Build Coastguard Worker
923*5a6e8488SAndroid Build Coastguard WorkerThis header defines the version of `bc`.
924*5a6e8488SAndroid Build Coastguard Worker
925*5a6e8488SAndroid Build Coastguard WorkerThere is no code associated with this header.
926*5a6e8488SAndroid Build Coastguard Worker
927*5a6e8488SAndroid Build Coastguard Worker#### `vm.h`
928*5a6e8488SAndroid Build Coastguard Worker
929*5a6e8488SAndroid Build Coastguard WorkerThis header defines the API for setting up and running `bc` and `dc`.
930*5a6e8488SAndroid Build Coastguard Worker
931*5a6e8488SAndroid Build Coastguard WorkerIt is so named because I think of it as the "virtual machine" of `bc`, though
932*5a6e8488SAndroid Build Coastguard Workerthat is probably not true as [`program.h`][57] is probably the "virtual machine"
933*5a6e8488SAndroid Build Coastguard WorkerAPI. Thus, the name is more historical accident.
934*5a6e8488SAndroid Build Coastguard Worker
935*5a6e8488SAndroid Build Coastguard WorkerThe code associated with this header is in [`src/vm.c`][58].
936*5a6e8488SAndroid Build Coastguard Worker
937*5a6e8488SAndroid Build Coastguard Worker### `locales/`
938*5a6e8488SAndroid Build Coastguard Worker
939*5a6e8488SAndroid Build Coastguard WorkerThis folder contains a bunch of `.msg` files and soft links to the real `.msg`
940*5a6e8488SAndroid Build Coastguard Workerfiles. This is how locale support is implemented in `bc`.
941*5a6e8488SAndroid Build Coastguard Worker
942*5a6e8488SAndroid Build Coastguard WorkerThe files are in the format required by the [`gencat`][59] POSIX utility. They
943*5a6e8488SAndroid Build Coastguard Workerall have the same messages, in the same order, with the same numbering, under
944*5a6e8488SAndroid Build Coastguard Workerthe same groups. This is because the locale system expects those messages in
945*5a6e8488SAndroid Build Coastguard Workerthat order.
946*5a6e8488SAndroid Build Coastguard Worker
947*5a6e8488SAndroid Build Coastguard WorkerThe softlinks exist because for many locales, they would contain the exact same
948*5a6e8488SAndroid Build Coastguard Workerinformation. To prevent duplication, they are simply linked to a master copy.
949*5a6e8488SAndroid Build Coastguard Worker
950*5a6e8488SAndroid Build Coastguard WorkerThe naming format for all files is:
951*5a6e8488SAndroid Build Coastguard Worker
952*5a6e8488SAndroid Build Coastguard Worker```
953*5a6e8488SAndroid Build Coastguard Worker<language_code>_<country_code>.<encoding>.msg
954*5a6e8488SAndroid Build Coastguard Worker```
955*5a6e8488SAndroid Build Coastguard Worker
956*5a6e8488SAndroid Build Coastguard WorkerThis naming format must be followed for all locale files.
957*5a6e8488SAndroid Build Coastguard Worker
958*5a6e8488SAndroid Build Coastguard Worker### `manuals/`
959*5a6e8488SAndroid Build Coastguard Worker
960*5a6e8488SAndroid Build Coastguard WorkerThis folder contains the documentation for `bc`, `dc`, and [`bcl`][156], along
961*5a6e8488SAndroid Build Coastguard Workerwith a few other manuals.
962*5a6e8488SAndroid Build Coastguard Worker
963*5a6e8488SAndroid Build Coastguard Worker#### `algorithms.md`
964*5a6e8488SAndroid Build Coastguard Worker
965*5a6e8488SAndroid Build Coastguard WorkerThis file explains the mathematical algorithms that are used.
966*5a6e8488SAndroid Build Coastguard Worker
967*5a6e8488SAndroid Build Coastguard WorkerThe hope is that this file will guide people in understanding how the math code
968*5a6e8488SAndroid Build Coastguard Workerworks.
969*5a6e8488SAndroid Build Coastguard Worker
970*5a6e8488SAndroid Build Coastguard Worker#### `bc.1.md.in`
971*5a6e8488SAndroid Build Coastguard Worker
972*5a6e8488SAndroid Build Coastguard WorkerThis file is a template for the markdown version of the `bc` manual and
973*5a6e8488SAndroid Build Coastguard Workermanpages.
974*5a6e8488SAndroid Build Coastguard Worker
975*5a6e8488SAndroid Build Coastguard WorkerFor more information about how the manpages and markdown manuals are generated,
976*5a6e8488SAndroid Build Coastguard Workerand for why, see [`scripts/manpage.sh`][60] and [Manuals][86].
977*5a6e8488SAndroid Build Coastguard Worker
978*5a6e8488SAndroid Build Coastguard Worker#### `bcl.3`
979*5a6e8488SAndroid Build Coastguard Worker
980*5a6e8488SAndroid Build Coastguard WorkerThis is the manpage for the [`bcl`][156] library. It is generated from
981*5a6e8488SAndroid Build Coastguard Worker[`bcl.3.md`][61] using [`scripts/manpage.sh`][60].
982*5a6e8488SAndroid Build Coastguard Worker
983*5a6e8488SAndroid Build Coastguard WorkerFor the reason why I check generated data into the repo, see
984*5a6e8488SAndroid Build Coastguard Worker[`scripts/manpage.sh`][60] and [Manuals][86].
985*5a6e8488SAndroid Build Coastguard Worker
986*5a6e8488SAndroid Build Coastguard Worker#### `bcl.3.md`
987*5a6e8488SAndroid Build Coastguard Worker
988*5a6e8488SAndroid Build Coastguard WorkerThis is the markdown manual for the [`bcl`][156] library. It is the source for the
989*5a6e8488SAndroid Build Coastguard Workergenerated [`bcl.3`][62] file.
990*5a6e8488SAndroid Build Coastguard Worker
991*5a6e8488SAndroid Build Coastguard Worker#### `benchmarks.md`
992*5a6e8488SAndroid Build Coastguard Worker
993*5a6e8488SAndroid Build Coastguard WorkerThis is a document that compares this `bc` to GNU `bc` in various benchmarks. It
994*5a6e8488SAndroid Build Coastguard Workerwas last updated when version [`3.0.0`][32] was released.
995*5a6e8488SAndroid Build Coastguard Worker
996*5a6e8488SAndroid Build Coastguard WorkerIt has very little documentation value, other than showing what compiler options
997*5a6e8488SAndroid Build Coastguard Workerare useful for performance.
998*5a6e8488SAndroid Build Coastguard Worker
999*5a6e8488SAndroid Build Coastguard Worker#### `build.md`
1000*5a6e8488SAndroid Build Coastguard Worker
1001*5a6e8488SAndroid Build Coastguard WorkerThis is the [build manual][14].
1002*5a6e8488SAndroid Build Coastguard Worker
1003*5a6e8488SAndroid Build Coastguard WorkerThis `bc` has a custom build system. The reason for this is because of
1004*5a6e8488SAndroid Build Coastguard Worker[*portability*][136].
1005*5a6e8488SAndroid Build Coastguard Worker
1006*5a6e8488SAndroid Build Coastguard WorkerIf `bc` used an outside build system, that build system would be an external
1007*5a6e8488SAndroid Build Coastguard Workerdependency. Thus, I had to write a build system for `bc` that used nothing but
1008*5a6e8488SAndroid Build Coastguard WorkerC99 and POSIX utilities, including barebones [POSIX `make`][74].
1009*5a6e8488SAndroid Build Coastguard Worker
1010*5a6e8488SAndroid Build Coastguard Workerfor more information about the build system, see the [build system][142]
1011*5a6e8488SAndroid Build Coastguard Workersection, the [build manual][14], [`configure.sh`][69], and [`Makefile.in`][70].
1012*5a6e8488SAndroid Build Coastguard Worker
1013*5a6e8488SAndroid Build Coastguard Worker#### `dc.1.md.in`
1014*5a6e8488SAndroid Build Coastguard Worker
1015*5a6e8488SAndroid Build Coastguard WorkerThis file is a template for the markdown version of the `dc` manual and
1016*5a6e8488SAndroid Build Coastguard Workermanpages.
1017*5a6e8488SAndroid Build Coastguard Worker
1018*5a6e8488SAndroid Build Coastguard WorkerFor more information about how the manpages and markdown manuals are generated,
1019*5a6e8488SAndroid Build Coastguard Workerand for why, see [`scripts/manpage.sh`][60] and [Manuals][86].
1020*5a6e8488SAndroid Build Coastguard Worker
1021*5a6e8488SAndroid Build Coastguard Worker#### `development.md`
1022*5a6e8488SAndroid Build Coastguard Worker
1023*5a6e8488SAndroid Build Coastguard WorkerThe file you are reading right now.
1024*5a6e8488SAndroid Build Coastguard Worker
1025*5a6e8488SAndroid Build Coastguard Worker#### `header_bcl.txt`
1026*5a6e8488SAndroid Build Coastguard Worker
1027*5a6e8488SAndroid Build Coastguard WorkerUsed by [`scripts/manpage.sh`][60] to give the [`bcl.3`][62] manpage a proper
1028*5a6e8488SAndroid Build Coastguard Workerheader.
1029*5a6e8488SAndroid Build Coastguard Worker
1030*5a6e8488SAndroid Build Coastguard WorkerFor more information about generating manuals, see [`scripts/manpage.sh`][60]
1031*5a6e8488SAndroid Build Coastguard Workerand [Manuals][86].
1032*5a6e8488SAndroid Build Coastguard Worker
1033*5a6e8488SAndroid Build Coastguard Worker#### `header_bc.txt`
1034*5a6e8488SAndroid Build Coastguard Worker
1035*5a6e8488SAndroid Build Coastguard WorkerUsed by [`scripts/manpage.sh`][60] to give the [generated `bc` manpages][79] a
1036*5a6e8488SAndroid Build Coastguard Workerproper header.
1037*5a6e8488SAndroid Build Coastguard Worker
1038*5a6e8488SAndroid Build Coastguard WorkerFor more information about generating manuals, see [`scripts/manpage.sh`][60]
1039*5a6e8488SAndroid Build Coastguard Workerand [Manuals][86].
1040*5a6e8488SAndroid Build Coastguard Worker
1041*5a6e8488SAndroid Build Coastguard Worker#### `header_dc.txt`
1042*5a6e8488SAndroid Build Coastguard Worker
1043*5a6e8488SAndroid Build Coastguard WorkerUsed by [`scripts/manpage.sh`][60] to give the [generated `dc` manpages][80] a
1044*5a6e8488SAndroid Build Coastguard Workerproper header.
1045*5a6e8488SAndroid Build Coastguard Worker
1046*5a6e8488SAndroid Build Coastguard WorkerFor more information about generating manuals, see [`scripts/manpage.sh`][60]
1047*5a6e8488SAndroid Build Coastguard Workerand [Manuals][86].
1048*5a6e8488SAndroid Build Coastguard Worker
1049*5a6e8488SAndroid Build Coastguard Worker#### `header.txt`
1050*5a6e8488SAndroid Build Coastguard Worker
1051*5a6e8488SAndroid Build Coastguard WorkerUsed by [`scripts/manpage.sh`][60] to give all generated manpages a license
1052*5a6e8488SAndroid Build Coastguard Workerheader.
1053*5a6e8488SAndroid Build Coastguard Worker
1054*5a6e8488SAndroid Build Coastguard WorkerFor more information about generating manuals, see [`scripts/manpage.sh`][60]
1055*5a6e8488SAndroid Build Coastguard Workerand [Manuals][86].
1056*5a6e8488SAndroid Build Coastguard Worker
1057*5a6e8488SAndroid Build Coastguard Worker#### `release.md`
1058*5a6e8488SAndroid Build Coastguard Worker
1059*5a6e8488SAndroid Build Coastguard WorkerA checklist that I try to somewhat follow when making a release.
1060*5a6e8488SAndroid Build Coastguard Worker
1061*5a6e8488SAndroid Build Coastguard Worker#### `bc/`
1062*5a6e8488SAndroid Build Coastguard Worker
1063*5a6e8488SAndroid Build Coastguard WorkerA folder containing the `bc` manuals.
1064*5a6e8488SAndroid Build Coastguard Worker
1065*5a6e8488SAndroid Build Coastguard WorkerEach `bc` manual corresponds to a [build type][81]. See that link for more
1066*5a6e8488SAndroid Build Coastguard Workerdetails.
1067*5a6e8488SAndroid Build Coastguard Worker
1068*5a6e8488SAndroid Build Coastguard WorkerFor each manual, there are two copies: the markdown version generated from the
1069*5a6e8488SAndroid Build Coastguard Workertemplate, and the manpage generated from the markdown version.
1070*5a6e8488SAndroid Build Coastguard Worker
1071*5a6e8488SAndroid Build Coastguard Worker#### `dc/`
1072*5a6e8488SAndroid Build Coastguard Worker
1073*5a6e8488SAndroid Build Coastguard WorkerA folder containing the `dc` manuals.
1074*5a6e8488SAndroid Build Coastguard Worker
1075*5a6e8488SAndroid Build Coastguard WorkerEach `dc` manual corresponds to a [build type][81]. See that link for more
1076*5a6e8488SAndroid Build Coastguard Workerdetails.
1077*5a6e8488SAndroid Build Coastguard Worker
1078*5a6e8488SAndroid Build Coastguard WorkerFor each manual, there are two copies: the markdown version generated from the
1079*5a6e8488SAndroid Build Coastguard Workertemplate, and the manpage generated from the markdown version.
1080*5a6e8488SAndroid Build Coastguard Worker
1081*5a6e8488SAndroid Build Coastguard Worker### `scripts/`
1082*5a6e8488SAndroid Build Coastguard Worker
1083*5a6e8488SAndroid Build Coastguard WorkerThis folder contains helper scripts. Most of them are written in pure [POSIX
1084*5a6e8488SAndroid Build Coastguard Worker`sh`][72], but three ([`afl.py`][94], [`karatsuba.py`][78], and
1085*5a6e8488SAndroid Build Coastguard Worker[`randmath.py`][95]) are written in Python 3, and one ([`ministat.c`][223]) is
1086*5a6e8488SAndroid Build Coastguard Workerwritten in C. [`ministat.c`][223] in particular is copied from elsewhere.
1087*5a6e8488SAndroid Build Coastguard Worker
1088*5a6e8488SAndroid Build Coastguard WorkerFor more information about the shell scripts, see [POSIX Shell Scripts][76].
1089*5a6e8488SAndroid Build Coastguard Worker
1090*5a6e8488SAndroid Build Coastguard Worker#### `afl.py`
1091*5a6e8488SAndroid Build Coastguard Worker
1092*5a6e8488SAndroid Build Coastguard WorkerThis script is meant to be used as part of the fuzzing workflow.
1093*5a6e8488SAndroid Build Coastguard Worker
1094*5a6e8488SAndroid Build Coastguard WorkerIt does one of two things: checks for valid crashes, or runs `bc` and or `dc`
1095*5a6e8488SAndroid Build Coastguard Workerunder all of the paths found by [AFL++][125].
1096*5a6e8488SAndroid Build Coastguard Worker
1097*5a6e8488SAndroid Build Coastguard WorkerSee [Fuzzing][82] for more information about fuzzing, including this script.
1098*5a6e8488SAndroid Build Coastguard Worker
1099*5a6e8488SAndroid Build Coastguard Worker#### `alloc.sh`
1100*5a6e8488SAndroid Build Coastguard Worker
1101*5a6e8488SAndroid Build Coastguard WorkerThis script is a quick and dirty script to test whether or not the garbage
1102*5a6e8488SAndroid Build Coastguard Workercollection mechanism of the [`BcNum` caching][96] works. It has been little-used
1103*5a6e8488SAndroid Build Coastguard Workerbecause it tests something that is not important to correctness.
1104*5a6e8488SAndroid Build Coastguard Worker
1105*5a6e8488SAndroid Build Coastguard Worker#### `benchmark.sh`
1106*5a6e8488SAndroid Build Coastguard Worker
1107*5a6e8488SAndroid Build Coastguard WorkerA script making it easy to run benchmarks and to run the executable produced by
1108*5a6e8488SAndroid Build Coastguard Worker[`ministat.c`][223] on them.
1109*5a6e8488SAndroid Build Coastguard Worker
1110*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [Benchmarks][144] section.
1111*5a6e8488SAndroid Build Coastguard Worker
1112*5a6e8488SAndroid Build Coastguard Worker#### `bitfuncgen.c`
1113*5a6e8488SAndroid Build Coastguard Worker
1114*5a6e8488SAndroid Build Coastguard WorkerA source file for an executable to generate tests for `bc`'s bitwise functions
1115*5a6e8488SAndroid Build Coastguard Workerin [`gen/lib2.bc`][26]. The executable is `scripts/bitfuncgen`, and it is built
1116*5a6e8488SAndroid Build Coastguard Workerwith `make bitfuncgen`. It produces the test on `stdout` and the expected
1117*5a6e8488SAndroid Build Coastguard Workerresults on `stderr`. This means that to generat tests, use the following
1118*5a6e8488SAndroid Build Coastguard Workerinvokation:
1119*5a6e8488SAndroid Build Coastguard Worker
1120*5a6e8488SAndroid Build Coastguard Worker```
1121*5a6e8488SAndroid Build Coastguard Workerscripts/bitfuncgen > tests/bc/bitfuncs.txt 2> tests/bc/bitfuncs_results.txt
1122*5a6e8488SAndroid Build Coastguard Worker```
1123*5a6e8488SAndroid Build Coastguard Worker
1124*5a6e8488SAndroid Build Coastguard WorkerIt calls `abort()` if it runs into an error.
1125*5a6e8488SAndroid Build Coastguard Worker
1126*5a6e8488SAndroid Build Coastguard Worker#### `exec-install.sh`
1127*5a6e8488SAndroid Build Coastguard Worker
1128*5a6e8488SAndroid Build Coastguard WorkerThis script is the magic behind making sure `dc` is installed properly if it's
1129*5a6e8488SAndroid Build Coastguard Workera symlink to `bc`. It checks to see if it is a link, and if so, it just creates
1130*5a6e8488SAndroid Build Coastguard Workera new symlink in the install directory. Of course, it also installs `bc` itself,
1131*5a6e8488SAndroid Build Coastguard Workeror `dc` when it's alone.
1132*5a6e8488SAndroid Build Coastguard Worker
1133*5a6e8488SAndroid Build Coastguard Worker#### `functions.sh`
1134*5a6e8488SAndroid Build Coastguard Worker
1135*5a6e8488SAndroid Build Coastguard WorkerThis file is a bunch of common functions for most of the POSIX shell scripts. It
1136*5a6e8488SAndroid Build Coastguard Workeris not supposed to be run; instead, it is *sourced* by other POSIX shell
1137*5a6e8488SAndroid Build Coastguard Workerscripts, like so:
1138*5a6e8488SAndroid Build Coastguard Worker
1139*5a6e8488SAndroid Build Coastguard Worker```
1140*5a6e8488SAndroid Build Coastguard Worker. "$scriptdir/functions.sh"
1141*5a6e8488SAndroid Build Coastguard Worker```
1142*5a6e8488SAndroid Build Coastguard Worker
1143*5a6e8488SAndroid Build Coastguard Workeror the equivalent, depending on where the sourcing script is.
1144*5a6e8488SAndroid Build Coastguard Worker
1145*5a6e8488SAndroid Build Coastguard WorkerFor more information about the shell scripts, see [POSIX Shell Scripts][76].
1146*5a6e8488SAndroid Build Coastguard Worker
1147*5a6e8488SAndroid Build Coastguard Worker#### `fuzz_prep.sh`
1148*5a6e8488SAndroid Build Coastguard Worker
1149*5a6e8488SAndroid Build Coastguard WorkerFuzzing is a regular activity when I am preparing for a release.
1150*5a6e8488SAndroid Build Coastguard Worker
1151*5a6e8488SAndroid Build Coastguard WorkerThis script handles all the options and such for building a fuzzable binary.
1152*5a6e8488SAndroid Build Coastguard WorkerInstead of having to remember a bunch of options, I just put them in this script
1153*5a6e8488SAndroid Build Coastguard Workerand run the script when I want to fuzz.
1154*5a6e8488SAndroid Build Coastguard Worker
1155*5a6e8488SAndroid Build Coastguard WorkerFor more information about fuzzing, see [Fuzzing][82].
1156*5a6e8488SAndroid Build Coastguard Worker
1157*5a6e8488SAndroid Build Coastguard Worker#### `karatsuba.py`
1158*5a6e8488SAndroid Build Coastguard Worker
1159*5a6e8488SAndroid Build Coastguard WorkerThis script has at least one of two major differences from most of the other
1160*5a6e8488SAndroid Build Coastguard Workerscripts:
1161*5a6e8488SAndroid Build Coastguard Worker
1162*5a6e8488SAndroid Build Coastguard Worker* It's written in Python 3.
1163*5a6e8488SAndroid Build Coastguard Worker* It's meant for software packagers.
1164*5a6e8488SAndroid Build Coastguard Worker
1165*5a6e8488SAndroid Build Coastguard WorkerFor example, [`scripts/afl.py`][94] and [`scripts/randmath.py`][95] are both in
1166*5a6e8488SAndroid Build Coastguard WorkerPython 3, but they are not meant for the end user or software packagers and are
1167*5a6e8488SAndroid Build Coastguard Workernot included in source distributions. But this script is.
1168*5a6e8488SAndroid Build Coastguard Worker
1169*5a6e8488SAndroid Build Coastguard WorkerThis script breaks my rule of only POSIX utilities necessary for package
1170*5a6e8488SAndroid Build Coastguard Workermaintainers, but there's a very good reason for that: it's only meant to be run
1171*5a6e8488SAndroid Build Coastguard Worker*once* when the package is created for the first time, and maybe not even then.
1172*5a6e8488SAndroid Build Coastguard Worker
1173*5a6e8488SAndroid Build Coastguard WorkerYou see, this script does two things: it tests the Karatsuba implementation at
1174*5a6e8488SAndroid Build Coastguard Workervarious settings for `KARATSUBA_LEN`, and it figures out what the optimal
1175*5a6e8488SAndroid Build Coastguard Worker`KARATSUBA_LEN` is for the machine that it is running on.
1176*5a6e8488SAndroid Build Coastguard Worker
1177*5a6e8488SAndroid Build Coastguard WorkerPackage maintainers can use this script, when creating a package for this `bc`,
1178*5a6e8488SAndroid Build Coastguard Workerto figure out what is optimal for their users. Then they don't have to run it
1179*5a6e8488SAndroid Build Coastguard Workerever again. So this script only has to run on the packagers machine.
1180*5a6e8488SAndroid Build Coastguard Worker
1181*5a6e8488SAndroid Build Coastguard WorkerI tried to write the script in `sh`, by the way, and I finally accepted the
1182*5a6e8488SAndroid Build Coastguard Workertradeoff of using Python 3 when it became too hard.
1183*5a6e8488SAndroid Build Coastguard Worker
1184*5a6e8488SAndroid Build Coastguard WorkerHowever, I also mentioned that it's for testing Karatsuba with various settings
1185*5a6e8488SAndroid Build Coastguard Workerof `KARATSUBA_LEN`. Package maintainers will want to run the [test suite][124],
1186*5a6e8488SAndroid Build Coastguard Workerright?
1187*5a6e8488SAndroid Build Coastguard Worker
1188*5a6e8488SAndroid Build Coastguard WorkerYes, but this script is not part of the [test suite][124]; it's used for testing
1189*5a6e8488SAndroid Build Coastguard Workerin the [`scripts/release.sh`][83] script, which is maintainer use only.
1190*5a6e8488SAndroid Build Coastguard Worker
1191*5a6e8488SAndroid Build Coastguard WorkerHowever, there is one snare with `karatsuba.py`: I didn't want the user to have
1192*5a6e8488SAndroid Build Coastguard Workerto install any Python libraries to run it. Keep that in mind if you change it.
1193*5a6e8488SAndroid Build Coastguard Worker
1194*5a6e8488SAndroid Build Coastguard Worker#### `link.sh`
1195*5a6e8488SAndroid Build Coastguard Worker
1196*5a6e8488SAndroid Build Coastguard WorkerThis script is the magic behind making `dc` a symlink of `bc` when both
1197*5a6e8488SAndroid Build Coastguard Workercalculators are built.
1198*5a6e8488SAndroid Build Coastguard Worker
1199*5a6e8488SAndroid Build Coastguard Worker#### `locale_install.sh`
1200*5a6e8488SAndroid Build Coastguard Worker
1201*5a6e8488SAndroid Build Coastguard WorkerThis script does what its name says: it installs locales.
1202*5a6e8488SAndroid Build Coastguard Worker
1203*5a6e8488SAndroid Build Coastguard WorkerIt turns out that this is complicated.
1204*5a6e8488SAndroid Build Coastguard Worker
1205*5a6e8488SAndroid Build Coastguard WorkerThere is a magic environment variable, `$NLSPATH`, that tells you how and where
1206*5a6e8488SAndroid Build Coastguard Workeryou are supposed to install locales.
1207*5a6e8488SAndroid Build Coastguard Worker
1208*5a6e8488SAndroid Build Coastguard WorkerYes, *how*. And where.
1209*5a6e8488SAndroid Build Coastguard Worker
1210*5a6e8488SAndroid Build Coastguard WorkerBut now is not the place to rant about `$NLSPATH`. For more information on
1211*5a6e8488SAndroid Build Coastguard Workerlocales and `$NLSPATH`, see [Locales][85].
1212*5a6e8488SAndroid Build Coastguard Worker
1213*5a6e8488SAndroid Build Coastguard Worker#### `locale_uninstall.sh`
1214*5a6e8488SAndroid Build Coastguard Worker
1215*5a6e8488SAndroid Build Coastguard WorkerThis script does what its name says: it uninstalls locales.
1216*5a6e8488SAndroid Build Coastguard Worker
1217*5a6e8488SAndroid Build Coastguard WorkerThis is far less complicated than installing locales. I basically generate a
1218*5a6e8488SAndroid Build Coastguard Workerwildcard path and then list all paths that fit that wildcard. Then I delete each
1219*5a6e8488SAndroid Build Coastguard Workerone of those paths. Easy.
1220*5a6e8488SAndroid Build Coastguard Worker
1221*5a6e8488SAndroid Build Coastguard WorkerFor more information on locales, see [Locales][85].
1222*5a6e8488SAndroid Build Coastguard Worker
1223*5a6e8488SAndroid Build Coastguard Worker#### `manpage.sh`
1224*5a6e8488SAndroid Build Coastguard Worker
1225*5a6e8488SAndroid Build Coastguard WorkerThis script is the one that generates markdown manuals from a template and a
1226*5a6e8488SAndroid Build Coastguard Workermanpage from a markdown manual.
1227*5a6e8488SAndroid Build Coastguard Worker
1228*5a6e8488SAndroid Build Coastguard WorkerFor more information about generating manuals, see [Manuals][86].
1229*5a6e8488SAndroid Build Coastguard Worker
1230*5a6e8488SAndroid Build Coastguard Worker#### `ministat.c`
1231*5a6e8488SAndroid Build Coastguard Worker
1232*5a6e8488SAndroid Build Coastguard WorkerThis is a file copied [from FreeBSD][221] that calculates the standard
1233*5a6e8488SAndroid Build Coastguard Workerstatistical numbers, such as mean, average, and median, based on numbers
1234*5a6e8488SAndroid Build Coastguard Workerobtained from a file.
1235*5a6e8488SAndroid Build Coastguard Worker
1236*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [FreeBSD ministat(1) manpage][222].
1237*5a6e8488SAndroid Build Coastguard Worker
1238*5a6e8488SAndroid Build Coastguard WorkerThis file allows `bc` to build the `scripts/ministat` executable using the
1239*5a6e8488SAndroid Build Coastguard Workercommand `make ministat`, and this executable helps programmers evaluate the
1240*5a6e8488SAndroid Build Coastguard Workerresults of [benchmarks][144] more accurately.
1241*5a6e8488SAndroid Build Coastguard Worker
1242*5a6e8488SAndroid Build Coastguard Worker#### `package.sh`
1243*5a6e8488SAndroid Build Coastguard Worker
1244*5a6e8488SAndroid Build Coastguard WorkerThis script is what helps `bc` maintainers cut a release. It does the following:
1245*5a6e8488SAndroid Build Coastguard Worker
1246*5a6e8488SAndroid Build Coastguard Worker1.	Creates the appropriate `git` tag.
1247*5a6e8488SAndroid Build Coastguard Worker2.	Pushes the `git` tag.
1248*5a6e8488SAndroid Build Coastguard Worker3.	Copies the repo to a temp directory.
1249*5a6e8488SAndroid Build Coastguard Worker4.	Removes files that should not be included in source distributions.
1250*5a6e8488SAndroid Build Coastguard Worker5.	Creates the tarballs.
1251*5a6e8488SAndroid Build Coastguard Worker6.	Signs the tarballs.
1252*5a6e8488SAndroid Build Coastguard Worker7.	Zips and signs the Windows executables if they exist.
1253*5a6e8488SAndroid Build Coastguard Worker8.	Calculates and outputs SHA512 and SHA256 sums for all of the files,
1254*5a6e8488SAndroid Build Coastguard Worker	including the signatures.
1255*5a6e8488SAndroid Build Coastguard Worker
1256*5a6e8488SAndroid Build Coastguard WorkerThis script is for `bc` maintainers to use when cutting a release. It is not
1257*5a6e8488SAndroid Build Coastguard Workermeant for outside use. This means that some non-POSIX utilities can be used,
1258*5a6e8488SAndroid Build Coastguard Workersuch as `git` and `gpg`.
1259*5a6e8488SAndroid Build Coastguard Worker
1260*5a6e8488SAndroid Build Coastguard WorkerIn addition, before using this script, it expects that the folders that Windows
1261*5a6e8488SAndroid Build Coastguard Workergenerated when building `bc`, `dc`, and [`bcl`][156], are in the parent
1262*5a6e8488SAndroid Build Coastguard Workerdirectory of the repo, exactly as Windows generated them. If they are not there,
1263*5a6e8488SAndroid Build Coastguard Workerthen it will not zip and sign, nor calculate sums of, the Windows executables.
1264*5a6e8488SAndroid Build Coastguard Worker
1265*5a6e8488SAndroid Build Coastguard WorkerBecause this script creates a tag and pushes it, it should *only* be run *ONCE*
1266*5a6e8488SAndroid Build Coastguard Workerper release.
1267*5a6e8488SAndroid Build Coastguard Worker
1268*5a6e8488SAndroid Build Coastguard Worker#### `radamsa.sh`
1269*5a6e8488SAndroid Build Coastguard Worker
1270*5a6e8488SAndroid Build Coastguard WorkerA script to test `bc`'s command-line expression parsing code, which, while
1271*5a6e8488SAndroid Build Coastguard Workersimple, strives to handle as much as possible.
1272*5a6e8488SAndroid Build Coastguard Worker
1273*5a6e8488SAndroid Build Coastguard WorkerWhat this script does is it uses the test cases in [`radamsa.txt`][98] an input
1274*5a6e8488SAndroid Build Coastguard Workerto the [Radamsa fuzzer][99].
1275*5a6e8488SAndroid Build Coastguard Worker
1276*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [Radamsa][128] section.
1277*5a6e8488SAndroid Build Coastguard Worker
1278*5a6e8488SAndroid Build Coastguard Worker#### `radamsa.txt`
1279*5a6e8488SAndroid Build Coastguard Worker
1280*5a6e8488SAndroid Build Coastguard WorkerInitial test cases for the [`radamsa.sh`][100] script.
1281*5a6e8488SAndroid Build Coastguard Worker
1282*5a6e8488SAndroid Build Coastguard Worker#### `randmath.py`
1283*5a6e8488SAndroid Build Coastguard Worker
1284*5a6e8488SAndroid Build Coastguard WorkerThis script generates random math problems and checks that `bc`'s and `dc`'s
1285*5a6e8488SAndroid Build Coastguard Workeroutput matches the GNU `bc` and `dc`. (For this reason, it is necessary to have
1286*5a6e8488SAndroid Build Coastguard WorkerGNU `bc` and `dc` installed before using this script.)
1287*5a6e8488SAndroid Build Coastguard Worker
1288*5a6e8488SAndroid Build Coastguard WorkerOne snare: be sure that this script is using the GNU `bc` and `dc`, not a
1289*5a6e8488SAndroid Build Coastguard Workerpreviously-installed version of this `bc` and `dc`.
1290*5a6e8488SAndroid Build Coastguard Worker
1291*5a6e8488SAndroid Build Coastguard WorkerIf you want to check for memory issues or failing asserts, you can build the
1292*5a6e8488SAndroid Build Coastguard Worker`bc` using `./scripts/fuzz_prep.sh -a`, and then run it under this script. Any
1293*5a6e8488SAndroid Build Coastguard Workererrors or crashes should be caught by the script and given to the user as part
1294*5a6e8488SAndroid Build Coastguard Workerof the "checklist" (see below).
1295*5a6e8488SAndroid Build Coastguard Worker
1296*5a6e8488SAndroid Build Coastguard WorkerThe basic idea behind this script is that it generates as many math problems as
1297*5a6e8488SAndroid Build Coastguard Workerit can, biasing towards situations that may be likely to have bugs, and testing
1298*5a6e8488SAndroid Build Coastguard Workereach math problem against GNU `bc` or `dc`.
1299*5a6e8488SAndroid Build Coastguard Worker
1300*5a6e8488SAndroid Build Coastguard WorkerIf GNU `bc` or `dc` fails, it just continues. If this `bc` or `dc` fails, it
1301*5a6e8488SAndroid Build Coastguard Workerstores that problem. If the output mismatches, it also stores the problem.
1302*5a6e8488SAndroid Build Coastguard Worker
1303*5a6e8488SAndroid Build Coastguard WorkerThen, when the user sends a `SIGINT`, the script stops testing and goes into
1304*5a6e8488SAndroid Build Coastguard Workerreport mode. One-by-one, it will go through the "checklist," the list of failed
1305*5a6e8488SAndroid Build Coastguard Workerproblems, and present each problem to the user, as well as whether this `bc` or
1306*5a6e8488SAndroid Build Coastguard Worker`dc` crashed, and its output versus GNU. Then the user can decide to add them as
1307*5a6e8488SAndroid Build Coastguard Workertest cases, which it does automatically to the appropriate test file.
1308*5a6e8488SAndroid Build Coastguard Worker
1309*5a6e8488SAndroid Build Coastguard Worker#### `release_settings.txt`
1310*5a6e8488SAndroid Build Coastguard Worker
1311*5a6e8488SAndroid Build Coastguard WorkerA text file of settings combinations that [`release.sh`][83] uses to ensure that
1312*5a6e8488SAndroid Build Coastguard Worker`bc` and `dc` build and work with various default settings. [`release.sh`][83]
1313*5a6e8488SAndroid Build Coastguard Workersimply reads it line by line and uses each line for one build.
1314*5a6e8488SAndroid Build Coastguard Worker
1315*5a6e8488SAndroid Build Coastguard Worker#### `release.sh`
1316*5a6e8488SAndroid Build Coastguard Worker
1317*5a6e8488SAndroid Build Coastguard WorkerThis script is for `bc` maintainers only. It runs `bc`, `dc`, and [`bcl`][156]
1318*5a6e8488SAndroid Build Coastguard Workerthrough a gauntlet that is mostly meant to be used in preparation for a release.
1319*5a6e8488SAndroid Build Coastguard Worker
1320*5a6e8488SAndroid Build Coastguard WorkerIt does the following:
1321*5a6e8488SAndroid Build Coastguard Worker
1322*5a6e8488SAndroid Build Coastguard Worker1.	Builds every [build type][81], with every setting combo in
1323*5a6e8488SAndroid Build Coastguard Worker	[`release_settings.txt`][93] with both calculators, `bc` alone, and `dc`
1324*5a6e8488SAndroid Build Coastguard Worker	alone.
1325*5a6e8488SAndroid Build Coastguard Worker2.	Builds every [build type][81], with every setting combo in
1326*5a6e8488SAndroid Build Coastguard Worker	[`release_settings.txt`][93] with both calculators, `bc` alone, and `dc`
1327*5a6e8488SAndroid Build Coastguard Worker	alone for 32-bit.
1328*5a6e8488SAndroid Build Coastguard Worker3.	Does #1 and #2 for Debug, Release, Release with Debug Info, and Min Size
1329*5a6e8488SAndroid Build Coastguard Worker	Release builds.
1330*5a6e8488SAndroid Build Coastguard Worker4.	Runs the [test suite][124] on every build, if desired.
1331*5a6e8488SAndroid Build Coastguard Worker5.	Runs the [test suite][124] under [ASan, UBSan, and MSan][21] for every build
1332*5a6e8488SAndroid Build Coastguard Worker	type/setting combo.
1333*5a6e8488SAndroid Build Coastguard Worker6.	Runs [`scripts/karatsuba.py`][78] in test mode.
1334*5a6e8488SAndroid Build Coastguard Worker7.	Runs the [test suite][124] for both calculators, `bc` alone, and `dc` alone
1335*5a6e8488SAndroid Build Coastguard Worker	under [valgrind][20] and errors if there are any memory bugs or memory
1336*5a6e8488SAndroid Build Coastguard Worker	leaks.
1337*5a6e8488SAndroid Build Coastguard Worker
1338*5a6e8488SAndroid Build Coastguard Worker#### `safe-install.sh`
1339*5a6e8488SAndroid Build Coastguard Worker
1340*5a6e8488SAndroid Build Coastguard WorkerA script copied from [musl][101] to atomically install files.
1341*5a6e8488SAndroid Build Coastguard Worker
1342*5a6e8488SAndroid Build Coastguard Worker#### `test_settings.sh`
1343*5a6e8488SAndroid Build Coastguard Worker
1344*5a6e8488SAndroid Build Coastguard WorkerA quick and dirty script to help automate rebuilding while manually testing the
1345*5a6e8488SAndroid Build Coastguard Workervarious default settings.
1346*5a6e8488SAndroid Build Coastguard Worker
1347*5a6e8488SAndroid Build Coastguard WorkerThis script uses [`test_settings.txt`][103] to generate the various settings
1348*5a6e8488SAndroid Build Coastguard Workercombos.
1349*5a6e8488SAndroid Build Coastguard Worker
1350*5a6e8488SAndroid Build Coastguard WorkerFor more information about settings, see [Settings][102] in the [build
1351*5a6e8488SAndroid Build Coastguard Workermanual][14].
1352*5a6e8488SAndroid Build Coastguard Worker
1353*5a6e8488SAndroid Build Coastguard Worker#### `test_settings.txt`
1354*5a6e8488SAndroid Build Coastguard Worker
1355*5a6e8488SAndroid Build Coastguard WorkerA list of the various settings combos to be used by [`test_settings.sh`][104].
1356*5a6e8488SAndroid Build Coastguard Worker
1357*5a6e8488SAndroid Build Coastguard Worker### `src/`
1358*5a6e8488SAndroid Build Coastguard Worker
1359*5a6e8488SAndroid Build Coastguard WorkerThis folder is, obviously, where the actual heart and soul of `bc`, the source
1360*5a6e8488SAndroid Build Coastguard Workercode, is.
1361*5a6e8488SAndroid Build Coastguard Worker
1362*5a6e8488SAndroid Build Coastguard WorkerAll of the source files are in one folder; this simplifies the build system
1363*5a6e8488SAndroid Build Coastguard Workerimmensely.
1364*5a6e8488SAndroid Build Coastguard Worker
1365*5a6e8488SAndroid Build Coastguard WorkerThere are separate files for `bc` and `dc` specific code ([`bc.c`][40],
1366*5a6e8488SAndroid Build Coastguard Worker[`bc_lex.c`][41], [`bc_parse.c`][42], [`dc.c`][44], [`dc_lex.c`][45], and
1367*5a6e8488SAndroid Build Coastguard Worker[`dc_parse.c`][46]) where possible because it is cleaner to exclude an entire
1368*5a6e8488SAndroid Build Coastguard Workersource file from a build than to have `#if`/`#endif` preprocessor guards.
1369*5a6e8488SAndroid Build Coastguard Worker
1370*5a6e8488SAndroid Build Coastguard WorkerThat said, it was easier in many cases to use preprocessor macros where both
1371*5a6e8488SAndroid Build Coastguard Workercalculators used much of the same code and data structures, so there is a
1372*5a6e8488SAndroid Build Coastguard Workerliberal sprinkling of them through the code.
1373*5a6e8488SAndroid Build Coastguard Worker
1374*5a6e8488SAndroid Build Coastguard Worker#### `args.c`
1375*5a6e8488SAndroid Build Coastguard Worker
1376*5a6e8488SAndroid Build Coastguard WorkerCode for processing command-line arguments.
1377*5a6e8488SAndroid Build Coastguard Worker
1378*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/args.h`][31].
1379*5a6e8488SAndroid Build Coastguard Worker
1380*5a6e8488SAndroid Build Coastguard Worker#### `bc.c`
1381*5a6e8488SAndroid Build Coastguard Worker
1382*5a6e8488SAndroid Build Coastguard WorkerThe code for the `bc` main function `bc_main()`.
1383*5a6e8488SAndroid Build Coastguard Worker
1384*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/bc.h`][106].
1385*5a6e8488SAndroid Build Coastguard Worker
1386*5a6e8488SAndroid Build Coastguard Worker#### `bc_lex.c`
1387*5a6e8488SAndroid Build Coastguard Worker
1388*5a6e8488SAndroid Build Coastguard WorkerThe code for lexing that only `bc` needs.
1389*5a6e8488SAndroid Build Coastguard Worker
1390*5a6e8488SAndroid Build Coastguard WorkerThe headers for this file are [`include/lex.h`][180] and [`include/bc.h`][106].
1391*5a6e8488SAndroid Build Coastguard Worker
1392*5a6e8488SAndroid Build Coastguard Worker#### `bc_parse.c`
1393*5a6e8488SAndroid Build Coastguard Worker
1394*5a6e8488SAndroid Build Coastguard WorkerThe code for parsing that only `bc` needs. This code is the most complex and
1395*5a6e8488SAndroid Build Coastguard Workersubtle in the entire codebase.
1396*5a6e8488SAndroid Build Coastguard Worker
1397*5a6e8488SAndroid Build Coastguard WorkerThe headers for this file are [`include/parse.h`][181] and
1398*5a6e8488SAndroid Build Coastguard Worker[`include/bc.h`][106].
1399*5a6e8488SAndroid Build Coastguard Worker
1400*5a6e8488SAndroid Build Coastguard Worker#### `data.c`
1401*5a6e8488SAndroid Build Coastguard Worker
1402*5a6e8488SAndroid Build Coastguard WorkerDue to [historical accident][23] because of a desire to get my `bc` into
1403*5a6e8488SAndroid Build Coastguard Worker[toybox][16], all of the constant data that `bc` needs is all in one file. This
1404*5a6e8488SAndroid Build Coastguard Workeris that file.
1405*5a6e8488SAndroid Build Coastguard Worker
1406*5a6e8488SAndroid Build Coastguard WorkerThere is no code in this file, but a lot of the const data has a heavy influence
1407*5a6e8488SAndroid Build Coastguard Workeron code, including the order of data in arrays because that order has to
1408*5a6e8488SAndroid Build Coastguard Workercorrespond to the order of other things elsewhere in the codebase. If you change
1409*5a6e8488SAndroid Build Coastguard Workerthe order of something in this file, run `make test`, and get errors, you
1410*5a6e8488SAndroid Build Coastguard Workerchanged something that depends on the order that you messed up.
1411*5a6e8488SAndroid Build Coastguard Worker
1412*5a6e8488SAndroid Build Coastguard WorkerAlmost all headers have `extern` references to items in this file.
1413*5a6e8488SAndroid Build Coastguard Worker
1414*5a6e8488SAndroid Build Coastguard Worker#### `dc.c`
1415*5a6e8488SAndroid Build Coastguard Worker
1416*5a6e8488SAndroid Build Coastguard WorkerThe code for the `dc` main function `dc_main()`.
1417*5a6e8488SAndroid Build Coastguard Worker
1418*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/dc.h`][182].
1419*5a6e8488SAndroid Build Coastguard Worker
1420*5a6e8488SAndroid Build Coastguard Worker#### `dc_lex.c`
1421*5a6e8488SAndroid Build Coastguard Worker
1422*5a6e8488SAndroid Build Coastguard WorkerThe code for lexing that only `dc` needs.
1423*5a6e8488SAndroid Build Coastguard Worker
1424*5a6e8488SAndroid Build Coastguard WorkerThe headers for this file are [`include/lex.h`][180] and [`include/dc.h`][182].
1425*5a6e8488SAndroid Build Coastguard Worker
1426*5a6e8488SAndroid Build Coastguard Worker#### `dc_parse.c`
1427*5a6e8488SAndroid Build Coastguard Worker
1428*5a6e8488SAndroid Build Coastguard WorkerThe code for parsing that only `dc` needs.
1429*5a6e8488SAndroid Build Coastguard Worker
1430*5a6e8488SAndroid Build Coastguard WorkerThe headers for this file are [`include/parse.h`][181] and
1431*5a6e8488SAndroid Build Coastguard Worker[`include/bc.h`][182].
1432*5a6e8488SAndroid Build Coastguard Worker
1433*5a6e8488SAndroid Build Coastguard Worker#### `file.c`
1434*5a6e8488SAndroid Build Coastguard Worker
1435*5a6e8488SAndroid Build Coastguard WorkerThe code for `bc`'s implementation of buffered I/O. For more information about
1436*5a6e8488SAndroid Build Coastguard Workerwhy I implemented my own buffered I/O, see [`include/file.h`][55], [Error
1437*5a6e8488SAndroid Build Coastguard WorkerHandling][97], and [Custom I/O][114], along with [`status.h`][176] and the notes
1438*5a6e8488SAndroid Build Coastguard Workerabout version [`3.0.0`][32] in the [`NEWS`][32].
1439*5a6e8488SAndroid Build Coastguard Worker
1440*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/file.h`][55].
1441*5a6e8488SAndroid Build Coastguard Worker
1442*5a6e8488SAndroid Build Coastguard Worker#### `history.c`
1443*5a6e8488SAndroid Build Coastguard Worker
1444*5a6e8488SAndroid Build Coastguard WorkerThe code for `bc`'s implementation of command-line editing/history, which is
1445*5a6e8488SAndroid Build Coastguard Workerbased on a [UTF-8-aware fork][28] of [`linenoise`][29].
1446*5a6e8488SAndroid Build Coastguard Worker
1447*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [Command-Line History][189] section.
1448*5a6e8488SAndroid Build Coastguard Worker
1449*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/history.h`][36].
1450*5a6e8488SAndroid Build Coastguard Worker
1451*5a6e8488SAndroid Build Coastguard Worker#### `lang.c`
1452*5a6e8488SAndroid Build Coastguard Worker
1453*5a6e8488SAndroid Build Coastguard WorkerThe data structures used for actual execution of `bc` and `dc` code.
1454*5a6e8488SAndroid Build Coastguard Worker
1455*5a6e8488SAndroid Build Coastguard WorkerWhile execution is done in [`src/program.c`][53], this file defines functions
1456*5a6e8488SAndroid Build Coastguard Workerfor initializing, copying, and freeing the data structures, which is somewhat
1457*5a6e8488SAndroid Build Coastguard Workerorthogonal to actual execution.
1458*5a6e8488SAndroid Build Coastguard Worker
1459*5a6e8488SAndroid Build Coastguard WorkerYes, it's misnamed; that's an accident of history where the first things I put
1460*5a6e8488SAndroid Build Coastguard Workerinto it all seemed related to the `bc` language.
1461*5a6e8488SAndroid Build Coastguard Worker
1462*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/lang.h`][38].
1463*5a6e8488SAndroid Build Coastguard Worker
1464*5a6e8488SAndroid Build Coastguard Worker#### `lex.c`
1465*5a6e8488SAndroid Build Coastguard Worker
1466*5a6e8488SAndroid Build Coastguard WorkerThe code for the common things that both programs need for lexing.
1467*5a6e8488SAndroid Build Coastguard Worker
1468*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/lex.h`][180].
1469*5a6e8488SAndroid Build Coastguard Worker
1470*5a6e8488SAndroid Build Coastguard Worker#### `library.c`
1471*5a6e8488SAndroid Build Coastguard Worker
1472*5a6e8488SAndroid Build Coastguard WorkerThe code to implement the public API of the `bcl` library.
1473*5a6e8488SAndroid Build Coastguard Worker
1474*5a6e8488SAndroid Build Coastguard WorkerThe code in this file does a lot to ensure that clients do not have to worry
1475*5a6e8488SAndroid Build Coastguard Workerabout internal `bc` details, especially error handling with `setjmp()` and
1476*5a6e8488SAndroid Build Coastguard Worker`longjmp()`. That and encapsulating the handling of numbers are the bulk of what
1477*5a6e8488SAndroid Build Coastguard Workerthe code in this file actually does because most of the library is still
1478*5a6e8488SAndroid Build Coastguard Workerimplemented in [`src/num.c`][39].
1479*5a6e8488SAndroid Build Coastguard Worker
1480*5a6e8488SAndroid Build Coastguard WorkerThe headers for this file are [`include/bcl.h`][30] and
1481*5a6e8488SAndroid Build Coastguard Worker[`include/library.h`][183].
1482*5a6e8488SAndroid Build Coastguard Worker
1483*5a6e8488SAndroid Build Coastguard Worker#### `main.c`
1484*5a6e8488SAndroid Build Coastguard Worker
1485*5a6e8488SAndroid Build Coastguard WorkerThe entry point for both programs; this is the `main()` function.
1486*5a6e8488SAndroid Build Coastguard Worker
1487*5a6e8488SAndroid Build Coastguard WorkerThis file has no headers associated with it.
1488*5a6e8488SAndroid Build Coastguard Worker
1489*5a6e8488SAndroid Build Coastguard Worker#### `num.c`
1490*5a6e8488SAndroid Build Coastguard Worker
1491*5a6e8488SAndroid Build Coastguard WorkerThe code for all of the arbitrary-precision [numbers][177] and [math][178] in
1492*5a6e8488SAndroid Build Coastguard Worker`bc`.
1493*5a6e8488SAndroid Build Coastguard Worker
1494*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/num.h`][184].
1495*5a6e8488SAndroid Build Coastguard Worker
1496*5a6e8488SAndroid Build Coastguard Worker#### `opt.c`
1497*5a6e8488SAndroid Build Coastguard Worker
1498*5a6e8488SAndroid Build Coastguard WorkerThe code for parsing command-line options.
1499*5a6e8488SAndroid Build Coastguard Worker
1500*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/opt.h`][35].
1501*5a6e8488SAndroid Build Coastguard Worker
1502*5a6e8488SAndroid Build Coastguard Worker#### `parse.c`
1503*5a6e8488SAndroid Build Coastguard Worker
1504*5a6e8488SAndroid Build Coastguard WorkerThe code for the common items that both programs need for parsing.
1505*5a6e8488SAndroid Build Coastguard Worker
1506*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/parse.h`][181].
1507*5a6e8488SAndroid Build Coastguard Worker
1508*5a6e8488SAndroid Build Coastguard Worker#### `program.c`
1509*5a6e8488SAndroid Build Coastguard Worker
1510*5a6e8488SAndroid Build Coastguard WorkerThe code for the actual execution engine for `bc` and `dc` code.
1511*5a6e8488SAndroid Build Coastguard Worker
1512*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/program.h`][57].
1513*5a6e8488SAndroid Build Coastguard Worker
1514*5a6e8488SAndroid Build Coastguard Worker#### `rand.c`
1515*5a6e8488SAndroid Build Coastguard Worker
1516*5a6e8488SAndroid Build Coastguard WorkerThe code for the [pseudo-random number generator (PRNG)][179] and the special
1517*5a6e8488SAndroid Build Coastguard Workerstack handling it needs.
1518*5a6e8488SAndroid Build Coastguard Worker
1519*5a6e8488SAndroid Build Coastguard WorkerThe PRNG only generates fixed-size integers. The magic of generating random
1520*5a6e8488SAndroid Build Coastguard Workernumbers of arbitrary size is actually given to the code that does math
1521*5a6e8488SAndroid Build Coastguard Worker([`src/num.c`][39]).
1522*5a6e8488SAndroid Build Coastguard Worker
1523*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/rand.h`][37].
1524*5a6e8488SAndroid Build Coastguard Worker
1525*5a6e8488SAndroid Build Coastguard Worker#### `read.c`
1526*5a6e8488SAndroid Build Coastguard Worker
1527*5a6e8488SAndroid Build Coastguard WorkerThe code for reading from files and `stdin`.
1528*5a6e8488SAndroid Build Coastguard Worker
1529*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/read.h`][185].
1530*5a6e8488SAndroid Build Coastguard Worker
1531*5a6e8488SAndroid Build Coastguard Worker#### `vector.c`
1532*5a6e8488SAndroid Build Coastguard Worker
1533*5a6e8488SAndroid Build Coastguard WorkerThe code for [vectors][111], [maps][186], and [slab vectors][187], along with
1534*5a6e8488SAndroid Build Coastguard Workerslabs.
1535*5a6e8488SAndroid Build Coastguard Worker
1536*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/vector.h`][174].
1537*5a6e8488SAndroid Build Coastguard Worker
1538*5a6e8488SAndroid Build Coastguard Worker#### `vm.c`
1539*5a6e8488SAndroid Build Coastguard Worker
1540*5a6e8488SAndroid Build Coastguard WorkerThe code for setting up and running `bc` and `dc`.
1541*5a6e8488SAndroid Build Coastguard Worker
1542*5a6e8488SAndroid Build Coastguard WorkerIt is so named because I think of it as the "virtual machine" of `bc`, though
1543*5a6e8488SAndroid Build Coastguard Workerthat is probably not true as [`program.h`][57] is probably the "virtual machine"
1544*5a6e8488SAndroid Build Coastguard Workercode. Thus, the name is more historical accident.
1545*5a6e8488SAndroid Build Coastguard Worker
1546*5a6e8488SAndroid Build Coastguard WorkerThe header for this file is [`include/vm.h`][27].
1547*5a6e8488SAndroid Build Coastguard Worker
1548*5a6e8488SAndroid Build Coastguard Worker### `tests/`
1549*5a6e8488SAndroid Build Coastguard Worker
1550*5a6e8488SAndroid Build Coastguard WorkerThis directory contains the entire [test suite][124] and its infrastructure.
1551*5a6e8488SAndroid Build Coastguard Worker
1552*5a6e8488SAndroid Build Coastguard Worker#### `all.sh`
1553*5a6e8488SAndroid Build Coastguard Worker
1554*5a6e8488SAndroid Build Coastguard WorkerA convenience script for the `make run_all_tests` target (see the [Group
1555*5a6e8488SAndroid Build Coastguard WorkerTests][141] section for more information).
1556*5a6e8488SAndroid Build Coastguard Worker
1557*5a6e8488SAndroid Build Coastguard Worker#### `all.txt`
1558*5a6e8488SAndroid Build Coastguard Worker
1559*5a6e8488SAndroid Build Coastguard WorkerThe file with the names of the calculators. This is to make it easier for the
1560*5a6e8488SAndroid Build Coastguard Workertest scripts to know where the standard and other test directories are.
1561*5a6e8488SAndroid Build Coastguard Worker
1562*5a6e8488SAndroid Build Coastguard Worker#### `bcl.c`
1563*5a6e8488SAndroid Build Coastguard Worker
1564*5a6e8488SAndroid Build Coastguard WorkerThe test for the [`bcl`][156] API. For more information, see the [`bcl`
1565*5a6e8488SAndroid Build Coastguard WorkerTest][157] section.
1566*5a6e8488SAndroid Build Coastguard Worker
1567*5a6e8488SAndroid Build Coastguard Worker#### `error.sh`
1568*5a6e8488SAndroid Build Coastguard Worker
1569*5a6e8488SAndroid Build Coastguard WorkerThe script to run the file-based error tests in `tests/<calculator>/errors/` for
1570*5a6e8488SAndroid Build Coastguard Workereach calculator. For more information, see the [Error Tests][151] section.
1571*5a6e8488SAndroid Build Coastguard Worker
1572*5a6e8488SAndroid Build Coastguard WorkerThis is a separate script so that each error file can be run separately and in
1573*5a6e8488SAndroid Build Coastguard Workerparallel.
1574*5a6e8488SAndroid Build Coastguard Worker
1575*5a6e8488SAndroid Build Coastguard Worker#### `errors.sh`
1576*5a6e8488SAndroid Build Coastguard Worker
1577*5a6e8488SAndroid Build Coastguard WorkerThe script to run the line-based error tests in `tests/<calculator>/errors.txt`
1578*5a6e8488SAndroid Build Coastguard Workerfor each calculator. For more information, see the [Error Tests][151] section.
1579*5a6e8488SAndroid Build Coastguard Worker
1580*5a6e8488SAndroid Build Coastguard Worker#### `extra_required.txt`
1581*5a6e8488SAndroid Build Coastguard Worker
1582*5a6e8488SAndroid Build Coastguard WorkerThe file with the list of tests which both calculators have that need the [Extra
1583*5a6e8488SAndroid Build Coastguard WorkerMath build option][188]. This exists to make it easy for test scripts to skip
1584*5a6e8488SAndroid Build Coastguard Workerthose tests when the [Extra Math build option][188] is disabled.
1585*5a6e8488SAndroid Build Coastguard Worker
1586*5a6e8488SAndroid Build Coastguard Worker#### `history.py`
1587*5a6e8488SAndroid Build Coastguard Worker
1588*5a6e8488SAndroid Build Coastguard WorkerThe file with all of the history tests. For more information, see the [History
1589*5a6e8488SAndroid Build Coastguard WorkerTests][155] section.
1590*5a6e8488SAndroid Build Coastguard Worker
1591*5a6e8488SAndroid Build Coastguard Worker#### `history.sh`
1592*5a6e8488SAndroid Build Coastguard Worker
1593*5a6e8488SAndroid Build Coastguard WorkerThe script to integrate [`history.py`][139] into the build system in a portable
1594*5a6e8488SAndroid Build Coastguard Workerway, and to skip it if necessary.
1595*5a6e8488SAndroid Build Coastguard Worker
1596*5a6e8488SAndroid Build Coastguard WorkerThis script also re-runs the test three times if it fails. This is because
1597*5a6e8488SAndroid Build Coastguard Worker`pexpect` can be flaky at times.
1598*5a6e8488SAndroid Build Coastguard Worker
1599*5a6e8488SAndroid Build Coastguard Worker#### `other.sh`
1600*5a6e8488SAndroid Build Coastguard Worker
1601*5a6e8488SAndroid Build Coastguard WorkerThe script to run the "other" (miscellaneous) tests for each calculator. For
1602*5a6e8488SAndroid Build Coastguard Workermore information, see the [Other Tests][154] section.
1603*5a6e8488SAndroid Build Coastguard Worker
1604*5a6e8488SAndroid Build Coastguard Worker#### `read.sh`
1605*5a6e8488SAndroid Build Coastguard Worker
1606*5a6e8488SAndroid Build Coastguard WorkerThe script to run the read tests for each calculator. For more information, see
1607*5a6e8488SAndroid Build Coastguard Workerthe [`read()` Tests][153] section.
1608*5a6e8488SAndroid Build Coastguard Worker
1609*5a6e8488SAndroid Build Coastguard Worker#### `script.sed`
1610*5a6e8488SAndroid Build Coastguard Worker
1611*5a6e8488SAndroid Build Coastguard WorkerThe `sed` script to edit the output of GNU `bc` when generating script tests.
1612*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [Script Tests][150] section.
1613*5a6e8488SAndroid Build Coastguard Worker
1614*5a6e8488SAndroid Build Coastguard Worker#### `script.sh`
1615*5a6e8488SAndroid Build Coastguard Worker
1616*5a6e8488SAndroid Build Coastguard WorkerThe script for running one script test. For more information, see the [Script
1617*5a6e8488SAndroid Build Coastguard WorkerTests][150] section.
1618*5a6e8488SAndroid Build Coastguard Worker
1619*5a6e8488SAndroid Build Coastguard Worker#### `scripts.sh`
1620*5a6e8488SAndroid Build Coastguard Worker
1621*5a6e8488SAndroid Build Coastguard WorkerThe script to help the `make run_all_tests` (see the [Group Tests][141] section)
1622*5a6e8488SAndroid Build Coastguard Workerrun all of the script tests.
1623*5a6e8488SAndroid Build Coastguard Worker
1624*5a6e8488SAndroid Build Coastguard Worker#### `stdin.sh`
1625*5a6e8488SAndroid Build Coastguard Worker
1626*5a6e8488SAndroid Build Coastguard WorkerThe script to run the `stdin` tests for each calculator. For more information,
1627*5a6e8488SAndroid Build Coastguard Workersee the [`stdin` Tests][152] section.
1628*5a6e8488SAndroid Build Coastguard Worker
1629*5a6e8488SAndroid Build Coastguard Worker#### `test.sh`
1630*5a6e8488SAndroid Build Coastguard Worker
1631*5a6e8488SAndroid Build Coastguard WorkerThe script to run one standard test. For more information, see the [Standard
1632*5a6e8488SAndroid Build Coastguard WorkerTests][149] section.
1633*5a6e8488SAndroid Build Coastguard Worker
1634*5a6e8488SAndroid Build Coastguard Worker#### `bc/`
1635*5a6e8488SAndroid Build Coastguard Worker
1636*5a6e8488SAndroid Build Coastguard WorkerThe standard tests directory for `bc`. For more information, see the [Standard
1637*5a6e8488SAndroid Build Coastguard WorkerTests][149] section.
1638*5a6e8488SAndroid Build Coastguard Worker
1639*5a6e8488SAndroid Build Coastguard Worker##### `all.txt`
1640*5a6e8488SAndroid Build Coastguard Worker
1641*5a6e8488SAndroid Build Coastguard WorkerThe file to tell the build system and `make run_all_tests` (see the [Group
1642*5a6e8488SAndroid Build Coastguard WorkerTests][141] section) what standard tests to run for `bc`, as well as in what
1643*5a6e8488SAndroid Build Coastguard Workerorder.
1644*5a6e8488SAndroid Build Coastguard Worker
1645*5a6e8488SAndroid Build Coastguard WorkerThis file just lists the test names, one per line.
1646*5a6e8488SAndroid Build Coastguard Worker
1647*5a6e8488SAndroid Build Coastguard Worker##### `errors.txt`
1648*5a6e8488SAndroid Build Coastguard Worker
1649*5a6e8488SAndroid Build Coastguard WorkerThe initial error test file for `bc`. This file has one test per line. See the
1650*5a6e8488SAndroid Build Coastguard Worker[Error Tests][151] section for more information.
1651*5a6e8488SAndroid Build Coastguard Worker
1652*5a6e8488SAndroid Build Coastguard Worker##### `posix_errors.txt`
1653*5a6e8488SAndroid Build Coastguard Worker
1654*5a6e8488SAndroid Build Coastguard WorkerThe file of tests for POSIX compatibility for `bc`. This file has one test per
1655*5a6e8488SAndroid Build Coastguard Workerline. For more information, see the [Error Tests][151] section.
1656*5a6e8488SAndroid Build Coastguard Worker
1657*5a6e8488SAndroid Build Coastguard Worker##### `timeconst.sh`
1658*5a6e8488SAndroid Build Coastguard Worker
1659*5a6e8488SAndroid Build Coastguard WorkerThe script to run the `bc` tests that use the [Linux `timeconst.bc` script][6].
1660*5a6e8488SAndroid Build Coastguard WorkerFor more information, see the [Linux `timeconst.bc` Script][191]section.
1661*5a6e8488SAndroid Build Coastguard Worker
1662*5a6e8488SAndroid Build Coastguard Worker##### `errors/`
1663*5a6e8488SAndroid Build Coastguard Worker
1664*5a6e8488SAndroid Build Coastguard WorkerThe directory with error tests for `bc`, most discovered by AFL++ (see the
1665*5a6e8488SAndroid Build Coastguard Worker[Fuzzing][82] section). There is one test per file. For more information, see
1666*5a6e8488SAndroid Build Coastguard Workerthe [Error Tests][151] section.
1667*5a6e8488SAndroid Build Coastguard Worker
1668*5a6e8488SAndroid Build Coastguard Worker##### `scripts/`
1669*5a6e8488SAndroid Build Coastguard Worker
1670*5a6e8488SAndroid Build Coastguard WorkerThe script tests directory for `bc`. For more information, see the [Script
1671*5a6e8488SAndroid Build Coastguard WorkerTests][150] section.
1672*5a6e8488SAndroid Build Coastguard Worker
1673*5a6e8488SAndroid Build Coastguard Worker###### `all.txt`
1674*5a6e8488SAndroid Build Coastguard Worker
1675*5a6e8488SAndroid Build Coastguard WorkerA file to tell the build system and `make run_all_tests` (see the [Group
1676*5a6e8488SAndroid Build Coastguard WorkerTests][141] section) what script tests to run for `bc`, as well as in what
1677*5a6e8488SAndroid Build Coastguard Workerorder.
1678*5a6e8488SAndroid Build Coastguard Worker
1679*5a6e8488SAndroid Build Coastguard WorkerThis file just lists the test names, one per line.
1680*5a6e8488SAndroid Build Coastguard Worker
1681*5a6e8488SAndroid Build Coastguard Worker#### `dc/`
1682*5a6e8488SAndroid Build Coastguard Worker
1683*5a6e8488SAndroid Build Coastguard WorkerThe standard tests directory for `dc`. For more information, see the [Standard
1684*5a6e8488SAndroid Build Coastguard WorkerTests][149] section.
1685*5a6e8488SAndroid Build Coastguard Worker
1686*5a6e8488SAndroid Build Coastguard Worker##### `all.txt`
1687*5a6e8488SAndroid Build Coastguard Worker
1688*5a6e8488SAndroid Build Coastguard WorkerThe file to tell the build system and `make run_all_tests` (see the [Group
1689*5a6e8488SAndroid Build Coastguard WorkerTests][141] section) what standard tests to run for `dc`, as well as in what
1690*5a6e8488SAndroid Build Coastguard Workerorder.
1691*5a6e8488SAndroid Build Coastguard Worker
1692*5a6e8488SAndroid Build Coastguard WorkerThis file just lists the test names, one per line.
1693*5a6e8488SAndroid Build Coastguard Worker
1694*5a6e8488SAndroid Build Coastguard Worker##### `errors.txt`
1695*5a6e8488SAndroid Build Coastguard Worker
1696*5a6e8488SAndroid Build Coastguard WorkerThe initial error test file for `dc`. This file has one test per line. See the
1697*5a6e8488SAndroid Build Coastguard Worker[Error Tests][151] section for more information.
1698*5a6e8488SAndroid Build Coastguard Worker
1699*5a6e8488SAndroid Build Coastguard Worker##### `read_errors.txt`
1700*5a6e8488SAndroid Build Coastguard Worker
1701*5a6e8488SAndroid Build Coastguard WorkerThe file of tests errors with the `?` command (`read()` in `bc`). This file has
1702*5a6e8488SAndroid Build Coastguard Workerone test per line. See the [Error Tests][151] section for more information.
1703*5a6e8488SAndroid Build Coastguard Worker
1704*5a6e8488SAndroid Build Coastguard Worker##### `errors/`
1705*5a6e8488SAndroid Build Coastguard Worker
1706*5a6e8488SAndroid Build Coastguard WorkerThe directory with error tests for `dc`, most discovered by AFL++ (see the
1707*5a6e8488SAndroid Build Coastguard Worker[Fuzzing][82] section). There is one test per file. For more information, see
1708*5a6e8488SAndroid Build Coastguard Workerthe [Error Tests][151] section.
1709*5a6e8488SAndroid Build Coastguard Worker
1710*5a6e8488SAndroid Build Coastguard Worker##### `scripts/`
1711*5a6e8488SAndroid Build Coastguard Worker
1712*5a6e8488SAndroid Build Coastguard WorkerThe script tests directory for `dc`. For more information, see the [Script
1713*5a6e8488SAndroid Build Coastguard WorkerTests][150] section.
1714*5a6e8488SAndroid Build Coastguard Worker
1715*5a6e8488SAndroid Build Coastguard Worker###### `all.txt`
1716*5a6e8488SAndroid Build Coastguard Worker
1717*5a6e8488SAndroid Build Coastguard WorkerThe file to tell the build system and `make run_all_tests` (see the [Group
1718*5a6e8488SAndroid Build Coastguard WorkerTests][141] section) what script tests to run for `dc`, as well as in what
1719*5a6e8488SAndroid Build Coastguard Workerorder.
1720*5a6e8488SAndroid Build Coastguard Worker
1721*5a6e8488SAndroid Build Coastguard WorkerThis file just lists the test names, one per line.
1722*5a6e8488SAndroid Build Coastguard Worker
1723*5a6e8488SAndroid Build Coastguard Worker#### `fuzzing/`
1724*5a6e8488SAndroid Build Coastguard Worker
1725*5a6e8488SAndroid Build Coastguard WorkerThe directory containing the fuzzing infrastructure. For more information, see
1726*5a6e8488SAndroid Build Coastguard Workerthe [Fuzzing][82] section.
1727*5a6e8488SAndroid Build Coastguard Worker
1728*5a6e8488SAndroid Build Coastguard Worker##### `bc_afl_continue.yaml`
1729*5a6e8488SAndroid Build Coastguard Worker
1730*5a6e8488SAndroid Build Coastguard WorkerThe [`tmuxp`][123] config (for use with [`tmux`][122]) for easily restarting a
1731*5a6e8488SAndroid Build Coastguard Workerfuzz run. For more information, see the [Convenience][130] subsection of the
1732*5a6e8488SAndroid Build Coastguard Worker[Fuzzing][82] section.
1733*5a6e8488SAndroid Build Coastguard Worker
1734*5a6e8488SAndroid Build Coastguard Worker##### `bc_afl.yaml`
1735*5a6e8488SAndroid Build Coastguard Worker
1736*5a6e8488SAndroid Build Coastguard WorkerThe [`tmuxp`][123] config (for use with [`tmux`][122]) for easily starting a
1737*5a6e8488SAndroid Build Coastguard Workerfuzz run. For more information, see the [Convenience][130] subsection of the
1738*5a6e8488SAndroid Build Coastguard Worker[Fuzzing][82] section.
1739*5a6e8488SAndroid Build Coastguard Worker
1740*5a6e8488SAndroid Build Coastguard WorkerBe aware that this will delete all previous unsaved fuzzing tests in the output
1741*5a6e8488SAndroid Build Coastguard Workerdirectories.
1742*5a6e8488SAndroid Build Coastguard Worker
1743*5a6e8488SAndroid Build Coastguard Worker##### `bc_inputs1/`
1744*5a6e8488SAndroid Build Coastguard Worker
1745*5a6e8488SAndroid Build Coastguard WorkerThe fuzzing input directory for the first third of inputs for `bc`. For more
1746*5a6e8488SAndroid Build Coastguard Workerinformation, see the [Corpuses][192] subsection of the [Fuzzing][82] section.
1747*5a6e8488SAndroid Build Coastguard Worker
1748*5a6e8488SAndroid Build Coastguard Worker##### `bc_inputs2/`
1749*5a6e8488SAndroid Build Coastguard Worker
1750*5a6e8488SAndroid Build Coastguard WorkerThe fuzzing input directory for the second third of inputs for `bc`. For more
1751*5a6e8488SAndroid Build Coastguard Workerinformation, see the [Corpuses][192] subsection of the [Fuzzing][82] section.
1752*5a6e8488SAndroid Build Coastguard Worker
1753*5a6e8488SAndroid Build Coastguard Worker##### `bc_inputs3/`
1754*5a6e8488SAndroid Build Coastguard Worker
1755*5a6e8488SAndroid Build Coastguard WorkerThe fuzzing input directory for the third third of inputs for `bc`. For more
1756*5a6e8488SAndroid Build Coastguard Workerinformation, see the [Corpuses][192] subsection of the [Fuzzing][82] section.
1757*5a6e8488SAndroid Build Coastguard Worker
1758*5a6e8488SAndroid Build Coastguard Worker##### `dc_inputs/`
1759*5a6e8488SAndroid Build Coastguard Worker
1760*5a6e8488SAndroid Build Coastguard WorkerThe fuzzing input directory for the inputs for `dc`. For more information, see
1761*5a6e8488SAndroid Build Coastguard Workerthe [Corpuses][192] subsection of the [Fuzzing][82] section.
1762*5a6e8488SAndroid Build Coastguard Worker
1763*5a6e8488SAndroid Build Coastguard Worker### `vs/`
1764*5a6e8488SAndroid Build Coastguard Worker
1765*5a6e8488SAndroid Build Coastguard WorkerThe directory containing all of the materials needed to build `bc`, `dc`, and
1766*5a6e8488SAndroid Build Coastguard Worker`bcl` on Windows.
1767*5a6e8488SAndroid Build Coastguard Worker
1768*5a6e8488SAndroid Build Coastguard Worker#### `bcl.sln`
1769*5a6e8488SAndroid Build Coastguard Worker
1770*5a6e8488SAndroid Build Coastguard WorkerA Visual Studio solution file for [`bcl`][156]. This, along with
1771*5a6e8488SAndroid Build Coastguard Worker[`bcl.vcxproj`][63] and [`bcl.vcxproj.filters`][64] is what makes it possible to
1772*5a6e8488SAndroid Build Coastguard Workerbuild [`bcl`][156] on Windows.
1773*5a6e8488SAndroid Build Coastguard Worker
1774*5a6e8488SAndroid Build Coastguard Worker#### `bcl.vcxproj`
1775*5a6e8488SAndroid Build Coastguard Worker
1776*5a6e8488SAndroid Build Coastguard WorkerA Visual Studio project file for [`bcl`][156]. This, along with [`bcl.sln`][65]
1777*5a6e8488SAndroid Build Coastguard Workerand [`bcl.vcxproj.filters`][64] is what makes it possible to build [`bcl`][156]
1778*5a6e8488SAndroid Build Coastguard Workeron Windows.
1779*5a6e8488SAndroid Build Coastguard Worker
1780*5a6e8488SAndroid Build Coastguard Worker#### `bcl.vcxproj.filters`
1781*5a6e8488SAndroid Build Coastguard Worker
1782*5a6e8488SAndroid Build Coastguard WorkerA Visual Studio filters file for [`bcl`][156]. This, along with [`bcl.sln`][65]
1783*5a6e8488SAndroid Build Coastguard Workerand [`bcl.vcxproj`][63] is what makes it possible to build [`bcl`][156] on
1784*5a6e8488SAndroid Build Coastguard WorkerWindows.
1785*5a6e8488SAndroid Build Coastguard Worker
1786*5a6e8488SAndroid Build Coastguard Worker#### `bc.sln`
1787*5a6e8488SAndroid Build Coastguard Worker
1788*5a6e8488SAndroid Build Coastguard WorkerA Visual Studio solution file for `bc`. This, along with [`bc.vcxproj`][66]
1789*5a6e8488SAndroid Build Coastguard Workerand [`bc.vcxproj.filters`][67] is what makes it possible to build `bc` on
1790*5a6e8488SAndroid Build Coastguard WorkerWindows.
1791*5a6e8488SAndroid Build Coastguard Worker
1792*5a6e8488SAndroid Build Coastguard Worker#### `bc.vcxproj`
1793*5a6e8488SAndroid Build Coastguard Worker
1794*5a6e8488SAndroid Build Coastguard WorkerA Visual Studio project file for `bc`. This, along with [`bc.sln`][68] and
1795*5a6e8488SAndroid Build Coastguard Worker[`bc.vcxproj.filters`][67] is what makes it possible to build `bc` on Windows.
1796*5a6e8488SAndroid Build Coastguard Worker
1797*5a6e8488SAndroid Build Coastguard Worker#### `bc.vcxproj.filters`
1798*5a6e8488SAndroid Build Coastguard Worker
1799*5a6e8488SAndroid Build Coastguard WorkerA Visual Studio filters file for `bc`. This, along with [`bc.sln`][68] and
1800*5a6e8488SAndroid Build Coastguard Worker[`bc.vcxproj`][66] is what makes it possible to build `bc` on Windows.
1801*5a6e8488SAndroid Build Coastguard Worker
1802*5a6e8488SAndroid Build Coastguard Worker#### `tests/`
1803*5a6e8488SAndroid Build Coastguard Worker
1804*5a6e8488SAndroid Build Coastguard WorkerA directory of files to run tests on Windows.
1805*5a6e8488SAndroid Build Coastguard Worker
1806*5a6e8488SAndroid Build Coastguard Worker##### `tests_bc.bat`
1807*5a6e8488SAndroid Build Coastguard Worker
1808*5a6e8488SAndroid Build Coastguard WorkerA file to run basic `bc` tests on Windows. It expects that it will be run from
1809*5a6e8488SAndroid Build Coastguard Workerthe directory containing it, and it also expects a `bc.exe` in the same
1810*5a6e8488SAndroid Build Coastguard Workerdirectory.
1811*5a6e8488SAndroid Build Coastguard Worker
1812*5a6e8488SAndroid Build Coastguard Worker##### `tests_dc.bat`
1813*5a6e8488SAndroid Build Coastguard Worker
1814*5a6e8488SAndroid Build Coastguard WorkerA file to run basic `dc` tests on Windows. It expects that it will be run from
1815*5a6e8488SAndroid Build Coastguard Workerthe directory containing it, and it also expects a `bc.exe` in the same
1816*5a6e8488SAndroid Build Coastguard Workerdirectory.
1817*5a6e8488SAndroid Build Coastguard Worker
1818*5a6e8488SAndroid Build Coastguard Worker## Build System
1819*5a6e8488SAndroid Build Coastguard Worker
1820*5a6e8488SAndroid Build Coastguard WorkerThe build system is described in detail in the [build manual][14], so
1821*5a6e8488SAndroid Build Coastguard Workermaintainers should start there. This section, however, describes some parts of
1822*5a6e8488SAndroid Build Coastguard Workerthe build system that only maintainers will care about.
1823*5a6e8488SAndroid Build Coastguard Worker
1824*5a6e8488SAndroid Build Coastguard Worker### Clean Targets
1825*5a6e8488SAndroid Build Coastguard Worker
1826*5a6e8488SAndroid Build Coastguard Worker`bc` has a default `make clean` target that cleans up the build files. However,
1827*5a6e8488SAndroid Build Coastguard Workerbecause `bc`'s build system can generate many different types of files, there
1828*5a6e8488SAndroid Build Coastguard Workerare other clean targets that may be useful:
1829*5a6e8488SAndroid Build Coastguard Worker
1830*5a6e8488SAndroid Build Coastguard Worker* `make clean_gen` cleans the `gen/strgen` executable generated from
1831*5a6e8488SAndroid Build Coastguard Worker  [`gen/strgen.c`][15]. It has no prerequisites.
1832*5a6e8488SAndroid Build Coastguard Worker* `make clean` cleans object files, `*.cat` files (see the [Locales][85]
1833*5a6e8488SAndroid Build Coastguard Worker  section), executables, and files generated from text files in [`gen/`][145],
1834*5a6e8488SAndroid Build Coastguard Worker  including `gen/strgen` if it was built. So this has a prerequisite on
1835*5a6e8488SAndroid Build Coastguard Worker  `make clean_gen` in normal use.
1836*5a6e8488SAndroid Build Coastguard Worker* `make clean_benchmarks` cleans [benchmarks][144], including the `ministat`
1837*5a6e8488SAndroid Build Coastguard Worker  executable. It has no prerequisites.
1838*5a6e8488SAndroid Build Coastguard Worker* `make clean_config` cleans the generated `Makefile` and the manuals that
1839*5a6e8488SAndroid Build Coastguard Worker  [`configure.sh`][69] copied in preparation for install. It also depends on
1840*5a6e8488SAndroid Build Coastguard Worker  `make clean` and `make clean_benchmarks`, so it cleans those items too. This
1841*5a6e8488SAndroid Build Coastguard Worker  is the target that [`configure.sh`][69] uses before it does its work.
1842*5a6e8488SAndroid Build Coastguard Worker* `make clean_coverage` cleans the generated coverage files for the [test
1843*5a6e8488SAndroid Build Coastguard Worker  suite][124]'s [code coverage][146] capabilities. It has no prerequisites. This
1844*5a6e8488SAndroid Build Coastguard Worker  is useful if the code coverage tools are giving errors.
1845*5a6e8488SAndroid Build Coastguard Worker* `make clean_tests` cleans *everything*. It has prerequisites on all previous
1846*5a6e8488SAndroid Build Coastguard Worker  clean targets, but it also cleans all of the [generated tests][143].
1847*5a6e8488SAndroid Build Coastguard Worker
1848*5a6e8488SAndroid Build Coastguard WorkerWhen adding more generated files, you may need to add them to one of these
1849*5a6e8488SAndroid Build Coastguard Workertargets and/or add a target for them especially.
1850*5a6e8488SAndroid Build Coastguard Worker
1851*5a6e8488SAndroid Build Coastguard Worker### Preprocessor Macros
1852*5a6e8488SAndroid Build Coastguard Worker
1853*5a6e8488SAndroid Build Coastguard Worker`bc` and `dc` use *a lot* of preprocessor macros to ensure that each build type:
1854*5a6e8488SAndroid Build Coastguard Worker
1855*5a6e8488SAndroid Build Coastguard Worker* builds,
1856*5a6e8488SAndroid Build Coastguard Worker* works under the [test suite][124], and
1857*5a6e8488SAndroid Build Coastguard Worker* excludes as much code as possible from all builds.
1858*5a6e8488SAndroid Build Coastguard Worker
1859*5a6e8488SAndroid Build Coastguard WorkerThis section will explain the preprocessor style of `bc` and `dc`, as well as
1860*5a6e8488SAndroid Build Coastguard Workerprovide an explanation of the macros used.
1861*5a6e8488SAndroid Build Coastguard Worker
1862*5a6e8488SAndroid Build Coastguard Worker#### Style
1863*5a6e8488SAndroid Build Coastguard Worker
1864*5a6e8488SAndroid Build Coastguard WorkerThe style of macro use in `bc` is pretty straightforward: I avoid depending on
1865*5a6e8488SAndroid Build Coastguard Workermacro definitions and instead, I set defaults if the macro is not defined and
1866*5a6e8488SAndroid Build Coastguard Workerthen test the value if the macro with a plain `#if`.
1867*5a6e8488SAndroid Build Coastguard Worker
1868*5a6e8488SAndroid Build Coastguard Worker(Some examples of setting defaults are in [`include/status.h`][176], just above
1869*5a6e8488SAndroid Build Coastguard Workerthe definition of the `BcStatus` enum.)
1870*5a6e8488SAndroid Build Coastguard Worker
1871*5a6e8488SAndroid Build Coastguard WorkerIn other words, I use `#if` instead of `#ifndef` or `#ifdef`, where possible.
1872*5a6e8488SAndroid Build Coastguard Worker
1873*5a6e8488SAndroid Build Coastguard WorkerThere are a couple of cases where I went with standard stuff instead.
1874*5a6e8488SAndroid Build Coastguard Worker
1875*5a6e8488SAndroid Build Coastguard Worker#### Standard Macros
1876*5a6e8488SAndroid Build Coastguard Worker
1877*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLED`
1878*5a6e8488SAndroid Build Coastguard Worker
1879*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if `bc` is enabled, `0` if disabled.
1880*5a6e8488SAndroid Build Coastguard Worker
1881*5a6e8488SAndroid Build Coastguard Worker`DC_ENABLED`
1882*5a6e8488SAndroid Build Coastguard Worker
1883*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if `dc` is enabled, `0` if disabled.
1884*5a6e8488SAndroid Build Coastguard Worker
1885*5a6e8488SAndroid Build Coastguard Worker`BUILD_TYPE`
1886*5a6e8488SAndroid Build Coastguard Worker
1887*5a6e8488SAndroid Build Coastguard Worker:   The macro expands to the build type, which is one of: `A`, `E`, `H`, `N`,
1888*5a6e8488SAndroid Build Coastguard Worker    `EH`, `EN`, `HN`, `EHN`. This build type is used in the help text to direct
1889*5a6e8488SAndroid Build Coastguard Worker    the user to the correct markdown manual in the `git.gavinhoward.com`
1890*5a6e8488SAndroid Build Coastguard Worker    website.
1891*5a6e8488SAndroid Build Coastguard Worker
1892*5a6e8488SAndroid Build Coastguard Worker`EXECPREFIX`
1893*5a6e8488SAndroid Build Coastguard Worker
1894*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to the prefix on the executable name. This is used to
1895*5a6e8488SAndroid Build Coastguard Worker    allow `bc` and `dc` to skip the prefix when finding out which calculator is
1896*5a6e8488SAndroid Build Coastguard Worker    executing.
1897*5a6e8488SAndroid Build Coastguard Worker
1898*5a6e8488SAndroid Build Coastguard Worker`BC_NUM_KARATSUBA_LEN`
1899*5a6e8488SAndroid Build Coastguard Worker
1900*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to an integer, which is the length of numbers below which
1901*5a6e8488SAndroid Build Coastguard Worker    the Karatsuba multiplication algorithm switches to brute-force
1902*5a6e8488SAndroid Build Coastguard Worker    multiplication.
1903*5a6e8488SAndroid Build Coastguard Worker
1904*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLE_EXTRA_MATH`
1905*5a6e8488SAndroid Build Coastguard Worker
1906*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if the [Extra Math build option][188] is enabled,
1907*5a6e8488SAndroid Build Coastguard Worker    `0` if disabled.
1908*5a6e8488SAndroid Build Coastguard Worker
1909*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLE_HISTORY`
1910*5a6e8488SAndroid Build Coastguard Worker
1911*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if the [History build option][193] is enabled, `0`
1912*5a6e8488SAndroid Build Coastguard Worker    if disabled.
1913*5a6e8488SAndroid Build Coastguard Worker
1914*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLE_NLS`
1915*5a6e8488SAndroid Build Coastguard Worker
1916*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if the [NLS build option][193] (for locales) is
1917*5a6e8488SAndroid Build Coastguard Worker    enabled, `0` if disabled.
1918*5a6e8488SAndroid Build Coastguard Worker
1919*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLE_LIBRARY`
1920*5a6e8488SAndroid Build Coastguard Worker
1921*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if the [`bcl` library][156] is enabled, `0` if
1922*5a6e8488SAndroid Build Coastguard Worker    disabled. If this is enabled, building the calculators themselves is
1923*5a6e8488SAndroid Build Coastguard Worker    disabled, but both `BC_ENABLED` and `DC_ENABLED` must be non-zero.
1924*5a6e8488SAndroid Build Coastguard Worker
1925*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLE_MEMCHECK`
1926*5a6e8488SAndroid Build Coastguard Worker
1927*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if `bc` has been built for use with Valgrind's
1928*5a6e8488SAndroid Build Coastguard Worker    [Memcheck][194], `0` otherwise. This ensures that fatal errors still free
1929*5a6e8488SAndroid Build Coastguard Worker    all of their memory when exiting. `bc` does not do that normally because
1930*5a6e8488SAndroid Build Coastguard Worker    what's the point?
1931*5a6e8488SAndroid Build Coastguard Worker
1932*5a6e8488SAndroid Build Coastguard Worker`BC_ENABLE_AFL`
1933*5a6e8488SAndroid Build Coastguard Worker
1934*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to `1` if `bc` has been built for fuzzing with
1935*5a6e8488SAndroid Build Coastguard Worker    [AFL++][125], `0` otherwise. See the [Fuzzing][82] section for more
1936*5a6e8488SAndroid Build Coastguard Worker    information.
1937*5a6e8488SAndroid Build Coastguard Worker
1938*5a6e8488SAndroid Build Coastguard Worker`BC_DEFAULT_BANNER`
1939*5a6e8488SAndroid Build Coastguard Worker
1940*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to the default value for displaying the `bc` banner.
1941*5a6e8488SAndroid Build Coastguard Worker
1942*5a6e8488SAndroid Build Coastguard Worker`BC_DEFAULT_SIGINT_RESET`
1943*5a6e8488SAndroid Build Coastguard Worker
1944*5a6e8488SAndroid Build Coastguard Worker:   The macro expands to the default value for whether or not `bc` should reset
1945*5a6e8488SAndroid Build Coastguard Worker    on `SIGINT` or quit.
1946*5a6e8488SAndroid Build Coastguard Worker
1947*5a6e8488SAndroid Build Coastguard Worker`BC_DEFAULT_TTY_MODE`
1948*5a6e8488SAndroid Build Coastguard Worker
1949*5a6e8488SAndroid Build Coastguard Worker:   The macro expands to the default value for whether or not `bc` should use
1950*5a6e8488SAndroid Build Coastguard Worker    TTY mode when it available.
1951*5a6e8488SAndroid Build Coastguard Worker
1952*5a6e8488SAndroid Build Coastguard Worker`BC_DEFAULT_PROMPT`
1953*5a6e8488SAndroid Build Coastguard Worker
1954*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to the default value for whether or not `bc` should use a
1955*5a6e8488SAndroid Build Coastguard Worker    prompt when TTY mode is available.
1956*5a6e8488SAndroid Build Coastguard Worker
1957*5a6e8488SAndroid Build Coastguard Worker`DC_DEFAULT_SIGINT_RESET`
1958*5a6e8488SAndroid Build Coastguard Worker
1959*5a6e8488SAndroid Build Coastguard Worker:   The macro expands to the default value for whether or not `dc` should reset
1960*5a6e8488SAndroid Build Coastguard Worker    on `SIGINT` or quit.
1961*5a6e8488SAndroid Build Coastguard Worker
1962*5a6e8488SAndroid Build Coastguard Worker`DC_DEFAULT_TTY_MODE`
1963*5a6e8488SAndroid Build Coastguard Worker
1964*5a6e8488SAndroid Build Coastguard Worker:   The macro expands to the default value for whether or not `dc` should use
1965*5a6e8488SAndroid Build Coastguard Worker    TTY mode when it available.
1966*5a6e8488SAndroid Build Coastguard Worker
1967*5a6e8488SAndroid Build Coastguard Worker`DC_DEFAULT_PROMPT`
1968*5a6e8488SAndroid Build Coastguard Worker
1969*5a6e8488SAndroid Build Coastguard Worker:   This macro expands to the default value for whether or not `dc` should use a
1970*5a6e8488SAndroid Build Coastguard Worker    prompt when TTY mode is available.
1971*5a6e8488SAndroid Build Coastguard Worker
1972*5a6e8488SAndroid Build Coastguard Worker`BC_DEBUG_CODE`
1973*5a6e8488SAndroid Build Coastguard Worker
1974*5a6e8488SAndroid Build Coastguard Worker:   If this macro expands to a non-zero integer, then `bc` is built with *a lot*
1975*5a6e8488SAndroid Build Coastguard Worker    of extra debugging code. This is never set by the build system and must be
1976*5a6e8488SAndroid Build Coastguard Worker    set by the programmer manually. This should never be set in builds given to
1977*5a6e8488SAndroid Build Coastguard Worker    end users. For more information, see the [Debugging][134] section.
1978*5a6e8488SAndroid Build Coastguard Worker
1979*5a6e8488SAndroid Build Coastguard Worker## Test Suite
1980*5a6e8488SAndroid Build Coastguard Worker
1981*5a6e8488SAndroid Build Coastguard WorkerWhile the source code may be the heart and soul of `bc`, the test suite is the
1982*5a6e8488SAndroid Build Coastguard Workerarms and legs: it gives `bc` the power to do anything it needs to do.
1983*5a6e8488SAndroid Build Coastguard Worker
1984*5a6e8488SAndroid Build Coastguard WorkerThe test suite is what allowed `bc` to climb to such high heights of quality.
1985*5a6e8488SAndroid Build Coastguard WorkerThis even goes for fuzzing because fuzzing depends on the test suite for its
1986*5a6e8488SAndroid Build Coastguard Workerinput corpuses. (See the [Fuzzing][82] section.)
1987*5a6e8488SAndroid Build Coastguard Worker
1988*5a6e8488SAndroid Build Coastguard WorkerUnderstanding how the test suite works should be, I think, the first thing that
1989*5a6e8488SAndroid Build Coastguard Workermaintainers learn after learning what `bc` and `dc` should do. This is because
1990*5a6e8488SAndroid Build Coastguard Workerthe test suite, properly used, gives confidence that changes have not caused
1991*5a6e8488SAndroid Build Coastguard Workerbugs or regressions.
1992*5a6e8488SAndroid Build Coastguard Worker
1993*5a6e8488SAndroid Build Coastguard WorkerThat is why I spent the time to make the test suite as easy to use and as fast
1994*5a6e8488SAndroid Build Coastguard Workeras possible.
1995*5a6e8488SAndroid Build Coastguard Worker
1996*5a6e8488SAndroid Build Coastguard WorkerTo use the test suite (assuming `bc` and/or `dc` are already built), run the
1997*5a6e8488SAndroid Build Coastguard Workerfollowing command:
1998*5a6e8488SAndroid Build Coastguard Worker
1999*5a6e8488SAndroid Build Coastguard Worker```
2000*5a6e8488SAndroid Build Coastguard Workermake test
2001*5a6e8488SAndroid Build Coastguard Worker```
2002*5a6e8488SAndroid Build Coastguard Worker
2003*5a6e8488SAndroid Build Coastguard WorkerThat's it. That's all.
2004*5a6e8488SAndroid Build Coastguard Worker
2005*5a6e8488SAndroid Build Coastguard WorkerIt will return an error code if the test suite failed. It will also print out
2006*5a6e8488SAndroid Build Coastguard Workerinformation about the failure.
2007*5a6e8488SAndroid Build Coastguard Worker
2008*5a6e8488SAndroid Build Coastguard WorkerIf you want the test suite to go fast, then run the following command:
2009*5a6e8488SAndroid Build Coastguard Worker
2010*5a6e8488SAndroid Build Coastguard Worker```
2011*5a6e8488SAndroid Build Coastguard Workermake -j<cores> test
2012*5a6e8488SAndroid Build Coastguard Worker```
2013*5a6e8488SAndroid Build Coastguard Worker
2014*5a6e8488SAndroid Build Coastguard WorkerWhere `<cores>` is the number of cores that your computer has. Of course, this
2015*5a6e8488SAndroid Build Coastguard Workerrequires a `make` implementation that supports that option, but most do. (And I
2016*5a6e8488SAndroid Build Coastguard Workerwill use this convention throughout the rest of this section.)
2017*5a6e8488SAndroid Build Coastguard Worker
2018*5a6e8488SAndroid Build Coastguard WorkerI have even tried as much as possible, to put longer-running tests near the
2019*5a6e8488SAndroid Build Coastguard Workerbeginning of the run so that the entire suite runs as fast as possible.
2020*5a6e8488SAndroid Build Coastguard Worker
2021*5a6e8488SAndroid Build Coastguard WorkerHowever, if you want to be sure which test is failing, then running a bare
2022*5a6e8488SAndroid Build Coastguard Worker`make test` is a great way to do that.
2023*5a6e8488SAndroid Build Coastguard Worker
2024*5a6e8488SAndroid Build Coastguard WorkerBut enough about how you have no excuses to use the test suite as much as
2025*5a6e8488SAndroid Build Coastguard Workerpossible; let's talk about how it works and what you *can* do with it.
2026*5a6e8488SAndroid Build Coastguard Worker
2027*5a6e8488SAndroid Build Coastguard Worker### Standard Tests
2028*5a6e8488SAndroid Build Coastguard Worker
2029*5a6e8488SAndroid Build Coastguard WorkerThe heavy lifting of testing the math in `bc`, as well as basic scripting, is
2030*5a6e8488SAndroid Build Coastguard Workerdone by the "standard tests" for each calculator.
2031*5a6e8488SAndroid Build Coastguard Worker
2032*5a6e8488SAndroid Build Coastguard WorkerThese tests use the files in the [`tests/bc/`][161] and [`tests/dc/`][162]
2033*5a6e8488SAndroid Build Coastguard Workerdirectories (except for [`tests/bc/all.txt`][163], [`tests/bc/errors.txt`][164],
2034*5a6e8488SAndroid Build Coastguard Worker[`tests/bc/posix_errors.txt`][165], [`tests/bc/timeconst.sh`][166],
2035*5a6e8488SAndroid Build Coastguard Worker[`tests/dc/all.txt`][167], [`tests/dc/errors.txt`][168], and
2036*5a6e8488SAndroid Build Coastguard Worker[`tests/dc/read_errors.txt`][175]), which are called the "standard test
2037*5a6e8488SAndroid Build Coastguard Workerdirectories."
2038*5a6e8488SAndroid Build Coastguard Worker
2039*5a6e8488SAndroid Build Coastguard WorkerFor every test, there is the test file and the results file. The test files have
2040*5a6e8488SAndroid Build Coastguard Workernames of the form `<test>.txt`, where `<test>` is the name of the test, and the
2041*5a6e8488SAndroid Build Coastguard Workerresults files have names of the form `<test>_results.txt`.
2042*5a6e8488SAndroid Build Coastguard Worker
2043*5a6e8488SAndroid Build Coastguard WorkerIf the test file exists but the results file does not, the results for that test
2044*5a6e8488SAndroid Build Coastguard Workerare generated by a GNU-compatible `bc` or `dc`. See the [Generated Tests][143]
2045*5a6e8488SAndroid Build Coastguard Workersection.
2046*5a6e8488SAndroid Build Coastguard Worker
2047*5a6e8488SAndroid Build Coastguard WorkerThe `all.txt` file in each standard tests directory is what tells the test suite
2048*5a6e8488SAndroid Build Coastguard Workerand [build system][142] what tests there are, and the tests are either run in
2049*5a6e8488SAndroid Build Coastguard Workerthat order, or in the case of parallel `make`, that is the order that the
2050*5a6e8488SAndroid Build Coastguard Workertargets are listed as prerequisites of `make test`.
2051*5a6e8488SAndroid Build Coastguard Worker
2052*5a6e8488SAndroid Build Coastguard WorkerIf the test exists in the `all.txt` file but does not *actually* exist, the test
2053*5a6e8488SAndroid Build Coastguard Workerand its results are generated by a GNU-compatible `bc` or `dc`. See the
2054*5a6e8488SAndroid Build Coastguard Worker[Generated Tests][143] section.
2055*5a6e8488SAndroid Build Coastguard Worker
2056*5a6e8488SAndroid Build Coastguard WorkerTo add a non-generated standard test, do the following:
2057*5a6e8488SAndroid Build Coastguard Worker
2058*5a6e8488SAndroid Build Coastguard Worker* Add the test file (`<test>.txt` in the standard tests directory).
2059*5a6e8488SAndroid Build Coastguard Worker* Add the results file (`<test>_results.txt` in the standard tests directory).
2060*5a6e8488SAndroid Build Coastguard Worker  You can skip this step if just the results file needs to be generated. See the
2061*5a6e8488SAndroid Build Coastguard Worker  [Generated Tests][147] section for more information.
2062*5a6e8488SAndroid Build Coastguard Worker* Add the name of the test to the `all.txt` file in the standard tests
2063*5a6e8488SAndroid Build Coastguard Worker  directory, putting it in the order it should be in. If possible, I would put
2064*5a6e8488SAndroid Build Coastguard Worker  longer tests near the beginning because they will start running earlier with
2065*5a6e8488SAndroid Build Coastguard Worker  parallel `make`. I always keep `decimal` first, though, as a smoke test.
2066*5a6e8488SAndroid Build Coastguard Worker
2067*5a6e8488SAndroid Build Coastguard WorkerIf you need to add a generated standard test, see the [Generated Tests][147]
2068*5a6e8488SAndroid Build Coastguard Workersection for how to do that.
2069*5a6e8488SAndroid Build Coastguard Worker
2070*5a6e8488SAndroid Build Coastguard WorkerSome standard tests need to be skipped in certain cases. That is handled by the
2071*5a6e8488SAndroid Build Coastguard Worker[build system][142]. See the [Integration with the Build System][147] section
2072*5a6e8488SAndroid Build Coastguard Workerfor more details.
2073*5a6e8488SAndroid Build Coastguard Worker
2074*5a6e8488SAndroid Build Coastguard WorkerIn addition to all of the above, the standard test directory is not only the
2075*5a6e8488SAndroid Build Coastguard Workerdirectory for the standard tests of each calculator, it is also the parent
2076*5a6e8488SAndroid Build Coastguard Workerdirectory of all other test directories for each calculator.
2077*5a6e8488SAndroid Build Coastguard Worker
2078*5a6e8488SAndroid Build Coastguard Worker#### `bc` Standard Tests
2079*5a6e8488SAndroid Build Coastguard Worker
2080*5a6e8488SAndroid Build Coastguard WorkerThe list of current (27 February 2023) standard tests for `bc` is below:
2081*5a6e8488SAndroid Build Coastguard Worker
2082*5a6e8488SAndroid Build Coastguard Workerdecimal
2083*5a6e8488SAndroid Build Coastguard Worker
2084*5a6e8488SAndroid Build Coastguard Worker:   Tests decimal parsing and printing.
2085*5a6e8488SAndroid Build Coastguard Worker
2086*5a6e8488SAndroid Build Coastguard Workerprint
2087*5a6e8488SAndroid Build Coastguard Worker
2088*5a6e8488SAndroid Build Coastguard Worker:   Tests printing in every base from decimal. This is near the top for
2089*5a6e8488SAndroid Build Coastguard Worker    performance of parallel testing.
2090*5a6e8488SAndroid Build Coastguard Worker
2091*5a6e8488SAndroid Build Coastguard Workerparse
2092*5a6e8488SAndroid Build Coastguard Worker
2093*5a6e8488SAndroid Build Coastguard Worker:   Tests parsing in any base and outputting in decimal. This is near the top
2094*5a6e8488SAndroid Build Coastguard Worker    for performance of parallel testing.
2095*5a6e8488SAndroid Build Coastguard Worker
2096*5a6e8488SAndroid Build Coastguard Workerlib2
2097*5a6e8488SAndroid Build Coastguard Worker
2098*5a6e8488SAndroid Build Coastguard Worker:   Tests the extended math library. This is near the top for performance of
2099*5a6e8488SAndroid Build Coastguard Worker    parallel testing.
2100*5a6e8488SAndroid Build Coastguard Worker
2101*5a6e8488SAndroid Build Coastguard Workerprint2
2102*5a6e8488SAndroid Build Coastguard Worker
2103*5a6e8488SAndroid Build Coastguard Worker:   Tests printing at the extreme values of `obase`.
2104*5a6e8488SAndroid Build Coastguard Worker
2105*5a6e8488SAndroid Build Coastguard Workerlength
2106*5a6e8488SAndroid Build Coastguard Worker
2107*5a6e8488SAndroid Build Coastguard Worker:   Tests the `length()` builtin function.
2108*5a6e8488SAndroid Build Coastguard Worker
2109*5a6e8488SAndroid Build Coastguard Workerscale
2110*5a6e8488SAndroid Build Coastguard Worker
2111*5a6e8488SAndroid Build Coastguard Worker:   Tests the `scale()` builtin function.
2112*5a6e8488SAndroid Build Coastguard Worker
2113*5a6e8488SAndroid Build Coastguard Workershift
2114*5a6e8488SAndroid Build Coastguard Worker
2115*5a6e8488SAndroid Build Coastguard Worker:   Tests the left (`<<`) and right (`>>`) shift operators.
2116*5a6e8488SAndroid Build Coastguard Worker
2117*5a6e8488SAndroid Build Coastguard Workeradd
2118*5a6e8488SAndroid Build Coastguard Worker
2119*5a6e8488SAndroid Build Coastguard Worker:   Tests addition.
2120*5a6e8488SAndroid Build Coastguard Worker
2121*5a6e8488SAndroid Build Coastguard Workersubtract
2122*5a6e8488SAndroid Build Coastguard Worker
2123*5a6e8488SAndroid Build Coastguard Worker:   Tests subtraction.
2124*5a6e8488SAndroid Build Coastguard Worker
2125*5a6e8488SAndroid Build Coastguard Workermultiply
2126*5a6e8488SAndroid Build Coastguard Worker
2127*5a6e8488SAndroid Build Coastguard Worker:   Tests multiplication.
2128*5a6e8488SAndroid Build Coastguard Worker
2129*5a6e8488SAndroid Build Coastguard Workerdivide
2130*5a6e8488SAndroid Build Coastguard Worker
2131*5a6e8488SAndroid Build Coastguard Worker:   Tests division.
2132*5a6e8488SAndroid Build Coastguard Worker
2133*5a6e8488SAndroid Build Coastguard Workermodulus
2134*5a6e8488SAndroid Build Coastguard Worker
2135*5a6e8488SAndroid Build Coastguard Worker:   Tests modulus.
2136*5a6e8488SAndroid Build Coastguard Worker
2137*5a6e8488SAndroid Build Coastguard Workerpower
2138*5a6e8488SAndroid Build Coastguard Worker
2139*5a6e8488SAndroid Build Coastguard Worker:   Tests power (exponentiation).
2140*5a6e8488SAndroid Build Coastguard Worker
2141*5a6e8488SAndroid Build Coastguard Workersqrt
2142*5a6e8488SAndroid Build Coastguard Worker
2143*5a6e8488SAndroid Build Coastguard Worker:   Tests the `sqrt()` (square root) builtin function.
2144*5a6e8488SAndroid Build Coastguard Worker
2145*5a6e8488SAndroid Build Coastguard Workertrunc
2146*5a6e8488SAndroid Build Coastguard Worker
2147*5a6e8488SAndroid Build Coastguard Worker:   Tests the truncation (`$`) operator.
2148*5a6e8488SAndroid Build Coastguard Worker
2149*5a6e8488SAndroid Build Coastguard Workerplaces
2150*5a6e8488SAndroid Build Coastguard Worker
2151*5a6e8488SAndroid Build Coastguard Worker:   Tests the places (`@`) operator.
2152*5a6e8488SAndroid Build Coastguard Worker
2153*5a6e8488SAndroid Build Coastguard Workervars
2154*5a6e8488SAndroid Build Coastguard Worker
2155*5a6e8488SAndroid Build Coastguard Worker:   Tests some usage of variables. This one came from [AFL++][125] I think.
2156*5a6e8488SAndroid Build Coastguard Worker
2157*5a6e8488SAndroid Build Coastguard Workerboolean
2158*5a6e8488SAndroid Build Coastguard Worker
2159*5a6e8488SAndroid Build Coastguard Worker:   Tests boolean operators.
2160*5a6e8488SAndroid Build Coastguard Worker
2161*5a6e8488SAndroid Build Coastguard Workercomp
2162*5a6e8488SAndroid Build Coastguard Worker
2163*5a6e8488SAndroid Build Coastguard Worker:   Tests comparison operators.
2164*5a6e8488SAndroid Build Coastguard Worker
2165*5a6e8488SAndroid Build Coastguard Workerabs
2166*5a6e8488SAndroid Build Coastguard Worker
2167*5a6e8488SAndroid Build Coastguard Worker:   Tests the `abs()` builtin function.
2168*5a6e8488SAndroid Build Coastguard Worker
2169*5a6e8488SAndroid Build Coastguard Workerassignments
2170*5a6e8488SAndroid Build Coastguard Worker
2171*5a6e8488SAndroid Build Coastguard Worker:   Tests assignment operators, including increment/decrement operators.
2172*5a6e8488SAndroid Build Coastguard Worker
2173*5a6e8488SAndroid Build Coastguard Workerfunctions
2174*5a6e8488SAndroid Build Coastguard Worker
2175*5a6e8488SAndroid Build Coastguard Worker:   Tests functions, specifically function parameters being replaced before they
2176*5a6e8488SAndroid Build Coastguard Worker    themselves are used. See the comment in `bc_program_call()` about the last
2177*5a6e8488SAndroid Build Coastguard Worker    condition.
2178*5a6e8488SAndroid Build Coastguard Worker
2179*5a6e8488SAndroid Build Coastguard Workerscientific
2180*5a6e8488SAndroid Build Coastguard Worker
2181*5a6e8488SAndroid Build Coastguard Worker:   Tests scientific notation.
2182*5a6e8488SAndroid Build Coastguard Worker
2183*5a6e8488SAndroid Build Coastguard Workerengineering
2184*5a6e8488SAndroid Build Coastguard Worker
2185*5a6e8488SAndroid Build Coastguard Worker:   Tests engineering notation.
2186*5a6e8488SAndroid Build Coastguard Worker
2187*5a6e8488SAndroid Build Coastguard Workerglobals
2188*5a6e8488SAndroid Build Coastguard Worker
2189*5a6e8488SAndroid Build Coastguard Worker:   Tests that assigning to globals affects callers.
2190*5a6e8488SAndroid Build Coastguard Worker
2191*5a6e8488SAndroid Build Coastguard Workerstrings
2192*5a6e8488SAndroid Build Coastguard Worker
2193*5a6e8488SAndroid Build Coastguard Worker:   Tests strings.
2194*5a6e8488SAndroid Build Coastguard Worker
2195*5a6e8488SAndroid Build Coastguard Workerstrings2
2196*5a6e8488SAndroid Build Coastguard Worker
2197*5a6e8488SAndroid Build Coastguard Worker:   Tests string allocation in slabs, to ensure slabs work.
2198*5a6e8488SAndroid Build Coastguard Worker
2199*5a6e8488SAndroid Build Coastguard Workerletters
2200*5a6e8488SAndroid Build Coastguard Worker
2201*5a6e8488SAndroid Build Coastguard Worker:   Tests single and double letter numbers to ensure they behave differently.
2202*5a6e8488SAndroid Build Coastguard Worker    Single-letter numbers always be set to the same value, regardless of
2203*5a6e8488SAndroid Build Coastguard Worker    `ibase`.
2204*5a6e8488SAndroid Build Coastguard Worker
2205*5a6e8488SAndroid Build Coastguard Workerexponent
2206*5a6e8488SAndroid Build Coastguard Worker
2207*5a6e8488SAndroid Build Coastguard Worker:   Tests the `e()` function in the math library.
2208*5a6e8488SAndroid Build Coastguard Worker
2209*5a6e8488SAndroid Build Coastguard Workerlog
2210*5a6e8488SAndroid Build Coastguard Worker
2211*5a6e8488SAndroid Build Coastguard Worker:   Tests the `l()` function in the math library.
2212*5a6e8488SAndroid Build Coastguard Worker
2213*5a6e8488SAndroid Build Coastguard Workerpi
2214*5a6e8488SAndroid Build Coastguard Worker
2215*5a6e8488SAndroid Build Coastguard Worker:   Tests that `bc` produces the right value of pi for numbers with varying
2216*5a6e8488SAndroid Build Coastguard Worker    `scale` values.
2217*5a6e8488SAndroid Build Coastguard Worker
2218*5a6e8488SAndroid Build Coastguard Workerarctangent
2219*5a6e8488SAndroid Build Coastguard Worker
2220*5a6e8488SAndroid Build Coastguard Worker:   Tests the `a()` function in the math library.
2221*5a6e8488SAndroid Build Coastguard Worker
2222*5a6e8488SAndroid Build Coastguard Workersine
2223*5a6e8488SAndroid Build Coastguard Worker
2224*5a6e8488SAndroid Build Coastguard Worker:   Tests the `s()` function in the math library.
2225*5a6e8488SAndroid Build Coastguard Worker
2226*5a6e8488SAndroid Build Coastguard Workercosine
2227*5a6e8488SAndroid Build Coastguard Worker
2228*5a6e8488SAndroid Build Coastguard Worker:   Tests the `c()` function in the math library.
2229*5a6e8488SAndroid Build Coastguard Worker
2230*5a6e8488SAndroid Build Coastguard Workerbessel
2231*5a6e8488SAndroid Build Coastguard Worker
2232*5a6e8488SAndroid Build Coastguard Worker:   Tests the `j()` function in the math library.
2233*5a6e8488SAndroid Build Coastguard Worker
2234*5a6e8488SAndroid Build Coastguard Workerfib
2235*5a6e8488SAndroid Build Coastguard Worker
2236*5a6e8488SAndroid Build Coastguard Worker:   Tests the `fib()` Fibonacci function in the extended math library.
2237*5a6e8488SAndroid Build Coastguard Worker
2238*5a6e8488SAndroid Build Coastguard Workerarrays
2239*5a6e8488SAndroid Build Coastguard Worker
2240*5a6e8488SAndroid Build Coastguard Worker:   Test arrays.
2241*5a6e8488SAndroid Build Coastguard Worker
2242*5a6e8488SAndroid Build Coastguard Workermisc
2243*5a6e8488SAndroid Build Coastguard Worker
2244*5a6e8488SAndroid Build Coastguard Worker:   Miscellaneous tests. I named it this because at the time, I struggled to
2245*5a6e8488SAndroid Build Coastguard Worker    classify them, but it's really testing multi-line numbers.
2246*5a6e8488SAndroid Build Coastguard Worker
2247*5a6e8488SAndroid Build Coastguard Workermisc1
2248*5a6e8488SAndroid Build Coastguard Worker
2249*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2250*5a6e8488SAndroid Build Coastguard Worker
2251*5a6e8488SAndroid Build Coastguard Workermisc2
2252*5a6e8488SAndroid Build Coastguard Worker
2253*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2254*5a6e8488SAndroid Build Coastguard Worker
2255*5a6e8488SAndroid Build Coastguard Workermisc3
2256*5a6e8488SAndroid Build Coastguard Worker
2257*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2258*5a6e8488SAndroid Build Coastguard Worker
2259*5a6e8488SAndroid Build Coastguard Workermisc4
2260*5a6e8488SAndroid Build Coastguard Worker
2261*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2262*5a6e8488SAndroid Build Coastguard Worker
2263*5a6e8488SAndroid Build Coastguard Workermisc5
2264*5a6e8488SAndroid Build Coastguard Worker
2265*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2266*5a6e8488SAndroid Build Coastguard Worker
2267*5a6e8488SAndroid Build Coastguard Workermisc6
2268*5a6e8488SAndroid Build Coastguard Worker
2269*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2270*5a6e8488SAndroid Build Coastguard Worker
2271*5a6e8488SAndroid Build Coastguard Workermisc7
2272*5a6e8488SAndroid Build Coastguard Worker
2273*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test found by [AFL++][125].
2274*5a6e8488SAndroid Build Coastguard Worker
2275*5a6e8488SAndroid Build Coastguard Workervoid
2276*5a6e8488SAndroid Build Coastguard Worker
2277*5a6e8488SAndroid Build Coastguard Worker:   Tests void functions.
2278*5a6e8488SAndroid Build Coastguard Worker
2279*5a6e8488SAndroid Build Coastguard Workerrand
2280*5a6e8488SAndroid Build Coastguard Worker
2281*5a6e8488SAndroid Build Coastguard Worker:   Tests the pseudo-random number generator and its special stack handling.
2282*5a6e8488SAndroid Build Coastguard Worker
2283*5a6e8488SAndroid Build Coastguard Workerrand_limits
2284*5a6e8488SAndroid Build Coastguard Worker
2285*5a6e8488SAndroid Build Coastguard Worker:   Tests the limits of the pseudo-random number generator `irand()`.
2286*5a6e8488SAndroid Build Coastguard Worker
2287*5a6e8488SAndroid Build Coastguard Workerrecursive_arrays
2288*5a6e8488SAndroid Build Coastguard Worker
2289*5a6e8488SAndroid Build Coastguard Worker:   Tested the slab vector undo ability in used in `bc_parse_name()` when it
2290*5a6e8488SAndroid Build Coastguard Worker    existed. Now used as a stress test.
2291*5a6e8488SAndroid Build Coastguard Worker
2292*5a6e8488SAndroid Build Coastguard Workerdivmod
2293*5a6e8488SAndroid Build Coastguard Worker
2294*5a6e8488SAndroid Build Coastguard Worker:   Tests divmod.
2295*5a6e8488SAndroid Build Coastguard Worker
2296*5a6e8488SAndroid Build Coastguard Workermodexp
2297*5a6e8488SAndroid Build Coastguard Worker
2298*5a6e8488SAndroid Build Coastguard Worker:   Tests modular exponentiation.
2299*5a6e8488SAndroid Build Coastguard Worker
2300*5a6e8488SAndroid Build Coastguard Workerbitfuncs
2301*5a6e8488SAndroid Build Coastguard Worker
2302*5a6e8488SAndroid Build Coastguard Worker:   Tests the bitwise functions, `band()`, `bor()`, `bxor()`, `blshift()` and
2303*5a6e8488SAndroid Build Coastguard Worker    `brshift()` in [`gen/lib2.bc`][26].
2304*5a6e8488SAndroid Build Coastguard Worker
2305*5a6e8488SAndroid Build Coastguard Workerleadingzero
2306*5a6e8488SAndroid Build Coastguard Worker
2307*5a6e8488SAndroid Build Coastguard Worker:   Tests the leading zero functionality and the `plz*()` and `pnlz*()`
2308*5a6e8488SAndroid Build Coastguard Worker    functions in [`gen/lib2.bc`][26].
2309*5a6e8488SAndroid Build Coastguard Worker
2310*5a6e8488SAndroid Build Coastguard Workeris_number
2311*5a6e8488SAndroid Build Coastguard Worker
2312*5a6e8488SAndroid Build Coastguard Worker:   Tests the `is_number()` built-in function.
2313*5a6e8488SAndroid Build Coastguard Worker
2314*5a6e8488SAndroid Build Coastguard Workeris_string
2315*5a6e8488SAndroid Build Coastguard Worker
2316*5a6e8488SAndroid Build Coastguard Worker:   Tests the `is_number()` built-in function.
2317*5a6e8488SAndroid Build Coastguard Worker
2318*5a6e8488SAndroid Build Coastguard Workerasciify_array
2319*5a6e8488SAndroid Build Coastguard Worker
2320*5a6e8488SAndroid Build Coastguard Worker:   Tests the ability of `asciify()` to convert an array into a full string.
2321*5a6e8488SAndroid Build Coastguard Worker
2322*5a6e8488SAndroid Build Coastguard Workerline_by_line1
2323*5a6e8488SAndroid Build Coastguard Worker
2324*5a6e8488SAndroid Build Coastguard Worker:   Tests the line-by-line behavior of `bc` with regards to `quit` in a function
2325*5a6e8488SAndroid Build Coastguard Worker    definition.
2326*5a6e8488SAndroid Build Coastguard Worker
2327*5a6e8488SAndroid Build Coastguard Workerline_by_line2
2328*5a6e8488SAndroid Build Coastguard Worker
2329*5a6e8488SAndroid Build Coastguard Worker:   Tests the line-by-line behavior of `bc` with regards to `quit`.
2330*5a6e8488SAndroid Build Coastguard Worker
2331*5a6e8488SAndroid Build Coastguard Workerline_loop_quit1
2332*5a6e8488SAndroid Build Coastguard Worker
2333*5a6e8488SAndroid Build Coastguard Worker:   Tests the behavior of `bc` with a `quit` after a single-line loop.
2334*5a6e8488SAndroid Build Coastguard Worker
2335*5a6e8488SAndroid Build Coastguard Workerline_loop_quit2
2336*5a6e8488SAndroid Build Coastguard Worker
2337*5a6e8488SAndroid Build Coastguard Worker:   Tests the behavior of `bc` with a `quit` after a single-line loop and a
2338*5a6e8488SAndroid Build Coastguard Worker    newline escape.
2339*5a6e8488SAndroid Build Coastguard Worker
2340*5a6e8488SAndroid Build Coastguard Worker#### `dc` Standard Tests
2341*5a6e8488SAndroid Build Coastguard Worker
2342*5a6e8488SAndroid Build Coastguard WorkerThe list of current (27 February 2023) standard tests for `dc` is below:
2343*5a6e8488SAndroid Build Coastguard Worker
2344*5a6e8488SAndroid Build Coastguard Workerdecimal
2345*5a6e8488SAndroid Build Coastguard Worker
2346*5a6e8488SAndroid Build Coastguard Worker:   Tests decimal parsing and printing.
2347*5a6e8488SAndroid Build Coastguard Worker
2348*5a6e8488SAndroid Build Coastguard Workerlength
2349*5a6e8488SAndroid Build Coastguard Worker
2350*5a6e8488SAndroid Build Coastguard Worker:   Tests the `length()` builtin function, including for strings and arrays.
2351*5a6e8488SAndroid Build Coastguard Worker
2352*5a6e8488SAndroid Build Coastguard Workerstack_len
2353*5a6e8488SAndroid Build Coastguard Worker
2354*5a6e8488SAndroid Build Coastguard Worker:   Tests taking the length of the results stack.
2355*5a6e8488SAndroid Build Coastguard Worker
2356*5a6e8488SAndroid Build Coastguard Workerexec_stack_len
2357*5a6e8488SAndroid Build Coastguard Worker
2358*5a6e8488SAndroid Build Coastguard Worker:   Tests taking the length of the execution stack.
2359*5a6e8488SAndroid Build Coastguard Worker
2360*5a6e8488SAndroid Build Coastguard Workeradd
2361*5a6e8488SAndroid Build Coastguard Worker
2362*5a6e8488SAndroid Build Coastguard Worker:   Tests addition.
2363*5a6e8488SAndroid Build Coastguard Worker
2364*5a6e8488SAndroid Build Coastguard Workersubtract
2365*5a6e8488SAndroid Build Coastguard Worker
2366*5a6e8488SAndroid Build Coastguard Worker:   Tests subtraction.
2367*5a6e8488SAndroid Build Coastguard Worker
2368*5a6e8488SAndroid Build Coastguard Workermultiply
2369*5a6e8488SAndroid Build Coastguard Worker
2370*5a6e8488SAndroid Build Coastguard Worker:   Tests multiplication.
2371*5a6e8488SAndroid Build Coastguard Worker
2372*5a6e8488SAndroid Build Coastguard Workerdivide
2373*5a6e8488SAndroid Build Coastguard Worker
2374*5a6e8488SAndroid Build Coastguard Worker:   Tests division.
2375*5a6e8488SAndroid Build Coastguard Worker
2376*5a6e8488SAndroid Build Coastguard Workermodulus
2377*5a6e8488SAndroid Build Coastguard Worker
2378*5a6e8488SAndroid Build Coastguard Worker:   Tests modulus.
2379*5a6e8488SAndroid Build Coastguard Worker
2380*5a6e8488SAndroid Build Coastguard Workerdivmod
2381*5a6e8488SAndroid Build Coastguard Worker
2382*5a6e8488SAndroid Build Coastguard Worker:   Tests divmod.
2383*5a6e8488SAndroid Build Coastguard Worker
2384*5a6e8488SAndroid Build Coastguard Workerpower
2385*5a6e8488SAndroid Build Coastguard Worker
2386*5a6e8488SAndroid Build Coastguard Worker:   Tests power (exponentiation).
2387*5a6e8488SAndroid Build Coastguard Worker
2388*5a6e8488SAndroid Build Coastguard Workersqrt
2389*5a6e8488SAndroid Build Coastguard Worker
2390*5a6e8488SAndroid Build Coastguard Worker:   Tests the `sqrt()` (square root) builtin function.
2391*5a6e8488SAndroid Build Coastguard Worker
2392*5a6e8488SAndroid Build Coastguard Workermodexp
2393*5a6e8488SAndroid Build Coastguard Worker
2394*5a6e8488SAndroid Build Coastguard Worker:   Tests modular exponentiation.
2395*5a6e8488SAndroid Build Coastguard Worker
2396*5a6e8488SAndroid Build Coastguard Workerboolean
2397*5a6e8488SAndroid Build Coastguard Worker
2398*5a6e8488SAndroid Build Coastguard Worker:   Tests boolean operators.
2399*5a6e8488SAndroid Build Coastguard Worker
2400*5a6e8488SAndroid Build Coastguard Workernegate
2401*5a6e8488SAndroid Build Coastguard Worker
2402*5a6e8488SAndroid Build Coastguard Worker:   Tests negation as a command and as part of numbers.
2403*5a6e8488SAndroid Build Coastguard Worker
2404*5a6e8488SAndroid Build Coastguard Workertrunc
2405*5a6e8488SAndroid Build Coastguard Worker
2406*5a6e8488SAndroid Build Coastguard Worker:   Tests the truncation (`$`) operator.
2407*5a6e8488SAndroid Build Coastguard Worker
2408*5a6e8488SAndroid Build Coastguard Workerplaces
2409*5a6e8488SAndroid Build Coastguard Worker
2410*5a6e8488SAndroid Build Coastguard Worker:   Tests the places (`@`) operator.
2411*5a6e8488SAndroid Build Coastguard Worker
2412*5a6e8488SAndroid Build Coastguard Workershift
2413*5a6e8488SAndroid Build Coastguard Worker
2414*5a6e8488SAndroid Build Coastguard Worker:   Tests the left (`<<`) and right (`>>`) shift operators.
2415*5a6e8488SAndroid Build Coastguard Worker
2416*5a6e8488SAndroid Build Coastguard Workerabs
2417*5a6e8488SAndroid Build Coastguard Worker
2418*5a6e8488SAndroid Build Coastguard Worker:   Tests the `abs()` builtin function (the `b` command).
2419*5a6e8488SAndroid Build Coastguard Worker
2420*5a6e8488SAndroid Build Coastguard Workerscientific
2421*5a6e8488SAndroid Build Coastguard Worker
2422*5a6e8488SAndroid Build Coastguard Worker:   Tests scientific notation.
2423*5a6e8488SAndroid Build Coastguard Worker
2424*5a6e8488SAndroid Build Coastguard Workerengineering
2425*5a6e8488SAndroid Build Coastguard Worker
2426*5a6e8488SAndroid Build Coastguard Worker:   Tests engineering notation.
2427*5a6e8488SAndroid Build Coastguard Worker
2428*5a6e8488SAndroid Build Coastguard Workervars
2429*5a6e8488SAndroid Build Coastguard Worker
2430*5a6e8488SAndroid Build Coastguard Worker:   Tests some usage of variables. This one came from [AFL++][125] I think.
2431*5a6e8488SAndroid Build Coastguard Worker
2432*5a6e8488SAndroid Build Coastguard Workermisc
2433*5a6e8488SAndroid Build Coastguard Worker
2434*5a6e8488SAndroid Build Coastguard Worker:   Miscellaneous tests. I named it this because at the time, I struggled to
2435*5a6e8488SAndroid Build Coastguard Worker    classify them.
2436*5a6e8488SAndroid Build Coastguard Worker
2437*5a6e8488SAndroid Build Coastguard Workermisc1
2438*5a6e8488SAndroid Build Coastguard Worker
2439*5a6e8488SAndroid Build Coastguard Worker:   More miscellaneous tests. This used to be an error file
2440*5a6e8488SAndroid Build Coastguard Worker    (`tests/dc/errors/15.txt`) due to the presence of a invalid `u` character.
2441*5a6e8488SAndroid Build Coastguard Worker    However, starting with version `6.1.0`, `u` became a valid command
2442*5a6e8488SAndroid Build Coastguard Worker    (`is_number()`), so the error file became valid. It was checked manually and
2443*5a6e8488SAndroid Build Coastguard Worker    moved, and `tests/dc/errors/34.txt` became the new `tests/dc/errors/15.txt`.
2444*5a6e8488SAndroid Build Coastguard Worker
2445*5a6e8488SAndroid Build Coastguard Workerstrings
2446*5a6e8488SAndroid Build Coastguard Worker
2447*5a6e8488SAndroid Build Coastguard Worker:   Tests strings.
2448*5a6e8488SAndroid Build Coastguard Worker
2449*5a6e8488SAndroid Build Coastguard Workerrand
2450*5a6e8488SAndroid Build Coastguard Worker
2451*5a6e8488SAndroid Build Coastguard Worker:   Tests the pseudo-random number generator and its special stack handling.
2452*5a6e8488SAndroid Build Coastguard Worker
2453*5a6e8488SAndroid Build Coastguard Workeris_number
2454*5a6e8488SAndroid Build Coastguard Worker
2455*5a6e8488SAndroid Build Coastguard Worker:   Tests the `is_number()` built-in function (the `u` command).
2456*5a6e8488SAndroid Build Coastguard Worker
2457*5a6e8488SAndroid Build Coastguard Workeris_string
2458*5a6e8488SAndroid Build Coastguard Worker
2459*5a6e8488SAndroid Build Coastguard Worker:   Tests the `is_number()` built-in function (the `t` command).
2460*5a6e8488SAndroid Build Coastguard Worker
2461*5a6e8488SAndroid Build Coastguard Worker### Script Tests
2462*5a6e8488SAndroid Build Coastguard Worker
2463*5a6e8488SAndroid Build Coastguard WorkerThe heavy lifting of testing the scripting of `bc` is done by the "script tests"
2464*5a6e8488SAndroid Build Coastguard Workerfor each calculator.
2465*5a6e8488SAndroid Build Coastguard Worker
2466*5a6e8488SAndroid Build Coastguard WorkerThese tests use the files in the [`tests/bc/scripts/`][169] and
2467*5a6e8488SAndroid Build Coastguard Worker[`tests/dc/scripts/`][170] directories (except for
2468*5a6e8488SAndroid Build Coastguard Worker[`tests/bc/scripts/all.txt`][171] and [`tests/dc/scripts/all.txt`][172]), which
2469*5a6e8488SAndroid Build Coastguard Workerare called the "script test directories."
2470*5a6e8488SAndroid Build Coastguard Worker
2471*5a6e8488SAndroid Build Coastguard WorkerTo add a script test, do the following:
2472*5a6e8488SAndroid Build Coastguard Worker
2473*5a6e8488SAndroid Build Coastguard Worker* Add the test file (`<test>.bc` or `<test>.dc` in the script tests directory).
2474*5a6e8488SAndroid Build Coastguard Worker* Add the results file (`<test>.txt` in the script tests directory). You can
2475*5a6e8488SAndroid Build Coastguard Worker  skip this step if just the results file needs to be generated. See the
2476*5a6e8488SAndroid Build Coastguard Worker  [Generated Tests][147] section for more information.
2477*5a6e8488SAndroid Build Coastguard Worker* Add the name of the test to the `all.txt` file in the script tests directory,
2478*5a6e8488SAndroid Build Coastguard Worker  putting it in the order it should be in. If possible, I would put longer tests
2479*5a6e8488SAndroid Build Coastguard Worker  near the beginning because they will start running earlier with parallel
2480*5a6e8488SAndroid Build Coastguard Worker  `make`.
2481*5a6e8488SAndroid Build Coastguard Worker
2482*5a6e8488SAndroid Build Coastguard WorkerSome script tests need to be skipped in certain cases. That is handled by the
2483*5a6e8488SAndroid Build Coastguard Worker[build system][142]. See the [Integration with the Build System][147] section
2484*5a6e8488SAndroid Build Coastguard Workerfor more details.
2485*5a6e8488SAndroid Build Coastguard Worker
2486*5a6e8488SAndroid Build Coastguard WorkerAnother unique thing about the script tests, at least for `bc`: they test the
2487*5a6e8488SAndroid Build Coastguard Worker`-g` and `--global-stacks` flags. This means that all of the script tests for
2488*5a6e8488SAndroid Build Coastguard Worker`bc` are written assuming the `-g` flag was given on the command-line
2489*5a6e8488SAndroid Build Coastguard Worker
2490*5a6e8488SAndroid Build Coastguard WorkerThere is one extra piece of script tests: [`tests/script.sed`][190]. This `sed`
2491*5a6e8488SAndroid Build Coastguard Workerscript is used to remove an incompatibility with GNU `bc`.
2492*5a6e8488SAndroid Build Coastguard Worker
2493*5a6e8488SAndroid Build Coastguard WorkerIf there is only one more character to print at the end of `BC_LINE_LENGTH`, GNU
2494*5a6e8488SAndroid Build Coastguard Worker`bc` still prints a backslash+newline+digit combo. OpenBSD doesn't, which is
2495*5a6e8488SAndroid Build Coastguard Workercorrect according to my reading of the `bc` spec, so my `bc` doesn't as well.
2496*5a6e8488SAndroid Build Coastguard Worker
2497*5a6e8488SAndroid Build Coastguard WorkerThe `sed` script edits numbers that end with just one digit on a line by itself
2498*5a6e8488SAndroid Build Coastguard Workerto put it on the same line as others.
2499*5a6e8488SAndroid Build Coastguard Worker
2500*5a6e8488SAndroid Build Coastguard Worker#### `bc` Script Tests
2501*5a6e8488SAndroid Build Coastguard Worker
2502*5a6e8488SAndroid Build Coastguard WorkerThe list of current (27 February 2023) script tests for `bc` is below:
2503*5a6e8488SAndroid Build Coastguard Worker
2504*5a6e8488SAndroid Build Coastguard Workerprint.bc
2505*5a6e8488SAndroid Build Coastguard Worker
2506*5a6e8488SAndroid Build Coastguard Worker:   Tests printing even harder than the print standard test.
2507*5a6e8488SAndroid Build Coastguard Worker
2508*5a6e8488SAndroid Build Coastguard Workerprint2
2509*5a6e8488SAndroid Build Coastguard Worker
2510*5a6e8488SAndroid Build Coastguard Worker:   Tests printing at the extreme edge of line length in various bases.
2511*5a6e8488SAndroid Build Coastguard Worker
2512*5a6e8488SAndroid Build Coastguard Workermultiply.bc
2513*5a6e8488SAndroid Build Coastguard Worker
2514*5a6e8488SAndroid Build Coastguard Worker:   Tests multiplication even harder than the multiply standard test.
2515*5a6e8488SAndroid Build Coastguard Worker
2516*5a6e8488SAndroid Build Coastguard Workerdivide.bc
2517*5a6e8488SAndroid Build Coastguard Worker
2518*5a6e8488SAndroid Build Coastguard Worker:   Tests division even harder than the divide standard test.
2519*5a6e8488SAndroid Build Coastguard Worker
2520*5a6e8488SAndroid Build Coastguard Workersubtract.bc
2521*5a6e8488SAndroid Build Coastguard Worker
2522*5a6e8488SAndroid Build Coastguard Worker:   Tests subtraction even harder than the subtract standard test.
2523*5a6e8488SAndroid Build Coastguard Worker
2524*5a6e8488SAndroid Build Coastguard Workeradd.bc
2525*5a6e8488SAndroid Build Coastguard Worker
2526*5a6e8488SAndroid Build Coastguard Worker:   Tests addition even harder than the add standard test.
2527*5a6e8488SAndroid Build Coastguard Worker
2528*5a6e8488SAndroid Build Coastguard Workerparse.bc
2529*5a6e8488SAndroid Build Coastguard Worker
2530*5a6e8488SAndroid Build Coastguard Worker:   Tests parsing even harder than the parse standard test.
2531*5a6e8488SAndroid Build Coastguard Worker
2532*5a6e8488SAndroid Build Coastguard Workerroot.bc
2533*5a6e8488SAndroid Build Coastguard Worker
2534*5a6e8488SAndroid Build Coastguard Worker:   Tests that `root()` and `cbrt()` do not go into an infinite loop on a
2535*5a6e8488SAndroid Build Coastguard Worker    pathological case found by a user.
2536*5a6e8488SAndroid Build Coastguard Worker
2537*5a6e8488SAndroid Build Coastguard Workerarray.bc
2538*5a6e8488SAndroid Build Coastguard Worker
2539*5a6e8488SAndroid Build Coastguard Worker:   Tests arrays even harder than the arrays standard test.
2540*5a6e8488SAndroid Build Coastguard Worker
2541*5a6e8488SAndroid Build Coastguard Workerarray2.bc
2542*5a6e8488SAndroid Build Coastguard Worker
2543*5a6e8488SAndroid Build Coastguard Worker:   Implements a test where an array element is passed as a parameter to a
2544*5a6e8488SAndroid Build Coastguard Worker    function, and then another array is passed to a later parameter that is
2545*5a6e8488SAndroid Build Coastguard Worker    named the same as the first array. This was added because of a bug found
2546*5a6e8488SAndroid Build Coastguard Worker    while writing a script in bc.
2547*5a6e8488SAndroid Build Coastguard Worker
2548*5a6e8488SAndroid Build Coastguard Workeratan.bc
2549*5a6e8488SAndroid Build Coastguard Worker
2550*5a6e8488SAndroid Build Coastguard Worker:   Tests arctangent even harder than the arctangent standard test.
2551*5a6e8488SAndroid Build Coastguard Worker
2552*5a6e8488SAndroid Build Coastguard Workerbessel.bc
2553*5a6e8488SAndroid Build Coastguard Worker
2554*5a6e8488SAndroid Build Coastguard Worker:   Tests bessel even harder than the bessel standard test.
2555*5a6e8488SAndroid Build Coastguard Worker
2556*5a6e8488SAndroid Build Coastguard Workerfunctions.bc
2557*5a6e8488SAndroid Build Coastguard Worker
2558*5a6e8488SAndroid Build Coastguard Worker:   Tests functions even harder than the functions standard test.
2559*5a6e8488SAndroid Build Coastguard Worker
2560*5a6e8488SAndroid Build Coastguard Workerglobals.bc
2561*5a6e8488SAndroid Build Coastguard Worker
2562*5a6e8488SAndroid Build Coastguard Worker:   Tests global stacks directly.
2563*5a6e8488SAndroid Build Coastguard Worker
2564*5a6e8488SAndroid Build Coastguard Workerlen.bc
2565*5a6e8488SAndroid Build Coastguard Worker
2566*5a6e8488SAndroid Build Coastguard Worker:   Tests the `length()` builtin on arrays.
2567*5a6e8488SAndroid Build Coastguard Worker
2568*5a6e8488SAndroid Build Coastguard Workerrand.bc
2569*5a6e8488SAndroid Build Coastguard Worker
2570*5a6e8488SAndroid Build Coastguard Worker:   Tests the random number generator in the presence of global stacks.
2571*5a6e8488SAndroid Build Coastguard Worker
2572*5a6e8488SAndroid Build Coastguard Workerreferences.bc
2573*5a6e8488SAndroid Build Coastguard Worker
2574*5a6e8488SAndroid Build Coastguard Worker:   Tests functions with array reference parameters.
2575*5a6e8488SAndroid Build Coastguard Worker
2576*5a6e8488SAndroid Build Coastguard Workerscreen.bc
2577*5a6e8488SAndroid Build Coastguard Worker
2578*5a6e8488SAndroid Build Coastguard Worker:   A random script provided by an early user that he used to calculate the size
2579*5a6e8488SAndroid Build Coastguard Worker    of computer screens
2580*5a6e8488SAndroid Build Coastguard Worker
2581*5a6e8488SAndroid Build Coastguard Workerstrings2.bc
2582*5a6e8488SAndroid Build Coastguard Worker
2583*5a6e8488SAndroid Build Coastguard Worker:   Tests escaping in strings.
2584*5a6e8488SAndroid Build Coastguard Worker
2585*5a6e8488SAndroid Build Coastguard Workerifs.bc
2586*5a6e8488SAndroid Build Coastguard Worker
2587*5a6e8488SAndroid Build Coastguard Worker:   Tests proper ending of `if` statements without `else` statements.
2588*5a6e8488SAndroid Build Coastguard Worker
2589*5a6e8488SAndroid Build Coastguard Workerifs2.bc
2590*5a6e8488SAndroid Build Coastguard Worker
2591*5a6e8488SAndroid Build Coastguard Worker:   More tests proper ending of `if` statements without `else` statements.
2592*5a6e8488SAndroid Build Coastguard Worker
2593*5a6e8488SAndroid Build Coastguard Worker#### `dc` Script Tests
2594*5a6e8488SAndroid Build Coastguard Worker
2595*5a6e8488SAndroid Build Coastguard WorkerThe list of current (27 February 2023) script tests for `dc` is below:
2596*5a6e8488SAndroid Build Coastguard Worker
2597*5a6e8488SAndroid Build Coastguard Workerprime.dc
2598*5a6e8488SAndroid Build Coastguard Worker
2599*5a6e8488SAndroid Build Coastguard Worker:   Tests scripting by generating the first 100,000 primes.
2600*5a6e8488SAndroid Build Coastguard Worker
2601*5a6e8488SAndroid Build Coastguard Workerasciify.dc
2602*5a6e8488SAndroid Build Coastguard Worker
2603*5a6e8488SAndroid Build Coastguard Worker:   Tests the asciify command.
2604*5a6e8488SAndroid Build Coastguard Worker
2605*5a6e8488SAndroid Build Coastguard Workerstream.dc
2606*5a6e8488SAndroid Build Coastguard Worker
2607*5a6e8488SAndroid Build Coastguard Worker:   Tests the stream command.
2608*5a6e8488SAndroid Build Coastguard Worker
2609*5a6e8488SAndroid Build Coastguard Workerarray.dc
2610*5a6e8488SAndroid Build Coastguard Worker
2611*5a6e8488SAndroid Build Coastguard Worker:   Tests arrays.
2612*5a6e8488SAndroid Build Coastguard Worker
2613*5a6e8488SAndroid Build Coastguard Workerelse.dc
2614*5a6e8488SAndroid Build Coastguard Worker
2615*5a6e8488SAndroid Build Coastguard Worker:   Tests else clauses on conditional execution commands.
2616*5a6e8488SAndroid Build Coastguard Worker
2617*5a6e8488SAndroid Build Coastguard Workerfactorial.dc
2618*5a6e8488SAndroid Build Coastguard Worker
2619*5a6e8488SAndroid Build Coastguard Worker:   Tests scripting with factorial.
2620*5a6e8488SAndroid Build Coastguard Worker
2621*5a6e8488SAndroid Build Coastguard Workerloop.dc
2622*5a6e8488SAndroid Build Coastguard Worker
2623*5a6e8488SAndroid Build Coastguard Worker:   Tests scripting by implementing loops.
2624*5a6e8488SAndroid Build Coastguard Worker
2625*5a6e8488SAndroid Build Coastguard Workerquit.dc
2626*5a6e8488SAndroid Build Coastguard Worker
2627*5a6e8488SAndroid Build Coastguard Worker:   Tests the quit command in the presence of tail calls.
2628*5a6e8488SAndroid Build Coastguard Worker
2629*5a6e8488SAndroid Build Coastguard Workerweird.dc
2630*5a6e8488SAndroid Build Coastguard Worker
2631*5a6e8488SAndroid Build Coastguard Worker:   A miscellaneous test.
2632*5a6e8488SAndroid Build Coastguard Worker
2633*5a6e8488SAndroid Build Coastguard Workerno_clamp.dc
2634*5a6e8488SAndroid Build Coastguard Worker
2635*5a6e8488SAndroid Build Coastguard Worker:   A test to ensure `dc` has the same behavior as the BSD `dc` with digi
2636*5a6e8488SAndroid Build Coastguard Worker    clamping off when parsing numbers.
2637*5a6e8488SAndroid Build Coastguard Worker
2638*5a6e8488SAndroid Build Coastguard Worker### Error Tests
2639*5a6e8488SAndroid Build Coastguard Worker
2640*5a6e8488SAndroid Build Coastguard WorkerOne of the most useful parts of the `bc` test suite, in my opinion, is the heavy
2641*5a6e8488SAndroid Build Coastguard Workertesting of error conditions.
2642*5a6e8488SAndroid Build Coastguard Worker
2643*5a6e8488SAndroid Build Coastguard WorkerJust about every error condition I can think of is tested, along with many
2644*5a6e8488SAndroid Build Coastguard Workermachine-generated (by [AFL++][125]) ones.
2645*5a6e8488SAndroid Build Coastguard Worker
2646*5a6e8488SAndroid Build Coastguard WorkerHowever, because the error tests will often return error codes, they require
2647*5a6e8488SAndroid Build Coastguard Workerdifferent infrastructure from the rest of the test suite, which assumes that
2648*5a6e8488SAndroid Build Coastguard Workerthe calculator under test will return successfully. A lot of that infrastructure
2649*5a6e8488SAndroid Build Coastguard Workeris in the [`scripts/functions.sh`][105] script, but it basically allows the
2650*5a6e8488SAndroid Build Coastguard Workercalculator to exit with an error code and then tests that there *was* an error
2651*5a6e8488SAndroid Build Coastguard Workercode.
2652*5a6e8488SAndroid Build Coastguard Worker
2653*5a6e8488SAndroid Build Coastguard WorkerBesides returning error codes, error tests also ensure that there is output from
2654*5a6e8488SAndroid Build Coastguard Worker`stderr`. This is to make sure that an error message is always printed.
2655*5a6e8488SAndroid Build Coastguard Worker
2656*5a6e8488SAndroid Build Coastguard WorkerThe error tests for each calculator are spread through two directories, due to
2657*5a6e8488SAndroid Build Coastguard Workerhistorical accident. These two directories are the standard test directory (see
2658*5a6e8488SAndroid Build Coastguard Workerthe [Standard Tests][149] section) and the `errors/` directory directly
2659*5a6e8488SAndroid Build Coastguard Workerunderneath the standard tests directory.
2660*5a6e8488SAndroid Build Coastguard Worker
2661*5a6e8488SAndroid Build Coastguard WorkerThis split is convenient, however, because the tests in each directory are
2662*5a6e8488SAndroid Build Coastguard Workertreated differently.
2663*5a6e8488SAndroid Build Coastguard Worker
2664*5a6e8488SAndroid Build Coastguard WorkerThe error tests in the standard test directory, which include `errors.txt` for
2665*5a6e8488SAndroid Build Coastguard Workerboth calculators, `posix_errors.txt` for `bc`, and `read_errors.txt` for both,
2666*5a6e8488SAndroid Build Coastguard Workerare run by [`tests/errors.sh`][226]. It reads them line-by-line and shoves the
2667*5a6e8488SAndroid Build Coastguard Workerdata through `stdin`. Each line is considered a separate test. For this reason,
2668*5a6e8488SAndroid Build Coastguard Workerthere can't be any blank lines in the error files in the standard tests
2669*5a6e8488SAndroid Build Coastguard Workerdirectory because a blank line causes a successful exit.
2670*5a6e8488SAndroid Build Coastguard Worker
2671*5a6e8488SAndroid Build Coastguard WorkerOn the other hand, the tests in the `errors/` directory below the standard tests
2672*5a6e8488SAndroid Build Coastguard Workerdirectory are run by [`tests/error.sh`][227] and are considered to be one test
2673*5a6e8488SAndroid Build Coastguard Workerper file. As such, they are used differently. They are shoved into the
2674*5a6e8488SAndroid Build Coastguard Workercalculator through `stdin`, but they are also executed by passing them on the
2675*5a6e8488SAndroid Build Coastguard Workercommand-line.
2676*5a6e8488SAndroid Build Coastguard Worker
2677*5a6e8488SAndroid Build Coastguard WorkerTo add an error test, first figure out which kind you want.
2678*5a6e8488SAndroid Build Coastguard Worker
2679*5a6e8488SAndroid Build Coastguard WorkerIs it a simple one-liner, and you don't care if it's tested through a file?
2680*5a6e8488SAndroid Build Coastguard Worker
2681*5a6e8488SAndroid Build Coastguard WorkerThen put it in one of the error files in the standard test directory. I would
2682*5a6e8488SAndroid Build Coastguard Workeronly put POSIX errors in the `posix_errors.txt` file for `bc`, and only `read()`
2683*5a6e8488SAndroid Build Coastguard Workererrors in the `read_errors.txt` file for `dc`; all others I would put in the
2684*5a6e8488SAndroid Build Coastguard Workerrespective `errors.txt` file.
2685*5a6e8488SAndroid Build Coastguard Worker
2686*5a6e8488SAndroid Build Coastguard WorkerOn the other hand, if you care if the error is run as a file on the
2687*5a6e8488SAndroid Build Coastguard Workercommand-line, or the error requires multiple lines to reproduce, then put the
2688*5a6e8488SAndroid Build Coastguard Workertest in the respective `errors/` directory and run the [`configure.sh`][69]
2689*5a6e8488SAndroid Build Coastguard Workerscript again.
2690*5a6e8488SAndroid Build Coastguard Worker
2691*5a6e8488SAndroid Build Coastguard WorkerAfter that, you are done; the test suite will automatically pick up the new
2692*5a6e8488SAndroid Build Coastguard Workertest, and you don't have to tell the test suite the expected results.
2693*5a6e8488SAndroid Build Coastguard Worker
2694*5a6e8488SAndroid Build Coastguard Worker### `stdin` Tests
2695*5a6e8488SAndroid Build Coastguard Worker
2696*5a6e8488SAndroid Build Coastguard WorkerThe `stdin` tests specifically test the lexing and parsing of multi-line
2697*5a6e8488SAndroid Build Coastguard Workercomments and strings. This is important because when reading from `stdin`, the
2698*5a6e8488SAndroid Build Coastguard Workercalculators can only read one line at a time, so partial parses are possible.
2699*5a6e8488SAndroid Build Coastguard Worker
2700*5a6e8488SAndroid Build Coastguard WorkerTo add `stdin` tests, just add the tests to the `stdin.txt` file in the
2701*5a6e8488SAndroid Build Coastguard Workerrespective standard tests directory, and add the expected results in the
2702*5a6e8488SAndroid Build Coastguard Worker`stdin_results.txt` in the respective standard tests directory.
2703*5a6e8488SAndroid Build Coastguard Worker
2704*5a6e8488SAndroid Build Coastguard Worker### `read()` Tests
2705*5a6e8488SAndroid Build Coastguard Worker
2706*5a6e8488SAndroid Build Coastguard WorkerThe `read()` tests are meant to test the `read()` builtin function, to ensure
2707*5a6e8488SAndroid Build Coastguard Workerthat the parsing and execution is correct.
2708*5a6e8488SAndroid Build Coastguard Worker
2709*5a6e8488SAndroid Build Coastguard WorkerEach line is one test, as that is the nature of using the `read()` function, so
2710*5a6e8488SAndroid Build Coastguard Workerto add a test, just add it as another line in the `read.txt` file in the
2711*5a6e8488SAndroid Build Coastguard Workerrespective standard tests directory, and add its result to the
2712*5a6e8488SAndroid Build Coastguard Worker`read_results.txt` file in the respective standard tests directory.
2713*5a6e8488SAndroid Build Coastguard Worker
2714*5a6e8488SAndroid Build Coastguard Worker### Other Tests
2715*5a6e8488SAndroid Build Coastguard Worker
2716*5a6e8488SAndroid Build Coastguard WorkerThe "other" tests are just random tests that I could not easily classify under
2717*5a6e8488SAndroid Build Coastguard Workerother types of tests. They usually include things like command-line parsing and
2718*5a6e8488SAndroid Build Coastguard Workerenvironment variable testing.
2719*5a6e8488SAndroid Build Coastguard Worker
2720*5a6e8488SAndroid Build Coastguard WorkerTo add an other test, it requires adding the programming for it to
2721*5a6e8488SAndroid Build Coastguard Worker[`tests/other.sh`][195] because all of the tests are written specifically in
2722*5a6e8488SAndroid Build Coastguard Workerthat script. It would be best to use the infrastructure in
2723*5a6e8488SAndroid Build Coastguard Worker[`scripts/functions.sh`][105].
2724*5a6e8488SAndroid Build Coastguard Worker
2725*5a6e8488SAndroid Build Coastguard Worker### Linux `timeconst.bc` Script
2726*5a6e8488SAndroid Build Coastguard Worker
2727*5a6e8488SAndroid Build Coastguard WorkerOne special script that `bc`'s test suite will use is the [Linux `timeconst.bc`
2728*5a6e8488SAndroid Build Coastguard Workerscript][6].
2729*5a6e8488SAndroid Build Coastguard Worker
2730*5a6e8488SAndroid Build Coastguard WorkerI made the test suite able to use this script because the reason the
2731*5a6e8488SAndroid Build Coastguard Worker[toybox][16] maintainer wanted my `bc` is because of this script, and I wanted
2732*5a6e8488SAndroid Build Coastguard Workerto be sure that it would run correctly on the script.
2733*5a6e8488SAndroid Build Coastguard Worker
2734*5a6e8488SAndroid Build Coastguard WorkerHowever, it is not part of the distribution, nor is it part of the repository.
2735*5a6e8488SAndroid Build Coastguard WorkerThe reason for this is because [`timeconst.bc`][6] is under the GPL, while this
2736*5a6e8488SAndroid Build Coastguard Workerrepo is under a BSD license.
2737*5a6e8488SAndroid Build Coastguard Worker
2738*5a6e8488SAndroid Build Coastguard WorkerIf you want `bc` to run tests on [`timeconst.bc`][6], download it and place it
2739*5a6e8488SAndroid Build Coastguard Workerat `tests/bc/scripts/timeconst.bc`. If it is there, the test suite will
2740*5a6e8488SAndroid Build Coastguard Workerautomatically run its tests; otherwise, it will skip it.
2741*5a6e8488SAndroid Build Coastguard Worker
2742*5a6e8488SAndroid Build Coastguard Worker### History Tests
2743*5a6e8488SAndroid Build Coastguard Worker
2744*5a6e8488SAndroid Build Coastguard WorkerThere are automatic tests for history; however, they have dependencies: Python 3
2745*5a6e8488SAndroid Build Coastguard Workerand [`pexpect`][137].
2746*5a6e8488SAndroid Build Coastguard Worker
2747*5a6e8488SAndroid Build Coastguard WorkerAs a result, because I need the [test suite to be portable][138], like the rest
2748*5a6e8488SAndroid Build Coastguard Workerof `bc`, the history tests are carefully guarded with things to ensure that they
2749*5a6e8488SAndroid Build Coastguard Workerare skipped, rather than failing if Python and [`pexpect`][137] are not
2750*5a6e8488SAndroid Build Coastguard Workerinstalled. For this reason, there is a `sh` script, [`tests/history.sh`][140]
2751*5a6e8488SAndroid Build Coastguard Workerthat runs the actual script, [`tests/history.py`][139].
2752*5a6e8488SAndroid Build Coastguard Worker
2753*5a6e8488SAndroid Build Coastguard WorkerI have added as many tests as I could to cover as many lines and branches as
2754*5a6e8488SAndroid Build Coastguard Workerpossible. I guess I could have done more, but doing so would have required a lot
2755*5a6e8488SAndroid Build Coastguard Workerof time.
2756*5a6e8488SAndroid Build Coastguard Worker
2757*5a6e8488SAndroid Build Coastguard WorkerI have tried to make it as easy as possible to run the history tests. They will
2758*5a6e8488SAndroid Build Coastguard Workerrun automatically if you use the `make test_history` command, and they will also
2759*5a6e8488SAndroid Build Coastguard Workeruse parallel execution with `make -j<cores> test_history`.
2760*5a6e8488SAndroid Build Coastguard Worker
2761*5a6e8488SAndroid Build Coastguard WorkerHowever, the history tests are meant only to be run by maintainers of `bc`; they
2762*5a6e8488SAndroid Build Coastguard Workerare *not* meant to be run by users and packagers. The reason for this is that
2763*5a6e8488SAndroid Build Coastguard Workerthey only seem to work reliably on Linux; `pexpect` seems to have issues on
2764*5a6e8488SAndroid Build Coastguard Workerother platforms, especially timeout issues.
2765*5a6e8488SAndroid Build Coastguard Worker
2766*5a6e8488SAndroid Build Coastguard WorkerThus, they are excluded from running with `make test` and [`tests/all.sh`][225].
2767*5a6e8488SAndroid Build Coastguard WorkerHowever, they can be run from the [`scripts/release.sh`][83] script.
2768*5a6e8488SAndroid Build Coastguard Worker
2769*5a6e8488SAndroid Build Coastguard WorkerAll of the tests are contained in [`tests/history.py`][139]. The reason for this
2770*5a6e8488SAndroid Build Coastguard Workeris because they are in Python, and I don't have an easy way of including Python
2771*5a6e8488SAndroid Build Coastguard Worker(or at the very least, I am not familiar enough with Python to do that). So they
2772*5a6e8488SAndroid Build Coastguard Workerare all in the same file to make it easier on me.
2773*5a6e8488SAndroid Build Coastguard Worker
2774*5a6e8488SAndroid Build Coastguard WorkerEach test is one function in the script. They all take the same number and type
2775*5a6e8488SAndroid Build Coastguard Workerof arguments:
2776*5a6e8488SAndroid Build Coastguard Worker
2777*5a6e8488SAndroid Build Coastguard Worker1.	`exe`: the executable to run.
2778*5a6e8488SAndroid Build Coastguard Worker2.	`args`: the arguments to pass to the executable.
2779*5a6e8488SAndroid Build Coastguard Worker3.	`env`: the environment.
2780*5a6e8488SAndroid Build Coastguard Worker
2781*5a6e8488SAndroid Build Coastguard WorkerEach function creates a child process with `pexpect.spawn` and then tests with
2782*5a6e8488SAndroid Build Coastguard Workerthat child. Then the function returns the child to the caller, who closes it
2783*5a6e8488SAndroid Build Coastguard Workerand checks its error code against its expected error code.
2784*5a6e8488SAndroid Build Coastguard Worker
2785*5a6e8488SAndroid Build Coastguard WorkerYes, the error code is not a success all the time. This is because of the UTF-8
2786*5a6e8488SAndroid Build Coastguard Workertests; `bc` gives a fatal error on any non-ASCII data because ASCII is all `bc`
2787*5a6e8488SAndroid Build Coastguard Workeris required to handle, per the [standard][2].
2788*5a6e8488SAndroid Build Coastguard Worker
2789*5a6e8488SAndroid Build Coastguard WorkerSo in [`tests/history.py`][139], there are four main arrays:
2790*5a6e8488SAndroid Build Coastguard Worker
2791*5a6e8488SAndroid Build Coastguard Worker* `bc` test functions,
2792*5a6e8488SAndroid Build Coastguard Worker* `bc` expected error codes.
2793*5a6e8488SAndroid Build Coastguard Worker* `dc` test functions.
2794*5a6e8488SAndroid Build Coastguard Worker* `dc` expected error codes.
2795*5a6e8488SAndroid Build Coastguard Worker
2796*5a6e8488SAndroid Build Coastguard Worker[`tests/history.py`][139] takes an index as an argument; that index is what test
2797*5a6e8488SAndroid Build Coastguard Workerit should run. That index is used to index into the proper test and error code
2798*5a6e8488SAndroid Build Coastguard Workerarray.
2799*5a6e8488SAndroid Build Coastguard Worker
2800*5a6e8488SAndroid Build Coastguard WorkerIf you need to add more history tests, you need to do the following:
2801*5a6e8488SAndroid Build Coastguard Worker
2802*5a6e8488SAndroid Build Coastguard Worker1.	Add the function for that test to [`tests/history.py`][139].
2803*5a6e8488SAndroid Build Coastguard Worker2.	Add the function to the proper array of tests.
2804*5a6e8488SAndroid Build Coastguard Worker3.	Add the expected error code to the proper array of error codes.
2805*5a6e8488SAndroid Build Coastguard Worker4.	Add a target for the test to [`Makefile.in`][70].
2806*5a6e8488SAndroid Build Coastguard Worker5.	Add that target as a prerequisite to either `test_bc_history` or
2807*5a6e8488SAndroid Build Coastguard Worker	`test_dc_history`.
2808*5a6e8488SAndroid Build Coastguard Worker
2809*5a6e8488SAndroid Build Coastguard WorkerYou do not need to do anything to add the test to `history_all_tests` (see
2810*5a6e8488SAndroid Build Coastguard Worker[Group Tests][141] below) because the scripts will automatically run all of the
2811*5a6e8488SAndroid Build Coastguard Workertests properly.
2812*5a6e8488SAndroid Build Coastguard Worker
2813*5a6e8488SAndroid Build Coastguard Worker### Generated Tests
2814*5a6e8488SAndroid Build Coastguard Worker
2815*5a6e8488SAndroid Build Coastguard WorkerSome tests are *large*, and as such, it is impractical to check them into `git`.
2816*5a6e8488SAndroid Build Coastguard WorkerInstead, the tests depend on the existence of a GNU-compatible `bc` in the
2817*5a6e8488SAndroid Build Coastguard Worker`PATH`, which is then used to generate the tests.
2818*5a6e8488SAndroid Build Coastguard Worker
2819*5a6e8488SAndroid Build Coastguard WorkerIf [`configure.sh`][69] was run with the `-G` argument, which disables generated
2820*5a6e8488SAndroid Build Coastguard Workertests, then `make test` and friends will automatically skip generated tests.
2821*5a6e8488SAndroid Build Coastguard WorkerThis is useful to do on platforms that are not guaranteed to have a
2822*5a6e8488SAndroid Build Coastguard WorkerGNU-compatible `bc` installed.
2823*5a6e8488SAndroid Build Coastguard Worker
2824*5a6e8488SAndroid Build Coastguard WorkerHowever, adding a generated test is a complicated because you have to figure out
2825*5a6e8488SAndroid Build Coastguard Worker*where* you want to put the file to generate the test.
2826*5a6e8488SAndroid Build Coastguard Worker
2827*5a6e8488SAndroid Build Coastguard WorkerFor example, `bc`'s test suite will automatically use a GNU-compatible `bc` to
2828*5a6e8488SAndroid Build Coastguard Workergenerate a `<test>_results.txt` file in the [standard tests][149] directory
2829*5a6e8488SAndroid Build Coastguard Worker(either `tests/bc/` or `tests/dc/`) if none exists for the `<test>` test. If no
2830*5a6e8488SAndroid Build Coastguard Worker`<test>.txt` file exists in the [standard tests][149] directory, then `bc`'s
2831*5a6e8488SAndroid Build Coastguard Workertest suite will look for a `<test>.bc` or `<test>.dc` file in the [script
2832*5a6e8488SAndroid Build Coastguard Workertests][150] directory (either `tests/bc/scripts` or `tests/dc/scripts`), and if
2833*5a6e8488SAndroid Build Coastguard Workerthat exists, it will use that script to generate the `<test>.txt` file in the
2834*5a6e8488SAndroid Build Coastguard Worker[standard tests][149] directory after which it will generate the
2835*5a6e8488SAndroid Build Coastguard Worker`<test>_results.txt` file in the [standard tests][149] directory.
2836*5a6e8488SAndroid Build Coastguard Worker
2837*5a6e8488SAndroid Build Coastguard WorkerSo you can choose to either:
2838*5a6e8488SAndroid Build Coastguard Worker
2839*5a6e8488SAndroid Build Coastguard Worker* Have a test in the [standard tests][149] directory without a corresponding
2840*5a6e8488SAndroid Build Coastguard Worker  `*_results.txt` file, or
2841*5a6e8488SAndroid Build Coastguard Worker* Have a script in the [script tests][150] directory to generate the
2842*5a6e8488SAndroid Build Coastguard Worker  corresponding file in the standard test directory before generating the
2843*5a6e8488SAndroid Build Coastguard Worker  corresponding `*_results.txt` file.
2844*5a6e8488SAndroid Build Coastguard Worker
2845*5a6e8488SAndroid Build Coastguard WorkerAdding a script has a double benefit: the script itself can be used as a test.
2846*5a6e8488SAndroid Build Coastguard WorkerHowever, script test results can also be generated.
2847*5a6e8488SAndroid Build Coastguard Worker
2848*5a6e8488SAndroid Build Coastguard WorkerIf `bc` is asked to run a script test, then if the script does not exist, `bc`'s
2849*5a6e8488SAndroid Build Coastguard Workertest suite returns an error. If it *does* exist, but no corresponding
2850*5a6e8488SAndroid Build Coastguard Worker`<test>.txt` file exists in the [script tests][150] directory, then a
2851*5a6e8488SAndroid Build Coastguard WorkerGNU-compatible `bc` is used to generate the `<test>.txt` results file.
2852*5a6e8488SAndroid Build Coastguard Worker
2853*5a6e8488SAndroid Build Coastguard WorkerIf generated tests are disabled through [`configure.sh`][69], then these tests
2854*5a6e8488SAndroid Build Coastguard Workerare not generated if they do not exist. However, if they *do* exist, then they
2855*5a6e8488SAndroid Build Coastguard Workerare run. This can happen if a `make clean_tests` was not run between a build
2856*5a6e8488SAndroid Build Coastguard Workerthat generated tests and a build that will not.
2857*5a6e8488SAndroid Build Coastguard Worker
2858*5a6e8488SAndroid Build Coastguard Worker### Group Tests
2859*5a6e8488SAndroid Build Coastguard Worker
2860*5a6e8488SAndroid Build Coastguard WorkerWhile the test suite has a lot of targets in order to get parallel execution,
2861*5a6e8488SAndroid Build Coastguard Workerthere are five targets that allow you to run each section, or all, of the test
2862*5a6e8488SAndroid Build Coastguard Workersuite as one unit:
2863*5a6e8488SAndroid Build Coastguard Worker
2864*5a6e8488SAndroid Build Coastguard Worker* `bc_all_tests` (`bc` tests)
2865*5a6e8488SAndroid Build Coastguard Worker* `timeconst_all_tests` ([Linux `timeconst.bc` script][6] tests)
2866*5a6e8488SAndroid Build Coastguard Worker* `dc_all_tests` (`dc` tests)
2867*5a6e8488SAndroid Build Coastguard Worker* `history_all_tests` (history tests)
2868*5a6e8488SAndroid Build Coastguard Worker* `run_all_tests` (combination of the previous four)
2869*5a6e8488SAndroid Build Coastguard Worker
2870*5a6e8488SAndroid Build Coastguard WorkerIn addition, there are more fine-grained targets available:
2871*5a6e8488SAndroid Build Coastguard Worker
2872*5a6e8488SAndroid Build Coastguard Worker* `test_bc` runs all `bc` tests (except history tests).
2873*5a6e8488SAndroid Build Coastguard Worker* `test_dc` runs all `dc` tests (except history tests).
2874*5a6e8488SAndroid Build Coastguard Worker* `test_bc_tests` runs all `bc` [standard tests][149].
2875*5a6e8488SAndroid Build Coastguard Worker* `test_dc_tests` runs all `dc` [standard tests][149].
2876*5a6e8488SAndroid Build Coastguard Worker* `test_bc_scripts` runs all `bc` [script tests][150].
2877*5a6e8488SAndroid Build Coastguard Worker* `test_dc_scripts` runs all `dc` [script tests][150].
2878*5a6e8488SAndroid Build Coastguard Worker* `test_bc_stdin` runs the `bc` [`stdin` tests][152].
2879*5a6e8488SAndroid Build Coastguard Worker* `test_dc_stdin` runs the `dc` [`stdin` tests][152].
2880*5a6e8488SAndroid Build Coastguard Worker* `test_bc_read` runs the `bc` [`read()` tests][153].
2881*5a6e8488SAndroid Build Coastguard Worker* `test_dc_read` runs the `dc` [`read()` tests][153].
2882*5a6e8488SAndroid Build Coastguard Worker* `test_bc_errors` runs the `bc` [error tests][151].
2883*5a6e8488SAndroid Build Coastguard Worker* `test_dc_errors` runs the `dc` [error tests][151].
2884*5a6e8488SAndroid Build Coastguard Worker* `test_bc_other` runs the `bc` [other tests][151].
2885*5a6e8488SAndroid Build Coastguard Worker* `test_dc_other` runs the `dc` [other tests][151].
2886*5a6e8488SAndroid Build Coastguard Worker* `timeconst` runs the tests for the [Linux `timeconst.bc` script][6].
2887*5a6e8488SAndroid Build Coastguard Worker* `test_history` runs all history tests.
2888*5a6e8488SAndroid Build Coastguard Worker* `test_bc_history` runs all `bc` history tests.
2889*5a6e8488SAndroid Build Coastguard Worker* `test_dc_history` runs all `dc` history tests.
2890*5a6e8488SAndroid Build Coastguard Worker
2891*5a6e8488SAndroid Build Coastguard WorkerAll of the above tests are parallelizable.
2892*5a6e8488SAndroid Build Coastguard Worker
2893*5a6e8488SAndroid Build Coastguard Worker### Individual Tests
2894*5a6e8488SAndroid Build Coastguard Worker
2895*5a6e8488SAndroid Build Coastguard WorkerIn addition to all of the above, individual test targets are available. These
2896*5a6e8488SAndroid Build Coastguard Workerare mostly useful for attempting to fix a singular test failure.
2897*5a6e8488SAndroid Build Coastguard Worker
2898*5a6e8488SAndroid Build Coastguard WorkerThese tests are:
2899*5a6e8488SAndroid Build Coastguard Worker
2900*5a6e8488SAndroid Build Coastguard Worker* `test_bc_<test>`, where `<test>` is the name of a `bc` [standard test][149].
2901*5a6e8488SAndroid Build Coastguard Worker  The name is the name of the test file without the `.txt` extension. It is the
2902*5a6e8488SAndroid Build Coastguard Worker  name printed by the test suite when running the test.
2903*5a6e8488SAndroid Build Coastguard Worker* `test_dc_<test>`, where `<test>` is the name of a `dc` [standard test][149].
2904*5a6e8488SAndroid Build Coastguard Worker  The name is the name of the test file without the `.txt` extension. It is the
2905*5a6e8488SAndroid Build Coastguard Worker  name printed by the test suite when running the test.
2906*5a6e8488SAndroid Build Coastguard Worker* `test_bc_script_<test>`, where `<test>` is the name of a `bc` [script
2907*5a6e8488SAndroid Build Coastguard Worker  test][150]. The name of the test is the name of the script without the `.bc`
2908*5a6e8488SAndroid Build Coastguard Worker  extension.
2909*5a6e8488SAndroid Build Coastguard Worker* `test_dc_script_<test>`, where `<test>` is the name of a `dc` [script
2910*5a6e8488SAndroid Build Coastguard Worker  test][150]. The name of the test is the name of the script without the `.dc`
2911*5a6e8488SAndroid Build Coastguard Worker  extension.
2912*5a6e8488SAndroid Build Coastguard Worker* `test_bc_history<idx>` runs the `bc` history test with index `<idx>`.
2913*5a6e8488SAndroid Build Coastguard Worker* `test_dc_history<idx>` runs the `dc` history test with index `<idx>`.
2914*5a6e8488SAndroid Build Coastguard Worker
2915*5a6e8488SAndroid Build Coastguard Worker### [`bcl`][156] Test
2916*5a6e8488SAndroid Build Coastguard Worker
2917*5a6e8488SAndroid Build Coastguard WorkerWhen [`bcl`][156] is built, the [build system][142] automatically ensures that
2918*5a6e8488SAndroid Build Coastguard Worker`make test` runs the [`bcl`][156] test instead of the `bc` and `dc` tests.
2919*5a6e8488SAndroid Build Coastguard Worker
2920*5a6e8488SAndroid Build Coastguard WorkerThere is only one test, and it is built from [`tests/bcl.c`][158].
2921*5a6e8488SAndroid Build Coastguard Worker
2922*5a6e8488SAndroid Build Coastguard WorkerThe reason the test is in C is because [`bcl`][156] is a C library; I did not
2923*5a6e8488SAndroid Build Coastguard Workerwant to have to write C code *and* POSIX `sh` scripts to run it.
2924*5a6e8488SAndroid Build Coastguard Worker
2925*5a6e8488SAndroid Build Coastguard WorkerThe reason there is only one test is because most of the code for the library is
2926*5a6e8488SAndroid Build Coastguard Workertested by virtue of testing `bc` and `dc`; the test needs to only ensure that
2927*5a6e8488SAndroid Build Coastguard Workerthe library bindings and plumbing do not interfere with the underlying code.
2928*5a6e8488SAndroid Build Coastguard Worker
2929*5a6e8488SAndroid Build Coastguard WorkerHowever, just because there is only one test does not mean that it doesn't test
2930*5a6e8488SAndroid Build Coastguard Workermore than one thing. The code actually handles a series of tests, along with
2931*5a6e8488SAndroid Build Coastguard Workererror checking to ensure that nothing went wrong.
2932*5a6e8488SAndroid Build Coastguard Worker
2933*5a6e8488SAndroid Build Coastguard WorkerTo add a [`bcl`][156] test, just figure out what test you want, figure out where
2934*5a6e8488SAndroid Build Coastguard Workerin the [`tests/bcl.c`][158] would be best to put it, and put it there. Do as
2935*5a6e8488SAndroid Build Coastguard Workermuch error checking as possible, and use the `err(BclError)` function. Ensure
2936*5a6e8488SAndroid Build Coastguard Workerthat all memory is freed because that test is run through [Valgrind][159] and
2937*5a6e8488SAndroid Build Coastguard Worker[AddressSanitizer][160].
2938*5a6e8488SAndroid Build Coastguard Worker
2939*5a6e8488SAndroid Build Coastguard Worker### Integration with the Build System
2940*5a6e8488SAndroid Build Coastguard Worker
2941*5a6e8488SAndroid Build Coastguard WorkerIf it was not obvious by now, the test suite is heavily integrated into the
2942*5a6e8488SAndroid Build Coastguard Worker[build system][142], but the integration goes further than just making the test
2943*5a6e8488SAndroid Build Coastguard Workersuite easy to run from `make` and generating individual and group tests.
2944*5a6e8488SAndroid Build Coastguard Worker
2945*5a6e8488SAndroid Build Coastguard WorkerThe big problem the test suite has is that some `bc` code, stuff that is
2946*5a6e8488SAndroid Build Coastguard Workerimportant to test, is only in *some* builds. This includes all of the extra math
2947*5a6e8488SAndroid Build Coastguard Workerextensions, for example.
2948*5a6e8488SAndroid Build Coastguard Worker
2949*5a6e8488SAndroid Build Coastguard WorkerSo the test suite needs to have some way of turning off the tests that depend on
2950*5a6e8488SAndroid Build Coastguard Workercertain [build types][81] when those [build types][81] are not used.
2951*5a6e8488SAndroid Build Coastguard Worker
2952*5a6e8488SAndroid Build Coastguard WorkerThis is the reason the is tightly integrated with the [build system][142]: the
2953*5a6e8488SAndroid Build Coastguard Worker[build system][142] knows what [build type][81] was used and can tell the test
2954*5a6e8488SAndroid Build Coastguard Workersuite to turn off the tests that do not apply.
2955*5a6e8488SAndroid Build Coastguard Worker
2956*5a6e8488SAndroid Build Coastguard WorkerIt does this with arguments to the test scripts that are either a `1` or a `0`,
2957*5a6e8488SAndroid Build Coastguard Workerdepending on whether tests of that type should be enabled or not. These
2958*5a6e8488SAndroid Build Coastguard Workerarguments are why I suggest, in the [Test Scripts][148] section, to always use a
2959*5a6e8488SAndroid Build Coastguard Worker`make` target to run the test suite or any individual test. I have added a lot
2960*5a6e8488SAndroid Build Coastguard Workerof targets to make this easy and as fast as possible.
2961*5a6e8488SAndroid Build Coastguard Worker
2962*5a6e8488SAndroid Build Coastguard WorkerIn addition to all of that, the build system is responsible for selecting the
2963*5a6e8488SAndroid Build Coastguard Worker`bc`/`dc` tests or the [`bcl` test][157].
2964*5a6e8488SAndroid Build Coastguard Worker
2965*5a6e8488SAndroid Build Coastguard Worker### Output Directories
2966*5a6e8488SAndroid Build Coastguard Worker
2967*5a6e8488SAndroid Build Coastguard WorkerDuring any run of the test suite, the test suite outputs the results of running
2968*5a6e8488SAndroid Build Coastguard Workervarious tests to files. These files are usually output to `tests/bc_outputs/`
2969*5a6e8488SAndroid Build Coastguard Workerand `tests/dc_outputs/`.
2970*5a6e8488SAndroid Build Coastguard Worker
2971*5a6e8488SAndroid Build Coastguard WorkerHowever, in some cases, it may be necessary to output test results to a
2972*5a6e8488SAndroid Build Coastguard Workerdifferent directory. If that is the case, set the environment variable
2973*5a6e8488SAndroid Build Coastguard Worker`BC_TEST_OUTPUT_DIR` to the name of the directory.
2974*5a6e8488SAndroid Build Coastguard Worker
2975*5a6e8488SAndroid Build Coastguard WorkerIf that is done, then test results will be written to
2976*5a6e8488SAndroid Build Coastguard Worker`$BC_TEST_OUTPUT_DIR/bc_outputs/` and `$BC_TEST_OUTPUT_DIR/dc_outputs/`.
2977*5a6e8488SAndroid Build Coastguard Worker
2978*5a6e8488SAndroid Build Coastguard Worker### Test Suite Portability
2979*5a6e8488SAndroid Build Coastguard Worker
2980*5a6e8488SAndroid Build Coastguard WorkerThe test suite is meant to be run by users and packagers as part of their
2981*5a6e8488SAndroid Build Coastguard Workerinstall process.
2982*5a6e8488SAndroid Build Coastguard Worker
2983*5a6e8488SAndroid Build Coastguard WorkerThis puts some constraints on the test suite, but the biggest is that the test
2984*5a6e8488SAndroid Build Coastguard Workersuite must be as [portable as `bc` itself][136].
2985*5a6e8488SAndroid Build Coastguard Worker
2986*5a6e8488SAndroid Build Coastguard WorkerThis means that the test suite must be implemented in pure POSIX `make`, `sh`,
2987*5a6e8488SAndroid Build Coastguard Workerand C99.
2988*5a6e8488SAndroid Build Coastguard Worker
2989*5a6e8488SAndroid Build Coastguard Worker#### Test Scripts
2990*5a6e8488SAndroid Build Coastguard Worker
2991*5a6e8488SAndroid Build Coastguard WorkerTo accomplish the portability, the test suite is run by a bunch of `sh` scripts
2992*5a6e8488SAndroid Build Coastguard Workerthat have the constraints laid out in [POSIX Shell Scripts][76].
2993*5a6e8488SAndroid Build Coastguard Worker
2994*5a6e8488SAndroid Build Coastguard WorkerHowever, that means they have some quirks, made worse by the fact that there are
2995*5a6e8488SAndroid Build Coastguard Worker[generated tests][143] and [tests that need to be skipped, but only
2996*5a6e8488SAndroid Build Coastguard Workersometimes][147].
2997*5a6e8488SAndroid Build Coastguard Worker
2998*5a6e8488SAndroid Build Coastguard WorkerThis means that a lot of the scripts take an awkward number and type of
2999*5a6e8488SAndroid Build Coastguard Workerarguments. Some arguments are strings, but most are integers, like
3000*5a6e8488SAndroid Build Coastguard Worker[`scripts/release.sh`][83].
3001*5a6e8488SAndroid Build Coastguard Worker
3002*5a6e8488SAndroid Build Coastguard WorkerIt is for this reason that I do not suggest running the test scripts directly.
3003*5a6e8488SAndroid Build Coastguard WorkerInstead, always use an appropriate `make` target, which already knows the
3004*5a6e8488SAndroid Build Coastguard Workercorrect arguments for the test because of the [integration with the build
3005*5a6e8488SAndroid Build Coastguard Workersystem][147].
3006*5a6e8488SAndroid Build Coastguard Worker
3007*5a6e8488SAndroid Build Coastguard Worker### Test Coverage
3008*5a6e8488SAndroid Build Coastguard Worker
3009*5a6e8488SAndroid Build Coastguard WorkerIn order to get test coverage information, you need `gcc`, `gcov`, and `gcovr`.
3010*5a6e8488SAndroid Build Coastguard Worker
3011*5a6e8488SAndroid Build Coastguard WorkerIf you have them, run the following commands:
3012*5a6e8488SAndroid Build Coastguard Worker
3013*5a6e8488SAndroid Build Coastguard Worker```
3014*5a6e8488SAndroid Build Coastguard WorkerCC=gcc ./configure -gO3 -c
3015*5a6e8488SAndroid Build Coastguard Workermake -j<cores>
3016*5a6e8488SAndroid Build Coastguard Workermake coverage
3017*5a6e8488SAndroid Build Coastguard Worker```
3018*5a6e8488SAndroid Build Coastguard Worker
3019*5a6e8488SAndroid Build Coastguard WorkerNote that `make coverage` does not have a `-j<cores>` part; it cannot be run in
3020*5a6e8488SAndroid Build Coastguard Workerparallel. If you try, you will get errors. And note that `CC=gcc` is used.
3021*5a6e8488SAndroid Build Coastguard Worker
3022*5a6e8488SAndroid Build Coastguard WorkerAfter running those commands, you can open your web browser and open the
3023*5a6e8488SAndroid Build Coastguard Worker`index.html` file in the root directory of the repo. From there, you can explore
3024*5a6e8488SAndroid Build Coastguard Workerall of the coverage results.
3025*5a6e8488SAndroid Build Coastguard Worker
3026*5a6e8488SAndroid Build Coastguard WorkerIf you see lines or branches that you think you could hit with a manual
3027*5a6e8488SAndroid Build Coastguard Workerexecution, do such manual execution, and then run the following command:
3028*5a6e8488SAndroid Build Coastguard Worker
3029*5a6e8488SAndroid Build Coastguard Worker```
3030*5a6e8488SAndroid Build Coastguard Workermake coverage_output
3031*5a6e8488SAndroid Build Coastguard Worker```
3032*5a6e8488SAndroid Build Coastguard Worker
3033*5a6e8488SAndroid Build Coastguard Workerand the coverage output will be updated.
3034*5a6e8488SAndroid Build Coastguard Worker
3035*5a6e8488SAndroid Build Coastguard WorkerIf you want to rerun `make coverage`, you must do a `make clean` and build
3036*5a6e8488SAndroid Build Coastguard Workerfirst, like this:
3037*5a6e8488SAndroid Build Coastguard Worker
3038*5a6e8488SAndroid Build Coastguard Worker```
3039*5a6e8488SAndroid Build Coastguard Workermake clean
3040*5a6e8488SAndroid Build Coastguard Workermake -j<cores>
3041*5a6e8488SAndroid Build Coastguard Workermake coverage
3042*5a6e8488SAndroid Build Coastguard Worker```
3043*5a6e8488SAndroid Build Coastguard Worker
3044*5a6e8488SAndroid Build Coastguard WorkerOtherwise, you will get errors.
3045*5a6e8488SAndroid Build Coastguard Worker
3046*5a6e8488SAndroid Build Coastguard WorkerIf you want to run tests in parallel, you can do this:
3047*5a6e8488SAndroid Build Coastguard Worker
3048*5a6e8488SAndroid Build Coastguard Worker```
3049*5a6e8488SAndroid Build Coastguard Workermake -j<cores>
3050*5a6e8488SAndroid Build Coastguard Workermake -j<cores> test
3051*5a6e8488SAndroid Build Coastguard Workermake coverage_output
3052*5a6e8488SAndroid Build Coastguard Worker```
3053*5a6e8488SAndroid Build Coastguard Worker
3054*5a6e8488SAndroid Build Coastguard Workerand that will generate coverage output correctly.
3055*5a6e8488SAndroid Build Coastguard Worker
3056*5a6e8488SAndroid Build Coastguard Worker### [AddressSanitizer][21] and Friends
3057*5a6e8488SAndroid Build Coastguard Worker
3058*5a6e8488SAndroid Build Coastguard WorkerTo run the test suite under [AddressSanitizer][21] or any of its friends, use
3059*5a6e8488SAndroid Build Coastguard Workerthe following commands:
3060*5a6e8488SAndroid Build Coastguard Worker
3061*5a6e8488SAndroid Build Coastguard Worker```
3062*5a6e8488SAndroid Build Coastguard WorkerCFLAGS="-fsanitize=<sanitizer> ./configure -gO3 -m
3063*5a6e8488SAndroid Build Coastguard Workermake -j<cores>
3064*5a6e8488SAndroid Build Coastguard Workermake -j<cores> test
3065*5a6e8488SAndroid Build Coastguard Worker```
3066*5a6e8488SAndroid Build Coastguard Worker
3067*5a6e8488SAndroid Build Coastguard Workerwhere `<sanitizer>` is the correct name of the desired sanitizer. There is one
3068*5a6e8488SAndroid Build Coastguard Workerexception to the above: `UndefinedBehaviorSanitizer` should be run on a build
3069*5a6e8488SAndroid Build Coastguard Workerthat has zero optimization, so for `UBSan`, use the following commands:
3070*5a6e8488SAndroid Build Coastguard Worker
3071*5a6e8488SAndroid Build Coastguard Worker```
3072*5a6e8488SAndroid Build Coastguard WorkerCFLAGS="-fsanitize=undefined" ./configure -gO0 -m
3073*5a6e8488SAndroid Build Coastguard Workermake -j<cores>
3074*5a6e8488SAndroid Build Coastguard Workermake -j<cores> test
3075*5a6e8488SAndroid Build Coastguard Worker```
3076*5a6e8488SAndroid Build Coastguard Worker
3077*5a6e8488SAndroid Build Coastguard Worker### [Valgrind][20]
3078*5a6e8488SAndroid Build Coastguard Worker
3079*5a6e8488SAndroid Build Coastguard WorkerTo run the test suite under [Valgrind][20], run the following commands:
3080*5a6e8488SAndroid Build Coastguard Worker
3081*5a6e8488SAndroid Build Coastguard Worker```
3082*5a6e8488SAndroid Build Coastguard Worker./configure -gO3 -v
3083*5a6e8488SAndroid Build Coastguard Workermake -j<cores>
3084*5a6e8488SAndroid Build Coastguard Workermake -j<cores> test
3085*5a6e8488SAndroid Build Coastguard Worker```
3086*5a6e8488SAndroid Build Coastguard Worker
3087*5a6e8488SAndroid Build Coastguard WorkerIt really is that easy. I have directly added infrastructure to the build system
3088*5a6e8488SAndroid Build Coastguard Workerand the test suite to ensure that if [Valgrind][20] detects any memory errors or
3089*5a6e8488SAndroid Build Coastguard Workerany memory leaks at all, it will tell the test suite infrastructure to report an
3090*5a6e8488SAndroid Build Coastguard Workererror and exit accordingly.
3091*5a6e8488SAndroid Build Coastguard Worker
3092*5a6e8488SAndroid Build Coastguard Worker## POSIX Shell Scripts
3093*5a6e8488SAndroid Build Coastguard Worker
3094*5a6e8488SAndroid Build Coastguard WorkerThere is a lot of shell scripts in this repository, and every single one of them
3095*5a6e8488SAndroid Build Coastguard Workeris written in pure [POSIX `sh`][72].
3096*5a6e8488SAndroid Build Coastguard Worker
3097*5a6e8488SAndroid Build Coastguard WorkerThe reason that they are written in [POSIX `sh`][72] is for *portability*: POSIX
3098*5a6e8488SAndroid Build Coastguard Workersystems are only guaranteed to have a barebones implementation of `sh`
3099*5a6e8488SAndroid Build Coastguard Workeravailable.
3100*5a6e8488SAndroid Build Coastguard Worker
3101*5a6e8488SAndroid Build Coastguard WorkerThere are *many* snares for unwary programmers attempting to modify
3102*5a6e8488SAndroid Build Coastguard Worker[`configure.sh`][69], any of the scripts in this directory, [`strgen.sh`][9], or
3103*5a6e8488SAndroid Build Coastguard Workerany of the scripts in [`tests/`][77]. Here are some of them:
3104*5a6e8488SAndroid Build Coastguard Worker
3105*5a6e8488SAndroid Build Coastguard Worker1.	No `bash`-isms.
3106*5a6e8488SAndroid Build Coastguard Worker2.	Only POSIX standard utilities are allowed.
3107*5a6e8488SAndroid Build Coastguard Worker3.	Only command-line options defined in the POSIX standard for POSIX utilities
3108*5a6e8488SAndroid Build Coastguard Worker	are allowed.
3109*5a6e8488SAndroid Build Coastguard Worker4.	Only the standardized behavior of POSIX utilities is allowed.
3110*5a6e8488SAndroid Build Coastguard Worker5.	Functions return data by *printing* it. Using `return` sets their exit code.
3111*5a6e8488SAndroid Build Coastguard Worker
3112*5a6e8488SAndroid Build Coastguard WorkerIn other words, the script must only use what is standardized in the [`sh`][72]
3113*5a6e8488SAndroid Build Coastguard Workerand [Shell Command Language][73] standards in POSIX. This is *hard*. It precludes
3114*5a6e8488SAndroid Build Coastguard Workerthings like `local` and the `[[ ]]` notation.
3115*5a6e8488SAndroid Build Coastguard Worker
3116*5a6e8488SAndroid Build Coastguard WorkerThese are *enormous* restrictions and must be tested properly. I put out at
3117*5a6e8488SAndroid Build Coastguard Workerleast one release with a change to `configure.sh` that wasn't portable. That was
3118*5a6e8488SAndroid Build Coastguard Workeran embarrassing mistake.
3119*5a6e8488SAndroid Build Coastguard Worker
3120*5a6e8488SAndroid Build Coastguard WorkerThe lack of `local`, by the way, is why variables in functions are named with
3121*5a6e8488SAndroid Build Coastguard Workerthe form:
3122*5a6e8488SAndroid Build Coastguard Worker
3123*5a6e8488SAndroid Build Coastguard Worker```
3124*5a6e8488SAndroid Build Coastguard Worker_<function_name>_<var_name>
3125*5a6e8488SAndroid Build Coastguard Worker```
3126*5a6e8488SAndroid Build Coastguard Worker
3127*5a6e8488SAndroid Build Coastguard WorkerThis is done to prevent any clashes of variable names with already existing
3128*5a6e8488SAndroid Build Coastguard Workernames. And this applies to *all* shell scripts. However, there are a few times
3129*5a6e8488SAndroid Build Coastguard Workerwhen that naming convention is *not* used; all of them are because those
3130*5a6e8488SAndroid Build Coastguard Workerfunctions are required to change variables in the global scope.
3131*5a6e8488SAndroid Build Coastguard Worker
3132*5a6e8488SAndroid Build Coastguard Worker### Maintainer-Only Scripts
3133*5a6e8488SAndroid Build Coastguard Worker
3134*5a6e8488SAndroid Build Coastguard WorkerIf a script is meant to be used for maintainers (of `bc`, not package
3135*5a6e8488SAndroid Build Coastguard Workermaintainers), then rules 2, 3, and 4 don't need to be followed as much because
3136*5a6e8488SAndroid Build Coastguard Workerit is assumed that maintainers will be able to install whatever tools are
3137*5a6e8488SAndroid Build Coastguard Workernecessary to do the job.
3138*5a6e8488SAndroid Build Coastguard Worker
3139*5a6e8488SAndroid Build Coastguard Worker## Manuals
3140*5a6e8488SAndroid Build Coastguard Worker
3141*5a6e8488SAndroid Build Coastguard WorkerThe manuals for `bc` and `dc` are all generated, and the manpages for `bc`,
3142*5a6e8488SAndroid Build Coastguard Worker`dc`, and `bcl` are also generated.
3143*5a6e8488SAndroid Build Coastguard Worker
3144*5a6e8488SAndroid Build Coastguard WorkerWhy?
3145*5a6e8488SAndroid Build Coastguard Worker
3146*5a6e8488SAndroid Build Coastguard WorkerI don't like the format of manpages, and I am not confident in my ability to
3147*5a6e8488SAndroid Build Coastguard Workerwrite them. Also, they are not easy to read on the web.
3148*5a6e8488SAndroid Build Coastguard Worker
3149*5a6e8488SAndroid Build Coastguard WorkerSo that explains why `bcl`'s manpage is generated from its markdown version. But
3150*5a6e8488SAndroid Build Coastguard Workerwhy are the markdown versions of the `bc` and `dc` generated?
3151*5a6e8488SAndroid Build Coastguard Worker
3152*5a6e8488SAndroid Build Coastguard WorkerBecause the content of the manuals needs to change based on the [build
3153*5a6e8488SAndroid Build Coastguard Workertype][81]. For example, if `bc` was built with no history support, it should not
3154*5a6e8488SAndroid Build Coastguard Workerhave the **COMMAND LINE HISTORY** section in its manual. If it did, that would
3155*5a6e8488SAndroid Build Coastguard Workerjust confuse users.
3156*5a6e8488SAndroid Build Coastguard Worker
3157*5a6e8488SAndroid Build Coastguard WorkerSo the markdown manuals for `bc` and `dc` are generated from templates
3158*5a6e8488SAndroid Build Coastguard Worker([`manuals/bc.1.md.in`][89] and [`manuals/dc.1.md.in`][90]). And from there,
3159*5a6e8488SAndroid Build Coastguard Workerthe manpages are generated from the generated manuals.
3160*5a6e8488SAndroid Build Coastguard Worker
3161*5a6e8488SAndroid Build Coastguard WorkerThe generated manpage for `bcl` ([`manuals/bcl.3`][62]) is checked into version
3162*5a6e8488SAndroid Build Coastguard Workercontrol, and the generated markdown manuals and manpages for `bc`
3163*5a6e8488SAndroid Build Coastguard Worker([`manuals/bc`][79]) and `dc` ([`manuals/dc`][80]) are as well.
3164*5a6e8488SAndroid Build Coastguard Worker
3165*5a6e8488SAndroid Build Coastguard WorkerThis is because generating the manuals and manpages requires a heavy dependency
3166*5a6e8488SAndroid Build Coastguard Workerthat only maintainers should care about: [Pandoc][92]. Because users [should not
3167*5a6e8488SAndroid Build Coastguard Workerhave to install *any* dependencies][136], the files are generated, checked into
3168*5a6e8488SAndroid Build Coastguard Workerversion control, and included in distribution tarballs.
3169*5a6e8488SAndroid Build Coastguard Worker
3170*5a6e8488SAndroid Build Coastguard WorkerIf you run [`configure.sh`][69], you have an easy way of generating the markdown
3171*5a6e8488SAndroid Build Coastguard Workermanuals and manpages: just run `make manpages`. This target calls
3172*5a6e8488SAndroid Build Coastguard Worker[`scripts/manpage.sh`][60] appropriately for `bc`, `dc`, and `bcl`.
3173*5a6e8488SAndroid Build Coastguard Worker
3174*5a6e8488SAndroid Build Coastguard WorkerFor more on how generating manuals and manpages works, see
3175*5a6e8488SAndroid Build Coastguard Worker[`scripts/manpage.sh`][60].
3176*5a6e8488SAndroid Build Coastguard Worker
3177*5a6e8488SAndroid Build Coastguard Worker## Locales
3178*5a6e8488SAndroid Build Coastguard Worker
3179*5a6e8488SAndroid Build Coastguard WorkerThe locale system of `bc` is enormously complex, but that's because
3180*5a6e8488SAndroid Build Coastguard WorkerPOSIX-compatible locales are terrible.
3181*5a6e8488SAndroid Build Coastguard Worker
3182*5a6e8488SAndroid Build Coastguard WorkerHow are they terrible?
3183*5a6e8488SAndroid Build Coastguard Worker
3184*5a6e8488SAndroid Build Coastguard WorkerFirst, `gencat` does not work for generating cross-compilation. In other words,
3185*5a6e8488SAndroid Build Coastguard Workerit does not generate machine-portable files. There's nothing I can do about
3186*5a6e8488SAndroid Build Coastguard Workerthis except for warn users.
3187*5a6e8488SAndroid Build Coastguard Worker
3188*5a6e8488SAndroid Build Coastguard WorkerSecond, the format of `.msg` files is...interesting. Thank goodness it is text
3189*5a6e8488SAndroid Build Coastguard Workerbecause otherwise, it would be impossible to get them right.
3190*5a6e8488SAndroid Build Coastguard Worker
3191*5a6e8488SAndroid Build Coastguard WorkerThird, `.msg` files are not used. In other words, `gencat` exists. Why?
3192*5a6e8488SAndroid Build Coastguard Worker
3193*5a6e8488SAndroid Build Coastguard WorkerFourth, `$NLSPATH` is an awful way to set where and *how* to install locales.
3194*5a6e8488SAndroid Build Coastguard Worker
3195*5a6e8488SAndroid Build Coastguard WorkerYes, where and *how*.
3196*5a6e8488SAndroid Build Coastguard Worker
3197*5a6e8488SAndroid Build Coastguard WorkerObviously, from it's name, it's a path, and that's the where. The *how* is more
3198*5a6e8488SAndroid Build Coastguard Workercomplicated.
3199*5a6e8488SAndroid Build Coastguard Worker
3200*5a6e8488SAndroid Build Coastguard WorkerIt's actually *not* a path, but a path template. It's a format string, and it
3201*5a6e8488SAndroid Build Coastguard Workercan have a few format specifiers. For more information on that, see [this
3202*5a6e8488SAndroid Build Coastguard Workerlink][84]. But in essence, those format specifiers configure how each locale is
3203*5a6e8488SAndroid Build Coastguard Workersupposed to be installed.
3204*5a6e8488SAndroid Build Coastguard Worker
3205*5a6e8488SAndroid Build Coastguard WorkerWith all those problems, why use POSIX locales? Portability, as always. I can't
3206*5a6e8488SAndroid Build Coastguard Workerassume that `gettext` will be available, but I *can* pretty well assume that
3207*5a6e8488SAndroid Build Coastguard WorkerPOSIX locales will be available.
3208*5a6e8488SAndroid Build Coastguard Worker
3209*5a6e8488SAndroid Build Coastguard WorkerThe locale system of `bc` includes all files under [`locales/`][85],
3210*5a6e8488SAndroid Build Coastguard Worker[`scripts/locale_install.sh`][87], [`scripts/locale_uninstall.sh`][88],
3211*5a6e8488SAndroid Build Coastguard Worker[`scripts/functions.sh`][105], the `bc_err_*` constants in [`src/data.c`][131],
3212*5a6e8488SAndroid Build Coastguard Workerand the parts of the build system needed to activate it. There is also code in
3213*5a6e8488SAndroid Build Coastguard Worker[`src/vm.c`][58] (in `bc_vm_gettext()`) for loading the current locale.
3214*5a6e8488SAndroid Build Coastguard Worker
3215*5a6e8488SAndroid Build Coastguard WorkerIf the order of error messages and/or categories are changed, the order of
3216*5a6e8488SAndroid Build Coastguard Workererrors must be changed in the enum, the default error messages and categories in
3217*5a6e8488SAndroid Build Coastguard Worker[`src/data.c`][131], and all of the messages and categories in the `.msg` files
3218*5a6e8488SAndroid Build Coastguard Workerunder [`locales/`][85].
3219*5a6e8488SAndroid Build Coastguard Worker
3220*5a6e8488SAndroid Build Coastguard Worker## Static Analysis
3221*5a6e8488SAndroid Build Coastguard Worker
3222*5a6e8488SAndroid Build Coastguard WorkerI do *some* static analysis on `bc`.
3223*5a6e8488SAndroid Build Coastguard Worker
3224*5a6e8488SAndroid Build Coastguard WorkerI used to use [Coverity][196], but I stopped using it when it started giving me
3225*5a6e8488SAndroid Build Coastguard Workertoo many false positives and also because it had a vulnerability.
3226*5a6e8488SAndroid Build Coastguard Worker
3227*5a6e8488SAndroid Build Coastguard WorkerHowever, I still use the [Clang Static Analyzer][197] through
3228*5a6e8488SAndroid Build Coastguard Worker[`scan-build`][19]. I only use it in debug mode because I have to add some
3229*5a6e8488SAndroid Build Coastguard Workerspecial code to make it not complain about things that are definitely not a
3230*5a6e8488SAndroid Build Coastguard Workerproblem.
3231*5a6e8488SAndroid Build Coastguard Worker
3232*5a6e8488SAndroid Build Coastguard WorkerThe most frequent example of false positives is where a local is passed to a
3233*5a6e8488SAndroid Build Coastguard Workerfunction to be initialized. [`scan-build`][19] misses that fact, so I
3234*5a6e8488SAndroid Build Coastguard Workerpre-initialize such locals to prevent the warnings.
3235*5a6e8488SAndroid Build Coastguard Worker
3236*5a6e8488SAndroid Build Coastguard WorkerTo run `scan-build`, do the following:
3237*5a6e8488SAndroid Build Coastguard Worker
3238*5a6e8488SAndroid Build Coastguard Worker```
3239*5a6e8488SAndroid Build Coastguard Workermake clean
3240*5a6e8488SAndroid Build Coastguard Workerscan-build make
3241*5a6e8488SAndroid Build Coastguard Worker```
3242*5a6e8488SAndroid Build Coastguard Worker
3243*5a6e8488SAndroid Build Coastguard Worker`scan-build` will print its warnings to `stdout`.
3244*5a6e8488SAndroid Build Coastguard Worker
3245*5a6e8488SAndroid Build Coastguard Worker## Fuzzing
3246*5a6e8488SAndroid Build Coastguard Worker
3247*5a6e8488SAndroid Build Coastguard WorkerThe quality of this `bc` is directly related to the amount of fuzzing I did. As
3248*5a6e8488SAndroid Build Coastguard Workersuch, I spent a lot of work making the fuzzing convenient and fast, though I do
3249*5a6e8488SAndroid Build Coastguard Workeradmit that it took me a long time to admit that it did need to be faster.
3250*5a6e8488SAndroid Build Coastguard Worker
3251*5a6e8488SAndroid Build Coastguard WorkerFirst, there were several things which make fuzzing fast:
3252*5a6e8488SAndroid Build Coastguard Worker
3253*5a6e8488SAndroid Build Coastguard Worker* Using [AFL++][125]'s deferred initialization.
3254*5a6e8488SAndroid Build Coastguard Worker* Splitting `bc`'s corpuses.
3255*5a6e8488SAndroid Build Coastguard Worker* Parallel fuzzing.
3256*5a6e8488SAndroid Build Coastguard Worker
3257*5a6e8488SAndroid Build Coastguard WorkerSecond, there are several things which make fuzzing convenient:
3258*5a6e8488SAndroid Build Coastguard Worker
3259*5a6e8488SAndroid Build Coastguard Worker* Preprepared input corpuses.
3260*5a6e8488SAndroid Build Coastguard Worker* [`scripts/fuzz_prep.sh`][119].
3261*5a6e8488SAndroid Build Coastguard Worker* `tmux` and `tmuxp` configs.
3262*5a6e8488SAndroid Build Coastguard Worker* [`scripts/afl.py`][94].
3263*5a6e8488SAndroid Build Coastguard Worker
3264*5a6e8488SAndroid Build Coastguard Worker### Fuzzing Performance
3265*5a6e8488SAndroid Build Coastguard Worker
3266*5a6e8488SAndroid Build Coastguard WorkerFuzzing with [AFL++][125] can be ***SLOW***. Spending the time to make it as
3267*5a6e8488SAndroid Build Coastguard Workerfast as possible is well worth the time.
3268*5a6e8488SAndroid Build Coastguard Worker
3269*5a6e8488SAndroid Build Coastguard WorkerHowever, there is a caveat to the above: it is easy to make [AFL++][125] crash,
3270*5a6e8488SAndroid Build Coastguard Workerbe unstable, or be unable to find "paths" (see [AFL++ Quickstart][129]) if the
3271*5a6e8488SAndroid Build Coastguard Workerperformance enhancements are done poorly.
3272*5a6e8488SAndroid Build Coastguard Worker
3273*5a6e8488SAndroid Build Coastguard WorkerTo stop [AFL++][125] from crashing on test cases, and to be stable, these are
3274*5a6e8488SAndroid Build Coastguard Workerthe requirements:
3275*5a6e8488SAndroid Build Coastguard Worker
3276*5a6e8488SAndroid Build Coastguard Worker* The state at startup must be *exactly* the same.
3277*5a6e8488SAndroid Build Coastguard Worker* The virtual memory setup at startup must be *exactly* the same.
3278*5a6e8488SAndroid Build Coastguard Worker
3279*5a6e8488SAndroid Build Coastguard WorkerThe first isn't too hard; it's the second that is difficult.
3280*5a6e8488SAndroid Build Coastguard Worker
3281*5a6e8488SAndroid Build Coastguard Worker`bc` allocates a lot of memory at start. ("A lot" is relative; it's far less
3282*5a6e8488SAndroid Build Coastguard Workerthan most programs.) After going through an execution run, however, some of that
3283*5a6e8488SAndroid Build Coastguard Workermemory, while it could be cleared and reset, is in different places because of
3284*5a6e8488SAndroid Build Coastguard Workervectors. Since vectors reallocate, their allocations are not guaranteed to be in
3285*5a6e8488SAndroid Build Coastguard Workerthe same place.
3286*5a6e8488SAndroid Build Coastguard Worker
3287*5a6e8488SAndroid Build Coastguard WorkerSo to make all three work, I had to set up the deferred initialization and
3288*5a6e8488SAndroid Build Coastguard Workerpersistent mode *before* any memory was allocated (except for `vm.jmp_bufs`,
3289*5a6e8488SAndroid Build Coastguard Workerwhich is probably what caused the stability to drop below 100%). However, using
3290*5a6e8488SAndroid Build Coastguard Workerdeferred alone let me put the [AFL++][125] initialization further back. This
3291*5a6e8488SAndroid Build Coastguard Workerworks because [AFL++][125] sets up a `fork()` server that `fork()`'s `bc` right
3292*5a6e8488SAndroid Build Coastguard Workerat that call. Thus, every run has the exact same virtual memory setup, and each
3293*5a6e8488SAndroid Build Coastguard Workerrun can skip all of the setup code.
3294*5a6e8488SAndroid Build Coastguard Worker
3295*5a6e8488SAndroid Build Coastguard WorkerI tested `bc` using [AFL++][125]'s deferred initialization, plus persistent
3296*5a6e8488SAndroid Build Coastguard Workermode, plus shared memory fuzzing. In order to do it safely, with stability above
3297*5a6e8488SAndroid Build Coastguard Worker99%, all of that was actually *slower* than using just deferred initialization
3298*5a6e8488SAndroid Build Coastguard Workerwith the initialization *right before* `stdin` was read. And as a bonus, the
3299*5a6e8488SAndroid Build Coastguard Workerstability in that situation is 100%.
3300*5a6e8488SAndroid Build Coastguard Worker
3301*5a6e8488SAndroid Build Coastguard WorkerAs a result, my [AFL++][125] setup only uses deferred initialization. That's the
3302*5a6e8488SAndroid Build Coastguard Worker`__AFL_INIT()` call.
3303*5a6e8488SAndroid Build Coastguard Worker
3304*5a6e8488SAndroid Build Coastguard Worker(Note: there is one more big item that must be done in order to have 100%
3305*5a6e8488SAndroid Build Coastguard Workerstability: the pseudo-random number generator *must* start with *exactly* the
3306*5a6e8488SAndroid Build Coastguard Workersame seed for every run. This is set up with the `tmux` and `tmuxp` configs that
3307*5a6e8488SAndroid Build Coastguard WorkerI talk about below in [Convenience][130]. This seed is set before the
3308*5a6e8488SAndroid Build Coastguard Worker`__AFL_INIT()` call, so setting it has no runtime cost for each run, but without
3309*5a6e8488SAndroid Build Coastguard Workerit, stability would be abysmal.)
3310*5a6e8488SAndroid Build Coastguard Worker
3311*5a6e8488SAndroid Build Coastguard WorkerOn top of that, while `dc` is plenty fast under fuzzing (because of a faster
3312*5a6e8488SAndroid Build Coastguard Workerparser and less test cases), `bc` can be slow. So I have split the `bc` input
3313*5a6e8488SAndroid Build Coastguard Workercorpus into three parts, and I set fuzzers to run on each individually. This
3314*5a6e8488SAndroid Build Coastguard Workermeans that they will duplicate work, but they will also find more stuff.
3315*5a6e8488SAndroid Build Coastguard Worker
3316*5a6e8488SAndroid Build Coastguard WorkerOn top of all of that, each input corpus (the three `bc` corpuses and the one
3317*5a6e8488SAndroid Build Coastguard Worker`dc` corpus) is set to run with 4 fuzzers. That works out perfectly for two
3318*5a6e8488SAndroid Build Coastguard Workerreasons: first, my machine has 16 cores, and second, the [AFL++][125] docs
3319*5a6e8488SAndroid Build Coastguard Workerrecommend 4 parallel fuzzers, at least, to run different "power schedules."
3320*5a6e8488SAndroid Build Coastguard Worker
3321*5a6e8488SAndroid Build Coastguard Worker### Convenience
3322*5a6e8488SAndroid Build Coastguard Worker
3323*5a6e8488SAndroid Build Coastguard WorkerThe preprepared input corpuses are contained in the
3324*5a6e8488SAndroid Build Coastguard Worker`tests/fuzzing/bc_inputs{1,2,3}/`, and `tests/fuzzing/dc_inputs` directories.
3325*5a6e8488SAndroid Build Coastguard WorkerThere are three `bc` directories and only one `dc` directory because `bc`'s
3326*5a6e8488SAndroid Build Coastguard Workerinput corpuses are about three times as large, and `bc` is a larger program;
3327*5a6e8488SAndroid Build Coastguard Workerit's going to need much more fuzzing.
3328*5a6e8488SAndroid Build Coastguard Worker
3329*5a6e8488SAndroid Build Coastguard Worker(They do share code though, so fuzzing all of them still tests a lot of the same
3330*5a6e8488SAndroid Build Coastguard Workermath code.)
3331*5a6e8488SAndroid Build Coastguard Worker
3332*5a6e8488SAndroid Build Coastguard WorkerThe next feature of convenience is the [`scripts/fuzz_prep.sh`][119] script. It
3333*5a6e8488SAndroid Build Coastguard Workerassumes the existence of `afl-clang-lto` in the `$PATH`, but if that exists, it
3334*5a6e8488SAndroid Build Coastguard Workerautomatically configures and builds `bc` with a fuzz-ideal build.
3335*5a6e8488SAndroid Build Coastguard Worker
3336*5a6e8488SAndroid Build Coastguard WorkerA fuzz-ideal build has several things:
3337*5a6e8488SAndroid Build Coastguard Worker
3338*5a6e8488SAndroid Build Coastguard Worker* `afl-clang-lto` as the compiler. (See [AFL++ Quickstart][129].)
3339*5a6e8488SAndroid Build Coastguard Worker* Debug mode, to crash as easily as possible.
3340*5a6e8488SAndroid Build Coastguard Worker* Full optimization (including [Link-Time Optimization][126]), for performance.
3341*5a6e8488SAndroid Build Coastguard Worker* [AFL++][125]'s deferred initialization (see [Fuzzing Performance][127] above).
3342*5a6e8488SAndroid Build Coastguard Worker* And `AFL_HARDEN=1` during the build to harden the build. See the [AFL++][125]
3343*5a6e8488SAndroid Build Coastguard Worker  documentation for more information.
3344*5a6e8488SAndroid Build Coastguard Worker
3345*5a6e8488SAndroid Build Coastguard WorkerThere is one big thing that a fuzz-ideal build does *not* have: it does not use
3346*5a6e8488SAndroid Build Coastguard Worker[AFL++][125]'s `libdislocator.so`. This is because `libdislocator.so` crashes if
3347*5a6e8488SAndroid Build Coastguard Workerit fails to allocate memory. I do not want to consider those as crashes because
3348*5a6e8488SAndroid Build Coastguard Workermy `bc` does, in fact, handle them gracefully by exiting with a set error code.
3349*5a6e8488SAndroid Build Coastguard WorkerSo `libdislocator.so` is not an option.
3350*5a6e8488SAndroid Build Coastguard Worker
3351*5a6e8488SAndroid Build Coastguard WorkerHowever, to add to [`scripts/fuzz_prep.sh`][119] making a fuzz-ideal build, in
3352*5a6e8488SAndroid Build Coastguard Worker`tests/fuzzing/`, there are two `yaml` files: [`tests/fuzzing/bc_afl.yaml`][120]
3353*5a6e8488SAndroid Build Coastguard Workerand [`tests/fuzzing/bc_afl_continue.yaml`][121]. These files are meant to be
3354*5a6e8488SAndroid Build Coastguard Workerused with [`tmux`][122] and [`tmuxp`][123]. While other programmers will have to
3355*5a6e8488SAndroid Build Coastguard Workeradjust the `start_directory` item, once it is adjusted, then using this command:
3356*5a6e8488SAndroid Build Coastguard Worker
3357*5a6e8488SAndroid Build Coastguard Worker```
3358*5a6e8488SAndroid Build Coastguard Workertmuxp load tests/fuzzing/bc_afl.yaml
3359*5a6e8488SAndroid Build Coastguard Worker```
3360*5a6e8488SAndroid Build Coastguard Worker
3361*5a6e8488SAndroid Build Coastguard Workerwill start fuzzing.
3362*5a6e8488SAndroid Build Coastguard Worker
3363*5a6e8488SAndroid Build Coastguard WorkerIn other words, to start fuzzing, the sequence is:
3364*5a6e8488SAndroid Build Coastguard Worker
3365*5a6e8488SAndroid Build Coastguard Worker```
3366*5a6e8488SAndroid Build Coastguard Worker./scripts/fuzz_prep.sh
3367*5a6e8488SAndroid Build Coastguard Workertmuxp load tests/fuzzing/bc_afl.yaml
3368*5a6e8488SAndroid Build Coastguard Worker```
3369*5a6e8488SAndroid Build Coastguard Worker
3370*5a6e8488SAndroid Build Coastguard WorkerDoing that will load, in `tmux`, 16 separate instances of [AFL++][125], 12 on
3371*5a6e8488SAndroid Build Coastguard Worker`bc` and 4 on `dc`. The outputs will be put into the
3372*5a6e8488SAndroid Build Coastguard Worker`tests/fuzzing/bc_outputs{1,2,3}/` and `tests/fuzzing/dc_outputs/` directories.
3373*5a6e8488SAndroid Build Coastguard Worker
3374*5a6e8488SAndroid Build Coastguard Worker(Note that loading that config will also delete all unsaved [AFL++][125] output
3375*5a6e8488SAndroid Build Coastguard Workerfrom the output directories.)
3376*5a6e8488SAndroid Build Coastguard Worker
3377*5a6e8488SAndroid Build Coastguard WorkerSometimes, [AFL++][125] will report crashes when there are none. When crashes
3378*5a6e8488SAndroid Build Coastguard Workerare reported, I always run the following command:
3379*5a6e8488SAndroid Build Coastguard Worker
3380*5a6e8488SAndroid Build Coastguard Worker```
3381*5a6e8488SAndroid Build Coastguard Worker./scripts/afl.py <dir>
3382*5a6e8488SAndroid Build Coastguard Worker```
3383*5a6e8488SAndroid Build Coastguard Worker
3384*5a6e8488SAndroid Build Coastguard Workerwhere `dir` is one of `bc1`, `bc2`, `bc3`, or `dc`, depending on which of the
3385*5a6e8488SAndroid Build Coastguard Worker16 instances reported the crash. If it was one of the first four (`bc11` through
3386*5a6e8488SAndroid Build Coastguard Worker`bc14`), I use `bc1`. If it was one of the second four (`bc21` through `bc24`, I
3387*5a6e8488SAndroid Build Coastguard Workeruse `bc2`. If it was one of the third four (`bc31` through `bc34`, I use `bc3`.
3388*5a6e8488SAndroid Build Coastguard WorkerAnd if it was `dc`, I use `dc`.
3389*5a6e8488SAndroid Build Coastguard Worker
3390*5a6e8488SAndroid Build Coastguard WorkerThe [`scripts/afl.py`][94] script will report whether [AFL++][125] correctly
3391*5a6e8488SAndroid Build Coastguard Workerreported a crash or not. If so, it will copy the crashing test case to
3392*5a6e8488SAndroid Build Coastguard Worker`.test.txt` and tell you whether it was from running it as a file or through
3393*5a6e8488SAndroid Build Coastguard Worker`stdin`.
3394*5a6e8488SAndroid Build Coastguard Worker
3395*5a6e8488SAndroid Build Coastguard WorkerFrom there, I personally always investigate the crash and fix it. Then, when the
3396*5a6e8488SAndroid Build Coastguard Workercrash is fixed, I either move `.test.txt` to `tests/{bc,dc}/errors/<idx>.txt` as
3397*5a6e8488SAndroid Build Coastguard Workeran error test (if it produces an error) or I create a new
3398*5a6e8488SAndroid Build Coastguard Worker`tests/{bc,dc}/misc<idx>.txt` test for it and a corresponding results file. (See
3399*5a6e8488SAndroid Build Coastguard Worker[Test Suite][124] for more information about the test suite.) In either case,
3400*5a6e8488SAndroid Build Coastguard Worker`<idx>` is the next number for a file in that particular place. For example, if
3401*5a6e8488SAndroid Build Coastguard Workerthe last file in `tests/{bc,dc}/errors/` is `tests/{bc,dc}/errors/18.txt`, I
3402*5a6e8488SAndroid Build Coastguard Workermove `.test.txt` to `tests/bc/error/19.txt`.
3403*5a6e8488SAndroid Build Coastguard Worker
3404*5a6e8488SAndroid Build Coastguard WorkerThen I immediately run [`scripts/afl.py`][94] again to find the next crash
3405*5a6e8488SAndroid Build Coastguard Workerbecause often, [AFL++][125] found multiple test cases that trigger the same
3406*5a6e8488SAndroid Build Coastguard Workercrash. If it finds another, I repeat the process until it is happy.
3407*5a6e8488SAndroid Build Coastguard Worker
3408*5a6e8488SAndroid Build Coastguard WorkerOnce it *is* happy, I do the same `fuzz_prep.sh`, `tmuxp load` sequence and
3409*5a6e8488SAndroid Build Coastguard Workerrestart fuzzing. Why do I restart instead of continuing? Because with the
3410*5a6e8488SAndroid Build Coastguard Workerchanges, the test outputs could be stale and invalid.
3411*5a6e8488SAndroid Build Coastguard Worker
3412*5a6e8488SAndroid Build Coastguard WorkerHowever, there *is* a case where I continue: if [`scripts/afl.py`][94] finds
3413*5a6e8488SAndroid Build Coastguard Workerthat every crash reported by [AFL++][125] is invalid. If that's the case, I can
3414*5a6e8488SAndroid Build Coastguard Workerjust continue with the command:
3415*5a6e8488SAndroid Build Coastguard Worker
3416*5a6e8488SAndroid Build Coastguard Worker```
3417*5a6e8488SAndroid Build Coastguard Workertmuxp load tests/fuzzing/bc_afl_continue.yaml
3418*5a6e8488SAndroid Build Coastguard Worker```
3419*5a6e8488SAndroid Build Coastguard Worker
3420*5a6e8488SAndroid Build Coastguard Worker(Note: I admit that I usually run [`scripts/afl.py`][94] while the fuzzer is
3421*5a6e8488SAndroid Build Coastguard Workerstill running, so often, I don't find a need to continue since there was no
3422*5a6e8488SAndroid Build Coastguard Workerstop. However, the capability is there, if needed.)
3423*5a6e8488SAndroid Build Coastguard Worker
3424*5a6e8488SAndroid Build Coastguard WorkerIn addition, my fuzzing setup, including the `tmux` and `tmuxp` configs,
3425*5a6e8488SAndroid Build Coastguard Workerautomatically set up [AFL++][125] power schedules (see [Fuzzing
3426*5a6e8488SAndroid Build Coastguard WorkerPerformance][127] above). They also set up the parallel fuzzing such that there
3427*5a6e8488SAndroid Build Coastguard Workeris one fuzzer in each group of 4 that does deterministic fuzzing. It's always
3428*5a6e8488SAndroid Build Coastguard Workerthe first one in each group.
3429*5a6e8488SAndroid Build Coastguard Worker
3430*5a6e8488SAndroid Build Coastguard WorkerFor more information about deterministic fuzzing, see the [AFL++][125]
3431*5a6e8488SAndroid Build Coastguard Workerdocumentation.
3432*5a6e8488SAndroid Build Coastguard Worker
3433*5a6e8488SAndroid Build Coastguard Worker### Corpuses
3434*5a6e8488SAndroid Build Coastguard Worker
3435*5a6e8488SAndroid Build Coastguard WorkerI occasionally add to the input corpuses. These files come from new files in the
3436*5a6e8488SAndroid Build Coastguard Worker[Test Suite][124]. In fact, I use soft links when the files are the same.
3437*5a6e8488SAndroid Build Coastguard Worker
3438*5a6e8488SAndroid Build Coastguard WorkerHowever, when I add new files to an input corpus, I sometimes reduce the size of
3439*5a6e8488SAndroid Build Coastguard Workerthe file by removing some redundancies.
3440*5a6e8488SAndroid Build Coastguard Worker
3441*5a6e8488SAndroid Build Coastguard WorkerAnd then, when adding to the `bc` corpuses, I try to add them evenly so that
3442*5a6e8488SAndroid Build Coastguard Workereach corpus will take about the same amount of time to get to a finished state.
3443*5a6e8488SAndroid Build Coastguard Worker
3444*5a6e8488SAndroid Build Coastguard Worker### [AFL++][125] Quickstart
3445*5a6e8488SAndroid Build Coastguard Worker
3446*5a6e8488SAndroid Build Coastguard WorkerThe way [AFL++][125] works is complicated.
3447*5a6e8488SAndroid Build Coastguard Worker
3448*5a6e8488SAndroid Build Coastguard WorkerFirst, it is the one to invoke the compiler. It leverages the compiler to add
3449*5a6e8488SAndroid Build Coastguard Workercode to the binary to help it know when certain branches are taken.
3450*5a6e8488SAndroid Build Coastguard Worker
3451*5a6e8488SAndroid Build Coastguard WorkerThen, when fuzzing, it uses that branch information to generate information
3452*5a6e8488SAndroid Build Coastguard Workerabout the "path" that was taken through the binary.
3453*5a6e8488SAndroid Build Coastguard Worker
3454*5a6e8488SAndroid Build Coastguard WorkerI don't know what AFL++ counts as a new path, but each new path is added to an
3455*5a6e8488SAndroid Build Coastguard Workeroutput corpus, and it is later used as a springboard to find new paths.
3456*5a6e8488SAndroid Build Coastguard Worker
3457*5a6e8488SAndroid Build Coastguard WorkerThis is what makes AFL++ so effective: it's not just blindly thrashing a binary;
3458*5a6e8488SAndroid Build Coastguard Workerit adapts to the binary by leveraging information about paths.
3459*5a6e8488SAndroid Build Coastguard Worker
3460*5a6e8488SAndroid Build Coastguard Worker### Fuzzing Runs
3461*5a6e8488SAndroid Build Coastguard Worker
3462*5a6e8488SAndroid Build Coastguard WorkerFor doing a fuzzing run, I expect about a week or two where my computer is
3463*5a6e8488SAndroid Build Coastguard Workerbasically unusable, except for text editing and light web browsing.
3464*5a6e8488SAndroid Build Coastguard Worker
3465*5a6e8488SAndroid Build Coastguard WorkerYes, it can take two weeks for me to do a full fuzzing run, and that does *not*
3466*5a6e8488SAndroid Build Coastguard Workerinclude the time needed to find and fix crashes; it only counts the time on the
3467*5a6e8488SAndroid Build Coastguard Worker*last* run, the one that does not find any crashes. This means that the entire
3468*5a6e8488SAndroid Build Coastguard Workerprocess can take a month or more.
3469*5a6e8488SAndroid Build Coastguard Worker
3470*5a6e8488SAndroid Build Coastguard WorkerWhat I use as an indicator that the fuzzing run is good enough is when the
3471*5a6e8488SAndroid Build Coastguard Workernumber of "Pending" paths (see [AFL++ Quickstart][129] above) for all fuzzer
3472*5a6e8488SAndroid Build Coastguard Workerinstances, except maybe the deterministic instances, is below 50. And even then,
3473*5a6e8488SAndroid Build Coastguard WorkerI try to let deterministic instances get that far as well.
3474*5a6e8488SAndroid Build Coastguard Worker
3475*5a6e8488SAndroid Build Coastguard WorkerYou can see how many pending paths are left in the "path geometry" section of
3476*5a6e8488SAndroid Build Coastguard Workerthe [AFL++][125] dashboard.
3477*5a6e8488SAndroid Build Coastguard Worker
3478*5a6e8488SAndroid Build Coastguard WorkerAlso, to make [AFL++][125] quit, you need to send it a `SIGINT`, either with
3479*5a6e8488SAndroid Build Coastguard Worker`Ctrl+c` or some other method. It will not quit until you tell it to.
3480*5a6e8488SAndroid Build Coastguard Worker
3481*5a6e8488SAndroid Build Coastguard Worker### Radamsa
3482*5a6e8488SAndroid Build Coastguard Worker
3483*5a6e8488SAndroid Build Coastguard WorkerI rarely use [Radamsa][99] instead of [AFL++][125]. In fact, it's only happened
3484*5a6e8488SAndroid Build Coastguard Workeronce.
3485*5a6e8488SAndroid Build Coastguard Worker
3486*5a6e8488SAndroid Build Coastguard WorkerThe reason I use [Radamsa][99] instead of [AFL++][125] is because it is easier
3487*5a6e8488SAndroid Build Coastguard Workerto use with varying command-line arguments, which was needed for testing `bc`'s
3488*5a6e8488SAndroid Build Coastguard Workercommand-line expression parsing code, and [AFL++][125] is best when testing
3489*5a6e8488SAndroid Build Coastguard Workerinput from `stdin`.
3490*5a6e8488SAndroid Build Coastguard Worker
3491*5a6e8488SAndroid Build Coastguard Worker[`scripts/radamsa.sh`][100] does also do fuzzing on the [AFL++][125] inputs, but
3492*5a6e8488SAndroid Build Coastguard Workerit's not as effective at that, so I don't really use it for that either.
3493*5a6e8488SAndroid Build Coastguard Worker
3494*5a6e8488SAndroid Build Coastguard Worker[`scripts/radamsa.sh`][100] and [Radamsa][99] were only really used once; I have
3495*5a6e8488SAndroid Build Coastguard Workernot had to touch the command-line expression parsing code since.
3496*5a6e8488SAndroid Build Coastguard Worker
3497*5a6e8488SAndroid Build Coastguard Worker### [AddressSanitizer][21] with Fuzzing
3498*5a6e8488SAndroid Build Coastguard Worker
3499*5a6e8488SAndroid Build Coastguard WorkerOne advantage of using [AFL++][125] is that it saves every test case that
3500*5a6e8488SAndroid Build Coastguard Workergenerated a new path (see [AFL++ Quickstart][129] above), and it doesn't delete
3501*5a6e8488SAndroid Build Coastguard Workerthem when the user makes it quit.
3502*5a6e8488SAndroid Build Coastguard Worker
3503*5a6e8488SAndroid Build Coastguard WorkerKeeping them around is not a good idea, for several reasons:
3504*5a6e8488SAndroid Build Coastguard Worker
3505*5a6e8488SAndroid Build Coastguard Worker* They are frequently large.
3506*5a6e8488SAndroid Build Coastguard Worker* There are a lot of them.
3507*5a6e8488SAndroid Build Coastguard Worker* They go stale; after `bc` is changed, the generated paths may not be valid
3508*5a6e8488SAndroid Build Coastguard Worker  anymore.
3509*5a6e8488SAndroid Build Coastguard Worker
3510*5a6e8488SAndroid Build Coastguard WorkerHowever, before they are deleted, they can definitely be leveraged for even
3511*5a6e8488SAndroid Build Coastguard Worker*more* bug squashing by running *all* of the paths through a build of `bc` with
3512*5a6e8488SAndroid Build Coastguard Worker[AddressSanitizer][21].
3513*5a6e8488SAndroid Build Coastguard Worker
3514*5a6e8488SAndroid Build Coastguard WorkerThis can easily be done with these four commands:
3515*5a6e8488SAndroid Build Coastguard Worker
3516*5a6e8488SAndroid Build Coastguard Worker```
3517*5a6e8488SAndroid Build Coastguard Worker./scripts/fuzz_prep.sh -a
3518*5a6e8488SAndroid Build Coastguard Worker./scripts/afl.py --asan bc1
3519*5a6e8488SAndroid Build Coastguard Worker./scripts/afl.py --asan bc2
3520*5a6e8488SAndroid Build Coastguard Worker./scripts/afl.py --asan bc3
3521*5a6e8488SAndroid Build Coastguard Worker./scripts/afl.py --asan dc
3522*5a6e8488SAndroid Build Coastguard Worker```
3523*5a6e8488SAndroid Build Coastguard Worker
3524*5a6e8488SAndroid Build Coastguard Worker(By the way, the last four commands could be run in separate terminals to do the
3525*5a6e8488SAndroid Build Coastguard Workerprocessing in parallel.)
3526*5a6e8488SAndroid Build Coastguard Worker
3527*5a6e8488SAndroid Build Coastguard WorkerThese commands build an [ASan][21]-enabled build of `bc` and `dc` and then they
3528*5a6e8488SAndroid Build Coastguard Workerrun `bc` and `dc` on all of the found crashes and path output corpuses. This is
3529*5a6e8488SAndroid Build Coastguard Workerto check that no path or crash has found any memory errors, including memory
3530*5a6e8488SAndroid Build Coastguard Workerleaks.
3531*5a6e8488SAndroid Build Coastguard Worker
3532*5a6e8488SAndroid Build Coastguard WorkerBecause the output corpuses can contain test cases that generate infinite loops
3533*5a6e8488SAndroid Build Coastguard Workerin `bc` or `dc`, [`scripts/afl.py`][94] has a timeout of 8 seconds, which is far
3534*5a6e8488SAndroid Build Coastguard Workergreater than the timeout that [AFL++][125] uses and should be enough to catch
3535*5a6e8488SAndroid Build Coastguard Workerany crash.
3536*5a6e8488SAndroid Build Coastguard Worker
3537*5a6e8488SAndroid Build Coastguard WorkerIf [AFL++][125] fails to find crashes *and* [ASan][21] fails to find memory
3538*5a6e8488SAndroid Build Coastguard Workererrors on the outputs of [AFL++][125], that is an excellent indicator of very
3539*5a6e8488SAndroid Build Coastguard Workerfew bugs in `bc`, and a release can be made with confidence.
3540*5a6e8488SAndroid Build Coastguard Worker
3541*5a6e8488SAndroid Build Coastguard Worker## Code Concepts
3542*5a6e8488SAndroid Build Coastguard Worker
3543*5a6e8488SAndroid Build Coastguard WorkerThis section is about concepts that, if understood, will make it easier to
3544*5a6e8488SAndroid Build Coastguard Workerunderstand the code as it is written.
3545*5a6e8488SAndroid Build Coastguard Worker
3546*5a6e8488SAndroid Build Coastguard WorkerThe concepts in this section are not found in a single source file, but they are
3547*5a6e8488SAndroid Build Coastguard Workerlittered throughout the code. That's why I am writing them all down in a single
3548*5a6e8488SAndroid Build Coastguard Workerplace.
3549*5a6e8488SAndroid Build Coastguard Worker
3550*5a6e8488SAndroid Build Coastguard Worker### POSIX Mode
3551*5a6e8488SAndroid Build Coastguard Worker
3552*5a6e8488SAndroid Build Coastguard WorkerPOSIX mode is `bc`-only.
3553*5a6e8488SAndroid Build Coastguard Worker
3554*5a6e8488SAndroid Build Coastguard WorkerIn fact, POSIX mode is two different modes: Standard Mode and Warning Mode.
3555*5a6e8488SAndroid Build Coastguard WorkerThese modes are designed to help users write POSIX-compatible `bc` scripts.
3556*5a6e8488SAndroid Build Coastguard Worker
3557*5a6e8488SAndroid Build Coastguard Worker#### Standard Mode
3558*5a6e8488SAndroid Build Coastguard Worker
3559*5a6e8488SAndroid Build Coastguard WorkerStandard Mode is activated with the `-s` or `--standard` flags.
3560*5a6e8488SAndroid Build Coastguard Worker
3561*5a6e8488SAndroid Build Coastguard WorkerIn this mode, `bc` will error if any constructs are used that are not strictly
3562*5a6e8488SAndroid Build Coastguard Workercompatible with the [POSIX `bc` specification][2].
3563*5a6e8488SAndroid Build Coastguard Worker
3564*5a6e8488SAndroid Build Coastguard Worker#### Warning Mode
3565*5a6e8488SAndroid Build Coastguard Worker
3566*5a6e8488SAndroid Build Coastguard WorkerWarning Mode is activated with the `-w` or `--warn` flags.
3567*5a6e8488SAndroid Build Coastguard Worker
3568*5a6e8488SAndroid Build Coastguard WorkerIn this mode, `bc` will issue warnings, but continue, if any constructs are used
3569*5a6e8488SAndroid Build Coastguard Workerthat are not strictly compatible with the [POSIX `bc` specification][2].
3570*5a6e8488SAndroid Build Coastguard Worker
3571*5a6e8488SAndroid Build Coastguard Worker### Memory Management
3572*5a6e8488SAndroid Build Coastguard Worker
3573*5a6e8488SAndroid Build Coastguard WorkerThe memory management in `bc` is simple: everything is owned by one thing.
3574*5a6e8488SAndroid Build Coastguard Worker
3575*5a6e8488SAndroid Build Coastguard WorkerIf something is in a vector, it is owned by that vector.
3576*5a6e8488SAndroid Build Coastguard Worker
3577*5a6e8488SAndroid Build Coastguard WorkerIf something is contained in a struct, it is owned by that struct with one
3578*5a6e8488SAndroid Build Coastguard Workerexception: structs can be given pointers to other things, but only if those
3579*5a6e8488SAndroid Build Coastguard Workerother things will outlast the struct itself.
3580*5a6e8488SAndroid Build Coastguard Worker
3581*5a6e8488SAndroid Build Coastguard WorkerAs an example, the `BcParse` struct has a pointer to the one `BcProgram` in
3582*5a6e8488SAndroid Build Coastguard Worker`bc`. This is okay because the program is initialized first and deallocated
3583*5a6e8488SAndroid Build Coastguard Workerlast.
3584*5a6e8488SAndroid Build Coastguard Worker
3585*5a6e8488SAndroid Build Coastguard WorkerIn other words, it's simple: if a field in a struct is a pointer, then unless
3586*5a6e8488SAndroid Build Coastguard Workerthat pointer is directly allocated by the struct (like the vector array or the
3587*5a6e8488SAndroid Build Coastguard Workernumber limb array), that struct does not own the item at that pointer.
3588*5a6e8488SAndroid Build Coastguard WorkerOtherwise, the struct *does* own the item.
3589*5a6e8488SAndroid Build Coastguard Worker
3590*5a6e8488SAndroid Build Coastguard Worker### [Async-Signal-Safe][115] Signal Handling
3591*5a6e8488SAndroid Build Coastguard Worker
3592*5a6e8488SAndroid Build Coastguard Worker`bc` is not the typical Unix utility. Most Unix utilities are I/O bound, but
3593*5a6e8488SAndroid Build Coastguard Worker`bc` is, by and large, CPU-bound. This has several consequences, but the biggest
3594*5a6e8488SAndroid Build Coastguard Workeris that there is no easy way to allow signals to interrupt it.
3595*5a6e8488SAndroid Build Coastguard Worker
3596*5a6e8488SAndroid Build Coastguard WorkerThis consequence is not obvious, but it comes from the fact that a lot of I/O
3597*5a6e8488SAndroid Build Coastguard Workeroperations can be interrupted and return [`EINTR`][198]. This makes such I/O
3598*5a6e8488SAndroid Build Coastguard Workercalls natural places for allowing signals to interrupt execution, even when the
3599*5a6e8488SAndroid Build Coastguard Workersignal comes during execution, and not interrupting I/O calls. The way this is
3600*5a6e8488SAndroid Build Coastguard Workerdone is setting a flag in the signal handler, which is checked around the time
3601*5a6e8488SAndroid Build Coastguard Workerof the I/O call, when it is convenient.
3602*5a6e8488SAndroid Build Coastguard Worker
3603*5a6e8488SAndroid Build Coastguard WorkerAlternatively, I/O bound programs can use the [self-pipe trick][199].
3604*5a6e8488SAndroid Build Coastguard Worker
3605*5a6e8488SAndroid Build Coastguard WorkerNeither of these are possible in `bc` because the execution of math code can
3606*5a6e8488SAndroid Build Coastguard Workertake a long time. If a signal arrives during this long execution time, setting a
3607*5a6e8488SAndroid Build Coastguard Workerflag like an I/O bound application and waiting until the next I/O call could
3608*5a6e8488SAndroid Build Coastguard Workertake seconds, minutes, hours, or even days. (Last I checked, my `bc` takes a
3609*5a6e8488SAndroid Build Coastguard Workerweek to calculate a million digits of pi, and it's not slow as far as `bc`
3610*5a6e8488SAndroid Build Coastguard Workerimplementations go.)
3611*5a6e8488SAndroid Build Coastguard Worker
3612*5a6e8488SAndroid Build Coastguard WorkerThus, using just the technique of setting the flag just will not work for an
3613*5a6e8488SAndroid Build Coastguard Workerinteractive calculator.
3614*5a6e8488SAndroid Build Coastguard Worker
3615*5a6e8488SAndroid Build Coastguard WorkerWell, it can, but it requires a lot of code and massive inefficiencies. I know
3616*5a6e8488SAndroid Build Coastguard Workerthis because that was the original implementation.
3617*5a6e8488SAndroid Build Coastguard Worker
3618*5a6e8488SAndroid Build Coastguard WorkerThe original implementation set a flag and just exit the signal handler. Then,
3619*5a6e8488SAndroid Build Coastguard Workeron just about every loop header, I have a check for the signal flag. These
3620*5a6e8488SAndroid Build Coastguard Workerchecks happened on every iteration of every loop. It was a massive waste because
3621*5a6e8488SAndroid Build Coastguard Workerit was polling, and [polling is evil][200].
3622*5a6e8488SAndroid Build Coastguard Worker
3623*5a6e8488SAndroid Build Coastguard WorkerSo for version [3.0.0][32], I expended a lot of effort to change the
3624*5a6e8488SAndroid Build Coastguard Workerimplementation.
3625*5a6e8488SAndroid Build Coastguard Worker
3626*5a6e8488SAndroid Build Coastguard WorkerIn the new system, code *outside* the signal handler sets a flag (`vm.sig_lock`)
3627*5a6e8488SAndroid Build Coastguard Workerto tell the signal handler whether it can use `longjmp()` to stop the current
3628*5a6e8488SAndroid Build Coastguard Workerexecution. If so, it does. If not, it sets a flag, which then is used by the
3629*5a6e8488SAndroid Build Coastguard Workercode outside the signal handler that set the `vm.sig_lock` flag. When that code
3630*5a6e8488SAndroid Build Coastguard Workerunsets `vm.sig_lock`, it checks to see if a signal happened, and if so, that
3631*5a6e8488SAndroid Build Coastguard Workercode executes the `longjmp()` and stops the current execution.
3632*5a6e8488SAndroid Build Coastguard Worker
3633*5a6e8488SAndroid Build Coastguard WorkerOther than that, the rest of the interrupt-based implementation is best
3634*5a6e8488SAndroid Build Coastguard Workerdescribed in the [Error Handling][97].
3635*5a6e8488SAndroid Build Coastguard Worker
3636*5a6e8488SAndroid Build Coastguard WorkerHowever, there are rules for signal handlers that I must lay out.
3637*5a6e8488SAndroid Build Coastguard Worker
3638*5a6e8488SAndroid Build Coastguard WorkerFirst, signal handlers can only call [async-signal-safe][115] functions.
3639*5a6e8488SAndroid Build Coastguard Worker
3640*5a6e8488SAndroid Build Coastguard WorkerSecond, any field set or read by both the signal handler and normal code must be
3641*5a6e8488SAndroid Build Coastguard Workera `volatile sig_atomic_t`.
3642*5a6e8488SAndroid Build Coastguard Worker
3643*5a6e8488SAndroid Build Coastguard WorkerThird, when setting such fields, they must be set to constants and no math can
3644*5a6e8488SAndroid Build Coastguard Workerbe done on them. This restriction and the above restriction exist in order to
3645*5a6e8488SAndroid Build Coastguard Workerensure that the setting of the fields is always atomic with respect to signals.
3646*5a6e8488SAndroid Build Coastguard Worker
3647*5a6e8488SAndroid Build Coastguard WorkerThese rules exist for *any* code using Unix signal handlers, not just `bc`.
3648*5a6e8488SAndroid Build Coastguard Worker
3649*5a6e8488SAndroid Build Coastguard Worker#### Vectors and Numbers
3650*5a6e8488SAndroid Build Coastguard Worker
3651*5a6e8488SAndroid Build Coastguard WorkerVectors and numbers needed special consideration with the interrupt-based signal
3652*5a6e8488SAndroid Build Coastguard Workerhandling.
3653*5a6e8488SAndroid Build Coastguard Worker
3654*5a6e8488SAndroid Build Coastguard WorkerWhen vectors and numbers are about to allocate, or *reallocate* their arrays,
3655*5a6e8488SAndroid Build Coastguard Workerthey need to lock signals to ensure that they do not call `malloc()` and friends
3656*5a6e8488SAndroid Build Coastguard Workerand get interrupted by a signal because, as you will see in the [Error
3657*5a6e8488SAndroid Build Coastguard WorkerHandling][97] section, `longjmp()` cannot be used in a signal handler if it may
3658*5a6e8488SAndroid Build Coastguard Workerbe able to interrupt a non-[async-signal-safe][115] function like `malloc()` and
3659*5a6e8488SAndroid Build Coastguard Workerfriends.
3660*5a6e8488SAndroid Build Coastguard Worker
3661*5a6e8488SAndroid Build Coastguard Worker### Asserts
3662*5a6e8488SAndroid Build Coastguard Worker
3663*5a6e8488SAndroid Build Coastguard WorkerIf you asked me what procedure is used the most in `bc`, I would reply without
3664*5a6e8488SAndroid Build Coastguard Workerhesitation, "`assert()`."
3665*5a6e8488SAndroid Build Coastguard Worker
3666*5a6e8488SAndroid Build Coastguard WorkerI use `assert()` everywhere. In fact, it is what made [fuzzing][82] with
3667*5a6e8488SAndroid Build Coastguard Worker[AFL++][125] so effective. [AFL++][125] is incredibly good at finding crashes,
3668*5a6e8488SAndroid Build Coastguard Workerand a failing `assert()` counts as one.
3669*5a6e8488SAndroid Build Coastguard Worker
3670*5a6e8488SAndroid Build Coastguard WorkerSo while a lot of bad bugs might have corrupted data and *not* caused crashes,
3671*5a6e8488SAndroid Build Coastguard Workerbecause I put in so many `assert()`'s, they were *turned into* crashing bugs,
3672*5a6e8488SAndroid Build Coastguard Workerand [AFL++][125] found them.
3673*5a6e8488SAndroid Build Coastguard Worker
3674*5a6e8488SAndroid Build Coastguard WorkerBy far, the most bugs it found this way was in the `bc` parser. (See the [`bc`
3675*5a6e8488SAndroid Build Coastguard WorkerParsing][110] for more information.) And even though I was careful to put
3676*5a6e8488SAndroid Build Coastguard Worker`assert()`'s everywhere, most parser bugs manifested during execution of
3677*5a6e8488SAndroid Build Coastguard Workerbytecode because the virtual machine assumes the bytecode is valid.
3678*5a6e8488SAndroid Build Coastguard Worker
3679*5a6e8488SAndroid Build Coastguard WorkerSidenote: one of those bugs caused an infinite recursion when running the sine
3680*5a6e8488SAndroid Build Coastguard Worker(`s()`) function in the math library, so yes, parser bugs can be *very* weird.
3681*5a6e8488SAndroid Build Coastguard Worker
3682*5a6e8488SAndroid Build Coastguard WorkerAnyway, the way I did `assert()`'s was like this: whenever I realized that I
3683*5a6e8488SAndroid Build Coastguard Workerhad put assumptions into the code, I would put an `assert()` there to test it
3684*5a6e8488SAndroid Build Coastguard Worker**and** to *document* it.
3685*5a6e8488SAndroid Build Coastguard Worker
3686*5a6e8488SAndroid Build Coastguard WorkerYes, documentation. In fact, by far the best documentation of the code in `bc`
3687*5a6e8488SAndroid Build Coastguard Workeris actually the `assert()`'s. The only time I would not put an `assert()` to
3688*5a6e8488SAndroid Build Coastguard Workertest an assumption is if that assumption was already tested by an `assert()`
3689*5a6e8488SAndroid Build Coastguard Workerearlier.
3690*5a6e8488SAndroid Build Coastguard Worker
3691*5a6e8488SAndroid Build Coastguard WorkerAs an example, if a function calls another function and passes a pointer that
3692*5a6e8488SAndroid Build Coastguard Workerthe caller previously `assert()`'ed was *not* `NULL`, then the callee does not
3693*5a6e8488SAndroid Build Coastguard Workerhave to `assert()` it too, unless *also* called by another function that does
3694*5a6e8488SAndroid Build Coastguard Workernot `assert()` that.
3695*5a6e8488SAndroid Build Coastguard Worker
3696*5a6e8488SAndroid Build Coastguard WorkerAt first glance, it may seem like putting asserts for pointers being non-`NULL`
3697*5a6e8488SAndroid Build Coastguard Workereverywhere would actually be good, but unfortunately, not for fuzzing. Each
3698*5a6e8488SAndroid Build Coastguard Worker`assert()` is a branch, and [AFL++][125] rates its own effectiveness based on
3699*5a6e8488SAndroid Build Coastguard Workerhow many branches it covers. If there are too many `assert()`'s, it may think
3700*5a6e8488SAndroid Build Coastguard Workerthat it is not being effective and that more fuzzing is needed.
3701*5a6e8488SAndroid Build Coastguard Worker
3702*5a6e8488SAndroid Build Coastguard WorkerThis means that `assert()`'s show up most often in two places: function
3703*5a6e8488SAndroid Build Coastguard Workerpreconditions and function postconditions.
3704*5a6e8488SAndroid Build Coastguard Worker
3705*5a6e8488SAndroid Build Coastguard WorkerFunction preconditions are `assert()`'s that test conditions relating to the
3706*5a6e8488SAndroid Build Coastguard Workerarguments a function was given. They appear at the top of the function, usually
3707*5a6e8488SAndroid Build Coastguard Workerbefore anything else (except maybe initializing a local variable).
3708*5a6e8488SAndroid Build Coastguard Worker
3709*5a6e8488SAndroid Build Coastguard WorkerFunction postconditions are `assert()`'s that test the return values or other
3710*5a6e8488SAndroid Build Coastguard Workerconditions when a function exits. These are at the bottom of a function or just
3711*5a6e8488SAndroid Build Coastguard Workerbefore a `return` statement.
3712*5a6e8488SAndroid Build Coastguard Worker
3713*5a6e8488SAndroid Build Coastguard WorkerThe other `assert()`'s cover various miscellaneous assumptions.
3714*5a6e8488SAndroid Build Coastguard Worker
3715*5a6e8488SAndroid Build Coastguard WorkerIf you change the code, I ***HIGHLY*** suggest that you use `assert()`'s to
3716*5a6e8488SAndroid Build Coastguard Workerdocument your assumptions. And don't remove them when [AFL++][125] gleefully
3717*5a6e8488SAndroid Build Coastguard Workercrashes `bc` and `dc` over and over again.
3718*5a6e8488SAndroid Build Coastguard Worker
3719*5a6e8488SAndroid Build Coastguard Worker### Vectors
3720*5a6e8488SAndroid Build Coastguard Worker
3721*5a6e8488SAndroid Build Coastguard WorkerIn `bc`, vectors mean resizable arrays, and they are the most fundamental piece
3722*5a6e8488SAndroid Build Coastguard Workerof code in the entire codebase.
3723*5a6e8488SAndroid Build Coastguard Worker
3724*5a6e8488SAndroid Build Coastguard WorkerI had previously written a [vector implementation][112], which I used to guide
3725*5a6e8488SAndroid Build Coastguard Workermy decisions, but I wrote a new one so that `bc` would not have a dependency. I
3726*5a6e8488SAndroid Build Coastguard Workeralso didn't make it as sophisticated; the one in `bc` is very simple.
3727*5a6e8488SAndroid Build Coastguard Worker
3728*5a6e8488SAndroid Build Coastguard WorkerVectors store some information about the type that they hold:
3729*5a6e8488SAndroid Build Coastguard Worker
3730*5a6e8488SAndroid Build Coastguard Worker* The size (as returned by `sizeof`).
3731*5a6e8488SAndroid Build Coastguard Worker* An enum designating the destructor.
3732*5a6e8488SAndroid Build Coastguard Worker
3733*5a6e8488SAndroid Build Coastguard WorkerIf the destructor is `BC_DTOR_NONE`, it is counted as the type not having a
3734*5a6e8488SAndroid Build Coastguard Workerdestructor.
3735*5a6e8488SAndroid Build Coastguard Worker
3736*5a6e8488SAndroid Build Coastguard WorkerBut by storing the size, the vector can then allocate `size * cap` bytes, where
3737*5a6e8488SAndroid Build Coastguard Worker`cap` is the capacity. Then, when growing the vector, the `cap` is doubled again
3738*5a6e8488SAndroid Build Coastguard Workerand again until it is bigger than the requested size.
3739*5a6e8488SAndroid Build Coastguard Worker
3740*5a6e8488SAndroid Build Coastguard WorkerBut to store items, or to push items, or even to return items, the vector has to
3741*5a6e8488SAndroid Build Coastguard Workerfigure out where they are, since to it, the array just looks like an array of
3742*5a6e8488SAndroid Build Coastguard Workerbytes.
3743*5a6e8488SAndroid Build Coastguard Worker
3744*5a6e8488SAndroid Build Coastguard WorkerIt does this by calculating a pointer to the underlying type with
3745*5a6e8488SAndroid Build Coastguard Worker`v + (i * size)`, where `v` is the array of bytes, `i` is the index of the
3746*5a6e8488SAndroid Build Coastguard Workerdesired element, and `size` is the size of the underlying type.
3747*5a6e8488SAndroid Build Coastguard Worker
3748*5a6e8488SAndroid Build Coastguard WorkerDoing that, vectors can avoid undefined behavior (because `char` pointers can
3749*5a6e8488SAndroid Build Coastguard Workerbe cast to any other pointer type), while calculating the exact position of
3750*5a6e8488SAndroid Build Coastguard Workerevery element.
3751*5a6e8488SAndroid Build Coastguard Worker
3752*5a6e8488SAndroid Build Coastguard WorkerBecause it can do that, it can figure out where to push new elements by
3753*5a6e8488SAndroid Build Coastguard Workercalculating `v + (len * size)`, where `len` is the number of items actually in
3754*5a6e8488SAndroid Build Coastguard Workerthe vector.
3755*5a6e8488SAndroid Build Coastguard Worker
3756*5a6e8488SAndroid Build Coastguard WorkerBy the way, `len` is different from `cap`. While `cap` is the amount of storage
3757*5a6e8488SAndroid Build Coastguard Worker*available*, `len` is the number of actual elements in the vector at the present
3758*5a6e8488SAndroid Build Coastguard Workerpoint in time.
3759*5a6e8488SAndroid Build Coastguard Worker
3760*5a6e8488SAndroid Build Coastguard WorkerGrowing the vector happens when `len` is equal to `cap` *before* pushing new
3761*5a6e8488SAndroid Build Coastguard Workeritems, not after.
3762*5a6e8488SAndroid Build Coastguard Worker
3763*5a6e8488SAndroid Build Coastguard WorkerTo add a destructor, you need to add an enum item to `BcDtorType` in
3764*5a6e8488SAndroid Build Coastguard Worker[`include/vector.h`][174] and add the actual destructor in the same place as the
3765*5a6e8488SAndroid Build Coastguard Workerenum item in the `bc_vec_dtors[]` array in [`src/data.c`][131].
3766*5a6e8488SAndroid Build Coastguard Worker
3767*5a6e8488SAndroid Build Coastguard Worker#### Pointer Invalidation
3768*5a6e8488SAndroid Build Coastguard Worker
3769*5a6e8488SAndroid Build Coastguard WorkerThere is one big danger with the vectors as currently implemented: pointer
3770*5a6e8488SAndroid Build Coastguard Workerinvalidation.
3771*5a6e8488SAndroid Build Coastguard Worker
3772*5a6e8488SAndroid Build Coastguard WorkerIf a piece of code receives a pointer from a vector, then adds an item to the
3773*5a6e8488SAndroid Build Coastguard Workervector before they finish using the pointer, that code must then update the
3774*5a6e8488SAndroid Build Coastguard Workerpointer from the vector again.
3775*5a6e8488SAndroid Build Coastguard Worker
3776*5a6e8488SAndroid Build Coastguard WorkerThis is because any pointer inside the vector is calculated based off of the
3777*5a6e8488SAndroid Build Coastguard Workerarray in the vector, and when the vector grows, it can `realloc()` the array,
3778*5a6e8488SAndroid Build Coastguard Workerwhich may move it in memory. If that is done, any pointer returned by
3779*5a6e8488SAndroid Build Coastguard Worker`bc_vec_item()`, `bc_vec_top()` and `bc_vec_item_rev()` will be invalid.
3780*5a6e8488SAndroid Build Coastguard Worker
3781*5a6e8488SAndroid Build Coastguard WorkerThis fact was the single most common cause of crashes in the early days of this
3782*5a6e8488SAndroid Build Coastguard Worker`bc`; wherever I have put a comment about pointers becoming invalidated and
3783*5a6e8488SAndroid Build Coastguard Workerupdating them with another call to `bc_vec_item()` and friends, *do **NOT**
3784*5a6e8488SAndroid Build Coastguard Workerremove that code!*
3785*5a6e8488SAndroid Build Coastguard Worker
3786*5a6e8488SAndroid Build Coastguard Worker#### Maps
3787*5a6e8488SAndroid Build Coastguard Worker
3788*5a6e8488SAndroid Build Coastguard WorkerMaps in `bc` are...not.
3789*5a6e8488SAndroid Build Coastguard Worker
3790*5a6e8488SAndroid Build Coastguard WorkerThey are really a combination of two vectors. Those combinations are easily
3791*5a6e8488SAndroid Build Coastguard Workerrecognized in the source because one vector is named `<name>s` (plural), and the
3792*5a6e8488SAndroid Build Coastguard Workerother is named `<name>_map`.
3793*5a6e8488SAndroid Build Coastguard Worker
3794*5a6e8488SAndroid Build Coastguard WorkerThere are currently three, all in `BcProgram`:
3795*5a6e8488SAndroid Build Coastguard Worker
3796*5a6e8488SAndroid Build Coastguard Worker* `fns` and `fn_map` (`bc` functions).
3797*5a6e8488SAndroid Build Coastguard Worker* `vars` and `var_map` (variables).
3798*5a6e8488SAndroid Build Coastguard Worker* `arrs` and `arr_map` (arrays).
3799*5a6e8488SAndroid Build Coastguard Worker
3800*5a6e8488SAndroid Build Coastguard WorkerThey work like this: the `<name>_map` vector holds `BcId`'s, which just holds a
3801*5a6e8488SAndroid Build Coastguard Workerstring and an index. The string is the name of the item, and the index is the
3802*5a6e8488SAndroid Build Coastguard Workerindex of that item in the `<name>s` vector.
3803*5a6e8488SAndroid Build Coastguard Worker
3804*5a6e8488SAndroid Build Coastguard WorkerObviously, I could have just done a linear search for items in the `<name>s`
3805*5a6e8488SAndroid Build Coastguard Workervector, but that would be slow with a lot of functions/variables/arrays.
3806*5a6e8488SAndroid Build Coastguard WorkerInstead, I ensure that whenever an item is inserted into the `<name>_map`
3807*5a6e8488SAndroid Build Coastguard Workervector, the item is inserted in sorted order. This means that the `<name>_map`
3808*5a6e8488SAndroid Build Coastguard Workeris always sorted (by the names of the items).
3809*5a6e8488SAndroid Build Coastguard Worker
3810*5a6e8488SAndroid Build Coastguard WorkerSo when looking up an item in the "map", what is really done is this:
3811*5a6e8488SAndroid Build Coastguard Worker
3812*5a6e8488SAndroid Build Coastguard Worker1.	A binary search is carried out on the names in the `<name>_map` vector.
3813*5a6e8488SAndroid Build Coastguard Worker2.	When one is found, it returns the index in the `<name>_map` vector where the
3814*5a6e8488SAndroid Build Coastguard Worker	item was found.
3815*5a6e8488SAndroid Build Coastguard Worker3.	This index is then used to retrieve the `BcId`.
3816*5a6e8488SAndroid Build Coastguard Worker4.	The index from the `BcId` is then used to index into the `<name>s` vector,
3817*5a6e8488SAndroid Build Coastguard Worker	which returns the *actual* desired item.
3818*5a6e8488SAndroid Build Coastguard Worker
3819*5a6e8488SAndroid Build Coastguard WorkerWhy were the `<name>s` and `<name>_map` vectors not combined for ease? The
3820*5a6e8488SAndroid Build Coastguard Workeranswer is that sometime, when attempting to insert into the "map", code might
3821*5a6e8488SAndroid Build Coastguard Workerfind that something is already there. For example, a function with that name may
3822*5a6e8488SAndroid Build Coastguard Workeralready exist, or the variable might already exist.
3823*5a6e8488SAndroid Build Coastguard Worker
3824*5a6e8488SAndroid Build Coastguard WorkerIf the insert fails, then the name already exists, and the inserting code can
3825*5a6e8488SAndroid Build Coastguard Workerforego creating a new item to put into the vector. However, if there is no item,
3826*5a6e8488SAndroid Build Coastguard Workerthe inserting code must create a new item and insert it.
3827*5a6e8488SAndroid Build Coastguard Worker
3828*5a6e8488SAndroid Build Coastguard WorkerIf the two vectors were combined together, it would not be possible to separate
3829*5a6e8488SAndroid Build Coastguard Workerthe steps such that creating a new item could be avoided if it already exists.
3830*5a6e8488SAndroid Build Coastguard Worker
3831*5a6e8488SAndroid Build Coastguard Worker#### Slabs and Slab Vectors
3832*5a6e8488SAndroid Build Coastguard Worker
3833*5a6e8488SAndroid Build Coastguard Worker`bc` allocates *a lot* of small strings, and small allocations are the toughest
3834*5a6e8488SAndroid Build Coastguard Workerfor general-purpose allocators to handle efficiently.
3835*5a6e8488SAndroid Build Coastguard Worker
3836*5a6e8488SAndroid Build Coastguard WorkerBecause of that reason, I decided to create a system for allocating small
3837*5a6e8488SAndroid Build Coastguard Workerstrings using something that I call a "slab vector" after [slab
3838*5a6e8488SAndroid Build Coastguard Workerallocators][201].
3839*5a6e8488SAndroid Build Coastguard Worker
3840*5a6e8488SAndroid Build Coastguard WorkerThese vectors allocate what I call "slabs," which are just an allocation of a
3841*5a6e8488SAndroid Build Coastguard Workersingle page with a length to tell the slab how much of the slab is used.
3842*5a6e8488SAndroid Build Coastguard Worker
3843*5a6e8488SAndroid Build Coastguard WorkerThe vector itself holds slabs, and when the slab vector is asked to allocate a
3844*5a6e8488SAndroid Build Coastguard Workerstring, it attempts to in the last slab. If that slab cannot do so, it allocates
3845*5a6e8488SAndroid Build Coastguard Workera new slab and allocates from that.
3846*5a6e8488SAndroid Build Coastguard Worker
3847*5a6e8488SAndroid Build Coastguard WorkerThere is one exception: if a string is going to be bigger than 128 bytes, then
3848*5a6e8488SAndroid Build Coastguard Workerthe string is directly allocated, and a slab is created with that pointer and a
3849*5a6e8488SAndroid Build Coastguard Workerlength of `SIZE_MAX`, which tells the slab vector that it is a direct
3850*5a6e8488SAndroid Build Coastguard Workerallocation. Then, the last slab is pushed into the next spot and the new special
3851*5a6e8488SAndroid Build Coastguard Workerslab is put into the vacated spot. This ensures that a non-special slab is
3852*5a6e8488SAndroid Build Coastguard Workeralways last.
3853*5a6e8488SAndroid Build Coastguard Worker
3854*5a6e8488SAndroid Build Coastguard Worker### Command-Line History
3855*5a6e8488SAndroid Build Coastguard Worker
3856*5a6e8488SAndroid Build Coastguard WorkerWhen I first wrote `bc`, I immediately started using it in order to eat my own
3857*5a6e8488SAndroid Build Coastguard Workerdog food.
3858*5a6e8488SAndroid Build Coastguard Worker
3859*5a6e8488SAndroid Build Coastguard WorkerIt sucked, and the biggest reason why was because of the lack of command-line
3860*5a6e8488SAndroid Build Coastguard Workerhistory.
3861*5a6e8488SAndroid Build Coastguard Worker
3862*5a6e8488SAndroid Build Coastguard WorkerAt first, I just dealt with it, not knowing how command-line history might be
3863*5a6e8488SAndroid Build Coastguard Workerimplemented.
3864*5a6e8488SAndroid Build Coastguard Worker
3865*5a6e8488SAndroid Build Coastguard WorkerEventually, I caved and attempted to adapt [`linenoise-mob`][28], which I had
3866*5a6e8488SAndroid Build Coastguard Workerknown about for some time.
3867*5a6e8488SAndroid Build Coastguard Worker
3868*5a6e8488SAndroid Build Coastguard WorkerIt turned out to be easier than I thought; the hardest part was the tedious
3869*5a6e8488SAndroid Build Coastguard Workerrenaming of everything to fit the `bc` naming scheme.
3870*5a6e8488SAndroid Build Coastguard Worker
3871*5a6e8488SAndroid Build Coastguard WorkerUnderstanding command-line history in `bc` is really about understanding VT-100
3872*5a6e8488SAndroid Build Coastguard Workerescape codes, so I would start there.
3873*5a6e8488SAndroid Build Coastguard Worker
3874*5a6e8488SAndroid Build Coastguard WorkerNow, the history implementation of `bc` has been adapted far beyond that initial
3875*5a6e8488SAndroid Build Coastguard Workeradaptation to make the command-line history implementation perfect for `bc`
3876*5a6e8488SAndroid Build Coastguard Workeralone, including integrating it into `bc`'s [Custom I/O][114] and making sure
3877*5a6e8488SAndroid Build Coastguard Workerthat it does not disturb output that did not end with a newline.
3878*5a6e8488SAndroid Build Coastguard Worker
3879*5a6e8488SAndroid Build Coastguard WorkerOn top of that, at one point, I attempted to get history to work on Windows. It
3880*5a6e8488SAndroid Build Coastguard Workerbarely worked after a lot of work and a lot of portability code, but even with
3881*5a6e8488SAndroid Build Coastguard Workerall of that, it does not have at least one feature: multi-line pasting from the
3882*5a6e8488SAndroid Build Coastguard Workerclipboard.
3883*5a6e8488SAndroid Build Coastguard Worker
3884*5a6e8488SAndroid Build Coastguard Worker#### Editline and Readline
3885*5a6e8488SAndroid Build Coastguard Worker
3886*5a6e8488SAndroid Build Coastguard WorkerI also implemented an integration with both editline and readline, though not at
3887*5a6e8488SAndroid Build Coastguard Workerthe same time. This allows distributions that want to use either one in `bc` to
3888*5a6e8488SAndroid Build Coastguard Workerdo so. This enables users to use their `.editrc` and `.inputrc` settings.
3889*5a6e8488SAndroid Build Coastguard Worker
3890*5a6e8488SAndroid Build Coastguard WorkerThe integration is custom to each library, and it does *not* involve any of
3891*5a6e8488SAndroid Build Coastguard Worker`bc`'s custom history code. It also required changes to `bc`'s [Custom
3892*5a6e8488SAndroid Build Coastguard WorkerI/O][114].
3893*5a6e8488SAndroid Build Coastguard Worker
3894*5a6e8488SAndroid Build Coastguard Worker### Error Handling
3895*5a6e8488SAndroid Build Coastguard Worker
3896*5a6e8488SAndroid Build Coastguard WorkerThe error handling on `bc` got an overhaul for version [`3.0.0`][32], and it
3897*5a6e8488SAndroid Build Coastguard Workerbecame one of the things that taught me the most about C in particular and
3898*5a6e8488SAndroid Build Coastguard Workerprogramming in general.
3899*5a6e8488SAndroid Build Coastguard Worker
3900*5a6e8488SAndroid Build Coastguard WorkerBefore then, error handling was manual. Almost all functions returned a
3901*5a6e8488SAndroid Build Coastguard Worker`BcStatus` indicating if an error had occurred. This led to a proliferation of
3902*5a6e8488SAndroid Build Coastguard Workerlines like:
3903*5a6e8488SAndroid Build Coastguard Worker
3904*5a6e8488SAndroid Build Coastguard Worker```
3905*5a6e8488SAndroid Build Coastguard Workerif (BC_ERR(s)) return s;
3906*5a6e8488SAndroid Build Coastguard Worker```
3907*5a6e8488SAndroid Build Coastguard Worker
3908*5a6e8488SAndroid Build Coastguard WorkerIn fact, a quick and dirty count of such lines in version `2.7.2` (the last
3909*5a6e8488SAndroid Build Coastguard Workerversion before [`3.0.0`][32]) turned up 252 occurrences of that sort of line.
3910*5a6e8488SAndroid Build Coastguard Worker
3911*5a6e8488SAndroid Build Coastguard WorkerAnd that didn't even guarantee that return values were checked *everywhere*.
3912*5a6e8488SAndroid Build Coastguard Worker
3913*5a6e8488SAndroid Build Coastguard WorkerBut before I can continue, let me back up a bit.
3914*5a6e8488SAndroid Build Coastguard Worker
3915*5a6e8488SAndroid Build Coastguard WorkerFrom the beginning, I decided that I would not do what GNU `bc` does on errors;
3916*5a6e8488SAndroid Build Coastguard Workerit tries to find a point at which it can recover. Instead, I decided that I
3917*5a6e8488SAndroid Build Coastguard Workerwould have `bc` reset to a clean slate, which I believed, would reduce the
3918*5a6e8488SAndroid Build Coastguard Workernumber of bugs where an unclean state caused errors with continuing execution.
3919*5a6e8488SAndroid Build Coastguard Worker
3920*5a6e8488SAndroid Build Coastguard WorkerSo from the beginning, errors would essentially unwind the stack until they got
3921*5a6e8488SAndroid Build Coastguard Workerto a safe place from which to clean the slate, reset, and ask for more input.
3922*5a6e8488SAndroid Build Coastguard Worker
3923*5a6e8488SAndroid Build Coastguard WorkerWell, if that weren't enough, `bc` also has to handle [POSIX signals][113]. As
3924*5a6e8488SAndroid Build Coastguard Workersuch, it had a signal handler that set a flag. But it could not safely interrupt
3925*5a6e8488SAndroid Build Coastguard Workerexecution, so that's all it could do.
3926*5a6e8488SAndroid Build Coastguard Worker
3927*5a6e8488SAndroid Build Coastguard WorkerIn order to actually respond to the signal, I had to litter checks for the flag
3928*5a6e8488SAndroid Build Coastguard Worker*everywhere* in the code. And I mean *everywhere*. They had to be checked on
3929*5a6e8488SAndroid Build Coastguard Workerevery iteration of *every* loop. They had to be checked going into and out of
3930*5a6e8488SAndroid Build Coastguard Workercertain functions.
3931*5a6e8488SAndroid Build Coastguard Worker
3932*5a6e8488SAndroid Build Coastguard WorkerIt was a mess.
3933*5a6e8488SAndroid Build Coastguard Worker
3934*5a6e8488SAndroid Build Coastguard WorkerBut fortunately for me, signals did the same thing that errors did: they unwound
3935*5a6e8488SAndroid Build Coastguard Workerthe stack to the *same* place.
3936*5a6e8488SAndroid Build Coastguard Worker
3937*5a6e8488SAndroid Build Coastguard WorkerDo you see where I am going with this?
3938*5a6e8488SAndroid Build Coastguard Worker
3939*5a6e8488SAndroid Build Coastguard WorkerIt turns out that what I needed was a [async-signal-safe][115] form of what
3940*5a6e8488SAndroid Build Coastguard Workerprogrammers call "exceptions" in other languages.
3941*5a6e8488SAndroid Build Coastguard Worker
3942*5a6e8488SAndroid Build Coastguard WorkerI knew that [`setjmp()`][116] and [`longjmp()`][117] are used in C to implement
3943*5a6e8488SAndroid Build Coastguard Workerexceptions, so I thought I would learn how to use them. How hard could it be?
3944*5a6e8488SAndroid Build Coastguard Worker
3945*5a6e8488SAndroid Build Coastguard WorkerQuite hard, it turns out, especially in the presence of signals. And that's
3946*5a6e8488SAndroid Build Coastguard Workerbecause there are a few big snares:
3947*5a6e8488SAndroid Build Coastguard Worker
3948*5a6e8488SAndroid Build Coastguard Worker1.	The value of any local variables are not guaranteed to be preserved after a
3949*5a6e8488SAndroid Build Coastguard Worker	`longjmp()` back into a function.
3950*5a6e8488SAndroid Build Coastguard Worker2.	While `longjmp()` is required to be [async-signal-safe][115], if it is
3951*5a6e8488SAndroid Build Coastguard Worker	invoked by a signal handler that interrupted a non-[async-signal-safe][115]
3952*5a6e8488SAndroid Build Coastguard Worker	function, then the behavior is undefined.
3953*5a6e8488SAndroid Build Coastguard Worker3.	Any mutation that is not guaranteed to be atomic with respect to signals may
3954*5a6e8488SAndroid Build Coastguard Worker	be incomplete when a signal arrives.
3955*5a6e8488SAndroid Build Coastguard Worker
3956*5a6e8488SAndroid Build Coastguard WorkerOh boy.
3957*5a6e8488SAndroid Build Coastguard Worker
3958*5a6e8488SAndroid Build Coastguard WorkerFor number 1, the answer to this is to hide data that must stay changed behind
3959*5a6e8488SAndroid Build Coastguard Workerpointers. Only the *pointers* are considered local, so as long as I didn't do
3960*5a6e8488SAndroid Build Coastguard Workerany modifying pointer arithmetic, pointers and their data would be safe. For
3961*5a6e8488SAndroid Build Coastguard Workercases where I have local data that must change and stay changed, I needed to
3962*5a6e8488SAndroid Build Coastguard Worker*undo* the `setjmp()`, do the change, and the *redo* the `setjmp()`.
3963*5a6e8488SAndroid Build Coastguard Worker
3964*5a6e8488SAndroid Build Coastguard WorkerFor number 2 and number 3, `bc` needs some way to tell the signal handler that
3965*5a6e8488SAndroid Build Coastguard Workerit cannot do a `longjmp()`. This is done by "locking" signals with a `volatile
3966*5a6e8488SAndroid Build Coastguard Workersig_atomic_t`. (For more information, see the [Async-Signal-Safe Signal
3967*5a6e8488SAndroid Build Coastguard WorkerHandling][173] section.) For every function that calls a function that is not
3968*5a6e8488SAndroid Build Coastguard Workerasync-signal-safe, they first need to use `BC_SIG_LOCK` to lock signals, and
3969*5a6e8488SAndroid Build Coastguard Workerafterward, use `BC_SIG_UNLOCK` to unlock signals.
3970*5a6e8488SAndroid Build Coastguard Worker
3971*5a6e8488SAndroid Build Coastguard WorkerCode also need to do this for all global, non-atomic mutation, which means that
3972*5a6e8488SAndroid Build Coastguard Workermodifying any part of the `BcVm` global struct.
3973*5a6e8488SAndroid Build Coastguard Worker
3974*5a6e8488SAndroid Build Coastguard Worker`BC_SIG_UNLOCK` has another requirement: it must check for signals or errors and
3975*5a6e8488SAndroid Build Coastguard Workerjump if necessary.
3976*5a6e8488SAndroid Build Coastguard Worker
3977*5a6e8488SAndroid Build Coastguard WorkerOn top of all of that, *all* functions with cleanup needed to be able to run
3978*5a6e8488SAndroid Build Coastguard Workertheir cleanup. This meant that `longjmp()` could not just jump to the finish; it
3979*5a6e8488SAndroid Build Coastguard Workerhad to start what I call a "jump series," using a stack of `jmp_buf`'s
3980*5a6e8488SAndroid Build Coastguard Worker(`jmp_bufs` in `BcVm`). Each `longjmp()` uses the top of the `jmp_bufs` stack to
3981*5a6e8488SAndroid Build Coastguard Workerexecute its jump. Then, if the cleanup code was executed because of a jump, the
3982*5a6e8488SAndroid Build Coastguard Workercleanup code was responsible for continuing the jump series by popping the
3983*5a6e8488SAndroid Build Coastguard Workerprevious item off the stack and using the new top of the stack for a jump.
3984*5a6e8488SAndroid Build Coastguard Worker
3985*5a6e8488SAndroid Build Coastguard WorkerIn this way, C++-style exceptions were implemented in pure C. Not fun, but it
3986*5a6e8488SAndroid Build Coastguard Workerworks. However, the order of operations matters, especially in the macros that
3987*5a6e8488SAndroid Build Coastguard Workerhelp implement the error handling.
3988*5a6e8488SAndroid Build Coastguard Worker
3989*5a6e8488SAndroid Build Coastguard WorkerFor example, in `BC_UNSETJMP`, signals are unlocked before checking for signals.
3990*5a6e8488SAndroid Build Coastguard WorkerIf a signal comes between, that's fine; it will still cause a jump to the right
3991*5a6e8488SAndroid Build Coastguard Workerplace. However, disabling the lock after could mean that a signal could come
3992*5a6e8488SAndroid Build Coastguard Worker*after* checking for signals, but before signals were unlocked, causing the
3993*5a6e8488SAndroid Build Coastguard Workerhandling of the signal to be delayed.
3994*5a6e8488SAndroid Build Coastguard Worker
3995*5a6e8488SAndroid Build Coastguard Worker#### Custom I/O
3996*5a6e8488SAndroid Build Coastguard Worker
3997*5a6e8488SAndroid Build Coastguard WorkerWhy did I implement my own buffered I/O for `bc`? Because I use `setjmp()` and
3998*5a6e8488SAndroid Build Coastguard Worker`longjmp()` for error handling (see the [Error Handling][97] section), and the
3999*5a6e8488SAndroid Build Coastguard Workerbuffered I/O in `libc` does not interact well with the use of those procedures;
4000*5a6e8488SAndroid Build Coastguard Workerall of the buffered I/O API is basically non-[async-signal-safe][115].
4001*5a6e8488SAndroid Build Coastguard Worker
4002*5a6e8488SAndroid Build Coastguard WorkerImplementing custom buffered I/O had other benefits. First, it allowed me to
4003*5a6e8488SAndroid Build Coastguard Workertightly integrate history with the I/O code. Second, it allowed me to make
4004*5a6e8488SAndroid Build Coastguard Workerchanges to history in order to make it adapt to user prompts.
4005*5a6e8488SAndroid Build Coastguard Worker
4006*5a6e8488SAndroid Build Coastguard Worker### Lexing
4007*5a6e8488SAndroid Build Coastguard Worker
4008*5a6e8488SAndroid Build Coastguard WorkerTo simplify parsing, both calculators use lexers to turn the text into a more
4009*5a6e8488SAndroid Build Coastguard Workereasily-parsable form.
4010*5a6e8488SAndroid Build Coastguard Worker
4011*5a6e8488SAndroid Build Coastguard WorkerWhile some tokens are only one character long, others require many tokens, and
4012*5a6e8488SAndroid Build Coastguard Workersome of those need to store all of the text corresponding to the token for use
4013*5a6e8488SAndroid Build Coastguard Workerby the parsers. Tokens that need to store their corresponding text include, but
4014*5a6e8488SAndroid Build Coastguard Workerare not limited to:
4015*5a6e8488SAndroid Build Coastguard Worker
4016*5a6e8488SAndroid Build Coastguard Worker* Strings.
4017*5a6e8488SAndroid Build Coastguard Worker* Numbers.
4018*5a6e8488SAndroid Build Coastguard Worker* Identifiers.
4019*5a6e8488SAndroid Build Coastguard Worker
4020*5a6e8488SAndroid Build Coastguard WorkerFor this purpose, the lexer has a [vector][111] named `str` to store the data
4021*5a6e8488SAndroid Build Coastguard Workerfor tokens. This data is overwritten if another token is lexed that needs to
4022*5a6e8488SAndroid Build Coastguard Workerstore data, so the parsers need to copy the data before calling the lexer again.
4023*5a6e8488SAndroid Build Coastguard Worker
4024*5a6e8488SAndroid Build Coastguard WorkerBoth lexers do some of the same things:
4025*5a6e8488SAndroid Build Coastguard Worker
4026*5a6e8488SAndroid Build Coastguard Worker* Lex identifiers into tokens, storing the identifier in `str`.
4027*5a6e8488SAndroid Build Coastguard Worker* Lex number strings into tokens, storing the string in `str`.
4028*5a6e8488SAndroid Build Coastguard Worker* Lex whitespace.
4029*5a6e8488SAndroid Build Coastguard Worker* Lex comments.
4030*5a6e8488SAndroid Build Coastguard Worker
4031*5a6e8488SAndroid Build Coastguard WorkerOther than that, and some common plumbing, the lexers have separate code.
4032*5a6e8488SAndroid Build Coastguard Worker
4033*5a6e8488SAndroid Build Coastguard Worker#### `dc` Lexing
4034*5a6e8488SAndroid Build Coastguard Worker
4035*5a6e8488SAndroid Build Coastguard WorkerThe `dc` lexer is remarkably simple; in fact, besides [`src/main.c`][205],
4036*5a6e8488SAndroid Build Coastguard Worker[`src/bc.c`][40], and [`src/dc.c`][44], which just contain one function each,
4037*5a6e8488SAndroid Build Coastguard Workerthe only file smaller than [`src/dc_lex.c`][45] is [`src/args.c`][206], which
4038*5a6e8488SAndroid Build Coastguard Workerjust processes command-line arguments after they are parsed by
4039*5a6e8488SAndroid Build Coastguard Worker[`src/opt.c`][51].
4040*5a6e8488SAndroid Build Coastguard Worker
4041*5a6e8488SAndroid Build Coastguard WorkerFor most characters, the `dc` lexer is able to convert directly from the
4042*5a6e8488SAndroid Build Coastguard Workercharacter to its corresponding token. This happens using `dc_lex_tokens[]` in
4043*5a6e8488SAndroid Build Coastguard Worker[`src/data.c`][131].
4044*5a6e8488SAndroid Build Coastguard Worker
4045*5a6e8488SAndroid Build Coastguard Worker`dc`'s lexer also has to lex the register name after lexing tokens for commands
4046*5a6e8488SAndroid Build Coastguard Workerthat need registers.
4047*5a6e8488SAndroid Build Coastguard Worker
4048*5a6e8488SAndroid Build Coastguard WorkerAnd finally, `dc`'s lexer needs to parse `dc` strings, which is the only part of
4049*5a6e8488SAndroid Build Coastguard Workerthe `dc` lexer that is more complex than the `bc` lexer. This is because `dc`
4050*5a6e8488SAndroid Build Coastguard Workerstrings need to have a balanced number of brackets.
4051*5a6e8488SAndroid Build Coastguard Worker
4052*5a6e8488SAndroid Build Coastguard Worker#### `bc` Lexing
4053*5a6e8488SAndroid Build Coastguard Worker
4054*5a6e8488SAndroid Build Coastguard WorkerThe `bc` lexer is fairly simple. It does the following things:
4055*5a6e8488SAndroid Build Coastguard Worker
4056*5a6e8488SAndroid Build Coastguard Worker* Lexes `bc` strings.
4057*5a6e8488SAndroid Build Coastguard Worker* Lexes `bc` identifiers. This is necessary because this is how `bc` keywords
4058*5a6e8488SAndroid Build Coastguard Worker  are lexed. After ensuring that an identifier is not a keyword, the `bc` lexer
4059*5a6e8488SAndroid Build Coastguard Worker  allows the common identifier function to take over.
4060*5a6e8488SAndroid Build Coastguard Worker* Turns characters and groups of characters into `bc` operator tokens.
4061*5a6e8488SAndroid Build Coastguard Worker
4062*5a6e8488SAndroid Build Coastguard Worker### Parsing
4063*5a6e8488SAndroid Build Coastguard Worker
4064*5a6e8488SAndroid Build Coastguard WorkerThe difference between parsing `bc` and `dc` code is...vast. The `dc` parser is
4065*5a6e8488SAndroid Build Coastguard Workersimple, while the `bc` parser is the most complex piece of code in the entire
4066*5a6e8488SAndroid Build Coastguard Workercodebase.
4067*5a6e8488SAndroid Build Coastguard Worker
4068*5a6e8488SAndroid Build Coastguard WorkerHowever, they both do some of the same things.
4069*5a6e8488SAndroid Build Coastguard Worker
4070*5a6e8488SAndroid Build Coastguard WorkerFirst, the parsers do *not* use [abstract syntax trees][207]; instead, they
4071*5a6e8488SAndroid Build Coastguard Workerdirectly generate the bytecode that will be executed by the `BcProgram` code.
4072*5a6e8488SAndroid Build Coastguard WorkerEven in the case of `bc`, this heavily simplifies the parsing because the
4073*5a6e8488SAndroid Build Coastguard Worker[Shunting-Yard Algorithm][109] is designed to generate [Reverse Polish
4074*5a6e8488SAndroid Build Coastguard WorkerNotation][108], which is basically directly executable.
4075*5a6e8488SAndroid Build Coastguard Worker
4076*5a6e8488SAndroid Build Coastguard WorkerSecond, any extra data that the `BcProgram` needs for execution is stored into
4077*5a6e8488SAndroid Build Coastguard Workerfunctions (see the [Functions][208] section). These include constants and
4078*5a6e8488SAndroid Build Coastguard Workerstrings.
4079*5a6e8488SAndroid Build Coastguard Worker
4080*5a6e8488SAndroid Build Coastguard Worker#### `dc` Parsing
4081*5a6e8488SAndroid Build Coastguard Worker
4082*5a6e8488SAndroid Build Coastguard WorkerThe parser for `dc`, like its lexer, is remarkably simple. In fact, the easiness
4083*5a6e8488SAndroid Build Coastguard Workerof lexing and parsing [Reverse Polish notation][108] is probably why it was used
4084*5a6e8488SAndroid Build Coastguard Workerfor `dc` when it was first created at Bell Labs.
4085*5a6e8488SAndroid Build Coastguard Worker
4086*5a6e8488SAndroid Build Coastguard WorkerFor most tokens, the `dc` parser is able to convert directly from the token
4087*5a6e8488SAndroid Build Coastguard Workerto its corresponding instruction. This happens using `dc_parse_insts[]` in
4088*5a6e8488SAndroid Build Coastguard Worker[`src/data.c`][131].
4089*5a6e8488SAndroid Build Coastguard Worker
4090*5a6e8488SAndroid Build Coastguard Worker`dc`'s parser also has to parse the register name for commands that need
4091*5a6e8488SAndroid Build Coastguard Workerregisters. This is the most complex part of the `dc` parser; each different
4092*5a6e8488SAndroid Build Coastguard Workerregister command needs to be parsed differently because most of them require two
4093*5a6e8488SAndroid Build Coastguard Workeror more instructions to execute properly.
4094*5a6e8488SAndroid Build Coastguard Worker
4095*5a6e8488SAndroid Build Coastguard WorkerFor example, storing in a register requires a swap instruction and an assignment
4096*5a6e8488SAndroid Build Coastguard Workerinstruction.
4097*5a6e8488SAndroid Build Coastguard Worker
4098*5a6e8488SAndroid Build Coastguard WorkerAnother example are conditional execution instructions; they need to produce the
4099*5a6e8488SAndroid Build Coastguard Workerinstruction for the condition, and then they must parse a possible "else" part,
4100*5a6e8488SAndroid Build Coastguard Workerwhich might not exist.
4101*5a6e8488SAndroid Build Coastguard Worker
4102*5a6e8488SAndroid Build Coastguard Worker##### Existing Commands
4103*5a6e8488SAndroid Build Coastguard Worker
4104*5a6e8488SAndroid Build Coastguard Worker`dc` is based on commands, which are usually one letter. The following table is
4105*5a6e8488SAndroid Build Coastguard Workera table of which ASCII characters are already used:
4106*5a6e8488SAndroid Build Coastguard Worker
4107*5a6e8488SAndroid Build Coastguard Worker| Characters | Used? | For...                                     |
4108*5a6e8488SAndroid Build Coastguard Worker|------------|-------|--------------------------------------------|
4109*5a6e8488SAndroid Build Coastguard Worker| Space      | x     | Separator                                  |
4110*5a6e8488SAndroid Build Coastguard Worker| `!`        | x     | Conditional Execution of Registers         |
4111*5a6e8488SAndroid Build Coastguard Worker| `"`        | x     | Bounded Rand Operator                      |
4112*5a6e8488SAndroid Build Coastguard Worker| `#`        | x     | Comments                                   |
4113*5a6e8488SAndroid Build Coastguard Worker| `$`        | x     | Truncation                                 |
4114*5a6e8488SAndroid Build Coastguard Worker| `%`        | x     | Modulus                                    |
4115*5a6e8488SAndroid Build Coastguard Worker| `&`        |       |                                            |
4116*5a6e8488SAndroid Build Coastguard Worker| `'`        | x     | Rand Operator                              |
4117*5a6e8488SAndroid Build Coastguard Worker| `(`        | x     | Greater Than Operator                      |
4118*5a6e8488SAndroid Build Coastguard Worker| `)`        | x     | Less Than Operator                         |
4119*5a6e8488SAndroid Build Coastguard Worker| `*`        | x     | Multiplication                             |
4120*5a6e8488SAndroid Build Coastguard Worker| `+`        | x     | Addition                                   |
4121*5a6e8488SAndroid Build Coastguard Worker| `,`        | x     | Depth of Execution Stack                   |
4122*5a6e8488SAndroid Build Coastguard Worker| `-`        | x     | Subtraction                                |
4123*5a6e8488SAndroid Build Coastguard Worker| `.`        | x     | Numbers                                    |
4124*5a6e8488SAndroid Build Coastguard Worker| `/`        | x     | Division                                   |
4125*5a6e8488SAndroid Build Coastguard Worker| `0-9`      | x     | Numbers                                    |
4126*5a6e8488SAndroid Build Coastguard Worker| `:`        | x     | Store into Array                           |
4127*5a6e8488SAndroid Build Coastguard Worker| `;`        | x     | Load from Array                            |
4128*5a6e8488SAndroid Build Coastguard Worker| `<`        | x     | Conditional Execution of Registers         |
4129*5a6e8488SAndroid Build Coastguard Worker| `=`        | x     | Conditional Execution of Registers         |
4130*5a6e8488SAndroid Build Coastguard Worker| `>`        | x     | Conditional Execution of Registers         |
4131*5a6e8488SAndroid Build Coastguard Worker| `?`        | x     | Ask for User Input                         |
4132*5a6e8488SAndroid Build Coastguard Worker| `@`        | x     | Places Operator                            |
4133*5a6e8488SAndroid Build Coastguard Worker| `A-F`      | x     | Numbers                                    |
4134*5a6e8488SAndroid Build Coastguard Worker| `G`        | x     | Equal Operator                             |
4135*5a6e8488SAndroid Build Coastguard Worker| `H`        | x     | Shift Left                                 |
4136*5a6e8488SAndroid Build Coastguard Worker| `I`        | x     | Push `ibase` onto Stack                    |
4137*5a6e8488SAndroid Build Coastguard Worker| `J`        | x     | Push `seed` onto Stack                     |
4138*5a6e8488SAndroid Build Coastguard Worker| `K`        | x     | Push `scale` onto Stack                    |
4139*5a6e8488SAndroid Build Coastguard Worker| `L`        | x     | Pop off of Register                        |
4140*5a6e8488SAndroid Build Coastguard Worker| `M`        | x     | Boolean And Operator                       |
4141*5a6e8488SAndroid Build Coastguard Worker| `N`        | x     | Boolean Not Operator                       |
4142*5a6e8488SAndroid Build Coastguard Worker| `O`        | x     | Push `obase` onto Stack                    |
4143*5a6e8488SAndroid Build Coastguard Worker| `P`        | x     | Byte Stream Printing                       |
4144*5a6e8488SAndroid Build Coastguard Worker| `Q`        | x     | Quit Some Number of Macros                 |
4145*5a6e8488SAndroid Build Coastguard Worker| `R`        | x     | Pop Top of Stack                           |
4146*5a6e8488SAndroid Build Coastguard Worker| `S`        | x     | Push onto Register                         |
4147*5a6e8488SAndroid Build Coastguard Worker| `T`        | x     | Push Max `ibase` onto Stack                |
4148*5a6e8488SAndroid Build Coastguard Worker| `U`        | x     | Push Max `obase` onto Stack                |
4149*5a6e8488SAndroid Build Coastguard Worker| `V`        | x     | Push Max `scale` onto Stack                |
4150*5a6e8488SAndroid Build Coastguard Worker| `W`        | x     | Push Max of `'` Operator                   |
4151*5a6e8488SAndroid Build Coastguard Worker| `X`        | x     | Scale of a Number                          |
4152*5a6e8488SAndroid Build Coastguard Worker| `Y`        | x     | Length of Array                            |
4153*5a6e8488SAndroid Build Coastguard Worker| `Z`        | x     | Number of Significant Digits               |
4154*5a6e8488SAndroid Build Coastguard Worker| `[`        | x     | Strings                                    |
4155*5a6e8488SAndroid Build Coastguard Worker| `\\`       | x     | Escaping Brackets in Strings               |
4156*5a6e8488SAndroid Build Coastguard Worker| `]`        | x     | Strings                                    |
4157*5a6e8488SAndroid Build Coastguard Worker| `^`        | x     | Power                                      |
4158*5a6e8488SAndroid Build Coastguard Worker| `_`        | x     | Negative Numbers and Negation              |
4159*5a6e8488SAndroid Build Coastguard Worker| Backtick   |       |                                            |
4160*5a6e8488SAndroid Build Coastguard Worker| `a`        | x     | Asciify                                    |
4161*5a6e8488SAndroid Build Coastguard Worker| `b`        | x     | Absolute Value                             |
4162*5a6e8488SAndroid Build Coastguard Worker| `c`        | x     | Clear Stack                                |
4163*5a6e8488SAndroid Build Coastguard Worker| `d`        | x     | Duplication of Top of Stack                |
4164*5a6e8488SAndroid Build Coastguard Worker| `e`        | x     | Else in Conditional Execution of Registers |
4165*5a6e8488SAndroid Build Coastguard Worker| `f`        | x     | Printing the Stack                         |
4166*5a6e8488SAndroid Build Coastguard Worker| `g`        | x     | Global Settings                            |
4167*5a6e8488SAndroid Build Coastguard Worker| `h`        | x     | Shift Right                                |
4168*5a6e8488SAndroid Build Coastguard Worker| `i`        | x     | Set `ibase`                                |
4169*5a6e8488SAndroid Build Coastguard Worker| `j`        | x     | Set `seed`                                 |
4170*5a6e8488SAndroid Build Coastguard Worker| `k`        | x     | Set `scale`                                |
4171*5a6e8488SAndroid Build Coastguard Worker| `l`        | x     | Load from Register                         |
4172*5a6e8488SAndroid Build Coastguard Worker| `m`        | x     | Boolean Or Operator                        |
4173*5a6e8488SAndroid Build Coastguard Worker| `n`        | x     | Print and Pop                              |
4174*5a6e8488SAndroid Build Coastguard Worker| `o`        | x     | Set `obase`                                |
4175*5a6e8488SAndroid Build Coastguard Worker| `p`        | x     | Print with Newline                         |
4176*5a6e8488SAndroid Build Coastguard Worker| `q`        | x     | Quit Two Macros                            |
4177*5a6e8488SAndroid Build Coastguard Worker| `r`        | x     | Swap Top Two Items                         |
4178*5a6e8488SAndroid Build Coastguard Worker| `s`        | x     | Store into Register                        |
4179*5a6e8488SAndroid Build Coastguard Worker| `t`        | x     | Equivalent of `bc`'s `is_number()`         |
4180*5a6e8488SAndroid Build Coastguard Worker| `u`        | x     | Equivalent of `bc`'s `is_string()`         |
4181*5a6e8488SAndroid Build Coastguard Worker| `v`        | x     | Square Root                                |
4182*5a6e8488SAndroid Build Coastguard Worker| `w`        |       |                                            |
4183*5a6e8488SAndroid Build Coastguard Worker| `x`        | x     | Execute String                             |
4184*5a6e8488SAndroid Build Coastguard Worker| `y`        | x     | Current Depth of a Register                |
4185*5a6e8488SAndroid Build Coastguard Worker| `z`        | x     | Current Depth of Stack                     |
4186*5a6e8488SAndroid Build Coastguard Worker| `{`        | x     | Greater Than or Equal Operator             |
4187*5a6e8488SAndroid Build Coastguard Worker| `\|`       | x     | Moduler Exponentiation                     |
4188*5a6e8488SAndroid Build Coastguard Worker| `}`        | x     | Less Than or Equal Operator                |
4189*5a6e8488SAndroid Build Coastguard Worker| `~`        | x     | Division and Modulus Combined              |
4190*5a6e8488SAndroid Build Coastguard Worker
4191*5a6e8488SAndroid Build Coastguard Worker#### `bc` Parsing
4192*5a6e8488SAndroid Build Coastguard Worker
4193*5a6e8488SAndroid Build Coastguard Worker`bc`'s parser is, by far, the most sensitive piece of code in this software, and
4194*5a6e8488SAndroid Build Coastguard Workerthere is a very big reason for that: `bc`'s standard is awful and defined a very
4195*5a6e8488SAndroid Build Coastguard Workerpoor language.
4196*5a6e8488SAndroid Build Coastguard Worker
4197*5a6e8488SAndroid Build Coastguard WorkerThe standard says that either semicolons or newlines can end statements. Trying
4198*5a6e8488SAndroid Build Coastguard Workerto parse the end of a statement when it can either be a newline or a semicolon
4199*5a6e8488SAndroid Build Coastguard Workeris subtle. Doing it in the presence of control flow constructs that do not have
4200*5a6e8488SAndroid Build Coastguard Workerto use braces is even harder.
4201*5a6e8488SAndroid Build Coastguard Worker
4202*5a6e8488SAndroid Build Coastguard WorkerAnd then comes the biggest complication of all: `bc` has to assume that it is
4203*5a6e8488SAndroid Build Coastguard Worker*always* at a REPL (Read-Eval-Print Loop). `bc` is, first and foremost, an
4204*5a6e8488SAndroid Build Coastguard Worker*interactive* utility.
4205*5a6e8488SAndroid Build Coastguard Worker
4206*5a6e8488SAndroid Build Coastguard Worker##### Flags
4207*5a6e8488SAndroid Build Coastguard Worker
4208*5a6e8488SAndroid Build Coastguard WorkerAll of this means that `bc` has to be able to partially parse something, store
4209*5a6e8488SAndroid Build Coastguard Workerenough data to recreate that state later, and return, making sure to not
4210*5a6e8488SAndroid Build Coastguard Workerexecute anything in the meantime.
4211*5a6e8488SAndroid Build Coastguard Worker
4212*5a6e8488SAndroid Build Coastguard Worker*That* is what the flags in [`include/bc.h`][106] are: they are the state that
4213*5a6e8488SAndroid Build Coastguard Worker`bc` is saving for itself.
4214*5a6e8488SAndroid Build Coastguard Worker
4215*5a6e8488SAndroid Build Coastguard WorkerIt saves them in a stack, by the way, because it's possible to nest
4216*5a6e8488SAndroid Build Coastguard Workerstructures, just like any other programming language. Thus, not only does it
4217*5a6e8488SAndroid Build Coastguard Workerhave to store state, it needs to do it arbitrarily, and still be able to
4218*5a6e8488SAndroid Build Coastguard Workercome back to it.
4219*5a6e8488SAndroid Build Coastguard Worker
4220*5a6e8488SAndroid Build Coastguard WorkerSo `bc` stores its parser state with flags in a stack. Careful setting of these
4221*5a6e8488SAndroid Build Coastguard Workerflags, along with properly using them and maintaining the flag stack, are what
4222*5a6e8488SAndroid Build Coastguard Workermake `bc` parsing work, but it's complicated. In fact, as I mentioned, the `bc`
4223*5a6e8488SAndroid Build Coastguard Workerparser is the single most subtle, fickle, and sensitive piece of code in the
4224*5a6e8488SAndroid Build Coastguard Workerentire codebase. Only one thing came close once: square root, and that was only
4225*5a6e8488SAndroid Build Coastguard Workersensitive because I wrote it wrong. This parser is pretty good, and it is
4226*5a6e8488SAndroid Build Coastguard Worker*still* sensitive. And flags are the reason why.
4227*5a6e8488SAndroid Build Coastguard Worker
4228*5a6e8488SAndroid Build Coastguard WorkerFor more information about what individual flags there are, see the comments in
4229*5a6e8488SAndroid Build Coastguard Worker[`include/bc.h`][106].
4230*5a6e8488SAndroid Build Coastguard Worker
4231*5a6e8488SAndroid Build Coastguard Worker##### Labels
4232*5a6e8488SAndroid Build Coastguard Worker
4233*5a6e8488SAndroid Build Coastguard Worker`bc`'s language is Turing-complete. That means that code needs the ability to
4234*5a6e8488SAndroid Build Coastguard Workerjump around, specifically to implement control flow like `if` statements and
4235*5a6e8488SAndroid Build Coastguard Workerloops.
4236*5a6e8488SAndroid Build Coastguard Worker
4237*5a6e8488SAndroid Build Coastguard Worker`bc` handles this while parsing with what I called "labels."
4238*5a6e8488SAndroid Build Coastguard Worker
4239*5a6e8488SAndroid Build Coastguard WorkerLabels are markers in the bytecode. They are stored in functions alongside the
4240*5a6e8488SAndroid Build Coastguard Workerbytecode, and they are just indices into the bytecode.
4241*5a6e8488SAndroid Build Coastguard Worker
4242*5a6e8488SAndroid Build Coastguard WorkerWhen the `bc` parser creates a label, it pushes an index onto the labels array,
4243*5a6e8488SAndroid Build Coastguard Workerand the index of the label in that array is the index that will be inserted into
4244*5a6e8488SAndroid Build Coastguard Workerthe bytecode.
4245*5a6e8488SAndroid Build Coastguard Worker
4246*5a6e8488SAndroid Build Coastguard WorkerThen, when a jump happens, the index pulled out of the bytecode is used to index
4247*5a6e8488SAndroid Build Coastguard Workerthe labels array, and the label (index) at the index is then used to set the
4248*5a6e8488SAndroid Build Coastguard Workerinstruction pointer.
4249*5a6e8488SAndroid Build Coastguard Worker
4250*5a6e8488SAndroid Build Coastguard Worker##### Cond Labels
4251*5a6e8488SAndroid Build Coastguard Worker
4252*5a6e8488SAndroid Build Coastguard Worker"Cond" labels are so-called because they are used by conditionals.
4253*5a6e8488SAndroid Build Coastguard Worker
4254*5a6e8488SAndroid Build Coastguard WorkerThe key to them is that they come *before* the code that uses them. In other
4255*5a6e8488SAndroid Build Coastguard Workerwords, when jumping to a condition, code is jumping *backwards*.
4256*5a6e8488SAndroid Build Coastguard Worker
4257*5a6e8488SAndroid Build Coastguard WorkerThis means that when a cond label is created, the value that should go there is
4258*5a6e8488SAndroid Build Coastguard Workerwell-known. Cond labels are easy.
4259*5a6e8488SAndroid Build Coastguard Worker
4260*5a6e8488SAndroid Build Coastguard WorkerHowever, they are still stored on a stack so that the parser knows what cond
4261*5a6e8488SAndroid Build Coastguard Workerlabel to use.
4262*5a6e8488SAndroid Build Coastguard Worker
4263*5a6e8488SAndroid Build Coastguard Worker##### Exit Labels
4264*5a6e8488SAndroid Build Coastguard Worker
4265*5a6e8488SAndroid Build Coastguard WorkerExit labels are not so easy.
4266*5a6e8488SAndroid Build Coastguard Worker
4267*5a6e8488SAndroid Build Coastguard Worker"Exit" labels are so-called because they are used by code "exiting" out of `if`
4268*5a6e8488SAndroid Build Coastguard Workerstatements or loops.
4269*5a6e8488SAndroid Build Coastguard Worker
4270*5a6e8488SAndroid Build Coastguard WorkerThe key to them is that they come *after* the code that uses them. In other
4271*5a6e8488SAndroid Build Coastguard Workerwords, when jumping to an exit, code is jumping *forwards*.
4272*5a6e8488SAndroid Build Coastguard Worker
4273*5a6e8488SAndroid Build Coastguard WorkerBut this means that when an exit label is created, the value that should go
4274*5a6e8488SAndroid Build Coastguard Workerthere is *not* known. The code that needs it must be parsed and generated first.
4275*5a6e8488SAndroid Build Coastguard Worker
4276*5a6e8488SAndroid Build Coastguard WorkerThat means that exit labels are created with the index of `SIZE_MAX`, which is
4277*5a6e8488SAndroid Build Coastguard Workerthen specifically checked for with an assert in `bc_program_exec()` before using
4278*5a6e8488SAndroid Build Coastguard Workerthose indices.
4279*5a6e8488SAndroid Build Coastguard Worker
4280*5a6e8488SAndroid Build Coastguard WorkerThere should ***NEVER*** be a case when an exit label is not filled in properly
4281*5a6e8488SAndroid Build Coastguard Workerif the parser has no bugs. This is because every `if` statement, every loop,
4282*5a6e8488SAndroid Build Coastguard Workermust have an exit, so the exit must be set. If not, there is a bug.
4283*5a6e8488SAndroid Build Coastguard Worker
4284*5a6e8488SAndroid Build Coastguard WorkerExit labels are also stored on a stack so that the parser knows what exit label
4285*5a6e8488SAndroid Build Coastguard Workerto use.
4286*5a6e8488SAndroid Build Coastguard Worker
4287*5a6e8488SAndroid Build Coastguard Worker##### Expression Parsing
4288*5a6e8488SAndroid Build Coastguard Worker
4289*5a6e8488SAndroid Build Coastguard Worker`bc` has expressions like you might expect in a typical programming language.
4290*5a6e8488SAndroid Build Coastguard WorkerThis means [infix notation][107].
4291*5a6e8488SAndroid Build Coastguard Worker
4292*5a6e8488SAndroid Build Coastguard WorkerOne thing about infix notation is that you can't just generate code straight
4293*5a6e8488SAndroid Build Coastguard Workerfrom it like you can with [Reverse Polish notation][108]. It requires more work
4294*5a6e8488SAndroid Build Coastguard Workerto shape it into a form that works for execution on a stack machine.
4295*5a6e8488SAndroid Build Coastguard Worker
4296*5a6e8488SAndroid Build Coastguard WorkerThat extra work is called the [Shunting-Yard algorithm][109], and the form it
4297*5a6e8488SAndroid Build Coastguard Workertranslates infix notation into is...[Reverse Polish notation][108].
4298*5a6e8488SAndroid Build Coastguard Worker
4299*5a6e8488SAndroid Build Coastguard WorkerIn order to understand the rest of this section, you must understand the
4300*5a6e8488SAndroid Build Coastguard Worker[Shunting-Yard algorithm][109]. Go do that before you read on.
4301*5a6e8488SAndroid Build Coastguard Worker
4302*5a6e8488SAndroid Build Coastguard Worker###### Operator Stack
4303*5a6e8488SAndroid Build Coastguard Worker
4304*5a6e8488SAndroid Build Coastguard WorkerIn `bc`, the [Shunting-Yard algorithm][109] is implemented with bytecode as the
4305*5a6e8488SAndroid Build Coastguard Workeroutput and an explicit operator stack (the `ops` field in `BcParse`) as the
4306*5a6e8488SAndroid Build Coastguard Workeroperator stack. It stores tokens from `BcLex`.
4307*5a6e8488SAndroid Build Coastguard Worker
4308*5a6e8488SAndroid Build Coastguard WorkerHowever, there is one **HUGE** hangup: multiple expressions can stack. This
4309*5a6e8488SAndroid Build Coastguard Workermeans that multiple expressions can be parsed at one time (think an array element
4310*5a6e8488SAndroid Build Coastguard Workerexpression in the middle of a larger expression). Because of that, we need to
4311*5a6e8488SAndroid Build Coastguard Workerkeep track of where the previous expression ended. That's what `start` parameter
4312*5a6e8488SAndroid Build Coastguard Workerto `bc_parse_operator()` is.
4313*5a6e8488SAndroid Build Coastguard Worker
4314*5a6e8488SAndroid Build Coastguard WorkerParsing multiple expressions on one operator stack only works because
4315*5a6e8488SAndroid Build Coastguard Workerexpressions can only *stack*; this means that, if an expression begins before
4316*5a6e8488SAndroid Build Coastguard Workeranother ends, it must *also* end before that other expression ends. This
4317*5a6e8488SAndroid Build Coastguard Workerproperty ensures that operators will never interfere with each other on the
4318*5a6e8488SAndroid Build Coastguard Workeroperator stack.
4319*5a6e8488SAndroid Build Coastguard Worker
4320*5a6e8488SAndroid Build Coastguard WorkerAlso, the operator precedence is reversed: a lower precedence *value* means a
4321*5a6e8488SAndroid Build Coastguard Workerhigher precedence.
4322*5a6e8488SAndroid Build Coastguard Worker
4323*5a6e8488SAndroid Build Coastguard Worker###### Recursion
4324*5a6e8488SAndroid Build Coastguard Worker
4325*5a6e8488SAndroid Build Coastguard WorkerBecause expressions can stack, parsing expressions actually requires recursion.
4326*5a6e8488SAndroid Build Coastguard WorkerWell, it doesn't *require* it, but the code is much more readable that way.
4327*5a6e8488SAndroid Build Coastguard Worker
4328*5a6e8488SAndroid Build Coastguard WorkerThis recursion is indirect; the functions that `bc_parse_expr_err()` (the actual
4329*5a6e8488SAndroid Build Coastguard Workerexpression parsing function) calls can, in turn, call it.
4330*5a6e8488SAndroid Build Coastguard Worker
4331*5a6e8488SAndroid Build Coastguard Worker###### Expression Flags
4332*5a6e8488SAndroid Build Coastguard Worker
4333*5a6e8488SAndroid Build Coastguard WorkerThere is one more big thing: not all expressions in `bc` are equal.
4334*5a6e8488SAndroid Build Coastguard Worker
4335*5a6e8488SAndroid Build Coastguard WorkerSome expressions have requirements that others don't have. For example, only
4336*5a6e8488SAndroid Build Coastguard Workerarray arguments can be arrays (which are technically not expressions, but are
4337*5a6e8488SAndroid Build Coastguard Workertreated as such for parsing), and some operators (in POSIX) are not allowed in
4338*5a6e8488SAndroid Build Coastguard Workercertain places.
4339*5a6e8488SAndroid Build Coastguard Worker
4340*5a6e8488SAndroid Build Coastguard WorkerFor this reason, functions that are part of the expression parsing
4341*5a6e8488SAndroid Build Coastguard Workerinfrastructure in `bc`'s parser usually take a `flags` argument. This is meant
4342*5a6e8488SAndroid Build Coastguard Workerto be passed to children, and somewhere, they will be checked to ensure that the
4343*5a6e8488SAndroid Build Coastguard Workerresulting expression meets its requirements.
4344*5a6e8488SAndroid Build Coastguard Worker
4345*5a6e8488SAndroid Build Coastguard WorkerThere are also places where the flags are changed. This is because the
4346*5a6e8488SAndroid Build Coastguard Workerrequirements change.
4347*5a6e8488SAndroid Build Coastguard Worker
4348*5a6e8488SAndroid Build Coastguard WorkerMaintaining the integrity of the requirements flag set is an important part of
4349*5a6e8488SAndroid Build Coastguard Workerthe `bc` parser. However, they do not have to be stored on a stack because their
4350*5a6e8488SAndroid Build Coastguard Workerstack is implicit from the recursion that expression parsing uses.
4351*5a6e8488SAndroid Build Coastguard Worker
4352*5a6e8488SAndroid Build Coastguard Worker### Functions
4353*5a6e8488SAndroid Build Coastguard Worker
4354*5a6e8488SAndroid Build Coastguard WorkerFunctions, in `bc`, are data structures that contain the bytecode and data
4355*5a6e8488SAndroid Build Coastguard Workerproduced by the parsers. Functions are what the `BcProgram` program executes.
4356*5a6e8488SAndroid Build Coastguard Worker
4357*5a6e8488SAndroid Build Coastguard Worker#### Main and Read Functions
4358*5a6e8488SAndroid Build Coastguard Worker
4359*5a6e8488SAndroid Build Coastguard WorkerThere are two functions that always exist, which I call the "main" and "read"
4360*5a6e8488SAndroid Build Coastguard Workerfunctions.
4361*5a6e8488SAndroid Build Coastguard Worker
4362*5a6e8488SAndroid Build Coastguard WorkerThe "main" function is the function in which any code and data outside other
4363*5a6e8488SAndroid Build Coastguard Workerfunctions is put. Basically, it is the function where the scripting code ends
4364*5a6e8488SAndroid Build Coastguard Workerup.
4365*5a6e8488SAndroid Build Coastguard Worker
4366*5a6e8488SAndroid Build Coastguard WorkerThe "read" function is the function that is reset and parsed every time a call
4367*5a6e8488SAndroid Build Coastguard Workerto the `read()` builtin function happens.
4368*5a6e8488SAndroid Build Coastguard Worker
4369*5a6e8488SAndroid Build Coastguard Worker#### `dc` Strings
4370*5a6e8488SAndroid Build Coastguard Worker
4371*5a6e8488SAndroid Build Coastguard WorkerIn `dc`, strings can be executed, and since there are no actual "functions" in
4372*5a6e8488SAndroid Build Coastguard Worker`dc`, strings are handled as functions. In fact, they are effectively translated
4373*5a6e8488SAndroid Build Coastguard Workerinto functions by parsing.
4374*5a6e8488SAndroid Build Coastguard Worker
4375*5a6e8488SAndroid Build Coastguard Worker##### Tail Calls
4376*5a6e8488SAndroid Build Coastguard Worker
4377*5a6e8488SAndroid Build Coastguard WorkerSince strings in `dc` are functions, and the fact that `dc` has no native loops,
4378*5a6e8488SAndroid Build Coastguard Workersuch loops are implemented in `dc` code using strings with conditional execution
4379*5a6e8488SAndroid Build Coastguard Workercommands at the end of strings.
4380*5a6e8488SAndroid Build Coastguard Worker
4381*5a6e8488SAndroid Build Coastguard WorkerWhen such conditional execution, or even unconditional execution, commands are
4382*5a6e8488SAndroid Build Coastguard Workerthe very last commands in a string, then `dc` can perform a [tail call][202].
4383*5a6e8488SAndroid Build Coastguard Worker
4384*5a6e8488SAndroid Build Coastguard WorkerThis is done by recording the fact that a tail call happened, done by
4385*5a6e8488SAndroid Build Coastguard Workerincrementing an integer on a stack. When a string is executed *without* a tail
4386*5a6e8488SAndroid Build Coastguard Workercall, a new entry is pushed onto the stack with the integer `1`.
4387*5a6e8488SAndroid Build Coastguard Worker
4388*5a6e8488SAndroid Build Coastguard WorkerWhen a string finally quits that followed tail calls, its stack entry is popped,
4389*5a6e8488SAndroid Build Coastguard Workereliminating all of those tail calls.
4390*5a6e8488SAndroid Build Coastguard Worker
4391*5a6e8488SAndroid Build Coastguard WorkerWhy perform tail calls? Because otherwise, `dc` would be subject to the same
4392*5a6e8488SAndroid Build Coastguard Workerthing that plagues [functional programming languages][203]: stack overflow. In
4393*5a6e8488SAndroid Build Coastguard Worker`dc`'s case, that would manifest itself as a growing [heap][204], because the
4394*5a6e8488SAndroid Build Coastguard Workerexecution stack is stored on the heap, until a fatal allocation failure would
4395*5a6e8488SAndroid Build Coastguard Workeroccur.
4396*5a6e8488SAndroid Build Coastguard Worker
4397*5a6e8488SAndroid Build Coastguard Worker#### Execution
4398*5a6e8488SAndroid Build Coastguard Worker
4399*5a6e8488SAndroid Build Coastguard WorkerExecution is handled by an interpreter implemented using `BcProgram` and code
4400*5a6e8488SAndroid Build Coastguard Workerin [`src/program.c`][53].
4401*5a6e8488SAndroid Build Coastguard Worker
4402*5a6e8488SAndroid Build Coastguard WorkerThe interpreter is a mix between a [stack machine][210] and a [register
4403*5a6e8488SAndroid Build Coastguard Workermachine][211]. It is a stack machine in that operations happen on a stack I call
4404*5a6e8488SAndroid Build Coastguard Workerthe "results stack," but it is a register machine in that items on the stack can
4405*5a6e8488SAndroid Build Coastguard Workerbe stored to and loaded from "registers" (`dc` terminology), variables (`bc`
4406*5a6e8488SAndroid Build Coastguard Workerterminology), and arrays.
4407*5a6e8488SAndroid Build Coastguard Worker
4408*5a6e8488SAndroid Build Coastguard Worker##### Stacks
4409*5a6e8488SAndroid Build Coastguard Worker
4410*5a6e8488SAndroid Build Coastguard WorkerThere are two stacks in the interpreter:
4411*5a6e8488SAndroid Build Coastguard Worker
4412*5a6e8488SAndroid Build Coastguard Worker* The "results" stack (as mentioned above).
4413*5a6e8488SAndroid Build Coastguard Worker* The "execution" stack.
4414*5a6e8488SAndroid Build Coastguard Worker
4415*5a6e8488SAndroid Build Coastguard WorkerThe results stack (the `results` field of the `BcProgram` struct) is the stack
4416*5a6e8488SAndroid Build Coastguard Workerwhere the results of computations are stored. It is what makes the interpreter
4417*5a6e8488SAndroid Build Coastguard Workerpart [stack machine][210]. It is filled with `BcResult`'s.
4418*5a6e8488SAndroid Build Coastguard Worker
4419*5a6e8488SAndroid Build Coastguard WorkerThe execution stack (the `stack` field of the `BcProgram` struct) is the stack
4420*5a6e8488SAndroid Build Coastguard Workerthat tracks the current execution state of the interpreter. It is the presence
4421*5a6e8488SAndroid Build Coastguard Workerof this separate stack that allows the interpreter to implement the machine as a
4422*5a6e8488SAndroid Build Coastguard Workerloop, rather than recursively. It is filled with `BcInstPtr`'s, which are the
4423*5a6e8488SAndroid Build Coastguard Worker"instruction pointers."
4424*5a6e8488SAndroid Build Coastguard Worker
4425*5a6e8488SAndroid Build Coastguard WorkerThese instruction pointers have three fields, all integers:
4426*5a6e8488SAndroid Build Coastguard Worker
4427*5a6e8488SAndroid Build Coastguard Worker* `func`, the index of the function that is currently executing.
4428*5a6e8488SAndroid Build Coastguard Worker* `idx`, the index of the next bytecode instruction to execute in the function's
4429*5a6e8488SAndroid Build Coastguard Worker  bytecode array.
4430*5a6e8488SAndroid Build Coastguard Worker* `len`, which is the length of the results stack when the function started
4431*5a6e8488SAndroid Build Coastguard Worker  executing. This is not used by `dc`, but it used by `bc` because functions
4432*5a6e8488SAndroid Build Coastguard Worker  in `bc` should never affect the results stack of their callers.
4433*5a6e8488SAndroid Build Coastguard Worker
4434*5a6e8488SAndroid Build Coastguard WorkerWith these three fields, and always executing using the instruction pointer at
4435*5a6e8488SAndroid Build Coastguard Workerthe top of the execution stack, the interpreter can always keep track of its
4436*5a6e8488SAndroid Build Coastguard Workerexecution.
4437*5a6e8488SAndroid Build Coastguard Worker
4438*5a6e8488SAndroid Build Coastguard WorkerWhen a function or a string starts executing, a new `BcInstPtr` is pushed onto
4439*5a6e8488SAndroid Build Coastguard Workerthe execution stack for it. This includes if a function was called recursively.
4440*5a6e8488SAndroid Build Coastguard WorkerAnd then, when the function or string returns, its `BcInstPtr` is popped off of
4441*5a6e8488SAndroid Build Coastguard Workerthe execution stack.
4442*5a6e8488SAndroid Build Coastguard Worker
4443*5a6e8488SAndroid Build Coastguard Worker##### Bytecode
4444*5a6e8488SAndroid Build Coastguard Worker
4445*5a6e8488SAndroid Build Coastguard WorkerExecution of functions are done through bytecode produced directly by the
4446*5a6e8488SAndroid Build Coastguard Workerparsers (see the [Parsing][209]). This bytecode is stored in the `code`
4447*5a6e8488SAndroid Build Coastguard Worker[vector][111] of the `BcFunc` struct.
4448*5a6e8488SAndroid Build Coastguard Worker
4449*5a6e8488SAndroid Build Coastguard WorkerThis is a vector for two reasons:
4450*5a6e8488SAndroid Build Coastguard Worker
4451*5a6e8488SAndroid Build Coastguard Worker* It makes it easier to add bytecode to the vector in the parsers.
4452*5a6e8488SAndroid Build Coastguard Worker* `bc` allows users to redefine functions.
4453*5a6e8488SAndroid Build Coastguard Worker
4454*5a6e8488SAndroid Build Coastguard WorkerThe reason I can use bytecode is because there are less than 256 instructions,
4455*5a6e8488SAndroid Build Coastguard Workerso an `unsigned char` can store all the bytecodes.
4456*5a6e8488SAndroid Build Coastguard Worker
4457*5a6e8488SAndroid Build Coastguard Worker###### Bytecode Indices
4458*5a6e8488SAndroid Build Coastguard Worker
4459*5a6e8488SAndroid Build Coastguard WorkerThere is one other factor to bytecode: there are instructions that need to
4460*5a6e8488SAndroid Build Coastguard Workerreference strings, constants, variables, or arrays. Bytecode need some way to
4461*5a6e8488SAndroid Build Coastguard Workerreference those things.
4462*5a6e8488SAndroid Build Coastguard Worker
4463*5a6e8488SAndroid Build Coastguard WorkerFortunately, all of those things can be referenced in the same way: with indices
4464*5a6e8488SAndroid Build Coastguard Workerbecause all of the items are in vectors.
4465*5a6e8488SAndroid Build Coastguard Worker
4466*5a6e8488SAndroid Build Coastguard WorkerSo `bc` has a way of encoding an index into bytecode. It does this by, after
4467*5a6e8488SAndroid Build Coastguard Workerpushing the instruction that references anything, pushing a byte set to the
4468*5a6e8488SAndroid Build Coastguard Workerlength of the index in bytes, then the bytes of the index are pushed in
4469*5a6e8488SAndroid Build Coastguard Workerlittle-endian order.
4470*5a6e8488SAndroid Build Coastguard Worker
4471*5a6e8488SAndroid Build Coastguard WorkerThen, when the interpreter encounters an instruction that needs one or more
4472*5a6e8488SAndroid Build Coastguard Workeritems, it decodes the index or indices there and updates the `idx` field of the
4473*5a6e8488SAndroid Build Coastguard Workercurrent `BcInstPtr` to point to the byte after the index or indices.
4474*5a6e8488SAndroid Build Coastguard Worker
4475*5a6e8488SAndroid Build Coastguard WorkerOne more thing: the encoder of the indices only pushes as many bytes as
4476*5a6e8488SAndroid Build Coastguard Workernecessary to encode the index. It stops pushing when the index has no more bytes
4477*5a6e8488SAndroid Build Coastguard Workerwith any 1 bits.
4478*5a6e8488SAndroid Build Coastguard Worker
4479*5a6e8488SAndroid Build Coastguard Worker##### Variables
4480*5a6e8488SAndroid Build Coastguard Worker
4481*5a6e8488SAndroid Build Coastguard WorkerIn `bc`, the vector of variables, `vars` in `BcProgram`, is not a vector of
4482*5a6e8488SAndroid Build Coastguard Workernumbers; it is a vector of vector of numbers. The first vector is the vector of
4483*5a6e8488SAndroid Build Coastguard Workervariables, the second is the variable stack, and the last level is the actual
4484*5a6e8488SAndroid Build Coastguard Workernumber.
4485*5a6e8488SAndroid Build Coastguard Worker
4486*5a6e8488SAndroid Build Coastguard WorkerThis is because both `bc` and `dc` need variables to be stacks.
4487*5a6e8488SAndroid Build Coastguard Worker
4488*5a6e8488SAndroid Build Coastguard WorkerFor `dc`, registers are *defined* to be stacks.
4489*5a6e8488SAndroid Build Coastguard Worker
4490*5a6e8488SAndroid Build Coastguard WorkerFor `bc`, variables as stacks is how function arguments/parameters and function
4491*5a6e8488SAndroid Build Coastguard Worker`auto` variables are implemented.
4492*5a6e8488SAndroid Build Coastguard Worker
4493*5a6e8488SAndroid Build Coastguard WorkerWhen a function is called, and a value needs to be used as a function argument,
4494*5a6e8488SAndroid Build Coastguard Workera copy of the value is pushed onto the stack corresponding to the variable with
4495*5a6e8488SAndroid Build Coastguard Workerthe same name as the function's parameter. For `auto` variables, a new number
4496*5a6e8488SAndroid Build Coastguard Workerset to zero is pushed onto each stack corresponding to the `auto` variables.
4497*5a6e8488SAndroid Build Coastguard Worker(Zero is used because the [`bc` spec][2] requires that `auto` variables are set
4498*5a6e8488SAndroid Build Coastguard Workerto zero.)
4499*5a6e8488SAndroid Build Coastguard Worker
4500*5a6e8488SAndroid Build Coastguard WorkerIt is in this way that the old value of the variable, which may not even be
4501*5a6e8488SAndroid Build Coastguard Workerrelated to the function parameter or `auto` variable, is preserved while the
4502*5a6e8488SAndroid Build Coastguard Workervariable is used as a function parameter or `auto` variable.
4503*5a6e8488SAndroid Build Coastguard Worker
4504*5a6e8488SAndroid Build Coastguard WorkerWhen the function returns, of course, the stacks of the variables for the
4505*5a6e8488SAndroid Build Coastguard Workerparameters and `auto`'s will have their top item popped, restoring the old value
4506*5a6e8488SAndroid Build Coastguard Workeras it was before the function call.
4507*5a6e8488SAndroid Build Coastguard Worker
4508*5a6e8488SAndroid Build Coastguard Worker##### Arrays
4509*5a6e8488SAndroid Build Coastguard Worker
4510*5a6e8488SAndroid Build Coastguard WorkerLike variables, arrays are also implemented as stacks. However, because they are
4511*5a6e8488SAndroid Build Coastguard Workerarrays, there is yet another level; the `arrs` field in `BcProgram` is a vector
4512*5a6e8488SAndroid Build Coastguard Workerof vectors of vectors of numbers. The first of the two levels is the vector of
4513*5a6e8488SAndroid Build Coastguard Workerarrays, the second the stack of for each array, the third the actual array, and
4514*5a6e8488SAndroid Build Coastguard Workerlast the numbers in the array.
4515*5a6e8488SAndroid Build Coastguard Worker
4516*5a6e8488SAndroid Build Coastguard Worker`dc` has no need of this extra stack, but `bc` does because arrays can be
4517*5a6e8488SAndroid Build Coastguard Workerfunction parameters themselves.
4518*5a6e8488SAndroid Build Coastguard Worker
4519*5a6e8488SAndroid Build Coastguard WorkerWhen arrays are used for function arguments, they are copied with a deep copy;
4520*5a6e8488SAndroid Build Coastguard Workereach item of the source vector is copied. This is because in `bc`, according to
4521*5a6e8488SAndroid Build Coastguard Workerthe [`bc` spec][2], all function arguments are passed by value.
4522*5a6e8488SAndroid Build Coastguard Worker
4523*5a6e8488SAndroid Build Coastguard WorkerHowever, array references are possible (see below).
4524*5a6e8488SAndroid Build Coastguard Worker
4525*5a6e8488SAndroid Build Coastguard WorkerWhen arrays are used as `auto`'s, a new vector is pushed with one element; if
4526*5a6e8488SAndroid Build Coastguard Workermore elements are needed, the array is grown automatically, and new elements are
4527*5a6e8488SAndroid Build Coastguard Workergiven the value of zero.
4528*5a6e8488SAndroid Build Coastguard Worker
4529*5a6e8488SAndroid Build Coastguard WorkerIn fact, if *any* array is accessed and does not have an element at that index,
4530*5a6e8488SAndroid Build Coastguard Workerthe array is automatically grown to that size, and all new elements are given
4531*5a6e8488SAndroid Build Coastguard Workerthe value zero. This behavior is guaranteed by the [`bc` spec][2].
4532*5a6e8488SAndroid Build Coastguard Worker
4533*5a6e8488SAndroid Build Coastguard Worker###### Array References
4534*5a6e8488SAndroid Build Coastguard Worker
4535*5a6e8488SAndroid Build Coastguard WorkerArray references had to be implemented as vectors themselves because they must
4536*5a6e8488SAndroid Build Coastguard Workerbe pushed on the vectors stacks, which, as seen above, expect vectors
4537*5a6e8488SAndroid Build Coastguard Workerthemselves.
4538*5a6e8488SAndroid Build Coastguard Worker
4539*5a6e8488SAndroid Build Coastguard WorkerSo thus, references are implemented as vectors on the vector stacks. These
4540*5a6e8488SAndroid Build Coastguard Workervectors are not vectors of vectors themselves; they are vectors of bytes; in
4541*5a6e8488SAndroid Build Coastguard Workerfact, the fact that they are byte vectors and not vector vectors is how a
4542*5a6e8488SAndroid Build Coastguard Workerreference vector is detected.
4543*5a6e8488SAndroid Build Coastguard Worker
4544*5a6e8488SAndroid Build Coastguard WorkerThese reference vectors always have the same two things pushed: a byte encoding
4545*5a6e8488SAndroid Build Coastguard Worker(the same way bytecode indices are) of the referenced vector's index in the
4546*5a6e8488SAndroid Build Coastguard Worker`arrs` vector, and a byte encoding of the referenced vectors index in the vector
4547*5a6e8488SAndroid Build Coastguard Workerstack.
4548*5a6e8488SAndroid Build Coastguard Worker
4549*5a6e8488SAndroid Build Coastguard WorkerIf an item in a referenced vector is needed, then the reference is dereferenced,
4550*5a6e8488SAndroid Build Coastguard Workerand the item is returned.
4551*5a6e8488SAndroid Build Coastguard Worker
4552*5a6e8488SAndroid Build Coastguard WorkerIf a reference vector is passed to a function that does *not* expect a
4553*5a6e8488SAndroid Build Coastguard Workerreference, the vector is dereferenced and a deep copy is done, in the same way
4554*5a6e8488SAndroid Build Coastguard Workeras vectors are copied for normal array function parameters.
4555*5a6e8488SAndroid Build Coastguard Worker
4556*5a6e8488SAndroid Build Coastguard Worker### Callbacks
4557*5a6e8488SAndroid Build Coastguard Worker
4558*5a6e8488SAndroid Build Coastguard WorkerThere are many places in `bc` and `dc` where function pointers are used:
4559*5a6e8488SAndroid Build Coastguard Worker
4560*5a6e8488SAndroid Build Coastguard Worker* To implement destructors in vectors. (See the [Vectors][111] section.)
4561*5a6e8488SAndroid Build Coastguard Worker* To select the correct lex and parse functions for `bc` and `dc`.
4562*5a6e8488SAndroid Build Coastguard Worker* To select the correct function to execute unary operators.
4563*5a6e8488SAndroid Build Coastguard Worker* To select the correct function to execute binary operators.
4564*5a6e8488SAndroid Build Coastguard Worker* To calculate the correct number size for binary operators.
4565*5a6e8488SAndroid Build Coastguard Worker* To print a "digit" of a number.
4566*5a6e8488SAndroid Build Coastguard Worker* To seed the pseudo-random number generator.
4567*5a6e8488SAndroid Build Coastguard Worker
4568*5a6e8488SAndroid Build Coastguard WorkerAnd there might be more.
4569*5a6e8488SAndroid Build Coastguard Worker
4570*5a6e8488SAndroid Build Coastguard WorkerIn every case, they are used for reducing the amount of code. Instead of
4571*5a6e8488SAndroid Build Coastguard Worker`if`/`else` chains, such as:
4572*5a6e8488SAndroid Build Coastguard Worker
4573*5a6e8488SAndroid Build Coastguard Worker```
4574*5a6e8488SAndroid Build Coastguard Workerif (BC_IS_BC) {
4575*5a6e8488SAndroid Build Coastguard Worker	bc_parse_parse(vm.parse);
4576*5a6e8488SAndroid Build Coastguard Worker}
4577*5a6e8488SAndroid Build Coastguard Workerelse {
4578*5a6e8488SAndroid Build Coastguard Worker	dc_parse_parse(vm.parse);
4579*5a6e8488SAndroid Build Coastguard Worker}
4580*5a6e8488SAndroid Build Coastguard Worker```
4581*5a6e8488SAndroid Build Coastguard Worker
4582*5a6e8488SAndroid Build Coastguard WorkerThe best example of this is `bc_num_binary()`. It is called by every binary
4583*5a6e8488SAndroid Build Coastguard Workeroperator. It figures out if it needs to allocate space for a new `BcNum`. If so,
4584*5a6e8488SAndroid Build Coastguard Workerit allocates the space and then calls the function pointer to the *true*
4585*5a6e8488SAndroid Build Coastguard Workeroperation.
4586*5a6e8488SAndroid Build Coastguard Worker
4587*5a6e8488SAndroid Build Coastguard WorkerDoing it like that shrunk the code *immensely*. First, instead of every single
4588*5a6e8488SAndroid Build Coastguard Workerbinary operator duplicating the allocation code, it only exists in one place.
4589*5a6e8488SAndroid Build Coastguard WorkerSecond, `bc_num_binary()` itself does not have a massive `if`/`else` chain or a
4590*5a6e8488SAndroid Build Coastguard Worker`switch` statement.
4591*5a6e8488SAndroid Build Coastguard Worker
4592*5a6e8488SAndroid Build Coastguard WorkerBut perhaps the most important use was for destructors in vectors.
4593*5a6e8488SAndroid Build Coastguard Worker
4594*5a6e8488SAndroid Build Coastguard WorkerMost of the data structures in `bc` are stored in vectors. If I hadn't made
4595*5a6e8488SAndroid Build Coastguard Workerdestructors available for vectors, then ensuring that `bc` had no memory leaks
4596*5a6e8488SAndroid Build Coastguard Workerwould have been nigh impossible. As it is, I check `bc` for memory leaks every
4597*5a6e8488SAndroid Build Coastguard Workerrelease when I change the code, and I have not released `bc` after version
4598*5a6e8488SAndroid Build Coastguard Worker`1.0.0` with any memory leaks, as far as I can remember anyway.
4599*5a6e8488SAndroid Build Coastguard Worker
4600*5a6e8488SAndroid Build Coastguard Worker### Numbers
4601*5a6e8488SAndroid Build Coastguard Worker
4602*5a6e8488SAndroid Build Coastguard WorkerIn order to do arbitrary-precision math, as `bc` must do, there must be some way
4603*5a6e8488SAndroid Build Coastguard Workerof representing arbitrary-precision numbers. `BcNum` in [`include/num.h`][184]
4604*5a6e8488SAndroid Build Coastguard Workeris `bc`'s way of doing that.
4605*5a6e8488SAndroid Build Coastguard Worker
4606*5a6e8488SAndroid Build Coastguard Worker(Note: the word ["limb"][214] is used below; it has a specific meaning when
4607*5a6e8488SAndroid Build Coastguard Workerapplied to arbitrary-precision numbers. It means one piece of the number. It can
4608*5a6e8488SAndroid Build Coastguard Workerhave a single digit, which is what GNU `bc` does, or it can have multiple, which
4609*5a6e8488SAndroid Build Coastguard Workeris what this `bc` does.)
4610*5a6e8488SAndroid Build Coastguard Worker
4611*5a6e8488SAndroid Build Coastguard WorkerThis struct needs to store several things:
4612*5a6e8488SAndroid Build Coastguard Worker
4613*5a6e8488SAndroid Build Coastguard Worker* The array of limbs of the number. This is the `num` field.
4614*5a6e8488SAndroid Build Coastguard Worker* The location of the decimal point. This is the `rdx` (short for [radix][215])
4615*5a6e8488SAndroid Build Coastguard Worker  field.
4616*5a6e8488SAndroid Build Coastguard Worker* The number of limbs the number has. This is the `len` field.
4617*5a6e8488SAndroid Build Coastguard Worker* Whether the number is negative or not. This is the least significant bit of
4618*5a6e8488SAndroid Build Coastguard Worker  the `rdx` field. More on that later.
4619*5a6e8488SAndroid Build Coastguard Worker
4620*5a6e8488SAndroid Build Coastguard WorkerIn addition, `bc`'s number stores the capacity of the limb array; this is the
4621*5a6e8488SAndroid Build Coastguard Worker`cap` field.
4622*5a6e8488SAndroid Build Coastguard Worker
4623*5a6e8488SAndroid Build Coastguard WorkerIf the number needs to grow, and the capacity of the number is big enough, the
4624*5a6e8488SAndroid Build Coastguard Workernumber is not reallocated; the number of limbs is just added to.
4625*5a6e8488SAndroid Build Coastguard Worker
4626*5a6e8488SAndroid Build Coastguard WorkerThere is one additional wrinkle: to make the usual operations (binary operators)
4627*5a6e8488SAndroid Build Coastguard Workerfast, the decimal point is *not* allowed to be in the middle of a limb; it must
4628*5a6e8488SAndroid Build Coastguard Workeralways be between limbs, after all limbs (integer), or before all limbs (real
4629*5a6e8488SAndroid Build Coastguard Workerbetween -1 and 1).
4630*5a6e8488SAndroid Build Coastguard Worker
4631*5a6e8488SAndroid Build Coastguard WorkerThe reason for this is because addition, subtraction, multiplication, and
4632*5a6e8488SAndroid Build Coastguard Workerdivision expect digits to be lined up on the decimal point. By requiring that it
4633*5a6e8488SAndroid Build Coastguard Workerbe between limbs, no extra alignment is needed, and those operations can proceed
4634*5a6e8488SAndroid Build Coastguard Workerwithout extra overhead.
4635*5a6e8488SAndroid Build Coastguard Worker
4636*5a6e8488SAndroid Build Coastguard WorkerThis does make some operations, most notably extending, truncating, and
4637*5a6e8488SAndroid Build Coastguard Workershifting, more expensive, but the overhead is constant, and these operations are
4638*5a6e8488SAndroid Build Coastguard Workerusually cheap compared to the binary operators anyway.
4639*5a6e8488SAndroid Build Coastguard Worker
4640*5a6e8488SAndroid Build Coastguard WorkerThis also requires something else: `bc` numbers need to know *exactly* how many
4641*5a6e8488SAndroid Build Coastguard Workerdecimal places they have after the decimal point. If the decimal point must be
4642*5a6e8488SAndroid Build Coastguard Workerinbetween limbs, the last decimal place could be in the middle of a limb. The
4643*5a6e8488SAndroid Build Coastguard Workeramount of decimal places in a number is carefully tracked and stored in the
4644*5a6e8488SAndroid Build Coastguard Worker`scale` field, and this number must always coincide with the `rdx` field by the
4645*5a6e8488SAndroid Build Coastguard Workerfollowing formula:
4646*5a6e8488SAndroid Build Coastguard Worker
4647*5a6e8488SAndroid Build Coastguard Worker```
4648*5a6e8488SAndroid Build Coastguard Workerscale + (BC_BASE_DIGS - 1) / BC_BASE_DIGS == rdx >> 1
4649*5a6e8488SAndroid Build Coastguard Worker```
4650*5a6e8488SAndroid Build Coastguard Worker
4651*5a6e8488SAndroid Build Coastguard Worker(`BC_BASE_DIGS` is the number of decimal digits stored in one limb. It is 9 on
4652*5a6e8488SAndroid Build Coastguard Worker64-bit systems and 4 on other systems.)
4653*5a6e8488SAndroid Build Coastguard Worker
4654*5a6e8488SAndroid Build Coastguard WorkerYes, `rdx` is shifted; that is because the negative bit is stored in the least
4655*5a6e8488SAndroid Build Coastguard Workersignificant bit of the `rdx` field, and the actual radix (amount of limbs after
4656*5a6e8488SAndroid Build Coastguard Workerthe decimal/radix point) is stored in the rest of the bits. This is safe because
4657*5a6e8488SAndroid Build Coastguard Worker`BC_BASE_DIGS` is always at least 4, which means `rdx` will always need at least
4658*5a6e8488SAndroid Build Coastguard Worker2 bits less than `scale`.
4659*5a6e8488SAndroid Build Coastguard Worker
4660*5a6e8488SAndroid Build Coastguard WorkerIn addition to `rdx` always matching `scale`, another invariant is that `rdx`
4661*5a6e8488SAndroid Build Coastguard Workermust always be less than or equal to `len`. (Because `scale` may be greater than
4662*5a6e8488SAndroid Build Coastguard Worker`rdx`, `scale` does not have to be less than or equal to `len`.)
4663*5a6e8488SAndroid Build Coastguard Worker
4664*5a6e8488SAndroid Build Coastguard WorkerAnother invariant is that `len` must always be less than or equal to `cap`, for
4665*5a6e8488SAndroid Build Coastguard Workerobvious reasons.
4666*5a6e8488SAndroid Build Coastguard Worker
4667*5a6e8488SAndroid Build Coastguard WorkerThe last thing programmers need to know is that the limb array is stored in
4668*5a6e8488SAndroid Build Coastguard Workerlittle-endian order. This means that the last decimal places are in the limb
4669*5a6e8488SAndroid Build Coastguard Workerstored at index 0, and the most significant digits are stored at index `len-1`.
4670*5a6e8488SAndroid Build Coastguard Worker
4671*5a6e8488SAndroid Build Coastguard WorkerThis is done to make the most important operations fast. Addition and
4672*5a6e8488SAndroid Build Coastguard Workersubtraction are done from least significant to most significant limbs, which
4673*5a6e8488SAndroid Build Coastguard Workermeans they can speed through memory in the way most computers are best at.
4674*5a6e8488SAndroid Build Coastguard WorkerMultiplication does the same, sort of, and with division, it matters less.
4675*5a6e8488SAndroid Build Coastguard WorkerComparison does need to go backwards, but that's after exhausting all other
4676*5a6e8488SAndroid Build Coastguard Workeralternatives, including for example, checking the length of the integer portion
4677*5a6e8488SAndroid Build Coastguard Workerof each limb array.
4678*5a6e8488SAndroid Build Coastguard Worker
4679*5a6e8488SAndroid Build Coastguard WorkerFinally, here are some possible special situations with numbers and what they
4680*5a6e8488SAndroid Build Coastguard Workermean:
4681*5a6e8488SAndroid Build Coastguard Worker
4682*5a6e8488SAndroid Build Coastguard Worker* `len == 0`: the number equals 0.
4683*5a6e8488SAndroid Build Coastguard Worker* `len == 0 && scale != 0`: the number equals 0, but it has a `scale` value.
4684*5a6e8488SAndroid Build Coastguard Worker  This is the only case where `scale` does not have to coincide with `rdx`
4685*5a6e8488SAndroid Build Coastguard Worker  This can happen with division, for example, that sets a specific `scale` for
4686*5a6e8488SAndroid Build Coastguard Worker  the result value but may produce 0.
4687*5a6e8488SAndroid Build Coastguard Worker* `(rdx >> 1) == len`: the number is greater than or equal to 1, or less than or
4688*5a6e8488SAndroid Build Coastguard Worker  equal to -1.
4689*5a6e8488SAndroid Build Coastguard Worker* `(rdx >> 1) < len`: the number is greater than -1 and less than 1, not
4690*5a6e8488SAndroid Build Coastguard Worker  including 0, although this will be true for 0 as well. However, 0 is always
4691*5a6e8488SAndroid Build Coastguard Worker  assumed to be represented by `len == 0`.
4692*5a6e8488SAndroid Build Coastguard Worker* `(rdx >> 1) == 0`: the number is an integer. In this case, `scale` must also
4693*5a6e8488SAndroid Build Coastguard Worker  equal 0.
4694*5a6e8488SAndroid Build Coastguard Worker
4695*5a6e8488SAndroid Build Coastguard Worker#### Math Style
4696*5a6e8488SAndroid Build Coastguard Worker
4697*5a6e8488SAndroid Build Coastguard WorkerWhen I wrote the math for `bc`, I adopted a certain style that, if known, will
4698*5a6e8488SAndroid Build Coastguard Workermake it easier to understand the code. The style follows these rules:
4699*5a6e8488SAndroid Build Coastguard Worker
4700*5a6e8488SAndroid Build Coastguard Worker* `BcNum` arguments always come before arguments of other types.
4701*5a6e8488SAndroid Build Coastguard Worker* Among the `BcNum` arguments, the operands always come first, and the `BcNum`
4702*5a6e8488SAndroid Build Coastguard Worker  where the result(s) will be stored come last.
4703*5a6e8488SAndroid Build Coastguard Worker* Error checking is placed first in the function.
4704*5a6e8488SAndroid Build Coastguard Worker* Easy cases are placed next.
4705*5a6e8488SAndroid Build Coastguard Worker* Preparation, such as allocating temporaries, comes next.
4706*5a6e8488SAndroid Build Coastguard Worker* The actual math.
4707*5a6e8488SAndroid Build Coastguard Worker* Cleanup and ensuring invariants.
4708*5a6e8488SAndroid Build Coastguard Worker
4709*5a6e8488SAndroid Build Coastguard WorkerWhile these rules are not hard and fast, using them as a guide will probably
4710*5a6e8488SAndroid Build Coastguard Workerhelp.
4711*5a6e8488SAndroid Build Coastguard Worker
4712*5a6e8488SAndroid Build Coastguard Worker#### Digit Clamping
4713*5a6e8488SAndroid Build Coastguard Worker
4714*5a6e8488SAndroid Build Coastguard WorkerThere is a question of what to do when parsing numbers and coming across digits
4715*5a6e8488SAndroid Build Coastguard Workerthat are greater than or equal to the current `ibase`. `bc` and `dc` do one of
4716*5a6e8488SAndroid Build Coastguard Workertwo things:
4717*5a6e8488SAndroid Build Coastguard Worker
4718*5a6e8488SAndroid Build Coastguard Worker* Clamp the digit to the maximum correct digit, or
4719*5a6e8488SAndroid Build Coastguard Worker* Use the digit as-is, multiplying it by the `ibase` to the power of the digit's
4720*5a6e8488SAndroid Build Coastguard Worker  position (starting from 0 at the least significant digit).
4721*5a6e8488SAndroid Build Coastguard Worker
4722*5a6e8488SAndroid Build Coastguard WorkerThe former behavior is what GNU `bc` does, and the latter is what GNU `dc`, the
4723*5a6e8488SAndroid Build Coastguard WorkerOpenBSD `bc`, and the OpenBSD `dc` do.
4724*5a6e8488SAndroid Build Coastguard Worker
4725*5a6e8488SAndroid Build Coastguard WorkerIt is possible to switch between the two with the `-c` and `-C` command-line
4726*5a6e8488SAndroid Build Coastguard Workeroptions. However, it is important for developers to understand that both
4727*5a6e8488SAndroid Build Coastguard Workerbehaviors exist and should exist.
4728*5a6e8488SAndroid Build Coastguard Worker
4729*5a6e8488SAndroid Build Coastguard WorkerThe library can also do both, and it is set in a global for each thread.
4730*5a6e8488SAndroid Build Coastguard Worker
4731*5a6e8488SAndroid Build Coastguard Worker### Strings as Numbers
4732*5a6e8488SAndroid Build Coastguard Worker
4733*5a6e8488SAndroid Build Coastguard WorkerStrings can be assigned to variables. This is a problem because the vectors for
4734*5a6e8488SAndroid Build Coastguard Workervariable stacks expect `BcNum` structs only.
4735*5a6e8488SAndroid Build Coastguard Worker
4736*5a6e8488SAndroid Build Coastguard WorkerWhile I could have made a union, I decided that the complexity of adding an
4737*5a6e8488SAndroid Build Coastguard Workerentirely new type, with destructor and everything, was not worth it. Instead, I
4738*5a6e8488SAndroid Build Coastguard Workertook advantage of the fact that `free()`, when passed a `NULL` pointer, will do
4739*5a6e8488SAndroid Build Coastguard Workernothing.
4740*5a6e8488SAndroid Build Coastguard Worker
4741*5a6e8488SAndroid Build Coastguard WorkerUsing that, I made it so `BcNum`'s could store strings instead. This is marked
4742*5a6e8488SAndroid Build Coastguard Workerby the `BcNum` having a `NULL` limb array (`num`) and a `cap` of 0 (which should
4743*5a6e8488SAndroid Build Coastguard Worker*never* happen with a real number, though the other fields could be 0).
4744*5a6e8488SAndroid Build Coastguard Worker
4745*5a6e8488SAndroid Build Coastguard WorkerThe `BcNum` stores the function that stores the string in the `rdx` field, and
4746*5a6e8488SAndroid Build Coastguard Workerit stores the index of the string in the `scale` field. This is used to actually
4747*5a6e8488SAndroid Build Coastguard Workerload the string if necessary.
4748*5a6e8488SAndroid Build Coastguard Worker
4749*5a6e8488SAndroid Build Coastguard WorkerNote that historically, string information was stored in the `loc` field of
4750*5a6e8488SAndroid Build Coastguard Workerthe `d` union in a `BcResult`. This was changed recently to standardize; now,
4751*5a6e8488SAndroid Build Coastguard Workerall string information are stored in the `n` field of the `d` union regardless.
4752*5a6e8488SAndroid Build Coastguard WorkerThis means that all string information is stored in `BcNum`'s. This removes
4753*5a6e8488SAndroid Build Coastguard Workerextra cases.
4754*5a6e8488SAndroid Build Coastguard Worker
4755*5a6e8488SAndroid Build Coastguard WorkerAlso, if a temp is made with a string, then the result type should still be
4756*5a6e8488SAndroid Build Coastguard Worker`BC_RESULT_STR`, not `BC_RESULT_TEMP`. This is to make it easier to do type
4757*5a6e8488SAndroid Build Coastguard Workerchecks.
4758*5a6e8488SAndroid Build Coastguard Worker
4759*5a6e8488SAndroid Build Coastguard Worker### Pseudo-Random Number Generator
4760*5a6e8488SAndroid Build Coastguard Worker
4761*5a6e8488SAndroid Build Coastguard WorkerIn order to understand this section, I suggest you read the information in the
4762*5a6e8488SAndroid Build Coastguard Workermanpages about the pseudo-random number generator (PRNG) first; that will help
4763*5a6e8488SAndroid Build Coastguard Workeryou understand the guarantees it has, which is important because this section
4764*5a6e8488SAndroid Build Coastguard Workerdelves into implementation details.
4765*5a6e8488SAndroid Build Coastguard Worker
4766*5a6e8488SAndroid Build Coastguard WorkerFirst, the PRNG I use is seeded; this is because most OS's have an excellent
4767*5a6e8488SAndroid Build Coastguard Workercryptographically secure PRNG available via command-line, usually
4768*5a6e8488SAndroid Build Coastguard Worker`/dev/urandom`, but the only *seeded* PRNG available is usually `bash`'s
4769*5a6e8488SAndroid Build Coastguard Worker`$RANDOM`, which is essentially a wrapper around C's `rand()`.
4770*5a6e8488SAndroid Build Coastguard Worker
4771*5a6e8488SAndroid Build Coastguard Worker`rand()` is...bad. It is only guaranteed to return 15 bits of random data.
4772*5a6e8488SAndroid Build Coastguard WorkerObviously, getting good random data out of that would be hard with that alone,
4773*5a6e8488SAndroid Build Coastguard Workerbut implementations also seem to be poor.
4774*5a6e8488SAndroid Build Coastguard Worker
4775*5a6e8488SAndroid Build Coastguard WorkerOn top of that, `bc` is an arbitrary-precision calculator; if I made it able to
4776*5a6e8488SAndroid Build Coastguard Workergenerate random numbers, I could make it generate random numbers of any size,
4777*5a6e8488SAndroid Build Coastguard Workerand since it would be seeded, results would be reproducible, when wanted.
4778*5a6e8488SAndroid Build Coastguard Worker
4779*5a6e8488SAndroid Build Coastguard WorkerSo to get that, I needed a seeded PRNG with good characteristics. After scouring
4780*5a6e8488SAndroid Build Coastguard Workerthe Internet, I decided on the [PCG PRNG][215], mostly because of [this blog
4781*5a6e8488SAndroid Build Coastguard Workerpost][216]. Part of the reason was the behavior of the xoroshiro128+ author, who
4782*5a6e8488SAndroid Build Coastguard Workerhates on PCG and its author, but also because PCG seemed to do better when
4783*5a6e8488SAndroid Build Coastguard Workertested by independent parties.
4784*5a6e8488SAndroid Build Coastguard Worker
4785*5a6e8488SAndroid Build Coastguard WorkerAfter that decision, I faced a challenge: PCG requires 255 bits of seed: 128 for
4786*5a6e8488SAndroid Build Coastguard Workerthe actual seed, and 127 for the "increment." (Melissa O'Neill, the PCG author,
4787*5a6e8488SAndroid Build Coastguard Workerlikens the increment to selecting a codebook.)
4788*5a6e8488SAndroid Build Coastguard Worker
4789*5a6e8488SAndroid Build Coastguard WorkerI could, of course, put the entire 255 bits into one massive arbitrary-precision
4790*5a6e8488SAndroid Build Coastguard Workernumber; `bc` is good at that, after all. But that didn't sit right with me
4791*5a6e8488SAndroid Build Coastguard Workerbecause it would mean any seed selected by users would have the real portion
4792*5a6e8488SAndroid Build Coastguard Workerignored, which is stupid in a program like `bc`.
4793*5a6e8488SAndroid Build Coastguard Worker
4794*5a6e8488SAndroid Build Coastguard WorkerInstead, I decided to make the integer portion the increment (clamped down to
4795*5a6e8488SAndroid Build Coastguard Workersize), and the real portion the seed.
4796*5a6e8488SAndroid Build Coastguard Worker
4797*5a6e8488SAndroid Build Coastguard WorkerIn most cases, this would be a bad idea because you cannot, in general, know how
4798*5a6e8488SAndroid Build Coastguard Workermany decimal places you need to represent any number with `n` real digits in
4799*5a6e8488SAndroid Build Coastguard Workerbase `b` in another base. However, there is an easy to how many decimal digits
4800*5a6e8488SAndroid Build Coastguard Workerafter the decimal point it takes to represent reals of base 2 in base 10: the
4801*5a6e8488SAndroid Build Coastguard Workerpower of two.
4802*5a6e8488SAndroid Build Coastguard Worker
4803*5a6e8488SAndroid Build Coastguard WorkerIt turns out that, for base 2 represented in base 10, the power of 2 is
4804*5a6e8488SAndroid Build Coastguard Worker*exactly* how many digits are necessary to represent *any* number `n/2^p`, where
4805*5a6e8488SAndroid Build Coastguard Worker`p` is the power of 2. This is because at every halving, the number of decimal
4806*5a6e8488SAndroid Build Coastguard Workerplaces increases by 1:
4807*5a6e8488SAndroid Build Coastguard Worker
4808*5a6e8488SAndroid Build Coastguard Worker```
4809*5a6e8488SAndroid Build Coastguard Worker0.5
4810*5a6e8488SAndroid Build Coastguard Worker0.25
4811*5a6e8488SAndroid Build Coastguard Worker0.125
4812*5a6e8488SAndroid Build Coastguard Worker0.0625
4813*5a6e8488SAndroid Build Coastguard Worker0.03125
4814*5a6e8488SAndroid Build Coastguard Worker0.015625
4815*5a6e8488SAndroid Build Coastguard Worker...
4816*5a6e8488SAndroid Build Coastguard Worker```
4817*5a6e8488SAndroid Build Coastguard Worker
4818*5a6e8488SAndroid Build Coastguard WorkerThis happens because because every decimal representation of `1/2^p` ends with a
4819*5a6e8488SAndroid Build Coastguard Worker5 because 5 is half of 10. Because 5 is odd, half of it always ends with the
4820*5a6e8488SAndroid Build Coastguard Workerdigits 25, where 2 is where the previous 5 was, and the new 5 is one decimal
4821*5a6e8488SAndroid Build Coastguard Workerplace further right.
4822*5a6e8488SAndroid Build Coastguard Worker
4823*5a6e8488SAndroid Build Coastguard WorkerSo the algorithm to convert all 255 bits of the seed is as follows:
4824*5a6e8488SAndroid Build Coastguard Worker
4825*5a6e8488SAndroid Build Coastguard Worker1.	Convert the increment to a `BcNum`.
4826*5a6e8488SAndroid Build Coastguard Worker2.	Convert the seed to a `BcNum`.
4827*5a6e8488SAndroid Build Coastguard Worker3.	Divide the seed by `2^128` with a `scale` of 128. (For 32-bit systems,
4828*5a6e8488SAndroid Build Coastguard Worker	substitute 64 bits for 128.)
4829*5a6e8488SAndroid Build Coastguard Worker4.	Add the two numbers together.
4830*5a6e8488SAndroid Build Coastguard Worker
4831*5a6e8488SAndroid Build Coastguard WorkerLikewise, the algorithm to convert from a user-supplied number to a seed is:
4832*5a6e8488SAndroid Build Coastguard Worker
4833*5a6e8488SAndroid Build Coastguard Worker1.	Truncate a copy of the number.
4834*5a6e8488SAndroid Build Coastguard Worker2.	Subtract the result from #1 from the original number. This gives the real
4835*5a6e8488SAndroid Build Coastguard Worker	portion of the number.
4836*5a6e8488SAndroid Build Coastguard Worker3.	Clamp the result of #1 to 127 (or 63) bits. This is the increment.
4837*5a6e8488SAndroid Build Coastguard Worker4.	Multiply the result of #2 by `2^128`.
4838*5a6e8488SAndroid Build Coastguard Worker5.	Truncate the result of #4. This is the seed.
4839*5a6e8488SAndroid Build Coastguard Worker
4840*5a6e8488SAndroid Build Coastguard Worker#### Generating Arbitrary-Precision Numbers
4841*5a6e8488SAndroid Build Coastguard Worker
4842*5a6e8488SAndroid Build Coastguard WorkerI wrote a function (`bc_rand_bounded()`) that will return unbiased results with
4843*5a6e8488SAndroid Build Coastguard Workerany bound below the max that PCG can generate.
4844*5a6e8488SAndroid Build Coastguard Worker
4845*5a6e8488SAndroid Build Coastguard WorkerTo generate an integer of arbitrary size using a bound, `bc` simply uses
4846*5a6e8488SAndroid Build Coastguard Worker`bc_rand_bounded()` to generate numbers with a bound `10^BC_BASE_DIGS` for as
4847*5a6e8488SAndroid Build Coastguard Workermany limbs as needed to satisfy the bigger bound.
4848*5a6e8488SAndroid Build Coastguard Worker
4849*5a6e8488SAndroid Build Coastguard WorkerTo generate numbers with arbitrary precision after the decimal point, `bc`
4850*5a6e8488SAndroid Build Coastguard Workermerely generates an arbitrary precision integer with the bound `10^p`, where `p`
4851*5a6e8488SAndroid Build Coastguard Workeris the desired number of decimal places, then divides in by `10^p` with a
4852*5a6e8488SAndroid Build Coastguard Worker`scale` of `p`.
4853*5a6e8488SAndroid Build Coastguard Worker
4854*5a6e8488SAndroid Build Coastguard Worker## Debug Code
4855*5a6e8488SAndroid Build Coastguard Worker
4856*5a6e8488SAndroid Build Coastguard WorkerBesides building `bc` in debug mode with the `-g` flag to [`configure.sh`][69],
4857*5a6e8488SAndroid Build Coastguard Workerprogrammers can also add `-DBC_DEBUG_CODE=1` to the `CFLAGS`. This will enable
4858*5a6e8488SAndroid Build Coastguard Workerthe inclusion of *a lot* of extra code to assist with debugging.
4859*5a6e8488SAndroid Build Coastguard Worker
4860*5a6e8488SAndroid Build Coastguard WorkerFor more information, see all of the code guarded by `#if BC_DEBUG_CODE` in the
4861*5a6e8488SAndroid Build Coastguard Worker[`include/`][212] directory and in the [`src/`][213] directory.
4862*5a6e8488SAndroid Build Coastguard Worker
4863*5a6e8488SAndroid Build Coastguard WorkerYes, all of the code is guarded by `#if` preprocessor statements; this is
4864*5a6e8488SAndroid Build Coastguard Workerbecause the code should *never* be in a release build, and by making programmers
4865*5a6e8488SAndroid Build Coastguard Workeradd this manually (not even an option to [`configure.sh`][69]), it is easier to
4866*5a6e8488SAndroid Build Coastguard Workerensure that never happens.
4867*5a6e8488SAndroid Build Coastguard Worker
4868*5a6e8488SAndroid Build Coastguard WorkerHowever, that said, the extra debug code is useful; that was why I kept it in.
4869*5a6e8488SAndroid Build Coastguard Worker
4870*5a6e8488SAndroid Build Coastguard Worker## Performance
4871*5a6e8488SAndroid Build Coastguard Worker
4872*5a6e8488SAndroid Build Coastguard WorkerWhile I have put in a lot of effort to make `bc` as fast as possible, there
4873*5a6e8488SAndroid Build Coastguard Workermight be some things users can do to speed it up without changing the code.
4874*5a6e8488SAndroid Build Coastguard Worker
4875*5a6e8488SAndroid Build Coastguard WorkerFirst, users can probably use [profile-guided optimization][217] to optimize
4876*5a6e8488SAndroid Build Coastguard Workereven better, using the test suite to profile.
4877*5a6e8488SAndroid Build Coastguard Worker
4878*5a6e8488SAndroid Build Coastguard WorkerSecond, I included macros that might help branch placement and prediction:
4879*5a6e8488SAndroid Build Coastguard Worker
4880*5a6e8488SAndroid Build Coastguard Worker* `BC_ERR(e)`
4881*5a6e8488SAndroid Build Coastguard Worker* `BC_UNLIKELY(e)`
4882*5a6e8488SAndroid Build Coastguard Worker* `BC_NO_ERR(e)`
4883*5a6e8488SAndroid Build Coastguard Worker* `BC_LIKELY(e)`
4884*5a6e8488SAndroid Build Coastguard Worker
4885*5a6e8488SAndroid Build Coastguard Worker`BC_ERR` is the same as `BC_UNLIKELY`, and `BC_NO_ERR` is the same as
4886*5a6e8488SAndroid Build Coastguard Worker`BC_LIKELY`; I just added them to also document branches that lead to error
4887*5a6e8488SAndroid Build Coastguard Workerconditions or *away* from error conditions.
4888*5a6e8488SAndroid Build Coastguard Worker
4889*5a6e8488SAndroid Build Coastguard WorkerAnyway, if `BC_LIKELY` and `BC_UNLIKELY` are not defined during compilation,
4890*5a6e8488SAndroid Build Coastguard Workerthey expand to nothing but the argument they were given.
4891*5a6e8488SAndroid Build Coastguard Worker
4892*5a6e8488SAndroid Build Coastguard WorkerThey can, however, be defined to `__builtin_expect((e), 1)` and
4893*5a6e8488SAndroid Build Coastguard Worker`__builtin_expect((e), 0)`, respectively, on GCC and Clang for better branch
4894*5a6e8488SAndroid Build Coastguard Workerprediction and placement. (For more information about `__builtin_expect()` see
4895*5a6e8488SAndroid Build Coastguard Workerthe [GCC documentation][218].)
4896*5a6e8488SAndroid Build Coastguard Worker
4897*5a6e8488SAndroid Build Coastguard WorkerThere might be other compilers that can take advantage of that, but I don't know
4898*5a6e8488SAndroid Build Coastguard Workeranything about that.
4899*5a6e8488SAndroid Build Coastguard Worker
4900*5a6e8488SAndroid Build Coastguard WorkerAlso, as stated in the [build manual][219], link-time optimization is excellent
4901*5a6e8488SAndroid Build Coastguard Workerat optimizing this `bc`. Use it.
4902*5a6e8488SAndroid Build Coastguard Worker
4903*5a6e8488SAndroid Build Coastguard Worker### Benchmarks
4904*5a6e8488SAndroid Build Coastguard Worker
4905*5a6e8488SAndroid Build Coastguard WorkerTo help programmers improve performance, I have built and assembled
4906*5a6e8488SAndroid Build Coastguard Workerinfrastructure to make benchmarking easy.
4907*5a6e8488SAndroid Build Coastguard Worker
4908*5a6e8488SAndroid Build Coastguard WorkerFirst, in order to easily run benchmarks, I created
4909*5a6e8488SAndroid Build Coastguard Worker[`scripts/benchmark.sh`][220].
4910*5a6e8488SAndroid Build Coastguard Worker
4911*5a6e8488SAndroid Build Coastguard WorkerSecond, I copied and adapted [`ministat.c`][223] [from FreeBSD][221], to make it
4912*5a6e8488SAndroid Build Coastguard Workereasier to judge whether the results are significant or not.
4913*5a6e8488SAndroid Build Coastguard Worker
4914*5a6e8488SAndroid Build Coastguard WorkerThird, I made the `make` clean target `make clean_benchmarks`, to clean
4915*5a6e8488SAndroid Build Coastguard Worker`scripts/ministat` and the generated benchmark files.
4916*5a6e8488SAndroid Build Coastguard Worker
4917*5a6e8488SAndroid Build Coastguard WorkerFourth, I made it so [`scripts/benchmark.sh`][220] outputs the timing and memory
4918*5a6e8488SAndroid Build Coastguard Workerdata in a format that is easy for `scripts/ministat` to digest.
4919*5a6e8488SAndroid Build Coastguard Worker
4920*5a6e8488SAndroid Build Coastguard WorkerTo add a benchmark, add a script in the right directory to generate the
4921*5a6e8488SAndroid Build Coastguard Workerbenchmark. Yes, generate.
4922*5a6e8488SAndroid Build Coastguard Worker
4923*5a6e8488SAndroid Build Coastguard WorkerAll of the benchmarks are generated first, from `.bc` and `.dc` files in the
4924*5a6e8488SAndroid Build Coastguard Worker[`benchmarks/bc/`][91] and [`benchmarks/dc/`][224]. This is so that massive
4925*5a6e8488SAndroid Build Coastguard Workeramounts of data can be generated and then pushed through the calculators.
4926*5a6e8488SAndroid Build Coastguard Worker
4927*5a6e8488SAndroid Build Coastguard WorkerIf you need to benchmark `bc` or `dc` with simple loops, have the generator
4928*5a6e8488SAndroid Build Coastguard Workerfiles simply print the loop code.
4929*5a6e8488SAndroid Build Coastguard Worker
4930*5a6e8488SAndroid Build Coastguard Worker### Caching of Numbers
4931*5a6e8488SAndroid Build Coastguard Worker
4932*5a6e8488SAndroid Build Coastguard WorkerIn order to provide some performance boost, `bc` tries to reuse old `BcNum`'s
4933*5a6e8488SAndroid Build Coastguard Workerthat have the default capacity (`BC_NUM_DEF_SIZE`).
4934*5a6e8488SAndroid Build Coastguard Worker
4935*5a6e8488SAndroid Build Coastguard WorkerIt does this by allowing `bc_num_free()` to put the limb array onto a
4936*5a6e8488SAndroid Build Coastguard Workerstatically-allocated stack (it's just a global array with a set size). Then,
4937*5a6e8488SAndroid Build Coastguard Workerwhen a `BcNum` with the default capacity is needed, `bc_num_init()` asks if any
4938*5a6e8488SAndroid Build Coastguard Workerare available. If the answer is yes, the one on top of the stack is returned.
4939*5a6e8488SAndroid Build Coastguard WorkerOtherwise, `NULL` is returned, and `bc_num_free()` knows it needs to `malloc()`
4940*5a6e8488SAndroid Build Coastguard Workera new limb array.
4941*5a6e8488SAndroid Build Coastguard Worker
4942*5a6e8488SAndroid Build Coastguard WorkerWhen the stack is filled, any numbers that `bc` attempts to put on it are just
4943*5a6e8488SAndroid Build Coastguard Workerfreed.
4944*5a6e8488SAndroid Build Coastguard Worker
4945*5a6e8488SAndroid Build Coastguard WorkerThis setup saved a few percent in my testing for version [3.0.0][32], which is
4946*5a6e8488SAndroid Build Coastguard Workerwhen I added it.
4947*5a6e8488SAndroid Build Coastguard Worker
4948*5a6e8488SAndroid Build Coastguard Worker## `bcl`
4949*5a6e8488SAndroid Build Coastguard Worker
4950*5a6e8488SAndroid Build Coastguard WorkerAt the request of one of my biggest users, I spent the time to make a build mode
4951*5a6e8488SAndroid Build Coastguard Workerwhere the number and math code of `bc` could be wrapped into a library, which I
4952*5a6e8488SAndroid Build Coastguard Workercalled `bcl`.
4953*5a6e8488SAndroid Build Coastguard Worker
4954*5a6e8488SAndroid Build Coastguard WorkerThis mode is exclusive; `bc` and `dc` themselves are *not* built when building
4955*5a6e8488SAndroid Build Coastguard Worker`bcl`.
4956*5a6e8488SAndroid Build Coastguard Worker
4957*5a6e8488SAndroid Build Coastguard WorkerThe only things in the `bc` math code that is not included is:
4958*5a6e8488SAndroid Build Coastguard Worker
4959*5a6e8488SAndroid Build Coastguard Worker* Printing newlines (clients do not care about `bc`'s line lenth restriction).
4960*5a6e8488SAndroid Build Coastguard Worker* `dc`'s stream print.
4961*5a6e8488SAndroid Build Coastguard Worker
4962*5a6e8488SAndroid Build Coastguard WorkerEven the [pseudo-random number generator][179] is included, with extra support
4963*5a6e8488SAndroid Build Coastguard Workerfor generating real numbers with it. (In `bc`, such support is in
4964*5a6e8488SAndroid Build Coastguard Worker[`lib2.bc`][26].)
4965*5a6e8488SAndroid Build Coastguard Worker
4966*5a6e8488SAndroid Build Coastguard Worker### Signal Handling
4967*5a6e8488SAndroid Build Coastguard Worker
4968*5a6e8488SAndroid Build Coastguard Worker`bcl` does not have the capability for signal handling (as of version `6.0.0`).
4969*5a6e8488SAndroid Build Coastguard Worker
4970*5a6e8488SAndroid Build Coastguard Worker### Threads
4971*5a6e8488SAndroid Build Coastguard Worker
4972*5a6e8488SAndroid Build Coastguard WorkerIt is possible to use `bcl` across multiple threads. However, `bcl` must be
4973*5a6e8488SAndroid Build Coastguard Workerinitialized on each thread where it will be used.
4974*5a6e8488SAndroid Build Coastguard Worker
4975*5a6e8488SAndroid Build Coastguard WorkerThis is because `bcl` uses thread-specific data to store the "globals" for each
4976*5a6e8488SAndroid Build Coastguard Workerthread. This is to ensure that threads do not stomp on each other's numbers or
4977*5a6e8488SAndroid Build Coastguard Workerother data structures.
4978*5a6e8488SAndroid Build Coastguard Worker
4979*5a6e8488SAndroid Build Coastguard Worker### Contexts
4980*5a6e8488SAndroid Build Coastguard Worker
4981*5a6e8488SAndroid Build Coastguard WorkerContexts were an idea by the same user that requested `bcl`. They are meant to
4982*5a6e8488SAndroid Build Coastguard Workermake it so multiple clients in one program can keep their data separate from
4983*5a6e8488SAndroid Build Coastguard Workereach other.
4984*5a6e8488SAndroid Build Coastguard Worker
4985*5a6e8488SAndroid Build Coastguard Worker### Numbers
4986*5a6e8488SAndroid Build Coastguard Worker
4987*5a6e8488SAndroid Build Coastguard WorkerNumbers in `bcl` are literally indices into an encapsulated array of numbers,
4988*5a6e8488SAndroid Build Coastguard Workerhidden in the context. These indices are then passed to clients to refer to
4989*5a6e8488SAndroid Build Coastguard Workernumbers later.
4990*5a6e8488SAndroid Build Coastguard Worker
4991*5a6e8488SAndroid Build Coastguard Worker### Operand Consumption
4992*5a6e8488SAndroid Build Coastguard Worker
4993*5a6e8488SAndroid Build Coastguard WorkerMost math functions in `bcl` "consume" their operand arguments; the arguments
4994*5a6e8488SAndroid Build Coastguard Workerare freed, whether or not an error is returned.
4995*5a6e8488SAndroid Build Coastguard Worker
4996*5a6e8488SAndroid Build Coastguard WorkerThis is to make it easy to implement math code, like this:
4997*5a6e8488SAndroid Build Coastguard Worker
4998*5a6e8488SAndroid Build Coastguard Worker```
4999*5a6e8488SAndroid Build Coastguard Workern = bcl_add(bcl_mul(a, b), bcl_div(c, d));
5000*5a6e8488SAndroid Build Coastguard Worker```
5001*5a6e8488SAndroid Build Coastguard Worker
5002*5a6e8488SAndroid Build Coastguard WorkerIf numbers need to be preserved, they can be with `bcl_dup()`:
5003*5a6e8488SAndroid Build Coastguard Worker
5004*5a6e8488SAndroid Build Coastguard Worker```
5005*5a6e8488SAndroid Build Coastguard Workern = bcl_add(bcl_mul(bcl_dup(a), bc_dup(b)), bcl_div(bcl_dup(c), bcl_dup(d)));
5006*5a6e8488SAndroid Build Coastguard Worker```
5007*5a6e8488SAndroid Build Coastguard Worker
5008*5a6e8488SAndroid Build Coastguard Worker### Errors
5009*5a6e8488SAndroid Build Coastguard Worker
5010*5a6e8488SAndroid Build Coastguard WorkerErrors can be encoded in the indices representing numbers, and where necessary,
5011*5a6e8488SAndroid Build Coastguard Workerclients are responsible for checking those errors.
5012*5a6e8488SAndroid Build Coastguard Worker
5013*5a6e8488SAndroid Build Coastguard WorkerThe encoding of errors is this: if an error happens, the value `0-error` is
5014*5a6e8488SAndroid Build Coastguard Workerreturned. To decode, do the exact same thing. Thus, any index above
5015*5a6e8488SAndroid Build Coastguard Worker`0-num_errors` is an error.
5016*5a6e8488SAndroid Build Coastguard Worker
5017*5a6e8488SAndroid Build Coastguard WorkerIf an index that represents an error is passed to a math function, that function
5018*5a6e8488SAndroid Build Coastguard Workerpropagates the error to its result and does not perform the math operation.
5019*5a6e8488SAndroid Build Coastguard Worker
5020*5a6e8488SAndroid Build Coastguard WorkerAll of this is to, once again, make it easy to implement the math code as above.
5021*5a6e8488SAndroid Build Coastguard Worker
5022*5a6e8488SAndroid Build Coastguard WorkerHowever, where possible, errors are returned directly.
5023*5a6e8488SAndroid Build Coastguard Worker
5024*5a6e8488SAndroid Build Coastguard Worker[1]: https://en.wikipedia.org/wiki/Bus_factor
5025*5a6e8488SAndroid Build Coastguard Worker[2]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html#top
5026*5a6e8488SAndroid Build Coastguard Worker[3]: https://en.wikipedia.org/wiki/Dc_(Unix)
5027*5a6e8488SAndroid Build Coastguard Worker[4]: https://en.wikipedia.org/wiki/Reverse_Polish_notation
5028*5a6e8488SAndroid Build Coastguard Worker[5]: ./bc/A.1.md#standard-library
5029*5a6e8488SAndroid Build Coastguard Worker[6]: https://github.com/torvalds/linux/blob/master/kernel/time/timeconst.bc
5030*5a6e8488SAndroid Build Coastguard Worker[7]: ./bc/A.1.md#extended-library
5031*5a6e8488SAndroid Build Coastguard Worker[8]: #libbc-2
5032*5a6e8488SAndroid Build Coastguard Worker[9]: #strgensh
5033*5a6e8488SAndroid Build Coastguard Worker[10]: https://vimeo.com/230142234
5034*5a6e8488SAndroid Build Coastguard Worker[11]: https://gavinhoward.com/2019/12/values-for-yao/
5035*5a6e8488SAndroid Build Coastguard Worker[12]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
5036*5a6e8488SAndroid Build Coastguard Worker[13]: ./build.md#cross-compiling
5037*5a6e8488SAndroid Build Coastguard Worker[14]: ./build.md
5038*5a6e8488SAndroid Build Coastguard Worker[15]: #strgenc
5039*5a6e8488SAndroid Build Coastguard Worker[16]: http://landley.net/toybox/about.html
5040*5a6e8488SAndroid Build Coastguard Worker[17]: https://www.busybox.net/
5041*5a6e8488SAndroid Build Coastguard Worker[18]: https://en.wikipedia.org/wiki/Karatsuba_algorithm
5042*5a6e8488SAndroid Build Coastguard Worker[19]: https://clang-analyzer.llvm.org/scan-build.html
5043*5a6e8488SAndroid Build Coastguard Worker[20]: https://www.valgrind.org/
5044*5a6e8488SAndroid Build Coastguard Worker[21]: https://clang.llvm.org/docs/AddressSanitizer.html
5045*5a6e8488SAndroid Build Coastguard Worker[22]: https://gavinhoward.com/2019/11/finishing-software/
5046*5a6e8488SAndroid Build Coastguard Worker[23]: #history
5047*5a6e8488SAndroid Build Coastguard Worker[24]: https://clang.llvm.org/docs/ClangFormat.html
5048*5a6e8488SAndroid Build Coastguard Worker[25]: ./algorithms.md
5049*5a6e8488SAndroid Build Coastguard Worker[26]: #lib2bc
5050*5a6e8488SAndroid Build Coastguard Worker[27]: #vmh
5051*5a6e8488SAndroid Build Coastguard Worker[28]: https://github.com/rain-1/linenoise-mob
5052*5a6e8488SAndroid Build Coastguard Worker[29]: https://github.com/antirez/linenoise
5053*5a6e8488SAndroid Build Coastguard Worker[30]: #bclh
5054*5a6e8488SAndroid Build Coastguard Worker[31]: #argsh
5055*5a6e8488SAndroid Build Coastguard Worker[32]: ../NEWS.md#3-0-0
5056*5a6e8488SAndroid Build Coastguard Worker[33]: ../NEWS.md
5057*5a6e8488SAndroid Build Coastguard Worker[34]: https://github.com/skeeto/optparse
5058*5a6e8488SAndroid Build Coastguard Worker[35]: #opth
5059*5a6e8488SAndroid Build Coastguard Worker[36]: #historyh
5060*5a6e8488SAndroid Build Coastguard Worker[37]: #randh
5061*5a6e8488SAndroid Build Coastguard Worker[38]: #langh
5062*5a6e8488SAndroid Build Coastguard Worker[39]: #numc
5063*5a6e8488SAndroid Build Coastguard Worker[40]: #bcc
5064*5a6e8488SAndroid Build Coastguard Worker[41]: #bc_lexc
5065*5a6e8488SAndroid Build Coastguard Worker[42]: #bc_parsec
5066*5a6e8488SAndroid Build Coastguard Worker[43]: #libraryc
5067*5a6e8488SAndroid Build Coastguard Worker[44]: #dcc
5068*5a6e8488SAndroid Build Coastguard Worker[45]: #dc_lexc
5069*5a6e8488SAndroid Build Coastguard Worker[46]: #dc_parsec
5070*5a6e8488SAndroid Build Coastguard Worker[47]: #filec
5071*5a6e8488SAndroid Build Coastguard Worker[48]: #historyc
5072*5a6e8488SAndroid Build Coastguard Worker[49]: #langc
5073*5a6e8488SAndroid Build Coastguard Worker[50]: #lexc
5074*5a6e8488SAndroid Build Coastguard Worker[51]: #optc
5075*5a6e8488SAndroid Build Coastguard Worker[52]: #parsec
5076*5a6e8488SAndroid Build Coastguard Worker[53]: #programc
5077*5a6e8488SAndroid Build Coastguard Worker[54]: #randc
5078*5a6e8488SAndroid Build Coastguard Worker[55]: #fileh
5079*5a6e8488SAndroid Build Coastguard Worker[56]: #readc
5080*5a6e8488SAndroid Build Coastguard Worker[57]: #programh
5081*5a6e8488SAndroid Build Coastguard Worker[58]: #vmc
5082*5a6e8488SAndroid Build Coastguard Worker[59]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/gencat.html#top
5083*5a6e8488SAndroid Build Coastguard Worker[60]: #manpagesh
5084*5a6e8488SAndroid Build Coastguard Worker[61]: #bcl3md
5085*5a6e8488SAndroid Build Coastguard Worker[62]: #bcl3
5086*5a6e8488SAndroid Build Coastguard Worker[63]: #bclvcxproj
5087*5a6e8488SAndroid Build Coastguard Worker[64]: #bclvcxprojfilters
5088*5a6e8488SAndroid Build Coastguard Worker[65]: #bclsln
5089*5a6e8488SAndroid Build Coastguard Worker[66]: #bcvcxproj
5090*5a6e8488SAndroid Build Coastguard Worker[67]: #bcvcxprojfilters
5091*5a6e8488SAndroid Build Coastguard Worker[68]: #bcsln
5092*5a6e8488SAndroid Build Coastguard Worker[69]: #configuresh
5093*5a6e8488SAndroid Build Coastguard Worker[70]: #makefilein
5094*5a6e8488SAndroid Build Coastguard Worker[71]: #functionsh
5095*5a6e8488SAndroid Build Coastguard Worker[72]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html#top
5096*5a6e8488SAndroid Build Coastguard Worker[73]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18
5097*5a6e8488SAndroid Build Coastguard Worker[74]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html#top
5098*5a6e8488SAndroid Build Coastguard Worker[75]: #versionh
5099*5a6e8488SAndroid Build Coastguard Worker[76]: ##posix-shell-scripts
5100*5a6e8488SAndroid Build Coastguard Worker[77]: #tests
5101*5a6e8488SAndroid Build Coastguard Worker[78]: #karatsubapy
5102*5a6e8488SAndroid Build Coastguard Worker[79]: #bc-1
5103*5a6e8488SAndroid Build Coastguard Worker[80]: #dc-1
5104*5a6e8488SAndroid Build Coastguard Worker[81]: ./build.md#build-type
5105*5a6e8488SAndroid Build Coastguard Worker[82]: #fuzzing-1
5106*5a6e8488SAndroid Build Coastguard Worker[83]: #releasesh
5107*5a6e8488SAndroid Build Coastguard Worker[84]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_02
5108*5a6e8488SAndroid Build Coastguard Worker[85]: #locales-1
5109*5a6e8488SAndroid Build Coastguard Worker[86]: #manuals-1
5110*5a6e8488SAndroid Build Coastguard Worker[87]: #locale_installsh
5111*5a6e8488SAndroid Build Coastguard Worker[88]: #locale_uninstallsh
5112*5a6e8488SAndroid Build Coastguard Worker[89]: #bc1mdin
5113*5a6e8488SAndroid Build Coastguard Worker[90]: #dc1mdin
5114*5a6e8488SAndroid Build Coastguard Worker[91]: #bc
5115*5a6e8488SAndroid Build Coastguard Worker[92]: https://pandoc.org/
5116*5a6e8488SAndroid Build Coastguard Worker[93]: #release_settingstxt
5117*5a6e8488SAndroid Build Coastguard Worker[94]: #aflpy
5118*5a6e8488SAndroid Build Coastguard Worker[95]: #randmathpy
5119*5a6e8488SAndroid Build Coastguard Worker[96]: #caching-of-numbers
5120*5a6e8488SAndroid Build Coastguard Worker[97]: #error-handling
5121*5a6e8488SAndroid Build Coastguard Worker[98]: #radamsatxt
5122*5a6e8488SAndroid Build Coastguard Worker[99]: https://gitlab.com/akihe/radamsa
5123*5a6e8488SAndroid Build Coastguard Worker[100]: #radamsash
5124*5a6e8488SAndroid Build Coastguard Worker[101]: https://musl.libc.org/
5125*5a6e8488SAndroid Build Coastguard Worker[102]: ./build.md#settings
5126*5a6e8488SAndroid Build Coastguard Worker[103]: #test_settingstxt
5127*5a6e8488SAndroid Build Coastguard Worker[104]: #test_settingssh
5128*5a6e8488SAndroid Build Coastguard Worker[105]: #functionssh
5129*5a6e8488SAndroid Build Coastguard Worker[106]: #bch
5130*5a6e8488SAndroid Build Coastguard Worker[107]: https://en.wikipedia.org/wiki/Infix_notation
5131*5a6e8488SAndroid Build Coastguard Worker[108]: https://en.wikipedia.org/wiki/Reverse_Polish_notation
5132*5a6e8488SAndroid Build Coastguard Worker[109]: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
5133*5a6e8488SAndroid Build Coastguard Worker[110]: #bc-parsing
5134*5a6e8488SAndroid Build Coastguard Worker[111]: #vectors
5135*5a6e8488SAndroid Build Coastguard Worker[112]: https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/vector.h
5136*5a6e8488SAndroid Build Coastguard Worker[113]: https://en.wikipedia.org/wiki/Signal_(IPC)
5137*5a6e8488SAndroid Build Coastguard Worker[114]: #custom-io
5138*5a6e8488SAndroid Build Coastguard Worker[115]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03_03
5139*5a6e8488SAndroid Build Coastguard Worker[116]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/setjmp.html
5140*5a6e8488SAndroid Build Coastguard Worker[117]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/longjmp.html
5141*5a6e8488SAndroid Build Coastguard Worker[118]: https://www.youtube.com/watch?v=4PaWFYm0kEw
5142*5a6e8488SAndroid Build Coastguard Worker[119]: #fuzz_prepsh
5143*5a6e8488SAndroid Build Coastguard Worker[120]: #bc_aflyaml
5144*5a6e8488SAndroid Build Coastguard Worker[121]: #bc_afl_continueyaml
5145*5a6e8488SAndroid Build Coastguard Worker[122]: https://github.com/tmux/tmux
5146*5a6e8488SAndroid Build Coastguard Worker[123]: https://tmuxp.git-pull.com/
5147*5a6e8488SAndroid Build Coastguard Worker[124]: #test-suite
5148*5a6e8488SAndroid Build Coastguard Worker[125]: https://aflplus.plus/
5149*5a6e8488SAndroid Build Coastguard Worker[126]: #link-time-optimization
5150*5a6e8488SAndroid Build Coastguard Worker[127]: #fuzzing-performance
5151*5a6e8488SAndroid Build Coastguard Worker[128]: #radamsa
5152*5a6e8488SAndroid Build Coastguard Worker[129]: #afl-quickstart
5153*5a6e8488SAndroid Build Coastguard Worker[130]: #convenience
5154*5a6e8488SAndroid Build Coastguard Worker[131]: #datac
5155*5a6e8488SAndroid Build Coastguard Worker[132]: https://git.gavinhoward.com/gavin/vim-bc
5156*5a6e8488SAndroid Build Coastguard Worker[133]: https://git.gavinhoward.com/gavin/bc_libs
5157*5a6e8488SAndroid Build Coastguard Worker[134]: #debugging
5158*5a6e8488SAndroid Build Coastguard Worker[135]: #asserts
5159*5a6e8488SAndroid Build Coastguard Worker[136]: #portability
5160*5a6e8488SAndroid Build Coastguard Worker[137]: https://pexpect.readthedocs.io/en/stable/
5161*5a6e8488SAndroid Build Coastguard Worker[138]: #test-suite-portability
5162*5a6e8488SAndroid Build Coastguard Worker[139]: #historypy
5163*5a6e8488SAndroid Build Coastguard Worker[140]: #historysh
5164*5a6e8488SAndroid Build Coastguard Worker[141]: #group-tests
5165*5a6e8488SAndroid Build Coastguard Worker[142]: #build-system
5166*5a6e8488SAndroid Build Coastguard Worker[143]: #generated-tests
5167*5a6e8488SAndroid Build Coastguard Worker[144]: #benchmarks-1
5168*5a6e8488SAndroid Build Coastguard Worker[145]: #gen
5169*5a6e8488SAndroid Build Coastguard Worker[146]: #test-coverage
5170*5a6e8488SAndroid Build Coastguard Worker[147]: #integration-with-the-build-system
5171*5a6e8488SAndroid Build Coastguard Worker[148]: #test-scripts
5172*5a6e8488SAndroid Build Coastguard Worker[149]: #standard-tests
5173*5a6e8488SAndroid Build Coastguard Worker[150]: #script-tests
5174*5a6e8488SAndroid Build Coastguard Worker[151]: #error-tests
5175*5a6e8488SAndroid Build Coastguard Worker[152]: #stdin-tests
5176*5a6e8488SAndroid Build Coastguard Worker[153]: #read-tests
5177*5a6e8488SAndroid Build Coastguard Worker[154]: #other-tests
5178*5a6e8488SAndroid Build Coastguard Worker[155]: #history-tests
5179*5a6e8488SAndroid Build Coastguard Worker[156]: #bcl
5180*5a6e8488SAndroid Build Coastguard Worker[157]: #bcl-test
5181*5a6e8488SAndroid Build Coastguard Worker[158]: #bclc
5182*5a6e8488SAndroid Build Coastguard Worker[159]: #valgrind
5183*5a6e8488SAndroid Build Coastguard Worker[160]: #addresssanitizer-and-friends
5184*5a6e8488SAndroid Build Coastguard Worker[161]: #bc-2
5185*5a6e8488SAndroid Build Coastguard Worker[162]: #dc-2
5186*5a6e8488SAndroid Build Coastguard Worker[163]: #alltxt-1
5187*5a6e8488SAndroid Build Coastguard Worker[164]: #errorstxt
5188*5a6e8488SAndroid Build Coastguard Worker[165]: #posix_errorstxt
5189*5a6e8488SAndroid Build Coastguard Worker[166]: #timeconstsh
5190*5a6e8488SAndroid Build Coastguard Worker[167]: #alltxt-3
5191*5a6e8488SAndroid Build Coastguard Worker[168]: #errorstxt-1
5192*5a6e8488SAndroid Build Coastguard Worker[169]: #scripts-1
5193*5a6e8488SAndroid Build Coastguard Worker[170]: #scripts-2
5194*5a6e8488SAndroid Build Coastguard Worker[171]: #alltxt-2
5195*5a6e8488SAndroid Build Coastguard Worker[172]: #alltxt-4
5196*5a6e8488SAndroid Build Coastguard Worker[173]: #async-signal-safe-signal-handling
5197*5a6e8488SAndroid Build Coastguard Worker[174]: #vectorh
5198*5a6e8488SAndroid Build Coastguard Worker[175]: #read_errorstxt
5199*5a6e8488SAndroid Build Coastguard Worker[176]: #statush
5200*5a6e8488SAndroid Build Coastguard Worker[177]: #numbers
5201*5a6e8488SAndroid Build Coastguard Worker[178]: #math-style
5202*5a6e8488SAndroid Build Coastguard Worker[179]: #pseudo-random-number-generator
5203*5a6e8488SAndroid Build Coastguard Worker[180]: #lexh
5204*5a6e8488SAndroid Build Coastguard Worker[181]: #parseh
5205*5a6e8488SAndroid Build Coastguard Worker[182]: #dch
5206*5a6e8488SAndroid Build Coastguard Worker[183]: #libraryh
5207*5a6e8488SAndroid Build Coastguard Worker[184]: #numh
5208*5a6e8488SAndroid Build Coastguard Worker[185]: #readh
5209*5a6e8488SAndroid Build Coastguard Worker[186]: #maps
5210*5a6e8488SAndroid Build Coastguard Worker[187]: #slabs-and-slab-vectors
5211*5a6e8488SAndroid Build Coastguard Worker[188]: ./build.md#extra-math
5212*5a6e8488SAndroid Build Coastguard Worker[189]: #command-line-history
5213*5a6e8488SAndroid Build Coastguard Worker[190]: #scriptsed
5214*5a6e8488SAndroid Build Coastguard Worker[191]: #linux-timeconstbc-script
5215*5a6e8488SAndroid Build Coastguard Worker[192]: #corpuses
5216*5a6e8488SAndroid Build Coastguard Worker[193]: ./build.md#history
5217*5a6e8488SAndroid Build Coastguard Worker[194]: https://www.valgrind.org/docs/manual/mc-manual.html
5218*5a6e8488SAndroid Build Coastguard Worker[195]: #othersh
5219*5a6e8488SAndroid Build Coastguard Worker[196]: https://scan.coverity.com/
5220*5a6e8488SAndroid Build Coastguard Worker[197]: https://clang-analyzer.llvm.org/
5221*5a6e8488SAndroid Build Coastguard Worker[198]: https://unix.stackexchange.com/questions/253349/eintr-is-there-a-rationale-behind-it
5222*5a6e8488SAndroid Build Coastguard Worker[199]: https://cr.yp.to/docs/selfpipe.html
5223*5a6e8488SAndroid Build Coastguard Worker[200]: https://skarnet.org/cgi-bin/archive.cgi?2:mss:1607:201701:dfblejammjllfkggpcph
5224*5a6e8488SAndroid Build Coastguard Worker[201]: https://slembcke.github.io/2020/10/12/CustomAllocators.html#1-slab-allocator
5225*5a6e8488SAndroid Build Coastguard Worker[202]: https://en.wikipedia.org/wiki/Tail_call
5226*5a6e8488SAndroid Build Coastguard Worker[203]: https://en.wikipedia.org/wiki/Functional_programming_language
5227*5a6e8488SAndroid Build Coastguard Worker[204]: https://en.wikipedia.org/wiki/C_dynamic_memory_allocation
5228*5a6e8488SAndroid Build Coastguard Worker[205]: #mainc
5229*5a6e8488SAndroid Build Coastguard Worker[206]: #argc
5230*5a6e8488SAndroid Build Coastguard Worker[207]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
5231*5a6e8488SAndroid Build Coastguard Worker[208]: #functions
5232*5a6e8488SAndroid Build Coastguard Worker[209]: #parsing
5233*5a6e8488SAndroid Build Coastguard Worker[210]: https://en.wikipedia.org/wiki/Stack_machine
5234*5a6e8488SAndroid Build Coastguard Worker[211]: https://en.wikipedia.org/wiki/Register_machine
5235*5a6e8488SAndroid Build Coastguard Worker[212]: #include
5236*5a6e8488SAndroid Build Coastguard Worker[213]: #src
5237*5a6e8488SAndroid Build Coastguard Worker[214]: https://gmplib.org/manual/Nomenclature-and-Types
5238*5a6e8488SAndroid Build Coastguard Worker[215]: https://en.wikipedia.org/wiki/Radix_point
5239*5a6e8488SAndroid Build Coastguard Worker[216]: #main-and-read-functions
5240*5a6e8488SAndroid Build Coastguard Worker[215]: https://www.pcg-random.org/
5241*5a6e8488SAndroid Build Coastguard Worker[216]: https://lemire.me/blog/2017/08/22/testing-non-cryptographic-random-number-generators-my-results/
5242*5a6e8488SAndroid Build Coastguard Worker[217]: https://en.wikipedia.org/wiki/Profile-guided_optimization
5243*5a6e8488SAndroid Build Coastguard Worker[218]: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fexpect
5244*5a6e8488SAndroid Build Coastguard Worker[219]: ./build.md#optimization
5245*5a6e8488SAndroid Build Coastguard Worker[220]: #benchmarksh
5246*5a6e8488SAndroid Build Coastguard Worker[221]: https://cgit.freebsd.org/src/tree/usr.bin/ministat/ministat.c
5247*5a6e8488SAndroid Build Coastguard Worker[222]: https://www.freebsd.org/cgi/man.cgi?query=ministat&apropos=0&sektion=0&manpath=FreeBSD+13.0-RELEASE+and+Ports&arch=default&format=html
5248*5a6e8488SAndroid Build Coastguard Worker[223]: #ministatc
5249*5a6e8488SAndroid Build Coastguard Worker[224]: #dc
5250*5a6e8488SAndroid Build Coastguard Worker[225]: #allsh
5251*5a6e8488SAndroid Build Coastguard Worker[226]: #errorssh
5252*5a6e8488SAndroid Build Coastguard Worker[227]: #errorsh
5253*5a6e8488SAndroid Build Coastguard Worker[228]: #vectorc
5254*5a6e8488SAndroid Build Coastguard Worker[229]: https://github.com/gavinhoward/bc/pull/72
5255