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