]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/README.md
contrib/bc: update to version 5.2.5
[FreeBSD/FreeBSD.git] / contrib / bc / README.md
1 # `bc`
2
3 ***WARNING: This project has moved to [https://git.yzena.com/][20] for [these
4 reasons][21], though GitHub will remain a mirror.***
5
6 This is an implementation of the [POSIX `bc` calculator][12] that implements
7 [GNU `bc`][1] extensions, as well as the period (`.`) extension for the BSD
8 flavor of `bc`.
9
10 For more information, see this `bc`'s full manual.
11
12 This `bc` also includes an implementation of `dc` in the same binary, accessible
13 via a symbolic link, which implements all FreeBSD and GNU extensions. (If a
14 standalone `dc` binary is desired, `bc` can be copied and renamed to `dc`.) The
15 `!` command is omitted; I believe this poses security concerns and that such
16 functionality is unnecessary.
17
18 For more information, see the `dc`'s full manual.
19
20 This `bc` also provides `bc`'s math as a library with C bindings, called `bcl`.
21
22 For more information, see the full manual for `bcl`.
23
24 ## License
25
26 This `bc` is Free and Open Source Software (FOSS). It is offered under the BSD
27 2-clause License. Full license text may be found in the [`LICENSE.md`][4] file.
28
29 ## Prerequisites
30
31 This `bc` only requires either:
32
33 1.      Windows 10 or later, or
34 2.      A C99-compatible compiler and a (mostly) POSIX 2008-compatible system with
35         the XSI (X/Open System Interfaces) option group.
36
37 Since POSIX 2008 with XSI requires the existence of a C99 compiler as `c99`, any
38 POSIX and XSI-compatible system will have everything needed.
39
40 POSIX-compatible systems that are known to work:
41
42 * Linux
43 * FreeBSD
44 * OpenBSD
45 * NetBSD
46 * Mac OSX
47 * Solaris* (as long as the Solaris version supports POSIX 2008)
48 * AIX
49 * HP-UX* (except for history)
50
51 In addition, there is compatibility code to make this `bc` work on Windows.
52
53 Please submit bug reports if this `bc` does not build out of the box on any
54 system.
55
56 ## Build
57
58 This `bc` should build unmodified on any POSIX-compliant system or on Windows
59 starting with Windows 10 (though earlier versions may work).
60
61 For more complex build requirements than the ones below, see the [build
62 manual][5].
63
64 ### Windows
65
66 There is no guarantee that this `bc` will work on any version of Windows earlier
67 than Windows 10 (I cannot test on earlier versions), but it is guaranteed to
68 work on Windows 10 at least.
69
70 Also, if building with MSBuild, the MSBuild bundled with Visual Studio is
71 required.
72
73 **Note**: Unlike the POSIX-compatible platforms, only one build configuration is
74 supported on Windows: extra math and prompt enabled, history and NLS (locale
75 support) disabled, with both calculators built.
76
77 #### `bc`
78
79 To build `bc`, you can open the `vs/bc.sln` file in Visual Studio, select the
80 configuration, and build.
81
82 You can also build using MSBuild with the following from the root directory:
83
84 ```
85 msbuild -property:Configuration=<config> vs/bc.sln
86 ```
87
88 where `<config>` is either one of `Debug` or `Release`.
89
90 On Windows, the calculators are built as `vs/bin/<platform>/<config>/bc.exe` and
91 `vs/bin/<Platform>/<Config>/dc.exe`, where `<platform>` can be either `Win32` or
92 `x64`, and `<config>` can be `Debug` or `Release`.
93
94 **Note**: On Windows, `dc.exe` is just copied from `bc.exe`; it is not linked.
95 Patches are welcome for a way to do that.
96
97 #### `bcl` (Library)
98
99 To build the library, you can open the `vs/bcl.sln` file in Visual Studio,
100 select the configuration, and build.
101
102 You can also build using MSBuild with the following from the root directory:
103
104 ```
105 msbuild -property:Configuration=<config> vs/bcl.sln
106 ```
107
108 where `<config>` is either one of `Debug`, `ReleaseMD`, or `ReleaseMT`.
109
110 On Windows, the library is built as `vs/lib/<platform>/<config>/bcl.lib`, where
111 `<platform>` can be either `Win32` or `x64`, and `<config>` can be `Debug`,
112 `ReleaseMD`, or `ReleaseMT`.
113
114 ### POSIX-Compatible Systems
115
116 On POSIX-compatible systems, `bc` is built as `bin/bc` and `dc` is built as
117 `bin/dc` by default.
118
119 #### Default
120
121 For the default build with optimization, use the following commands in the root
122 directory:
123
124 ```
125 ./configure.sh -O3
126 make
127 ```
128
129 #### One Calculator
130
131 To only build `bc`, use the following commands:
132
133 ```
134 ./configure.sh --disable-dc
135 make
136 ```
137
138 To only build `dc`, use the following commands:
139
140 ```
141 ./configure.sh --disable-bc
142 make
143 ```
144
145 #### Debug
146
147 For debug builds, use the following commands in the root directory:
148
149 ```
150 ./configure.sh -g
151 make
152 ```
153
154 #### Install
155
156 To install, use the following command:
157
158 ```
159 make install
160 ```
161
162 By default, `bc` and `dc` will be installed in `/usr/local`. For installing in
163 other locations, use the `PREFIX` environment variable when running
164 `configure.sh` or pass the `--prefix=<prefix>` option to `configure.sh`. See the
165 [build manual][5], or run `./configure.sh --help`, for more details.
166
167 #### Library
168
169 This `bc` does provide a way to build a math library with C bindings. This is
170 done by the `-a` or `--library` options to `configure.sh`:
171
172 ```
173 ./configure.sh -a
174 ```
175
176 When building the library, the executables are not built. For more information,
177 see the [build manual][5].
178
179 The library API can be found in [`manuals/bcl.3.md`][26] or `man bcl` once the
180 library is installed.
181
182 The library is built as `bin/libbcl.a` on POSIX-compatible systems or as
183 `Release/bcl/bcl.lib` on Windows.
184
185 #### Package and Distro Maintainers
186
187 This section is for package and distro maintainers.
188
189 ##### Out-of-Source Builds
190
191 Out-of-source builds are supported; just call `configure.sh` from the directory
192 where the actual build will happen.
193
194 For example, if the source is in `bc`, the build should happen in `build`, then
195 call `configure.sh` and `make` like so:
196
197 ```
198 ../bc/configure.sh
199 make
200 ```
201
202 ***WARNING***: The path to `configure.sh` from the build directory must not have
203 spaces because `make` does not support target names with spaces.
204
205 ##### Recommended Compiler
206
207 When I ran benchmarks with my `bc` compiled under `clang`, it performed much
208 better than when compiled under `gcc`. I recommend compiling this `bc` with
209 `clang`.
210
211 I also recommend building this `bc` with C11 if you can because `bc` will detect
212 a C11 compiler and add `_Noreturn` to any relevant function(s).
213
214 ##### Recommended Optimizations
215
216 I wrote this `bc` with Separation of Concerns, which means that there are many
217 small functions that could be inlined. However, they are often called across
218 file boundaries, and the default optimizer can only look at the current file,
219 which means that they are not inlined.
220
221 Thus, because of the way this `bc` is built, it will automatically be slower
222 than other `bc` implementations when running scripts with no math. (My `bc`'s
223 math is *much* faster, so any non-trivial script should run faster in my `bc`.)
224
225 Some, or all, of the difference can be made up with the right optimizations. The
226 optimizations I recommend are:
227
228 1.      `-O3`
229 2.      `-flto` (link-time optimization)
230
231 in that order.
232
233 Link-time optimization, in particular, speeds up the `bc` a lot. This is because
234 when link-time optimization is turned on, the optimizer can look across files
235 and inline *much* more heavily.
236
237 However, I recommend ***NOT*** using `-march=native`. Doing so will reduce this
238 `bc`'s performance, at least when building with link-time optimization. See the
239 [benchmarks][19] for more details.
240
241 ##### Stripping Binaries
242
243 By default, non-debug binaries are stripped, but stripping can be disabled with
244 the `-T` option to `configure.sh`.
245
246 ##### Using This `bc` as an Alternative
247
248 If this `bc` is packaged as an alternative to an already existing `bc` package,
249 it is possible to rename it in the build to prevent name collision. To prepend
250 to the name, just run the following:
251
252 ```
253 EXECPREFIX=<some_prefix> ./configure.sh
254 ```
255
256 To append to the name, just run the following:
257
258 ```
259 EXECSUFFIX=<some_suffix> ./configure.sh
260 ```
261
262 If a package maintainer wishes to add both a prefix and a suffix, that is
263 allowed.
264
265 **Note**: The suggested name (and package name) when `bc` is not available is
266 `bc-gh`.
267
268 ##### Karatsuba Number
269
270 Package and distro maintainers have one tool at their disposal to build this
271 `bc` in the optimal configuration: `scripts/karatsuba.py`.
272
273 This script is not a compile-time or runtime prerequisite; it is for package and
274 distro maintainers to run once when a package is being created. It finds the
275 optimal Karatsuba number (see the [algorithms manual][7] for more information)
276 for the machine that it is running on.
277
278 The easiest way to run this script is with `make karatsuba`.
279
280 If desired, maintainers can also skip running this script because there is a
281 sane default for the Karatsuba number.
282
283 ## Status
284
285 This `bc` is robust.
286
287 It is well-tested, fuzzed, and fully standards-compliant (though not certified)
288 with POSIX `bc`. The math has been tested with 40+ million random problems, so
289 it is as correct as I can make it.
290
291 This `bc` can be used as a drop-in replacement for any existing `bc`. This `bc`
292 is also compatible with MinGW toolchains, though history is not supported on
293 Windows.
294
295 In addition, this `bc` is considered complete; i.e., there will be no more
296 releases with additional features. However, it *is* actively maintained, so if
297 any bugs are found, they will be fixed in new releases. Also, additional
298 translations will also be added as they are provided.
299
300 ### Development
301
302 If I (Gavin D. Howard) get [hit by a bus][27] and future programmers need to
303 handle work themselves, the best place to start is the [Development manual][28].
304
305 ## Vim Syntax
306
307 I have developed (using other people's code to start) [`vim` syntax files][17]
308 for this `bc` and `dc`, including the extensions.
309
310 ## `bc` Libs
311
312 I have gathered some excellent [`bc` and `dc` libraries][18]. These libraries
313 may prove useful to any serious users.
314
315 ## Comparison to GNU `bc`
316
317 This `bc` compares favorably to GNU `bc`.
318
319 * This `bc` builds natively on Windows.
320 * It has more extensions, which make this `bc` more useful for scripting.
321 * This `bc` is a bit more POSIX compliant.
322 * It has a much less buggy parser. The GNU `bc` will give parse errors for what
323   is actually valid `bc` code, or should be. For example, putting an `else` on
324   a new line after a brace can cause GNU `bc` to give a parse error.
325 * This `bc` has fewer crashes.
326 * GNU `bc` calculates the wrong number of significant digits for `length(x)`.
327 * GNU `bc` will sometimes print numbers incorrectly. For example, when running
328   it on the file `tests/bc/power.txt` in this repo, GNU `bc` gets all the right
329   answers, but it fails to wrap the numbers at the proper place when outputting
330   to a file.
331 * This `bc` is faster. (See [Performance](#performance).)
332
333 ### Performance
334
335 Because this `bc` packs more than `1` decimal digit per hardware integer, this
336 `bc` is faster than GNU `bc` and can be *much* faster. Full benchmarks can be
337 found at [manuals/benchmarks.md][19].
338
339 There is one instance where this `bc` is slower: if scripts are light on math.
340 This is because this `bc`'s intepreter is slightly slower than GNU `bc`, but
341 that is because it is more robust. See the [benchmarks][19].
342
343 ## Algorithms
344
345 To see what algorithms this `bc` uses, see the [algorithms manual][7].
346
347 ## Locales
348
349 Currently, there is no locale support on Windows.
350
351 Additionally, this `bc` only has support for English (and US English), French,
352 German, Portuguese, Dutch, Polish, Russian, Japanese, and Chinese locales.
353 Patches are welcome for translations; use the existing `*.msg` files in
354 `locales/` as a starting point.
355
356 In addition, patches for improvements are welcome; the last two messages in
357 Portuguese were made with Google Translate, and the Dutch, Polish, Russian,
358 Japanese, and Chinese locales were all generated with [DeepL][22].
359
360 The message files provided assume that locales apply to all regions where a
361 language is used, but this might not be true for, e.g., `fr_CA` and `fr_CH`.
362 Any corrections or a confirmation that the current texts are acceptable for
363 those regions would be appreciated, too.
364
365 ## Other Projects
366
367 Other projects based on this bc are:
368
369 * [busybox `bc`][8]. The busybox maintainers have made their own changes, so any
370   bugs in the busybox `bc` should be reported to them.
371
372 * [toybox `bc`][9]. The maintainer has also made his own changes, so bugs in the
373   toybox `bc` should be reported there.
374
375 * [FreeBSD `bc`][23]. While the `bc` in FreeBSD is kept up-to-date, it is better
376   to [report bugs there][24], as well as [submit patches][25], and the
377   maintainers of the package will contact me if necessary.
378
379 ## Language
380
381 This `bc` is written in pure ISO C99, using POSIX 2008 APIs with custom Windows
382 compatibility code.
383
384 ## Commit Messages
385
386 This `bc` uses the commit message guidelines laid out in [this blog post][10].
387
388 ## Semantic Versioning
389
390 This `bc` uses [semantic versioning][11].
391
392 ## Contents
393
394 Items labeled with `(maintainer use only)` are not included in release source
395 tarballs.
396
397 Files:
398
399         .gitignore           The git ignore file (maintainer use only).
400         .gitattributes       The git attributes file (maintainer use only).
401         bcl.pc.in            A template pkg-config file for bcl.
402         configure            A symlink to configure.sh to make packaging easier.
403         configure.sh         The configure script.
404         LICENSE.md           A Markdown form of the BSD 2-clause License.
405         Makefile.in          The Makefile template.
406         NEWS.md              The changelog.
407         NOTICE.md            List of contributors and copyright owners.
408         RELEASE.md           A checklist for making a release (maintainer use only).
409
410 Folders:
411
412         gen      The bc math library, help texts, and code to generate C source.
413         include  All header files.
414         locales  Locale files, in .msg format. Patches welcome for translations.
415         manuals  Manuals for both programs.
416         src      All source code.
417         scripts  A bunch of shell scripts to help with development and building.
418         tests    All tests.
419         vs       Files needed for the build on Windows.
420
421 [1]: https://www.gnu.org/software/bc/
422 [4]: ./LICENSE.md
423 [5]: ./manuals/build.md
424 [7]: ./manuals/algorithms.md
425 [8]: https://git.busybox.net/busybox/tree/miscutils/bc.c
426 [9]: https://github.com/landley/toybox/blob/master/toys/pending/bc.c
427 [10]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
428 [11]: http://semver.org/
429 [12]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html
430 [17]: https://git.yzena.com/gavin/vim-bc
431 [18]: https://git.yzena.com/gavin/bc_libs
432 [19]: ./manuals/benchmarks.md
433 [20]: https://git.yzena.com/gavin/bc
434 [21]: https://gavinhoward.com/2020/04/i-am-moving-away-from-github/
435 [22]: https://www.deepl.com/translator
436 [23]: https://cgit.freebsd.org/src/tree/contrib/bc
437 [24]: https://bugs.freebsd.org/
438 [25]: https://reviews.freebsd.org/
439 [26]: ./manuals/bcl.3.md
440 [27]: https://en.wikipedia.org/wiki/Bus_factor
441 [28]: ./manuals/development.md