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