13aa99676SStefan Eßer<!--- 23aa99676SStefan Eßer 33aa99676SStefan EßerSPDX-License-Identifier: BSD-2-Clause 43aa99676SStefan Eßer 5*10328f8bSStefan EßerCopyright (c) 2018-2021 Gavin D. Howard and contributors. 63aa99676SStefan Eßer 73aa99676SStefan EßerRedistribution and use in source and binary forms, with or without 83aa99676SStefan Eßermodification, are permitted provided that the following conditions are met: 93aa99676SStefan Eßer 103aa99676SStefan Eßer* Redistributions of source code must retain the above copyright notice, this 113aa99676SStefan Eßer list of conditions and the following disclaimer. 123aa99676SStefan Eßer 133aa99676SStefan Eßer* Redistributions in binary form must reproduce the above copyright notice, 143aa99676SStefan Eßer this list of conditions and the following disclaimer in the documentation 153aa99676SStefan Eßer and/or other materials provided with the distribution. 163aa99676SStefan Eßer 173aa99676SStefan EßerTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 183aa99676SStefan EßerAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 193aa99676SStefan EßerIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 203aa99676SStefan EßerARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 213aa99676SStefan EßerLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 223aa99676SStefan EßerCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 233aa99676SStefan EßerSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 243aa99676SStefan EßerINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 253aa99676SStefan EßerCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 263aa99676SStefan EßerARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 273aa99676SStefan EßerPOSSIBILITY OF SUCH DAMAGE. 283aa99676SStefan Eßer 293aa99676SStefan Eßer--> 303aa99676SStefan Eßer 313aa99676SStefan Eßer# Name 323aa99676SStefan Eßer 3350696a6eSStefan Eßerdc - arbitrary-precision decimal reverse-Polish notation calculator 343aa99676SStefan Eßer 353aa99676SStefan Eßer# SYNOPSIS 363aa99676SStefan Eßer 373aa99676SStefan Eßer**dc** [**-hiPvVx**] [**--version**] [**--help**] [**--interactive**] [**--no-prompt**] [**--extended-register**] [**-e** *expr*] [**--expression**=*expr*...] [**-f** *file*...] [**-file**=*file*...] [*file*...] 383aa99676SStefan Eßer 393aa99676SStefan Eßer# DESCRIPTION 403aa99676SStefan Eßer 413aa99676SStefan Eßerdc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish 423aa99676SStefan Eßernotation) to store numbers and results of computations. Arithmetic operations 433aa99676SStefan Eßerpop arguments off of the stack and push the results. 443aa99676SStefan Eßer 453aa99676SStefan EßerIf no files are given on the command-line as extra arguments (i.e., not as 463aa99676SStefan Eßer**-f** or **--file** arguments), then dc(1) reads from **stdin**. Otherwise, 473aa99676SStefan Eßerthose files are processed, and dc(1) will then exit. 483aa99676SStefan Eßer 493aa99676SStefan EßerThis is different from the dc(1) on OpenBSD and possibly other dc(1) 503aa99676SStefan Eßerimplementations, where **-e** (**--expression**) and **-f** (**--file**) 513aa99676SStefan Eßerarguments cause dc(1) to execute them and exit. The reason for this is that this 523aa99676SStefan Eßerdc(1) allows users to set arguments in the environment variable **DC_ENV_ARGS** 533aa99676SStefan Eßer(see the **ENVIRONMENT VARIABLES** section). Any expressions given on the 543aa99676SStefan Eßercommand-line should be used to set up a standard environment. For example, if a 553aa99676SStefan Eßeruser wants the **scale** always set to **10**, they can set **DC_ENV_ARGS** to 563aa99676SStefan Eßer**-e 10k**, and this dc(1) will always start with a **scale** of **10**. 573aa99676SStefan Eßer 583aa99676SStefan EßerIf users want to have dc(1) exit after processing all input from **-e** and 593aa99676SStefan Eßer**-f** arguments (and their equivalents), then they can just simply add **-e q** 603aa99676SStefan Eßeras the last command-line argument or define the environment variable 613aa99676SStefan Eßer**DC_EXPR_EXIT**. 623aa99676SStefan Eßer 633aa99676SStefan Eßer# OPTIONS 643aa99676SStefan Eßer 653aa99676SStefan EßerThe following are the options that dc(1) accepts. 663aa99676SStefan Eßer 673aa99676SStefan Eßer**-h**, **--help** 683aa99676SStefan Eßer 693aa99676SStefan Eßer: Prints a usage message and quits. 703aa99676SStefan Eßer 713aa99676SStefan Eßer**-v**, **-V**, **--version** 723aa99676SStefan Eßer 733aa99676SStefan Eßer: Print the version information (copyright header) and exit. 743aa99676SStefan Eßer 753aa99676SStefan Eßer**-i**, **--interactive** 763aa99676SStefan Eßer 773aa99676SStefan Eßer: Forces interactive mode. (See the **INTERACTIVE MODE** section.) 783aa99676SStefan Eßer 793aa99676SStefan Eßer This is a **non-portable extension**. 803aa99676SStefan Eßer 813aa99676SStefan Eßer**-P**, **--no-prompt** 823aa99676SStefan Eßer 833aa99676SStefan Eßer: Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. 843aa99676SStefan Eßer See the **TTY MODE** section) This is mostly for those users that do not 853aa99676SStefan Eßer want a prompt or are not used to having them in dc(1). Most of those users 863aa99676SStefan Eßer would want to put this option in **DC_ENV_ARGS**. 873aa99676SStefan Eßer 883aa99676SStefan Eßer This is a **non-portable extension**. 893aa99676SStefan Eßer 903aa99676SStefan Eßer**-x** **--extended-register** 913aa99676SStefan Eßer 923aa99676SStefan Eßer: Enables extended register mode. See the *Extended Register Mode* subsection 933aa99676SStefan Eßer of the **REGISTERS** section for more information. 943aa99676SStefan Eßer 953aa99676SStefan Eßer This is a **non-portable extension**. 963aa99676SStefan Eßer 973aa99676SStefan Eßer**-e** *expr*, **--expression**=*expr* 983aa99676SStefan Eßer 993aa99676SStefan Eßer: Evaluates *expr*. If multiple expressions are given, they are evaluated in 1003aa99676SStefan Eßer order. If files are given as well (see below), the expressions and files are 1013aa99676SStefan Eßer evaluated in the order given. This means that if a file is given before an 1023aa99676SStefan Eßer expression, the file is read in and evaluated first. 1033aa99676SStefan Eßer 1045d934bc0SStefan Eßer After processing all expressions and files, dc(1) will exit, unless **-** 1055d934bc0SStefan Eßer (**stdin**) was given as an argument at least once to **-f** or **--file**. 1063aa99676SStefan Eßer 1073aa99676SStefan Eßer This is a **non-portable extension**. 1083aa99676SStefan Eßer 1093aa99676SStefan Eßer**-f** *file*, **--file**=*file* 1103aa99676SStefan Eßer 1113aa99676SStefan Eßer: Reads in *file* and evaluates it, line by line, as though it were read 1123aa99676SStefan Eßer through **stdin**. If expressions are also given (see above), the 1133aa99676SStefan Eßer expressions are evaluated in the order given. 1143aa99676SStefan Eßer 1155d934bc0SStefan Eßer After processing all expressions and files, dc(1) will exit, unless **-** 1165d934bc0SStefan Eßer (**stdin**) was given as an argument at least once to **-f** or **--file**. 1175d934bc0SStefan Eßer However, if any other **-e**, **--expression**, **-f**, or **--file** 1185d934bc0SStefan Eßer arguments are given after that, bc(1) will give a fatal error and exit. 1193aa99676SStefan Eßer 1203aa99676SStefan Eßer This is a **non-portable extension**. 1213aa99676SStefan Eßer 1223aa99676SStefan EßerAll long options are **non-portable extensions**. 1233aa99676SStefan Eßer 1243aa99676SStefan Eßer# STDOUT 1253aa99676SStefan Eßer 1263aa99676SStefan EßerAny non-error output is written to **stdout**. 1273aa99676SStefan Eßer 1283aa99676SStefan Eßer**Note**: Unlike other dc(1) implementations, this dc(1) will issue a fatal 1293aa99676SStefan Eßererror (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if 1303aa99676SStefan Eßer**stdout** is closed, as in **dc <file> >&-**, it will quit with an error. This 1313aa99676SStefan Eßeris done so that dc(1) can report problems when **stdout** is redirected to a 1323aa99676SStefan Eßerfile. 1333aa99676SStefan Eßer 1343aa99676SStefan EßerIf there are scripts that depend on the behavior of other dc(1) implementations, 1353aa99676SStefan Eßerit is recommended that those scripts be changed to redirect **stdout** to 1363aa99676SStefan Eßer**/dev/null**. 1373aa99676SStefan Eßer 1383aa99676SStefan Eßer# STDERR 1393aa99676SStefan Eßer 1403aa99676SStefan EßerAny error output is written to **stderr**. 1413aa99676SStefan Eßer 1423aa99676SStefan Eßer**Note**: Unlike other dc(1) implementations, this dc(1) will issue a fatal 1433aa99676SStefan Eßererror (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if 1443aa99676SStefan Eßer**stderr** is closed, as in **dc <file> 2>&-**, it will quit with an error. This 1453aa99676SStefan Eßeris done so that dc(1) can exit with an error code when **stderr** is redirected 1463aa99676SStefan Eßerto a file. 1473aa99676SStefan Eßer 1483aa99676SStefan EßerIf there are scripts that depend on the behavior of other dc(1) implementations, 1493aa99676SStefan Eßerit is recommended that those scripts be changed to redirect **stderr** to 1503aa99676SStefan Eßer**/dev/null**. 1513aa99676SStefan Eßer 1523aa99676SStefan Eßer# SYNTAX 1533aa99676SStefan Eßer 1543aa99676SStefan EßerEach item in the input source code, either a number (see the **NUMBERS** 1553aa99676SStefan Eßersection) or a command (see the **COMMANDS** section), is processed and executed, 1563aa99676SStefan Eßerin order. Input is processed immediately when entered. 1573aa99676SStefan Eßer 1583aa99676SStefan Eßer**ibase** is a register (see the **REGISTERS** section) that determines how to 1593aa99676SStefan Eßerinterpret constant numbers. It is the "input" base, or the number base used for 1603aa99676SStefan Eßerinterpreting input numbers. **ibase** is initially **10**. The max allowable 1613aa99676SStefan Eßervalue for **ibase** is **16**. The min allowable value for **ibase** is **2**. 1623aa99676SStefan EßerThe max allowable value for **ibase** can be queried in dc(1) programs with the 1633aa99676SStefan Eßer**T** command. 1643aa99676SStefan Eßer 1653aa99676SStefan Eßer**obase** is a register (see the **REGISTERS** section) that determines how to 1663aa99676SStefan Eßeroutput results. It is the "output" base, or the number base used for outputting 1673aa99676SStefan Eßernumbers. **obase** is initially **10**. The max allowable value for **obase** is 1683aa99676SStefan Eßer**DC_BASE_MAX** and can be queried with the **U** command. The min allowable 1693aa99676SStefan Eßervalue for **obase** is **2**. Values are output in the specified base. 1703aa99676SStefan Eßer 1713aa99676SStefan EßerThe *scale* of an expression is the number of digits in the result of the 1723aa99676SStefan Eßerexpression right of the decimal point, and **scale** is a register (see the 1733aa99676SStefan Eßer**REGISTERS** section) that sets the precision of any operations (with 1743aa99676SStefan Eßerexceptions). **scale** is initially **0**. **scale** cannot be negative. The max 1753aa99676SStefan Eßerallowable value for **scale** can be queried in dc(1) programs with the **V** 1763aa99676SStefan Eßercommand. 1773aa99676SStefan Eßer 1783aa99676SStefan Eßer## Comments 1793aa99676SStefan Eßer 1803aa99676SStefan EßerComments go from **#** until, and not including, the next newline. This is a 1813aa99676SStefan Eßer**non-portable extension**. 1823aa99676SStefan Eßer 1833aa99676SStefan Eßer# NUMBERS 1843aa99676SStefan Eßer 1853aa99676SStefan EßerNumbers are strings made up of digits, uppercase letters up to **F**, and at 1863aa99676SStefan Eßermost **1** period for a radix. Numbers can have up to **DC_NUM_MAX** digits. 1873aa99676SStefan EßerUppercase letters are equal to **9** + their position in the alphabet (i.e., 1883aa99676SStefan Eßer**A** equals **10**, or **9+1**). If a digit or letter makes no sense with the 1893aa99676SStefan Eßercurrent value of **ibase**, they are set to the value of the highest valid digit 1903aa99676SStefan Eßerin **ibase**. 1913aa99676SStefan Eßer 1923aa99676SStefan EßerSingle-character numbers (i.e., **A** alone) take the value that they would have 1933aa99676SStefan Eßerif they were valid digits, regardless of the value of **ibase**. This means that 1943aa99676SStefan Eßer**A** alone always equals decimal **10** and **F** alone always equals decimal 1953aa99676SStefan Eßer**15**. 1963aa99676SStefan Eßer 1973aa99676SStefan Eßer# COMMANDS 1983aa99676SStefan Eßer 1993aa99676SStefan EßerThe valid commands are listed below. 2003aa99676SStefan Eßer 2013aa99676SStefan Eßer## Printing 2023aa99676SStefan Eßer 2033aa99676SStefan EßerThese commands are used for printing. 2043aa99676SStefan Eßer 2053aa99676SStefan Eßer**p** 2063aa99676SStefan Eßer 2073aa99676SStefan Eßer: Prints the value on top of the stack, whether number or string, and prints a 2083aa99676SStefan Eßer newline after. 2093aa99676SStefan Eßer 2103aa99676SStefan Eßer This does not alter the stack. 2113aa99676SStefan Eßer 2123aa99676SStefan Eßer**n** 2133aa99676SStefan Eßer 2143aa99676SStefan Eßer: Prints the value on top of the stack, whether number or string, and pops it 2153aa99676SStefan Eßer off of the stack. 2163aa99676SStefan Eßer 2173aa99676SStefan Eßer**P** 2183aa99676SStefan Eßer 2193aa99676SStefan Eßer: Pops a value off the stack. 2203aa99676SStefan Eßer 2213aa99676SStefan Eßer If the value is a number, it is truncated and the absolute value of the 2223aa99676SStefan Eßer result is printed as though **obase** is **UCHAR_MAX+1** and each digit is 2233aa99676SStefan Eßer interpreted as an ASCII character, making it a byte stream. 2243aa99676SStefan Eßer 2253aa99676SStefan Eßer If the value is a string, it is printed without a trailing newline. 2263aa99676SStefan Eßer 2273aa99676SStefan Eßer This is a **non-portable extension**. 2283aa99676SStefan Eßer 2293aa99676SStefan Eßer**f** 2303aa99676SStefan Eßer 2313aa99676SStefan Eßer: Prints the entire contents of the stack, in order from newest to oldest, 2323aa99676SStefan Eßer without altering anything. 2333aa99676SStefan Eßer 2343aa99676SStefan Eßer Users should use this command when they get lost. 2353aa99676SStefan Eßer 2363aa99676SStefan Eßer## Arithmetic 2373aa99676SStefan Eßer 2383aa99676SStefan EßerThese are the commands used for arithmetic. 2393aa99676SStefan Eßer 2403aa99676SStefan Eßer**+** 2413aa99676SStefan Eßer 2423aa99676SStefan Eßer: The top two values are popped off the stack, added, and the result is pushed 2433aa99676SStefan Eßer onto the stack. The *scale* of the result is equal to the max *scale* of 2443aa99676SStefan Eßer both operands. 2453aa99676SStefan Eßer 2463aa99676SStefan Eßer**-** 2473aa99676SStefan Eßer 2483aa99676SStefan Eßer: The top two values are popped off the stack, subtracted, and the result is 2493aa99676SStefan Eßer pushed onto the stack. The *scale* of the result is equal to the max 2503aa99676SStefan Eßer *scale* of both operands. 2513aa99676SStefan Eßer 2523aa99676SStefan Eßer**\*** 2533aa99676SStefan Eßer 2543aa99676SStefan Eßer: The top two values are popped off the stack, multiplied, and the result is 2553aa99676SStefan Eßer pushed onto the stack. If **a** is the *scale* of the first expression and 2563aa99676SStefan Eßer **b** is the *scale* of the second expression, the *scale* of the result 2573aa99676SStefan Eßer is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return 2583aa99676SStefan Eßer the obvious values. 2593aa99676SStefan Eßer 2603aa99676SStefan Eßer**/** 2613aa99676SStefan Eßer 2623aa99676SStefan Eßer: The top two values are popped off the stack, divided, and the result is 2633aa99676SStefan Eßer pushed onto the stack. The *scale* of the result is equal to **scale**. 2643aa99676SStefan Eßer 2653aa99676SStefan Eßer The first value popped off of the stack must be non-zero. 2663aa99676SStefan Eßer 2673aa99676SStefan Eßer**%** 2683aa99676SStefan Eßer 2693aa99676SStefan Eßer: The top two values are popped off the stack, remaindered, and the result is 2703aa99676SStefan Eßer pushed onto the stack. 2713aa99676SStefan Eßer 2723aa99676SStefan Eßer Remaindering is equivalent to 1) Computing **a/b** to current **scale**, and 2733aa99676SStefan Eßer 2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale* 2743aa99676SStefan Eßer **max(scale+scale(b),scale(a))**. 2753aa99676SStefan Eßer 2763aa99676SStefan Eßer The first value popped off of the stack must be non-zero. 2773aa99676SStefan Eßer 2783aa99676SStefan Eßer**~** 2793aa99676SStefan Eßer 2803aa99676SStefan Eßer: The top two values are popped off the stack, divided and remaindered, and 2813aa99676SStefan Eßer the results (divided first, remainder second) are pushed onto the stack. 2823aa99676SStefan Eßer This is equivalent to **x y / x y %** except that **x** and **y** are only 2833aa99676SStefan Eßer evaluated once. 2843aa99676SStefan Eßer 2853aa99676SStefan Eßer The first value popped off of the stack must be non-zero. 2863aa99676SStefan Eßer 2873aa99676SStefan Eßer This is a **non-portable extension**. 2883aa99676SStefan Eßer 2893aa99676SStefan Eßer**\^** 2903aa99676SStefan Eßer 2913aa99676SStefan Eßer: The top two values are popped off the stack, the second is raised to the 29250696a6eSStefan Eßer power of the first, and the result is pushed onto the stack. The *scale* of 29350696a6eSStefan Eßer the result is equal to **scale**. 2943aa99676SStefan Eßer 2953aa99676SStefan Eßer The first value popped off of the stack must be an integer, and if that 2963aa99676SStefan Eßer value is negative, the second value popped off of the stack must be 2973aa99676SStefan Eßer non-zero. 2983aa99676SStefan Eßer 2993aa99676SStefan Eßer**v** 3003aa99676SStefan Eßer 3013aa99676SStefan Eßer: The top value is popped off the stack, its square root is computed, and the 3023aa99676SStefan Eßer result is pushed onto the stack. The *scale* of the result is equal to 3033aa99676SStefan Eßer **scale**. 3043aa99676SStefan Eßer 3053aa99676SStefan Eßer The value popped off of the stack must be non-negative. 3063aa99676SStefan Eßer 3073aa99676SStefan Eßer**\_** 3083aa99676SStefan Eßer 3093aa99676SStefan Eßer: If this command *immediately* precedes a number (i.e., no spaces or other 3103aa99676SStefan Eßer commands), then that number is input as a negative number. 3113aa99676SStefan Eßer 3123aa99676SStefan Eßer Otherwise, the top value on the stack is popped and copied, and the copy is 3133aa99676SStefan Eßer negated and pushed onto the stack. This behavior without a number is a 3143aa99676SStefan Eßer **non-portable extension**. 3153aa99676SStefan Eßer 3163aa99676SStefan Eßer**b** 3173aa99676SStefan Eßer 3183aa99676SStefan Eßer: The top value is popped off the stack, and if it is zero, it is pushed back 3193aa99676SStefan Eßer onto the stack. Otherwise, its absolute value is pushed onto the stack. 3203aa99676SStefan Eßer 3213aa99676SStefan Eßer This is a **non-portable extension**. 3223aa99676SStefan Eßer 3233aa99676SStefan Eßer**|** 3243aa99676SStefan Eßer 3253aa99676SStefan Eßer: The top three values are popped off the stack, a modular exponentiation is 3263aa99676SStefan Eßer computed, and the result is pushed onto the stack. 3273aa99676SStefan Eßer 3283aa99676SStefan Eßer The first value popped is used as the reduction modulus and must be an 3293aa99676SStefan Eßer integer and non-zero. The second value popped is used as the exponent and 3303aa99676SStefan Eßer must be an integer and non-negative. The third value popped is the base and 3313aa99676SStefan Eßer must be an integer. 3323aa99676SStefan Eßer 3333aa99676SStefan Eßer This is a **non-portable extension**. 3343aa99676SStefan Eßer 3353aa99676SStefan Eßer**G** 3363aa99676SStefan Eßer 3373aa99676SStefan Eßer: The top two values are popped off of the stack, they are compared, and a 3383aa99676SStefan Eßer **1** is pushed if they are equal, or **0** otherwise. 3393aa99676SStefan Eßer 3403aa99676SStefan Eßer This is a **non-portable extension**. 3413aa99676SStefan Eßer 3423aa99676SStefan Eßer**N** 3433aa99676SStefan Eßer 3443aa99676SStefan Eßer: The top value is popped off of the stack, and if it a **0**, a **1** is 3453aa99676SStefan Eßer pushed; otherwise, a **0** is pushed. 3463aa99676SStefan Eßer 3473aa99676SStefan Eßer This is a **non-portable extension**. 3483aa99676SStefan Eßer 3493aa99676SStefan Eßer**(** 3503aa99676SStefan Eßer 3513aa99676SStefan Eßer: The top two values are popped off of the stack, they are compared, and a 3523aa99676SStefan Eßer **1** is pushed if the first is less than the second, or **0** otherwise. 3533aa99676SStefan Eßer 3543aa99676SStefan Eßer This is a **non-portable extension**. 3553aa99676SStefan Eßer 3563aa99676SStefan Eßer**{** 3573aa99676SStefan Eßer 3583aa99676SStefan Eßer: The top two values are popped off of the stack, they are compared, and a 3593aa99676SStefan Eßer **1** is pushed if the first is less than or equal to the second, or **0** 3603aa99676SStefan Eßer otherwise. 3613aa99676SStefan Eßer 3623aa99676SStefan Eßer This is a **non-portable extension**. 3633aa99676SStefan Eßer 3643aa99676SStefan Eßer**)** 3653aa99676SStefan Eßer 3663aa99676SStefan Eßer: The top two values are popped off of the stack, they are compared, and a 3673aa99676SStefan Eßer **1** is pushed if the first is greater than the second, or **0** otherwise. 3683aa99676SStefan Eßer 3693aa99676SStefan Eßer This is a **non-portable extension**. 3703aa99676SStefan Eßer 3713aa99676SStefan Eßer**}** 3723aa99676SStefan Eßer 3733aa99676SStefan Eßer: The top two values are popped off of the stack, they are compared, and a 3743aa99676SStefan Eßer **1** is pushed if the first is greater than or equal to the second, or 3753aa99676SStefan Eßer **0** otherwise. 3763aa99676SStefan Eßer 3773aa99676SStefan Eßer This is a **non-portable extension**. 3783aa99676SStefan Eßer 3793aa99676SStefan Eßer**M** 3803aa99676SStefan Eßer 3813aa99676SStefan Eßer: The top two values are popped off of the stack. If they are both non-zero, a 3823aa99676SStefan Eßer **1** is pushed onto the stack. If either of them is zero, or both of them 3833aa99676SStefan Eßer are, then a **0** is pushed onto the stack. 3843aa99676SStefan Eßer 3853aa99676SStefan Eßer This is like the **&&** operator in bc(1), and it is *not* a short-circuit 3863aa99676SStefan Eßer operator. 3873aa99676SStefan Eßer 3883aa99676SStefan Eßer This is a **non-portable extension**. 3893aa99676SStefan Eßer 3903aa99676SStefan Eßer**m** 3913aa99676SStefan Eßer 3923aa99676SStefan Eßer: The top two values are popped off of the stack. If at least one of them is 3933aa99676SStefan Eßer non-zero, a **1** is pushed onto the stack. If both of them are zero, then a 3943aa99676SStefan Eßer **0** is pushed onto the stack. 3953aa99676SStefan Eßer 3963aa99676SStefan Eßer This is like the **||** operator in bc(1), and it is *not* a short-circuit 3973aa99676SStefan Eßer operator. 3983aa99676SStefan Eßer 3993aa99676SStefan Eßer This is a **non-portable extension**. 4003aa99676SStefan Eßer 4013aa99676SStefan Eßer## Stack Control 4023aa99676SStefan Eßer 4033aa99676SStefan EßerThese commands control the stack. 4043aa99676SStefan Eßer 4053aa99676SStefan Eßer**c** 4063aa99676SStefan Eßer 4073aa99676SStefan Eßer: Removes all items from ("clears") the stack. 4083aa99676SStefan Eßer 4093aa99676SStefan Eßer**d** 4103aa99676SStefan Eßer 4113aa99676SStefan Eßer: Copies the item on top of the stack ("duplicates") and pushes the copy onto 4123aa99676SStefan Eßer the stack. 4133aa99676SStefan Eßer 4143aa99676SStefan Eßer**r** 4153aa99676SStefan Eßer 4163aa99676SStefan Eßer: Swaps ("reverses") the two top items on the stack. 4173aa99676SStefan Eßer 4183aa99676SStefan Eßer**R** 4193aa99676SStefan Eßer 4203aa99676SStefan Eßer: Pops ("removes") the top value from the stack. 4213aa99676SStefan Eßer 4223aa99676SStefan Eßer## Register Control 4233aa99676SStefan Eßer 4243aa99676SStefan EßerThese commands control registers (see the **REGISTERS** section). 4253aa99676SStefan Eßer 4263aa99676SStefan Eßer**s***r* 4273aa99676SStefan Eßer 4283aa99676SStefan Eßer: Pops the value off the top of the stack and stores it into register *r*. 4293aa99676SStefan Eßer 4303aa99676SStefan Eßer**l***r* 4313aa99676SStefan Eßer 4323aa99676SStefan Eßer: Copies the value in register *r* and pushes it onto the stack. This does not 4333aa99676SStefan Eßer alter the contents of *r*. 4343aa99676SStefan Eßer 4353aa99676SStefan Eßer**S***r* 4363aa99676SStefan Eßer 4373aa99676SStefan Eßer: Pops the value off the top of the (main) stack and pushes it onto the stack 4383aa99676SStefan Eßer of register *r*. The previous value of the register becomes inaccessible. 4393aa99676SStefan Eßer 4403aa99676SStefan Eßer**L***r* 4413aa99676SStefan Eßer 4423aa99676SStefan Eßer: Pops the value off the top of the stack for register *r* and push it onto 4433aa99676SStefan Eßer the main stack. The previous value in the stack for register *r*, if any, is 4443aa99676SStefan Eßer now accessible via the **l***r* command. 4453aa99676SStefan Eßer 4463aa99676SStefan Eßer## Parameters 4473aa99676SStefan Eßer 4483aa99676SStefan EßerThese commands control the values of **ibase**, **obase**, and **scale**. Also 4493aa99676SStefan Eßersee the **SYNTAX** section. 4503aa99676SStefan Eßer 4513aa99676SStefan Eßer**i** 4523aa99676SStefan Eßer 4533aa99676SStefan Eßer: Pops the value off of the top of the stack and uses it to set **ibase**, 4543aa99676SStefan Eßer which must be between **2** and **16**, inclusive. 4553aa99676SStefan Eßer 4563aa99676SStefan Eßer If the value on top of the stack has any *scale*, the *scale* is ignored. 4573aa99676SStefan Eßer 4583aa99676SStefan Eßer**o** 4593aa99676SStefan Eßer 4603aa99676SStefan Eßer: Pops the value off of the top of the stack and uses it to set **obase**, 4613aa99676SStefan Eßer which must be between **2** and **DC_BASE_MAX**, inclusive (see the 4623aa99676SStefan Eßer **LIMITS** section). 4633aa99676SStefan Eßer 4643aa99676SStefan Eßer If the value on top of the stack has any *scale*, the *scale* is ignored. 4653aa99676SStefan Eßer 4663aa99676SStefan Eßer**k** 4673aa99676SStefan Eßer 4683aa99676SStefan Eßer: Pops the value off of the top of the stack and uses it to set **scale**, 4693aa99676SStefan Eßer which must be non-negative. 4703aa99676SStefan Eßer 4713aa99676SStefan Eßer If the value on top of the stack has any *scale*, the *scale* is ignored. 4723aa99676SStefan Eßer 4733aa99676SStefan Eßer**I** 4743aa99676SStefan Eßer 4753aa99676SStefan Eßer: Pushes the current value of **ibase** onto the main stack. 4763aa99676SStefan Eßer 4773aa99676SStefan Eßer**O** 4783aa99676SStefan Eßer 4793aa99676SStefan Eßer: Pushes the current value of **obase** onto the main stack. 4803aa99676SStefan Eßer 4813aa99676SStefan Eßer**K** 4823aa99676SStefan Eßer 4833aa99676SStefan Eßer: Pushes the current value of **scale** onto the main stack. 4843aa99676SStefan Eßer 4853aa99676SStefan Eßer**T** 4863aa99676SStefan Eßer 4873aa99676SStefan Eßer: Pushes the maximum allowable value of **ibase** onto the main stack. 4883aa99676SStefan Eßer 4893aa99676SStefan Eßer This is a **non-portable extension**. 4903aa99676SStefan Eßer 4913aa99676SStefan Eßer**U** 4923aa99676SStefan Eßer 4933aa99676SStefan Eßer: Pushes the maximum allowable value of **obase** onto the main stack. 4943aa99676SStefan Eßer 4953aa99676SStefan Eßer This is a **non-portable extension**. 4963aa99676SStefan Eßer 4973aa99676SStefan Eßer**V** 4983aa99676SStefan Eßer 4993aa99676SStefan Eßer: Pushes the maximum allowable value of **scale** onto the main stack. 5003aa99676SStefan Eßer 5013aa99676SStefan Eßer This is a **non-portable extension**. 5023aa99676SStefan Eßer 5033aa99676SStefan Eßer## Strings 5043aa99676SStefan Eßer 5053aa99676SStefan EßerThe following commands control strings. 5063aa99676SStefan Eßer 5073aa99676SStefan Eßerdc(1) can work with both numbers and strings, and registers (see the 5083aa99676SStefan Eßer**REGISTERS** section) can hold both strings and numbers. dc(1) always knows 5093aa99676SStefan Eßerwhether the contents of a register are a string or a number. 5103aa99676SStefan Eßer 5113aa99676SStefan EßerWhile arithmetic operations have to have numbers, and will print an error if 5123aa99676SStefan Eßergiven a string, other commands accept strings. 5133aa99676SStefan Eßer 5143aa99676SStefan EßerStrings can also be executed as macros. For example, if the string **[1pR]** is 5153aa99676SStefan Eßerexecuted as a macro, then the code **1pR** is executed, meaning that the **1** 5163aa99676SStefan Eßerwill be printed with a newline after and then popped from the stack. 5173aa99676SStefan Eßer 5183aa99676SStefan Eßer**\[**_characters_**\]** 5193aa99676SStefan Eßer 5203aa99676SStefan Eßer: Makes a string containing *characters* and pushes it onto the stack. 5213aa99676SStefan Eßer 5223aa99676SStefan Eßer If there are brackets (**\[** and **\]**) in the string, then they must be 5233aa99676SStefan Eßer balanced. Unbalanced brackets can be escaped using a backslash (**\\**) 5243aa99676SStefan Eßer character. 5253aa99676SStefan Eßer 5263aa99676SStefan Eßer If there is a backslash character in the string, the character after it 5273aa99676SStefan Eßer (even another backslash) is put into the string verbatim, but the (first) 5283aa99676SStefan Eßer backslash is not. 5293aa99676SStefan Eßer 5303aa99676SStefan Eßer**a** 5313aa99676SStefan Eßer 5323aa99676SStefan Eßer: The value on top of the stack is popped. 5333aa99676SStefan Eßer 5343aa99676SStefan Eßer If it is a number, it is truncated and its absolute value is taken. The 5353aa99676SStefan Eßer result mod **UCHAR_MAX+1** is calculated. If that result is **0**, push an 5363aa99676SStefan Eßer empty string; otherwise, push a one-character string where the character is 5373aa99676SStefan Eßer the result of the mod interpreted as an ASCII character. 5383aa99676SStefan Eßer 5393aa99676SStefan Eßer If it is a string, then a new string is made. If the original string is 5403aa99676SStefan Eßer empty, the new string is empty. If it is not, then the first character of 5413aa99676SStefan Eßer the original string is used to create the new string as a one-character 5423aa99676SStefan Eßer string. The new string is then pushed onto the stack. 5433aa99676SStefan Eßer 5443aa99676SStefan Eßer This is a **non-portable extension**. 5453aa99676SStefan Eßer 5463aa99676SStefan Eßer**x** 5473aa99676SStefan Eßer 5483aa99676SStefan Eßer: Pops a value off of the top of the stack. 5493aa99676SStefan Eßer 5503aa99676SStefan Eßer If it is a number, it is pushed back onto the stack. 5513aa99676SStefan Eßer 5523aa99676SStefan Eßer If it is a string, it is executed as a macro. 5533aa99676SStefan Eßer 5543aa99676SStefan Eßer This behavior is the norm whenever a macro is executed, whether by this 5553aa99676SStefan Eßer command or by the conditional execution commands below. 5563aa99676SStefan Eßer 5573aa99676SStefan Eßer**\>***r* 5583aa99676SStefan Eßer 5593aa99676SStefan Eßer: Pops two values off of the stack that must be numbers and compares them. If 5603aa99676SStefan Eßer the first value is greater than the second, then the contents of register 5613aa99676SStefan Eßer *r* are executed. 5623aa99676SStefan Eßer 5633aa99676SStefan Eßer For example, **0 1>a** will execute the contents of register **a**, and 5643aa99676SStefan Eßer **1 0>a** will not. 5653aa99676SStefan Eßer 5663aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 5673aa99676SStefan Eßer and reset (see the **RESET** section). 5683aa99676SStefan Eßer 5693aa99676SStefan Eßer**>***r***e***s* 5703aa99676SStefan Eßer 5713aa99676SStefan Eßer: Like the above, but will execute register *s* if the comparison fails. 5723aa99676SStefan Eßer 5733aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 5743aa99676SStefan Eßer and reset (see the **RESET** section). 5753aa99676SStefan Eßer 5763aa99676SStefan Eßer This is a **non-portable extension**. 5773aa99676SStefan Eßer 5783aa99676SStefan Eßer**!\>***r* 5793aa99676SStefan Eßer 5803aa99676SStefan Eßer: Pops two values off of the stack that must be numbers and compares them. If 5813aa99676SStefan Eßer the first value is not greater than the second (less than or equal to), then 5823aa99676SStefan Eßer the contents of register *r* are executed. 5833aa99676SStefan Eßer 5843aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 5853aa99676SStefan Eßer and reset (see the **RESET** section). 5863aa99676SStefan Eßer 5873aa99676SStefan Eßer**!\>***r***e***s* 5883aa99676SStefan Eßer 5893aa99676SStefan Eßer: Like the above, but will execute register *s* if the comparison fails. 5903aa99676SStefan Eßer 5913aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 5923aa99676SStefan Eßer and reset (see the **RESET** section). 5933aa99676SStefan Eßer 5943aa99676SStefan Eßer This is a **non-portable extension**. 5953aa99676SStefan Eßer 5963aa99676SStefan Eßer**\<***r* 5973aa99676SStefan Eßer 5983aa99676SStefan Eßer: Pops two values off of the stack that must be numbers and compares them. If 5993aa99676SStefan Eßer the first value is less than the second, then the contents of register *r* 6003aa99676SStefan Eßer are executed. 6013aa99676SStefan Eßer 6023aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6033aa99676SStefan Eßer and reset (see the **RESET** section). 6043aa99676SStefan Eßer 6053aa99676SStefan Eßer**\<***r***e***s* 6063aa99676SStefan Eßer 6073aa99676SStefan Eßer: Like the above, but will execute register *s* if the comparison fails. 6083aa99676SStefan Eßer 6093aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6103aa99676SStefan Eßer and reset (see the **RESET** section). 6113aa99676SStefan Eßer 6123aa99676SStefan Eßer This is a **non-portable extension**. 6133aa99676SStefan Eßer 6143aa99676SStefan Eßer**!\<***r* 6153aa99676SStefan Eßer 6163aa99676SStefan Eßer: Pops two values off of the stack that must be numbers and compares them. If 6173aa99676SStefan Eßer the first value is not less than the second (greater than or equal to), then 6183aa99676SStefan Eßer the contents of register *r* are executed. 6193aa99676SStefan Eßer 6203aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6213aa99676SStefan Eßer and reset (see the **RESET** section). 6223aa99676SStefan Eßer 6233aa99676SStefan Eßer**!\<***r***e***s* 6243aa99676SStefan Eßer 6253aa99676SStefan Eßer: Like the above, but will execute register *s* if the comparison fails. 6263aa99676SStefan Eßer 6273aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6283aa99676SStefan Eßer and reset (see the **RESET** section). 6293aa99676SStefan Eßer 6303aa99676SStefan Eßer This is a **non-portable extension**. 6313aa99676SStefan Eßer 6323aa99676SStefan Eßer**=***r* 6333aa99676SStefan Eßer 6343aa99676SStefan Eßer: Pops two values off of the stack that must be numbers and compares them. If 6353aa99676SStefan Eßer the first value is equal to the second, then the contents of register *r* 6363aa99676SStefan Eßer are executed. 6373aa99676SStefan Eßer 6383aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6393aa99676SStefan Eßer and reset (see the **RESET** section). 6403aa99676SStefan Eßer 6413aa99676SStefan Eßer**=***r***e***s* 6423aa99676SStefan Eßer 6433aa99676SStefan Eßer: Like the above, but will execute register *s* if the comparison fails. 6443aa99676SStefan Eßer 6453aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6463aa99676SStefan Eßer and reset (see the **RESET** section). 6473aa99676SStefan Eßer 6483aa99676SStefan Eßer This is a **non-portable extension**. 6493aa99676SStefan Eßer 6503aa99676SStefan Eßer**!=***r* 6513aa99676SStefan Eßer 6523aa99676SStefan Eßer: Pops two values off of the stack that must be numbers and compares them. If 6533aa99676SStefan Eßer the first value is not equal to the second, then the contents of register 6543aa99676SStefan Eßer *r* are executed. 6553aa99676SStefan Eßer 6563aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6573aa99676SStefan Eßer and reset (see the **RESET** section). 6583aa99676SStefan Eßer 6593aa99676SStefan Eßer**!=***r***e***s* 6603aa99676SStefan Eßer 6613aa99676SStefan Eßer: Like the above, but will execute register *s* if the comparison fails. 6623aa99676SStefan Eßer 6633aa99676SStefan Eßer If either or both of the values are not numbers, dc(1) will raise an error 6643aa99676SStefan Eßer and reset (see the **RESET** section). 6653aa99676SStefan Eßer 6663aa99676SStefan Eßer This is a **non-portable extension**. 6673aa99676SStefan Eßer 6683aa99676SStefan Eßer**?** 6693aa99676SStefan Eßer 6703aa99676SStefan Eßer: Reads a line from the **stdin** and executes it. This is to allow macros to 6713aa99676SStefan Eßer request input from users. 6723aa99676SStefan Eßer 6733aa99676SStefan Eßer**q** 6743aa99676SStefan Eßer 6753aa99676SStefan Eßer: During execution of a macro, this exits the execution of that macro and the 6763aa99676SStefan Eßer execution of the macro that executed it. If there are no macros, or only one 6773aa99676SStefan Eßer macro executing, dc(1) exits. 6783aa99676SStefan Eßer 6793aa99676SStefan Eßer**Q** 6803aa99676SStefan Eßer 6813aa99676SStefan Eßer: Pops a value from the stack which must be non-negative and is used the 6823aa99676SStefan Eßer number of macro executions to pop off of the execution stack. If the number 6833aa99676SStefan Eßer of levels to pop is greater than the number of executing macros, dc(1) 6843aa99676SStefan Eßer exits. 6853aa99676SStefan Eßer 6863aa99676SStefan Eßer## Status 6873aa99676SStefan Eßer 6883aa99676SStefan EßerThese commands query status of the stack or its top value. 6893aa99676SStefan Eßer 6903aa99676SStefan Eßer**Z** 6913aa99676SStefan Eßer 6923aa99676SStefan Eßer: Pops a value off of the stack. 6933aa99676SStefan Eßer 6943aa99676SStefan Eßer If it is a number, calculates the number of significant decimal digits it 6953aa99676SStefan Eßer has and pushes the result. 6963aa99676SStefan Eßer 6973aa99676SStefan Eßer If it is a string, pushes the number of characters the string has. 6983aa99676SStefan Eßer 6993aa99676SStefan Eßer**X** 7003aa99676SStefan Eßer 7013aa99676SStefan Eßer: Pops a value off of the stack. 7023aa99676SStefan Eßer 7033aa99676SStefan Eßer If it is a number, pushes the *scale* of the value onto the stack. 7043aa99676SStefan Eßer 7053aa99676SStefan Eßer If it is a string, pushes **0**. 7063aa99676SStefan Eßer 7073aa99676SStefan Eßer**z** 7083aa99676SStefan Eßer 7093aa99676SStefan Eßer: Pushes the current stack depth (before execution of this command). 7103aa99676SStefan Eßer 7113aa99676SStefan Eßer## Arrays 7123aa99676SStefan Eßer 7133aa99676SStefan EßerThese commands manipulate arrays. 7143aa99676SStefan Eßer 7153aa99676SStefan Eßer**:***r* 7163aa99676SStefan Eßer 7173aa99676SStefan Eßer: Pops the top two values off of the stack. The second value will be stored in 7183aa99676SStefan Eßer the array *r* (see the **REGISTERS** section), indexed by the first value. 7193aa99676SStefan Eßer 7203aa99676SStefan Eßer**;***r* 7213aa99676SStefan Eßer 7223aa99676SStefan Eßer: Pops the value on top of the stack and uses it as an index into the array 7233aa99676SStefan Eßer *r*. The selected value is then pushed onto the stack. 7243aa99676SStefan Eßer 7253aa99676SStefan Eßer# REGISTERS 7263aa99676SStefan Eßer 7273aa99676SStefan EßerRegisters are names that can store strings, numbers, and arrays. (Number/string 7283aa99676SStefan Eßerregisters do not interfere with array registers.) 7293aa99676SStefan Eßer 7303aa99676SStefan EßerEach register is also its own stack, so the current register value is the top of 7313aa99676SStefan Eßerthe stack for the register. All registers, when first referenced, have one value 7323aa99676SStefan Eßer(**0**) in their stack. 7333aa99676SStefan Eßer 7343aa99676SStefan EßerIn non-extended register mode, a register name is just the single character that 7353aa99676SStefan Eßerfollows any command that needs a register name. The only exception is a newline 7363aa99676SStefan Eßer(**'\\n'**); it is a parse error for a newline to be used as a register name. 7373aa99676SStefan Eßer 7383aa99676SStefan Eßer## Extended Register Mode 7393aa99676SStefan Eßer 7403aa99676SStefan EßerUnlike most other dc(1) implentations, this dc(1) provides nearly unlimited 7413aa99676SStefan Eßeramounts of registers, if extended register mode is enabled. 7423aa99676SStefan Eßer 7433aa99676SStefan EßerIf extended register mode is enabled (**-x** or **--extended-register** 7443aa99676SStefan Eßercommand-line arguments are given), then normal single character registers are 7453aa99676SStefan Eßerused *unless* the character immediately following a command that needs a 7463aa99676SStefan Eßerregister name is a space (according to **isspace()**) and not a newline 7473aa99676SStefan Eßer(**'\\n'**). 7483aa99676SStefan Eßer 7493aa99676SStefan EßerIn that case, the register name is found according to the regex 7503aa99676SStefan Eßer**\[a-z\]\[a-z0-9\_\]\*** (like bc(1) identifiers), and it is a parse error if 7513aa99676SStefan Eßerthe next non-space characters do not match that regex. 7523aa99676SStefan Eßer 7533aa99676SStefan Eßer# RESET 7543aa99676SStefan Eßer 7553aa99676SStefan EßerWhen dc(1) encounters an error or a signal that it has a non-default handler 7563aa99676SStefan Eßerfor, it resets. This means that several things happen. 7573aa99676SStefan Eßer 7583aa99676SStefan EßerFirst, any macros that are executing are stopped and popped off the stack. 7593aa99676SStefan EßerThe behavior is not unlike that of exceptions in programming languages. Then 7603aa99676SStefan Eßerthe execution point is set so that any code waiting to execute (after all 7613aa99676SStefan Eßermacros returned) is skipped. 7623aa99676SStefan Eßer 7633aa99676SStefan EßerThus, when dc(1) resets, it skips any remaining code waiting to be executed. 7643aa99676SStefan EßerThen, if it is interactive mode, and the error was not a fatal error (see the 7653aa99676SStefan Eßer**EXIT STATUS** section), it asks for more input; otherwise, it exits with the 7663aa99676SStefan Eßerappropriate return code. 7673aa99676SStefan Eßer 7683aa99676SStefan Eßer# PERFORMANCE 7693aa99676SStefan Eßer 7703aa99676SStefan EßerMost dc(1) implementations use **char** types to calculate the value of **1** 7713aa99676SStefan Eßerdecimal digit at a time, but that can be slow. This dc(1) does something 7723aa99676SStefan Eßerdifferent. 7733aa99676SStefan Eßer 7743aa99676SStefan EßerIt uses large integers to calculate more than **1** decimal digit at a time. If 7753aa99676SStefan Eßerbuilt in a environment where **DC_LONG_BIT** (see the **LIMITS** section) is 7763aa99676SStefan Eßer**64**, then each integer has **9** decimal digits. If built in an environment 7773aa99676SStefan Eßerwhere **DC_LONG_BIT** is **32** then each integer has **4** decimal digits. This 7783aa99676SStefan Eßervalue (the number of decimal digits per large integer) is called 7793aa99676SStefan Eßer**DC_BASE_DIGS**. 7803aa99676SStefan Eßer 7813aa99676SStefan EßerIn addition, this dc(1) uses an even larger integer for overflow checking. This 7823aa99676SStefan Eßerinteger type depends on the value of **DC_LONG_BIT**, but is always at least 7833aa99676SStefan Eßertwice as large as the integer type used to store digits. 7843aa99676SStefan Eßer 7853aa99676SStefan Eßer# LIMITS 7863aa99676SStefan Eßer 7873aa99676SStefan EßerThe following are the limits on dc(1): 7883aa99676SStefan Eßer 7893aa99676SStefan Eßer**DC_LONG_BIT** 7903aa99676SStefan Eßer 7913aa99676SStefan Eßer: The number of bits in the **long** type in the environment where dc(1) was 7923aa99676SStefan Eßer built. This determines how many decimal digits can be stored in a single 7933aa99676SStefan Eßer large integer (see the **PERFORMANCE** section). 7943aa99676SStefan Eßer 7953aa99676SStefan Eßer**DC_BASE_DIGS** 7963aa99676SStefan Eßer 7973aa99676SStefan Eßer: The number of decimal digits per large integer (see the **PERFORMANCE** 7983aa99676SStefan Eßer section). Depends on **DC_LONG_BIT**. 7993aa99676SStefan Eßer 8003aa99676SStefan Eßer**DC_BASE_POW** 8013aa99676SStefan Eßer 8023aa99676SStefan Eßer: The max decimal number that each large integer can store (see 8033aa99676SStefan Eßer **DC_BASE_DIGS**) plus **1**. Depends on **DC_BASE_DIGS**. 8043aa99676SStefan Eßer 8053aa99676SStefan Eßer**DC_OVERFLOW_MAX** 8063aa99676SStefan Eßer 8073aa99676SStefan Eßer: The max number that the overflow type (see the **PERFORMANCE** section) can 8083aa99676SStefan Eßer hold. Depends on **DC_LONG_BIT**. 8093aa99676SStefan Eßer 8103aa99676SStefan Eßer**DC_BASE_MAX** 8113aa99676SStefan Eßer 8123aa99676SStefan Eßer: The maximum output base. Set at **DC_BASE_POW**. 8133aa99676SStefan Eßer 8143aa99676SStefan Eßer**DC_DIM_MAX** 8153aa99676SStefan Eßer 8163aa99676SStefan Eßer: The maximum size of arrays. Set at **SIZE_MAX-1**. 8173aa99676SStefan Eßer 8183aa99676SStefan Eßer**DC_SCALE_MAX** 8193aa99676SStefan Eßer 8203aa99676SStefan Eßer: The maximum **scale**. Set at **DC_OVERFLOW_MAX-1**. 8213aa99676SStefan Eßer 8223aa99676SStefan Eßer**DC_STRING_MAX** 8233aa99676SStefan Eßer 8243aa99676SStefan Eßer: The maximum length of strings. Set at **DC_OVERFLOW_MAX-1**. 8253aa99676SStefan Eßer 8263aa99676SStefan Eßer**DC_NAME_MAX** 8273aa99676SStefan Eßer 8283aa99676SStefan Eßer: The maximum length of identifiers. Set at **DC_OVERFLOW_MAX-1**. 8293aa99676SStefan Eßer 8303aa99676SStefan Eßer**DC_NUM_MAX** 8313aa99676SStefan Eßer 8323aa99676SStefan Eßer: The maximum length of a number (in decimal digits), which includes digits 8333aa99676SStefan Eßer after the decimal point. Set at **DC_OVERFLOW_MAX-1**. 8343aa99676SStefan Eßer 8353aa99676SStefan EßerExponent 8363aa99676SStefan Eßer 8373aa99676SStefan Eßer: The maximum allowable exponent (positive or negative). Set at 8383aa99676SStefan Eßer **DC_OVERFLOW_MAX**. 8393aa99676SStefan Eßer 8403aa99676SStefan EßerNumber of vars 8413aa99676SStefan Eßer 8423aa99676SStefan Eßer: The maximum number of vars/arrays. Set at **SIZE_MAX-1**. 8433aa99676SStefan Eßer 8443aa99676SStefan EßerThese limits are meant to be effectively non-existent; the limits are so large 8453aa99676SStefan Eßer(at least on 64-bit machines) that there should not be any point at which they 8463aa99676SStefan Eßerbecome a problem. In fact, memory should be exhausted before these limits should 8473aa99676SStefan Eßerbe hit. 8483aa99676SStefan Eßer 8493aa99676SStefan Eßer# ENVIRONMENT VARIABLES 8503aa99676SStefan Eßer 8513aa99676SStefan Eßerdc(1) recognizes the following environment variables: 8523aa99676SStefan Eßer 8533aa99676SStefan Eßer**DC_ENV_ARGS** 8543aa99676SStefan Eßer 8553aa99676SStefan Eßer: This is another way to give command-line arguments to dc(1). They should be 8563aa99676SStefan Eßer in the same format as all other command-line arguments. These are always 8573aa99676SStefan Eßer processed first, so any files given in **DC_ENV_ARGS** will be processed 8583aa99676SStefan Eßer before arguments and files given on the command-line. This gives the user 8593aa99676SStefan Eßer the ability to set up "standard" options and files to be used at every 8603aa99676SStefan Eßer invocation. The most useful thing for such files to contain would be useful 8613aa99676SStefan Eßer functions that the user might want every time dc(1) runs. Another use would 8623aa99676SStefan Eßer be to use the **-e** option to set **scale** to a value other than **0**. 8633aa99676SStefan Eßer 8643aa99676SStefan Eßer The code that parses **DC_ENV_ARGS** will correctly handle quoted arguments, 8653aa99676SStefan Eßer but it does not understand escape sequences. For example, the string 8663aa99676SStefan Eßer **"/home/gavin/some dc file.dc"** will be correctly parsed, but the string 8673aa99676SStefan Eßer **"/home/gavin/some \"dc\" file.dc"** will include the backslashes. 8683aa99676SStefan Eßer 8693aa99676SStefan Eßer The quote parsing will handle either kind of quotes, **'** or **"**. Thus, 8703aa99676SStefan Eßer if you have a file with any number of single quotes in the name, you can use 8713aa99676SStefan Eßer double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice 8723aa99676SStefan Eßer versa if you have a file with double quotes. However, handling a file with 8733aa99676SStefan Eßer both kinds of quotes in **DC_ENV_ARGS** is not supported due to the 8743aa99676SStefan Eßer complexity of the parsing, though such files are still supported on the 8753aa99676SStefan Eßer command-line where the parsing is done by the shell. 8763aa99676SStefan Eßer 8773aa99676SStefan Eßer**DC_LINE_LENGTH** 8783aa99676SStefan Eßer 8793aa99676SStefan Eßer: If this environment variable exists and contains an integer that is greater 8803aa99676SStefan Eßer than **1** and is less than **UINT16_MAX** (**2\^16-1**), dc(1) will output 8813aa99676SStefan Eßer lines to that length, including the backslash newline combo. The default 8823aa99676SStefan Eßer line length is **70**. 8833aa99676SStefan Eßer 8843aa99676SStefan Eßer**DC_EXPR_EXIT** 8853aa99676SStefan Eßer 8863aa99676SStefan Eßer: If this variable exists (no matter the contents), dc(1) will exit 8873aa99676SStefan Eßer immediately after executing expressions and files given by the **-e** and/or 8883aa99676SStefan Eßer **-f** command-line options (and any equivalents). 8893aa99676SStefan Eßer 8903aa99676SStefan Eßer# EXIT STATUS 8913aa99676SStefan Eßer 8923aa99676SStefan Eßerdc(1) returns the following exit statuses: 8933aa99676SStefan Eßer 8943aa99676SStefan Eßer**0** 8953aa99676SStefan Eßer 8963aa99676SStefan Eßer: No error. 8973aa99676SStefan Eßer 8983aa99676SStefan Eßer**1** 8993aa99676SStefan Eßer 9003aa99676SStefan Eßer: A math error occurred. This follows standard practice of using **1** for 9013aa99676SStefan Eßer expected errors, since math errors will happen in the process of normal 9023aa99676SStefan Eßer execution. 9033aa99676SStefan Eßer 9043aa99676SStefan Eßer Math errors include divide by **0**, taking the square root of a negative 9053aa99676SStefan Eßer number, attempting to convert a negative number to a hardware integer, 9063aa99676SStefan Eßer overflow when converting a number to a hardware integer, and attempting to 9073aa99676SStefan Eßer use a non-integer where an integer is required. 9083aa99676SStefan Eßer 9093aa99676SStefan Eßer Converting to a hardware integer happens for the second operand of the power 9103aa99676SStefan Eßer (**\^**) operator. 9113aa99676SStefan Eßer 9123aa99676SStefan Eßer**2** 9133aa99676SStefan Eßer 9143aa99676SStefan Eßer: A parse error occurred. 9153aa99676SStefan Eßer 9163aa99676SStefan Eßer Parse errors include unexpected **EOF**, using an invalid character, failing 9173aa99676SStefan Eßer to find the end of a string or comment, and using a token where it is 9183aa99676SStefan Eßer invalid. 9193aa99676SStefan Eßer 9203aa99676SStefan Eßer**3** 9213aa99676SStefan Eßer 9223aa99676SStefan Eßer: A runtime error occurred. 9233aa99676SStefan Eßer 9243aa99676SStefan Eßer Runtime errors include assigning an invalid number to **ibase**, **obase**, 9253aa99676SStefan Eßer or **scale**; give a bad expression to a **read()** call, calling **read()** 9263aa99676SStefan Eßer inside of a **read()** call, type errors, and attempting an operation when 9273aa99676SStefan Eßer the stack has too few elements. 9283aa99676SStefan Eßer 9293aa99676SStefan Eßer**4** 9303aa99676SStefan Eßer 9313aa99676SStefan Eßer: A fatal error occurred. 9323aa99676SStefan Eßer 9333aa99676SStefan Eßer Fatal errors include memory allocation errors, I/O errors, failing to open 9343aa99676SStefan Eßer files, attempting to use files that do not have only ASCII characters (dc(1) 9353aa99676SStefan Eßer only accepts ASCII characters), attempting to open a directory as a file, 9363aa99676SStefan Eßer and giving invalid command-line options. 9373aa99676SStefan Eßer 9383aa99676SStefan EßerThe exit status **4** is special; when a fatal error occurs, dc(1) always exits 9393aa99676SStefan Eßerand returns **4**, no matter what mode dc(1) is in. 9403aa99676SStefan Eßer 9413aa99676SStefan EßerThe other statuses will only be returned when dc(1) is not in interactive mode 9423aa99676SStefan Eßer(see the **INTERACTIVE MODE** section), since dc(1) resets its state (see the 9433aa99676SStefan Eßer**RESET** section) and accepts more input when one of those errors occurs in 9443aa99676SStefan Eßerinteractive mode. This is also the case when interactive mode is forced by the 9453aa99676SStefan Eßer**-i** flag or **--interactive** option. 9463aa99676SStefan Eßer 9473aa99676SStefan EßerThese exit statuses allow dc(1) to be used in shell scripting with error 9483aa99676SStefan Eßerchecking, and its normal behavior can be forced by using the **-i** flag or 9493aa99676SStefan Eßer**--interactive** option. 9503aa99676SStefan Eßer 9513aa99676SStefan Eßer# INTERACTIVE MODE 9523aa99676SStefan Eßer 9533aa99676SStefan EßerLike bc(1), dc(1) has an interactive mode and a non-interactive mode. 9543aa99676SStefan EßerInteractive mode is turned on automatically when both **stdin** and **stdout** 9553aa99676SStefan Eßerare hooked to a terminal, but the **-i** flag and **--interactive** option can 9563aa99676SStefan Eßerturn it on in other cases. 9573aa99676SStefan Eßer 9583aa99676SStefan EßerIn interactive mode, dc(1) attempts to recover from errors (see the **RESET** 9593aa99676SStefan Eßersection), and in normal execution, flushes **stdout** as soon as execution is 9603aa99676SStefan Eßerdone for the current input. 9613aa99676SStefan Eßer 9623aa99676SStefan Eßer# TTY MODE 9633aa99676SStefan Eßer 9643aa99676SStefan EßerIf **stdin**, **stdout**, and **stderr** are all connected to a TTY, dc(1) turns 9653aa99676SStefan Eßeron "TTY mode." 9663aa99676SStefan Eßer 9673aa99676SStefan EßerTTY mode is required for history to be enabled (see the **COMMAND LINE HISTORY** 9683aa99676SStefan Eßersection). It is also required to enable special handling for **SIGINT** signals. 9693aa99676SStefan Eßer 9703aa99676SStefan EßerThe prompt is enabled in TTY mode. 9713aa99676SStefan Eßer 9723aa99676SStefan EßerTTY mode is different from interactive mode because interactive mode is required 9733aa99676SStefan Eßerin the [bc(1) specification][1], and interactive mode requires only **stdin** 9743aa99676SStefan Eßerand **stdout** to be connected to a terminal. 9753aa99676SStefan Eßer 9763aa99676SStefan Eßer# SIGNAL HANDLING 9773aa99676SStefan Eßer 9783aa99676SStefan EßerSending a **SIGINT** will cause dc(1) to stop execution of the current input. If 9793aa99676SStefan Eßerdc(1) is in TTY mode (see the **TTY MODE** section), it will reset (see the 9803aa99676SStefan Eßer**RESET** section). Otherwise, it will clean up and exit. 9813aa99676SStefan Eßer 9823aa99676SStefan EßerNote that "current input" can mean one of two things. If dc(1) is processing 9833aa99676SStefan Eßerinput from **stdin** in TTY mode, it will ask for more input. If dc(1) is 9843aa99676SStefan Eßerprocessing input from a file in TTY mode, it will stop processing the file and 9853aa99676SStefan Eßerstart processing the next file, if one exists, or ask for input from **stdin** 9863aa99676SStefan Eßerif no other file exists. 9873aa99676SStefan Eßer 9883aa99676SStefan EßerThis means that if a **SIGINT** is sent to dc(1) as it is executing a file, it 9893aa99676SStefan Eßercan seem as though dc(1) did not respond to the signal since it will immediately 9903aa99676SStefan Eßerstart executing the next file. This is by design; most files that users execute 9913aa99676SStefan Eßerwhen interacting with dc(1) have function definitions, which are quick to parse. 9923aa99676SStefan EßerIf a file takes a long time to execute, there may be a bug in that file. The 9933aa99676SStefan Eßerrest of the files could still be executed without problem, allowing the user to 9943aa99676SStefan Eßercontinue. 9953aa99676SStefan Eßer 9963aa99676SStefan Eßer**SIGTERM** and **SIGQUIT** cause dc(1) to clean up and exit, and it uses the 9973aa99676SStefan Eßerdefault handler for all other signals. The one exception is **SIGHUP**; in that 9983aa99676SStefan Eßercase, when dc(1) is in TTY mode, a **SIGHUP** will cause dc(1) to clean up and 9993aa99676SStefan Eßerexit. 10003aa99676SStefan Eßer 10013aa99676SStefan Eßer# COMMAND LINE HISTORY 10023aa99676SStefan Eßer 10033aa99676SStefan Eßerdc(1) supports interactive command-line editing. If dc(1) is in TTY mode (see 10043aa99676SStefan Eßerthe **TTY MODE** section), history is enabled. Previous lines can be recalled 10053aa99676SStefan Eßerand edited with the arrow keys. 10063aa99676SStefan Eßer 10073aa99676SStefan Eßer**Note**: tabs are converted to 8 spaces. 10083aa99676SStefan Eßer 10093aa99676SStefan Eßer# SEE ALSO 10103aa99676SStefan Eßer 10113aa99676SStefan Eßerbc(1) 10123aa99676SStefan Eßer 10133aa99676SStefan Eßer# STANDARDS 10143aa99676SStefan Eßer 10153aa99676SStefan EßerThe dc(1) utility operators are compliant with the operators in the bc(1) 10163aa99676SStefan Eßer[IEEE Std 1003.1-2017 (“POSIX.1-2017”)][1] specification. 10173aa99676SStefan Eßer 10183aa99676SStefan Eßer# BUGS 10193aa99676SStefan Eßer 10203aa99676SStefan EßerNone are known. Report bugs at https://git.yzena.com/gavin/bc. 10213aa99676SStefan Eßer 10223aa99676SStefan Eßer# AUTHOR 10233aa99676SStefan Eßer 102450696a6eSStefan EßerGavin D. Howard <gavin@yzena.com> and contributors. 10253aa99676SStefan Eßer 10263aa99676SStefan Eßer[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html 1027