]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/manuals/dc/H.1
Update to bmake-20200704
[FreeBSD/FreeBSD.git] / contrib / bc / manuals / dc / H.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 "DC" "1" "July 2020" "Gavin D. Howard" "General Commands Manual"
29 .SH Name
30 .PP
31 dc \- arbitrary\-precision reverse\-Polish notation calculator
32 .SH SYNOPSIS
33 .PP
34 \f[B]dc\f[] [\f[B]\-hiPvVx\f[]] [\f[B]\-\-version\f[]]
35 [\f[B]\-\-help\f[]] [\f[B]\-\-interactive\f[]] [\f[B]\-\-no\-prompt\f[]]
36 [\f[B]\-\-extended\-register\f[]] [\f[B]\-e\f[] \f[I]expr\f[]]
37 [\f[B]\-\-expression\f[]=\f[I]expr\f[]...] [\f[B]\-f\f[]
38 \f[I]file\f[]...] [\f[B]\-file\f[]=\f[I]file\f[]...] [\f[I]file\f[]...]
39 .SH DESCRIPTION
40 .PP
41 dc(1) is an arbitrary\-precision calculator.
42 It uses a stack (reverse Polish notation) to store numbers and results
43 of computations.
44 Arithmetic operations pop arguments off of the stack and push the
45 results.
46 .PP
47 If no files are given on the command\-line as extra arguments (i.e., not
48 as \f[B]\-f\f[] or \f[B]\-\-file\f[] arguments), then dc(1) reads from
49 \f[B]stdin\f[].
50 Otherwise, those files are processed, and dc(1) will then exit.
51 .PP
52 This is different from the dc(1) on OpenBSD and possibly other dc(1)
53 implementations, where \f[B]\-e\f[] (\f[B]\-\-expression\f[]) and
54 \f[B]\-f\f[] (\f[B]\-\-file\f[]) arguments cause dc(1) to execute them
55 and exit.
56 The reason for this is that this dc(1) allows users to set arguments in
57 the environment variable \f[B]DC_ENV_ARGS\f[] (see the \f[B]ENVIRONMENT
58 VARIABLES\f[] section).
59 Any expressions given on the command\-line should be used to set up a
60 standard environment.
61 For example, if a user wants the \f[B]scale\f[] always set to
62 \f[B]10\f[], they can set \f[B]DC_ENV_ARGS\f[] to \f[B]\-e 10k\f[], and
63 this dc(1) will always start with a \f[B]scale\f[] of \f[B]10\f[].
64 .PP
65 If users want to have dc(1) exit after processing all input from
66 \f[B]\-e\f[] and \f[B]\-f\f[] arguments (and their equivalents), then
67 they can just simply add \f[B]\-e q\f[] as the last command\-line
68 argument or define the environment variable \f[B]DC_EXPR_EXIT\f[].
69 .SH OPTIONS
70 .PP
71 The following are the options that dc(1) accepts.
72 .TP
73 .B \f[B]\-h\f[], \f[B]\-\-help\f[]
74 Prints a usage message and quits.
75 .RS
76 .RE
77 .TP
78 .B \f[B]\-v\f[], \f[B]\-V\f[], \f[B]\-\-version\f[]
79 Print the version information (copyright header) and exit.
80 .RS
81 .RE
82 .TP
83 .B \f[B]\-i\f[], \f[B]\-\-interactive\f[]
84 Forces interactive mode.
85 (See the \f[B]INTERACTIVE MODE\f[] section.)
86 .RS
87 .PP
88 This is a \f[B]non\-portable extension\f[].
89 .RE
90 .TP
91 .B \f[B]\-P\f[], \f[B]\-\-no\-prompt\f[]
92 Disables the prompt in TTY mode.
93 (The prompt is only enabled in TTY mode.
94 See the \f[B]TTY MODE\f[] section) This is mostly for those users that
95 do not want a prompt or are not used to having them in dc(1).
96 Most of those users would want to put this option in
97 \f[B]DC_ENV_ARGS\f[].
98 .RS
99 .PP
100 This is a \f[B]non\-portable extension\f[].
101 .RE
102 .TP
103 .B \f[B]\-x\f[] \f[B]\-\-extended\-register\f[]
104 Enables extended register mode.
105 See the \f[I]Extended Register Mode\f[] subsection of the
106 \f[B]REGISTERS\f[] section for more information.
107 .RS
108 .PP
109 This is a \f[B]non\-portable extension\f[].
110 .RE
111 .TP
112 .B \f[B]\-e\f[] \f[I]expr\f[], \f[B]\-\-expression\f[]=\f[I]expr\f[]
113 Evaluates \f[I]expr\f[].
114 If multiple expressions are given, they are evaluated in order.
115 If files are given as well (see below), the expressions and files are
116 evaluated in the order given.
117 This means that if a file is given before an expression, the file is
118 read in and evaluated first.
119 .RS
120 .PP
121 In other dc(1) implementations, this option causes the program to
122 execute the expressions and then exit.
123 This dc(1) does not, unless the \f[B]DC_EXPR_EXIT\f[] is defined (see
124 the \f[B]ENVIRONMENT VARIABLES\f[] section).
125 .PP
126 This is a \f[B]non\-portable extension\f[].
127 .RE
128 .TP
129 .B \f[B]\-f\f[] \f[I]file\f[], \f[B]\-\-file\f[]=\f[I]file\f[]
130 Reads in \f[I]file\f[] and evaluates it, line by line, as though it were
131 read through \f[B]stdin\f[].
132 If expressions are also given (see above), the expressions are evaluated
133 in the order given.
134 .RS
135 .PP
136 In other dc(1) implementations, this option causes the program to
137 execute the files and then exit.
138 This dc(1) does not, unless the \f[B]DC_EXPR_EXIT\f[] is defined (see
139 the \f[B]ENVIRONMENT VARIABLES\f[] section).
140 .PP
141 This is a \f[B]non\-portable extension\f[].
142 .RE
143 .PP
144 All long options are \f[B]non\-portable extensions\f[].
145 .SH STDOUT
146 .PP
147 Any non\-error output is written to \f[B]stdout\f[].
148 .PP
149 \f[B]Note\f[]: Unlike other dc(1) implementations, this dc(1) will issue
150 a fatal error (see the \f[B]EXIT STATUS\f[] section) if it cannot write
151 to \f[B]stdout\f[], so if \f[B]stdout\f[] is closed, as in \f[B]dc
152 >&\-\f[], it will quit with an error.
153 This is done so that dc(1) can report problems when \f[B]stdout\f[] is
154 redirected to a file.
155 .PP
156 If there are scripts that depend on the behavior of other dc(1)
157 implementations, it is recommended that those scripts be changed to
158 redirect \f[B]stdout\f[] to \f[B]/dev/null\f[].
159 .SH STDERR
160 .PP
161 Any error output is written to \f[B]stderr\f[].
162 .PP
163 \f[B]Note\f[]: Unlike other dc(1) implementations, this dc(1) will issue
164 a fatal error (see the \f[B]EXIT STATUS\f[] section) if it cannot write
165 to \f[B]stderr\f[], so if \f[B]stderr\f[] is closed, as in \f[B]dc
166 2>&\-\f[], it will quit with an error.
167 This is done so that dc(1) can exit with an error code when
168 \f[B]stderr\f[] is redirected to a file.
169 .PP
170 If there are scripts that depend on the behavior of other dc(1)
171 implementations, it is recommended that those scripts be changed to
172 redirect \f[B]stderr\f[] to \f[B]/dev/null\f[].
173 .SH SYNTAX
174 .PP
175 Each item in the input source code, either a number (see the
176 \f[B]NUMBERS\f[] section) or a command (see the \f[B]COMMANDS\f[]
177 section), is processed and executed, in order.
178 Input is processed immediately when entered.
179 .PP
180 \f[B]ibase\f[] is a register (see the \f[B]REGISTERS\f[] section) that
181 determines how to interpret constant numbers.
182 It is the "input" base, or the number base used for interpreting input
183 numbers.
184 \f[B]ibase\f[] is initially \f[B]10\f[].
185 The max allowable value for \f[B]ibase\f[] is \f[B]16\f[].
186 The min allowable value for \f[B]ibase\f[] is \f[B]2\f[].
187 The max allowable value for \f[B]ibase\f[] can be queried in dc(1)
188 programs with the \f[B]T\f[] command.
189 .PP
190 \f[B]obase\f[] is a register (see the \f[B]REGISTERS\f[] section) that
191 determines how to output results.
192 It is the "output" base, or the number base used for outputting numbers.
193 \f[B]obase\f[] is initially \f[B]10\f[].
194 The max allowable value for \f[B]obase\f[] is \f[B]DC_BASE_MAX\f[] and
195 can be queried with the \f[B]U\f[] command.
196 The min allowable value for \f[B]obase\f[] is \f[B]0\f[].
197 If \f[B]obase\f[] is \f[B]0\f[], values are output in scientific
198 notation, and if \f[B]obase\f[] is \f[B]1\f[], values are output in
199 engineering notation.
200 Otherwise, values are output in the specified base.
201 .PP
202 Outputting in scientific and engineering notations are
203 \f[B]non\-portable extensions\f[].
204 .PP
205 The \f[I]scale\f[] of an expression is the number of digits in the
206 result of the expression right of the decimal point, and \f[B]scale\f[]
207 is a register (see the \f[B]REGISTERS\f[] section) that sets the
208 precision of any operations (with exceptions).
209 \f[B]scale\f[] is initially \f[B]0\f[].
210 \f[B]scale\f[] cannot be negative.
211 The max allowable value for \f[B]scale\f[] can be queried in dc(1)
212 programs with the \f[B]V\f[] command.
213 .PP
214 \f[B]seed\f[] is a register containing the current seed for the
215 pseudo\-random number generator.
216 If the current value of \f[B]seed\f[] is queried and stored, then if it
217 is assigned to \f[B]seed\f[] later, the pseudo\-random number generator
218 is guaranteed to produce the same sequence of pseudo\-random numbers
219 that were generated after the value of \f[B]seed\f[] was first queried.
220 .PP
221 Multiple values assigned to \f[B]seed\f[] can produce the same sequence
222 of pseudo\-random numbers.
223 Likewise, when a value is assigned to \f[B]seed\f[], it is not
224 guaranteed that querying \f[B]seed\f[] immediately after will return the
225 same value.
226 In addition, the value of \f[B]seed\f[] will change after any call to
227 the \f[B]\[aq]\f[] command or the \f[B]"\f[] command that does not get
228 receive a value of \f[B]0\f[] or \f[B]1\f[].
229 The maximum integer returned by the \f[B]\[aq]\f[] command can be
230 queried with the \f[B]W\f[] command.
231 .PP
232 \f[B]Note\f[]: The values returned by the pseudo\-random number
233 generator with the \f[B]\[aq]\f[] and \f[B]"\f[] commands are guaranteed
234 to \f[B]NOT\f[] be cryptographically secure.
235 This is a consequence of using a seeded pseudo\-random number generator.
236 However, they \f[B]are\f[] guaranteed to be reproducible with identical
237 \f[B]seed\f[] values.
238 .PP
239 The pseudo\-random number generator, \f[B]seed\f[], and all associated
240 operations are \f[B]non\-portable extensions\f[].
241 .SS Comments
242 .PP
243 Comments go from \f[B]#\f[] until, and not including, the next newline.
244 This is a \f[B]non\-portable extension\f[].
245 .SH NUMBERS
246 .PP
247 Numbers are strings made up of digits, uppercase letters up to
248 \f[B]F\f[], and at most \f[B]1\f[] period for a radix.
249 Numbers can have up to \f[B]DC_NUM_MAX\f[] digits.
250 Uppercase letters are equal to \f[B]9\f[] + their position in the
251 alphabet (i.e., \f[B]A\f[] equals \f[B]10\f[], or \f[B]9+1\f[]).
252 If a digit or letter makes no sense with the current value of
253 \f[B]ibase\f[], they are set to the value of the highest valid digit in
254 \f[B]ibase\f[].
255 .PP
256 Single\-character numbers (i.e., \f[B]A\f[] alone) take the value that
257 they would have if they were valid digits, regardless of the value of
258 \f[B]ibase\f[].
259 This means that \f[B]A\f[] alone always equals decimal \f[B]10\f[] and
260 \f[B]F\f[] alone always equals decimal \f[B]15\f[].
261 .PP
262 In addition, dc(1) accepts numbers in scientific notation.
263 These have the form \f[B]<number>e<integer>\f[].
264 The power (the portion after the \f[B]e\f[]) must be an integer.
265 An example is \f[B]1.89237e9\f[], which is equal to \f[B]1892370000\f[].
266 Negative exponents are also allowed, so \f[B]4.2890e_3\f[] is equal to
267 \f[B]0.0042890\f[].
268 .PP
269 \f[B]WARNING\f[]: Both the number and the exponent in scientific
270 notation are interpreted according to the current \f[B]ibase\f[], but
271 the number is still multiplied by \f[B]10^exponent\f[] regardless of the
272 current \f[B]ibase\f[].
273 For example, if \f[B]ibase\f[] is \f[B]16\f[] and dc(1) is given the
274 number string \f[B]FFeA\f[], the resulting decimal number will be
275 \f[B]2550000000000\f[], and if dc(1) is given the number string
276 \f[B]10e_4\f[], the resulting decimal number will be \f[B]0.0016\f[].
277 .PP
278 Accepting input as scientific notation is a \f[B]non\-portable
279 extension\f[].
280 .SH COMMANDS
281 .PP
282 The valid commands are listed below.
283 .SS Printing
284 .PP
285 These commands are used for printing.
286 .PP
287 Note that both scientific notation and engineering notation are
288 available for printing numbers.
289 Scientific notation is activated by assigning \f[B]0\f[] to
290 \f[B]obase\f[] using \f[B]0o\f[], and engineering notation is activated
291 by assigning \f[B]1\f[] to \f[B]obase\f[] using \f[B]1o\f[].
292 To deactivate them, just assign a different value to \f[B]obase\f[].
293 .PP
294 Printing numbers in scientific notation and/or engineering notation is a
295 \f[B]non\-portable extension\f[].
296 .TP
297 .B \f[B]p\f[]
298 Prints the value on top of the stack, whether number or string, and
299 prints a newline after.
300 .RS
301 .PP
302 This does not alter the stack.
303 .RE
304 .TP
305 .B \f[B]n\f[]
306 Prints the value on top of the stack, whether number or string, and pops
307 it off of the stack.
308 .RS
309 .RE
310 .TP
311 .B \f[B]P\f[]
312 Pops a value off the stack.
313 .RS
314 .PP
315 If the value is a number, it is truncated and the absolute value of the
316 result is printed as though \f[B]obase\f[] is \f[B]UCHAR_MAX+1\f[] and
317 each digit is interpreted as an ASCII character, making it a byte
318 stream.
319 .PP
320 If the value is a string, it is printed without a trailing newline.
321 .PP
322 This is a \f[B]non\-portable extension\f[].
323 .RE
324 .TP
325 .B \f[B]f\f[]
326 Prints the entire contents of the stack, in order from newest to oldest,
327 without altering anything.
328 .RS
329 .PP
330 Users should use this command when they get lost.
331 .RE
332 .SS Arithmetic
333 .PP
334 These are the commands used for arithmetic.
335 .TP
336 .B \f[B]+\f[]
337 The top two values are popped off the stack, added, and the result is
338 pushed onto the stack.
339 The \f[I]scale\f[] of the result is equal to the max \f[I]scale\f[] of
340 both operands.
341 .RS
342 .RE
343 .TP
344 .B \f[B]\-\f[]
345 The top two values are popped off the stack, subtracted, and the result
346 is pushed onto the stack.
347 The \f[I]scale\f[] of the result is equal to the max \f[I]scale\f[] of
348 both operands.
349 .RS
350 .RE
351 .TP
352 .B \f[B]*\f[]
353 The top two values are popped off the stack, multiplied, and the result
354 is pushed onto the stack.
355 If \f[B]a\f[] is the \f[I]scale\f[] of the first expression and
356 \f[B]b\f[] is the \f[I]scale\f[] of the second expression, the
357 \f[I]scale\f[] of the result is equal to
358 \f[B]min(a+b,max(scale,a,b))\f[] where \f[B]min()\f[] and \f[B]max()\f[]
359 return the obvious values.
360 .RS
361 .RE
362 .TP
363 .B \f[B]/\f[]
364 The top two values are popped off the stack, divided, and the result is
365 pushed onto the stack.
366 The \f[I]scale\f[] of the result is equal to \f[B]scale\f[].
367 .RS
368 .PP
369 The first value popped off of the stack must be non\-zero.
370 .RE
371 .TP
372 .B \f[B]%\f[]
373 The top two values are popped off the stack, remaindered, and the result
374 is pushed onto the stack.
375 .RS
376 .PP
377 Remaindering is equivalent to 1) Computing \f[B]a/b\f[] to current
378 \f[B]scale\f[], and 2) Using the result of step 1 to calculate
379 \f[B]a\-(a/b)*b\f[] to \f[I]scale\f[]
380 \f[B]max(scale+scale(b),scale(a))\f[].
381 .PP
382 The first value popped off of the stack must be non\-zero.
383 .RE
384 .TP
385 .B \f[B]~\f[]
386 The top two values are popped off the stack, divided and remaindered,
387 and the results (divided first, remainder second) are pushed onto the
388 stack.
389 This is equivalent to \f[B]x y / x y %\f[] except that \f[B]x\f[] and
390 \f[B]y\f[] are only evaluated once.
391 .RS
392 .PP
393 The first value popped off of the stack must be non\-zero.
394 .PP
395 This is a \f[B]non\-portable extension\f[].
396 .RE
397 .TP
398 .B \f[B]^\f[]
399 The top two values are popped off the stack, the second is raised to the
400 power of the first, and the result is pushed onto the stack.
401 .RS
402 .PP
403 The first value popped off of the stack must be an integer, and if that
404 value is negative, the second value popped off of the stack must be
405 non\-zero.
406 .RE
407 .TP
408 .B \f[B]v\f[]
409 The top value is popped off the stack, its square root is computed, and
410 the result is pushed onto the stack.
411 The \f[I]scale\f[] of the result is equal to \f[B]scale\f[].
412 .RS
413 .PP
414 The value popped off of the stack must be non\-negative.
415 .RE
416 .TP
417 .B \f[B]_\f[]
418 If this command \f[I]immediately\f[] precedes a number (i.e., no spaces
419 or other commands), then that number is input as a negative number.
420 .RS
421 .PP
422 Otherwise, the top value on the stack is popped and copied, and the copy
423 is negated and pushed onto the stack.
424 This behavior without a number is a \f[B]non\-portable extension\f[].
425 .RE
426 .TP
427 .B \f[B]b\f[]
428 The top value is popped off the stack, and if it is zero, it is pushed
429 back onto the stack.
430 Otherwise, its absolute value is pushed onto the stack.
431 .RS
432 .PP
433 This is a \f[B]non\-portable extension\f[].
434 .RE
435 .TP
436 .B \f[B]|\f[]
437 The top three values are popped off the stack, a modular exponentiation
438 is computed, and the result is pushed onto the stack.
439 .RS
440 .PP
441 The first value popped is used as the reduction modulus and must be an
442 integer and non\-zero.
443 The second value popped is used as the exponent and must be an integer
444 and non\-negative.
445 The third value popped is the base and must be an integer.
446 .PP
447 This is a \f[B]non\-portable extension\f[].
448 .RE
449 .TP
450 .B \f[B]$\f[]
451 The top value is popped off the stack and copied, and the copy is
452 truncated and pushed onto the stack.
453 .RS
454 .PP
455 This is a \f[B]non\-portable extension\f[].
456 .RE
457 .TP
458 .B \f[B]\@\f[]
459 The top two values are popped off the stack, and the precision of the
460 second is set to the value of the first, whether by truncation or
461 extension.
462 .RS
463 .PP
464 The first value popped off of the stack must be an integer and
465 non\-negative.
466 .PP
467 This is a \f[B]non\-portable extension\f[].
468 .RE
469 .TP
470 .B \f[B]H\f[]
471 The top two values are popped off the stack, and the second is shifted
472 left (radix shifted right) to the value of the first.
473 .RS
474 .PP
475 The first value popped off of the stack must be an integer and
476 non\-negative.
477 .PP
478 This is a \f[B]non\-portable extension\f[].
479 .RE
480 .TP
481 .B \f[B]h\f[]
482 The top two values are popped off the stack, and the second is shifted
483 right (radix shifted left) to the value of the first.
484 .RS
485 .PP
486 The first value popped off of the stack must be an integer and
487 non\-negative.
488 .PP
489 This is a \f[B]non\-portable extension\f[].
490 .RE
491 .TP
492 .B \f[B]G\f[]
493 The top two values are popped off of the stack, they are compared, and a
494 \f[B]1\f[] is pushed if they are equal, or \f[B]0\f[] otherwise.
495 .RS
496 .PP
497 This is a \f[B]non\-portable extension\f[].
498 .RE
499 .TP
500 .B \f[B]N\f[]
501 The top value is popped off of the stack, and if it a \f[B]0\f[], a
502 \f[B]1\f[] is pushed; otherwise, a \f[B]0\f[] is pushed.
503 .RS
504 .PP
505 This is a \f[B]non\-portable extension\f[].
506 .RE
507 .TP
508 .B \f[B](\f[]
509 The top two values are popped off of the stack, they are compared, and a
510 \f[B]1\f[] is pushed if the first is less than the second, or \f[B]0\f[]
511 otherwise.
512 .RS
513 .PP
514 This is a \f[B]non\-portable extension\f[].
515 .RE
516 .TP
517 .B \f[B]{\f[]
518 The top two values are popped off of the stack, they are compared, and a
519 \f[B]1\f[] is pushed if the first is less than or equal to the second,
520 or \f[B]0\f[] otherwise.
521 .RS
522 .PP
523 This is a \f[B]non\-portable extension\f[].
524 .RE
525 .TP
526 .B \f[B])\f[]
527 The top two values are popped off of the stack, they are compared, and a
528 \f[B]1\f[] is pushed if the first is greater than the second, or
529 \f[B]0\f[] otherwise.
530 .RS
531 .PP
532 This is a \f[B]non\-portable extension\f[].
533 .RE
534 .TP
535 .B \f[B]}\f[]
536 The top two values are popped off of the stack, they are compared, and a
537 \f[B]1\f[] is pushed if the first is greater than or equal to the
538 second, or \f[B]0\f[] otherwise.
539 .RS
540 .PP
541 This is a \f[B]non\-portable extension\f[].
542 .RE
543 .TP
544 .B \f[B]M\f[]
545 The top two values are popped off of the stack.
546 If they are both non\-zero, a \f[B]1\f[] is pushed onto the stack.
547 If either of them is zero, or both of them are, then a \f[B]0\f[] is
548 pushed onto the stack.
549 .RS
550 .PP
551 This is like the \f[B]&&\f[] operator in bc(1), and it is \f[I]not\f[] a
552 short\-circuit operator.
553 .PP
554 This is a \f[B]non\-portable extension\f[].
555 .RE
556 .TP
557 .B \f[B]m\f[]
558 The top two values are popped off of the stack.
559 If at least one of them is non\-zero, a \f[B]1\f[] is pushed onto the
560 stack.
561 If both of them are zero, then a \f[B]0\f[] is pushed onto the stack.
562 .RS
563 .PP
564 This is like the \f[B]||\f[] operator in bc(1), and it is \f[I]not\f[] a
565 short\-circuit operator.
566 .PP
567 This is a \f[B]non\-portable extension\f[].
568 .RE
569 .SS Pseudo\-Random Number Generator
570 .PP
571 dc(1) has a built\-in pseudo\-random number generator.
572 These commands query the pseudo\-random number generator.
573 (See Parameters for more information about the \f[B]seed\f[] value that
574 controls the pseudo\-random number generator.)
575 .PP
576 The pseudo\-random number generator is guaranteed to \f[B]NOT\f[] be
577 cryptographically secure.
578 .TP
579 .B \f[B]\[aq]\f[]
580 Generates an integer between 0 and \f[B]DC_RAND_MAX\f[], inclusive (see
581 the \f[B]LIMITS\f[] section).
582 .RS
583 .PP
584 The generated integer is made as unbiased as possible, subject to the
585 limitations of the pseudo\-random number generator.
586 .PP
587 This is a \f[B]non\-portable extension\f[].
588 .RE
589 .TP
590 .B \f[B]"\f[]
591 Pops a value off of the stack, which is used as an \f[B]exclusive\f[]
592 upper bound on the integer that will be generated.
593 If the bound is negative or is a non\-integer, an error is raised, and
594 dc(1) resets (see the \f[B]RESET\f[] section) while \f[B]seed\f[]
595 remains unchanged.
596 If the bound is larger than \f[B]DC_RAND_MAX\f[], the higher bound is
597 honored by generating several pseudo\-random integers, multiplying them
598 by appropriate powers of \f[B]DC_RAND_MAX+1\f[], and adding them
599 together.
600 Thus, the size of integer that can be generated with this command is
601 unbounded.
602 Using this command will change the value of \f[B]seed\f[], unless the
603 operand is \f[B]0\f[] or \f[B]1\f[].
604 In that case, \f[B]0\f[] is pushed onto the stack, and \f[B]seed\f[] is
605 \f[I]not\f[] changed.
606 .RS
607 .PP
608 The generated integer is made as unbiased as possible, subject to the
609 limitations of the pseudo\-random number generator.
610 .PP
611 This is a \f[B]non\-portable extension\f[].
612 .RE
613 .SS Stack Control
614 .PP
615 These commands control the stack.
616 .TP
617 .B \f[B]c\f[]
618 Removes all items from ("clears") the stack.
619 .RS
620 .RE
621 .TP
622 .B \f[B]d\f[]
623 Copies the item on top of the stack ("duplicates") and pushes the copy
624 onto the stack.
625 .RS
626 .RE
627 .TP
628 .B \f[B]r\f[]
629 Swaps ("reverses") the two top items on the stack.
630 .RS
631 .RE
632 .TP
633 .B \f[B]R\f[]
634 Pops ("removes") the top value from the stack.
635 .RS
636 .RE
637 .SS Register Control
638 .PP
639 These commands control registers (see the \f[B]REGISTERS\f[] section).
640 .TP
641 .B \f[B]s\f[]\f[I]r\f[]
642 Pops the value off the top of the stack and stores it into register
643 \f[I]r\f[].
644 .RS
645 .RE
646 .TP
647 .B \f[B]l\f[]\f[I]r\f[]
648 Copies the value in register \f[I]r\f[] and pushes it onto the stack.
649 This does not alter the contents of \f[I]r\f[].
650 .RS
651 .RE
652 .TP
653 .B \f[B]S\f[]\f[I]r\f[]
654 Pops the value off the top of the (main) stack and pushes it onto the
655 stack of register \f[I]r\f[].
656 The previous value of the register becomes inaccessible.
657 .RS
658 .RE
659 .TP
660 .B \f[B]L\f[]\f[I]r\f[]
661 Pops the value off the top of the stack for register \f[I]r\f[] and push
662 it onto the main stack.
663 The previous value in the stack for register \f[I]r\f[], if any, is now
664 accessible via the \f[B]l\f[]\f[I]r\f[] command.
665 .RS
666 .RE
667 .SS Parameters
668 .PP
669 These commands control the values of \f[B]ibase\f[], \f[B]obase\f[],
670 \f[B]scale\f[], and \f[B]seed\f[].
671 Also see the \f[B]SYNTAX\f[] section.
672 .TP
673 .B \f[B]i\f[]
674 Pops the value off of the top of the stack and uses it to set
675 \f[B]ibase\f[], which must be between \f[B]2\f[] and \f[B]16\f[],
676 inclusive.
677 .RS
678 .PP
679 If the value on top of the stack has any \f[I]scale\f[], the
680 \f[I]scale\f[] is ignored.
681 .RE
682 .TP
683 .B \f[B]o\f[]
684 Pops the value off of the top of the stack and uses it to set
685 \f[B]obase\f[], which must be between \f[B]0\f[] and
686 \f[B]DC_BASE_MAX\f[], inclusive (see the \f[B]LIMITS\f[] section and the
687 \f[B]NUMBERS\f[] section).
688 .RS
689 .PP
690 If the value on top of the stack has any \f[I]scale\f[], the
691 \f[I]scale\f[] is ignored.
692 .RE
693 .TP
694 .B \f[B]k\f[]
695 Pops the value off of the top of the stack and uses it to set
696 \f[B]scale\f[], which must be non\-negative.
697 .RS
698 .PP
699 If the value on top of the stack has any \f[I]scale\f[], the
700 \f[I]scale\f[] is ignored.
701 .RE
702 .TP
703 .B \f[B]j\f[]
704 Pops the value off of the top of the stack and uses it to set
705 \f[B]seed\f[].
706 The meaning of \f[B]seed\f[] is dependent on the current pseudo\-random
707 number generator but is guaranteed to not change except for new major
708 versions.
709 .RS
710 .PP
711 The \f[I]scale\f[] and sign of the value may be significant.
712 .PP
713 If a previously used \f[B]seed\f[] value is used again, the
714 pseudo\-random number generator is guaranteed to produce the same
715 sequence of pseudo\-random numbers as it did when the \f[B]seed\f[]
716 value was previously used.
717 .PP
718 The exact value assigned to \f[B]seed\f[] is not guaranteed to be
719 returned if the \f[B]J\f[] command is used.
720 However, if \f[B]seed\f[] \f[I]does\f[] return a different value, both
721 values, when assigned to \f[B]seed\f[], are guaranteed to produce the
722 same sequence of pseudo\-random numbers.
723 This means that certain values assigned to \f[B]seed\f[] will not
724 produce unique sequences of pseudo\-random numbers.
725 .PP
726 There is no limit to the length (number of significant decimal digits)
727 or \f[I]scale\f[] of the value that can be assigned to \f[B]seed\f[].
728 .PP
729 This is a \f[B]non\-portable extension\f[].
730 .RE
731 .TP
732 .B \f[B]I\f[]
733 Pushes the current value of \f[B]ibase\f[] onto the main stack.
734 .RS
735 .RE
736 .TP
737 .B \f[B]O\f[]
738 Pushes the current value of \f[B]obase\f[] onto the main stack.
739 .RS
740 .RE
741 .TP
742 .B \f[B]K\f[]
743 Pushes the current value of \f[B]scale\f[] onto the main stack.
744 .RS
745 .RE
746 .TP
747 .B \f[B]J\f[]
748 Pushes the current value of \f[B]seed\f[] onto the main stack.
749 .RS
750 .PP
751 This is a \f[B]non\-portable extension\f[].
752 .RE
753 .TP
754 .B \f[B]T\f[]
755 Pushes the maximum allowable value of \f[B]ibase\f[] onto the main
756 stack.
757 .RS
758 .PP
759 This is a \f[B]non\-portable extension\f[].
760 .RE
761 .TP
762 .B \f[B]U\f[]
763 Pushes the maximum allowable value of \f[B]obase\f[] onto the main
764 stack.
765 .RS
766 .PP
767 This is a \f[B]non\-portable extension\f[].
768 .RE
769 .TP
770 .B \f[B]V\f[]
771 Pushes the maximum allowable value of \f[B]scale\f[] onto the main
772 stack.
773 .RS
774 .PP
775 This is a \f[B]non\-portable extension\f[].
776 .RE
777 .TP
778 .B \f[B]W\f[]
779 Pushes the maximum (inclusive) integer that can be generated with the
780 \f[B]\[aq]\f[] pseudo\-random number generator command.
781 .RS
782 .PP
783 This is a \f[B]non\-portable extension\f[].
784 .RE
785 .SS Strings
786 .PP
787 The following commands control strings.
788 .PP
789 dc(1) can work with both numbers and strings, and registers (see the
790 \f[B]REGISTERS\f[] section) can hold both strings and numbers.
791 dc(1) always knows whether the contents of a register are a string or a
792 number.
793 .PP
794 While arithmetic operations have to have numbers, and will print an
795 error if given a string, other commands accept strings.
796 .PP
797 Strings can also be executed as macros.
798 For example, if the string \f[B][1pR]\f[] is executed as a macro, then
799 the code \f[B]1pR\f[] is executed, meaning that the \f[B]1\f[] will be
800 printed with a newline after and then popped from the stack.
801 .TP
802 .B \f[B][\f[]\f[I]characters\f[]\f[B]]\f[]
803 Makes a string containing \f[I]characters\f[] and pushes it onto the
804 stack.
805 .RS
806 .PP
807 If there are brackets (\f[B][\f[] and \f[B]]\f[]) in the string, then
808 they must be balanced.
809 Unbalanced brackets can be escaped using a backslash (\f[B]\\\f[])
810 character.
811 .PP
812 If there is a backslash character in the string, the character after it
813 (even another backslash) is put into the string verbatim, but the
814 (first) backslash is not.
815 .RE
816 .TP
817 .B \f[B]a\f[]
818 The value on top of the stack is popped.
819 .RS
820 .PP
821 If it is a number, it is truncated and its absolute value is taken.
822 The result mod \f[B]UCHAR_MAX+1\f[] is calculated.
823 If that result is \f[B]0\f[], push an empty string; otherwise, push a
824 one\-character string where the character is the result of the mod
825 interpreted as an ASCII character.
826 .PP
827 If it is a string, then a new string is made.
828 If the original string is empty, the new string is empty.
829 If it is not, then the first character of the original string is used to
830 create the new string as a one\-character string.
831 The new string is then pushed onto the stack.
832 .PP
833 This is a \f[B]non\-portable extension\f[].
834 .RE
835 .TP
836 .B \f[B]x\f[]
837 Pops a value off of the top of the stack.
838 .RS
839 .PP
840 If it is a number, it is pushed back onto the stack.
841 .PP
842 If it is a string, it is executed as a macro.
843 .PP
844 This behavior is the norm whenever a macro is executed, whether by this
845 command or by the conditional execution commands below.
846 .RE
847 .TP
848 .B \f[B]>\f[]\f[I]r\f[]
849 Pops two values off of the stack that must be numbers and compares them.
850 If the first value is greater than the second, then the contents of
851 register \f[I]r\f[] are executed.
852 .RS
853 .PP
854 For example, \f[B]0 1>a\f[] will execute the contents of register
855 \f[B]a\f[], and \f[B]1 0>a\f[] will not.
856 .PP
857 If either or both of the values are not numbers, dc(1) will raise an
858 error and reset (see the \f[B]RESET\f[] section).
859 .RE
860 .TP
861 .B \f[B]>\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
862 Like the above, but will execute register \f[I]s\f[] if the comparison
863 fails.
864 .RS
865 .PP
866 If either or both of the values are not numbers, dc(1) will raise an
867 error and reset (see the \f[B]RESET\f[] section).
868 .PP
869 This is a \f[B]non\-portable extension\f[].
870 .RE
871 .TP
872 .B \f[B]!>\f[]\f[I]r\f[]
873 Pops two values off of the stack that must be numbers and compares them.
874 If the first value is not greater than the second (less than or equal
875 to), then the contents of register \f[I]r\f[] are executed.
876 .RS
877 .PP
878 If either or both of the values are not numbers, dc(1) will raise an
879 error and reset (see the \f[B]RESET\f[] section).
880 .RE
881 .TP
882 .B \f[B]!>\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
883 Like the above, but will execute register \f[I]s\f[] if the comparison
884 fails.
885 .RS
886 .PP
887 If either or both of the values are not numbers, dc(1) will raise an
888 error and reset (see the \f[B]RESET\f[] section).
889 .PP
890 This is a \f[B]non\-portable extension\f[].
891 .RE
892 .TP
893 .B \f[B]<\f[]\f[I]r\f[]
894 Pops two values off of the stack that must be numbers and compares them.
895 If the first value is less than the second, then the contents of
896 register \f[I]r\f[] are executed.
897 .RS
898 .PP
899 If either or both of the values are not numbers, dc(1) will raise an
900 error and reset (see the \f[B]RESET\f[] section).
901 .RE
902 .TP
903 .B \f[B]<\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
904 Like the above, but will execute register \f[I]s\f[] if the comparison
905 fails.
906 .RS
907 .PP
908 If either or both of the values are not numbers, dc(1) will raise an
909 error and reset (see the \f[B]RESET\f[] section).
910 .PP
911 This is a \f[B]non\-portable extension\f[].
912 .RE
913 .TP
914 .B \f[B]!<\f[]\f[I]r\f[]
915 Pops two values off of the stack that must be numbers and compares them.
916 If the first value is not less than the second (greater than or equal
917 to), then the contents of register \f[I]r\f[] are executed.
918 .RS
919 .PP
920 If either or both of the values are not numbers, dc(1) will raise an
921 error and reset (see the \f[B]RESET\f[] section).
922 .RE
923 .TP
924 .B \f[B]!<\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
925 Like the above, but will execute register \f[I]s\f[] if the comparison
926 fails.
927 .RS
928 .PP
929 If either or both of the values are not numbers, dc(1) will raise an
930 error and reset (see the \f[B]RESET\f[] section).
931 .PP
932 This is a \f[B]non\-portable extension\f[].
933 .RE
934 .TP
935 .B \f[B]=\f[]\f[I]r\f[]
936 Pops two values off of the stack that must be numbers and compares them.
937 If the first value is equal to the second, then the contents of register
938 \f[I]r\f[] are executed.
939 .RS
940 .PP
941 If either or both of the values are not numbers, dc(1) will raise an
942 error and reset (see the \f[B]RESET\f[] section).
943 .RE
944 .TP
945 .B \f[B]=\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
946 Like the above, but will execute register \f[I]s\f[] if the comparison
947 fails.
948 .RS
949 .PP
950 If either or both of the values are not numbers, dc(1) will raise an
951 error and reset (see the \f[B]RESET\f[] section).
952 .PP
953 This is a \f[B]non\-portable extension\f[].
954 .RE
955 .TP
956 .B \f[B]!=\f[]\f[I]r\f[]
957 Pops two values off of the stack that must be numbers and compares them.
958 If the first value is not equal to the second, then the contents of
959 register \f[I]r\f[] are executed.
960 .RS
961 .PP
962 If either or both of the values are not numbers, dc(1) will raise an
963 error and reset (see the \f[B]RESET\f[] section).
964 .RE
965 .TP
966 .B \f[B]!=\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
967 Like the above, but will execute register \f[I]s\f[] if the comparison
968 fails.
969 .RS
970 .PP
971 If either or both of the values are not numbers, dc(1) will raise an
972 error and reset (see the \f[B]RESET\f[] section).
973 .PP
974 This is a \f[B]non\-portable extension\f[].
975 .RE
976 .TP
977 .B \f[B]?\f[]
978 Reads a line from the \f[B]stdin\f[] and executes it.
979 This is to allow macros to request input from users.
980 .RS
981 .RE
982 .TP
983 .B \f[B]q\f[]
984 During execution of a macro, this exits the execution of that macro and
985 the execution of the macro that executed it.
986 If there are no macros, or only one macro executing, dc(1) exits.
987 .RS
988 .RE
989 .TP
990 .B \f[B]Q\f[]
991 Pops a value from the stack which must be non\-negative and is used the
992 number of macro executions to pop off of the execution stack.
993 If the number of levels to pop is greater than the number of executing
994 macros, dc(1) exits.
995 .RS
996 .RE
997 .SS Status
998 .PP
999 These commands query status of the stack or its top value.
1000 .TP
1001 .B \f[B]Z\f[]
1002 Pops a value off of the stack.
1003 .RS
1004 .PP
1005 If it is a number, calculates the number of significant decimal digits
1006 it has and pushes the result.
1007 .PP
1008 If it is a string, pushes the number of characters the string has.
1009 .RE
1010 .TP
1011 .B \f[B]X\f[]
1012 Pops a value off of the stack.
1013 .RS
1014 .PP
1015 If it is a number, pushes the \f[I]scale\f[] of the value onto the
1016 stack.
1017 .PP
1018 If it is a string, pushes \f[B]0\f[].
1019 .RE
1020 .TP
1021 .B \f[B]z\f[]
1022 Pushes the current stack depth (before execution of this command).
1023 .RS
1024 .RE
1025 .SS Arrays
1026 .PP
1027 These commands manipulate arrays.
1028 .TP
1029 .B \f[B]:\f[]\f[I]r\f[]
1030 Pops the top two values off of the stack.
1031 The second value will be stored in the array \f[I]r\f[] (see the
1032 \f[B]REGISTERS\f[] section), indexed by the first value.
1033 .RS
1034 .RE
1035 .TP
1036 .B \f[B];\f[]\f[I]r\f[]
1037 Pops the value on top of the stack and uses it as an index into the
1038 array \f[I]r\f[].
1039 The selected value is then pushed onto the stack.
1040 .RS
1041 .RE
1042 .SH REGISTERS
1043 .PP
1044 Registers are names that can store strings, numbers, and arrays.
1045 (Number/string registers do not interfere with array registers.)
1046 .PP
1047 Each register is also its own stack, so the current register value is
1048 the top of the stack for the register.
1049 All registers, when first referenced, have one value (\f[B]0\f[]) in
1050 their stack.
1051 .PP
1052 In non\-extended register mode, a register name is just the single
1053 character that follows any command that needs a register name.
1054 The only exception is a newline (\f[B]\[aq]\\n\[aq]\f[]); it is a parse
1055 error for a newline to be used as a register name.
1056 .SS Extended Register Mode
1057 .PP
1058 Unlike most other dc(1) implentations, this dc(1) provides nearly
1059 unlimited amounts of registers, if extended register mode is enabled.
1060 .PP
1061 If extended register mode is enabled (\f[B]\-x\f[] or
1062 \f[B]\-\-extended\-register\f[] command\-line arguments are given), then
1063 normal single character registers are used \f[I]unless\f[] the character
1064 immediately following a command that needs a register name is a space
1065 (according to \f[B]isspace()\f[]) and not a newline
1066 (\f[B]\[aq]\\n\[aq]\f[]).
1067 .PP
1068 In that case, the register name is found according to the regex
1069 \f[B][a\-z][a\-z0\-9_]*\f[] (like bc(1) identifiers), and it is a parse
1070 error if the next non\-space characters do not match that regex.
1071 .SH RESET
1072 .PP
1073 When dc(1) encounters an error or a signal that it has a non\-default
1074 handler for, it resets.
1075 This means that several things happen.
1076 .PP
1077 First, any macros that are executing are stopped and popped off the
1078 stack.
1079 The behavior is not unlike that of exceptions in programming languages.
1080 Then the execution point is set so that any code waiting to execute
1081 (after all macros returned) is skipped.
1082 .PP
1083 Thus, when dc(1) resets, it skips any remaining code waiting to be
1084 executed.
1085 Then, if it is interactive mode, and the error was not a fatal error
1086 (see the \f[B]EXIT STATUS\f[] section), it asks for more input;
1087 otherwise, it exits with the appropriate return code.
1088 .SH PERFORMANCE
1089 .PP
1090 Most dc(1) implementations use \f[B]char\f[] types to calculate the
1091 value of \f[B]1\f[] decimal digit at a time, but that can be slow.
1092 This dc(1) does something different.
1093 .PP
1094 It uses large integers to calculate more than \f[B]1\f[] decimal digit
1095 at a time.
1096 If built in a environment where \f[B]DC_LONG_BIT\f[] (see the
1097 \f[B]LIMITS\f[] section) is \f[B]64\f[], then each integer has
1098 \f[B]9\f[] decimal digits.
1099 If built in an environment where \f[B]DC_LONG_BIT\f[] is \f[B]32\f[]
1100 then each integer has \f[B]4\f[] decimal digits.
1101 This value (the number of decimal digits per large integer) is called
1102 \f[B]DC_BASE_DIGS\f[].
1103 .PP
1104 In addition, this dc(1) uses an even larger integer for overflow
1105 checking.
1106 This integer type depends on the value of \f[B]DC_LONG_BIT\f[], but is
1107 always at least twice as large as the integer type used to store digits.
1108 .SH LIMITS
1109 .PP
1110 The following are the limits on dc(1):
1111 .TP
1112 .B \f[B]DC_LONG_BIT\f[]
1113 The number of bits in the \f[B]long\f[] type in the environment where
1114 dc(1) was built.
1115 This determines how many decimal digits can be stored in a single large
1116 integer (see the \f[B]PERFORMANCE\f[] section).
1117 .RS
1118 .RE
1119 .TP
1120 .B \f[B]DC_BASE_DIGS\f[]
1121 The number of decimal digits per large integer (see the
1122 \f[B]PERFORMANCE\f[] section).
1123 Depends on \f[B]DC_LONG_BIT\f[].
1124 .RS
1125 .RE
1126 .TP
1127 .B \f[B]DC_BASE_POW\f[]
1128 The max decimal number that each large integer can store (see
1129 \f[B]DC_BASE_DIGS\f[]) plus \f[B]1\f[].
1130 Depends on \f[B]DC_BASE_DIGS\f[].
1131 .RS
1132 .RE
1133 .TP
1134 .B \f[B]DC_OVERFLOW_MAX\f[]
1135 The max number that the overflow type (see the \f[B]PERFORMANCE\f[]
1136 section) can hold.
1137 Depends on \f[B]DC_LONG_BIT\f[].
1138 .RS
1139 .RE
1140 .TP
1141 .B \f[B]DC_BASE_MAX\f[]
1142 The maximum output base.
1143 Set at \f[B]DC_BASE_POW\f[].
1144 .RS
1145 .RE
1146 .TP
1147 .B \f[B]DC_DIM_MAX\f[]
1148 The maximum size of arrays.
1149 Set at \f[B]SIZE_MAX\-1\f[].
1150 .RS
1151 .RE
1152 .TP
1153 .B \f[B]DC_SCALE_MAX\f[]
1154 The maximum \f[B]scale\f[].
1155 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1156 .RS
1157 .RE
1158 .TP
1159 .B \f[B]DC_STRING_MAX\f[]
1160 The maximum length of strings.
1161 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1162 .RS
1163 .RE
1164 .TP
1165 .B \f[B]DC_NAME_MAX\f[]
1166 The maximum length of identifiers.
1167 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1168 .RS
1169 .RE
1170 .TP
1171 .B \f[B]DC_NUM_MAX\f[]
1172 The maximum length of a number (in decimal digits), which includes
1173 digits after the decimal point.
1174 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1175 .RS
1176 .RE
1177 .TP
1178 .B \f[B]DC_RAND_MAX\f[]
1179 The maximum integer (inclusive) returned by the \f[B]\[aq]\f[] command,
1180 if dc(1).
1181 Set at \f[B]2^DC_LONG_BIT\-1\f[].
1182 .RS
1183 .RE
1184 .TP
1185 .B Exponent
1186 The maximum allowable exponent (positive or negative).
1187 Set at \f[B]DC_OVERFLOW_MAX\f[].
1188 .RS
1189 .RE
1190 .TP
1191 .B Number of vars
1192 The maximum number of vars/arrays.
1193 Set at \f[B]SIZE_MAX\-1\f[].
1194 .RS
1195 .RE
1196 .PP
1197 These limits are meant to be effectively non\-existent; the limits are
1198 so large (at least on 64\-bit machines) that there should not be any
1199 point at which they become a problem.
1200 In fact, memory should be exhausted before these limits should be hit.
1201 .SH ENVIRONMENT VARIABLES
1202 .PP
1203 dc(1) recognizes the following environment variables:
1204 .TP
1205 .B \f[B]DC_ENV_ARGS\f[]
1206 This is another way to give command\-line arguments to dc(1).
1207 They should be in the same format as all other command\-line arguments.
1208 These are always processed first, so any files given in
1209 \f[B]DC_ENV_ARGS\f[] will be processed before arguments and files given
1210 on the command\-line.
1211 This gives the user the ability to set up "standard" options and files
1212 to be used at every invocation.
1213 The most useful thing for such files to contain would be useful
1214 functions that the user might want every time dc(1) runs.
1215 Another use would be to use the \f[B]\-e\f[] option to set
1216 \f[B]scale\f[] to a value other than \f[B]0\f[].
1217 .RS
1218 .PP
1219 The code that parses \f[B]DC_ENV_ARGS\f[] will correctly handle quoted
1220 arguments, but it does not understand escape sequences.
1221 For example, the string \f[B]"/home/gavin/some dc file.dc"\f[] will be
1222 correctly parsed, but the string \f[B]"/home/gavin/some "dc"
1223 file.dc"\f[] will include the backslashes.
1224 .PP
1225 The quote parsing will handle either kind of quotes, \f[B]\[aq]\f[] or
1226 \f[B]"\f[].
1227 Thus, if you have a file with any number of single quotes in the name,
1228 you can use double quotes as the outside quotes, as in \f[B]"some
1229 \[aq]bc\[aq] file.bc"\f[], and vice versa if you have a file with double
1230 quotes.
1231 However, handling a file with both kinds of quotes in
1232 \f[B]DC_ENV_ARGS\f[] is not supported due to the complexity of the
1233 parsing, though such files are still supported on the command\-line
1234 where the parsing is done by the shell.
1235 .RE
1236 .TP
1237 .B \f[B]DC_LINE_LENGTH\f[]
1238 If this environment variable exists and contains an integer that is
1239 greater than \f[B]1\f[] and is less than \f[B]UINT16_MAX\f[]
1240 (\f[B]2^16\-1\f[]), dc(1) will output lines to that length, including
1241 the backslash newline combo.
1242 The default line length is \f[B]70\f[].
1243 .RS
1244 .RE
1245 .TP
1246 .B \f[B]DC_EXPR_EXIT\f[]
1247 If this variable exists (no matter the contents), dc(1) will exit
1248 immediately after executing expressions and files given by the
1249 \f[B]\-e\f[] and/or \f[B]\-f\f[] command\-line options (and any
1250 equivalents).
1251 .RS
1252 .RE
1253 .SH EXIT STATUS
1254 .PP
1255 dc(1) returns the following exit statuses:
1256 .TP
1257 .B \f[B]0\f[]
1258 No error.
1259 .RS
1260 .RE
1261 .TP
1262 .B \f[B]1\f[]
1263 A math error occurred.
1264 This follows standard practice of using \f[B]1\f[] for expected errors,
1265 since math errors will happen in the process of normal execution.
1266 .RS
1267 .PP
1268 Math errors include divide by \f[B]0\f[], taking the square root of a
1269 negative number, using a negative number as a bound for the
1270 pseudo\-random number generator, attempting to convert a negative number
1271 to a hardware integer, overflow when converting a number to a hardware
1272 integer, and attempting to use a non\-integer where an integer is
1273 required.
1274 .PP
1275 Converting to a hardware integer happens for the second operand of the
1276 power (\f[B]^\f[]), places (\f[B]\@\f[]), left shift (\f[B]H\f[]), and
1277 right shift (\f[B]h\f[]) operators.
1278 .RE
1279 .TP
1280 .B \f[B]2\f[]
1281 A parse error occurred.
1282 .RS
1283 .PP
1284 Parse errors include unexpected \f[B]EOF\f[], using an invalid
1285 character, failing to find the end of a string or comment, and using a
1286 token where it is invalid.
1287 .RE
1288 .TP
1289 .B \f[B]3\f[]
1290 A runtime error occurred.
1291 .RS
1292 .PP
1293 Runtime errors include assigning an invalid number to \f[B]ibase\f[],
1294 \f[B]obase\f[], or \f[B]scale\f[]; give a bad expression to a
1295 \f[B]read()\f[] call, calling \f[B]read()\f[] inside of a
1296 \f[B]read()\f[] call, type errors, and attempting an operation when the
1297 stack has too few elements.
1298 .RE
1299 .TP
1300 .B \f[B]4\f[]
1301 A fatal error occurred.
1302 .RS
1303 .PP
1304 Fatal errors include memory allocation errors, I/O errors, failing to
1305 open files, attempting to use files that do not have only ASCII
1306 characters (dc(1) only accepts ASCII characters), attempting to open a
1307 directory as a file, and giving invalid command\-line options.
1308 .RE
1309 .PP
1310 The exit status \f[B]4\f[] is special; when a fatal error occurs, dc(1)
1311 always exits and returns \f[B]4\f[], no matter what mode dc(1) is in.
1312 .PP
1313 The other statuses will only be returned when dc(1) is not in
1314 interactive mode (see the \f[B]INTERACTIVE MODE\f[] section), since
1315 dc(1) resets its state (see the \f[B]RESET\f[] section) and accepts more
1316 input when one of those errors occurs in interactive mode.
1317 This is also the case when interactive mode is forced by the
1318 \f[B]\-i\f[] flag or \f[B]\-\-interactive\f[] option.
1319 .PP
1320 These exit statuses allow dc(1) to be used in shell scripting with error
1321 checking, and its normal behavior can be forced by using the
1322 \f[B]\-i\f[] flag or \f[B]\-\-interactive\f[] option.
1323 .SH INTERACTIVE MODE
1324 .PP
1325 Like bc(1), dc(1) has an interactive mode and a non\-interactive mode.
1326 Interactive mode is turned on automatically when both \f[B]stdin\f[] and
1327 \f[B]stdout\f[] are hooked to a terminal, but the \f[B]\-i\f[] flag and
1328 \f[B]\-\-interactive\f[] option can turn it on in other cases.
1329 .PP
1330 In interactive mode, dc(1) attempts to recover from errors (see the
1331 \f[B]RESET\f[] section), and in normal execution, flushes
1332 \f[B]stdout\f[] as soon as execution is done for the current input.
1333 .SH TTY MODE
1334 .PP
1335 If \f[B]stdin\f[], \f[B]stdout\f[], and \f[B]stderr\f[] are all
1336 connected to a TTY, dc(1) turns on "TTY mode."
1337 .PP
1338 The prompt is enabled in TTY mode.
1339 .PP
1340 TTY mode is different from interactive mode because interactive mode is
1341 required in the bc(1)
1342 specification (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1343 and interactive mode requires only \f[B]stdin\f[] and \f[B]stdout\f[] to
1344 be connected to a terminal.
1345 .SH SIGNAL HANDLING
1346 .PP
1347 Sending a \f[B]SIGINT\f[] will cause dc(1) to stop execution of the
1348 current input.
1349 If dc(1) is in TTY mode (see the \f[B]TTY MODE\f[] section), it will
1350 reset (see the \f[B]RESET\f[] section).
1351 Otherwise, it will clean up and exit.
1352 .PP
1353 Note that "current input" can mean one of two things.
1354 If dc(1) is processing input from \f[B]stdin\f[] in TTY mode, it will
1355 ask for more input.
1356 If dc(1) is processing input from a file in TTY mode, it will stop
1357 processing the file and start processing the next file, if one exists,
1358 or ask for input from \f[B]stdin\f[] if no other file exists.
1359 .PP
1360 This means that if a \f[B]SIGINT\f[] is sent to dc(1) as it is executing
1361 a file, it can seem as though dc(1) did not respond to the signal since
1362 it will immediately start executing the next file.
1363 This is by design; most files that users execute when interacting with
1364 dc(1) have function definitions, which are quick to parse.
1365 If a file takes a long time to execute, there may be a bug in that file.
1366 The rest of the files could still be executed without problem, allowing
1367 the user to continue.
1368 .PP
1369 \f[B]SIGTERM\f[] and \f[B]SIGQUIT\f[] cause dc(1) to clean up and exit,
1370 and it uses the default handler for all other signals.
1371 .SH LOCALES
1372 .PP
1373 This dc(1) ships with support for adding error messages for different
1374 locales and thus, supports \f[B]LC_MESSAGS\f[].
1375 .SH SEE ALSO
1376 .PP
1377 bc(1)
1378 .SH STANDARDS
1379 .PP
1380 The dc(1) utility operators are compliant with the operators in the
1381 bc(1) IEEE Std 1003.1\-2017
1382 (“POSIX.1\-2017”) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1383 specification.
1384 .SH BUGS
1385 .PP
1386 None are known.
1387 Report bugs at https://git.yzena.com/gavin/bc.
1388 .SH AUTHOR
1389 .PP
1390 Gavin D.
1391 Howard <yzena.tech@gmail.com> and contributors.