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