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