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