2 /* $OpenBSD: bc.y,v 1.33 2009/10/27 23:59:36 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.0-FreeBSD"
56 #define END_NODE ((ssize_t) -1)
57 #define CONST_STRING ((ssize_t) -2)
58 #define ALLOC_STRING ((ssize_t) -3)
80 static void grow(void);
81 static ssize_t cs(const char *);
82 static ssize_t as(const char *);
83 static ssize_t node(ssize_t, ...);
84 static void emit(ssize_t);
85 static void emit_macro(int, ssize_t);
86 static void free_tree(void);
87 static ssize_t numnode(int);
88 static ssize_t lookup(char *, size_t, char);
89 static ssize_t letter_node(char *);
90 static ssize_t array_node(char *);
91 static ssize_t function_node(char *);
93 static void add_par(ssize_t);
94 static void add_local(ssize_t);
95 static void warning(const char *);
96 static void init(void);
97 static void usage(void);
98 static char *escape(const char *);
100 static ssize_t instr_sz = 0;
101 static struct tree *instructions = NULL;
102 static ssize_t current = 0;
103 static int macro_char = '0';
104 static int reset_macro_char = '0';
105 static int nesting = 0;
106 static int breakstack[16];
107 static int breaksp = 0;
108 static ssize_t prologue;
109 static ssize_t epilogue;
110 static bool st_has_continue;
111 static char str_table[UCHAR_MAX][2];
112 static bool do_fork = true;
113 static u_short var_count;
116 static void sigchld(int);
118 extern char *__progname;
120 #define BREAKSTACK_SZ (sizeof(breakstack)/sizeof(breakstack[0]))
122 /* These values are 4.4BSD bc compatible */
123 #define FUNC_CHAR 0x01
124 #define ARRAY_CHAR 0xa1
126 /* Skip '\0', [, \ and ] */
127 #define ENCODE(c) ((c) < '[' ? (c) : (c) + 3);
128 #define VAR_BASE (256-4)
129 #define MAX_VARIABLES (VAR_BASE * VAR_BASE)
131 const struct option long_options[] =
133 {"expression", required_argument, NULL, 'e'},
134 {"help", no_argument, NULL, 'h'},
135 {"mathlib", no_argument, NULL, 'l'},
136 /* compatibility option */
137 {"quiet", no_argument, NULL, 'q'},
138 {"version", no_argument, NULL, 'v'},
139 {NULL, no_argument, NULL, 0}
147 struct lvalue lvalue;
153 %token COMMA SEMICOLON LPAR RPAR LBRACE RBRACE LBRACKET RBRACKET DOT
156 %token <str> NUMBER STRING
157 %token DEFINE BREAK QUIT LENGTH
158 %token RETURN FOR IF WHILE SQRT
159 %token SCALE IBASE OBASE AUTO
160 %token CONTINUE ELSE PRINT
165 %nonassoc EQUALS LESS_EQ GREATER_EQ UNEQUALS LESS GREATER
166 %right <str> ASSIGN_OP
168 %left MULTIPLY DIVIDE REMAINDER
173 %type <lvalue> named_expression
174 %type <node> argument_list
175 %type <node> alloc_macro
176 %type <node> expression
177 %type <node> function
178 %type <node> function_header
179 %type <node> input_item
180 %type <node> opt_argument_list
181 %type <node> opt_expression
182 %type <node> opt_relational_expression
183 %type <node> opt_statement
184 %type <node> print_expression
185 %type <node> print_expression_list
186 %type <node> relational_expression
187 %type <node> return_expression
188 %type <node> semicolon_list
189 %type <node> statement
190 %type <node> statement_list
194 program : /* empty */
198 input_item : semicolon_list NEWLINE
201 macro_char = reset_macro_char;
204 st_has_continue = false;
210 st_has_continue = false;
222 semicolon_list : /* empty */
227 | semicolon_list SEMICOLON statement
229 $$ = node($1, $3, END_NODE);
231 | semicolon_list SEMICOLON
234 statement_list : /* empty */
239 | statement_list NEWLINE
240 | statement_list NEWLINE statement
242 $$ = node($1, $3, END_NODE);
244 | statement_list SEMICOLON
245 | statement_list SEMICOLON statement
247 $$ = node($1, $3, END_NODE);
252 opt_statement : /* empty */
259 statement : expression
261 $$ = node($1, cs("ps."), END_NODE);
263 | named_expression ASSIGN_OP expression
266 $$ = node($3, cs($2), $1.store,
269 $$ = node($1.load, $3, cs($2), $1.store,
274 $$ = node(cs("["), as($1),
280 warning("break not in for or while");
285 breakstack[breaksp-1]),
292 warning("continue not in for or while");
295 st_has_continue = true;
296 $$ = node(numnode(nesting -
297 breakstack[breaksp-1] - 1),
308 sigprocmask(SIG_BLOCK, NULL, &mask);
313 | RETURN return_expression
316 warning("return must be in a function");
321 | FOR LPAR alloc_macro opt_expression SEMICOLON
322 opt_relational_expression SEMICOLON
323 opt_expression RPAR opt_statement pop_nesting
328 n = node($10, cs("M"), $8, cs("s."),
331 n = node($10, $8, cs("s."), $6, $3,
335 $$ = node($4, cs("s."), $6, $3, cs(" "),
338 | IF LPAR alloc_macro pop_nesting relational_expression RPAR
342 $$ = node($5, $3, cs(" "), END_NODE);
344 | IF LPAR alloc_macro pop_nesting relational_expression RPAR
345 opt_statement ELSE alloc_macro pop_nesting opt_statement
349 $$ = node($5, $3, cs("e"), $9, cs(" "),
352 | WHILE LPAR alloc_macro relational_expression RPAR
353 opt_statement pop_nesting
358 n = node($6, cs("M"), $4, $3, END_NODE);
360 n = node($6, $4, $3, END_NODE);
362 $$ = node($4, $3, cs(" "), END_NODE);
364 | LBRACE statement_list RBRACE
368 | PRINT print_expression_list
374 alloc_macro : /* empty */
376 $$ = cs(str_table[macro_char]);
378 /* Do not use [, \ and ] */
379 if (macro_char == '[')
382 else if (macro_char == 'a')
384 else if (macro_char == ARRAY_CHAR)
386 else if (macro_char == 255)
387 fatal("program too big");
388 if (breaksp == BREAKSTACK_SZ)
389 fatal("nesting too deep");
390 breakstack[breaksp++] = nesting++;
394 pop_nesting : /* empty */
400 function : function_header opt_parameter_list RPAR opt_newline
401 LBRACE NEWLINE opt_auto_define_list
402 statement_list RBRACE
404 int n = node(prologue, $8, epilogue,
405 cs("0"), numnode(nesting),
408 reset_macro_char = macro_char;
414 function_header : DEFINE LETTER LPAR
416 $$ = function_node($2);
422 breakstack[breaksp] = 0;
426 opt_newline : /* empty */
436 parameter_list : LETTER
438 add_par(letter_node($1));
441 | LETTER LBRACKET RBRACKET
443 add_par(array_node($1));
446 | parameter_list COMMA LETTER
448 add_par(letter_node($3));
451 | parameter_list COMMA LETTER LBRACKET RBRACKET
453 add_par(array_node($3));
462 | AUTO define_list NEWLINE
463 | AUTO define_list SEMICOLON
469 add_local(letter_node($1));
472 | LETTER LBRACKET RBRACKET
474 add_local(array_node($1));
477 | define_list COMMA LETTER
479 add_local(letter_node($3));
482 | define_list COMMA LETTER LBRACKET RBRACKET
484 add_local(array_node($3));
499 argument_list : expression
500 | argument_list COMMA expression
502 $$ = node($1, $3, END_NODE);
504 | argument_list COMMA LETTER LBRACKET RBRACKET
506 $$ = node($1, cs("l"), array_node($3),
512 opt_relational_expression
517 | relational_expression
520 relational_expression
521 : expression EQUALS expression
523 $$ = node($1, $3, cs("="), END_NODE);
525 | expression UNEQUALS expression
527 $$ = node($1, $3, cs("!="), END_NODE);
529 | expression LESS expression
531 $$ = node($1, $3, cs(">"), END_NODE);
533 | expression LESS_EQ expression
535 $$ = node($1, $3, cs("!<"), END_NODE);
537 | expression GREATER expression
539 $$ = node($1, $3, cs("<"), END_NODE);
541 | expression GREATER_EQ expression
543 $$ = node($1, $3, cs("!>"), END_NODE);
547 $$ = node($1, cs(" 0!="), END_NODE);
555 $$ = node(cs("0"), epilogue,
556 numnode(nesting), cs("Q"), END_NODE);
560 $$ = node($1, epilogue,
561 numnode(nesting), cs("Q"), END_NODE);
565 $$ = node(cs("0"), epilogue,
566 numnode(nesting), cs("Q"), END_NODE);
571 opt_expression : /* empty */
578 expression : named_expression
580 $$ = node($1.load, END_NODE);
583 $$ = node(cs("l."), END_NODE);
587 $$ = node(cs(" "), as($1), END_NODE);
589 | LPAR expression RPAR
593 | LETTER LPAR opt_argument_list RPAR
595 $$ = node($3, cs("l"),
596 function_node($1), cs("x"),
600 | MINUS expression %prec UMINUS
602 $$ = node(cs(" 0"), $2, cs("-"),
605 | expression PLUS expression
607 $$ = node($1, $3, cs("+"), END_NODE);
609 | expression MINUS expression
611 $$ = node($1, $3, cs("-"), END_NODE);
613 | expression MULTIPLY expression
615 $$ = node($1, $3, cs("*"), END_NODE);
617 | expression DIVIDE expression
619 $$ = node($1, $3, cs("/"), END_NODE);
621 | expression REMAINDER expression
623 $$ = node($1, $3, cs("%"), END_NODE);
625 | expression EXPONENT expression
627 $$ = node($1, $3, cs("^"), END_NODE);
629 | INCR named_expression
631 $$ = node($2.load, cs("1+d"), $2.store,
634 | DECR named_expression
636 $$ = node($2.load, cs("1-d"),
639 | named_expression INCR
641 $$ = node($1.load, cs("d1+"),
644 | named_expression DECR
646 $$ = node($1.load, cs("d1-"),
649 | named_expression ASSIGN_OP expression
652 $$ = node($3, cs($2), cs("d"), $1.store,
655 $$ = node($1.load, $3, cs($2), cs("d"),
658 | LENGTH LPAR expression RPAR
660 $$ = node($3, cs("Z"), END_NODE);
662 | SQRT LPAR expression RPAR
664 $$ = node($3, cs("v"), END_NODE);
666 | SCALE LPAR expression RPAR
668 $$ = node($3, cs("X"), END_NODE);
670 | BOOL_NOT expression
672 $$ = node($2, cs("N"), END_NODE);
674 | expression BOOL_AND alloc_macro pop_nesting expression
676 ssize_t n = node(cs("R"), $5, END_NODE);
678 $$ = node($1, cs("d0!="), $3, END_NODE);
680 | expression BOOL_OR alloc_macro pop_nesting expression
682 ssize_t n = node(cs("R"), $5, END_NODE);
684 $$ = node($1, cs("d0="), $3, END_NODE);
686 | expression EQUALS expression
688 $$ = node($1, $3, cs("G"), END_NODE);
690 | expression UNEQUALS expression
692 $$ = node($1, $3, cs("GN"), END_NODE);
694 | expression LESS expression
696 $$ = node($3, $1, cs("("), END_NODE);
698 | expression LESS_EQ expression
700 $$ = node($3, $1, cs("{"), END_NODE);
702 | expression GREATER expression
704 $$ = node($1, $3, cs("("), END_NODE);
706 | expression GREATER_EQ expression
708 $$ = node($1, $3, cs("{"), END_NODE);
715 $$.load = node(cs("l"), letter_node($1),
717 $$.store = node(cs("s"), letter_node($1),
721 | LETTER LBRACKET expression RBRACKET
723 $$.load = node($3, cs(";"),
724 array_node($1), END_NODE);
725 $$.store = node($3, cs(":"),
726 array_node($1), END_NODE);
746 print_expression_list
748 | print_expression_list COMMA print_expression
750 $$ = node($1, $3, END_NODE);
756 $$ = node($1, cs("ds.n"), END_NODE);
760 char *p = escape($1);
761 $$ = node(cs("["), as(p), cs("]n"), END_NODE);
773 if (current == instr_sz) {
774 newsize = instr_sz * 2 + 1;
775 p = realloc(instructions, newsize * sizeof(*p));
790 instructions[current].index = CONST_STRING;
791 instructions[current].u.cstr = str;
800 instructions[current].index = ALLOC_STRING;
801 instructions[current].u.astr = strdup(str);
802 if (instructions[current].u.astr == NULL)
808 node(ssize_t arg, ...)
817 instructions[current++].index = arg;
820 arg = va_arg(ap, ssize_t);
822 instructions[current++].index = arg;
823 } while (arg != END_NODE);
833 if (instructions[i].index >= 0)
834 while (instructions[i].index != END_NODE)
835 emit(instructions[i++].index);
837 fputs(instructions[i].u.cstr, stdout);
841 emit_macro(int nodeidx, ssize_t code)
846 printf("]s%s\n", instructions[nodeidx].u.cstr);
855 for (i = 0; i < current; i++)
856 if (instructions[i].index == ALLOC_STRING)
857 free(instructions[i].u.astr);
867 p = str_table['0' + num];
869 p = str_table['A' - 10 + num];
871 errx(1, "internal error: break num > 15");
872 return (node(cs(" "), cs(p), END_NODE));
877 lookup(char * str, size_t len, char type)
883 /* The scanner allocated an extra byte already */
884 if (str[len-1] != type) {
889 found = hsearch(entry, FIND);
891 if (var_count == MAX_VARIABLES)
892 errx(1, "too many variables");
898 p[1] = ENCODE(num / VAR_BASE + 1);
899 p[2] = ENCODE(num % VAR_BASE + 1);
902 entry.data = (char *)p;
903 entry.key = strdup(str);
904 if (entry.key == NULL)
906 found = hsearch(entry, ENTER);
910 return (cs(found->data));
914 letter_node(char *str)
919 if (len == 1 && str[0] != '_')
920 return (cs(str_table[(int)str[0]]));
922 return (lookup(str, len, 'L'));
926 array_node(char *str)
931 if (len == 1 && str[0] != '_')
932 return (cs(str_table[(int)str[0] - 'a' + ARRAY_CHAR]));
934 return (lookup(str, len, 'A'));
938 function_node(char *str)
943 if (len == 1 && str[0] != '_')
944 return (cs(str_table[(int)str[0] - 'a' + FUNC_CHAR]));
946 return (lookup(str, len, 'F'));
953 prologue = node(cs("S"), n, prologue, END_NODE);
954 epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
961 prologue = node(cs("0S"), n, prologue, END_NODE);
962 epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
966 yyerror(const char *s)
971 if (yyin != NULL && feof(yyin))
972 n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF",
973 __progname, filename, lineno, s);
974 else if (isspace(yytext[0]) || !isprint(yytext[0]))
976 "%s: %s:%d: %s: ascii char 0x%02x unexpected",
977 __progname, filename, lineno, s, yytext[0]);
979 n = asprintf(&str, "%s: %s:%d: %s: %s unexpected",
980 __progname, filename, lineno, s, yytext);
985 for (p = str; *p != '\0'; p++) {
986 if (*p == '[' || *p == ']' || *p =='\\')
990 fputs("]pc\n", stdout);
998 errx(1, "%s:%d: %s", filename, lineno, s);
1002 warning(const char *s)
1005 warnx("%s:%d: %s", filename, lineno, s);
1013 for (i = 0; i < UCHAR_MAX; i++) {
1014 str_table[i][0] = i;
1015 str_table[i][1] = '\0';
1017 if (hcreate(1 << 16) == 0)
1026 fprintf(stderr, "usage: %s [-chlqv] [-e expression] [file ...]\n",
1032 escape(const char *str)
1036 ret = malloc(strlen(str) + 1);
1041 while (*str != '\0') {
1043 * We get _escaped_ strings here. Single backslashes are
1044 * already converted to double backslashes
1047 if (*++str == '\\') {
1096 pid = waitpid(dc, &status, WCONTINUED);
1102 if (WIFEXITED(status) || WIFSIGNALED(status))
1118 main(int argc, char *argv[])
1127 sargv = malloc(argc * sizeof(char *));
1131 if ((cmdexpr = strdup("")) == NULL)
1133 /* The d debug option is 4.4 BSD bc(1) compatible */
1134 while ((ch = getopt_long(argc, argv, "cde:hlqv",
1135 long_options, NULL)) != -1) {
1143 if (asprintf(&cmdexpr, "%s%s\n", cmdexpr, optarg) == -1)
1151 sargv[sargc++] = _PATH_LIBB;
1154 /* compatibility option */
1157 fprintf(stderr, "%s (BSD bc) %s\n", __progname, BC_VER);
1168 interactive = isatty(STDIN_FILENO);
1169 for (i = 0; i < argc; i++)
1170 sargv[sargc++] = argv[i];
1174 err(1, "cannot create pipe");
1177 err(1, "cannot fork");
1179 signal(SIGCHLD, sigchld);
1180 close(STDOUT_FILENO);
1185 el = el_init("bc", stdin, stderr, stderr);
1186 hist = history_init();
1187 history(hist, &he, H_SETSIZE, 100);
1188 el_set(el, EL_HIST, history, hist);
1189 el_set(el, EL_EDITOR, "emacs");
1190 el_set(el, EL_SIGNAL, 1);
1191 el_set(el, EL_PROMPT, dummy_prompt);
1192 el_source(el, NULL);
1195 close(STDIN_FILENO);
1199 execl(_PATH_DC, "dc", "-x", (char *)NULL);
1200 err(1, "cannot find dc");