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