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