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