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