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 #include <sys/types.h>
51 #include "pathnames.h"
53 #define BC_VER "1.1-FreeBSD"
54 #define END_NODE ((ssize_t) -1)
55 #define CONST_STRING ((ssize_t) -2)
56 #define ALLOC_STRING ((ssize_t) -3)
77 static void grow(void);
78 static ssize_t cs(const char *);
79 static ssize_t as(const char *);
80 static ssize_t node(ssize_t, ...);
81 static void emit(ssize_t, int);
82 static void emit_macro(int, ssize_t);
83 static void free_tree(void);
84 static ssize_t numnode(int);
85 static ssize_t lookup(char *, size_t, char);
86 static ssize_t letter_node(char *);
87 static ssize_t array_node(char *);
88 static ssize_t function_node(char *);
90 static void add_par(ssize_t);
91 static void add_local(ssize_t);
92 static void warning(const char *);
93 static void init(void);
94 static void usage(void);
95 static char *escape(const char *);
97 static ssize_t instr_sz = 0;
98 static struct tree *instructions = NULL;
99 static ssize_t current = 0;
100 static int macro_char = '0';
101 static int reset_macro_char = '0';
102 static int nesting = 0;
103 static int breakstack[16];
104 static int breaksp = 0;
105 static ssize_t prologue;
106 static ssize_t epilogue;
107 static bool st_has_continue;
108 static char str_table[UCHAR_MAX][2];
109 static bool do_fork = true;
110 static u_short var_count;
113 static void sigchld(int);
115 extern char *__progname;
117 #define BREAKSTACK_SZ (sizeof(breakstack)/sizeof(breakstack[0]))
119 /* These values are 4.4BSD bc compatible */
120 #define FUNC_CHAR 0x01
121 #define ARRAY_CHAR 0xa1
123 /* Skip '\0', [, \ and ] */
124 #define ENCODE(c) ((c) < '[' ? (c) : (c) + 3);
125 #define VAR_BASE (256-4)
126 #define MAX_VARIABLES (VAR_BASE * VAR_BASE)
128 const struct option long_options[] =
130 {"expression", required_argument, NULL, 'e'},
131 {"help", no_argument, NULL, 'h'},
132 {"mathlib", no_argument, NULL, 'l'},
133 /* compatibility option */
134 {"quiet", no_argument, NULL, 'q'},
135 {"version", no_argument, NULL, 'v'},
136 {NULL, no_argument, NULL, 0}
144 struct lvalue lvalue;
150 %token COMMA SEMICOLON LPAR RPAR LBRACE RBRACE LBRACKET RBRACKET DOT
153 %token <str> NUMBER STRING
154 %token DEFINE BREAK QUIT LENGTH
155 %token RETURN FOR IF WHILE SQRT
156 %token SCALE IBASE OBASE AUTO
157 %token CONTINUE ELSE PRINT
162 %nonassoc EQUALS LESS_EQ GREATER_EQ UNEQUALS LESS GREATER
163 %right <str> ASSIGN_OP
165 %left MULTIPLY DIVIDE REMAINDER
170 %type <lvalue> named_expression
171 %type <node> argument_list
172 %type <node> alloc_macro
173 %type <node> expression
174 %type <node> function
175 %type <node> function_header
176 %type <node> input_item
177 %type <node> opt_argument_list
178 %type <node> opt_expression
179 %type <node> opt_relational_expression
180 %type <node> opt_statement
181 %type <node> print_expression
182 %type <node> print_expression_list
183 %type <node> relational_expression
184 %type <node> return_expression
185 %type <node> semicolon_list
186 %type <node> statement
187 %type <node> statement_list
191 program : /* empty */
195 input_item : semicolon_list NEWLINE
198 macro_char = reset_macro_char;
201 st_has_continue = false;
207 st_has_continue = false;
219 semicolon_list : /* empty */
224 | semicolon_list SEMICOLON statement
226 $$ = node($1, $3, END_NODE);
228 | semicolon_list SEMICOLON
231 statement_list : /* empty */
236 | statement_list NEWLINE
237 | statement_list NEWLINE statement
239 $$ = node($1, $3, END_NODE);
241 | statement_list SEMICOLON
242 | statement_list SEMICOLON statement
244 $$ = node($1, $3, END_NODE);
249 opt_statement : /* empty */
256 statement : expression
258 $$ = node($1, cs("ps."), END_NODE);
260 | named_expression ASSIGN_OP expression
263 $$ = node($3, cs($2), $1.store,
266 $$ = node($1.load, $3, cs($2), $1.store,
271 $$ = node(cs("["), as($1),
277 warning("break not in for or while");
282 breakstack[breaksp-1]),
289 warning("continue not in for or while");
292 st_has_continue = true;
293 $$ = node(numnode(nesting -
294 breakstack[breaksp-1] - 1),
305 sigprocmask(SIG_BLOCK, NULL, &mask);
310 | RETURN return_expression
313 warning("return must be in a function");
318 | FOR LPAR alloc_macro opt_expression SEMICOLON
319 opt_relational_expression SEMICOLON
320 opt_expression RPAR opt_statement pop_nesting
325 n = node($10, cs("M"), $8, cs("s."),
328 n = node($10, $8, cs("s."), $6, $3,
332 $$ = node($4, cs("s."), $6, $3, cs(" "),
335 | IF LPAR alloc_macro pop_nesting relational_expression RPAR
339 $$ = node($5, $3, cs(" "), END_NODE);
341 | IF LPAR alloc_macro pop_nesting relational_expression RPAR
342 opt_statement ELSE alloc_macro pop_nesting opt_statement
346 $$ = node($5, $3, cs("e"), $9, cs(" "),
349 | WHILE LPAR alloc_macro relational_expression RPAR
350 opt_statement pop_nesting
355 n = node($6, cs("M"), $4, $3, END_NODE);
357 n = node($6, $4, $3, END_NODE);
359 $$ = node($4, $3, cs(" "), END_NODE);
361 | LBRACE statement_list RBRACE
365 | PRINT print_expression_list
371 alloc_macro : /* empty */
373 $$ = cs(str_table[macro_char]);
375 /* Do not use [, \ and ] */
376 if (macro_char == '[')
379 else if (macro_char == 'a')
381 else if (macro_char == ARRAY_CHAR)
383 else if (macro_char == 255)
384 fatal("program too big");
385 if (breaksp == BREAKSTACK_SZ)
386 fatal("nesting too deep");
387 breakstack[breaksp++] = nesting++;
391 pop_nesting : /* empty */
397 function : function_header opt_parameter_list RPAR opt_newline
398 LBRACE NEWLINE opt_auto_define_list
399 statement_list RBRACE
401 int n = node(prologue, $8, epilogue,
402 cs("0"), numnode(nesting),
405 reset_macro_char = macro_char;
411 function_header : DEFINE LETTER LPAR
413 $$ = function_node($2);
419 breakstack[breaksp] = 0;
423 opt_newline : /* empty */
433 parameter_list : LETTER
435 add_par(letter_node($1));
438 | LETTER LBRACKET RBRACKET
440 add_par(array_node($1));
443 | parameter_list COMMA LETTER
445 add_par(letter_node($3));
448 | parameter_list COMMA LETTER LBRACKET RBRACKET
450 add_par(array_node($3));
459 | AUTO define_list NEWLINE
460 | AUTO define_list SEMICOLON
466 add_local(letter_node($1));
469 | LETTER LBRACKET RBRACKET
471 add_local(array_node($1));
474 | define_list COMMA LETTER
476 add_local(letter_node($3));
479 | define_list COMMA LETTER LBRACKET RBRACKET
481 add_local(array_node($3));
496 argument_list : expression
497 | argument_list COMMA expression
499 $$ = node($1, $3, END_NODE);
501 | argument_list COMMA LETTER LBRACKET RBRACKET
503 $$ = node($1, cs("l"), array_node($3),
509 opt_relational_expression
514 | relational_expression
517 relational_expression
518 : expression EQUALS expression
520 $$ = node($1, $3, cs("="), END_NODE);
522 | expression UNEQUALS expression
524 $$ = node($1, $3, cs("!="), END_NODE);
526 | expression LESS expression
528 $$ = node($1, $3, cs(">"), END_NODE);
530 | expression LESS_EQ expression
532 $$ = node($1, $3, cs("!<"), END_NODE);
534 | expression GREATER expression
536 $$ = node($1, $3, cs("<"), END_NODE);
538 | expression GREATER_EQ expression
540 $$ = node($1, $3, cs("!>"), END_NODE);
544 $$ = node($1, cs(" 0!="), END_NODE);
552 $$ = node(cs("0"), epilogue,
553 numnode(nesting), cs("Q"), END_NODE);
557 $$ = node($1, epilogue,
558 numnode(nesting), cs("Q"), END_NODE);
562 $$ = node(cs("0"), epilogue,
563 numnode(nesting), cs("Q"), END_NODE);
568 opt_expression : /* empty */
575 expression : named_expression
577 $$ = node($1.load, END_NODE);
580 $$ = node(cs("l."), END_NODE);
584 $$ = node(cs(" "), as($1), END_NODE);
586 | LPAR expression RPAR
590 | LETTER LPAR opt_argument_list RPAR
592 $$ = node($3, cs("l"),
593 function_node($1), cs("x"),
597 | MINUS expression %prec UMINUS
599 $$ = node(cs(" 0"), $2, cs("-"),
602 | expression PLUS expression
604 $$ = node($1, $3, cs("+"), END_NODE);
606 | expression MINUS expression
608 $$ = node($1, $3, cs("-"), END_NODE);
610 | expression MULTIPLY expression
612 $$ = node($1, $3, cs("*"), END_NODE);
614 | expression DIVIDE expression
616 $$ = node($1, $3, cs("/"), END_NODE);
618 | expression REMAINDER expression
620 $$ = node($1, $3, cs("%"), END_NODE);
622 | expression EXPONENT expression
624 $$ = node($1, $3, cs("^"), END_NODE);
626 | INCR named_expression
628 $$ = node($2.load, cs("1+d"), $2.store,
631 | DECR named_expression
633 $$ = node($2.load, cs("1-d"),
636 | named_expression INCR
638 $$ = node($1.load, cs("d1+"),
641 | named_expression DECR
643 $$ = node($1.load, cs("d1-"),
646 | named_expression ASSIGN_OP expression
649 $$ = node($3, cs($2), cs("d"), $1.store,
652 $$ = node($1.load, $3, cs($2), cs("d"),
655 | LENGTH LPAR expression RPAR
657 $$ = node($3, cs("Z"), END_NODE);
659 | SQRT LPAR expression RPAR
661 $$ = node($3, cs("v"), END_NODE);
663 | SCALE LPAR expression RPAR
665 $$ = node($3, cs("X"), END_NODE);
667 | BOOL_NOT expression
669 $$ = node($2, cs("N"), END_NODE);
671 | expression BOOL_AND alloc_macro pop_nesting expression
673 ssize_t n = node(cs("R"), $5, END_NODE);
675 $$ = node($1, cs("d0!="), $3, END_NODE);
677 | expression BOOL_OR alloc_macro pop_nesting expression
679 ssize_t n = node(cs("R"), $5, END_NODE);
681 $$ = node($1, cs("d0="), $3, END_NODE);
683 | expression EQUALS expression
685 $$ = node($1, $3, cs("G"), END_NODE);
687 | expression UNEQUALS expression
689 $$ = node($1, $3, cs("GN"), END_NODE);
691 | expression LESS expression
693 $$ = node($3, $1, cs("("), END_NODE);
695 | expression LESS_EQ expression
697 $$ = node($3, $1, cs("{"), END_NODE);
699 | expression GREATER expression
701 $$ = node($1, $3, cs("("), END_NODE);
703 | expression GREATER_EQ expression
705 $$ = node($1, $3, cs("{"), END_NODE);
712 $$.load = node(cs("l"), letter_node($1),
714 $$.store = node(cs("s"), letter_node($1),
718 | LETTER LBRACKET expression RBRACKET
720 $$.load = node($3, cs(";"),
721 array_node($1), END_NODE);
722 $$.store = node($3, cs(":"),
723 array_node($1), END_NODE);
743 print_expression_list
745 | print_expression_list COMMA print_expression
747 $$ = node($1, $3, END_NODE);
753 $$ = node($1, cs("ds.n"), END_NODE);
757 char *p = escape($1);
758 $$ = node(cs("["), as(p), cs("]n"), END_NODE);
770 if (current == instr_sz) {
771 newsize = instr_sz * 2 + 1;
772 p = reallocarray(instructions, newsize, sizeof(*p));
787 instructions[current].index = CONST_STRING;
788 instructions[current].u.cstr = str;
797 instructions[current].index = ALLOC_STRING;
798 instructions[current].u.astr = strdup(str);
799 if (instructions[current].u.astr == NULL)
805 node(ssize_t arg, ...)
814 instructions[current++].index = arg;
817 arg = va_arg(ap, ssize_t);
819 instructions[current++].index = arg;
820 } while (arg != END_NODE);
827 emit(ssize_t i, int level)
831 errx(1, "internal error: tree level > 1000");
832 if (instructions[i].index >= 0) {
833 while (instructions[i].index != END_NODE &&
834 instructions[i].index != i) {
835 emit(instructions[i].index, level + 1);
838 } else if (instructions[i].index != END_NODE)
839 fputs(instructions[i].u.cstr, stdout);
843 emit_macro(int nodeidx, ssize_t code)
848 printf("]s%s\n", instructions[nodeidx].u.cstr);
857 for (i = 0; i < current; i++)
858 if (instructions[i].index == ALLOC_STRING)
859 free(instructions[i].u.astr);
869 p = str_table['0' + num];
871 p = str_table['A' - 10 + num];
873 errx(1, "internal error: break num > 15");
874 return (node(cs(" "), cs(p), END_NODE));
879 lookup(char * str, size_t len, char type)
885 /* The scanner allocated an extra byte already */
886 if (str[len-1] != type) {
891 found = hsearch(entry, FIND);
893 if (var_count == MAX_VARIABLES)
894 errx(1, "too many variables");
900 p[1] = ENCODE(num / VAR_BASE + 1);
901 p[2] = ENCODE(num % VAR_BASE + 1);
904 entry.data = (char *)p;
905 entry.key = strdup(str);
906 if (entry.key == NULL)
908 found = hsearch(entry, ENTER);
912 return (cs(found->data));
916 letter_node(char *str)
921 if (len == 1 && str[0] != '_')
922 return (cs(str_table[(int)str[0]]));
924 return (lookup(str, len, 'L'));
928 array_node(char *str)
933 if (len == 1 && str[0] != '_')
934 return (cs(str_table[(int)str[0] - 'a' + ARRAY_CHAR]));
936 return (lookup(str, len, 'A'));
940 function_node(char *str)
945 if (len == 1 && str[0] != '_')
946 return (cs(str_table[(int)str[0] - 'a' + FUNC_CHAR]));
948 return (lookup(str, len, 'F'));
955 prologue = node(cs("S"), n, prologue, END_NODE);
956 epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
963 prologue = node(cs("0S"), n, prologue, END_NODE);
964 epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
968 yyerror(const char *s)
973 if (yyin != NULL && feof(yyin))
974 n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF",
975 __progname, filename, lineno, s);
976 else if (yytext[0] == '\n')
978 "%s: %s:%d: %s: newline unexpected",
979 __progname, filename, lineno, s);
980 else if (isspace((unsigned char)yytext[0]) ||
981 !isprint((unsigned char)yytext[0]))
983 "%s: %s:%d: %s: ascii char 0x%02x unexpected",
984 __progname, filename, lineno, s, yytext[0] & 0xff);
986 n = asprintf(&str, "%s: %s:%d: %s: %s unexpected",
987 __progname, filename, lineno, s, yytext);
992 for (p = str; *p != '\0'; p++) {
993 if (*p == '[' || *p == ']' || *p =='\\')
997 fputs("]ec\n", stdout);
1002 fatal(const char *s)
1005 errx(1, "%s:%d: %s", filename, lineno, s);
1009 warning(const char *s)
1012 warnx("%s:%d: %s", filename, lineno, s);
1020 for (i = 0; i < UCHAR_MAX; i++) {
1021 str_table[i][0] = i;
1022 str_table[i][1] = '\0';
1024 if (hcreate(1 << 16) == 0)
1033 fprintf(stderr, "usage: %s [-chlv] [-e expression] [file ...]\n",
1039 escape(const char *str)
1043 ret = malloc(strlen(str) + 1);
1048 while (*str != '\0') {
1050 * We get _escaped_ strings here. Single backslashes are
1051 * already converted to double backslashes
1054 if (*++str == '\\') {
1095 sigchld(int signo __unused)
1098 int status, save_errno = errno;
1101 pid = waitpid(dc, &status, WCONTINUED | WNOHANG);
1106 } else if (pid == 0)
1108 if (WIFEXITED(status) || WIFSIGNALED(status))
1124 main(int argc, char *argv[])
1131 setvbuf(stdout, NULL, _IOLBF, 0);
1133 sargv = reallocarray(NULL, argc, sizeof(char *));
1137 if ((cmdexpr = strdup("")) == NULL)
1139 /* The d debug option is 4.4 BSD bc(1) compatible */
1140 while ((ch = getopt_long(argc, argv, "cde:hlqv",
1141 long_options, NULL)) != -1) {
1149 if (asprintf(&cmdexpr, "%s%s\n", cmdexpr, optarg) == -1)
1157 sargv[sargc++] = _PATH_LIBB;
1160 /* compatibility option */
1163 fprintf(stderr, "%s (BSD bc) %s\n", __progname, BC_VER);
1174 interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) &&
1175 isatty(STDERR_FILENO);
1176 for (i = 0; i < argc; i++)
1177 sargv[sargc++] = argv[i];
1181 err(1, "cannot create pipe");
1184 err(1, "cannot fork");
1186 signal(SIGCHLD, sigchld);
1187 close(STDOUT_FILENO);
1192 close(STDIN_FILENO);
1196 execl(_PATH_DC, "dc", "-x", (char *)NULL);
1197 err(1, "cannot find dc");
1202 el = el_init("bc", stdin, stderr, stderr);
1203 hist = history_init();
1204 history(hist, &he, H_SETSIZE, 100);
1205 el_set(el, EL_HIST, history, hist);
1206 el_set(el, EL_EDITOR, "emacs");
1207 el_set(el, EL_SIGNAL, 1);
1208 el_set(el, EL_PROMPT, dummy_prompt);
1209 el_set(el, EL_ADDFN, "bc_eof", "", bc_eof);
1210 el_set(el, EL_BIND, "^D", "bc_eof", NULL);
1211 el_source(el, NULL);