]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - manuals/dc/EHNP.1.md
Update to version 3.2.0
[FreeBSD/FreeBSD.git] / manuals / dc / EHNP.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 decimal 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 **2**. Values are output in the specified base.
167
168 The *scale* of an expression is the number of digits in the result of the
169 expression right of the decimal point, and **scale** is a register (see the
170 **REGISTERS** section) that sets the precision of any operations (with
171 exceptions). **scale** is initially **0**. **scale** cannot be negative. The max
172 allowable value for **scale** can be queried in dc(1) programs with the **V**
173 command.
174
175 ## Comments
176
177 Comments go from **#** until, and not including, the next newline. This is a
178 **non-portable extension**.
179
180 # NUMBERS
181
182 Numbers are strings made up of digits, uppercase letters up to **F**, and at
183 most **1** period for a radix. Numbers can have up to **DC_NUM_MAX** digits.
184 Uppercase letters are equal to **9** + their position in the alphabet (i.e.,
185 **A** equals **10**, or **9+1**). If a digit or letter makes no sense with the
186 current value of **ibase**, they are set to the value of the highest valid digit
187 in **ibase**.
188
189 Single-character numbers (i.e., **A** alone) take the value that they would have
190 if they were valid digits, regardless of the value of **ibase**. This means that
191 **A** alone always equals decimal **10** and **F** alone always equals decimal
192 **15**.
193
194 # COMMANDS
195
196 The valid commands are listed below.
197
198 ## Printing
199
200 These commands are used for printing.
201
202 **p**
203
204 :   Prints the value on top of the stack, whether number or string, and prints a
205     newline after.
206
207     This does not alter the stack.
208
209 **n**
210
211 :   Prints the value on top of the stack, whether number or string, and pops it
212     off of the stack.
213
214 **P**
215
216 :   Pops a value off the stack.
217
218     If the value is a number, it is truncated and the absolute value of the
219     result is printed as though **obase** is **UCHAR_MAX+1** and each digit is
220     interpreted as an ASCII character, making it a byte stream.
221
222     If the value is a string, it is printed without a trailing newline.
223
224     This is a **non-portable extension**.
225
226 **f**
227
228 :   Prints the entire contents of the stack, in order from newest to oldest,
229     without altering anything.
230
231     Users should use this command when they get lost.
232
233 ## Arithmetic
234
235 These are the commands used for arithmetic.
236
237 **+**
238
239 :   The top two values are popped off the stack, added, and the result is pushed
240     onto the stack. The *scale* of the result is equal to the max *scale* of
241     both operands.
242
243 **-**
244
245 :   The top two values are popped off the stack, subtracted, and the result is
246     pushed onto the stack. The *scale* of the result is equal to the max
247     *scale* of both operands.
248
249 **\***
250
251 :   The top two values are popped off the stack, multiplied, and the result is
252     pushed onto the stack. If **a** is the *scale* of the first expression and
253     **b** is the *scale* of the second expression, the *scale* of the result
254     is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return
255     the obvious values.
256
257 **/**
258
259 :   The top two values are popped off the stack, divided, and the result is
260     pushed onto the stack. The *scale* of the result is equal to **scale**.
261
262     The first value popped off of the stack must be non-zero.
263
264 **%**
265
266 :   The top two values are popped off the stack, remaindered, and the result is
267     pushed onto the stack.
268
269     Remaindering is equivalent to 1) Computing **a/b** to current **scale**, and
270     2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale*
271     **max(scale+scale(b),scale(a))**.
272
273     The first value popped off of the stack must be non-zero.
274
275 **~**
276
277 :   The top two values are popped off the stack, divided and remaindered, and
278     the results (divided first, remainder second) are pushed onto the stack.
279     This is equivalent to **x y / x y %** except that **x** and **y** are only
280     evaluated once.
281
282     The first value popped off of the stack must be non-zero.
283
284     This is a **non-portable extension**.
285
286 **\^**
287
288 :   The top two values are popped off the stack, the second is raised to the
289     power of the first, and the result is pushed onto the stack. The *scale* of
290     the result is equal to **scale**.
291
292     The first value popped off of the stack must be an integer, and if that
293     value is negative, the second value popped off of the stack must be
294     non-zero.
295
296 **v**
297
298 :   The top value is popped off the stack, its square root is computed, and the
299     result is pushed onto the stack. The *scale* of the result is equal to
300     **scale**.
301
302     The value popped off of the stack must be non-negative.
303
304 **\_**
305
306 :   If this command *immediately* precedes a number (i.e., no spaces or other
307     commands), then that number is input as a negative number.
308
309     Otherwise, the top value on the stack is popped and copied, and the copy is
310     negated and pushed onto the stack. This behavior without a number is a
311     **non-portable extension**.
312
313 **b**
314
315 :   The top value is popped off the stack, and if it is zero, it is pushed back
316     onto the stack. Otherwise, its absolute value is pushed onto the stack.
317
318     This is a **non-portable extension**.
319
320 **|**
321
322 :   The top three values are popped off the stack, a modular exponentiation is
323     computed, and the result is pushed onto the stack.
324
325     The first value popped is used as the reduction modulus and must be an
326     integer and non-zero. The second value popped is used as the exponent and
327     must be an integer and non-negative. The third value popped is the base and
328     must be an integer.
329
330     This is a **non-portable extension**.
331
332 **G**
333
334 :   The top two values are popped off of the stack, they are compared, and a
335     **1** is pushed if they are equal, or **0** otherwise.
336
337     This is a **non-portable extension**.
338
339 **N**
340
341 :   The top value is popped off of the stack, and if it a **0**, a **1** is
342     pushed; otherwise, a **0** is pushed.
343
344     This is a **non-portable extension**.
345
346 **(**
347
348 :   The top two values are popped off of the stack, they are compared, and a
349     **1** is pushed if the first is less than the second, or **0** otherwise.
350
351     This is a **non-portable extension**.
352
353 **{**
354
355 :   The top two values are popped off of the stack, they are compared, and a
356     **1** is pushed if the first is less than or equal to the second, or **0**
357     otherwise.
358
359     This is a **non-portable extension**.
360
361 **)**
362
363 :   The top two values are popped off of the stack, they are compared, and a
364     **1** is pushed if the first is greater than the second, or **0** otherwise.
365
366     This is a **non-portable extension**.
367
368 **}**
369
370 :   The top two values are popped off of the stack, they are compared, and a
371     **1** is pushed if the first is greater than or equal to the second, or
372     **0** otherwise.
373
374     This is a **non-portable extension**.
375
376 **M**
377
378 :   The top two values are popped off of the stack. If they are both non-zero, a
379     **1** is pushed onto the stack. If either of them is zero, or both of them
380     are, then a **0** is pushed onto the stack.
381
382     This is like the **&&** operator in bc(1), and it is *not* a short-circuit
383     operator.
384
385     This is a **non-portable extension**.
386
387 **m**
388
389 :   The top two values are popped off of the stack. If at least one of them is
390     non-zero, a **1** is pushed onto the stack. If both of them are zero, then a
391     **0** is pushed onto the stack.
392
393     This is like the **||** operator in bc(1), and it is *not* a short-circuit
394     operator.
395
396     This is a **non-portable extension**.
397
398 ## Stack Control
399
400 These commands control the stack.
401
402 **c**
403
404 :   Removes all items from ("clears") the stack.
405
406 **d**
407
408 :   Copies the item on top of the stack ("duplicates") and pushes the copy onto
409     the stack.
410
411 **r**
412
413 :   Swaps ("reverses") the two top items on the stack.
414
415 **R**
416
417 :   Pops ("removes") the top value from the stack.
418
419 ## Register Control
420
421 These commands control registers (see the **REGISTERS** section).
422
423 **s***r*
424
425 :   Pops the value off the top of the stack and stores it into register *r*.
426
427 **l***r*
428
429 :   Copies the value in register *r* and pushes it onto the stack. This does not
430     alter the contents of *r*.
431
432 **S***r*
433
434 :   Pops the value off the top of the (main) stack and pushes it onto the stack
435     of register *r*. The previous value of the register becomes inaccessible.
436
437 **L***r*
438
439 :   Pops the value off the top of the stack for register *r* and push it onto
440     the main stack. The previous value in the stack for register *r*, if any, is
441     now accessible via the **l***r* command.
442
443 ## Parameters
444
445 These commands control the values of **ibase**, **obase**, and **scale**. Also
446 see the **SYNTAX** section.
447
448 **i**
449
450 :   Pops the value off of the top of the stack and uses it to set **ibase**,
451     which must be between **2** and **16**, inclusive.
452
453     If the value on top of the stack has any *scale*, the *scale* is ignored.
454
455 **o**
456
457 :   Pops the value off of the top of the stack and uses it to set **obase**,
458     which must be between **2** and **DC_BASE_MAX**, inclusive (see the
459     **LIMITS** section).
460
461     If the value on top of the stack has any *scale*, the *scale* is ignored.
462
463 **k**
464
465 :   Pops the value off of the top of the stack and uses it to set **scale**,
466     which must be non-negative.
467
468     If the value on top of the stack has any *scale*, the *scale* is ignored.
469
470 **I**
471
472 :   Pushes the current value of **ibase** onto the main stack.
473
474 **O**
475
476 :   Pushes the current value of **obase** onto the main stack.
477
478 **K**
479
480 :   Pushes the current value of **scale** onto the main stack.
481
482 **T**
483
484 :   Pushes the maximum allowable value of **ibase** onto the main stack.
485
486     This is a **non-portable extension**.
487
488 **U**
489
490 :   Pushes the maximum allowable value of **obase** onto the main stack.
491
492     This is a **non-portable extension**.
493
494 **V**
495
496 :   Pushes the maximum allowable value of **scale** onto the main stack.
497
498     This is a **non-portable extension**.
499
500 ## Strings
501
502 The following commands control strings.
503
504 dc(1) can work with both numbers and strings, and registers (see the
505 **REGISTERS** section) can hold both strings and numbers. dc(1) always knows
506 whether the contents of a register are a string or a number.
507
508 While arithmetic operations have to have numbers, and will print an error if
509 given a string, other commands accept strings.
510
511 Strings can also be executed as macros. For example, if the string **[1pR]** is
512 executed as a macro, then the code **1pR** is executed, meaning that the **1**
513 will be printed with a newline after and then popped from the stack.
514
515 **\[**_characters_**\]**
516
517 :   Makes a string containing *characters* and pushes it onto the stack.
518
519     If there are brackets (**\[** and **\]**) in the string, then they must be
520     balanced. Unbalanced brackets can be escaped using a backslash (**\\**)
521     character.
522
523     If there is a backslash character in the string, the character after it
524     (even another backslash) is put into the string verbatim, but the (first)
525     backslash is not.
526
527 **a**
528
529 :   The value on top of the stack is popped.
530
531     If it is a number, it is truncated and its absolute value is taken. The
532     result mod **UCHAR_MAX+1** is calculated. If that result is **0**, push an
533     empty string; otherwise, push a one-character string where the character is
534     the result of the mod interpreted as an ASCII character.
535
536     If it is a string, then a new string is made. If the original string is
537     empty, the new string is empty. If it is not, then the first character of
538     the original string is used to create the new string as a one-character
539     string. The new string is then pushed onto the stack.
540
541     This is a **non-portable extension**.
542
543 **x**
544
545 :   Pops a value off of the top of the stack.
546
547     If it is a number, it is pushed back onto the stack.
548
549     If it is a string, it is executed as a macro.
550
551     This behavior is the norm whenever a macro is executed, whether by this
552     command or by the conditional execution commands below.
553
554 **\>***r*
555
556 :   Pops two values off of the stack that must be numbers and compares them. If
557     the first value is greater than the second, then the contents of register
558     *r* are executed.
559
560     For example, **0 1>a** will execute the contents of register **a**, and
561     **1 0>a** will not.
562
563     If either or both of the values are not numbers, dc(1) will raise an error
564     and reset (see the **RESET** section).
565
566 **>***r***e***s*
567
568 :   Like the above, but will execute register *s* if the comparison fails.
569
570     If either or both of the values are not numbers, dc(1) will raise an error
571     and reset (see the **RESET** section).
572
573     This is a **non-portable extension**.
574
575 **!\>***r*
576
577 :   Pops two values off of the stack that must be numbers and compares them. If
578     the first value is not greater than the second (less than or equal to), then
579     the contents of register *r* are executed.
580
581     If either or both of the values are not numbers, dc(1) will raise an error
582     and reset (see the **RESET** section).
583
584 **!\>***r***e***s*
585
586 :   Like the above, but will execute register *s* if the comparison fails.
587
588     If either or both of the values are not numbers, dc(1) will raise an error
589     and reset (see the **RESET** section).
590
591     This is a **non-portable extension**.
592
593 **\<***r*
594
595 :   Pops two values off of the stack that must be numbers and compares them. If
596     the first value is less than the second, then the contents of register *r*
597     are executed.
598
599     If either or both of the values are not numbers, dc(1) will raise an error
600     and reset (see the **RESET** section).
601
602 **\<***r***e***s*
603
604 :   Like the above, but will execute register *s* if the comparison fails.
605
606     If either or both of the values are not numbers, dc(1) will raise an error
607     and reset (see the **RESET** section).
608
609     This is a **non-portable extension**.
610
611 **!\<***r*
612
613 :   Pops two values off of the stack that must be numbers and compares them. If
614     the first value is not less than the second (greater than or equal to), then
615     the contents of register *r* are executed.
616
617     If either or both of the values are not numbers, dc(1) will raise an error
618     and reset (see the **RESET** section).
619
620 **!\<***r***e***s*
621
622 :   Like the above, but will execute register *s* if the comparison fails.
623
624     If either or both of the values are not numbers, dc(1) will raise an error
625     and reset (see the **RESET** section).
626
627     This is a **non-portable extension**.
628
629 **=***r*
630
631 :   Pops two values off of the stack that must be numbers and compares them. If
632     the first value is equal to the second, then the contents of register *r*
633     are executed.
634
635     If either or both of the values are not numbers, dc(1) will raise an error
636     and reset (see the **RESET** section).
637
638 **=***r***e***s*
639
640 :   Like the above, but will execute register *s* if the comparison fails.
641
642     If either or both of the values are not numbers, dc(1) will raise an error
643     and reset (see the **RESET** section).
644
645     This is a **non-portable extension**.
646
647 **!=***r*
648
649 :   Pops two values off of the stack that must be numbers and compares them. If
650     the first value is not equal to the second, then the contents of register
651     *r* are executed.
652
653     If either or both of the values are not numbers, dc(1) will raise an error
654     and reset (see the **RESET** section).
655
656 **!=***r***e***s*
657
658 :   Like the above, but will execute register *s* if the comparison fails.
659
660     If either or both of the values are not numbers, dc(1) will raise an error
661     and reset (see the **RESET** section).
662
663     This is a **non-portable extension**.
664
665 **?**
666
667 :   Reads a line from the **stdin** and executes it. This is to allow macros to
668     request input from users.
669
670 **q**
671
672 :   During execution of a macro, this exits the execution of that macro and the
673     execution of the macro that executed it. If there are no macros, or only one
674     macro executing, dc(1) exits.
675
676 **Q**
677
678 :   Pops a value from the stack which must be non-negative and is used the
679     number of macro executions to pop off of the execution stack. If the number
680     of levels to pop is greater than the number of executing macros, dc(1)
681     exits.
682
683 ## Status
684
685 These commands query status of the stack or its top value.
686
687 **Z**
688
689 :   Pops a value off of the stack.
690
691     If it is a number, calculates the number of significant decimal digits it
692     has and pushes the result.
693
694     If it is a string, pushes the number of characters the string has.
695
696 **X**
697
698 :   Pops a value off of the stack.
699
700     If it is a number, pushes the *scale* of the value onto the stack.
701
702     If it is a string, pushes **0**.
703
704 **z**
705
706 :   Pushes the current stack depth (before execution of this command).
707
708 ## Arrays
709
710 These commands manipulate arrays.
711
712 **:***r*
713
714 :   Pops the top two values off of the stack. The second value will be stored in
715     the array *r* (see the **REGISTERS** section), indexed by the first value.
716
717 **;***r*
718
719 :   Pops the value on top of the stack and uses it as an index into the array
720     *r*. The selected value is then pushed onto the stack.
721
722 # REGISTERS
723
724 Registers are names that can store strings, numbers, and arrays. (Number/string
725 registers do not interfere with array registers.)
726
727 Each register is also its own stack, so the current register value is the top of
728 the stack for the register. All registers, when first referenced, have one value
729 (**0**) in their stack.
730
731 In non-extended register mode, a register name is just the single character that
732 follows any command that needs a register name. The only exception is a newline
733 (**'\\n'**); it is a parse error for a newline to be used as a register name.
734
735 ## Extended Register Mode
736
737 Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited
738 amounts of registers, if extended register mode is enabled.
739
740 If extended register mode is enabled (**-x** or **--extended-register**
741 command-line arguments are given), then normal single character registers are
742 used *unless* the character immediately following a command that needs a
743 register name is a space (according to **isspace()**) and not a newline
744 (**'\\n'**).
745
746 In that case, the register name is found according to the regex
747 **\[a-z\]\[a-z0-9\_\]\*** (like bc(1) identifiers), and it is a parse error if
748 the next non-space characters do not match that regex.
749
750 # RESET
751
752 When dc(1) encounters an error or a signal that it has a non-default handler
753 for, it resets. This means that several things happen.
754
755 First, any macros that are executing are stopped and popped off the stack.
756 The behavior is not unlike that of exceptions in programming languages. Then
757 the execution point is set so that any code waiting to execute (after all
758 macros returned) is skipped.
759
760 Thus, when dc(1) resets, it skips any remaining code waiting to be executed.
761 Then, if it is interactive mode, and the error was not a fatal error (see the
762 **EXIT STATUS** section), it asks for more input; otherwise, it exits with the
763 appropriate return code.
764
765 # PERFORMANCE
766
767 Most dc(1) implementations use **char** types to calculate the value of **1**
768 decimal digit at a time, but that can be slow. This dc(1) does something
769 different.
770
771 It uses large integers to calculate more than **1** decimal digit at a time. If
772 built in a environment where **DC_LONG_BIT** (see the **LIMITS** section) is
773 **64**, then each integer has **9** decimal digits. If built in an environment
774 where **DC_LONG_BIT** is **32** then each integer has **4** decimal digits. This
775 value (the number of decimal digits per large integer) is called
776 **DC_BASE_DIGS**.
777
778 In addition, this dc(1) uses an even larger integer for overflow checking. This
779 integer type depends on the value of **DC_LONG_BIT**, but is always at least
780 twice as large as the integer type used to store digits.
781
782 # LIMITS
783
784 The following are the limits on dc(1):
785
786 **DC_LONG_BIT**
787
788 :   The number of bits in the **long** type in the environment where dc(1) was
789     built. This determines how many decimal digits can be stored in a single
790     large integer (see the **PERFORMANCE** section).
791
792 **DC_BASE_DIGS**
793
794 :   The number of decimal digits per large integer (see the **PERFORMANCE**
795     section). Depends on **DC_LONG_BIT**.
796
797 **DC_BASE_POW**
798
799 :   The max decimal number that each large integer can store (see
800     **DC_BASE_DIGS**) plus **1**. Depends on **DC_BASE_DIGS**.
801
802 **DC_OVERFLOW_MAX**
803
804 :   The max number that the overflow type (see the **PERFORMANCE** section) can
805     hold. Depends on **DC_LONG_BIT**.
806
807 **DC_BASE_MAX**
808
809 :   The maximum output base. Set at **DC_BASE_POW**.
810
811 **DC_DIM_MAX**
812
813 :   The maximum size of arrays. Set at **SIZE_MAX-1**.
814
815 **DC_SCALE_MAX**
816
817 :   The maximum **scale**. Set at **DC_OVERFLOW_MAX-1**.
818
819 **DC_STRING_MAX**
820
821 :   The maximum length of strings. Set at **DC_OVERFLOW_MAX-1**.
822
823 **DC_NAME_MAX**
824
825 :   The maximum length of identifiers. Set at **DC_OVERFLOW_MAX-1**.
826
827 **DC_NUM_MAX**
828
829 :   The maximum length of a number (in decimal digits), which includes digits
830     after the decimal point. Set at **DC_OVERFLOW_MAX-1**.
831
832 Exponent
833
834 :   The maximum allowable exponent (positive or negative). Set at
835     **DC_OVERFLOW_MAX**.
836
837 Number of vars
838
839 :   The maximum number of vars/arrays. Set at **SIZE_MAX-1**.
840
841 These limits are meant to be effectively non-existent; the limits are so large
842 (at least on 64-bit machines) that there should not be any point at which they
843 become a problem. In fact, memory should be exhausted before these limits should
844 be hit.
845
846 # ENVIRONMENT VARIABLES
847
848 dc(1) recognizes the following environment variables:
849
850 **DC_ENV_ARGS**
851
852 :   This is another way to give command-line arguments to dc(1). They should be
853     in the same format as all other command-line arguments. These are always
854     processed first, so any files given in **DC_ENV_ARGS** will be processed
855     before arguments and files given on the command-line. This gives the user
856     the ability to set up "standard" options and files to be used at every
857     invocation. The most useful thing for such files to contain would be useful
858     functions that the user might want every time dc(1) runs. Another use would
859     be to use the **-e** option to set **scale** to a value other than **0**.
860
861     The code that parses **DC_ENV_ARGS** will correctly handle quoted arguments,
862     but it does not understand escape sequences. For example, the string
863     **"/home/gavin/some dc file.dc"** will be correctly parsed, but the string
864     **"/home/gavin/some \"dc\" file.dc"** will include the backslashes.
865
866     The quote parsing will handle either kind of quotes, **'** or **"**. Thus,
867     if you have a file with any number of single quotes in the name, you can use
868     double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice
869     versa if you have a file with double quotes. However, handling a file with
870     both kinds of quotes in **DC_ENV_ARGS** is not supported due to the
871     complexity of the parsing, though such files are still supported on the
872     command-line where the parsing is done by the shell.
873
874 **DC_LINE_LENGTH**
875
876 :   If this environment variable exists and contains an integer that is greater
877     than **1** and is less than **UINT16_MAX** (**2\^16-1**), dc(1) will output
878     lines to that length, including the backslash newline combo. The default
879     line length is **70**.
880
881 **DC_EXPR_EXIT**
882
883 :   If this variable exists (no matter the contents), dc(1) will exit
884     immediately after executing expressions and files given by the **-e** and/or
885     **-f** command-line options (and any equivalents).
886
887 # EXIT STATUS
888
889 dc(1) returns the following exit statuses:
890
891 **0**
892
893 :   No error.
894
895 **1**
896
897 :   A math error occurred. This follows standard practice of using **1** for
898     expected errors, since math errors will happen in the process of normal
899     execution.
900
901     Math errors include divide by **0**, taking the square root of a negative
902     number, attempting to convert a negative number to a hardware integer,
903     overflow when converting a number to a hardware integer, and attempting to
904     use a non-integer where an integer is required.
905
906     Converting to a hardware integer happens for the second operand of the power
907     (**\^**) operator.
908
909 **2**
910
911 :   A parse error occurred.
912
913     Parse errors include unexpected **EOF**, using an invalid character, failing
914     to find the end of a string or comment, and using a token where it is
915     invalid.
916
917 **3**
918
919 :   A runtime error occurred.
920
921     Runtime errors include assigning an invalid number to **ibase**, **obase**,
922     or **scale**; give a bad expression to a **read()** call, calling **read()**
923     inside of a **read()** call, type errors, and attempting an operation when
924     the stack has too few elements.
925
926 **4**
927
928 :   A fatal error occurred.
929
930     Fatal errors include memory allocation errors, I/O errors, failing to open
931     files, attempting to use files that do not have only ASCII characters (dc(1)
932     only accepts ASCII characters), attempting to open a directory as a file,
933     and giving invalid command-line options.
934
935 The exit status **4** is special; when a fatal error occurs, dc(1) always exits
936 and returns **4**, no matter what mode dc(1) is in.
937
938 The other statuses will only be returned when dc(1) is not in interactive mode
939 (see the **INTERACTIVE MODE** section), since dc(1) resets its state (see the
940 **RESET** section) and accepts more input when one of those errors occurs in
941 interactive mode. This is also the case when interactive mode is forced by the
942 **-i** flag or **--interactive** option.
943
944 These exit statuses allow dc(1) to be used in shell scripting with error
945 checking, and its normal behavior can be forced by using the **-i** flag or
946 **--interactive** option.
947
948 # INTERACTIVE MODE
949
950 Like bc(1), dc(1) has an interactive mode and a non-interactive mode.
951 Interactive mode is turned on automatically when both **stdin** and **stdout**
952 are hooked to a terminal, but the **-i** flag and **--interactive** option can
953 turn it on in other cases.
954
955 In interactive mode, dc(1) attempts to recover from errors (see the **RESET**
956 section), and in normal execution, flushes **stdout** as soon as execution is
957 done for the current input.
958
959 # TTY MODE
960
961 If **stdin**, **stdout**, and **stderr** are all connected to a TTY, dc(1) turns
962 on "TTY mode."
963
964 TTY mode is different from interactive mode because interactive mode is required
965 in the [bc(1) specification][1], and interactive mode requires only **stdin**
966 and **stdout** to be connected to a terminal.
967
968 # SIGNAL HANDLING
969
970 Sending a **SIGINT** will cause dc(1) to stop execution of the current input. If
971 dc(1) is in TTY mode (see the **TTY MODE** section), it will reset (see the
972 **RESET** section). Otherwise, it will clean up and exit.
973
974 Note that "current input" can mean one of two things. If dc(1) is processing
975 input from **stdin** in TTY mode, it will ask for more input. If dc(1) is
976 processing input from a file in TTY mode, it will stop processing the file and
977 start processing the next file, if one exists, or ask for input from **stdin**
978 if no other file exists.
979
980 This means that if a **SIGINT** is sent to dc(1) as it is executing a file, it
981 can seem as though dc(1) did not respond to the signal since it will immediately
982 start executing the next file. This is by design; most files that users execute
983 when interacting with dc(1) have function definitions, which are quick to parse.
984 If a file takes a long time to execute, there may be a bug in that file. The
985 rest of the files could still be executed without problem, allowing the user to
986 continue.
987
988 **SIGTERM** and **SIGQUIT** cause dc(1) to clean up and exit, and it uses the
989 default handler for all other signals.
990
991 # SEE ALSO
992
993 bc(1)
994
995 # STANDARDS
996
997 The dc(1) utility operators are compliant with the operators in the bc(1)
998 [IEEE Std 1003.1-2017 (“POSIX.1-2017”)][1] specification.
999
1000 # BUGS
1001
1002 None are known. Report bugs at https://git.yzena.com/gavin/bc.
1003
1004 # AUTHOR
1005
1006 Gavin D. Howard <gavin@yzena.com> and contributors.
1007
1008 [1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html