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