| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | .\" $NetBSD: bc.1,v 1.6 2021/01/07 20:37:33 uwe Exp $ | | 1 | .\" $NetBSD: bc.1,v 1.7 2021/01/07 21:21:35 uwe Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" bc.1 - the bc manual | | 3 | .\" bc.1 - the bc manual |
4 | .\" | | 4 | .\" |
5 | .\" Copyright (C) 1991-1994, 1997, 2000, 2003, 2012-2017 Free Software Foundation, Inc. | | 5 | .\" Copyright (C) 1991-1994, 1997, 2000, 2003, 2012-2017 Free Software Foundation, Inc. |
6 | .\" Copyright (C) 2004, 2017 Philip A. Nelson | | 6 | .\" Copyright (C) 2004, 2017 Philip A. Nelson |
7 | .\" Copyright (C) 2017 Thomas Klausner | | 7 | .\" Copyright (C) 2017 Thomas Klausner |
8 | .\" All rights reserved. | | 8 | .\" All rights reserved. |
9 | .\" | | 9 | .\" |
10 | .\" Redistribution and use in source and binary forms, with or without | | 10 | .\" Redistribution and use in source and binary forms, with or without |
11 | .\" modification, are permitted provided that the following conditions | | 11 | .\" modification, are permitted provided that the following conditions |
12 | .\" are met: | | 12 | .\" are met: |
13 | .\" 1. Redistributions of source code must retain the above copyright | | 13 | .\" 1. Redistributions of source code must retain the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer. | | 14 | .\" notice, this list of conditions and the following disclaimer. |
| @@ -118,27 +118,28 @@ has a length of 7 and a scale of 3. | | | @@ -118,27 +118,28 @@ has a length of 7 and a scale of 3. |
118 | .El | | 118 | .El |
119 | .Ss VARIABLES | | 119 | .Ss VARIABLES |
120 | Numbers are stored in two types of variables, simple variables and | | 120 | Numbers are stored in two types of variables, simple variables and |
121 | arrays. | | 121 | arrays. |
122 | Both simple variables and array variables are named. | | 122 | Both simple variables and array variables are named. |
123 | Names begin with a letter followed by any number of letters, digits and | | 123 | Names begin with a letter followed by any number of letters, digits and |
124 | underscores. | | 124 | underscores. |
125 | All letters must be lower case. | | 125 | All letters must be lower case. |
126 | (Full alpha-numeric names are an extension. | | 126 | (Full alpha-numeric names are an extension. |
127 | In POSIX | | 127 | In POSIX |
128 | .Nm | | 128 | .Nm |
129 | all names are a single lower case letter.) | | 129 | all names are a single lower case letter.) |
130 | The type of variable is clear by the context | | 130 | The type of variable is clear by the context |
131 | because all array variable names will be followed by brackets ([]). | | 131 | because all array variable names will be followed by brackets |
| | | 132 | .Pq Ql [] . |
132 | .Pp | | 133 | .Pp |
133 | There are four special variables, | | 134 | There are four special variables, |
134 | .Ic scale , | | 135 | .Ic scale , |
135 | .Ic ibase , | | 136 | .Ic ibase , |
136 | .Ic obase , | | 137 | .Ic obase , |
137 | and | | 138 | and |
138 | .Ic last . | | 139 | .Ic last . |
139 | .Ic scale | | 140 | .Ic scale |
140 | defines how some operations use digits after the decimal point. | | 141 | defines how some operations use digits after the decimal point. |
141 | The default value of | | 142 | The default value of |
142 | .Ic scale | | 143 | .Ic scale |
143 | is 0. | | 144 | is 0. |
144 | .Ic ibase | | 145 | .Ic ibase |
| @@ -146,41 +147,41 @@ and | | | @@ -146,41 +147,41 @@ and |
146 | .Ic obase | | 147 | .Ic obase |
147 | define the conversion base for input and output numbers. | | 148 | define the conversion base for input and output numbers. |
148 | The default for both input and output is base 10. | | 149 | The default for both input and output is base 10. |
149 | .Ic last | | 150 | .Ic last |
150 | (an extension) is a variable that has the value of the last | | 151 | (an extension) is a variable that has the value of the last |
151 | printed number. | | 152 | printed number. |
152 | These will be discussed in further detail where appropriate. | | 153 | These will be discussed in further detail where appropriate. |
153 | All of these variables may have values assigned to them | | 154 | All of these variables may have values assigned to them |
154 | as well as used in expressions. | | 155 | as well as used in expressions. |
155 | .Ss COMMENTS | | 156 | .Ss COMMENTS |
156 | Comments in | | 157 | Comments in |
157 | .Nm | | 158 | .Nm |
158 | start with the characters | | 159 | start with the characters |
159 | .Dq Ic /* | | 160 | .Ql /* |
160 | and end with the characters | | 161 | and end with the characters |
161 | .Dq Ic */ . | | 162 | .Ql */ . |
162 | Comments may start anywhere and appear as a single space in the input. | | 163 | Comments may start anywhere and appear as a single space in the input. |
163 | (This causes comments to delimit other input items. | | 164 | (This causes comments to delimit other input items. |
164 | For example, a comment can not be found in the middle of | | 165 | For example, a comment can not be found in the middle of |
165 | a variable name.) | | 166 | a variable name.) |
166 | Comments include any newlines (end of line) between | | 167 | Comments include any newlines (end of line) between |
167 | the start and the end of the comment. | | 168 | the start and the end of the comment. |
168 | .Pp | | 169 | .Pp |
169 | To support the use of scripts for | | 170 | To support the use of scripts for |
170 | .Nm , | | 171 | .Nm , |
171 | a single line comment has been added as an extension. | | 172 | a single line comment has been added as an extension. |
172 | A single line comment starts at a | | 173 | A single line comment starts at a |
173 | .Dq Ic # | | 174 | .Ql # |
174 | character and continues to the next end of the line. | | 175 | character and continues to the next end of the line. |
175 | The end of line | | 176 | The end of line |
176 | character is not part of the comment and is processed normally. | | 177 | character is not part of the comment and is processed normally. |
177 | .Ss EXPRESSIONS | | 178 | .Ss EXPRESSIONS |
178 | The numbers are manipulated by expressions and statements. | | 179 | The numbers are manipulated by expressions and statements. |
179 | Since the language was designed to be interactive, statements and expressions | | 180 | Since the language was designed to be interactive, statements and expressions |
180 | are executed as soon as possible. | | 181 | are executed as soon as possible. |
181 | There is no | | 182 | There is no |
182 | .Dq main | | 183 | .Dq main |
183 | program. | | 184 | program. |
184 | Instead, code is executed as it is encountered. | | 185 | Instead, code is executed as it is encountered. |
185 | (Functions, discussed in | | 186 | (Functions, discussed in |
186 | detail later, are defined when encountered.) | | 187 | detail later, are defined when encountered.) |
| @@ -195,33 +196,35 @@ by the variable | | | @@ -195,33 +196,35 @@ by the variable |
195 | The legal values of | | 196 | The legal values of |
196 | .Ic ibase | | 197 | .Ic ibase |
197 | are 2 through 36. | | 198 | are 2 through 36. |
198 | (Base values greater than 16 are an extension.) | | 199 | (Base values greater than 16 are an extension.) |
199 | Assigning a value outside this range to | | 200 | Assigning a value outside this range to |
200 | .Ic ibase | | 201 | .Ic ibase |
201 | will result in a value of 2 or 36. | | 202 | will result in a value of 2 or 36. |
202 | Input numbers may contain the characters 0-9 and A-Z. | | 203 | Input numbers may contain the characters 0-9 and A-Z. |
203 | (Note: They must be capitals. | | 204 | (Note: They must be capitals. |
204 | Lower case letters are variable names.) | | 205 | Lower case letters are variable names.) |
205 | Single digit numbers always | | 206 | Single digit numbers always |
206 | have the value of the digit regardless of the value of | | 207 | have the value of the digit regardless of the value of |
207 | .Ic ibase . | | 208 | .Ic ibase . |
208 | (i.e. A = 10.) | | 209 | (i.e.\& |
| | | 210 | .Ql A |
| | | 211 | =\~10.) |
209 | For multi-digit numbers, | | 212 | For multi-digit numbers, |
210 | .Nm | | 213 | .Nm |
211 | changes all input digits greater or equal to ibase to the value of | | 214 | changes all input digits greater or equal to ibase to the value of |
212 | .Ic ibase - 1 . | | 215 | .Ic ibase - 1 . |
213 | This makes the number | | 216 | This makes the number |
214 | .Dq ZZZ | | 217 | .Ql ZZZ |
215 | always be the largest 3 digit number of the input base. | | 218 | always be the largest 3 digit number of the input base. |
216 | .Pp | | 219 | .Pp |
217 | Full expressions are similar to many other high level languages. | | 220 | Full expressions are similar to many other high level languages. |
218 | Since there is only one kind of number, there are no rules for mixing | | 221 | Since there is only one kind of number, there are no rules for mixing |
219 | types. | | 222 | types. |
220 | Instead, there are rules on the scale of expressions. | | 223 | Instead, there are rules on the scale of expressions. |
221 | Every expression has a scale. | | 224 | Every expression has a scale. |
222 | This is derived from the scale of original | | 225 | This is derived from the scale of original |
223 | numbers, the operation performed and in many cases, the value of the | | 226 | numbers, the operation performed and in many cases, the value of the |
224 | variable | | 227 | variable |
225 | .Ic scale . | | 228 | .Ic scale . |
226 | Legal values of the variable | | 229 | Legal values of the variable |
227 | .Ic scale | | 230 | .Ic scale |
| @@ -352,72 +355,88 @@ The boolean | | | @@ -352,72 +355,88 @@ The boolean |
352 | operators are: | | 355 | operators are: |
353 | .Bl -tag -width 15n | | 356 | .Bl -tag -width 15n |
354 | .It Ic "!expr" | | 357 | .It Ic "!expr" |
355 | The result is 1 if expr is 0. | | 358 | The result is 1 if expr is 0. |
356 | .It Ic "expr && expr" | | 359 | .It Ic "expr && expr" |
357 | The result is 1 if both expressions are non-zero. | | 360 | The result is 1 if both expressions are non-zero. |
358 | .It Ic "expr || expr" | | 361 | .It Ic "expr || expr" |
359 | The result is 1 if either expression is non-zero. | | 362 | The result is 1 if either expression is non-zero. |
360 | .El | | 363 | .El |
361 | .Pp | | 364 | .Pp |
362 | The expression precedence is as follows: (lowest to highest) | | 365 | The expression precedence is as follows: (lowest to highest) |
363 | .Bl -enum -offset indent | | 366 | .Bl -enum -offset indent |
364 | .It | | 367 | .It |
365 | || operator, left associative | | 368 | .Ql || |
| | | 369 | operator, left associative |
366 | .It | | 370 | .It |
367 | && operator, left associative | | 371 | .Ql && |
| | | 372 | operator, left associative |
368 | .It | | 373 | .It |
369 | ! operator, nonassociative | | 374 | .Ql \&! |
| | | 375 | operator, nonassociative |
370 | .It | | 376 | .It |
371 | Relational operators, left associative | | 377 | Relational operators, left associative |
372 | .It | | 378 | .It |
373 | Assignment operator, right associative | | 379 | Assignment operator, right associative |
374 | .It | | 380 | .It |
375 | + and - operators, left associative | | 381 | .Ql + |
| | | 382 | and |
| | | 383 | .Ql - |
| | | 384 | operators, left associative |
376 | .It | | 385 | .It |
377 | *, / and % operators, left associative | | 386 | .Ql * , |
| | | 387 | .Ql / |
| | | 388 | and |
| | | 389 | .Ql % |
| | | 390 | operators, left associative |
378 | .It | | 391 | .It |
379 | ^ operator, right associative | | 392 | .Ql ^ |
| | | 393 | operator, right associative |
380 | .It | | 394 | .It |
381 | unary - operator, nonassociative | | 395 | unary |
| | | 396 | .Ql - |
| | | 397 | operator, nonassociative |
382 | .It | | 398 | .It |
383 | ++ and -- operators, nonassociative | | 399 | .Ql ++ |
| | | 400 | and |
| | | 401 | .Ql -- |
| | | 402 | operators, nonassociative |
384 | .El | | 403 | .El |
385 | .Pp | | 404 | .Pp |
386 | This precedence was chosen so that POSIX compliant | | 405 | This precedence was chosen so that POSIX compliant |
387 | .Nm | | 406 | .Nm |
388 | programs will run correctly. | | 407 | programs will run correctly. |
389 | This will cause the use of the relational and logical operators to | | 408 | This will cause the use of the relational and logical operators to |
390 | have some unusual behavior when used with assignment expressions. | | 409 | have some unusual behavior when used with assignment expressions. |
391 | Consider the expression: | | 410 | Consider the expression: |
392 | .Dl Ic a = 3 < 5 | | 411 | .Dl a = 3 < 5 |
393 | .Pp | | 412 | .Pp |
394 | Most C programmers would assume this would assign the result of | | 413 | Most C programmers would assume this would assign the result of |
395 | .Dq Ic 3 < 5 | | 414 | .Dq Li 3 < 5 |
396 | (the value 1) to the variable | | 415 | (the value 1) to the variable |
397 | .Dq Ic a . | | 416 | .Va a . |
398 | What this does in | | 417 | What this does in |
399 | .Nm | | 418 | .Nm |
400 | is assign the value 3 to the variable | | 419 | is assign the value 3 to the variable |
401 | .Dq Ic a | | 420 | .Va a |
402 | and then compare 3 to 5. | | 421 | and then compare 3 to 5. |
403 | It is best to use parenthesis when using relational and logical operators | | 422 | It is best to use parenthesis when using relational and logical operators |
404 | with the assignment operators. | | 423 | with the assignment operators. |
405 | .Pp | | 424 | .Pp |
406 | There are a few more special expressions that are provided in | | 425 | There are a few more special expressions that are provided in |
407 | .Nm . | | 426 | .Nm . |
408 | These have to do with user defined functions and standard functions. | | 427 | These have to do with user defined functions and standard functions. |
409 | They all appear as | | 428 | They all appear as |
410 | .Do Ar name ( Ar parameters ) Dc . | | 429 | .Do Ar name Ns Li \&( Ns Ar parameters Ns Li \&) Dc . |
411 | See the section on functions for user defined functions. | | 430 | See the section on functions for user defined functions. |
412 | The standard functions are: | | 431 | The standard functions are: |
413 | .Bl -tag -width 20n | | 432 | .Bl -tag -width 20n |
414 | .It Fn length expression | | 433 | .It Fn length expression |
415 | The value of the length function is the number of significant digits in the | | 434 | The value of the length function is the number of significant digits in the |
416 | expression. | | 435 | expression. |
417 | .It Fn read | | 436 | .It Fn read |
418 | The read function (an extension) will read a number from the standard | | 437 | The read function (an extension) will read a number from the standard |
419 | input, regardless of where the function occurs. | | 438 | input, regardless of where the function occurs. |
420 | Beware, this can cause problems with the mixing of data and program in | | 439 | Beware, this can cause problems with the mixing of data and program in |
421 | the standard input. | | 440 | the standard input. |
422 | The best use for this function is in a previously written program that | | 441 | The best use for this function is in a previously written program that |
423 | needs input from the user, but never allows program code to be input | | 442 | needs input from the user, but never allows program code to be input |
| @@ -452,27 +471,29 @@ An improperly placed newline will cause | | | @@ -452,27 +471,29 @@ An improperly placed newline will cause |
452 | Because newlines | | 471 | Because newlines |
453 | are statement separators, it is possible to hide a newline by using | | 472 | are statement separators, it is possible to hide a newline by using |
454 | the backslash character. | | 473 | the backslash character. |
455 | The sequence | | 474 | The sequence |
456 | .Dq Ic \e<nl> , | | 475 | .Dq Ic \e<nl> , |
457 | where <nl> is the newline appears to | | 476 | where <nl> is the newline appears to |
458 | .Nm | | 477 | .Nm |
459 | as whitespace instead of a newline. | | 478 | as whitespace instead of a newline. |
460 | A statement list is a series of statements separated by semicolons and | | 479 | A statement list is a series of statements separated by semicolons and |
461 | newlines. | | 480 | newlines. |
462 | The following is a list of | | 481 | The following is a list of |
463 | .Nm | | 482 | .Nm |
464 | statements and what they do: | | 483 | statements and what they do: |
465 | (Things enclosed in brackets ([]) are optional parts of the | | 484 | (Things enclosed in brackets |
| | | 485 | .Pq Oo Oc |
| | | 486 | are optional parts of the |
466 | statement.) | | 487 | statement.) |
467 | .Bl -tag -width 15n | | 488 | .Bl -tag -width 15n |
468 | .It Ar expression | | 489 | .It Ar expression |
469 | This statement does one of two things. | | 490 | This statement does one of two things. |
470 | If the expression starts with | | 491 | If the expression starts with |
471 | .Ao Ar variable Ac Ao Ar assignment Ac ... , | | 492 | .Ao Ar variable Ac Ao Ar assignment Ac ... , |
472 | it is considered to be an assignment statement. | | 493 | it is considered to be an assignment statement. |
473 | If the expression is not an assignment statement, the | | 494 | If the expression is not an assignment statement, the |
474 | expression is evaluated and printed to the output. | | 495 | expression is evaluated and printed to the output. |
475 | After the number is printed, a newline is printed. | | 496 | After the number is printed, a newline is printed. |
476 | For example, | | 497 | For example, |
477 | .Dq Ic a=1 | | 498 | .Dq Ic a=1 |
478 | is an assignment statement and | | 499 | is an assignment statement and |
| @@ -508,71 +529,73 @@ Due to the interactive nature of | | | @@ -508,71 +529,73 @@ Due to the interactive nature of |
508 | printing a number causes the side effect of assigning the printed value to the | | 529 | printing a number causes the side effect of assigning the printed value to the |
509 | special variable | | 530 | special variable |
510 | .Ic last . | | 531 | .Ic last . |
511 | This allows the user to recover the last value printed without having | | 532 | This allows the user to recover the last value printed without having |
512 | to retype the expression that printed the number. | | 533 | to retype the expression that printed the number. |
513 | Assigning to | | 534 | Assigning to |
514 | .Ic last | | 535 | .Ic last |
515 | is legal and will | | 536 | is legal and will |
516 | overwrite the last printed value with the assigned value. | | 537 | overwrite the last printed value with the assigned value. |
517 | The newly assigned value will remain until the next number is printed | | 538 | The newly assigned value will remain until the next number is printed |
518 | or another value is assigned to | | 539 | or another value is assigned to |
519 | .Ic last . | | 540 | .Ic last . |
520 | (Some installations may allow the | | 541 | (Some installations may allow the |
521 | use of a single period (.) which is not part of a number as a short | | 542 | use of a single period |
522 | hand notation for | | 543 | .Pq Ql \&. |
| | | 544 | which is not part of a number as a short hand notation for |
523 | .Ic last . ) | | 545 | .Ic last . ) |
524 | .It Ar "string" | | 546 | .It Ar "string" |
525 | The | | 547 | The |
526 | .Ar string | | 548 | .Ar string |
527 | is printed to the output. | | 549 | is printed to the output. |
528 | Strings start with a double quote | | 550 | Strings start with a double quote |
529 | character and contain all characters until the next double quote character. | | 551 | character and contain all characters until the next double quote character. |
530 | All characters are take literally, including any newline. | | 552 | All characters are take literally, including any newline. |
531 | No newline character is printed after the string. | | 553 | No newline character is printed after the string. |
532 | .It Ic print Ar list | | 554 | .It Ic print Ar list |
533 | The | | 555 | The |
534 | .Ic print | | 556 | .Ic print |
535 | statement (an extension) provides another method of output. | | 557 | statement (an extension) provides another method of output. |
536 | The | | 558 | The |
537 | .Ar list | | 559 | .Ar list |
538 | is a list of strings and expressions separated by commas. | | 560 | is a list of strings and expressions separated by commas. |
539 | Each string or expression is printed in the order of the list. | | 561 | Each string or expression is printed in the order of the list. |
540 | No terminating newline is printed. | | 562 | No terminating newline is printed. |
541 | Expressions are evaluated and their | | 563 | Expressions are evaluated and their |
542 | value is printed and assigned to the variable | | 564 | value is printed and assigned to the variable |
543 | .Ic last . | | 565 | .Ic last . |
544 | Strings in the | | 566 | Strings in the |
545 | .Ic print | | 567 | .Ic print |
546 | statement are printed to the output and may contain special characters. | | 568 | statement are printed to the output and may contain special characters. |
547 | Special characters start with the backslash character (\e). | | 569 | Special characters start with the backslash character |
| | | 570 | .Pq Ql \e . |
548 | The special characters recognized by | | 571 | The special characters recognized by |
549 | .Nm | | 572 | .Nm |
550 | are | | 573 | are |
551 | .Sq a | | 574 | .Ql a |
552 | (alert or bell), | | 575 | (alert or bell), |
553 | .Sq b | | 576 | .Ql b |
554 | (backspace), | | 577 | (backspace), |
555 | .Sq f | | 578 | .Ql f |
556 | (form feed), | | 579 | (form feed), |
557 | .Sq n | | 580 | .Ql n |
558 | (newline), | | 581 | (newline), |
559 | .Sq r | | 582 | .Ql r |
560 | (carriage return), | | 583 | (carriage return), |
561 | .Sq q | | 584 | .Ql q |
562 | (double quote), | | 585 | (double quote), |
563 | .Sq t | | 586 | .Ql t |
564 | (tab), and | | 587 | (tab), and |
565 | .Sq \e | | 588 | .Ql \e |
566 | (backslash). | | 589 | (backslash). |
567 | Any other character following the backslash will be ignored. | | 590 | Any other character following the backslash will be ignored. |
568 | .It { Ar statement_list } | | 591 | .It { Ar statement_list } |
569 | This is the compound statement. | | 592 | This is the compound statement. |
570 | It allows multiple statements to be grouped together for execution. | | 593 | It allows multiple statements to be grouped together for execution. |
571 | .It Ic if ( Ar expression ) Ar statement1 [ Ic else Ar statement2 ] | | 594 | .It Ic if ( Ar expression ) Ar statement1 [ Ic else Ar statement2 ] |
572 | The | | 595 | The |
573 | .Ic if | | 596 | .Ic if |
574 | statement evaluates the | | 597 | statement evaluates the |
575 | .Ar expression | | 598 | .Ar expression |
576 | and executes | | 599 | and executes |
577 | .Ar statement1 | | 600 | .Ar statement1 |
578 | or | | 601 | or |
| @@ -637,27 +660,27 @@ or | | | @@ -637,27 +660,27 @@ or |
637 | are missing, nothing is evaluated at the point they would be evaluated. | | 660 | are missing, nothing is evaluated at the point they would be evaluated. |
638 | If | | 661 | If |
639 | .Ar expression2 | | 662 | .Ar expression2 |
640 | is missing, it is the same as substituting the value 1 for | | 663 | is missing, it is the same as substituting the value 1 for |
641 | .Ar expression2 . | | 664 | .Ar expression2 . |
642 | (The optional expressions are an | | 665 | (The optional expressions are an |
643 | extension. | | 666 | extension. |
644 | POSIX | | 667 | POSIX |
645 | .Nm | | 668 | .Nm |
646 | requires all three expressions.) | | 669 | requires all three expressions.) |
647 | The following is equivalent code for the | | 670 | The following is equivalent code for the |
648 | .Ic for | | 671 | .Ic for |
649 | statement: | | 672 | statement: |
650 | .Bd -literal | | 673 | .Bd -literal -offset indent |
651 | expression1; | | 674 | expression1; |
652 | while (expression2) { | | 675 | while (expression2) { |
653 | statement; | | 676 | statement; |
654 | expression3; | | 677 | expression3; |
655 | } | | 678 | } |
656 | .Ed | | 679 | .Ed |
657 | .It Ic break | | 680 | .It Ic break |
658 | This statement causes a forced exit of the most recent enclosing | | 681 | This statement causes a forced exit of the most recent enclosing |
659 | .Ic while | | 682 | .Ic while |
660 | statement or | | 683 | statement or |
661 | .Ic for | | 684 | .Ic for |
662 | statement. | | 685 | statement. |
663 | .It Ic continue | | 686 | .It Ic continue |