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