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