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