2 /* $OpenBSD: bc.y,v 1.46 2014/10/14 15:35:18 deraadt Exp $ */
5 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * This implementation of bc(1) uses concepts from the original 4.4
22 * BSD bc(1). The code itself is a complete rewrite, based on the
23 * Posix defined bc(1) grammar. Other differences include type safe
24 * usage of pointers to build the tree of emitted code, typed yacc
25 * rule values, dynamic allocation of all data structures and a
26 * completely rewritten lexical analyzer using lex(1).
28 * Some effort has been made to make sure that the generated code is
29 * the same as the code generated by the older version, to provide
30 * easy regression testing.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
53 #include "pathnames.h"
55 #define BC_VER "1.1-FreeBSD"
56 #define END_NODE ((ssize_t) -1)
57 #define CONST_STRING ((ssize_t) -2)
58 #define ALLOC_STRING ((ssize_t) -3)
79 static void grow(void);
80 static ssize_t cs(const char *);
81 static ssize_t as(const char *);
82 static ssize_t node(ssize_t, ...);
83 static void emit(ssize_t, int);
84 static void emit_macro(int, ssize_t);
85 static void free_tree(void);
86 static ssize_t numnode(int);
87 static ssize_t lookup(char *, size_t, char);
88 static ssize_t letter_node(char *);
89 static ssize_t array_node(char *);
90 static ssize_t function_node(char *);
92 static void add_par(ssize_t);
93 static void add_local(ssize_t);
94 static void warning(const char *);
95 static void init(void);
96 static void usage(void);
97 static char *escape(const char *);
99 static ssize_t instr_sz = 0;
100 static struct tree *instructions = NULL;
101 static ssize_t current = 0;
102 static int macro_char = '0';
103 static int reset_macro_char = '0';
104 static int nesting = 0;
105 static int breakstack[16];
106 static int breaksp = 0;
107 static ssize_t prologue;
108 static ssize_t epilogue;
109 static bool st_has_continue;
110 static char str_table[UCHAR_MAX][2];
111 static bool do_fork = true;
112 static u_short var_count;
115 static void sigchld(int);
117 extern char *__progname;
119 #define BREAKSTACK_SZ (sizeof(breakstack)/sizeof(breakstack[0]))
121 /* These values are 4.4BSD bc compatible */
122 #define FUNC_CHAR 0x01
123 #define ARRAY_CHAR 0xa1
125 /* Skip '\0', [, \ and ] */
126 #define ENCODE(c) ((c) < '[' ? (c) : (c) + 3);
127 #define VAR_BASE (256-4)
128 #define MAX_VARIABLES (VAR_BASE * VAR_BASE)
130 const struct option long_options[] =
132 {"expression", required_argument, NULL, 'e'},
133 {"help", no_argument, NULL, 'h'},
134 {"mathlib", no_argument, NULL, 'l'},
135 /* compatibility option */
136 {"quiet", no_argument, NULL, 'q'},
137 {"version", no_argument, NULL, 'v'},
138 {NULL, no_argument, NULL, 0}
146 struct lvalue lvalue;
152 %token COMMA SEMICOLON LPAR RPAR LBRACE RBRACE LBRACKET RBRACKET DOT
155 %token <str> NUMBER STRING
156 %token DEFINE BREAK QUIT LENGTH
157 %token RETURN FOR IF WHILE SQRT
158 %token SCALE IBASE OBASE AUTO
159 %token CONTINUE ELSE PRINT
164 %nonassoc EQUALS LESS_EQ GREATER_EQ UNEQUALS LESS GREATER
165 %right <str> ASSIGN_OP
167 %left MULTIPLY DIVIDE REMAINDER
172 %type <lvalue> named_expression
173 %type <node> argument_list
174 %type <node> alloc_macro
175 %type <node> expression
176 %type <node> function
177 %type <node> function_header
178 %type <node> input_item
179 %type <node> opt_argument_list
180 %type <node> opt_expression
181 %type <node> opt_relational_expression
182 %type <node> opt_statement
183 %type <node> print_expression
184 %type <node> print_expression_list
185 %type <node> relational_expression
186 %type <node> return_expression
187 %type <node> semicolon_list
188 %type <node> statement
189 %type <node> statement_list
193 program : /* empty */
197 input_item : semicolon_list NEWLINE
200 macro_char = reset_macro_char;
203 st_has_continue = false;
209 st_has_continue = false;
221 semicolon_list : /* empty */
226 | semicolon_list SEMICOLON statement
228 $$ = node($1, $3, END_NODE);
230 | semicolon_list SEMICOLON
233 statement_list : /* empty */
238 | statement_list NEWLINE
239 | statement_list NEWLINE statement
241 $$ = node($1, $3, END_NODE);
243 | statement_list SEMICOLON
244 | statement_list SEMICOLON statement
246 $$ = node($1, $3, END_NODE);
251 opt_statement : /* empty */
258 statement : expression
260 $$ = node($1, cs("ps."), END_NODE);
262 | named_expression ASSIGN_OP expression
265 $$ = node($3, cs($2), $1.store,
268 $$ = node($1.load, $3, cs($2), $1.store,
273 $$ = node(cs("["), as($1),
279 warning("break not in for or while");
284 breakstack[breaksp-1]),
291 warning("continue not in for or while");
294 st_has_continue = true;
295 $$ = node(numnode(nesting -
296 breakstack[breaksp-1] - 1),
307 sigprocmask(SIG_BLOCK, NULL, &mask);
312 | RETURN return_expression
315 warning("return must be in a function");
320 | FOR LPAR alloc_macro opt_expression SEMICOLON
321 opt_relational_expression SEMICOLON
322 opt_expression RPAR opt_statement pop_nesting
327 n = node($10, cs("M"), $8, cs("s."),
330 n = node($10, $8, cs("s."), $6, $3,
334 $$ = node($4, cs("s."), $6, $3, cs(" "),
337 | IF LPAR alloc_macro pop_nesting relational_expression RPAR
341 $$ = node($5, $3, cs(" "), END_NODE);
343 | IF LPAR alloc_macro pop_nesting relational_expression RPAR
344 opt_statement ELSE alloc_macro pop_nesting opt_statement
348 $$ = node($5, $3, cs("e"), $9, cs(" "),
351 | WHILE LPAR alloc_macro relational_expression RPAR
352 opt_statement pop_nesting
357 n = node($6, cs("M"), $4, $3, END_NODE);
359 n = node($6, $4, $3, END_NODE);
361 $$ = node($4, $3, cs(" "), END_NODE);
363 | LBRACE statement_list RBRACE
367 | PRINT print_expression_list
373 alloc_macro : /* empty */
375 $$ = cs(str_table[macro_char]);
377 /* Do not use [, \ and ] */
378 if (macro_char == '[')
381 else if (macro_char == 'a')
383 else if (macro_char == ARRAY_CHAR)
385 else if (macro_char == 255)
386 fatal("program too big");
387 if (breaksp == BREAKSTACK_SZ)
388 fatal("nesting too deep");
389 breakstack[breaksp++] = nesting++;
393 pop_nesting : /* empty */
399 function : function_header opt_parameter_list RPAR opt_newline
400 LBRACE NEWLINE opt_auto_define_list
401 statement_list RBRACE
403 int n = node(prologue, $8, epilogue,
404 cs("0"), numnode(nesting),
407 reset_macro_char = macro_char;
413 function_header : DEFINE LETTER LPAR
415 $$ = function_node($2);
421 breakstack[breaksp] = 0;
425 opt_newline : /* empty */
435 parameter_list : LETTER
437 add_par(letter_node($1));
440 | LETTER LBRACKET RBRACKET
442 add_par(array_node($1));
445 | parameter_list COMMA LETTER
447 add_par(letter_node($3));
450 | parameter_list COMMA LETTER LBRACKET RBRACKET
452 add_par(array_node($3));
461 | AUTO define_list NEWLINE
462 | AUTO define_list SEMICOLON
468 add_local(letter_node($1));
471 | LETTER LBRACKET RBRACKET
473 add_local(array_node($1));
476 | define_list COMMA LETTER
478 add_local(letter_node($3));
481 | define_list COMMA LETTER LBRACKET RBRACKET
483 add_local(array_node($3));
498 argument_list : expression
499 | argument_list COMMA expression
501 $$ = node($1, $3, END_NODE);
503 | argument_list COMMA LETTER LBRACKET RBRACKET
505 $$ = node($1, cs("l"), array_node($3),
511 opt_relational_expression
516 | relational_expression
519 relational_expression
520 : expression EQUALS expression
522 $$ = node($1, $3, cs("="), END_NODE);
524 | expression UNEQUALS expression
526 $$ = node($1, $3, cs("!="), END_NODE);
528 | expression LESS expression
530 $$ = node($1, $3, cs(">"), END_NODE);
532 | expression LESS_EQ expression
534 $$ = node($1, $3, cs("!<"), END_NODE);
536 | expression GREATER expression
538 $$ = node($1, $3, cs("<"), END_NODE);
540 | expression GREATER_EQ expression
542 $$ = node($1, $3, cs("!>"), END_NODE);
546 $$ = node($1, cs(" 0!="), END_NODE);
554 $$ = node(cs("0"), epilogue,
555 numnode(nesting), cs("Q"), END_NODE);
559 $$ = node($1, epilogue,
560 numnode(nesting), cs("Q"), END_NODE);
564 $$ = node(cs("0"), epilogue,
565 numnode(nesting), cs("Q"), END_NODE);
570 opt_expression : /* empty */
577 expression : named_expression
579 $$ = node($1.load, END_NODE);
582 $$ = node(cs("l."), END_NODE);
586 $$ = node(cs(" "), as($1), END_NODE);
588 | LPAR expression RPAR
592 | LETTER LPAR opt_argument_list RPAR
594 $$ = node($3, cs("l"),
595 function_node($1), cs("x"),
599 | MINUS expression %prec UMINUS
601 $$ = node(cs(" 0"), $2, cs("-"),
604 | expression PLUS expression
606 $$ = node($1, $3, cs("+"), END_NODE);
608 | expression MINUS expression
610 $$ = node($1, $3, cs("-"), END_NODE);
612 | expression MULTIPLY expression
614 $$ = node($1, $3, cs("*"), END_NODE);
616 | expression DIVIDE expression
618 $$ = node($1, $3, cs("/"), END_NODE);
620 | expression REMAINDER expression
622 $$ = node($1, $3, cs("%"), END_NODE);
624 | expression EXPONENT expression
626 $$ = node($1, $3, cs("^"), END_NODE);
628 | INCR named_expression
630 $$ = node($2.load, cs("1+d"), $2.store,
633 | DECR named_expression
635 $$ = node($2.load, cs("1-d"),
638 | named_expression INCR
640 $$ = node($1.load, cs("d1+"),
643 | named_expression DECR
645 $$ = node($1.load, cs("d1-"),
648 | named_expression ASSIGN_OP expression
651 $$ = node($3, cs($2), cs("d"), $1.store,
654 $$ = node($1.load, $3, cs($2), cs("d"),
657 | LENGTH LPAR expression RPAR
659 $$ = node($3, cs("Z"), END_NODE);
661 | SQRT LPAR expression RPAR
663 $$ = node($3, cs("v"), END_NODE);
665 | SCALE LPAR expression RPAR
667 $$ = node($3, cs("X"), END_NODE);
669 | BOOL_NOT expression
671 $$ = node($2, cs("N"), END_NODE);
673 | expression BOOL_AND alloc_macro pop_nesting expression
675 ssize_t n = node(cs("R"), $5, END_NODE);
677 $$ = node($1, cs("d0!="), $3, END_NODE);
679 | expression BOOL_OR alloc_macro pop_nesting expression
681 ssize_t n = node(cs("R"), $5, END_NODE);
683 $$ = node($1, cs("d0="), $3, END_NODE);
685 | expression EQUALS expression
687 $$ = node($1, $3, cs("G"), END_NODE);
689 | expression UNEQUALS expression
691 $$ = node($1, $3, cs("GN"), END_NODE);
693 | expression LESS expression
695 $$ = node($3, $1, cs("("), END_NODE);
697 | expression LESS_EQ expression
699 $$ = node($3, $1, cs("{"), END_NODE);
701 | expression GREATER expression
703 $$ = node($1, $3, cs("("), END_NODE);
705 | expression GREATER_EQ expression
707 $$ = node($1, $3, cs("{"), END_NODE);
714 $$.load = node(cs("l"), letter_node($1),
716 $$.store = node(cs("s"), letter_node($1),
720 | LETTER LBRACKET expression RBRACKET
722 $$.load = node($3, cs(";"),
723 array_node($1), END_NODE);
724 $$.store = node($3, cs(":"),
725 array_node($1), END_NODE);
745 print_expression_list
747 | print_expression_list COMMA print_expression
749 $$ = node($1, $3, END_NODE);
755 $$ = node($1, cs("ds.n"), END_NODE);
759 char *p = escape($1);
760 $$ = node(cs("["), as(p), cs("]n"), END_NODE);
772 if (current == instr_sz) {
773 newsize = instr_sz * 2 + 1;
774 p = reallocarray(instructions, newsize, sizeof(*p));
789 instructions[current].index = CONST_STRING;
790 instructions[current].u.cstr = str;
799 instructions[current].index = ALLOC_STRING;
800 instructions[current].u.astr = strdup(str);
801 if (instructions[current].u.astr == NULL)
807 node(ssize_t arg, ...)
816 instructions[current++].index = arg;
819 arg = va_arg(ap, ssize_t);
821 instructions[current++].index = arg;
822 } while (arg != END_NODE);
829 emit(ssize_t i, int level)
833 errx(1, "internal error: tree level > 1000");
834 if (instructions[i].index >= 0) {
835 while (instructions[i].index != END_NODE &&
836 instructions[i].index != i) {
837 emit(instructions[i].index, level + 1);
840 } else if (instructions[i].index != END_NODE)
841 fputs(instructions[i].u.cstr, stdout);
845 emit_macro(int nodeidx, ssize_t code)
850 printf("]s%s\n", instructions[nodeidx].u.cstr);
859 for (i = 0; i < current; i++)
860 if (instructions[i].index == ALLOC_STRING)
861 free(instructions[i].u.astr);
871 p = str_table['0' + num];
873 p = str_table['A' - 10 + num];
875 errx(1, "internal error: break num > 15");
876 return (node(cs(" "), cs(p), END_NODE));
881 lookup(char * str, size_t len, char type)
887 /* The scanner allocated an extra byte already */
888 if (str[len-1] != type) {
893 found = hsearch(entry, FIND);
895 if (var_count == MAX_VARIABLES)
896 errx(1, "too many variables");
902 p[1] = ENCODE(num / VAR_BASE + 1);
903 p[2] = ENCODE(num % VAR_BASE + 1);
906 entry.data = (char *)p;
907 entry.key = strdup(str);
908 if (entry.key == NULL)
910 found = hsearch(entry, ENTER);
914 return (cs(found->data));
918 letter_node(char *str)
923 if (len == 1 && str[0] != '_')
924 return (cs(str_table[(int)str[0]]));
926 return (lookup(str, len, 'L'));
930 array_node(char *str)
935 if (len == 1 && str[0] != '_')
936 return (cs(str_table[(int)str[0] - 'a' + ARRAY_CHAR]));
938 return (lookup(str, len, 'A'));
942 function_node(char *str)
947 if (len == 1 && str[0] != '_')
948 return (cs(str_table[(int)str[0] - 'a' + FUNC_CHAR]));
950 return (lookup(str, len, 'F'));
957 prologue = node(cs("S"), n, prologue, END_NODE);
958 epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
965 prologue = node(cs("0S"), n, prologue, END_NODE);
966 epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
970 yyerror(const char *s)
975 if (yyin != NULL && feof(yyin))
976 n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF",
977 __progname, filename, lineno, s);
978 else if (yytext[0] == '\n')
980 "%s: %s:%d: %s: newline unexpected",
981 __progname, filename, lineno, s);
982 else if (isspace((unsigned char)yytext[0]) ||
983 !isprint((unsigned char)yytext[0]))
985 "%s: %s:%d: %s: ascii char 0x%02x unexpected",
986 __progname, filename, lineno, s, yytext[0] & 0xff);
988 n = asprintf(&str, "%s: %s:%d: %s: %s unexpected",
989 __progname, filename, lineno, s, yytext);
994 for (p = str; *p != '\0'; p++) {
995 if (*p == '[' || *p == ']' || *p =='\\')
999 fputs("]pc\n", stdout);
1004 fatal(const char *s)
1007 errx(1, "%s:%d: %s", filename, lineno, s);
1011 warning(const char *s)
1014 warnx("%s:%d: %s", filename, lineno, s);
1022 for (i = 0; i < UCHAR_MAX; i++) {
1023 str_table[i][0] = i;
1024 str_table[i][1] = '\0';
1026 if (hcreate(1 << 16) == 0)
1035 fprintf(stderr, "usage: %s [-chlv] [-e expression] [file ...]\n",
1041 escape(const char *str)
1045 ret = malloc(strlen(str) + 1);
1050 while (*str != '\0') {
1052 * We get _escaped_ strings here. Single backslashes are
1053 * already converted to double backslashes
1056 if (*++str == '\\') {
1097 sigchld(int signo __unused)
1100 int status, save_errno = errno;
1103 pid = waitpid(dc, &status, WCONTINUED | WNOHANG);
1108 } else if (pid == 0)
1110 if (WIFEXITED(status) || WIFSIGNALED(status))
1126 main(int argc, char *argv[])
1133 setvbuf(stdout, NULL, _IOLBF, 0);
1135 sargv = reallocarray(NULL, argc, sizeof(char *));
1139 if ((cmdexpr = strdup("")) == NULL)
1141 /* The d debug option is 4.4 BSD bc(1) compatible */
1142 while ((ch = getopt_long(argc, argv, "cde:hlqv",
1143 long_options, NULL)) != -1) {
1151 if (asprintf(&cmdexpr, "%s%s\n", cmdexpr, optarg) == -1)
1159 sargv[sargc++] = _PATH_LIBB;
1162 /* compatibility option */
1165 fprintf(stderr, "%s (BSD bc) %s\n", __progname, BC_VER);
1176 interactive = isatty(STDIN_FILENO);
1177 for (i = 0; i < argc; i++)
1178 sargv[sargc++] = argv[i];
1182 err(1, "cannot create pipe");
1185 err(1, "cannot fork");
1187 signal(SIGCHLD, sigchld);
1188 close(STDOUT_FILENO);
1193 close(STDIN_FILENO);
1197 execl(_PATH_DC, "dc", "-x", (char *)NULL);
1198 err(1, "cannot find dc");
1203 el = el_init("bc", stdin, stderr, stderr);
1204 hist = history_init();
1205 history(hist, &he, H_SETSIZE, 100);
1206 el_set(el, EL_HIST, history, hist);
1207 el_set(el, EL_EDITOR, "emacs");
1208 el_set(el, EL_SIGNAL, 1);
1209 el_set(el, EL_PROMPT, dummy_prompt);
1210 el_set(el, EL_ADDFN, "bc_eof", "", bc_eof);
1211 el_set(el, EL_BIND, "^D", "bc_eof", NULL);
1212 el_source(el, NULL);