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