]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/manuals/bc/EHN.1
libarchive: merge from vendor branch
[FreeBSD/FreeBSD.git] / contrib / bc / manuals / bc / EHN.1
1 .\"
2 .\" SPDX-License-Identifier: BSD-2-Clause
3 .\"
4 .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions are met:
8 .\"
9 .\" * Redistributions of source code must retain the above copyright notice,
10 .\"   this list of conditions and the following disclaimer.
11 .\"
12 .\" * Redistributions in binary form must reproduce the above copyright notice,
13 .\"   this list of conditions and the following disclaimer in the documentation
14 .\"   and/or other materials provided with the distribution.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 .\" POSSIBILITY OF SUCH DAMAGE.
27 .\"
28 .TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual"
29 .nh
30 .ad l
31 .SH NAME
32 .PP
33 bc - arbitrary-precision decimal arithmetic language and calculator
34 .SH SYNOPSIS
35 .PP
36 \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]]
37 [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]]
38 [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]]
39 [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]]
40 [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]]
41 [\f[B]-e\f[R] \f[I]expr\f[R]]
42 [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...]
43 [\f[B]-f\f[R] \f[I]file\f[R]\&...]
44 [\f[B]--file\f[R]=\f[I]file\f[R]\&...]
45 [\f[I]file\f[R]\&...]
46 .SH DESCRIPTION
47 .PP
48 bc(1) is an interactive processor for a language first standardized in
49 1991 by POSIX.
50 (See the \f[B]STANDARDS\f[R] section.)
51 The language provides unlimited precision decimal arithmetic and is
52 somewhat C-like, but there are differences.
53 Such differences will be noted in this document.
54 .PP
55 After parsing and handling options, this bc(1) reads any files given on
56 the command line and executes them before reading from \f[B]stdin\f[R].
57 .PP
58 This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including
59 (and especially) the GNU bc(1).
60 It also has many extensions and extra features beyond other
61 implementations.
62 .PP
63 \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for
64 another bc(1) gives a parse error, it is probably because a word this
65 bc(1) reserves as a keyword is used as the name of a function, variable,
66 or array.
67 To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R],
68 where \f[I]keyword\f[R] is the keyword that is used as a name in the
69 script.
70 For more information, see the \f[B]OPTIONS\f[R] section.
71 .PP
72 If parsing scripts meant for other bc(1) implementations still does not
73 work, that is a bug and should be reported.
74 See the \f[B]BUGS\f[R] section.
75 .SH OPTIONS
76 .PP
77 The following are the options that bc(1) accepts.
78 .TP
79 \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R]
80 Disables clamping of digits greater than or equal to the current
81 \f[B]ibase\f[R] when parsing numbers.
82 .RS
83 .PP
84 This means that the value added to a number from a digit is always that
85 digit\[cq]s value multiplied by the value of ibase raised to the power
86 of the digit\[cq]s position, which starts from 0 at the least
87 significant digit.
88 .PP
89 If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are
90 given multiple times, the last one given is used.
91 .PP
92 This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable
93 (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which
94 can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
95 .PP
96 This is a \f[B]non-portable extension\f[R].
97 .RE
98 .TP
99 \f[B]-c\f[R], \f[B]--digit-clamp\f[R]
100 Enables clamping of digits greater than or equal to the current
101 \f[B]ibase\f[R] when parsing numbers.
102 .RS
103 .PP
104 This means that digits that the value added to a number from a digit
105 that is greater than or equal to the ibase is the value of ibase minus 1
106 all multiplied by the value of ibase raised to the power of the
107 digit\[cq]s position, which starts from 0 at the least significant
108 digit.
109 .PP
110 If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options
111 are given multiple times, the last one given is used.
112 .PP
113 This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable
114 (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which
115 can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
116 .PP
117 This is a \f[B]non-portable extension\f[R].
118 .RE
119 .TP
120 \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R]
121 Evaluates \f[I]expr\f[R].
122 If multiple expressions are given, they are evaluated in order.
123 If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R]
124 options), the expressions and files are evaluated in the order given.
125 This means that if a file is given before an expression, the file is
126 read in and evaluated first.
127 .RS
128 .PP
129 If this option is given on the command-line (i.e., not in
130 \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section),
131 then after processing all expressions and files, bc(1) will exit, unless
132 \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to
133 \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in
134 \f[B]BC_ENV_ARGS\f[R].
135 However, if any other \f[B]-e\f[R], \f[B]--expression\f[R],
136 \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after
137 \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and
138 exit.
139 .PP
140 This is a \f[B]non-portable extension\f[R].
141 .RE
142 .TP
143 \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R]
144 Reads in \f[I]file\f[R] and evaluates it, line by line, as though it
145 were read through \f[B]stdin\f[R].
146 If expressions are also given (see the \f[B]-e\f[R] and
147 \f[B]--expression\f[R] options), the expressions are evaluated in the
148 order given.
149 .RS
150 .PP
151 If this option is given on the command-line (i.e., not in
152 \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section),
153 then after processing all expressions and files, bc(1) will exit, unless
154 \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to
155 \f[B]-f\f[R] or \f[B]--file\f[R].
156 However, if any other \f[B]-e\f[R], \f[B]--expression\f[R],
157 \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after
158 \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and
159 exit.
160 .PP
161 This is a \f[B]non-portable extension\f[R].
162 .RE
163 .TP
164 \f[B]-g\f[R], \f[B]--global-stacks\f[R]
165 Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R]
166 into stacks.
167 .RS
168 .PP
169 This has the effect that a copy of the current value of all three are
170 pushed onto a stack for every function call, as well as popped when
171 every function returns.
172 This means that functions can assign to any and all of those globals
173 without worrying that the change will affect other functions.
174 Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply
175 printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this:
176 .IP
177 .nf
178 \f[C]
179 define void output(x, b) {
180     obase=b
181     x
182 }
183 \f[R]
184 .fi
185 .PP
186 instead of like this:
187 .IP
188 .nf
189 \f[C]
190 define void output(x, b) {
191     auto c
192     c=obase
193     obase=b
194     x
195     obase=c
196 }
197 \f[R]
198 .fi
199 .PP
200 This makes writing functions much easier.
201 .PP
202 However, since using this flag means that functions cannot set
203 \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally, functions
204 that are made to do so cannot work anymore.
205 There are two possible use cases for that, and each has a solution.
206 .PP
207 First, if a function is called on startup to turn bc(1) into a number
208 converter, it is possible to replace that capability with various shell
209 aliases.
210 Examples:
211 .IP
212 .nf
213 \f[C]
214 alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq]
215 alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq]
216 \f[R]
217 .fi
218 .PP
219 Second, if the purpose of a function is to set \f[B]ibase\f[R],
220 \f[B]obase\f[R], or \f[B]scale\f[R] globally for any other purpose, it
221 could be split into one to three functions (based on how many globals it
222 sets) and each of those functions could return the desired value for a
223 global.
224 .PP
225 If the behavior of this option is desired for every run of bc(1), then
226 users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this
227 option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more
228 details).
229 .PP
230 If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option
231 is ignored.
232 .PP
233 This is a \f[B]non-portable extension\f[R].
234 .RE
235 .TP
236 \f[B]-h\f[R], \f[B]--help\f[R]
237 Prints a usage message and exits.
238 .TP
239 \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R]
240 Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R]
241 assuming that \f[I]ibase\f[R] is in base 10.
242 It is a fatal error if \f[I]ibase\f[R] is not a valid number.
243 .RS
244 .PP
245 If multiple instances of this option are given, the last is used.
246 .PP
247 This is a \f[B]non-portable extension\f[R].
248 .RE
249 .TP
250 \f[B]-i\f[R], \f[B]--interactive\f[R]
251 Forces interactive mode.
252 (See the \f[B]INTERACTIVE MODE\f[R] section.)
253 .RS
254 .PP
255 This is a \f[B]non-portable extension\f[R].
256 .RE
257 .TP
258 \f[B]-L\f[R], \f[B]--no-line-length\f[R]
259 Disables line length checking and prints numbers without backslashes and
260 newlines.
261 In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R]
262 (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
263 .RS
264 .PP
265 This is a \f[B]non-portable extension\f[R].
266 .RE
267 .TP
268 \f[B]-l\f[R], \f[B]--mathlib\f[R]
269 Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R]
270 and loads the included math library before running any code, including
271 any expressions or files specified on the command line.
272 .RS
273 .PP
274 To learn what is in the library, see the \f[B]LIBRARY\f[R] section.
275 .RE
276 .TP
277 \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R]
278 Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R]
279 assuming that \f[I]obase\f[R] is in base 10.
280 It is a fatal error if \f[I]obase\f[R] is not a valid number.
281 .RS
282 .PP
283 If multiple instances of this option are given, the last is used.
284 .PP
285 This is a \f[B]non-portable extension\f[R].
286 .RE
287 .TP
288 \f[B]-P\f[R], \f[B]--no-prompt\f[R]
289 Disables the prompt in TTY mode.
290 (The prompt is only enabled in TTY mode.
291 See the \f[B]TTY MODE\f[R] section.)
292 This is mostly for those users that do not want a prompt or are not used
293 to having them in bc(1).
294 Most of those users would want to put this option in
295 \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
296 .RS
297 .PP
298 These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R]
299 environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
300 .PP
301 This is a \f[B]non-portable extension\f[R].
302 .RE
303 .TP
304 \f[B]-q\f[R], \f[B]--quiet\f[R]
305 This option is for compatibility with the GNU bc(1)
306 (https://www.gnu.org/software/bc/); it is a no-op.
307 Without this option, GNU bc(1) prints a copyright header.
308 This bc(1) only prints the copyright header if one or more of the
309 \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given
310 unless the \f[B]BC_BANNER\f[R] environment variable is set and contains
311 a non-zero integer or if this bc(1) was built with the header displayed
312 by default.
313 If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R]
314 prevent bc(1) from printing the header.
315 .RS
316 .PP
317 This is a \f[B]non-portable extension\f[R].
318 .RE
319 .TP
320 \f[B]-R\f[R], \f[B]--no-read-prompt\f[R]
321 Disables the read prompt in TTY mode.
322 (The read prompt is only enabled in TTY mode.
323 See the \f[B]TTY MODE\f[R] section.)
324 This is mostly for those users that do not want a read prompt or are not
325 used to having them in bc(1).
326 Most of those users would want to put this option in
327 \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
328 This option is also useful in hash bang lines of bc(1) scripts that
329 prompt for user input.
330 .RS
331 .PP
332 This option does not disable the regular prompt because the read prompt
333 is only used when the \f[B]read()\f[R] built-in function is called.
334 .PP
335 These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and
336 \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT
337 VARIABLES\f[R] section), but only for the read prompt.
338 .PP
339 This is a \f[B]non-portable extension\f[R].
340 .RE
341 .TP
342 \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R]
343 Redefines \f[I]keyword\f[R] in order to allow it to be used as a
344 function, variable, or array name.
345 This is useful when this bc(1) gives parse errors when parsing scripts
346 meant for other bc(1) implementations.
347 .RS
348 .PP
349 The keywords this bc(1) allows to be redefined are:
350 .IP \[bu] 2
351 \f[B]abs\f[R]
352 .IP \[bu] 2
353 \f[B]asciify\f[R]
354 .IP \[bu] 2
355 \f[B]continue\f[R]
356 .IP \[bu] 2
357 \f[B]divmod\f[R]
358 .IP \[bu] 2
359 \f[B]else\f[R]
360 .IP \[bu] 2
361 \f[B]halt\f[R]
362 .IP \[bu] 2
363 \f[B]last\f[R]
364 .IP \[bu] 2
365 \f[B]limits\f[R]
366 .IP \[bu] 2
367 \f[B]maxibase\f[R]
368 .IP \[bu] 2
369 \f[B]maxobase\f[R]
370 .IP \[bu] 2
371 \f[B]maxscale\f[R]
372 .IP \[bu] 2
373 \f[B]modexp\f[R]
374 .IP \[bu] 2
375 \f[B]print\f[R]
376 .IP \[bu] 2
377 \f[B]read\f[R]
378 .IP \[bu] 2
379 \f[B]stream\f[R]
380 .PP
381 If any of those keywords are used as a function, variable, or array name
382 in a script, use this option with the keyword as the argument.
383 If multiple are used, use this option for all of them; it can be used
384 multiple times.
385 .PP
386 Keywords are \f[I]not\f[R] redefined when parsing the builtin math
387 library (see the \f[B]LIBRARY\f[R] section).
388 .PP
389 It is a fatal error to redefine keywords mandated by the POSIX standard
390 (see the \f[B]STANDARDS\f[R] section).
391 It is a fatal error to attempt to redefine words that this bc(1) does
392 not reserve as keywords.
393 .RE
394 .TP
395 \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R]
396 Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R]
397 assuming that \f[I]scale\f[R] is in base 10.
398 It is a fatal error if \f[I]scale\f[R] is not a valid number.
399 .RS
400 .PP
401 If multiple instances of this option are given, the last is used.
402 .PP
403 This is a \f[B]non-portable extension\f[R].
404 .RE
405 .TP
406 \f[B]-s\f[R], \f[B]--standard\f[R]
407 Process exactly the language defined by the standard (see the
408 \f[B]STANDARDS\f[R] section) and error if any extensions are used.
409 .RS
410 .PP
411 This is a \f[B]non-portable extension\f[R].
412 .RE
413 .TP
414 \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R]
415 Print the version information (copyright header) and exits.
416 .RS
417 .PP
418 This is a \f[B]non-portable extension\f[R].
419 .RE
420 .TP
421 \f[B]-w\f[R], \f[B]--warn\f[R]
422 Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and
423 not errors) are printed for non-standard extensions and execution
424 continues normally.
425 .RS
426 .PP
427 This is a \f[B]non-portable extension\f[R].
428 .RE
429 .TP
430 \f[B]-z\f[R], \f[B]--leading-zeroes\f[R]
431 Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than
432 \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero.
433 .RS
434 .PP
435 This can be set for individual numbers with the \f[B]plz(x)\f[R],
436 \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions
437 in the extended math library (see the \f[B]LIBRARY\f[R] section).
438 .PP
439 This is a \f[B]non-portable extension\f[R].
440 .RE
441 .PP
442 All long options are \f[B]non-portable extensions\f[R].
443 .SH STDIN
444 .PP
445 If no files or expressions are given by the \f[B]-f\f[R],
446 \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then
447 bc(1) reads from \f[B]stdin\f[R].
448 .PP
449 However, there are a few caveats to this.
450 .PP
451 First, \f[B]stdin\f[R] is evaluated a line at a time.
452 The only exception to this is if the parse cannot complete.
453 That means that starting a string without ending it or starting a
454 function, \f[B]if\f[R] statement, or loop without ending it will also
455 cause bc(1) to not execute.
456 .PP
457 Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an
458 \f[B]else\f[R] statement will follow, so it will not execute until it
459 knows there will not be an \f[B]else\f[R] statement.
460 .SH STDOUT
461 .PP
462 Any non-error output is written to \f[B]stdout\f[R].
463 In addition, if history (see the \f[B]HISTORY\f[R] section) and the
464 prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output
465 to \f[B]stdout\f[R].
466 .PP
467 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
468 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
469 write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in
470 \f[B]bc >&-\f[R], it will quit with an error.
471 This is done so that bc(1) can report problems when \f[B]stdout\f[R] is
472 redirected to a file.
473 .PP
474 If there are scripts that depend on the behavior of other bc(1)
475 implementations, it is recommended that those scripts be changed to
476 redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R].
477 .SH STDERR
478 .PP
479 Any error output is written to \f[B]stderr\f[R].
480 .PP
481 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
482 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
483 write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in
484 \f[B]bc 2>&-\f[R], it will quit with an error.
485 This is done so that bc(1) can exit with an error code when
486 \f[B]stderr\f[R] is redirected to a file.
487 .PP
488 If there are scripts that depend on the behavior of other bc(1)
489 implementations, it is recommended that those scripts be changed to
490 redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R].
491 .SH SYNTAX
492 .PP
493 The syntax for bc(1) programs is mostly C-like, with some differences.
494 This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R]
495 section), which is a much more thorough resource for the language this
496 bc(1) accepts.
497 This section is meant to be a summary and a listing of all the
498 extensions to the standard.
499 .PP
500 In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means
501 statement, and \f[B]I\f[R] means identifier.
502 .PP
503 Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be
504 followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase
505 letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores
506 (\f[B]_\f[R]).
507 The regex is \f[B][a-z][a-z0-9_]*\f[R].
508 Identifiers with more than one character (letter) are a
509 \f[B]non-portable extension\f[R].
510 .PP
511 \f[B]ibase\f[R] is a global variable determining how to interpret
512 constant numbers.
513 It is the \[lq]input\[rq] base, or the number base used for interpreting
514 input numbers.
515 \f[B]ibase\f[R] is initially \f[B]10\f[R].
516 If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R]
517 (\f[B]--warn\f[R]) flags were not given on the command line, the max
518 allowable value for \f[B]ibase\f[R] is \f[B]36\f[R].
519 Otherwise, it is \f[B]16\f[R].
520 The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R].
521 The max allowable value for \f[B]ibase\f[R] can be queried in bc(1)
522 programs with the \f[B]maxibase()\f[R] built-in function.
523 .PP
524 \f[B]obase\f[R] is a global variable determining how to output results.
525 It is the \[lq]output\[rq] base, or the number base used for outputting
526 numbers.
527 \f[B]obase\f[R] is initially \f[B]10\f[R].
528 The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and
529 can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in
530 function.
531 The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R].
532 Values are output in the specified base.
533 .PP
534 The \f[I]scale\f[R] of an expression is the number of digits in the
535 result of the expression right of the decimal point, and \f[B]scale\f[R]
536 is a global variable that sets the precision of any operations, with
537 exceptions.
538 \f[B]scale\f[R] is initially \f[B]0\f[R].
539 \f[B]scale\f[R] cannot be negative.
540 The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R]
541 and can be queried in bc(1) programs with the \f[B]maxscale()\f[R]
542 built-in function.
543 .PP
544 bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables.
545 All \f[I]local\f[R] variables are local to the function; they are
546 parameters or are introduced in the \f[B]auto\f[R] list of a function
547 (see the \f[B]FUNCTIONS\f[R] section).
548 If a variable is accessed which is not a parameter or in the
549 \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R].
550 If a parent function has a \f[I]local\f[R] variable version of a
551 variable that a child function considers \f[I]global\f[R], the value of
552 that \f[I]global\f[R] variable in the child function is the value of the
553 variable in the parent function, not the value of the actual
554 \f[I]global\f[R] variable.
555 .PP
556 All of the above applies to arrays as well.
557 .PP
558 The value of a statement that is an expression (i.e., any of the named
559 expressions or operands) is printed unless the lowest precedence
560 operator is an assignment operator \f[I]and\f[R] the expression is
561 notsurrounded by parentheses.
562 .PP
563 The value that is printed is also assigned to the special variable
564 \f[B]last\f[R].
565 A single dot (\f[B].\f[R]) may also be used as a synonym for
566 \f[B]last\f[R].
567 These are \f[B]non-portable extensions\f[R].
568 .PP
569 Either semicolons or newlines may separate statements.
570 .SS Comments
571 .PP
572 There are two kinds of comments:
573 .IP "1." 3
574 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R].
575 .IP "2." 3
576 Line comments go from \f[B]#\f[R] until, and not including, the next
577 newline.
578 This is a \f[B]non-portable extension\f[R].
579 .SS Named Expressions
580 .PP
581 The following are named expressions in bc(1):
582 .IP "1." 3
583 Variables: \f[B]I\f[R]
584 .IP "2." 3
585 Array Elements: \f[B]I[E]\f[R]
586 .IP "3." 3
587 \f[B]ibase\f[R]
588 .IP "4." 3
589 \f[B]obase\f[R]
590 .IP "5." 3
591 \f[B]scale\f[R]
592 .IP "6." 3
593 \f[B]last\f[R] or a single dot (\f[B].\f[R])
594 .PP
595 Number 6 is a \f[B]non-portable extension\f[R].
596 .PP
597 Variables and arrays do not interfere; users can have arrays named the
598 same as variables.
599 This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so
600 a user can have a variable, array, and function that all have the same
601 name, and they will not shadow each other, whether inside of functions
602 or not.
603 .PP
604 Named expressions are required as the operand of
605 \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side
606 of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R]
607 subsection).
608 .SS Operands
609 .PP
610 The following are valid operands in bc(1):
611 .IP " 1." 4
612 Numbers (see the \f[I]Numbers\f[R] subsection below).
613 .IP " 2." 4
614 Array indices (\f[B]I[E]\f[R]).
615 .IP " 3." 4
616 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence).
617 .IP " 4." 4
618 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R].
619 \f[B]E\f[R] must be non-negative.
620 .IP " 5." 4
621 \f[B]length(E)\f[R]: The number of significant decimal digits in
622 \f[B]E\f[R].
623 Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places.
624 If given a string, the length of the string is returned.
625 Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable
626 extension\f[R].
627 .IP " 6." 4
628 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R].
629 This is a \f[B]non-portable extension\f[R].
630 .IP " 7." 4
631 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R].
632 .IP " 8." 4
633 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R].
634 This is a \f[B]non-portable extension\f[R].
635 .IP " 9." 4
636 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number,
637 \f[B]0\f[R] if it is a string.
638 This is a \f[B]non-portable extension\f[R].
639 .IP "10." 4
640 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string,
641 \f[B]0\f[R] if it is a number.
642 This is a \f[B]non-portable extension\f[R].
643 .IP "11." 4
644 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first
645 expression is the base, the second is the exponent, and the third is the
646 modulus.
647 All three values must be integers.
648 The second argument must be non-negative.
649 The third argument must be non-zero.
650 This is a \f[B]non-portable extension\f[R].
651 .IP "12." 4
652 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation.
653 This is for optimization.
654 The first expression is the dividend, and the second is the divisor,
655 which must be non-zero.
656 The return value is the quotient, and the modulus is stored in index
657 \f[B]0\f[R] of the provided array (the last argument).
658 This is a \f[B]non-portable extension\f[R].
659 .IP "13." 4
660 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that
661 is the first letter of its argument.
662 If it is a number, calculates the number mod \f[B]256\f[R] and returns
663 that number as a one-character string.
664 This is a \f[B]non-portable extension\f[R].
665 .IP "14." 4
666 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that
667 would result from running \f[B]asciify(E)\f[R] on each element of the
668 array identified by the argument.
669 This allows creating multi-character strings and storing them.
670 This is a \f[B]non-portable extension\f[R].
671 .IP "15." 4
672 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
673 \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the
674 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
675 The \f[B]E\f[R] argument(s) may also be arrays of the form
676 \f[B]I[]\f[R], which will automatically be turned into array references
677 (see the \f[I]Array References\f[R] subsection of the
678 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
679 function definition is an array reference.
680 .IP "16." 4
681 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an
682 expression.
683 The result of that expression is the result of the \f[B]read()\f[R]
684 operand.
685 This is a \f[B]non-portable extension\f[R].
686 .IP "17." 4
687 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R].
688 This is a \f[B]non-portable extension\f[R].
689 .IP "18." 4
690 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R].
691 This is a \f[B]non-portable extension\f[R].
692 .IP "19." 4
693 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R].
694 This is a \f[B]non-portable extension\f[R].
695 .IP "20." 4
696 \f[B]line_length()\f[R]: The line length set with
697 \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R]
698 section).
699 This is a \f[B]non-portable extension\f[R].
700 .IP "21." 4
701 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled
702 with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero
703 otherwise.
704 See the \f[B]OPTIONS\f[R] section.
705 This is a \f[B]non-portable extension\f[R].
706 .IP "22." 4
707 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled
708 with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero
709 otherwise.
710 See the \f[B]OPTIONS\f[R] section.
711 This is a \f[B]non-portable extension\f[R].
712 .SS Numbers
713 .PP
714 Numbers are strings made up of digits, uppercase letters, and at most
715 \f[B]1\f[R] period for a radix.
716 Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits.
717 Uppercase letters are equal to \f[B]9\f[R] plus their position in the
718 alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals
719 \f[B]10\f[R], or \f[B]9+1\f[R]).
720 .PP
721 If a digit or letter makes no sense with the current value of
722 \f[B]ibase\f[R] (i.e., they are greater than or equal to the current
723 value of \f[B]ibase\f[R]), then the behavior depends on the existence of
724 the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or
725 \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the
726 \f[B]OPTIONS\f[R] section), the existence and setting of the
727 \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT
728 VARIABLES\f[R] section), or the default, which can be queried with the
729 \f[B]-h\f[R]/\f[B]--help\f[R] option.
730 .PP
731 If clamping is off, then digits or letters that are greater than or
732 equal to the current value of \f[B]ibase\f[R] are not changed.
733 Instead, their given value is multiplied by the appropriate power of
734 \f[B]ibase\f[R] and added into the number.
735 This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number
736 \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is
737 \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R].
738 .PP
739 If clamping is on, then digits or letters that are greater than or equal
740 to the current value of \f[B]ibase\f[R] are set to the value of the
741 highest valid digit in \f[B]ibase\f[R] before being multiplied by the
742 appropriate power of \f[B]ibase\f[R] and added into the number.
743 This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number
744 \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is
745 \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R].
746 .PP
747 There is one exception to clamping: single-character numbers (i.e.,
748 \f[B]A\f[R] alone).
749 Such numbers are never clamped and always take the value they would have
750 in the highest possible \f[B]ibase\f[R].
751 This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and
752 \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R].
753 This behavior is mandated by the standard (see the STANDARDS section)
754 and is meant to provide an easy way to set the current \f[B]ibase\f[R]
755 (with the \f[B]i\f[R] command) regardless of the current value of
756 \f[B]ibase\f[R].
757 .PP
758 If clamping is on, and the clamped value of a character is needed, use a
759 leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R].
760 .SS Operators
761 .PP
762 The following arithmetic and logical operators can be used.
763 They are listed in order of decreasing precedence.
764 Operators in the same group have the same precedence.
765 .TP
766 \f[B]++\f[R] \f[B]--\f[R]
767 Type: Prefix and Postfix
768 .RS
769 .PP
770 Associativity: None
771 .PP
772 Description: \f[B]increment\f[R], \f[B]decrement\f[R]
773 .RE
774 .TP
775 \f[B]-\f[R] \f[B]!\f[R]
776 Type: Prefix
777 .RS
778 .PP
779 Associativity: None
780 .PP
781 Description: \f[B]negation\f[R], \f[B]boolean not\f[R]
782 .RE
783 .TP
784 \f[B]\[ha]\f[R]
785 Type: Binary
786 .RS
787 .PP
788 Associativity: Right
789 .PP
790 Description: \f[B]power\f[R]
791 .RE
792 .TP
793 \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R]
794 Type: Binary
795 .RS
796 .PP
797 Associativity: Left
798 .PP
799 Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R]
800 .RE
801 .TP
802 \f[B]+\f[R] \f[B]-\f[R]
803 Type: Binary
804 .RS
805 .PP
806 Associativity: Left
807 .PP
808 Description: \f[B]add\f[R], \f[B]subtract\f[R]
809 .RE
810 .TP
811 \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R]
812 Type: Binary
813 .RS
814 .PP
815 Associativity: Right
816 .PP
817 Description: \f[B]assignment\f[R]
818 .RE
819 .TP
820 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R]
821 Type: Binary
822 .RS
823 .PP
824 Associativity: Left
825 .PP
826 Description: \f[B]relational\f[R]
827 .RE
828 .TP
829 \f[B]&&\f[R]
830 Type: Binary
831 .RS
832 .PP
833 Associativity: Left
834 .PP
835 Description: \f[B]boolean and\f[R]
836 .RE
837 .TP
838 \f[B]||\f[R]
839 Type: Binary
840 .RS
841 .PP
842 Associativity: Left
843 .PP
844 Description: \f[B]boolean or\f[R]
845 .RE
846 .PP
847 The operators will be described in more detail below.
848 .TP
849 \f[B]++\f[R] \f[B]--\f[R]
850 The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R]
851 operators behave exactly like they would in C.
852 They require a named expression (see the \f[I]Named Expressions\f[R]
853 subsection) as an operand.
854 .RS
855 .PP
856 The prefix versions of these operators are more efficient; use them
857 where possible.
858 .RE
859 .TP
860 \f[B]-\f[R]
861 The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts
862 to negate any expression with the value \f[B]0\f[R].
863 Otherwise, a copy of the expression with its sign flipped is returned.
864 .TP
865 \f[B]!\f[R]
866 The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression
867 is \f[B]0\f[R], or \f[B]0\f[R] otherwise.
868 .RS
869 .PP
870 This is a \f[B]non-portable extension\f[R].
871 .RE
872 .TP
873 \f[B]\[ha]\f[R]
874 The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator,
875 as it would be in C) takes two expressions and raises the first to the
876 power of the value of the second.
877 The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R].
878 .RS
879 .PP
880 The second expression must be an integer (no \f[I]scale\f[R]), and if it
881 is negative, the first value must be non-zero.
882 .RE
883 .TP
884 \f[B]*\f[R]
885 The \f[B]multiply\f[R] operator takes two expressions, multiplies them,
886 and returns the product.
887 If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and
888 \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the
889 \f[I]scale\f[R] of the result is equal to
890 \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and
891 \f[B]max()\f[R] return the obvious values.
892 .TP
893 \f[B]/\f[R]
894 The \f[B]divide\f[R] operator takes two expressions, divides them, and
895 returns the quotient.
896 The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R].
897 .RS
898 .PP
899 The second expression must be non-zero.
900 .RE
901 .TP
902 \f[B]%\f[R]
903 The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and
904 \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current
905 \f[B]scale\f[R] and 2) Using the result of step 1 to calculate
906 \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R]
907 \f[B]max(scale+scale(b),scale(a))\f[R].
908 .RS
909 .PP
910 The second expression must be non-zero.
911 .RE
912 .TP
913 \f[B]+\f[R]
914 The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and
915 \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the
916 max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
917 .TP
918 \f[B]-\f[R]
919 The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and
920 \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to
921 the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
922 .TP
923 \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R]
924 The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and
925 \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named
926 Expressions\f[R] subsection).
927 .RS
928 .PP
929 For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to
930 \f[B]a\f[R].
931 For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to
932 the corresponding arithmetic operator and the result is assigned to
933 \f[B]a\f[R].
934 .RE
935 .TP
936 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R]
937 The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R]
938 and \f[B]b\f[R], and if the relation holds, according to C language
939 semantics, the result is \f[B]1\f[R].
940 Otherwise, it is \f[B]0\f[R].
941 .RS
942 .PP
943 Note that unlike in C, these operators have a lower precedence than the
944 \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is
945 interpreted as \f[B](a=b)>c\f[R].
946 .PP
947 Also, unlike the standard (see the \f[B]STANDARDS\f[R] section)
948 requires, these operators can appear anywhere any other expressions can
949 be used.
950 This allowance is a \f[B]non-portable extension\f[R].
951 .RE
952 .TP
953 \f[B]&&\f[R]
954 The \f[B]boolean and\f[R] operator takes two expressions and returns
955 \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise.
956 .RS
957 .PP
958 This is \f[I]not\f[R] a short-circuit operator.
959 .PP
960 This is a \f[B]non-portable extension\f[R].
961 .RE
962 .TP
963 \f[B]||\f[R]
964 The \f[B]boolean or\f[R] operator takes two expressions and returns
965 \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R]
966 otherwise.
967 .RS
968 .PP
969 This is \f[I]not\f[R] a short-circuit operator.
970 .PP
971 This is a \f[B]non-portable extension\f[R].
972 .RE
973 .SS Statements
974 .PP
975 The following items are statements:
976 .IP " 1." 4
977 \f[B]E\f[R]
978 .IP " 2." 4
979 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&...
980 \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R]
981 .IP " 3." 4
982 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
983 .IP " 4." 4
984 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
985 \f[B]else\f[R] \f[B]S\f[R]
986 .IP " 5." 4
987 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
988 .IP " 6." 4
989 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R]
990 \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
991 .IP " 7." 4
992 An empty statement
993 .IP " 8." 4
994 \f[B]break\f[R]
995 .IP " 9." 4
996 \f[B]continue\f[R]
997 .IP "10." 4
998 \f[B]quit\f[R]
999 .IP "11." 4
1000 \f[B]halt\f[R]
1001 .IP "12." 4
1002 \f[B]limits\f[R]
1003 .IP "13." 4
1004 A string of characters, enclosed in double quotes
1005 .IP "14." 4
1006 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&...
1007 \f[B],\f[R] \f[B]E\f[R]
1008 .IP "15." 4
1009 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&...
1010 \f[B],\f[R] \f[B]E\f[R]
1011 .IP "16." 4
1012 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
1013 \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the
1014 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
1015 The \f[B]E\f[R] argument(s) may also be arrays of the form
1016 \f[B]I[]\f[R], which will automatically be turned into array references
1017 (see the \f[I]Array References\f[R] subsection of the
1018 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
1019 function definition is an array reference.
1020 .PP
1021 Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable
1022 extensions\f[R].
1023 .PP
1024 Also, as a \f[B]non-portable extension\f[R], any or all of the
1025 expressions in the header of a for loop may be omitted.
1026 If the condition (second expression) is omitted, it is assumed to be a
1027 constant \f[B]1\f[R].
1028 .PP
1029 The \f[B]break\f[R] statement causes a loop to stop iterating and resume
1030 execution immediately following a loop.
1031 This is only allowed in loops.
1032 .PP
1033 The \f[B]continue\f[R] statement causes a loop iteration to stop early
1034 and returns to the start of the loop, including testing the loop
1035 condition.
1036 This is only allowed in loops.
1037 .PP
1038 The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C.
1039 .PP
1040 The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a
1041 branch that will not be executed (it is a compile-time command).
1042 .PP
1043 \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is
1044 slightly different from other bc(1) implementations.
1045 Other bc(1) implementations will exit as soon as they finish parsing the
1046 line that a \f[B]quit\f[R] command is on.
1047 This bc(1) will execute any completed and executable statements that
1048 occur before the \f[B]quit\f[R] statement before exiting.
1049 .PP
1050 In other words, for the bc(1) code below:
1051 .IP
1052 .nf
1053 \f[C]
1054 for (i = 0; i < 3; ++i) i; quit
1055 \f[R]
1056 .fi
1057 .PP
1058 Other bc(1) implementations will print nothing, and this bc(1) will
1059 print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines
1060 before exiting.
1061 .PP
1062 The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed.
1063 (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement
1064 that is not executed, bc(1) does not quit.)
1065 .PP
1066 The \f[B]limits\f[R] statement prints the limits that this bc(1) is
1067 subject to.
1068 This is like the \f[B]quit\f[R] statement in that it is a compile-time
1069 command.
1070 .PP
1071 An expression by itself is evaluated and printed, followed by a newline.
1072 .SS Strings
1073 .PP
1074 If strings appear as a statement by themselves, they are printed without
1075 a trailing newline.
1076 .PP
1077 In addition to appearing as a lone statement by themselves, strings can
1078 be assigned to variables and array elements.
1079 They can also be passed to functions in variable parameters.
1080 .PP
1081 If any statement that expects a string is given a variable that had a
1082 string assigned to it, the statement acts as though it had received a
1083 string.
1084 .PP
1085 If any math operation is attempted on a string or a variable or array
1086 element that has been assigned a string, an error is raised, and bc(1)
1087 resets (see the \f[B]RESET\f[R] section).
1088 .PP
1089 Assigning strings to variables and array elements and passing them to
1090 functions are \f[B]non-portable extensions\f[R].
1091 .SS Print Statement
1092 .PP
1093 The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be
1094 strings.
1095 If they are, there are backslash escape sequences that are interpreted
1096 specially.
1097 What those sequences are, and what they cause to be printed, are shown
1098 below:
1099 .PP
1100 \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R]
1101 .PP
1102 \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R]
1103 .PP
1104 \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R]
1105 .PP
1106 \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R]
1107 .PP
1108 \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R]
1109 .PP
1110 \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R]
1111 .PP
1112 \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R]
1113 .PP
1114 \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R]
1115 .PP
1116 \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R]
1117 .PP
1118 Any other character following a backslash causes the backslash and
1119 character to be printed as-is.
1120 .PP
1121 Any non-string expression in a print statement shall be assigned to
1122 \f[B]last\f[R], like any other expression that is printed.
1123 .SS Stream Statement
1124 .PP
1125 The \[lq]expressions in a \f[B]stream\f[R] statement may also be
1126 strings.
1127 .PP
1128 If a \f[B]stream\f[R] statement is given a string, it prints the string
1129 as though the string had appeared as its own statement.
1130 In other words, the \f[B]stream\f[R] statement prints strings normally,
1131 without a newline.
1132 .PP
1133 If a \f[B]stream\f[R] statement is given a number, a copy of it is
1134 truncated and its absolute value is calculated.
1135 The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R]
1136 and each digit is interpreted as an 8-bit ASCII character, making it a
1137 byte stream.
1138 .SS Order of Evaluation
1139 .PP
1140 All expressions in a statment are evaluated left to right, except as
1141 necessary to maintain order of operations.
1142 This means, for example, assuming that \f[B]i\f[R] is equal to
1143 \f[B]0\f[R], in the expression
1144 .IP
1145 .nf
1146 \f[C]
1147 a[i++] = i++
1148 \f[R]
1149 .fi
1150 .PP
1151 the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and
1152 \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression.
1153 .PP
1154 This includes function arguments.
1155 Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in
1156 the expression
1157 .IP
1158 .nf
1159 \f[C]
1160 x(i++, i++)
1161 \f[R]
1162 .fi
1163 .PP
1164 the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the
1165 second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to
1166 \f[B]2\f[R] before the function starts executing.
1167 .SH FUNCTIONS
1168 .PP
1169 Function definitions are as follows:
1170 .IP
1171 .nf
1172 \f[C]
1173 define I(I,...,I){
1174     auto I,...,I
1175     S;...;S
1176     return(E)
1177 }
1178 \f[R]
1179 .fi
1180 .PP
1181 Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be
1182 replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an
1183 array, and any \f[B]I\f[R] in the parameter list may be replaced with
1184 \f[B]*I[]\f[R] to make a parameter an array reference.
1185 Callers of functions that take array references should not put an
1186 asterisk in the call; they must be called with just \f[B]I[]\f[R] like
1187 normal array parameters and will be automatically converted into
1188 references.
1189 .PP
1190 As a \f[B]non-portable extension\f[R], the opening brace of a
1191 \f[B]define\f[R] statement may appear on the next line.
1192 .PP
1193 As a \f[B]non-portable extension\f[R], the return statement may also be
1194 in one of the following forms:
1195 .IP "1." 3
1196 \f[B]return\f[R]
1197 .IP "2." 3
1198 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R]
1199 .IP "3." 3
1200 \f[B]return\f[R] \f[B]E\f[R]
1201 .PP
1202 The first two, or not specifying a \f[B]return\f[R] statement, is
1203 equivalent to \f[B]return (0)\f[R], unless the function is a
1204 \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection
1205 below).
1206 .SS Void Functions
1207 .PP
1208 Functions can also be \f[B]void\f[R] functions, defined as follows:
1209 .IP
1210 .nf
1211 \f[C]
1212 define void I(I,...,I){
1213     auto I,...,I
1214     S;...;S
1215     return
1216 }
1217 \f[R]
1218 .fi
1219 .PP
1220 They can only be used as standalone expressions, where such an
1221 expression would be printed alone, except in a print statement.
1222 .PP
1223 Void functions can only use the first two \f[B]return\f[R] statements
1224 listed above.
1225 They can also omit the return statement entirely.
1226 .PP
1227 The word \[lq]void\[rq] is not treated as a keyword; it is still
1228 possible to have variables, arrays, and functions named \f[B]void\f[R].
1229 The word \[lq]void\[rq] is only treated specially right after the
1230 \f[B]define\f[R] keyword.
1231 .PP
1232 This is a \f[B]non-portable extension\f[R].
1233 .SS Array References
1234 .PP
1235 For any array in the parameter list, if the array is declared in the
1236 form
1237 .IP
1238 .nf
1239 \f[C]
1240 *I[]
1241 \f[R]
1242 .fi
1243 .PP
1244 it is a \f[B]reference\f[R].
1245 Any changes to the array in the function are reflected, when the
1246 function returns, to the array that was passed in.
1247 .PP
1248 Other than this, all function arguments are passed by value.
1249 .PP
1250 This is a \f[B]non-portable extension\f[R].
1251 .SH LIBRARY
1252 .PP
1253 All of the functions below are available when the \f[B]-l\f[R] or
1254 \f[B]--mathlib\f[R] command-line flags are given.
1255 .SS Standard Library
1256 .PP
1257 The standard (see the \f[B]STANDARDS\f[R] section) defines the following
1258 functions for the math library:
1259 .TP
1260 \f[B]s(x)\f[R]
1261 Returns the sine of \f[B]x\f[R], which is assumed to be in radians.
1262 .RS
1263 .PP
1264 This is a transcendental function (see the \f[I]Transcendental
1265 Functions\f[R] subsection below).
1266 .RE
1267 .TP
1268 \f[B]c(x)\f[R]
1269 Returns the cosine of \f[B]x\f[R], which is assumed to be in radians.
1270 .RS
1271 .PP
1272 This is a transcendental function (see the \f[I]Transcendental
1273 Functions\f[R] subsection below).
1274 .RE
1275 .TP
1276 \f[B]a(x)\f[R]
1277 Returns the arctangent of \f[B]x\f[R], in radians.
1278 .RS
1279 .PP
1280 This is a transcendental function (see the \f[I]Transcendental
1281 Functions\f[R] subsection below).
1282 .RE
1283 .TP
1284 \f[B]l(x)\f[R]
1285 Returns the natural logarithm of \f[B]x\f[R].
1286 .RS
1287 .PP
1288 This is a transcendental function (see the \f[I]Transcendental
1289 Functions\f[R] subsection below).
1290 .RE
1291 .TP
1292 \f[B]e(x)\f[R]
1293 Returns the mathematical constant \f[B]e\f[R] raised to the power of
1294 \f[B]x\f[R].
1295 .RS
1296 .PP
1297 This is a transcendental function (see the \f[I]Transcendental
1298 Functions\f[R] subsection below).
1299 .RE
1300 .TP
1301 \f[B]j(x, n)\f[R]
1302 Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R].
1303 .RS
1304 .PP
1305 This is a transcendental function (see the \f[I]Transcendental
1306 Functions\f[R] subsection below).
1307 .RE
1308 .SS Transcendental Functions
1309 .PP
1310 All transcendental functions can return slightly inaccurate results, up
1311 to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place).
1312 This is unavoidable, and the article at
1313 https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why
1314 it is impossible and unnecessary to calculate exact results for the
1315 transcendental functions.
1316 .PP
1317 Because of the possible inaccuracy, I recommend that users call those
1318 functions with the precision (\f[B]scale\f[R]) set to at least 1 higher
1319 than is necessary.
1320 If exact results are \f[I]absolutely\f[R] required, users can double the
1321 precision (\f[B]scale\f[R]) and then truncate.
1322 .PP
1323 The transcendental functions in the standard math library are:
1324 .IP \[bu] 2
1325 \f[B]s(x)\f[R]
1326 .IP \[bu] 2
1327 \f[B]c(x)\f[R]
1328 .IP \[bu] 2
1329 \f[B]a(x)\f[R]
1330 .IP \[bu] 2
1331 \f[B]l(x)\f[R]
1332 .IP \[bu] 2
1333 \f[B]e(x)\f[R]
1334 .IP \[bu] 2
1335 \f[B]j(x, n)\f[R]
1336 .SH RESET
1337 .PP
1338 When bc(1) encounters an error or a signal that it has a non-default
1339 handler for, it resets.
1340 This means that several things happen.
1341 .PP
1342 First, any functions that are executing are stopped and popped off the
1343 stack.
1344 The behavior is not unlike that of exceptions in programming languages.
1345 Then the execution point is set so that any code waiting to execute
1346 (after all functions returned) is skipped.
1347 .PP
1348 Thus, when bc(1) resets, it skips any remaining code waiting to be
1349 executed.
1350 Then, if it is interactive mode, and the error was not a fatal error
1351 (see the \f[B]EXIT STATUS\f[R] section), it asks for more input;
1352 otherwise, it exits with the appropriate return code.
1353 .PP
1354 Note that this reset behavior is different from the GNU bc(1), which
1355 attempts to start executing the statement right after the one that
1356 caused an error.
1357 .SH PERFORMANCE
1358 .PP
1359 Most bc(1) implementations use \f[B]char\f[R] types to calculate the
1360 value of \f[B]1\f[R] decimal digit at a time, but that can be slow.
1361 This bc(1) does something different.
1362 .PP
1363 It uses large integers to calculate more than \f[B]1\f[R] decimal digit
1364 at a time.
1365 If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the
1366 \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has
1367 \f[B]9\f[R] decimal digits.
1368 If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R]
1369 then each integer has \f[B]4\f[R] decimal digits.
1370 This value (the number of decimal digits per large integer) is called
1371 \f[B]BC_BASE_DIGS\f[R].
1372 .PP
1373 The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R]
1374 can be queried with the \f[B]limits\f[R] statement.
1375 .PP
1376 In addition, this bc(1) uses an even larger integer for overflow
1377 checking.
1378 This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is
1379 always at least twice as large as the integer type used to store digits.
1380 .SH LIMITS
1381 .PP
1382 The following are the limits on bc(1):
1383 .TP
1384 \f[B]BC_LONG_BIT\f[R]
1385 The number of bits in the \f[B]long\f[R] type in the environment where
1386 bc(1) was built.
1387 This determines how many decimal digits can be stored in a single large
1388 integer (see the \f[B]PERFORMANCE\f[R] section).
1389 .TP
1390 \f[B]BC_BASE_DIGS\f[R]
1391 The number of decimal digits per large integer (see the
1392 \f[B]PERFORMANCE\f[R] section).
1393 Depends on \f[B]BC_LONG_BIT\f[R].
1394 .TP
1395 \f[B]BC_BASE_POW\f[R]
1396 The max decimal number that each large integer can store (see
1397 \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R].
1398 Depends on \f[B]BC_BASE_DIGS\f[R].
1399 .TP
1400 \f[B]BC_OVERFLOW_MAX\f[R]
1401 The max number that the overflow type (see the \f[B]PERFORMANCE\f[R]
1402 section) can hold.
1403 Depends on \f[B]BC_LONG_BIT\f[R].
1404 .TP
1405 \f[B]BC_BASE_MAX\f[R]
1406 The maximum output base.
1407 Set at \f[B]BC_BASE_POW\f[R].
1408 .TP
1409 \f[B]BC_DIM_MAX\f[R]
1410 The maximum size of arrays.
1411 Set at \f[B]SIZE_MAX-1\f[R].
1412 .TP
1413 \f[B]BC_SCALE_MAX\f[R]
1414 The maximum \f[B]scale\f[R].
1415 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1416 .TP
1417 \f[B]BC_STRING_MAX\f[R]
1418 The maximum length of strings.
1419 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1420 .TP
1421 \f[B]BC_NAME_MAX\f[R]
1422 The maximum length of identifiers.
1423 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1424 .TP
1425 \f[B]BC_NUM_MAX\f[R]
1426 The maximum length of a number (in decimal digits), which includes
1427 digits after the decimal point.
1428 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1429 .TP
1430 Exponent
1431 The maximum allowable exponent (positive or negative).
1432 Set at \f[B]BC_OVERFLOW_MAX\f[R].
1433 .TP
1434 Number of vars
1435 The maximum number of vars/arrays.
1436 Set at \f[B]SIZE_MAX-1\f[R].
1437 .PP
1438 The actual values can be queried with the \f[B]limits\f[R] statement.
1439 .PP
1440 These limits are meant to be effectively non-existent; the limits are so
1441 large (at least on 64-bit machines) that there should not be any point
1442 at which they become a problem.
1443 In fact, memory should be exhausted before these limits should be hit.
1444 .SH ENVIRONMENT VARIABLES
1445 .PP
1446 As \f[B]non-portable extensions\f[R], bc(1) recognizes the following
1447 environment variables:
1448 .TP
1449 \f[B]POSIXLY_CORRECT\f[R]
1450 If this variable exists (no matter the contents), bc(1) behaves as if
1451 the \f[B]-s\f[R] option was given.
1452 .TP
1453 \f[B]BC_ENV_ARGS\f[R]
1454 This is another way to give command-line arguments to bc(1).
1455 They should be in the same format as all other command-line arguments.
1456 These are always processed first, so any files given in
1457 \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given
1458 on the command-line.
1459 This gives the user the ability to set up \[lq]standard\[rq] options and
1460 files to be used at every invocation.
1461 The most useful thing for such files to contain would be useful
1462 functions that the user might want every time bc(1) runs.
1463 .RS
1464 .PP
1465 The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted
1466 arguments, but it does not understand escape sequences.
1467 For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R]
1468 will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some
1469 \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes.
1470 .PP
1471 The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or
1472 \f[B]\[lq]\f[R].
1473 Thus, if you have a file with any number of single quotes in the name,
1474 you can use double quotes as the outside quotes, as in \f[B]\[lq]some
1475 `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double
1476 quotes.
1477 However, handling a file with both kinds of quotes in
1478 \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the
1479 parsing, though such files are still supported on the command-line where
1480 the parsing is done by the shell.
1481 .RE
1482 .TP
1483 \f[B]BC_LINE_LENGTH\f[R]
1484 If this environment variable exists and contains an integer that is
1485 greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R]
1486 (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length,
1487 including the backslash (\f[B]\[rs]\f[R]).
1488 The default line length is \f[B]70\f[R].
1489 .RS
1490 .PP
1491 The special value of \f[B]0\f[R] will disable line length checking and
1492 print numbers without regard to line length and without backslashes and
1493 newlines.
1494 .RE
1495 .TP
1496 \f[B]BC_BANNER\f[R]
1497 If this environment variable exists and contains an integer, then a
1498 non-zero value activates the copyright banner when bc(1) is in
1499 interactive mode, while zero deactivates it.
1500 .RS
1501 .PP
1502 If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R]
1503 section), then this environment variable has no effect because bc(1)
1504 does not print the banner when not in interactive mode.
1505 .PP
1506 This environment variable overrides the default, which can be queried
1507 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
1508 .RE
1509 .TP
1510 \f[B]BC_SIGINT_RESET\f[R]
1511 If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R]
1512 section), then this environment variable has no effect because bc(1)
1513 exits on \f[B]SIGINT\f[R] when not in interactive mode.
1514 .RS
1515 .PP
1516 However, when bc(1) is in interactive mode, then if this environment
1517 variable exists and contains an integer, a non-zero value makes bc(1)
1518 reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit.
1519 If this environment variable exists and is \f[I]not\f[R] an integer,
1520 then bc(1) will exit on \f[B]SIGINT\f[R].
1521 .PP
1522 This environment variable overrides the default, which can be queried
1523 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
1524 .RE
1525 .TP
1526 \f[B]BC_TTY_MODE\f[R]
1527 If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R]
1528 section), then this environment variable has no effect.
1529 .RS
1530 .PP
1531 However, when TTY mode is available, then if this environment variable
1532 exists and contains an integer, then a non-zero value makes bc(1) use
1533 TTY mode, and zero makes bc(1) not use TTY mode.
1534 .PP
1535 This environment variable overrides the default, which can be queried
1536 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
1537 .RE
1538 .TP
1539 \f[B]BC_PROMPT\f[R]
1540 If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R]
1541 section), then this environment variable has no effect.
1542 .RS
1543 .PP
1544 However, when TTY mode is available, then if this environment variable
1545 exists and contains an integer, a non-zero value makes bc(1) use a
1546 prompt, and zero or a non-integer makes bc(1) not use a prompt.
1547 If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R]
1548 does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable
1549 is used.
1550 .PP
1551 This environment variable and the \f[B]BC_TTY_MODE\f[R] environment
1552 variable override the default, which can be queried with the
1553 \f[B]-h\f[R] or \f[B]--help\f[R] options.
1554 .RE
1555 .TP
1556 \f[B]BC_EXPR_EXIT\f[R]
1557 If any expressions or expression files are given on the command-line
1558 with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or
1559 \f[B]--file\f[R], then if this environment variable exists and contains
1560 an integer, a non-zero value makes bc(1) exit after executing the
1561 expressions and expression files, and a zero value makes bc(1) not exit.
1562 .RS
1563 .PP
1564 This environment variable overrides the default, which can be queried
1565 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
1566 .RE
1567 .TP
1568 \f[B]BC_DIGIT_CLAMP\f[R]
1569 When parsing numbers and if this environment variable exists and
1570 contains an integer, a non-zero value makes bc(1) clamp digits that are
1571 greater than or equal to the current \f[B]ibase\f[R] so that all such
1572 digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero
1573 value disables such clamping so that those digits are always equal to
1574 their value, which is multiplied by the power of the \f[B]ibase\f[R].
1575 .RS
1576 .PP
1577 This never applies to single-digit numbers, as per the standard (see the
1578 \f[B]STANDARDS\f[R] section).
1579 .PP
1580 This environment variable overrides the default, which can be queried
1581 with the \f[B]-h\f[R] or \f[B]--help\f[R] options.
1582 .RE
1583 .SH EXIT STATUS
1584 .PP
1585 bc(1) returns the following exit statuses:
1586 .TP
1587 \f[B]0\f[R]
1588 No error.
1589 .TP
1590 \f[B]1\f[R]
1591 A math error occurred.
1592 This follows standard practice of using \f[B]1\f[R] for expected errors,
1593 since math errors will happen in the process of normal execution.
1594 .RS
1595 .PP
1596 Math errors include divide by \f[B]0\f[R], taking the square root of a
1597 negative number, attempting to convert a negative number to a hardware
1598 integer, overflow when converting a number to a hardware integer,
1599 overflow when calculating the size of a number, and attempting to use a
1600 non-integer where an integer is required.
1601 .PP
1602 Converting to a hardware integer happens for the second operand of the
1603 power (\f[B]\[ha]\f[R]) operator and the corresponding assignment
1604 operator.
1605 .RE
1606 .TP
1607 \f[B]2\f[R]
1608 A parse error occurred.
1609 .RS
1610 .PP
1611 Parse errors include unexpected \f[B]EOF\f[R], using an invalid
1612 character, failing to find the end of a string or comment, using a token
1613 where it is invalid, giving an invalid expression, giving an invalid
1614 print statement, giving an invalid function definition, attempting to
1615 assign to an expression that is not a named expression (see the
1616 \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section),
1617 giving an invalid \f[B]auto\f[R] list, having a duplicate
1618 \f[B]auto\f[R]/function parameter, failing to find the end of a code
1619 block, attempting to return a value from a \f[B]void\f[R] function,
1620 attempting to use a variable as a reference, and using any extensions
1621 when the option \f[B]-s\f[R] or any equivalents were given.
1622 .RE
1623 .TP
1624 \f[B]3\f[R]
1625 A runtime error occurred.
1626 .RS
1627 .PP
1628 Runtime errors include assigning an invalid number to any global
1629 (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad
1630 expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside
1631 of a \f[B]read()\f[R] call, type errors, passing the wrong number of
1632 arguments to functions, attempting to call an undefined function, and
1633 attempting to use a \f[B]void\f[R] function call as a value in an
1634 expression.
1635 .RE
1636 .TP
1637 \f[B]4\f[R]
1638 A fatal error occurred.
1639 .RS
1640 .PP
1641 Fatal errors include memory allocation errors, I/O errors, failing to
1642 open files, attempting to use files that do not have only ASCII
1643 characters (bc(1) only accepts ASCII characters), attempting to open a
1644 directory as a file, and giving invalid command-line options.
1645 .RE
1646 .PP
1647 The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1)
1648 always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in.
1649 .PP
1650 The other statuses will only be returned when bc(1) is not in
1651 interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since
1652 bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts
1653 more input when one of those errors occurs in interactive mode.
1654 This is also the case when interactive mode is forced by the
1655 \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option.
1656 .PP
1657 These exit statuses allow bc(1) to be used in shell scripting with error
1658 checking, and its normal behavior can be forced by using the
1659 \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option.
1660 .SH INTERACTIVE MODE
1661 .PP
1662 Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an
1663 interactive mode and a non-interactive mode.
1664 Interactive mode is turned on automatically when both \f[B]stdin\f[R]
1665 and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag
1666 and \f[B]--interactive\f[R] option can turn it on in other situations.
1667 .PP
1668 In interactive mode, bc(1) attempts to recover from errors (see the
1669 \f[B]RESET\f[R] section), and in normal execution, flushes
1670 \f[B]stdout\f[R] as soon as execution is done for the current input.
1671 bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on
1672 the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R]
1673 environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section).
1674 .SH TTY MODE
1675 .PP
1676 If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all
1677 connected to a TTY, then \[lq]TTY mode\[rq] is considered to be
1678 available, and thus, bc(1) can turn on TTY mode, subject to some
1679 settings.
1680 .PP
1681 If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the
1682 environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if
1683 that environment variable contains a non-zero integer, bc(1) will turn
1684 on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R]
1685 are all connected to a TTY.
1686 If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is
1687 \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on.
1688 .PP
1689 If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R]
1690 exist, the default setting is used.
1691 The default setting can be queried with the \f[B]-h\f[R] or
1692 \f[B]--help\f[R] options.
1693 .PP
1694 TTY mode is different from interactive mode because interactive mode is
1695 required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section),
1696 and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R]
1697 to be connected to a terminal.
1698 .SS Prompt
1699 .PP
1700 If TTY mode is available, then a prompt can be enabled.
1701 Like TTY mode itself, it can be turned on or off with an environment
1702 variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R]
1703 section).
1704 .PP
1705 If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero
1706 integer, then the prompt is turned on when \f[B]stdin\f[R],
1707 \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the
1708 \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used.
1709 The read prompt will be turned on under the same conditions, except that
1710 the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be
1711 used.
1712 .PP
1713 However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be
1714 enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable,
1715 the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R]
1716 and \f[B]--no-read-prompt\f[R] options.
1717 See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections
1718 for more details.
1719 .SH SIGNAL HANDLING
1720 .PP
1721 Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things.
1722 .PP
1723 If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R]
1724 section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the
1725 \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not
1726 an integer or it is zero, bc(1) will exit.
1727 .PP
1728 However, if bc(1) is in interactive mode, and the
1729 \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero,
1730 then bc(1) will stop executing the current input and reset (see the
1731 \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R].
1732 .PP
1733 Note that \[lq]current input\[rq] can mean one of two things.
1734 If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode,
1735 it will ask for more input.
1736 If bc(1) is processing input from a file in interactive mode, it will
1737 stop processing the file and start processing the next file, if one
1738 exists, or ask for input from \f[B]stdin\f[R] if no other file exists.
1739 .PP
1740 This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is
1741 executing a file, it can seem as though bc(1) did not respond to the
1742 signal since it will immediately start executing the next file.
1743 This is by design; most files that users execute when interacting with
1744 bc(1) have function definitions, which are quick to parse.
1745 If a file takes a long time to execute, there may be a bug in that file.
1746 The rest of the files could still be executed without problem, allowing
1747 the user to continue.
1748 .PP
1749 \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and
1750 exit, and it uses the default handler for all other signals.
1751 .SH SEE ALSO
1752 .PP
1753 dc(1)
1754 .SH STANDARDS
1755 .PP
1756 bc(1) is compliant with the IEEE Std 1003.1-2017
1757 (\[lq]POSIX.1-2017\[rq]) specification at
1758 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html .
1759 The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions
1760 noted above are extensions to that specification.
1761 .PP
1762 In addition, the behavior of the \f[B]quit\f[R] implements an
1763 interpretation of that specification that is different from all known
1764 implementations.
1765 For more information see the \f[B]Statements\f[R] subsection of the
1766 \f[B]SYNTAX\f[R] section.
1767 .PP
1768 Note that the specification explicitly says that bc(1) only accepts
1769 numbers that use a period (\f[B].\f[R]) as a radix point, regardless of
1770 the value of \f[B]LC_NUMERIC\f[R].
1771 .SH BUGS
1772 .PP
1773 Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for
1774 the \f[B]quit\f[R] statement.
1775 .PP
1776 No other bugs are known.
1777 Report bugs at https://git.gavinhoward.com/gavin/bc .
1778 .SH AUTHORS
1779 .PP
1780 Gavin D.
1781 Howard <gavin@gavinhoward.com> and contributors.