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