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