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