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