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