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