blob: 4c57b0dd03e3b6c2b3b8e5f3863a72ff1973de52 [file] [log] [blame]
Gavin Howard7a62fec2020-07-01 13:40:37 -06001.\"
2.\" SPDX-License-Identifier: BSD-2-Clause
3.\"
4.\" Copyright (c) 2018-2020 Gavin D. Howard and contributors.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions are met:
8.\"
9.\" * Redistributions of source code must retain the above copyright notice,
10.\" this list of conditions and the following disclaimer.
11.\"
12.\" * Redistributions in binary form must reproduce the above copyright notice,
13.\" this list of conditions and the following disclaimer in the documentation
14.\" and/or other materials provided with the distribution.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26.\" POSSIBILITY OF SUCH DAMAGE.
27.\"
Gavin Howarda51c5012020-07-01 13:39:23 -060028.TH "DC" "1" "July 2020" "Gavin D. Howard" "General Commands Manual"
Gavin Howard55905652020-06-29 21:44:49 -060029.SH Name
30.PP
31dc \- arbitrary\-precision reverse\-Polish notation calculator
32.SH SYNOPSIS
33.PP
34\f[B]dc\f[] [\f[B]\-hiPvVx\f[]] [\f[B]\-\-version\f[]]
35[\f[B]\-\-help\f[]] [\f[B]\-\-interactive\f[]] [\f[B]\-\-no\-prompt\f[]]
36[\f[B]\-\-extended\-register\f[]] [\f[B]\-e\f[] \f[I]expr\f[]]
37[\f[B]\-\-expression\f[]=\f[I]expr\f[]...] [\f[B]\-f\f[]
38\f[I]file\f[]...] [\f[B]\-file\f[]=\f[I]file\f[]...] [\f[I]file\f[]...]
39.SH DESCRIPTION
40.PP
41dc(1) is an arbitrary\-precision calculator.
42It uses a stack (reverse Polish notation) to store numbers and results
43of computations.
44Arithmetic operations pop arguments off of the stack and push the
45results.
46.PP
47If no files are given on the command\-line as extra arguments (i.e., not
48as \f[B]\-f\f[] or \f[B]\-\-file\f[] arguments), then dc(1) reads from
49\f[B]stdin\f[].
50Otherwise, those files are processed, and dc(1) will then exit.
51.PP
52This is different from the dc(1) on OpenBSD and possibly other dc(1)
53implementations, where \f[B]\-e\f[] (\f[B]\-\-expression\f[]) and
54\f[B]\-f\f[] (\f[B]\-\-file\f[]) arguments cause dc(1) to execute them
55and exit.
56The reason for this is that this dc(1) allows users to set arguments in
57the environment variable \f[B]DC_ENV_ARGS\f[] (see the \f[B]ENVIRONMENT
58VARIABLES\f[] section).
59Any expressions given on the command\-line should be used to set up a
60standard environment.
61For example, if a user wants the \f[B]scale\f[] always set to
62\f[B]10\f[], they can set \f[B]DC_ENV_ARGS\f[] to \f[B]\-e 10k\f[], and
63this dc(1) will always start with a \f[B]scale\f[] of \f[B]10\f[].
64.PP
65If users want to have dc(1) exit after processing all input from
66\f[B]\-e\f[] and \f[B]\-f\f[] arguments (and their equivalents), then
67they can just simply add \f[B]\-e q\f[] as the last command\-line
68argument or define the environment variable \f[B]DC_EXPR_EXIT\f[].
69.SH OPTIONS
70.PP
71The following are the options that dc(1) accepts.
Gavin Howard67207b32020-07-01 21:13:43 -060072.TP
73.B \f[B]\-h\f[], \f[B]\-\-help\f[]
Gavin Howard55905652020-06-29 21:44:49 -060074Prints a usage message and quits.
Gavin Howard67207b32020-07-01 21:13:43 -060075.RS
Gavin Howard55905652020-06-29 21:44:49 -060076.RE
Gavin Howard67207b32020-07-01 21:13:43 -060077.TP
78.B \f[B]\-v\f[], \f[B]\-V\f[], \f[B]\-\-version\f[]
Gavin Howard55905652020-06-29 21:44:49 -060079Print the version information (copyright header) and exit.
Gavin Howard67207b32020-07-01 21:13:43 -060080.RS
Gavin Howard55905652020-06-29 21:44:49 -060081.RE
Gavin Howard67207b32020-07-01 21:13:43 -060082.TP
83.B \f[B]\-i\f[], \f[B]\-\-interactive\f[]
Gavin Howard55905652020-06-29 21:44:49 -060084Forces interactive mode.
85(See the \f[B]INTERACTIVE MODE\f[] section.)
Gavin Howard67207b32020-07-01 21:13:43 -060086.RS
Gavin Howard55905652020-06-29 21:44:49 -060087.PP
88This is a \f[B]non\-portable extension\f[].
89.RE
Gavin Howard67207b32020-07-01 21:13:43 -060090.TP
91.B \f[B]\-P\f[], \f[B]\-\-no\-prompt\f[]
Gavin Howard55905652020-06-29 21:44:49 -060092Disables the prompt in TTY mode.
93(The prompt is only enabled in TTY mode.
94See the \f[B]TTY MODE\f[] section) This is mostly for those users that
95do not want a prompt or are not used to having them in dc(1).
96Most of those users would want to put this option in
97\f[B]DC_ENV_ARGS\f[].
Gavin Howard67207b32020-07-01 21:13:43 -060098.RS
Gavin Howard55905652020-06-29 21:44:49 -060099.PP
100This is a \f[B]non\-portable extension\f[].
101.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600102.TP
103.B \f[B]\-x\f[] \f[B]\-\-extended\-register\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600104Enables extended register mode.
105See the \f[I]Extended Register Mode\f[] subsection of the
106\f[B]REGISTERS\f[] section for more information.
Gavin Howard67207b32020-07-01 21:13:43 -0600107.RS
Gavin Howard55905652020-06-29 21:44:49 -0600108.PP
109This is a \f[B]non\-portable extension\f[].
110.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600111.TP
112.B \f[B]\-e\f[] \f[I]expr\f[], \f[B]\-\-expression\f[]=\f[I]expr\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600113Evaluates \f[I]expr\f[].
114If multiple expressions are given, they are evaluated in order.
115If files are given as well (see below), the expressions and files are
116evaluated in the order given.
117This means that if a file is given before an expression, the file is
118read in and evaluated first.
Gavin Howard67207b32020-07-01 21:13:43 -0600119.RS
Gavin Howard55905652020-06-29 21:44:49 -0600120.PP
Gavin Howardd459be02020-07-30 20:30:09 -0600121After processing all expressions and files, dc(1) will exit, unless
122\f[B]\-\f[] (\f[B]stdin\f[]) was given as an argument at least once to
123\f[B]\-f\f[] or \f[B]\-\-file\f[].
Gavin Howard55905652020-06-29 21:44:49 -0600124.PP
125This is a \f[B]non\-portable extension\f[].
126.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600127.TP
128.B \f[B]\-f\f[] \f[I]file\f[], \f[B]\-\-file\f[]=\f[I]file\f[]
Gavin Howard03defed2020-07-03 12:52:22 -0600129Reads in \f[I]file\f[] and evaluates it, line by line, as though it were
130read through \f[B]stdin\f[].
Gavin Howard55905652020-06-29 21:44:49 -0600131If expressions are also given (see above), the expressions are evaluated
132in the order given.
Gavin Howard67207b32020-07-01 21:13:43 -0600133.RS
Gavin Howard55905652020-06-29 21:44:49 -0600134.PP
Gavin Howardd459be02020-07-30 20:30:09 -0600135After processing all expressions and files, dc(1) will exit, unless
136\f[B]\-\f[] (\f[B]stdin\f[]) was given as an argument at least once to
137\f[B]\-f\f[] or \f[B]\-\-file\f[].
Gavin Howard90a5b2f2020-08-03 08:25:59 -0600138However, if any other \f[B]\-e\f[], \f[B]\-\-expression\f[],
139\f[B]\-f\f[], or \f[B]\-\-file\f[] arguments are given after that, bc(1)
140will give a fatal error and exit.
Gavin Howard55905652020-06-29 21:44:49 -0600141.PP
142This is a \f[B]non\-portable extension\f[].
143.RE
144.PP
145All long options are \f[B]non\-portable extensions\f[].
146.SH STDOUT
147.PP
148Any non\-error output is written to \f[B]stdout\f[].
149.PP
150\f[B]Note\f[]: Unlike other dc(1) implementations, this dc(1) will issue
151a fatal error (see the \f[B]EXIT STATUS\f[] section) if it cannot write
152to \f[B]stdout\f[], so if \f[B]stdout\f[] is closed, as in \f[B]dc
153>&\-\f[], it will quit with an error.
154This is done so that dc(1) can report problems when \f[B]stdout\f[] is
155redirected to a file.
156.PP
157If there are scripts that depend on the behavior of other dc(1)
158implementations, it is recommended that those scripts be changed to
159redirect \f[B]stdout\f[] to \f[B]/dev/null\f[].
160.SH STDERR
161.PP
162Any error output is written to \f[B]stderr\f[].
163.PP
164\f[B]Note\f[]: Unlike other dc(1) implementations, this dc(1) will issue
165a fatal error (see the \f[B]EXIT STATUS\f[] section) if it cannot write
166to \f[B]stderr\f[], so if \f[B]stderr\f[] is closed, as in \f[B]dc
1672>&\-\f[], it will quit with an error.
168This is done so that dc(1) can exit with an error code when
169\f[B]stderr\f[] is redirected to a file.
170.PP
171If there are scripts that depend on the behavior of other dc(1)
172implementations, it is recommended that those scripts be changed to
173redirect \f[B]stderr\f[] to \f[B]/dev/null\f[].
174.SH SYNTAX
175.PP
176Each item in the input source code, either a number (see the
177\f[B]NUMBERS\f[] section) or a command (see the \f[B]COMMANDS\f[]
178section), is processed and executed, in order.
179Input is processed immediately when entered.
180.PP
181\f[B]ibase\f[] is a register (see the \f[B]REGISTERS\f[] section) that
182determines how to interpret constant numbers.
183It is the "input" base, or the number base used for interpreting input
184numbers.
185\f[B]ibase\f[] is initially \f[B]10\f[].
186The max allowable value for \f[B]ibase\f[] is \f[B]16\f[].
187The min allowable value for \f[B]ibase\f[] is \f[B]2\f[].
188The max allowable value for \f[B]ibase\f[] can be queried in dc(1)
189programs with the \f[B]T\f[] command.
190.PP
191\f[B]obase\f[] is a register (see the \f[B]REGISTERS\f[] section) that
192determines how to output results.
193It is the "output" base, or the number base used for outputting numbers.
194\f[B]obase\f[] is initially \f[B]10\f[].
195The max allowable value for \f[B]obase\f[] is \f[B]DC_BASE_MAX\f[] and
196can be queried with the \f[B]U\f[] command.
197The min allowable value for \f[B]obase\f[] is \f[B]2\f[].
198Values are output in the specified base.
199.PP
200The \f[I]scale\f[] of an expression is the number of digits in the
201result of the expression right of the decimal point, and \f[B]scale\f[]
202is a register (see the \f[B]REGISTERS\f[] section) that sets the
203precision of any operations (with exceptions).
204\f[B]scale\f[] is initially \f[B]0\f[].
205\f[B]scale\f[] cannot be negative.
206The max allowable value for \f[B]scale\f[] can be queried in dc(1)
207programs with the \f[B]V\f[] command.
208.SS Comments
209.PP
210Comments go from \f[B]#\f[] until, and not including, the next newline.
211This is a \f[B]non\-portable extension\f[].
212.SH NUMBERS
213.PP
214Numbers are strings made up of digits, uppercase letters up to
215\f[B]F\f[], and at most \f[B]1\f[] period for a radix.
216Numbers can have up to \f[B]DC_NUM_MAX\f[] digits.
217Uppercase letters are equal to \f[B]9\f[] + their position in the
218alphabet (i.e., \f[B]A\f[] equals \f[B]10\f[], or \f[B]9+1\f[]).
219If a digit or letter makes no sense with the current value of
220\f[B]ibase\f[], they are set to the value of the highest valid digit in
221\f[B]ibase\f[].
222.PP
223Single\-character numbers (i.e., \f[B]A\f[] alone) take the value that
224they would have if they were valid digits, regardless of the value of
225\f[B]ibase\f[].
226This means that \f[B]A\f[] alone always equals decimal \f[B]10\f[] and
227\f[B]F\f[] alone always equals decimal \f[B]15\f[].
228.SH COMMANDS
229.PP
230The valid commands are listed below.
231.SS Printing
232.PP
233These commands are used for printing.
Gavin Howard67207b32020-07-01 21:13:43 -0600234.TP
235.B \f[B]p\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600236Prints the value on top of the stack, whether number or string, and
237prints a newline after.
Gavin Howard67207b32020-07-01 21:13:43 -0600238.RS
Gavin Howard55905652020-06-29 21:44:49 -0600239.PP
240This does not alter the stack.
241.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600242.TP
243.B \f[B]n\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600244Prints the value on top of the stack, whether number or string, and pops
245it off of the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600246.RS
Gavin Howard55905652020-06-29 21:44:49 -0600247.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600248.TP
249.B \f[B]P\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600250Pops a value off the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600251.RS
Gavin Howard55905652020-06-29 21:44:49 -0600252.PP
253If the value is a number, it is truncated and the absolute value of the
254result is printed as though \f[B]obase\f[] is \f[B]UCHAR_MAX+1\f[] and
255each digit is interpreted as an ASCII character, making it a byte
256stream.
257.PP
258If the value is a string, it is printed without a trailing newline.
259.PP
260This is a \f[B]non\-portable extension\f[].
261.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600262.TP
263.B \f[B]f\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600264Prints the entire contents of the stack, in order from newest to oldest,
265without altering anything.
Gavin Howard67207b32020-07-01 21:13:43 -0600266.RS
Gavin Howard55905652020-06-29 21:44:49 -0600267.PP
268Users should use this command when they get lost.
269.RE
270.SS Arithmetic
271.PP
272These are the commands used for arithmetic.
Gavin Howard67207b32020-07-01 21:13:43 -0600273.TP
274.B \f[B]+\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600275The top two values are popped off the stack, added, and the result is
276pushed onto the stack.
277The \f[I]scale\f[] of the result is equal to the max \f[I]scale\f[] of
278both operands.
Gavin Howard67207b32020-07-01 21:13:43 -0600279.RS
Gavin Howard55905652020-06-29 21:44:49 -0600280.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600281.TP
282.B \f[B]\-\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600283The top two values are popped off the stack, subtracted, and the result
284is pushed onto the stack.
285The \f[I]scale\f[] of the result is equal to the max \f[I]scale\f[] of
286both operands.
Gavin Howard67207b32020-07-01 21:13:43 -0600287.RS
Gavin Howard55905652020-06-29 21:44:49 -0600288.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600289.TP
290.B \f[B]*\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600291The top two values are popped off the stack, multiplied, and the result
292is pushed onto the stack.
293If \f[B]a\f[] is the \f[I]scale\f[] of the first expression and
294\f[B]b\f[] is the \f[I]scale\f[] of the second expression, the
295\f[I]scale\f[] of the result is equal to
296\f[B]min(a+b,max(scale,a,b))\f[] where \f[B]min()\f[] and \f[B]max()\f[]
297return the obvious values.
Gavin Howard67207b32020-07-01 21:13:43 -0600298.RS
Gavin Howard55905652020-06-29 21:44:49 -0600299.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600300.TP
301.B \f[B]/\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600302The top two values are popped off the stack, divided, and the result is
303pushed onto the stack.
304The \f[I]scale\f[] of the result is equal to \f[B]scale\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600305.RS
Gavin Howard55905652020-06-29 21:44:49 -0600306.PP
307The first value popped off of the stack must be non\-zero.
308.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600309.TP
310.B \f[B]%\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600311The top two values are popped off the stack, remaindered, and the result
312is pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600313.RS
314.PP
Gavin Howard55905652020-06-29 21:44:49 -0600315Remaindering is equivalent to 1) Computing \f[B]a/b\f[] to current
Gavin Howard67207b32020-07-01 21:13:43 -0600316\f[B]scale\f[], and 2) Using the result of step 1 to calculate
317\f[B]a\-(a/b)*b\f[] to \f[I]scale\f[]
318\f[B]max(scale+scale(b),scale(a))\f[].
Gavin Howard55905652020-06-29 21:44:49 -0600319.PP
320The first value popped off of the stack must be non\-zero.
321.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600322.TP
323.B \f[B]~\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600324The top two values are popped off the stack, divided and remaindered,
325and the results (divided first, remainder second) are pushed onto the
326stack.
327This is equivalent to \f[B]x y / x y %\f[] except that \f[B]x\f[] and
328\f[B]y\f[] are only evaluated once.
Gavin Howard67207b32020-07-01 21:13:43 -0600329.RS
Gavin Howard55905652020-06-29 21:44:49 -0600330.PP
331The first value popped off of the stack must be non\-zero.
332.PP
333This is a \f[B]non\-portable extension\f[].
334.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600335.TP
336.B \f[B]^\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600337The top two values are popped off the stack, the second is raised to the
338power of the first, and the result is pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600339.RS
Gavin Howard55905652020-06-29 21:44:49 -0600340.PP
341The first value popped off of the stack must be an integer, and if that
342value is negative, the second value popped off of the stack must be
343non\-zero.
344.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600345.TP
346.B \f[B]v\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600347The top value is popped off the stack, its square root is computed, and
348the result is pushed onto the stack.
349The \f[I]scale\f[] of the result is equal to \f[B]scale\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600350.RS
Gavin Howard55905652020-06-29 21:44:49 -0600351.PP
352The value popped off of the stack must be non\-negative.
353.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600354.TP
355.B \f[B]_\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600356If this command \f[I]immediately\f[] precedes a number (i.e., no spaces
357or other commands), then that number is input as a negative number.
Gavin Howard67207b32020-07-01 21:13:43 -0600358.RS
Gavin Howard55905652020-06-29 21:44:49 -0600359.PP
360Otherwise, the top value on the stack is popped and copied, and the copy
361is negated and pushed onto the stack.
362This behavior without a number is a \f[B]non\-portable extension\f[].
363.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600364.TP
365.B \f[B]b\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600366The top value is popped off the stack, and if it is zero, it is pushed
367back onto the stack.
368Otherwise, its absolute value is pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600369.RS
Gavin Howard55905652020-06-29 21:44:49 -0600370.PP
371This is a \f[B]non\-portable extension\f[].
372.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600373.TP
374.B \f[B]|\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600375The top three values are popped off the stack, a modular exponentiation
376is computed, and the result is pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600377.RS
Gavin Howard55905652020-06-29 21:44:49 -0600378.PP
379The first value popped is used as the reduction modulus and must be an
380integer and non\-zero.
381The second value popped is used as the exponent and must be an integer
382and non\-negative.
383The third value popped is the base and must be an integer.
384.PP
385This is a \f[B]non\-portable extension\f[].
386.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600387.TP
388.B \f[B]G\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600389The top two values are popped off of the stack, they are compared, and a
390\f[B]1\f[] is pushed if they are equal, or \f[B]0\f[] otherwise.
Gavin Howard67207b32020-07-01 21:13:43 -0600391.RS
Gavin Howard55905652020-06-29 21:44:49 -0600392.PP
393This is a \f[B]non\-portable extension\f[].
394.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600395.TP
396.B \f[B]N\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600397The top value is popped off of the stack, and if it a \f[B]0\f[], a
398\f[B]1\f[] is pushed; otherwise, a \f[B]0\f[] is pushed.
Gavin Howard67207b32020-07-01 21:13:43 -0600399.RS
Gavin Howard55905652020-06-29 21:44:49 -0600400.PP
401This is a \f[B]non\-portable extension\f[].
402.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600403.TP
404.B \f[B](\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600405The top two values are popped off of the stack, they are compared, and a
406\f[B]1\f[] is pushed if the first is less than the second, or \f[B]0\f[]
407otherwise.
Gavin Howard67207b32020-07-01 21:13:43 -0600408.RS
Gavin Howard55905652020-06-29 21:44:49 -0600409.PP
410This is a \f[B]non\-portable extension\f[].
411.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600412.TP
413.B \f[B]{\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600414The top two values are popped off of the stack, they are compared, and a
415\f[B]1\f[] is pushed if the first is less than or equal to the second,
416or \f[B]0\f[] otherwise.
Gavin Howard67207b32020-07-01 21:13:43 -0600417.RS
Gavin Howard55905652020-06-29 21:44:49 -0600418.PP
419This is a \f[B]non\-portable extension\f[].
420.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600421.TP
422.B \f[B])\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600423The top two values are popped off of the stack, they are compared, and a
424\f[B]1\f[] is pushed if the first is greater than the second, or
425\f[B]0\f[] otherwise.
Gavin Howard67207b32020-07-01 21:13:43 -0600426.RS
Gavin Howard55905652020-06-29 21:44:49 -0600427.PP
428This is a \f[B]non\-portable extension\f[].
429.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600430.TP
431.B \f[B]}\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600432The top two values are popped off of the stack, they are compared, and a
433\f[B]1\f[] is pushed if the first is greater than or equal to the
434second, or \f[B]0\f[] otherwise.
Gavin Howard67207b32020-07-01 21:13:43 -0600435.RS
Gavin Howard55905652020-06-29 21:44:49 -0600436.PP
437This is a \f[B]non\-portable extension\f[].
438.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600439.TP
440.B \f[B]M\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600441The top two values are popped off of the stack.
442If they are both non\-zero, a \f[B]1\f[] is pushed onto the stack.
443If either of them is zero, or both of them are, then a \f[B]0\f[] is
444pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600445.RS
Gavin Howard55905652020-06-29 21:44:49 -0600446.PP
447This is like the \f[B]&&\f[] operator in bc(1), and it is \f[I]not\f[] a
448short\-circuit operator.
449.PP
450This is a \f[B]non\-portable extension\f[].
451.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600452.TP
453.B \f[B]m\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600454The top two values are popped off of the stack.
455If at least one of them is non\-zero, a \f[B]1\f[] is pushed onto the
456stack.
457If both of them are zero, then a \f[B]0\f[] is pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600458.RS
Gavin Howard55905652020-06-29 21:44:49 -0600459.PP
460This is like the \f[B]||\f[] operator in bc(1), and it is \f[I]not\f[] a
461short\-circuit operator.
462.PP
463This is a \f[B]non\-portable extension\f[].
464.RE
465.SS Stack Control
466.PP
467These commands control the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600468.TP
469.B \f[B]c\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600470Removes all items from ("clears") the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600471.RS
Gavin Howard55905652020-06-29 21:44:49 -0600472.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600473.TP
474.B \f[B]d\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600475Copies the item on top of the stack ("duplicates") and pushes the copy
476onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600477.RS
Gavin Howard55905652020-06-29 21:44:49 -0600478.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600479.TP
480.B \f[B]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600481Swaps ("reverses") the two top items on the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600482.RS
Gavin Howard55905652020-06-29 21:44:49 -0600483.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600484.TP
485.B \f[B]R\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600486Pops ("removes") the top value from the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600487.RS
Gavin Howard55905652020-06-29 21:44:49 -0600488.RE
489.SS Register Control
490.PP
491These commands control registers (see the \f[B]REGISTERS\f[] section).
Gavin Howard67207b32020-07-01 21:13:43 -0600492.TP
493.B \f[B]s\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600494Pops the value off the top of the stack and stores it into register
495\f[I]r\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600496.RS
Gavin Howard55905652020-06-29 21:44:49 -0600497.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600498.TP
499.B \f[B]l\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600500Copies the value in register \f[I]r\f[] and pushes it onto the stack.
501This does not alter the contents of \f[I]r\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600502.RS
Gavin Howard55905652020-06-29 21:44:49 -0600503.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600504.TP
505.B \f[B]S\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600506Pops the value off the top of the (main) stack and pushes it onto the
507stack of register \f[I]r\f[].
508The previous value of the register becomes inaccessible.
Gavin Howard67207b32020-07-01 21:13:43 -0600509.RS
Gavin Howard55905652020-06-29 21:44:49 -0600510.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600511.TP
512.B \f[B]L\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600513Pops the value off the top of the stack for register \f[I]r\f[] and push
514it onto the main stack.
515The previous value in the stack for register \f[I]r\f[], if any, is now
516accessible via the \f[B]l\f[]\f[I]r\f[] command.
Gavin Howard67207b32020-07-01 21:13:43 -0600517.RS
Gavin Howard55905652020-06-29 21:44:49 -0600518.RE
519.SS Parameters
520.PP
521These commands control the values of \f[B]ibase\f[], \f[B]obase\f[], and
522\f[B]scale\f[].
523Also see the \f[B]SYNTAX\f[] section.
Gavin Howard67207b32020-07-01 21:13:43 -0600524.TP
525.B \f[B]i\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600526Pops the value off of the top of the stack and uses it to set
527\f[B]ibase\f[], which must be between \f[B]2\f[] and \f[B]16\f[],
528inclusive.
Gavin Howard67207b32020-07-01 21:13:43 -0600529.RS
Gavin Howard55905652020-06-29 21:44:49 -0600530.PP
531If the value on top of the stack has any \f[I]scale\f[], the
532\f[I]scale\f[] is ignored.
533.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600534.TP
535.B \f[B]o\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600536Pops the value off of the top of the stack and uses it to set
537\f[B]obase\f[], which must be between \f[B]2\f[] and
538\f[B]DC_BASE_MAX\f[], inclusive (see the \f[B]LIMITS\f[] section).
Gavin Howard67207b32020-07-01 21:13:43 -0600539.RS
Gavin Howard55905652020-06-29 21:44:49 -0600540.PP
541If the value on top of the stack has any \f[I]scale\f[], the
542\f[I]scale\f[] is ignored.
543.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600544.TP
545.B \f[B]k\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600546Pops the value off of the top of the stack and uses it to set
547\f[B]scale\f[], which must be non\-negative.
Gavin Howard67207b32020-07-01 21:13:43 -0600548.RS
Gavin Howard55905652020-06-29 21:44:49 -0600549.PP
550If the value on top of the stack has any \f[I]scale\f[], the
551\f[I]scale\f[] is ignored.
552.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600553.TP
554.B \f[B]I\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600555Pushes the current value of \f[B]ibase\f[] onto the main stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600556.RS
Gavin Howard55905652020-06-29 21:44:49 -0600557.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600558.TP
559.B \f[B]O\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600560Pushes the current value of \f[B]obase\f[] onto the main stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600561.RS
Gavin Howard55905652020-06-29 21:44:49 -0600562.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600563.TP
564.B \f[B]K\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600565Pushes the current value of \f[B]scale\f[] onto the main stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600566.RS
Gavin Howard55905652020-06-29 21:44:49 -0600567.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600568.TP
569.B \f[B]T\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600570Pushes the maximum allowable value of \f[B]ibase\f[] onto the main
571stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600572.RS
Gavin Howard55905652020-06-29 21:44:49 -0600573.PP
574This is a \f[B]non\-portable extension\f[].
575.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600576.TP
577.B \f[B]U\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600578Pushes the maximum allowable value of \f[B]obase\f[] onto the main
579stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600580.RS
Gavin Howard55905652020-06-29 21:44:49 -0600581.PP
582This is a \f[B]non\-portable extension\f[].
583.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600584.TP
585.B \f[B]V\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600586Pushes the maximum allowable value of \f[B]scale\f[] onto the main
587stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600588.RS
Gavin Howard55905652020-06-29 21:44:49 -0600589.PP
590This is a \f[B]non\-portable extension\f[].
591.RE
592.SS Strings
593.PP
594The following commands control strings.
595.PP
596dc(1) can work with both numbers and strings, and registers (see the
597\f[B]REGISTERS\f[] section) can hold both strings and numbers.
598dc(1) always knows whether the contents of a register are a string or a
599number.
600.PP
601While arithmetic operations have to have numbers, and will print an
602error if given a string, other commands accept strings.
603.PP
604Strings can also be executed as macros.
605For example, if the string \f[B][1pR]\f[] is executed as a macro, then
606the code \f[B]1pR\f[] is executed, meaning that the \f[B]1\f[] will be
607printed with a newline after and then popped from the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600608.TP
609.B \f[B][\f[]\f[I]characters\f[]\f[B]]\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600610Makes a string containing \f[I]characters\f[] and pushes it onto the
611stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600612.RS
Gavin Howard55905652020-06-29 21:44:49 -0600613.PP
614If there are brackets (\f[B][\f[] and \f[B]]\f[]) in the string, then
615they must be balanced.
616Unbalanced brackets can be escaped using a backslash (\f[B]\\\f[])
617character.
618.PP
619If there is a backslash character in the string, the character after it
620(even another backslash) is put into the string verbatim, but the
621(first) backslash is not.
622.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600623.TP
624.B \f[B]a\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600625The value on top of the stack is popped.
Gavin Howard67207b32020-07-01 21:13:43 -0600626.RS
Gavin Howard55905652020-06-29 21:44:49 -0600627.PP
628If it is a number, it is truncated and its absolute value is taken.
629The result mod \f[B]UCHAR_MAX+1\f[] is calculated.
630If that result is \f[B]0\f[], push an empty string; otherwise, push a
631one\-character string where the character is the result of the mod
632interpreted as an ASCII character.
633.PP
634If it is a string, then a new string is made.
635If the original string is empty, the new string is empty.
636If it is not, then the first character of the original string is used to
637create the new string as a one\-character string.
638The new string is then pushed onto the stack.
639.PP
640This is a \f[B]non\-portable extension\f[].
641.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600642.TP
643.B \f[B]x\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600644Pops a value off of the top of the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600645.RS
Gavin Howard55905652020-06-29 21:44:49 -0600646.PP
647If it is a number, it is pushed back onto the stack.
648.PP
649If it is a string, it is executed as a macro.
650.PP
651This behavior is the norm whenever a macro is executed, whether by this
652command or by the conditional execution commands below.
653.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600654.TP
655.B \f[B]>\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600656Pops two values off of the stack that must be numbers and compares them.
657If the first value is greater than the second, then the contents of
658register \f[I]r\f[] are executed.
Gavin Howard67207b32020-07-01 21:13:43 -0600659.RS
Gavin Howard55905652020-06-29 21:44:49 -0600660.PP
661For example, \f[B]0 1>a\f[] will execute the contents of register
662\f[B]a\f[], and \f[B]1 0>a\f[] will not.
663.PP
664If either or both of the values are not numbers, dc(1) will raise an
665error and reset (see the \f[B]RESET\f[] section).
666.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600667.TP
668.B \f[B]>\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600669Like the above, but will execute register \f[I]s\f[] if the comparison
670fails.
Gavin Howard67207b32020-07-01 21:13:43 -0600671.RS
Gavin Howard55905652020-06-29 21:44:49 -0600672.PP
673If either or both of the values are not numbers, dc(1) will raise an
674error and reset (see the \f[B]RESET\f[] section).
675.PP
676This is a \f[B]non\-portable extension\f[].
677.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600678.TP
679.B \f[B]!>\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600680Pops two values off of the stack that must be numbers and compares them.
681If the first value is not greater than the second (less than or equal
682to), then the contents of register \f[I]r\f[] are executed.
Gavin Howard67207b32020-07-01 21:13:43 -0600683.RS
Gavin Howard55905652020-06-29 21:44:49 -0600684.PP
685If either or both of the values are not numbers, dc(1) will raise an
686error and reset (see the \f[B]RESET\f[] section).
687.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600688.TP
689.B \f[B]!>\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600690Like the above, but will execute register \f[I]s\f[] if the comparison
691fails.
Gavin Howard67207b32020-07-01 21:13:43 -0600692.RS
Gavin Howard55905652020-06-29 21:44:49 -0600693.PP
694If either or both of the values are not numbers, dc(1) will raise an
695error and reset (see the \f[B]RESET\f[] section).
696.PP
697This is a \f[B]non\-portable extension\f[].
698.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600699.TP
700.B \f[B]<\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600701Pops two values off of the stack that must be numbers and compares them.
702If the first value is less than the second, then the contents of
703register \f[I]r\f[] are executed.
Gavin Howard67207b32020-07-01 21:13:43 -0600704.RS
Gavin Howard55905652020-06-29 21:44:49 -0600705.PP
706If either or both of the values are not numbers, dc(1) will raise an
707error and reset (see the \f[B]RESET\f[] section).
708.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600709.TP
710.B \f[B]<\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600711Like the above, but will execute register \f[I]s\f[] if the comparison
712fails.
Gavin Howard67207b32020-07-01 21:13:43 -0600713.RS
Gavin Howard55905652020-06-29 21:44:49 -0600714.PP
715If either or both of the values are not numbers, dc(1) will raise an
716error and reset (see the \f[B]RESET\f[] section).
717.PP
718This is a \f[B]non\-portable extension\f[].
719.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600720.TP
721.B \f[B]!<\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600722Pops two values off of the stack that must be numbers and compares them.
723If the first value is not less than the second (greater than or equal
724to), then the contents of register \f[I]r\f[] are executed.
Gavin Howard67207b32020-07-01 21:13:43 -0600725.RS
Gavin Howard55905652020-06-29 21:44:49 -0600726.PP
727If either or both of the values are not numbers, dc(1) will raise an
728error and reset (see the \f[B]RESET\f[] section).
729.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600730.TP
731.B \f[B]!<\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600732Like the above, but will execute register \f[I]s\f[] if the comparison
733fails.
Gavin Howard67207b32020-07-01 21:13:43 -0600734.RS
Gavin Howard55905652020-06-29 21:44:49 -0600735.PP
736If either or both of the values are not numbers, dc(1) will raise an
737error and reset (see the \f[B]RESET\f[] section).
738.PP
739This is a \f[B]non\-portable extension\f[].
740.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600741.TP
742.B \f[B]=\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600743Pops two values off of the stack that must be numbers and compares them.
744If the first value is equal to the second, then the contents of register
745\f[I]r\f[] are executed.
Gavin Howard67207b32020-07-01 21:13:43 -0600746.RS
Gavin Howard55905652020-06-29 21:44:49 -0600747.PP
748If either or both of the values are not numbers, dc(1) will raise an
749error and reset (see the \f[B]RESET\f[] section).
750.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600751.TP
752.B \f[B]=\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600753Like the above, but will execute register \f[I]s\f[] if the comparison
754fails.
Gavin Howard67207b32020-07-01 21:13:43 -0600755.RS
Gavin Howard55905652020-06-29 21:44:49 -0600756.PP
757If either or both of the values are not numbers, dc(1) will raise an
758error and reset (see the \f[B]RESET\f[] section).
759.PP
760This is a \f[B]non\-portable extension\f[].
761.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600762.TP
763.B \f[B]!=\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600764Pops two values off of the stack that must be numbers and compares them.
765If the first value is not equal to the second, then the contents of
766register \f[I]r\f[] are executed.
Gavin Howard67207b32020-07-01 21:13:43 -0600767.RS
Gavin Howard55905652020-06-29 21:44:49 -0600768.PP
769If either or both of the values are not numbers, dc(1) will raise an
770error and reset (see the \f[B]RESET\f[] section).
771.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600772.TP
773.B \f[B]!=\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600774Like the above, but will execute register \f[I]s\f[] if the comparison
775fails.
Gavin Howard67207b32020-07-01 21:13:43 -0600776.RS
Gavin Howard55905652020-06-29 21:44:49 -0600777.PP
778If either or both of the values are not numbers, dc(1) will raise an
779error and reset (see the \f[B]RESET\f[] section).
780.PP
781This is a \f[B]non\-portable extension\f[].
782.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600783.TP
784.B \f[B]?\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600785Reads a line from the \f[B]stdin\f[] and executes it.
786This is to allow macros to request input from users.
Gavin Howard67207b32020-07-01 21:13:43 -0600787.RS
Gavin Howard55905652020-06-29 21:44:49 -0600788.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600789.TP
790.B \f[B]q\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600791During execution of a macro, this exits the execution of that macro and
792the execution of the macro that executed it.
793If there are no macros, or only one macro executing, dc(1) exits.
Gavin Howard67207b32020-07-01 21:13:43 -0600794.RS
Gavin Howard55905652020-06-29 21:44:49 -0600795.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600796.TP
797.B \f[B]Q\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600798Pops a value from the stack which must be non\-negative and is used the
799number of macro executions to pop off of the execution stack.
800If the number of levels to pop is greater than the number of executing
801macros, dc(1) exits.
Gavin Howard67207b32020-07-01 21:13:43 -0600802.RS
Gavin Howard55905652020-06-29 21:44:49 -0600803.RE
804.SS Status
805.PP
806These commands query status of the stack or its top value.
Gavin Howard67207b32020-07-01 21:13:43 -0600807.TP
808.B \f[B]Z\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600809Pops a value off of the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600810.RS
Gavin Howard55905652020-06-29 21:44:49 -0600811.PP
812If it is a number, calculates the number of significant decimal digits
813it has and pushes the result.
814.PP
815If it is a string, pushes the number of characters the string has.
816.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600817.TP
818.B \f[B]X\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600819Pops a value off of the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600820.RS
Gavin Howard55905652020-06-29 21:44:49 -0600821.PP
822If it is a number, pushes the \f[I]scale\f[] of the value onto the
823stack.
824.PP
825If it is a string, pushes \f[B]0\f[].
826.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600827.TP
828.B \f[B]z\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600829Pushes the current stack depth (before execution of this command).
Gavin Howard67207b32020-07-01 21:13:43 -0600830.RS
Gavin Howard55905652020-06-29 21:44:49 -0600831.RE
832.SS Arrays
833.PP
834These commands manipulate arrays.
Gavin Howard67207b32020-07-01 21:13:43 -0600835.TP
836.B \f[B]:\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600837Pops the top two values off of the stack.
838The second value will be stored in the array \f[I]r\f[] (see the
839\f[B]REGISTERS\f[] section), indexed by the first value.
Gavin Howard67207b32020-07-01 21:13:43 -0600840.RS
Gavin Howard55905652020-06-29 21:44:49 -0600841.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600842.TP
843.B \f[B];\f[]\f[I]r\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600844Pops the value on top of the stack and uses it as an index into the
845array \f[I]r\f[].
846The selected value is then pushed onto the stack.
Gavin Howard67207b32020-07-01 21:13:43 -0600847.RS
Gavin Howard55905652020-06-29 21:44:49 -0600848.RE
849.SH REGISTERS
850.PP
851Registers are names that can store strings, numbers, and arrays.
852(Number/string registers do not interfere with array registers.)
853.PP
854Each register is also its own stack, so the current register value is
855the top of the stack for the register.
856All registers, when first referenced, have one value (\f[B]0\f[]) in
857their stack.
858.PP
859In non\-extended register mode, a register name is just the single
860character that follows any command that needs a register name.
861The only exception is a newline (\f[B]\[aq]\\n\[aq]\f[]); it is a parse
862error for a newline to be used as a register name.
863.SS Extended Register Mode
864.PP
865Unlike most other dc(1) implentations, this dc(1) provides nearly
866unlimited amounts of registers, if extended register mode is enabled.
867.PP
868If extended register mode is enabled (\f[B]\-x\f[] or
869\f[B]\-\-extended\-register\f[] command\-line arguments are given), then
870normal single character registers are used \f[I]unless\f[] the character
871immediately following a command that needs a register name is a space
872(according to \f[B]isspace()\f[]) and not a newline
873(\f[B]\[aq]\\n\[aq]\f[]).
874.PP
875In that case, the register name is found according to the regex
876\f[B][a\-z][a\-z0\-9_]*\f[] (like bc(1) identifiers), and it is a parse
877error if the next non\-space characters do not match that regex.
878.SH RESET
879.PP
880When dc(1) encounters an error or a signal that it has a non\-default
881handler for, it resets.
882This means that several things happen.
883.PP
884First, any macros that are executing are stopped and popped off the
885stack.
886The behavior is not unlike that of exceptions in programming languages.
887Then the execution point is set so that any code waiting to execute
888(after all macros returned) is skipped.
889.PP
890Thus, when dc(1) resets, it skips any remaining code waiting to be
891executed.
892Then, if it is interactive mode, and the error was not a fatal error
893(see the \f[B]EXIT STATUS\f[] section), it asks for more input;
894otherwise, it exits with the appropriate return code.
895.SH PERFORMANCE
896.PP
897Most dc(1) implementations use \f[B]char\f[] types to calculate the
898value of \f[B]1\f[] decimal digit at a time, but that can be slow.
899This dc(1) does something different.
900.PP
901It uses large integers to calculate more than \f[B]1\f[] decimal digit
902at a time.
903If built in a environment where \f[B]DC_LONG_BIT\f[] (see the
904\f[B]LIMITS\f[] section) is \f[B]64\f[], then each integer has
905\f[B]9\f[] decimal digits.
906If built in an environment where \f[B]DC_LONG_BIT\f[] is \f[B]32\f[]
907then each integer has \f[B]4\f[] decimal digits.
908This value (the number of decimal digits per large integer) is called
909\f[B]DC_BASE_DIGS\f[].
910.PP
911In addition, this dc(1) uses an even larger integer for overflow
912checking.
913This integer type depends on the value of \f[B]DC_LONG_BIT\f[], but is
914always at least twice as large as the integer type used to store digits.
915.SH LIMITS
916.PP
Gavin Howarde29e0f92020-06-28 12:46:58 -0600917The following are the limits on dc(1):
Gavin Howard67207b32020-07-01 21:13:43 -0600918.TP
919.B \f[B]DC_LONG_BIT\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600920The number of bits in the \f[B]long\f[] type in the environment where
921dc(1) was built.
922This determines how many decimal digits can be stored in a single large
923integer (see the \f[B]PERFORMANCE\f[] section).
Gavin Howard67207b32020-07-01 21:13:43 -0600924.RS
Gavin Howard55905652020-06-29 21:44:49 -0600925.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600926.TP
927.B \f[B]DC_BASE_DIGS\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600928The number of decimal digits per large integer (see the
929\f[B]PERFORMANCE\f[] section).
930Depends on \f[B]DC_LONG_BIT\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600931.RS
Gavin Howard55905652020-06-29 21:44:49 -0600932.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600933.TP
934.B \f[B]DC_BASE_POW\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600935The max decimal number that each large integer can store (see
936\f[B]DC_BASE_DIGS\f[]) plus \f[B]1\f[].
937Depends on \f[B]DC_BASE_DIGS\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600938.RS
Gavin Howard55905652020-06-29 21:44:49 -0600939.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600940.TP
941.B \f[B]DC_OVERFLOW_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600942The max number that the overflow type (see the \f[B]PERFORMANCE\f[]
943section) can hold.
944Depends on \f[B]DC_LONG_BIT\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600945.RS
Gavin Howard55905652020-06-29 21:44:49 -0600946.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600947.TP
948.B \f[B]DC_BASE_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600949The maximum output base.
950Set at \f[B]DC_BASE_POW\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600951.RS
Gavin Howard55905652020-06-29 21:44:49 -0600952.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600953.TP
954.B \f[B]DC_DIM_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600955The maximum size of arrays.
956Set at \f[B]SIZE_MAX\-1\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600957.RS
Gavin Howard55905652020-06-29 21:44:49 -0600958.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600959.TP
960.B \f[B]DC_SCALE_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600961The maximum \f[B]scale\f[].
962Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600963.RS
Gavin Howard55905652020-06-29 21:44:49 -0600964.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600965.TP
966.B \f[B]DC_STRING_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600967The maximum length of strings.
968Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600969.RS
Gavin Howard55905652020-06-29 21:44:49 -0600970.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600971.TP
972.B \f[B]DC_NAME_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600973The maximum length of identifiers.
974Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600975.RS
Gavin Howard55905652020-06-29 21:44:49 -0600976.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600977.TP
978.B \f[B]DC_NUM_MAX\f[]
Gavin Howard55905652020-06-29 21:44:49 -0600979The maximum length of a number (in decimal digits), which includes
980digits after the decimal point.
981Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600982.RS
Gavin Howard55905652020-06-29 21:44:49 -0600983.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600984.TP
985.B Exponent
Gavin Howard55905652020-06-29 21:44:49 -0600986The maximum allowable exponent (positive or negative).
987Set at \f[B]DC_OVERFLOW_MAX\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600988.RS
Gavin Howard55905652020-06-29 21:44:49 -0600989.RE
Gavin Howard67207b32020-07-01 21:13:43 -0600990.TP
991.B Number of vars
Gavin Howard55905652020-06-29 21:44:49 -0600992The maximum number of vars/arrays.
993Set at \f[B]SIZE_MAX\-1\f[].
Gavin Howard67207b32020-07-01 21:13:43 -0600994.RS
Gavin Howard55905652020-06-29 21:44:49 -0600995.RE
996.PP
997These limits are meant to be effectively non\-existent; the limits are
998so large (at least on 64\-bit machines) that there should not be any
999point at which they become a problem.
1000In fact, memory should be exhausted before these limits should be hit.
1001.SH ENVIRONMENT VARIABLES
1002.PP
Gavin Howarde29e0f92020-06-28 12:46:58 -06001003dc(1) recognizes the following environment variables:
Gavin Howard67207b32020-07-01 21:13:43 -06001004.TP
1005.B \f[B]DC_ENV_ARGS\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001006This is another way to give command\-line arguments to dc(1).
1007They should be in the same format as all other command\-line arguments.
1008These are always processed first, so any files given in
1009\f[B]DC_ENV_ARGS\f[] will be processed before arguments and files given
1010on the command\-line.
1011This gives the user the ability to set up "standard" options and files
1012to be used at every invocation.
1013The most useful thing for such files to contain would be useful
1014functions that the user might want every time dc(1) runs.
1015Another use would be to use the \f[B]\-e\f[] option to set
1016\f[B]scale\f[] to a value other than \f[B]0\f[].
Gavin Howard67207b32020-07-01 21:13:43 -06001017.RS
Gavin Howard55905652020-06-29 21:44:49 -06001018.PP
1019The code that parses \f[B]DC_ENV_ARGS\f[] will correctly handle quoted
1020arguments, but it does not understand escape sequences.
1021For example, the string \f[B]"/home/gavin/some dc file.dc"\f[] will be
1022correctly parsed, but the string \f[B]"/home/gavin/some "dc"
1023file.dc"\f[] will include the backslashes.
1024.PP
1025The quote parsing will handle either kind of quotes, \f[B]\[aq]\f[] or
1026\f[B]"\f[].
1027Thus, if you have a file with any number of single quotes in the name,
1028you can use double quotes as the outside quotes, as in \f[B]"some
1029\[aq]bc\[aq] file.bc"\f[], and vice versa if you have a file with double
1030quotes.
1031However, handling a file with both kinds of quotes in
1032\f[B]DC_ENV_ARGS\f[] is not supported due to the complexity of the
1033parsing, though such files are still supported on the command\-line
1034where the parsing is done by the shell.
1035.RE
Gavin Howard67207b32020-07-01 21:13:43 -06001036.TP
1037.B \f[B]DC_LINE_LENGTH\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001038If this environment variable exists and contains an integer that is
1039greater than \f[B]1\f[] and is less than \f[B]UINT16_MAX\f[]
1040(\f[B]2^16\-1\f[]), dc(1) will output lines to that length, including
1041the backslash newline combo.
1042The default line length is \f[B]70\f[].
Gavin Howard67207b32020-07-01 21:13:43 -06001043.RS
Gavin Howard55905652020-06-29 21:44:49 -06001044.RE
Gavin Howard67207b32020-07-01 21:13:43 -06001045.TP
1046.B \f[B]DC_EXPR_EXIT\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001047If this variable exists (no matter the contents), dc(1) will exit
1048immediately after executing expressions and files given by the
1049\f[B]\-e\f[] and/or \f[B]\-f\f[] command\-line options (and any
1050equivalents).
Gavin Howard67207b32020-07-01 21:13:43 -06001051.RS
Gavin Howard55905652020-06-29 21:44:49 -06001052.RE
1053.SH EXIT STATUS
1054.PP
Gavin Howarde29e0f92020-06-28 12:46:58 -06001055dc(1) returns the following exit statuses:
Gavin Howard67207b32020-07-01 21:13:43 -06001056.TP
1057.B \f[B]0\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001058No error.
Gavin Howard67207b32020-07-01 21:13:43 -06001059.RS
Gavin Howard55905652020-06-29 21:44:49 -06001060.RE
Gavin Howard67207b32020-07-01 21:13:43 -06001061.TP
1062.B \f[B]1\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001063A math error occurred.
1064This follows standard practice of using \f[B]1\f[] for expected errors,
1065since math errors will happen in the process of normal execution.
Gavin Howard67207b32020-07-01 21:13:43 -06001066.RS
Gavin Howard55905652020-06-29 21:44:49 -06001067.PP
1068Math errors include divide by \f[B]0\f[], taking the square root of a
1069negative number, attempting to convert a negative number to a hardware
1070integer, overflow when converting a number to a hardware integer, and
1071attempting to use a non\-integer where an integer is required.
1072.PP
1073Converting to a hardware integer happens for the second operand of the
1074power (\f[B]^\f[]) operator.
1075.RE
Gavin Howard67207b32020-07-01 21:13:43 -06001076.TP
1077.B \f[B]2\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001078A parse error occurred.
Gavin Howard67207b32020-07-01 21:13:43 -06001079.RS
Gavin Howard55905652020-06-29 21:44:49 -06001080.PP
1081Parse errors include unexpected \f[B]EOF\f[], using an invalid
1082character, failing to find the end of a string or comment, and using a
1083token where it is invalid.
1084.RE
Gavin Howard67207b32020-07-01 21:13:43 -06001085.TP
1086.B \f[B]3\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001087A runtime error occurred.
Gavin Howard67207b32020-07-01 21:13:43 -06001088.RS
Gavin Howard55905652020-06-29 21:44:49 -06001089.PP
1090Runtime errors include assigning an invalid number to \f[B]ibase\f[],
1091\f[B]obase\f[], or \f[B]scale\f[]; give a bad expression to a
1092\f[B]read()\f[] call, calling \f[B]read()\f[] inside of a
1093\f[B]read()\f[] call, type errors, and attempting an operation when the
1094stack has too few elements.
1095.RE
Gavin Howard67207b32020-07-01 21:13:43 -06001096.TP
1097.B \f[B]4\f[]
Gavin Howard55905652020-06-29 21:44:49 -06001098A fatal error occurred.
Gavin Howard67207b32020-07-01 21:13:43 -06001099.RS
Gavin Howard55905652020-06-29 21:44:49 -06001100.PP
1101Fatal errors include memory allocation errors, I/O errors, failing to
1102open files, attempting to use files that do not have only ASCII
1103characters (dc(1) only accepts ASCII characters), attempting to open a
1104directory as a file, and giving invalid command\-line options.
1105.RE
1106.PP
1107The exit status \f[B]4\f[] is special; when a fatal error occurs, dc(1)
1108always exits and returns \f[B]4\f[], no matter what mode dc(1) is in.
1109.PP
1110The other statuses will only be returned when dc(1) is not in
1111interactive mode (see the \f[B]INTERACTIVE MODE\f[] section), since
1112dc(1) resets its state (see the \f[B]RESET\f[] section) and accepts more
1113input when one of those errors occurs in interactive mode.
1114This is also the case when interactive mode is forced by the
1115\f[B]\-i\f[] flag or \f[B]\-\-interactive\f[] option.
1116.PP
1117These exit statuses allow dc(1) to be used in shell scripting with error
1118checking, and its normal behavior can be forced by using the
1119\f[B]\-i\f[] flag or \f[B]\-\-interactive\f[] option.
1120.SH INTERACTIVE MODE
1121.PP
1122Like bc(1), dc(1) has an interactive mode and a non\-interactive mode.
1123Interactive mode is turned on automatically when both \f[B]stdin\f[] and
1124\f[B]stdout\f[] are hooked to a terminal, but the \f[B]\-i\f[] flag and
1125\f[B]\-\-interactive\f[] option can turn it on in other cases.
1126.PP
1127In interactive mode, dc(1) attempts to recover from errors (see the
1128\f[B]RESET\f[] section), and in normal execution, flushes
1129\f[B]stdout\f[] as soon as execution is done for the current input.
1130.SH TTY MODE
1131.PP
1132If \f[B]stdin\f[], \f[B]stdout\f[], and \f[B]stderr\f[] are all
1133connected to a TTY, dc(1) turns on "TTY mode."
1134.PP
1135TTY mode is required for history to be enabled (see the \f[B]COMMAND
1136LINE HISTORY\f[] section).
1137It is also required to enable special handling for \f[B]SIGINT\f[]
1138signals.
1139.PP
1140The prompt is enabled in TTY mode.
1141.PP
1142TTY mode is different from interactive mode because interactive mode is
1143required in the bc(1)
1144specification (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1145and interactive mode requires only \f[B]stdin\f[] and \f[B]stdout\f[] to
1146be connected to a terminal.
1147.SH SIGNAL HANDLING
1148.PP
1149Sending a \f[B]SIGINT\f[] will cause dc(1) to stop execution of the
1150current input.
1151If dc(1) is in TTY mode (see the \f[B]TTY MODE\f[] section), it will
1152reset (see the \f[B]RESET\f[] section).
1153Otherwise, it will clean up and exit.
1154.PP
1155Note that "current input" can mean one of two things.
1156If dc(1) is processing input from \f[B]stdin\f[] in TTY mode, it will
1157ask for more input.
1158If dc(1) is processing input from a file in TTY mode, it will stop
1159processing the file and start processing the next file, if one exists,
1160or ask for input from \f[B]stdin\f[] if no other file exists.
1161.PP
1162This means that if a \f[B]SIGINT\f[] is sent to dc(1) as it is executing
1163a file, it can seem as though dc(1) did not respond to the signal since
1164it will immediately start executing the next file.
1165This is by design; most files that users execute when interacting with
1166dc(1) have function definitions, which are quick to parse.
1167If a file takes a long time to execute, there may be a bug in that file.
1168The rest of the files could still be executed without problem, allowing
1169the user to continue.
1170.PP
1171\f[B]SIGTERM\f[] and \f[B]SIGQUIT\f[] cause dc(1) to clean up and exit,
1172and it uses the default handler for all other signals.
1173The one exception is \f[B]SIGHUP\f[]; in that case, when dc(1) is in TTY
1174mode, a \f[B]SIGHUP\f[] will cause dc(1) to clean up and exit.
1175.SH COMMAND LINE HISTORY
1176.PP
1177dc(1) supports interactive command\-line editing.
1178If dc(1) is in TTY mode (see the \f[B]TTY MODE\f[] section), history is
1179enabled.
1180Previous lines can be recalled and edited with the arrow keys.
1181.PP
1182\f[B]Note\f[]: tabs are converted to 8 spaces.
1183.SH SEE ALSO
1184.PP
Gavin Howarde29e0f92020-06-28 12:46:58 -06001185bc(1)
Gavin Howard55905652020-06-29 21:44:49 -06001186.SH STANDARDS
1187.PP
1188The dc(1) utility operators are compliant with the operators in the
1189bc(1) IEEE Std 1003.1\-2017
1190(“POSIX.1\-2017”) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1191specification.
1192.SH BUGS
1193.PP
1194None are known.
1195Report bugs at https://git.yzena.com/gavin/bc.
1196.SH AUTHOR
1197.PP
1198Gavin D.
1199Howard <yzena.tech@gmail.com> and contributors.