]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/manuals/bc/EHP.1
mlx4en(4): Fix wrong mbuf cluster size in mlx4_en_debugnet_init()
[FreeBSD/FreeBSD.git] / contrib / bc / manuals / bc / EHP.1
1 .\"
2 .\" SPDX-License-Identifier: BSD-2-Clause
3 .\"
4 .\" Copyright (c) 2018-2021 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" "April 2021" "Gavin D. Howard" "General Commands Manual"
29 .SH NAME
30 .PP
31 bc - arbitrary-precision decimal arithmetic language and calculator
32 .SH SYNOPSIS
33 .PP
34 \f[B]bc\f[R] [\f[B]-ghilPqRsvVw\f[R]] [\f[B]--global-stacks\f[R]]
35 [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]]
36 [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]]
37 [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]]
38 [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]...]
39 [\f[B]-f\f[R] \f[I]file\f[R]...] [\f[B]--file\f[R]=\f[I]file\f[R]...]
40 [\f[I]file\f[R]...]
41 .SH DESCRIPTION
42 .PP
43 bc(1) is an interactive processor for a language first standardized in
44 1991 by POSIX.
45 (The current standard is
46 here (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).)
47 The language provides unlimited precision decimal arithmetic and is
48 somewhat C-like, but there are differences.
49 Such differences will be noted in this document.
50 .PP
51 After parsing and handling options, this bc(1) reads any files given on
52 the command line and executes them before reading from \f[B]stdin\f[R].
53 .SH OPTIONS
54 .PP
55 The following are the options that bc(1) accepts.
56 .PP
57 \f[B]-g\f[R], \f[B]--global-stacks\f[R]
58 .IP
59 .nf
60 \f[C]
61 Turns the globals **ibase**, **obase**, and **scale** into stacks.
62
63 This has the effect that a copy of the current value of all three are pushed
64 onto a stack for every function call, as well as popped when every function
65 returns. This means that functions can assign to any and all of those
66 globals without worrying that the change will affect other functions.
67 Thus, a hypothetical function named **output(x,b)** that simply printed
68 **x** in base **b** could be written like this:
69
70     define void output(x, b) {
71         obase=b
72         x
73     }
74
75 instead of like this:
76
77     define void output(x, b) {
78         auto c
79         c=obase
80         obase=b
81         x
82         obase=c
83     }
84
85 This makes writing functions much easier.
86
87 However, since using this flag means that functions cannot set **ibase**,
88 **obase**, or **scale** globally, functions that are made to do so cannot
89 work anymore. There are two possible use cases for that, and each has a
90 solution.
91
92 First, if a function is called on startup to turn bc(1) into a number
93 converter, it is possible to replace that capability with various shell
94 aliases. Examples:
95
96     alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq]
97     alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq]
98
99 Second, if the purpose of a function is to set **ibase**, **obase**, or
100 **scale** globally for any other purpose, it could be split into one to
101 three functions (based on how many globals it sets) and each of those
102 functions could return the desired value for a global.
103
104 If the behavior of this option is desired for every run of bc(1), then users
105 could make sure to define **BC_ENV_ARGS** and include this option (see the
106 **ENVIRONMENT VARIABLES** section for more details).
107
108 If **-s**, **-w**, or any equivalents are used, this option is ignored.
109
110 This is a **non-portable extension**.
111 \f[R]
112 .fi
113 .PP
114 \f[B]-h\f[R], \f[B]--help\f[R]
115 .PP
116 : Prints a usage message and quits.
117 .PP
118 \f[B]-i\f[R], \f[B]--interactive\f[R]
119 .PP
120 : Forces interactive mode.
121 (See the \f[B]INTERACTIVE MODE\f[R] section.)
122 .IP
123 .nf
124 \f[C]
125 This is a **non-portable extension**.
126 \f[R]
127 .fi
128 .PP
129 \f[B]-l\f[R], \f[B]--mathlib\f[R]
130 .PP
131 : Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to
132 \f[B]20\f[R] and loads the included math library before running any
133 code, including any expressions or files specified on the command line.
134 .IP
135 .nf
136 \f[C]
137 To learn what is in the library, see the **LIBRARY** section.
138 \f[R]
139 .fi
140 .PP
141 \f[B]-P\f[R], \f[B]--no-prompt\f[R]
142 .PP
143 : This option is a no-op.
144 .IP
145 .nf
146 \f[C]
147 This is a **non-portable extension**.
148 \f[R]
149 .fi
150 .PP
151 \f[B]-R\f[R], \f[B]--no-read-prompt\f[R]
152 .PP
153 : Because bc(1) was built without support for prompts, this option is a
154 no-op.
155 .IP
156 .nf
157 \f[C]
158 This is a **non-portable extension**.
159 \f[R]
160 .fi
161 .PP
162 \f[B]-q\f[R], \f[B]--quiet\f[R]
163 .PP
164 : This option is for compatibility with the GNU
165 bc(1) (https://www.gnu.org/software/bc/); it is a no-op.
166 Without this option, GNU bc(1) prints a copyright header.
167 This bc(1) only prints the copyright header if one or more of the
168 \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given.
169 .IP
170 .nf
171 \f[C]
172 This is a **non-portable extension**.
173 \f[R]
174 .fi
175 .PP
176 \f[B]-s\f[R], \f[B]--standard\f[R]
177 .PP
178 : Process exactly the language defined by the
179 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
180 and error if any extensions are used.
181 .IP
182 .nf
183 \f[C]
184 This is a **non-portable extension**.
185 \f[R]
186 .fi
187 .PP
188 \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R]
189 .PP
190 : Print the version information (copyright header) and exit.
191 .IP
192 .nf
193 \f[C]
194 This is a **non-portable extension**.
195 \f[R]
196 .fi
197 .PP
198 \f[B]-w\f[R], \f[B]--warn\f[R]
199 .PP
200 : Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and
201 not errors) are printed for non-standard extensions and execution
202 continues normally.
203 .IP
204 .nf
205 \f[C]
206 This is a **non-portable extension**.
207 \f[R]
208 .fi
209 .PP
210 \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R]
211 .PP
212 : Evaluates \f[I]expr\f[R].
213 If multiple expressions are given, they are evaluated in order.
214 If files are given as well (see below), the expressions and files are
215 evaluated in the order given.
216 This means that if a file is given before an expression, the file is
217 read in and evaluated first.
218 .IP
219 .nf
220 \f[C]
221 If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**,
222 see the **ENVIRONMENT VARIABLES** section), then after processing all
223 expressions and files, bc(1) will exit, unless **-** (**stdin**) was given
224 as an argument at least once to **-f** or **-\[rs]-file**, whether on the
225 command-line or in **BC_ENV_ARGS**. However, if any other **-e**,
226 **-\[rs]-expression**, **-f**, or **-\[rs]-file** arguments are given after **-f-**
227 or equivalent is given, bc(1) will give a fatal error and exit.
228
229 This is a **non-portable extension**.
230 \f[R]
231 .fi
232 .PP
233 \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R]
234 .PP
235 : Reads in \f[I]file\f[R] and evaluates it, line by line, as though it
236 were read through \f[B]stdin\f[R].
237 If expressions are also given (see above), the expressions are evaluated
238 in the order given.
239 .IP
240 .nf
241 \f[C]
242 If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**,
243 see the **ENVIRONMENT VARIABLES** section), then after processing all
244 expressions and files, bc(1) will exit, unless **-** (**stdin**) was given
245 as an argument at least once to **-f** or **-\[rs]-file**. However, if any other
246 **-e**, **-\[rs]-expression**, **-f**, or **-\[rs]-file** arguments are given after
247 **-f-** or equivalent is given, bc(1) will give a fatal error and exit.
248
249 This is a **non-portable extension**.
250 \f[R]
251 .fi
252 .PP
253 All long options are \f[B]non-portable extensions\f[R].
254 .SH STDOUT
255 .PP
256 Any non-error output is written to \f[B]stdout\f[R].
257 In addition, if history (see the \f[B]HISTORY\f[R] section) and the
258 prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output
259 to \f[B]stdout\f[R].
260 .PP
261 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
262 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
263 write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in
264 \f[B]bc >&-\f[R], it will quit with an error.
265 This is done so that bc(1) can report problems when \f[B]stdout\f[R] is
266 redirected to a file.
267 .PP
268 If there are scripts that depend on the behavior of other bc(1)
269 implementations, it is recommended that those scripts be changed to
270 redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R].
271 .SH STDERR
272 .PP
273 Any error output is written to \f[B]stderr\f[R].
274 .PP
275 \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will
276 issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot
277 write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in
278 \f[B]bc 2>&-\f[R], it will quit with an error.
279 This is done so that bc(1) can exit with an error code when
280 \f[B]stderr\f[R] is redirected to a file.
281 .PP
282 If there are scripts that depend on the behavior of other bc(1)
283 implementations, it is recommended that those scripts be changed to
284 redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R].
285 .SH SYNTAX
286 .PP
287 The syntax for bc(1) programs is mostly C-like, with some differences.
288 This bc(1) follows the POSIX
289 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
290 which is a much more thorough resource for the language this bc(1)
291 accepts.
292 This section is meant to be a summary and a listing of all the
293 extensions to the standard.
294 .PP
295 In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means
296 statement, and \f[B]I\f[R] means identifier.
297 .PP
298 Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be
299 followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase
300 letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores
301 (\f[B]_\f[R]).
302 The regex is \f[B][a-z][a-z0-9_]*\f[R].
303 Identifiers with more than one character (letter) are a
304 \f[B]non-portable extension\f[R].
305 .PP
306 \f[B]ibase\f[R] is a global variable determining how to interpret
307 constant numbers.
308 It is the \[dq]input\[dq] base, or the number base used for interpreting
309 input numbers.
310 \f[B]ibase\f[R] is initially \f[B]10\f[R].
311 If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R]
312 (\f[B]--warn\f[R]) flags were not given on the command line, the max
313 allowable value for \f[B]ibase\f[R] is \f[B]36\f[R].
314 Otherwise, it is \f[B]16\f[R].
315 The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R].
316 The max allowable value for \f[B]ibase\f[R] can be queried in bc(1)
317 programs with the \f[B]maxibase()\f[R] built-in function.
318 .PP
319 \f[B]obase\f[R] is a global variable determining how to output results.
320 It is the \[dq]output\[dq] base, or the number base used for outputting
321 numbers.
322 \f[B]obase\f[R] is initially \f[B]10\f[R].
323 The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and
324 can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in
325 function.
326 The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R].
327 Values are output in the specified base.
328 .PP
329 The \f[I]scale\f[R] of an expression is the number of digits in the
330 result of the expression right of the decimal point, and \f[B]scale\f[R]
331 is a global variable that sets the precision of any operations, with
332 exceptions.
333 \f[B]scale\f[R] is initially \f[B]0\f[R].
334 \f[B]scale\f[R] cannot be negative.
335 The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R]
336 and can be queried in bc(1) programs with the \f[B]maxscale()\f[R]
337 built-in function.
338 .PP
339 bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables.
340 All \f[I]local\f[R] variables are local to the function; they are
341 parameters or are introduced in the \f[B]auto\f[R] list of a function
342 (see the \f[B]FUNCTIONS\f[R] section).
343 If a variable is accessed which is not a parameter or in the
344 \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R].
345 If a parent function has a \f[I]local\f[R] variable version of a
346 variable that a child function considers \f[I]global\f[R], the value of
347 that \f[I]global\f[R] variable in the child function is the value of the
348 variable in the parent function, not the value of the actual
349 \f[I]global\f[R] variable.
350 .PP
351 All of the above applies to arrays as well.
352 .PP
353 The value of a statement that is an expression (i.e., any of the named
354 expressions or operands) is printed unless the lowest precedence
355 operator is an assignment operator \f[I]and\f[R] the expression is
356 notsurrounded by parentheses.
357 .PP
358 The value that is printed is also assigned to the special variable
359 \f[B]last\f[R].
360 A single dot (\f[B].\f[R]) may also be used as a synonym for
361 \f[B]last\f[R].
362 These are \f[B]non-portable extensions\f[R].
363 .PP
364 Either semicolons or newlines may separate statements.
365 .SS Comments
366 .PP
367 There are two kinds of comments:
368 .IP "1." 3
369 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R].
370 .IP "2." 3
371 Line comments go from \f[B]#\f[R] until, and not including, the next
372 newline.
373 This is a \f[B]non-portable extension\f[R].
374 .SS Named Expressions
375 .PP
376 The following are named expressions in bc(1):
377 .IP "1." 3
378 Variables: \f[B]I\f[R]
379 .IP "2." 3
380 Array Elements: \f[B]I[E]\f[R]
381 .IP "3." 3
382 \f[B]ibase\f[R]
383 .IP "4." 3
384 \f[B]obase\f[R]
385 .IP "5." 3
386 \f[B]scale\f[R]
387 .IP "6." 3
388 \f[B]last\f[R] or a single dot (\f[B].\f[R])
389 .PP
390 Number 6 is a \f[B]non-portable extension\f[R].
391 .PP
392 Variables and arrays do not interfere; users can have arrays named the
393 same as variables.
394 This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so
395 a user can have a variable, array, and function that all have the same
396 name, and they will not shadow each other, whether inside of functions
397 or not.
398 .PP
399 Named expressions are required as the operand of
400 \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side
401 of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R]
402 subsection).
403 .SS Operands
404 .PP
405 The following are valid operands in bc(1):
406 .IP " 1." 4
407 Numbers (see the \f[I]Numbers\f[R] subsection below).
408 .IP " 2." 4
409 Array indices (\f[B]I[E]\f[R]).
410 .IP " 3." 4
411 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence).
412 .IP " 4." 4
413 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R].
414 \f[B]E\f[R] must be non-negative.
415 .IP " 5." 4
416 \f[B]length(E)\f[R]: The number of significant decimal digits in
417 \f[B]E\f[R].
418 .IP " 6." 4
419 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R].
420 This is a \f[B]non-portable extension\f[R].
421 .IP " 7." 4
422 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R].
423 .IP " 8." 4
424 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R].
425 This is a \f[B]non-portable extension\f[R].
426 .IP " 9." 4
427 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
428 \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the
429 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
430 The \f[B]E\f[R] argument(s) may also be arrays of the form
431 \f[B]I[]\f[R], which will automatically be turned into array references
432 (see the \f[I]Array References\f[R] subsection of the
433 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
434 function definition is an array reference.
435 .IP "10." 4
436 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an
437 expression.
438 The result of that expression is the result of the \f[B]read()\f[R]
439 operand.
440 This is a \f[B]non-portable extension\f[R].
441 .IP "11." 4
442 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R].
443 This is a \f[B]non-portable extension\f[R].
444 .IP "12." 4
445 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R].
446 This is a \f[B]non-portable extension\f[R].
447 .IP "13." 4
448 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R].
449 This is a \f[B]non-portable extension\f[R].
450 .SS Numbers
451 .PP
452 Numbers are strings made up of digits, uppercase letters, and at most
453 \f[B]1\f[R] period for a radix.
454 Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits.
455 Uppercase letters are equal to \f[B]9\f[R] + their position in the
456 alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]).
457 If a digit or letter makes no sense with the current value of
458 \f[B]ibase\f[R], they are set to the value of the highest valid digit in
459 \f[B]ibase\f[R].
460 .PP
461 Single-character numbers (i.e., \f[B]A\f[R] alone) take the value that
462 they would have if they were valid digits, regardless of the value of
463 \f[B]ibase\f[R].
464 This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and
465 \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R].
466 .SS Operators
467 .PP
468 The following arithmetic and logical operators can be used.
469 They are listed in order of decreasing precedence.
470 Operators in the same group have the same precedence.
471 .PP
472 \f[B]++\f[R] \f[B]--\f[R]
473 .PP
474 : Type: Prefix and Postfix
475 .IP
476 .nf
477 \f[C]
478 Associativity: None
479
480 Description: **increment**, **decrement**
481 \f[R]
482 .fi
483 .PP
484 \f[B]-\f[R] \f[B]!\f[R]
485 .PP
486 : Type: Prefix
487 .IP
488 .nf
489 \f[C]
490 Associativity: None
491
492 Description: **negation**, **boolean not**
493 \f[R]
494 .fi
495 .PP
496 \f[B]\[ha]\f[R]
497 .PP
498 : Type: Binary
499 .IP
500 .nf
501 \f[C]
502 Associativity: Right
503
504 Description: **power**
505 \f[R]
506 .fi
507 .PP
508 \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R]
509 .PP
510 : Type: Binary
511 .IP
512 .nf
513 \f[C]
514 Associativity: Left
515
516 Description: **multiply**, **divide**, **modulus**
517 \f[R]
518 .fi
519 .PP
520 \f[B]+\f[R] \f[B]-\f[R]
521 .PP
522 : Type: Binary
523 .IP
524 .nf
525 \f[C]
526 Associativity: Left
527
528 Description: **add**, **subtract**
529 \f[R]
530 .fi
531 .PP
532 \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R]
533 \f[B]%=\f[R] \f[B]\[ha]=\f[R]
534 .PP
535 : Type: Binary
536 .IP
537 .nf
538 \f[C]
539 Associativity: Right
540
541 Description: **assignment**
542 \f[R]
543 .fi
544 .PP
545 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R]
546 \f[B]>\f[R]
547 .PP
548 : Type: Binary
549 .IP
550 .nf
551 \f[C]
552 Associativity: Left
553
554 Description: **relational**
555 \f[R]
556 .fi
557 .PP
558 \f[B]&&\f[R]
559 .PP
560 : Type: Binary
561 .IP
562 .nf
563 \f[C]
564 Associativity: Left
565
566 Description: **boolean and**
567 \f[R]
568 .fi
569 .PP
570 \f[B]||\f[R]
571 .PP
572 : Type: Binary
573 .IP
574 .nf
575 \f[C]
576 Associativity: Left
577
578 Description: **boolean or**
579 \f[R]
580 .fi
581 .PP
582 The operators will be described in more detail below.
583 .PP
584 \f[B]++\f[R] \f[B]--\f[R]
585 .PP
586 : The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R]
587 operators behave exactly like they would in C.
588 They require a named expression (see the \f[I]Named Expressions\f[R]
589 subsection) as an operand.
590 .IP
591 .nf
592 \f[C]
593 The prefix versions of these operators are more efficient; use them where
594 possible.
595 \f[R]
596 .fi
597 .PP
598 \f[B]-\f[R]
599 .PP
600 : The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts
601 to negate any expression with the value \f[B]0\f[R].
602 Otherwise, a copy of the expression with its sign flipped is returned.
603 .PP
604 \f[B]!\f[R]
605 .PP
606 : The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the
607 expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise.
608 .IP
609 .nf
610 \f[C]
611 This is a **non-portable extension**.
612 \f[R]
613 .fi
614 .PP
615 \f[B]\[ha]\f[R]
616 .PP
617 : The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator,
618 as it would be in C) takes two expressions and raises the first to the
619 power of the value of the second.
620 The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R].
621 .IP
622 .nf
623 \f[C]
624 The second expression must be an integer (no *scale*), and if it is
625 negative, the first value must be non-zero.
626 \f[R]
627 .fi
628 .PP
629 \f[B]*\f[R]
630 .PP
631 : The \f[B]multiply\f[R] operator takes two expressions, multiplies
632 them, and returns the product.
633 If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and
634 \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the
635 \f[I]scale\f[R] of the result is equal to
636 \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and
637 \f[B]max()\f[R] return the obvious values.
638 .PP
639 \f[B]/\f[R]
640 .PP
641 : The \f[B]divide\f[R] operator takes two expressions, divides them, and
642 returns the quotient.
643 The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R].
644 .IP
645 .nf
646 \f[C]
647 The second expression must be non-zero.
648 \f[R]
649 .fi
650 .PP
651 \f[B]%\f[R]
652 .PP
653 : The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and
654 \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current
655 \f[B]scale\f[R] and 2) Using the result of step 1 to calculate
656 \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R]
657 \f[B]max(scale+scale(b),scale(a))\f[R].
658 .IP
659 .nf
660 \f[C]
661 The second expression must be non-zero.
662 \f[R]
663 .fi
664 .PP
665 \f[B]+\f[R]
666 .PP
667 : The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and
668 \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the
669 max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
670 .PP
671 \f[B]-\f[R]
672 .PP
673 : The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and
674 \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to
675 the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R].
676 .PP
677 \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R]
678 \f[B]%=\f[R] \f[B]\[ha]=\f[R]
679 .PP
680 : The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R]
681 and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the
682 \f[I]Named Expressions\f[R] subsection).
683 .IP
684 .nf
685 \f[C]
686 For **=**, **b** is copied and the result is assigned to **a**. For all
687 others, **a** and **b** are applied as operands to the corresponding
688 arithmetic operator and the result is assigned to **a**.
689 \f[R]
690 .fi
691 .PP
692 \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R]
693 \f[B]>\f[R]
694 .PP
695 : The \f[B]relational\f[R] operators compare two expressions,
696 \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C
697 language semantics, the result is \f[B]1\f[R].
698 Otherwise, it is \f[B]0\f[R].
699 .IP
700 .nf
701 \f[C]
702 Note that unlike in C, these operators have a lower precedence than the
703 **assignment** operators, which means that **a=b\[rs]>c** is interpreted as
704 **(a=b)\[rs]>c**.
705
706 Also, unlike the [standard][1] requires, these operators can appear anywhere
707 any other expressions can be used. This allowance is a
708 **non-portable extension**.
709 \f[R]
710 .fi
711 .PP
712 \f[B]&&\f[R]
713 .PP
714 : The \f[B]boolean and\f[R] operator takes two expressions and returns
715 \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise.
716 .IP
717 .nf
718 \f[C]
719 This is *not* a short-circuit operator.
720
721 This is a **non-portable extension**.
722 \f[R]
723 .fi
724 .PP
725 \f[B]||\f[R]
726 .PP
727 : The \f[B]boolean or\f[R] operator takes two expressions and returns
728 \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R]
729 otherwise.
730 .IP
731 .nf
732 \f[C]
733 This is *not* a short-circuit operator.
734
735 This is a **non-portable extension**.
736 \f[R]
737 .fi
738 .SS Statements
739 .PP
740 The following items are statements:
741 .IP " 1." 4
742 \f[B]E\f[R]
743 .IP " 2." 4
744 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] ...
745 \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R]
746 .IP " 3." 4
747 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
748 .IP " 4." 4
749 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
750 \f[B]else\f[R] \f[B]S\f[R]
751 .IP " 5." 4
752 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
753 .IP " 6." 4
754 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R]
755 \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R]
756 .IP " 7." 4
757 An empty statement
758 .IP " 8." 4
759 \f[B]break\f[R]
760 .IP " 9." 4
761 \f[B]continue\f[R]
762 .IP "10." 4
763 \f[B]quit\f[R]
764 .IP "11." 4
765 \f[B]halt\f[R]
766 .IP "12." 4
767 \f[B]limits\f[R]
768 .IP "13." 4
769 A string of characters, enclosed in double quotes
770 .IP "14." 4
771 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] ...
772 \f[B],\f[R] \f[B]E\f[R]
773 .IP "15." 4
774 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where
775 \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the
776 \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section).
777 The \f[B]E\f[R] argument(s) may also be arrays of the form
778 \f[B]I[]\f[R], which will automatically be turned into array references
779 (see the \f[I]Array References\f[R] subsection of the
780 \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the
781 function definition is an array reference.
782 .PP
783 Numbers 4, 9, 11, 12, 14, and 15 are \f[B]non-portable extensions\f[R].
784 .PP
785 Also, as a \f[B]non-portable extension\f[R], any or all of the
786 expressions in the header of a for loop may be omitted.
787 If the condition (second expression) is omitted, it is assumed to be a
788 constant \f[B]1\f[R].
789 .PP
790 The \f[B]break\f[R] statement causes a loop to stop iterating and resume
791 execution immediately following a loop.
792 This is only allowed in loops.
793 .PP
794 The \f[B]continue\f[R] statement causes a loop iteration to stop early
795 and returns to the start of the loop, including testing the loop
796 condition.
797 This is only allowed in loops.
798 .PP
799 The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C.
800 .PP
801 The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a
802 branch that will not be executed (it is a compile-time command).
803 .PP
804 The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed.
805 (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement
806 that is not executed, bc(1) does not quit.)
807 .PP
808 The \f[B]limits\f[R] statement prints the limits that this bc(1) is
809 subject to.
810 This is like the \f[B]quit\f[R] statement in that it is a compile-time
811 command.
812 .PP
813 An expression by itself is evaluated and printed, followed by a newline.
814 .SS Print Statement
815 .PP
816 The \[dq]expressions\[dq] in a \f[B]print\f[R] statement may also be
817 strings.
818 If they are, there are backslash escape sequences that are interpreted
819 specially.
820 What those sequences are, and what they cause to be printed, are shown
821 below:
822 .PP
823    *   *   *   *   *
824 .PP
825 \f[B]\[rs]a\f[R] \f[B]\[rs]a\f[R] \f[B]\[rs]b\f[R] \f[B]\[rs]b\f[R]
826 \f[B]\[rs]\[rs]\f[R] \f[B]\[rs]\f[R] \f[B]\[rs]e\f[R] \f[B]\[rs]\f[R]
827 \f[B]\[rs]f\f[R] \f[B]\[rs]f\f[R] \f[B]\[rs]n\f[R] \f[B]\[rs]n\f[R]
828 \f[B]\[rs]q\f[R] \f[B]\[dq]\f[R] \f[B]\[rs]r\f[R] \f[B]\[rs]r\f[R]
829 \f[B]\[rs]t\f[R] \f[B]\[rs]t\f[R]
830 .PP
831    *   *   *   *   *
832 .PP
833 Any other character following a backslash causes the backslash and
834 character to be printed as-is.
835 .PP
836 Any non-string expression in a print statement shall be assigned to
837 \f[B]last\f[R], like any other expression that is printed.
838 .SS Order of Evaluation
839 .PP
840 All expressions in a statment are evaluated left to right, except as
841 necessary to maintain order of operations.
842 This means, for example, assuming that \f[B]i\f[R] is equal to
843 \f[B]0\f[R], in the expression
844 .IP
845 .nf
846 \f[C]
847 a[i++] = i++
848 \f[R]
849 .fi
850 .PP
851 the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and
852 \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression.
853 .PP
854 This includes function arguments.
855 Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in
856 the expression
857 .IP
858 .nf
859 \f[C]
860 x(i++, i++)
861 \f[R]
862 .fi
863 .PP
864 the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the
865 second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to
866 \f[B]2\f[R] before the function starts executing.
867 .SH FUNCTIONS
868 .PP
869 Function definitions are as follows:
870 .IP
871 .nf
872 \f[C]
873 define I(I,...,I){
874     auto I,...,I
875     S;...;S
876     return(E)
877 }
878 \f[R]
879 .fi
880 .PP
881 Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be
882 replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an
883 array, and any \f[B]I\f[R] in the parameter list may be replaced with
884 \f[B]*I[]\f[R] to make a parameter an array reference.
885 Callers of functions that take array references should not put an
886 asterisk in the call; they must be called with just \f[B]I[]\f[R] like
887 normal array parameters and will be automatically converted into
888 references.
889 .PP
890 As a \f[B]non-portable extension\f[R], the opening brace of a
891 \f[B]define\f[R] statement may appear on the next line.
892 .PP
893 As a \f[B]non-portable extension\f[R], the return statement may also be
894 in one of the following forms:
895 .IP "1." 3
896 \f[B]return\f[R]
897 .IP "2." 3
898 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R]
899 .IP "3." 3
900 \f[B]return\f[R] \f[B]E\f[R]
901 .PP
902 The first two, or not specifying a \f[B]return\f[R] statement, is
903 equivalent to \f[B]return (0)\f[R], unless the function is a
904 \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection
905 below).
906 .SS Void Functions
907 .PP
908 Functions can also be \f[B]void\f[R] functions, defined as follows:
909 .IP
910 .nf
911 \f[C]
912 define void I(I,...,I){
913     auto I,...,I
914     S;...;S
915     return
916 }
917 \f[R]
918 .fi
919 .PP
920 They can only be used as standalone expressions, where such an
921 expression would be printed alone, except in a print statement.
922 .PP
923 Void functions can only use the first two \f[B]return\f[R] statements
924 listed above.
925 They can also omit the return statement entirely.
926 .PP
927 The word \[dq]void\[dq] is not treated as a keyword; it is still
928 possible to have variables, arrays, and functions named \f[B]void\f[R].
929 The word \[dq]void\[dq] is only treated specially right after the
930 \f[B]define\f[R] keyword.
931 .PP
932 This is a \f[B]non-portable extension\f[R].
933 .SS Array References
934 .PP
935 For any array in the parameter list, if the array is declared in the
936 form
937 .IP
938 .nf
939 \f[C]
940 *I[]
941 \f[R]
942 .fi
943 .PP
944 it is a \f[B]reference\f[R].
945 Any changes to the array in the function are reflected, when the
946 function returns, to the array that was passed in.
947 .PP
948 Other than this, all function arguments are passed by value.
949 .PP
950 This is a \f[B]non-portable extension\f[R].
951 .SH LIBRARY
952 .PP
953 All of the functions below are available when the \f[B]-l\f[R] or
954 \f[B]--mathlib\f[R] command-line flags are given.
955 .SS Standard Library
956 .PP
957 The
958 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
959 defines the following functions for the math library:
960 .PP
961 \f[B]s(x)\f[R]
962 .PP
963 : Returns the sine of \f[B]x\f[R], which is assumed to be in radians.
964 .IP
965 .nf
966 \f[C]
967 This is a transcendental function (see the *Transcendental Functions*
968 subsection below).
969 \f[R]
970 .fi
971 .PP
972 \f[B]c(x)\f[R]
973 .PP
974 : Returns the cosine of \f[B]x\f[R], which is assumed to be in radians.
975 .IP
976 .nf
977 \f[C]
978 This is a transcendental function (see the *Transcendental Functions*
979 subsection below).
980 \f[R]
981 .fi
982 .PP
983 \f[B]a(x)\f[R]
984 .PP
985 : Returns the arctangent of \f[B]x\f[R], in radians.
986 .IP
987 .nf
988 \f[C]
989 This is a transcendental function (see the *Transcendental Functions*
990 subsection below).
991 \f[R]
992 .fi
993 .PP
994 \f[B]l(x)\f[R]
995 .PP
996 : Returns the natural logarithm of \f[B]x\f[R].
997 .IP
998 .nf
999 \f[C]
1000 This is a transcendental function (see the *Transcendental Functions*
1001 subsection below).
1002 \f[R]
1003 .fi
1004 .PP
1005 \f[B]e(x)\f[R]
1006 .PP
1007 : Returns the mathematical constant \f[B]e\f[R] raised to the power of
1008 \f[B]x\f[R].
1009 .IP
1010 .nf
1011 \f[C]
1012 This is a transcendental function (see the *Transcendental Functions*
1013 subsection below).
1014 \f[R]
1015 .fi
1016 .PP
1017 \f[B]j(x, n)\f[R]
1018 .PP
1019 : Returns the bessel integer order \f[B]n\f[R] (truncated) of
1020 \f[B]x\f[R].
1021 .IP
1022 .nf
1023 \f[C]
1024 This is a transcendental function (see the *Transcendental Functions*
1025 subsection below).
1026 \f[R]
1027 .fi
1028 .SS Transcendental Functions
1029 .PP
1030 All transcendental functions can return slightly inaccurate results (up
1031 to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place)).
1032 This is unavoidable, and this
1033 article (https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT) explains
1034 why it is impossible and unnecessary to calculate exact results for the
1035 transcendental functions.
1036 .PP
1037 Because of the possible inaccuracy, I recommend that users call those
1038 functions with the precision (\f[B]scale\f[R]) set to at least 1 higher
1039 than is necessary.
1040 If exact results are \f[I]absolutely\f[R] required, users can double the
1041 precision (\f[B]scale\f[R]) and then truncate.
1042 .PP
1043 The transcendental functions in the standard math library are:
1044 .IP \[bu] 2
1045 \f[B]s(x)\f[R]
1046 .IP \[bu] 2
1047 \f[B]c(x)\f[R]
1048 .IP \[bu] 2
1049 \f[B]a(x)\f[R]
1050 .IP \[bu] 2
1051 \f[B]l(x)\f[R]
1052 .IP \[bu] 2
1053 \f[B]e(x)\f[R]
1054 .IP \[bu] 2
1055 \f[B]j(x, n)\f[R]
1056 .SH RESET
1057 .PP
1058 When bc(1) encounters an error or a signal that it has a non-default
1059 handler for, it resets.
1060 This means that several things happen.
1061 .PP
1062 First, any functions that are executing are stopped and popped off the
1063 stack.
1064 The behavior is not unlike that of exceptions in programming languages.
1065 Then the execution point is set so that any code waiting to execute
1066 (after all functions returned) is skipped.
1067 .PP
1068 Thus, when bc(1) resets, it skips any remaining code waiting to be
1069 executed.
1070 Then, if it is interactive mode, and the error was not a fatal error
1071 (see the \f[B]EXIT STATUS\f[R] section), it asks for more input;
1072 otherwise, it exits with the appropriate return code.
1073 .PP
1074 Note that this reset behavior is different from the GNU bc(1), which
1075 attempts to start executing the statement right after the one that
1076 caused an error.
1077 .SH PERFORMANCE
1078 .PP
1079 Most bc(1) implementations use \f[B]char\f[R] types to calculate the
1080 value of \f[B]1\f[R] decimal digit at a time, but that can be slow.
1081 This bc(1) does something different.
1082 .PP
1083 It uses large integers to calculate more than \f[B]1\f[R] decimal digit
1084 at a time.
1085 If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the
1086 \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has
1087 \f[B]9\f[R] decimal digits.
1088 If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R]
1089 then each integer has \f[B]4\f[R] decimal digits.
1090 This value (the number of decimal digits per large integer) is called
1091 \f[B]BC_BASE_DIGS\f[R].
1092 .PP
1093 The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R]
1094 can be queried with the \f[B]limits\f[R] statement.
1095 .PP
1096 In addition, this bc(1) uses an even larger integer for overflow
1097 checking.
1098 This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is
1099 always at least twice as large as the integer type used to store digits.
1100 .SH LIMITS
1101 .PP
1102 The following are the limits on bc(1):
1103 .PP
1104 \f[B]BC_LONG_BIT\f[R]
1105 .PP
1106 : The number of bits in the \f[B]long\f[R] type in the environment where
1107 bc(1) was built.
1108 This determines how many decimal digits can be stored in a single large
1109 integer (see the \f[B]PERFORMANCE\f[R] section).
1110 .PP
1111 \f[B]BC_BASE_DIGS\f[R]
1112 .PP
1113 : The number of decimal digits per large integer (see the
1114 \f[B]PERFORMANCE\f[R] section).
1115 Depends on \f[B]BC_LONG_BIT\f[R].
1116 .PP
1117 \f[B]BC_BASE_POW\f[R]
1118 .PP
1119 : The max decimal number that each large integer can store (see
1120 \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R].
1121 Depends on \f[B]BC_BASE_DIGS\f[R].
1122 .PP
1123 \f[B]BC_OVERFLOW_MAX\f[R]
1124 .PP
1125 : The max number that the overflow type (see the \f[B]PERFORMANCE\f[R]
1126 section) can hold.
1127 Depends on \f[B]BC_LONG_BIT\f[R].
1128 .PP
1129 \f[B]BC_BASE_MAX\f[R]
1130 .PP
1131 : The maximum output base.
1132 Set at \f[B]BC_BASE_POW\f[R].
1133 .PP
1134 \f[B]BC_DIM_MAX\f[R]
1135 .PP
1136 : The maximum size of arrays.
1137 Set at \f[B]SIZE_MAX-1\f[R].
1138 .PP
1139 \f[B]BC_SCALE_MAX\f[R]
1140 .PP
1141 : The maximum \f[B]scale\f[R].
1142 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1143 .PP
1144 \f[B]BC_STRING_MAX\f[R]
1145 .PP
1146 : The maximum length of strings.
1147 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1148 .PP
1149 \f[B]BC_NAME_MAX\f[R]
1150 .PP
1151 : The maximum length of identifiers.
1152 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1153 .PP
1154 \f[B]BC_NUM_MAX\f[R]
1155 .PP
1156 : The maximum length of a number (in decimal digits), which includes
1157 digits after the decimal point.
1158 Set at \f[B]BC_OVERFLOW_MAX-1\f[R].
1159 .PP
1160 Exponent
1161 .PP
1162 : The maximum allowable exponent (positive or negative).
1163 Set at \f[B]BC_OVERFLOW_MAX\f[R].
1164 .PP
1165 Number of vars
1166 .PP
1167 : The maximum number of vars/arrays.
1168 Set at \f[B]SIZE_MAX-1\f[R].
1169 .PP
1170 The actual values can be queried with the \f[B]limits\f[R] statement.
1171 .PP
1172 These limits are meant to be effectively non-existent; the limits are so
1173 large (at least on 64-bit machines) that there should not be any point
1174 at which they become a problem.
1175 In fact, memory should be exhausted before these limits should be hit.
1176 .SH ENVIRONMENT VARIABLES
1177 .PP
1178 bc(1) recognizes the following environment variables:
1179 .PP
1180 \f[B]POSIXLY_CORRECT\f[R]
1181 .PP
1182 : If this variable exists (no matter the contents), bc(1) behaves as if
1183 the \f[B]-s\f[R] option was given.
1184 .PP
1185 \f[B]BC_ENV_ARGS\f[R]
1186 .PP
1187 : This is another way to give command-line arguments to bc(1).
1188 They should be in the same format as all other command-line arguments.
1189 These are always processed first, so any files given in
1190 \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given
1191 on the command-line.
1192 This gives the user the ability to set up \[dq]standard\[dq] options and
1193 files to be used at every invocation.
1194 The most useful thing for such files to contain would be useful
1195 functions that the user might want every time bc(1) runs.
1196 .IP
1197 .nf
1198 \f[C]
1199 The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments,
1200 but it does not understand escape sequences. For example, the string
1201 **\[dq]/home/gavin/some bc file.bc\[dq]** will be correctly parsed, but the string
1202 **\[dq]/home/gavin/some \[rs]\[dq]bc\[rs]\[dq] file.bc\[dq]** will include the backslashes.
1203
1204 The quote parsing will handle either kind of quotes, **\[aq]** or **\[dq]**. Thus,
1205 if you have a file with any number of single quotes in the name, you can use
1206 double quotes as the outside quotes, as in **\[dq]some \[aq]bc\[aq] file.bc\[dq]**, and vice
1207 versa if you have a file with double quotes. However, handling a file with
1208 both kinds of quotes in **BC_ENV_ARGS** is not supported due to the
1209 complexity of the parsing, though such files are still supported on the
1210 command-line where the parsing is done by the shell.
1211 \f[R]
1212 .fi
1213 .PP
1214 \f[B]BC_LINE_LENGTH\f[R]
1215 .PP
1216 : If this environment variable exists and contains an integer that is
1217 greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R]
1218 (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length,
1219 including the backslash (\f[B]\[rs]\f[R]).
1220 The default line length is \f[B]70\f[R].
1221 .SH EXIT STATUS
1222 .PP
1223 bc(1) returns the following exit statuses:
1224 .PP
1225 \f[B]0\f[R]
1226 .PP
1227 : No error.
1228 .PP
1229 \f[B]1\f[R]
1230 .PP
1231 : A math error occurred.
1232 This follows standard practice of using \f[B]1\f[R] for expected errors,
1233 since math errors will happen in the process of normal execution.
1234 .IP
1235 .nf
1236 \f[C]
1237 Math errors include divide by **0**, taking the square root of a negative
1238 number, attempting to convert a negative number to a hardware integer,
1239 overflow when converting a number to a hardware integer, and attempting to
1240 use a non-integer where an integer is required.
1241
1242 Converting to a hardware integer happens for the second operand of the power
1243 (**\[rs]\[ha]**) operator and the corresponding assignment operator.
1244 \f[R]
1245 .fi
1246 .PP
1247 \f[B]2\f[R]
1248 .PP
1249 : A parse error occurred.
1250 .IP
1251 .nf
1252 \f[C]
1253 Parse errors include unexpected **EOF**, using an invalid character, failing
1254 to find the end of a string or comment, using a token where it is invalid,
1255 giving an invalid expression, giving an invalid print statement, giving an
1256 invalid function definition, attempting to assign to an expression that is
1257 not a named expression (see the *Named Expressions* subsection of the
1258 **SYNTAX** section), giving an invalid **auto** list, having a duplicate
1259 **auto**/function parameter, failing to find the end of a code block,
1260 attempting to return a value from a **void** function, attempting to use a
1261 variable as a reference, and using any extensions when the option **-s** or
1262 any equivalents were given.
1263 \f[R]
1264 .fi
1265 .PP
1266 \f[B]3\f[R]
1267 .PP
1268 : A runtime error occurred.
1269 .IP
1270 .nf
1271 \f[C]
1272 Runtime errors include assigning an invalid number to **ibase**, **obase**,
1273 or **scale**; give a bad expression to a **read()** call, calling **read()**
1274 inside of a **read()** call, type errors, passing the wrong number of
1275 arguments to functions, attempting to call an undefined function, and
1276 attempting to use a **void** function call as a value in an expression.
1277 \f[R]
1278 .fi
1279 .PP
1280 \f[B]4\f[R]
1281 .PP
1282 : A fatal error occurred.
1283 .IP
1284 .nf
1285 \f[C]
1286 Fatal errors include memory allocation errors, I/O errors, failing to open
1287 files, attempting to use files that do not have only ASCII characters (bc(1)
1288 only accepts ASCII characters), attempting to open a directory as a file,
1289 and giving invalid command-line options.
1290 \f[R]
1291 .fi
1292 .PP
1293 The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1)
1294 always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in.
1295 .PP
1296 The other statuses will only be returned when bc(1) is not in
1297 interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since
1298 bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts
1299 more input when one of those errors occurs in interactive mode.
1300 This is also the case when interactive mode is forced by the
1301 \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option.
1302 .PP
1303 These exit statuses allow bc(1) to be used in shell scripting with error
1304 checking, and its normal behavior can be forced by using the
1305 \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option.
1306 .SH INTERACTIVE MODE
1307 .PP
1308 Per the
1309 standard (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1310 bc(1) has an interactive mode and a non-interactive mode.
1311 Interactive mode is turned on automatically when both \f[B]stdin\f[R]
1312 and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag
1313 and \f[B]--interactive\f[R] option can turn it on in other cases.
1314 .PP
1315 In interactive mode, bc(1) attempts to recover from errors (see the
1316 \f[B]RESET\f[R] section), and in normal execution, flushes
1317 \f[B]stdout\f[R] as soon as execution is done for the current input.
1318 .SH TTY MODE
1319 .PP
1320 If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all
1321 connected to a TTY, bc(1) turns on \[dq]TTY mode.\[dq]
1322 .PP
1323 TTY mode is different from interactive mode because interactive mode is
1324 required in the bc(1)
1325 specification (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1326 and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R]
1327 to be connected to a terminal.
1328 .SH SIGNAL HANDLING
1329 .PP
1330 Sending a \f[B]SIGINT\f[R] will cause bc(1) to stop execution of the
1331 current input.
1332 If bc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), it will
1333 reset (see the \f[B]RESET\f[R] section).
1334 Otherwise, it will clean up and exit.
1335 .PP
1336 Note that \[dq]current input\[dq] can mean one of two things.
1337 If bc(1) is processing input from \f[B]stdin\f[R] in TTY mode, it will
1338 ask for more input.
1339 If bc(1) is processing input from a file in TTY mode, it will stop
1340 processing the file and start processing the next file, if one exists,
1341 or ask for input from \f[B]stdin\f[R] if no other file exists.
1342 .PP
1343 This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is
1344 executing a file, it can seem as though bc(1) did not respond to the
1345 signal since it will immediately start executing the next file.
1346 This is by design; most files that users execute when interacting with
1347 bc(1) have function definitions, which are quick to parse.
1348 If a file takes a long time to execute, there may be a bug in that file.
1349 The rest of the files could still be executed without problem, allowing
1350 the user to continue.
1351 .PP
1352 \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and
1353 exit, and it uses the default handler for all other signals.
1354 .SH LOCALES
1355 .PP
1356 This bc(1) ships with support for adding error messages for different
1357 locales and thus, supports \f[B]LC_MESSAGES\f[R].
1358 .SH SEE ALSO
1359 .PP
1360 dc(1)
1361 .SH STANDARDS
1362 .PP
1363 bc(1) is compliant with the IEEE Std 1003.1-2017
1364 (\[lq]POSIX.1-2017\[rq]) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1365 specification.
1366 The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions
1367 noted above are extensions to that specification.
1368 .PP
1369 Note that the specification explicitly says that bc(1) only accepts
1370 numbers that use a period (\f[B].\f[R]) as a radix point, regardless of
1371 the value of \f[B]LC_NUMERIC\f[R].
1372 .PP
1373 This bc(1) supports error messages for different locales, and thus, it
1374 supports \f[B]LC_MESSAGES\f[R].
1375 .SH BUGS
1376 .PP
1377 None are known.
1378 Report bugs at https://git.yzena.com/gavin/bc.
1379 .SH AUTHORS
1380 .PP
1381 Gavin D.
1382 Howard <gavin@yzena.com> and contributors.