[BACK]Return to bcode.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / dc

Annotation of src/usr.bin/dc/bcode.c, Revision 1.43

1.43    ! otto        1: /*     $OpenBSD: bcode.c,v 1.42 2012/03/08 08:15:37 otto Exp $ */
1.1       otto        2:
                      3: /*
                      4:  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <ssl/ssl.h>
                     20: #include <err.h>
                     21: #include <limits.h>
1.20      otto       22: #include <signal.h>
1.1       otto       23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26:
                     27: #include "extern.h"
                     28:
                     29: BIGNUM         zero;
1.9       otto       30:
                     31: /* #define     DEBUGGING */
1.1       otto       32:
                     33: #define MAX_ARRAY_INDEX                2048
1.27      otto       34: #define READSTACK_SIZE         8
1.1       otto       35:
1.11      otto       36: #define NO_ELSE                        -2      /* -1 is EOF */
1.18      otto       37: #define REG_ARRAY_SIZE_SMALL   (UCHAR_MAX + 1)
                     38: #define REG_ARRAY_SIZE_BIG     (UCHAR_MAX + 1 + USHRT_MAX + 1)
1.11      otto       39:
1.1       otto       40: struct bmachine {
                     41:        struct stack            stack;
                     42:        u_int                   scale;
                     43:        u_int                   obase;
                     44:        u_int                   ibase;
1.27      otto       45:        size_t                  readsp;
1.18      otto       46:        bool                    extended_regs;
                     47:        size_t                  reg_array_size;
                     48:        struct stack            *reg;
1.26      otto       49:        volatile sig_atomic_t   interrupted;
1.27      otto       50:        struct source           *readstack;
                     51:        size_t                  readstack_sz;
1.1       otto       52: };
                     53:
                     54: static struct bmachine bmachine;
1.20      otto       55: static void sighandler(int);
1.1       otto       56:
                     57: static __inline int    readch(void);
1.31      otto       58: static __inline void   unreadch(void);
1.1       otto       59: static __inline char   *readline(void);
                     60: static __inline void   src_free(void);
                     61:
                     62: static __inline u_int  max(u_int, u_int);
                     63: static u_long          get_ulong(struct number *);
                     64:
                     65: static __inline void   push_number(struct number *);
                     66: static __inline void   push_string(char *);
                     67: static __inline void   push(struct value *);
                     68: static __inline struct value *tos(void);
                     69: static __inline struct number  *pop_number(void);
                     70: static __inline char   *pop_string(void);
                     71: static __inline void   clear_stack(void);
                     72: static __inline void   print_tos(void);
1.14      otto       73: static void            pop_print(void);
                     74: static void            pop_printn(void);
1.18      otto       75: static __inline void   print_stack(void);
1.1       otto       76: static __inline void   dup(void);
1.13      otto       77: static void            swap(void);
1.17      otto       78: static void            drop(void);
1.1       otto       79:
                     80: static void            get_scale(void);
                     81: static void            set_scale(void);
                     82: static void            get_obase(void);
                     83: static void            set_obase(void);
                     84: static void            get_ibase(void);
                     85: static void            set_ibase(void);
                     86: static void            stackdepth(void);
                     87: static void            push_scale(void);
                     88: static u_int           count_digits(const struct number *);
                     89: static void            num_digits(void);
1.14      otto       90: static void            to_ascii(void);
1.1       otto       91: static void            push_line(void);
1.14      otto       92: static void            comment(void);
1.1       otto       93: static void            bexec(char *);
                     94: static void            badd(void);
                     95: static void            bsub(void);
                     96: static void            bmul(void);
                     97: static void            bdiv(void);
                     98: static void            bmod(void);
1.8       otto       99: static void            bdivmod(void);
1.1       otto      100: static void            bexp(void);
1.25      otto      101: static bool            bsqrt_stop(const BIGNUM *, const BIGNUM *, u_int *);
1.1       otto      102: static void            bsqrt(void);
1.16      otto      103: static void            not(void);
                    104: static void            equal_numbers(void);
                    105: static void            less_numbers(void);
                    106: static void            lesseq_numbers(void);
1.1       otto      107: static void            equal(void);
                    108: static void            not_equal(void);
                    109: static void            less(void);
                    110: static void            not_less(void);
                    111: static void            greater(void);
                    112: static void            not_greater(void);
                    113: static void            not_compare(void);
1.16      otto      114: static bool            compare_numbers(enum bcode_compare, struct number *,
                    115:                            struct number *);
1.1       otto      116: static void            compare(enum bcode_compare);
1.18      otto      117: static int             readreg(void);
1.1       otto      118: static void            load(void);
                    119: static void            store(void);
                    120: static void            load_stack(void);
                    121: static void            store_stack(void);
                    122: static void            load_array(void);
                    123: static void            store_array(void);
                    124: static void            nop(void);
                    125: static void            quit(void);
                    126: static void            quitN(void);
1.9       otto      127: static void            skipN(void);
                    128: static void            skip_until_mark(void);
1.1       otto      129: static void            parse_number(void);
                    130: static void            unknown(void);
                    131: static void            eval_string(char *);
                    132: static void            eval_line(void);
                    133: static void            eval_tos(void);
                    134:
                    135:
                    136: typedef void           (*opcode_function)(void);
                    137:
                    138: struct jump_entry {
                    139:        u_char          ch;
                    140:        opcode_function f;
                    141: };
                    142:
                    143: static opcode_function jump_table[UCHAR_MAX];
                    144:
                    145: static const struct jump_entry jump_table_data[] = {
1.14      otto      146:        { ' ',  nop             },
                    147:        { '!',  not_compare     },
                    148:        { '#',  comment         },
                    149:        { '%',  bmod            },
1.16      otto      150:        { '(',  less_numbers    },
1.14      otto      151:        { '*',  bmul            },
                    152:        { '+',  badd            },
                    153:        { '-',  bsub            },
                    154:        { '.',  parse_number    },
                    155:        { '/',  bdiv            },
1.1       otto      156:        { '0',  parse_number    },
                    157:        { '1',  parse_number    },
                    158:        { '2',  parse_number    },
                    159:        { '3',  parse_number    },
                    160:        { '4',  parse_number    },
                    161:        { '5',  parse_number    },
                    162:        { '6',  parse_number    },
                    163:        { '7',  parse_number    },
                    164:        { '8',  parse_number    },
                    165:        { '9',  parse_number    },
1.14      otto      166:        { ':',  store_array     },
                    167:        { ';',  load_array      },
                    168:        { '<',  less            },
                    169:        { '=',  equal           },
                    170:        { '>',  greater         },
                    171:        { '?',  eval_line       },
1.1       otto      172:        { 'A',  parse_number    },
                    173:        { 'B',  parse_number    },
                    174:        { 'C',  parse_number    },
                    175:        { 'D',  parse_number    },
                    176:        { 'E',  parse_number    },
                    177:        { 'F',  parse_number    },
1.16      otto      178:        { 'G',  equal_numbers   },
1.14      otto      179:        { 'I',  get_ibase       },
                    180:        { 'J',  skipN           },
                    181:        { 'K',  get_scale       },
1.1       otto      182:        { 'L',  load_stack      },
1.14      otto      183:        { 'M',  nop             },
1.16      otto      184:        { 'N',  not             },
1.14      otto      185:        { 'O',  get_obase       },
1.1       otto      186:        { 'P',  pop_print       },
1.14      otto      187:        { 'Q',  quitN           },
1.17      otto      188:        { 'R',  drop            },
1.14      otto      189:        { 'S',  store_stack     },
1.1       otto      190:        { 'X',  push_scale      },
1.14      otto      191:        { 'Z',  num_digits      },
1.1       otto      192:        { '[',  push_line       },
1.14      otto      193:        { '\f', nop             },
                    194:        { '\n', nop             },
                    195:        { '\r', nop             },
                    196:        { '\t', nop             },
                    197:        { '^',  bexp            },
                    198:        { '_',  parse_number    },
                    199:        { 'a',  to_ascii        },
1.1       otto      200:        { 'c',  clear_stack     },
1.14      otto      201:        { 'd',  dup             },
                    202:        { 'f',  print_stack     },
1.1       otto      203:        { 'i',  set_ibase       },
1.14      otto      204:        { 'k',  set_scale       },
                    205:        { 'l',  load            },
                    206:        { 'n',  pop_printn      },
1.1       otto      207:        { 'o',  set_obase       },
1.16      otto      208:        { 'p',  print_tos       },
1.14      otto      209:        { 'q',  quit            },
1.16      otto      210:        { 'r',  swap            },
1.14      otto      211:        { 's',  store           },
                    212:        { 'v',  bsqrt           },
                    213:        { 'x',  eval_tos        },
1.1       otto      214:        { 'z',  stackdepth      },
1.16      otto      215:        { '{',  lesseq_numbers  },
1.14      otto      216:        { '~',  bdivmod         }
1.1       otto      217: };
                    218:
                    219: #define JUMP_TABLE_DATA_SIZE \
                    220:        (sizeof(jump_table_data)/sizeof(jump_table_data[0]))
                    221:
1.23      deraadt   222: /* ARGSUSED */
1.20      otto      223: static void
                    224: sighandler(int ignored)
                    225: {
                    226:        bmachine.interrupted = true;
                    227: }
                    228:
1.1       otto      229: void
1.18      otto      230: init_bmachine(bool extended_registers)
1.1       otto      231: {
                    232:        int i;
                    233:
1.18      otto      234:        bmachine.extended_regs = extended_registers;
                    235:        bmachine.reg_array_size = bmachine.extended_regs ?
                    236:            REG_ARRAY_SIZE_BIG : REG_ARRAY_SIZE_SMALL;
                    237:
1.35      deraadt   238:        bmachine.reg = calloc(bmachine.reg_array_size,
1.18      otto      239:            sizeof(bmachine.reg[0]));
                    240:        if (bmachine.reg == NULL)
                    241:                err(1, NULL);
                    242:
1.3       deraadt   243:        for (i = 0; i < UCHAR_MAX; i++)
1.1       otto      244:                jump_table[i] = unknown;
                    245:        for (i = 0; i < JUMP_TABLE_DATA_SIZE; i++)
                    246:                jump_table[jump_table_data[i].ch] = jump_table_data[i].f;
                    247:
                    248:        stack_init(&bmachine.stack);
                    249:
1.18      otto      250:        for (i = 0; i < bmachine.reg_array_size; i++)
1.1       otto      251:                stack_init(&bmachine.reg[i]);
                    252:
1.27      otto      253:        bmachine.readstack_sz = READSTACK_SIZE;
1.35      deraadt   254:        bmachine.readstack = calloc(sizeof(struct source),
1.27      otto      255:            bmachine.readstack_sz);
                    256:        if (bmachine.readstack == NULL)
                    257:                err(1, NULL);
1.1       otto      258:        bmachine.obase = bmachine.ibase = 10;
                    259:        BN_init(&zero);
                    260:        bn_check(BN_zero(&zero));
1.31      otto      261:        (void)signal(SIGINT, sighandler);
1.1       otto      262: }
                    263:
                    264: /* Reset the things needed before processing a (new) file */
                    265: void
                    266: reset_bmachine(struct source *src)
                    267: {
                    268:        bmachine.readsp = 0;
                    269:        bmachine.readstack[0] = *src;
                    270: }
                    271:
                    272: static __inline int
                    273: readch(void)
                    274: {
                    275:        struct source *src = &bmachine.readstack[bmachine.readsp];
                    276:
                    277:        return src->vtable->readchar(src);
                    278: }
                    279:
1.31      otto      280: static __inline void
1.1       otto      281: unreadch(void)
                    282: {
                    283:        struct source *src = &bmachine.readstack[bmachine.readsp];
                    284:
1.31      otto      285:        src->vtable->unreadchar(src);
1.1       otto      286: }
                    287:
                    288: static __inline char *
                    289: readline(void)
                    290: {
                    291:        struct source *src = &bmachine.readstack[bmachine.readsp];
                    292:
                    293:        return src->vtable->readline(src);
                    294: }
                    295:
                    296: static __inline void
                    297: src_free(void)
                    298: {
                    299:        struct source *src = &bmachine.readstack[bmachine.readsp];
                    300:
                    301:        src->vtable->free(src);
                    302: }
                    303:
1.10      otto      304: #ifdef DEBUGGING
1.1       otto      305: void
1.28      deraadt   306: pn(const char *str, const struct number *n)
1.1       otto      307: {
                    308:        char *p = BN_bn2dec(n->number);
                    309:        if (p == NULL)
                    310:                err(1, "BN_bn2dec failed");
1.31      otto      311:        (void)fputs(str, stderr);
                    312:        (void)fprintf(stderr, " %s (%u)\n" , p, n->scale);
1.1       otto      313:        OPENSSL_free(p);
                    314: }
                    315:
                    316: void
1.28      deraadt   317: pbn(const char *str, const BIGNUM *n)
1.1       otto      318: {
                    319:        char *p = BN_bn2dec(n);
                    320:        if (p == NULL)
                    321:                err(1, "BN_bn2dec failed");
1.31      otto      322:        (void)fputs(str, stderr);
                    323:        (void)fprintf(stderr, " %s\n", p);
1.1       otto      324:        OPENSSL_free(p);
                    325: }
                    326:
                    327: #endif
                    328:
                    329: static __inline u_int
                    330: max(u_int a, u_int b)
                    331: {
                    332:        return a > b ? a : b;
                    333: }
                    334:
                    335: static unsigned long factors[] = {
                    336:        0, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
                    337:        100000000, 1000000000
                    338: };
                    339:
                    340: void
                    341: scale_number(BIGNUM *n, int s)
                    342: {
                    343:        int abs_scale;
                    344:
                    345:        if (s == 0)
                    346:                return;
                    347:
                    348:        abs_scale = s > 0 ? s : -s;
                    349:
                    350:        if (abs_scale < sizeof(factors)/sizeof(factors[0])) {
                    351:                if (s > 0)
                    352:                        bn_check(BN_mul_word(n, factors[abs_scale]));
                    353:                else
1.31      otto      354:                        (void)BN_div_word(n, factors[abs_scale]);
1.1       otto      355:        } else {
                    356:                BIGNUM *a, *p;
                    357:                BN_CTX *ctx;
                    358:
                    359:                a = BN_new();
                    360:                bn_checkp(a);
                    361:                p = BN_new();
                    362:                bn_checkp(p);
                    363:                ctx = BN_CTX_new();
                    364:                bn_checkp(ctx);
                    365:
                    366:                bn_check(BN_set_word(a, 10));
                    367:                bn_check(BN_set_word(p, abs_scale));
                    368:                bn_check(BN_exp(a, a, p, ctx));
                    369:                if (s > 0)
                    370:                        bn_check(BN_mul(n, n, a, ctx));
                    371:                else
                    372:                        bn_check(BN_div(n, NULL, n, a, ctx));
                    373:                BN_CTX_free(ctx);
                    374:                BN_free(a);
                    375:                BN_free(p);
                    376:        }
                    377: }
                    378:
                    379: void
                    380: split_number(const struct number *n, BIGNUM *i, BIGNUM *f)
                    381: {
                    382:        u_long rem;
                    383:
                    384:        bn_checkp(BN_copy(i, n->number));
                    385:
                    386:        if (n->scale == 0 && f != NULL)
1.31      otto      387:                bn_check(BN_zero(f));
1.1       otto      388:        else if (n->scale < sizeof(factors)/sizeof(factors[0])) {
                    389:                rem = BN_div_word(i, factors[n->scale]);
                    390:                if (f != NULL)
1.31      otto      391:                        bn_check(BN_set_word(f, rem));
1.1       otto      392:        } else {
                    393:                BIGNUM *a, *p;
                    394:                BN_CTX *ctx;
                    395:
                    396:                a = BN_new();
                    397:                bn_checkp(a);
                    398:                p = BN_new();
                    399:                bn_checkp(p);
                    400:                ctx = BN_CTX_new();
                    401:                bn_checkp(ctx);
                    402:
                    403:                bn_check(BN_set_word(a, 10));
                    404:                bn_check(BN_set_word(p, n->scale));
                    405:                bn_check(BN_exp(a, a, p, ctx));
                    406:                bn_check(BN_div(i, f, n->number, a, ctx));
                    407:                BN_CTX_free(ctx);
                    408:                BN_free(a);
                    409:                BN_free(p);
                    410:        }
                    411: }
                    412:
1.41      otto      413: void
1.1       otto      414: normalize(struct number *n, u_int s)
                    415: {
                    416:        scale_number(n->number, s - n->scale);
                    417:        n->scale = s;
                    418: }
                    419:
                    420: static u_long
                    421: get_ulong(struct number *n)
                    422: {
                    423:        normalize(n, 0);
                    424:        return BN_get_word(n->number);
                    425: }
                    426:
                    427: void
                    428: negate(struct number *n)
                    429: {
                    430:        bn_check(BN_sub(n->number, &zero, n->number));
                    431: }
                    432:
                    433: static __inline void
                    434: push_number(struct number *n)
                    435: {
                    436:        stack_pushnumber(&bmachine.stack, n);
                    437: }
                    438:
                    439: static __inline void
                    440: push_string(char *string)
                    441: {
                    442:        stack_pushstring(&bmachine.stack, string);
                    443: }
                    444:
                    445: static __inline void
                    446: push(struct value *v)
                    447: {
                    448:        stack_push(&bmachine.stack, v);
                    449: }
                    450:
                    451: static __inline struct value *
                    452: tos(void)
                    453: {
                    454:        return stack_tos(&bmachine.stack);
                    455: }
                    456:
                    457: static __inline struct value *
                    458: pop(void)
                    459: {
                    460:        return stack_pop(&bmachine.stack);
                    461: }
                    462:
                    463: static __inline struct number *
                    464: pop_number(void)
                    465: {
                    466:        return stack_popnumber(&bmachine.stack);
                    467: }
                    468:
                    469: static __inline char *
                    470: pop_string(void)
                    471: {
                    472:        return stack_popstring(&bmachine.stack);
                    473: }
                    474:
                    475: static __inline void
                    476: clear_stack(void)
                    477: {
                    478:        stack_clear(&bmachine.stack);
                    479: }
                    480:
                    481: static __inline void
                    482: print_stack(void)
                    483: {
                    484:        stack_print(stdout, &bmachine.stack, "", bmachine.obase);
                    485: }
                    486:
                    487: static __inline void
                    488: print_tos(void)
                    489: {
                    490:        struct value *value = tos();
                    491:        if (value != NULL) {
                    492:                print_value(stdout, value, "", bmachine.obase);
1.31      otto      493:                (void)putchar('\n');
1.1       otto      494:        }
                    495:        else
                    496:                warnx("stack empty");
                    497: }
                    498:
1.14      otto      499: static void
1.1       otto      500: pop_print(void)
                    501: {
                    502:        struct value *value = pop();
1.14      otto      503:
1.1       otto      504:        if (value != NULL) {
                    505:                switch (value->type) {
                    506:                case BCODE_NONE:
                    507:                        break;
                    508:                case BCODE_NUMBER:
                    509:                        normalize(value->u.num, 0);
                    510:                        print_ascii(stdout, value->u.num);
1.31      otto      511:                        (void)fflush(stdout);
1.1       otto      512:                        break;
                    513:                case BCODE_STRING:
1.31      otto      514:                        (void)fputs(value->u.string, stdout);
                    515:                        (void)fflush(stdout);
1.1       otto      516:                        break;
                    517:                }
                    518:                stack_free_value(value);
                    519:        }
                    520: }
                    521:
1.14      otto      522: static void
                    523: pop_printn(void)
                    524: {
                    525:        struct value *value = pop();
                    526:
                    527:        if (value != NULL) {
                    528:                print_value(stdout, value, "", bmachine.obase);
1.31      otto      529:                (void)fflush(stdout);
1.14      otto      530:                stack_free_value(value);
                    531:        }
                    532: }
                    533:
1.1       otto      534: static __inline void
                    535: dup(void)
                    536: {
                    537:        stack_dup(&bmachine.stack);
1.13      otto      538: }
                    539:
                    540: static void
                    541: swap(void)
                    542: {
                    543:        stack_swap(&bmachine.stack);
1.17      otto      544: }
                    545:
                    546: static void
                    547: drop(void)
                    548: {
                    549:        struct value *v = pop();
                    550:        if (v != NULL)
                    551:                stack_free_value(v);
1.1       otto      552: }
                    553:
                    554: static void
                    555: get_scale(void)
                    556: {
                    557:        struct number   *n;
                    558:
                    559:        n = new_number();
                    560:        bn_check(BN_set_word(n->number, bmachine.scale));
                    561:        push_number(n);
                    562: }
                    563:
                    564: static void
                    565: set_scale(void)
                    566: {
                    567:        struct number   *n;
                    568:        u_long          scale;
                    569:
                    570:        n = pop_number();
                    571:        if (n != NULL) {
                    572:                if (BN_cmp(n->number, &zero) < 0)
                    573:                        warnx("scale must be a nonnegative number");
                    574:                else {
                    575:                        scale = get_ulong(n);
1.34      otto      576:                        if (scale != BN_MASK2 && scale <= UINT_MAX)
                    577:                                bmachine.scale = (u_int)scale;
1.1       otto      578:                        else
                    579:                                warnx("scale too large");
                    580:                        }
                    581:                free_number(n);
                    582:        }
                    583: }
                    584:
                    585: static void
                    586: get_obase(void)
                    587: {
                    588:        struct number   *n;
                    589:
                    590:        n = new_number();
                    591:        bn_check(BN_set_word(n->number, bmachine.obase));
                    592:        push_number(n);
                    593: }
                    594:
                    595: static void
                    596: set_obase(void)
                    597: {
                    598:        struct number   *n;
                    599:        u_long          base;
                    600:
                    601:        n = pop_number();
                    602:        if (n != NULL) {
                    603:                base = get_ulong(n);
1.34      otto      604:                if (base != BN_MASK2 && base > 1 && base <= UINT_MAX)
                    605:                        bmachine.obase = (u_int)base;
1.1       otto      606:                else
                    607:                        warnx("output base must be a number greater than 1");
                    608:                free_number(n);
                    609:        }
                    610: }
                    611:
                    612: static void
                    613: get_ibase(void)
                    614: {
                    615:        struct number *n;
                    616:
                    617:        n = new_number();
                    618:        bn_check(BN_set_word(n->number, bmachine.ibase));
                    619:        push_number(n);
                    620: }
                    621:
                    622: static void
                    623: set_ibase(void)
                    624: {
                    625:        struct number   *n;
                    626:        u_long          base;
                    627:
                    628:        n = pop_number();
                    629:        if (n != NULL) {
                    630:                base = get_ulong(n);
                    631:                if (base != BN_MASK2 && 2 <= base && base <= 16)
1.34      otto      632:                        bmachine.ibase = (u_int)base;
1.1       otto      633:                else
                    634:                        warnx("input base must be a number between 2 and 16 "
                    635:                            "(inclusive)");
                    636:                free_number(n);
                    637:        }
                    638: }
                    639:
                    640: static void
                    641: stackdepth(void)
                    642: {
1.33      otto      643:        size_t i;
1.1       otto      644:        struct number *n;
                    645:
                    646:        i = stack_size(&bmachine.stack);
                    647:        n = new_number();
                    648:        bn_check(BN_set_word(n->number, i));
                    649:        push_number(n);
                    650: }
                    651:
                    652: static void
                    653: push_scale(void)
                    654: {
                    655:        struct value    *value;
                    656:        u_int           scale = 0;
                    657:        struct number   *n;
                    658:
                    659:
                    660:        value = pop();
                    661:        if (value != NULL) {
                    662:                switch (value->type) {
                    663:                case BCODE_NONE:
                    664:                        return;
                    665:                case BCODE_NUMBER:
                    666:                        scale = value->u.num->scale;
                    667:                        break;
                    668:                case BCODE_STRING:
                    669:                        break;
                    670:                }
                    671:                stack_free_value(value);
                    672:                n = new_number();
                    673:                bn_check(BN_set_word(n->number, scale));
                    674:                push_number(n);
                    675:        }
                    676: }
                    677:
                    678: static u_int
                    679: count_digits(const struct number *n)
                    680: {
                    681:        struct number   *int_part, *fract_part;
                    682:        u_int           i;
                    683:
                    684:        if (BN_is_zero(n->number))
1.42      otto      685:                return n->scale ? n->scale : 1;
1.1       otto      686:
                    687:        int_part = new_number();
                    688:        fract_part = new_number();
                    689:        fract_part->scale = n->scale;
                    690:        split_number(n, int_part->number, fract_part->number);
                    691:
                    692:        i = 0;
                    693:        while (!BN_is_zero(int_part->number)) {
1.31      otto      694:                (void)BN_div_word(int_part->number, 10);
1.1       otto      695:                i++;
                    696:        }
                    697:        free_number(int_part);
                    698:        free_number(fract_part);
                    699:        return i + n->scale;
                    700: }
                    701:
                    702: static void
                    703: num_digits(void)
                    704: {
                    705:        struct value    *value;
1.34      otto      706:        size_t          digits;
1.14      otto      707:        struct number   *n = NULL;
1.1       otto      708:
                    709:        value = pop();
                    710:        if (value != NULL) {
                    711:                switch (value->type) {
                    712:                case BCODE_NONE:
1.14      otto      713:                        return;
1.1       otto      714:                case BCODE_NUMBER:
                    715:                        digits = count_digits(value->u.num);
                    716:                        n = new_number();
                    717:                        bn_check(BN_set_word(n->number, digits));
                    718:                        break;
                    719:                case BCODE_STRING:
                    720:                        digits = strlen(value->u.string);
                    721:                        n = new_number();
                    722:                        bn_check(BN_set_word(n->number, digits));
                    723:                        break;
                    724:                }
1.14      otto      725:                stack_free_value(value);
                    726:                push_number(n);
                    727:        }
                    728: }
                    729:
                    730: static void
                    731: to_ascii(void)
                    732: {
                    733:        char            str[2];
                    734:        struct value    *value;
                    735:        struct number   *n;
                    736:
                    737:        value = pop();
                    738:        if (value != NULL) {
                    739:                str[1] = '\0';
                    740:                switch (value->type) {
                    741:                case BCODE_NONE:
                    742:                        return;
                    743:                case BCODE_NUMBER:
                    744:                        n = value->u.num;
                    745:                        normalize(n, 0);
                    746:                        if (BN_num_bits(n->number) > 8)
                    747:                                bn_check(BN_mask_bits(n->number, 8));
1.33      otto      748:                        str[0] = (char)BN_get_word(n->number);
1.14      otto      749:                        break;
                    750:                case BCODE_STRING:
                    751:                        str[0] = value->u.string[0];
                    752:                        break;
                    753:                }
                    754:                stack_free_value(value);
                    755:                push_string(bstrdup(str));
1.1       otto      756:        }
                    757: }
                    758:
1.18      otto      759: static int
                    760: readreg(void)
                    761: {
1.32      otto      762:        int idx, ch1, ch2;
1.18      otto      763:
1.32      otto      764:        idx = readch();
                    765:        if (idx == 0xff && bmachine.extended_regs) {
1.18      otto      766:                ch1 = readch();
                    767:                ch2 = readch();
                    768:                if (ch1 == EOF || ch2 == EOF) {
                    769:                        warnx("unexpected eof");
1.32      otto      770:                        idx = -1;
1.18      otto      771:                } else
1.32      otto      772:                        idx = (ch1 << 8) + ch2 + UCHAR_MAX + 1;
1.18      otto      773:        }
1.32      otto      774:        if (idx < 0 || idx >= bmachine.reg_array_size) {
                    775:                warnx("internal error: reg num = %d", idx);
                    776:                idx = -1;
1.18      otto      777:        }
1.32      otto      778:        return idx;
1.18      otto      779: }
                    780:
1.1       otto      781: static void
                    782: load(void)
                    783: {
1.32      otto      784:        int             idx;
1.1       otto      785:        struct value    *v, copy;
1.5       otto      786:        struct number   *n;
1.1       otto      787:
1.32      otto      788:        idx = readreg();
                    789:        if (idx >= 0) {
                    790:                v = stack_tos(&bmachine.reg[idx]);
1.5       otto      791:                if (v == NULL) {
                    792:                        n = new_number();
                    793:                        bn_check(BN_zero(n->number));
                    794:                        push_number(n);
                    795:                } else
1.1       otto      796:                        push(stack_dup_value(v, &copy));
1.18      otto      797:        }
1.1       otto      798: }
                    799:
                    800: static void
                    801: store(void)
                    802: {
1.32      otto      803:        int             idx;
1.1       otto      804:        struct value    *val;
                    805:
1.32      otto      806:        idx = readreg();
                    807:        if (idx >= 0) {
1.1       otto      808:                val = pop();
                    809:                if (val == NULL) {
                    810:                        return;
                    811:                }
1.32      otto      812:                stack_set_tos(&bmachine.reg[idx], val);
1.18      otto      813:        }
1.1       otto      814: }
                    815:
                    816: static void
                    817: load_stack(void)
                    818: {
1.32      otto      819:        int             idx;
1.1       otto      820:        struct stack    *stack;
1.37      otto      821:        struct value    *value;
1.1       otto      822:
1.32      otto      823:        idx = readreg();
                    824:        if (idx >= 0) {
                    825:                stack = &bmachine.reg[idx];
1.1       otto      826:                value = NULL;
                    827:                if (stack_size(stack) > 0) {
                    828:                        value = stack_pop(stack);
                    829:                }
                    830:                if (value != NULL)
1.37      otto      831:                        push(value);
1.1       otto      832:                else
                    833:                        warnx("stack register '%c' (0%o) is empty",
1.32      otto      834:                            idx, idx);
1.18      otto      835:        }
1.1       otto      836: }
                    837:
                    838: static void
                    839: store_stack(void)
                    840: {
1.32      otto      841:        int             idx;
1.1       otto      842:        struct value    *value;
                    843:
1.32      otto      844:        idx = readreg();
                    845:        if (idx >= 0) {
1.1       otto      846:                value = pop();
                    847:                if (value == NULL)
                    848:                        return;
1.32      otto      849:                stack_push(&bmachine.reg[idx], value);
1.18      otto      850:        }
1.1       otto      851: }
                    852:
                    853: static void
                    854: load_array(void)
                    855: {
                    856:        int                     reg;
                    857:        struct number           *inumber, *n;
1.32      otto      858:        u_long                  idx;
1.1       otto      859:        struct stack            *stack;
                    860:        struct value            *v, copy;
                    861:
1.18      otto      862:        reg = readreg();
                    863:        if (reg >= 0) {
1.1       otto      864:                inumber = pop_number();
                    865:                if (inumber == NULL)
                    866:                        return;
1.32      otto      867:                idx = get_ulong(inumber);
1.1       otto      868:                if (BN_cmp(inumber->number, &zero) < 0)
1.32      otto      869:                        warnx("negative idx");
                    870:                else if (idx == BN_MASK2 || idx > MAX_ARRAY_INDEX)
                    871:                        warnx("idx too big");
1.1       otto      872:                else {
                    873:                        stack = &bmachine.reg[reg];
1.32      otto      874:                        v = frame_retrieve(stack, idx);
1.39      otto      875:                        if (v == NULL || v->type == BCODE_NONE) {
1.1       otto      876:                                n = new_number();
                    877:                                bn_check(BN_zero(n->number));
                    878:                                push_number(n);
                    879:                        }
                    880:                        else
                    881:                                push(stack_dup_value(v, &copy));
                    882:                }
                    883:                free_number(inumber);
1.18      otto      884:        }
1.1       otto      885: }
                    886:
                    887: static void
                    888: store_array(void)
                    889: {
                    890:        int                     reg;
                    891:        struct number           *inumber;
1.32      otto      892:        u_long                  idx;
1.1       otto      893:        struct value            *value;
                    894:        struct stack            *stack;
                    895:
1.18      otto      896:        reg = readreg();
                    897:        if (reg >= 0) {
1.1       otto      898:                inumber = pop_number();
1.6       otto      899:                if (inumber == NULL)
                    900:                        return;
1.1       otto      901:                value = pop();
1.6       otto      902:                if (value == NULL) {
                    903:                        free_number(inumber);
1.1       otto      904:                        return;
                    905:                }
1.32      otto      906:                idx = get_ulong(inumber);
1.1       otto      907:                if (BN_cmp(inumber->number, &zero) < 0) {
1.32      otto      908:                        warnx("negative idx");
1.1       otto      909:                        stack_free_value(value);
1.32      otto      910:                } else if (idx == BN_MASK2 || idx > MAX_ARRAY_INDEX) {
                    911:                        warnx("idx too big");
1.1       otto      912:                        stack_free_value(value);
                    913:                } else {
                    914:                        stack = &bmachine.reg[reg];
1.32      otto      915:                        frame_assign(stack, idx, value);
1.1       otto      916:                }
                    917:                free_number(inumber);
1.18      otto      918:        }
1.1       otto      919: }
                    920:
                    921: static void
                    922: push_line(void)
                    923: {
                    924:        push_string(read_string(&bmachine.readstack[bmachine.readsp]));
1.14      otto      925: }
                    926:
                    927: static void
                    928: comment(void)
                    929: {
                    930:        free(readline());
1.1       otto      931: }
                    932:
                    933: static void
                    934: bexec(char *line)
                    935: {
1.31      otto      936:        (void)system(line);
1.1       otto      937:        free(line);
                    938: }
                    939:
                    940: static void
                    941: badd(void)
                    942: {
                    943:        struct number   *a, *b;
                    944:        struct number   *r;
                    945:
                    946:        a = pop_number();
                    947:        if (a == NULL) {
                    948:                return;
                    949:        }
                    950:        b = pop_number();
                    951:        if (b == NULL) {
                    952:                push_number(a);
                    953:                return;
                    954:        }
                    955:
                    956:        r = new_number();
                    957:        r->scale = max(a->scale, b->scale);
                    958:        if (r->scale > a->scale)
                    959:                normalize(a, r->scale);
                    960:        else if (r->scale > b->scale)
                    961:                normalize(b, r->scale);
                    962:        bn_check(BN_add(r->number, a->number, b->number));
                    963:        push_number(r);
                    964:        free_number(a);
                    965:        free_number(b);
                    966: }
                    967:
                    968: static void
                    969: bsub(void)
                    970: {
                    971:        struct number   *a, *b;
                    972:        struct number   *r;
                    973:
                    974:        a = pop_number();
                    975:        if (a == NULL) {
                    976:                return;
                    977:        }
                    978:        b = pop_number();
                    979:        if (b == NULL) {
                    980:                push_number(a);
                    981:                return;
                    982:        }
                    983:
                    984:        r = new_number();
                    985:
                    986:        r->scale = max(a->scale, b->scale);
                    987:        if (r->scale > a->scale)
                    988:                normalize(a, r->scale);
                    989:        else if (r->scale > b->scale)
                    990:                normalize(b, r->scale);
                    991:        bn_check(BN_sub(r->number, b->number, a->number));
                    992:        push_number(r);
                    993:        free_number(a);
                    994:        free_number(b);
                    995: }
                    996:
                    997: void
                    998: bmul_number(struct number *r, struct number *a, struct number *b)
                    999: {
                   1000:        BN_CTX          *ctx;
                   1001:
                   1002:        /* Create copies of the scales, since r might be equal to a or b */
                   1003:        u_int ascale = a->scale;
                   1004:        u_int bscale = b->scale;
                   1005:        u_int rscale = ascale + bscale;
                   1006:
                   1007:        ctx = BN_CTX_new();
                   1008:        bn_checkp(ctx);
                   1009:        bn_check(BN_mul(r->number, a->number, b->number, ctx));
                   1010:        BN_CTX_free(ctx);
                   1011:
                   1012:        if (rscale > bmachine.scale && rscale > ascale && rscale > bscale) {
                   1013:                r->scale = rscale;
                   1014:                normalize(r, max(bmachine.scale, max(ascale, bscale)));
                   1015:        } else
                   1016:                r->scale = rscale;
                   1017: }
                   1018:
                   1019: static void
                   1020: bmul(void)
                   1021: {
                   1022:        struct number   *a, *b;
                   1023:        struct number   *r;
                   1024:
                   1025:        a = pop_number();
                   1026:        if (a == NULL) {
                   1027:                return;
                   1028:        }
                   1029:        b = pop_number();
                   1030:        if (b == NULL) {
                   1031:                push_number(a);
                   1032:                return;
                   1033:        }
                   1034:
                   1035:        r = new_number();
                   1036:        bmul_number(r, a, b);
                   1037:
                   1038:        push_number(r);
                   1039:        free_number(a);
                   1040:        free_number(b);
                   1041: }
                   1042:
                   1043: static void
                   1044: bdiv(void)
                   1045: {
                   1046:        struct number   *a, *b;
                   1047:        struct number   *r;
                   1048:        u_int           scale;
                   1049:        BN_CTX          *ctx;
                   1050:
                   1051:        a = pop_number();
                   1052:        if (a == NULL) {
                   1053:                return;
                   1054:        }
                   1055:        b = pop_number();
                   1056:        if (b == NULL) {
                   1057:                push_number(a);
                   1058:                return;
                   1059:        }
                   1060:
                   1061:        r = new_number();
                   1062:        r->scale = bmachine.scale;
                   1063:        scale = max(a->scale, b->scale);
                   1064:
                   1065:        if (BN_is_zero(a->number))
                   1066:                warnx("divide by zero");
                   1067:        else {
                   1068:                normalize(a, scale);
                   1069:                normalize(b, scale + r->scale);
                   1070:
                   1071:                ctx = BN_CTX_new();
                   1072:                bn_checkp(ctx);
                   1073:                bn_check(BN_div(r->number, NULL, b->number, a->number, ctx));
                   1074:                BN_CTX_free(ctx);
                   1075:        }
                   1076:        push_number(r);
                   1077:        free_number(a);
                   1078:        free_number(b);
                   1079: }
                   1080:
                   1081: static void
                   1082: bmod(void)
                   1083: {
                   1084:        struct number   *a, *b;
                   1085:        struct number   *r;
                   1086:        u_int           scale;
                   1087:        BN_CTX          *ctx;
                   1088:
                   1089:        a = pop_number();
                   1090:        if (a == NULL) {
                   1091:                return;
                   1092:        }
                   1093:        b = pop_number();
                   1094:        if (b == NULL) {
                   1095:                push_number(a);
                   1096:                return;
                   1097:        }
                   1098:
                   1099:        r = new_number();
                   1100:        scale = max(a->scale, b->scale);
                   1101:        r->scale = max(b->scale, a->scale + bmachine.scale);
                   1102:
                   1103:        if (BN_is_zero(a->number))
                   1104:                warnx("remainder by zero");
                   1105:        else {
                   1106:                normalize(a, scale);
                   1107:                normalize(b, scale + bmachine.scale);
                   1108:
                   1109:                ctx = BN_CTX_new();
                   1110:                bn_checkp(ctx);
                   1111:                bn_check(BN_mod(r->number, b->number, a->number, ctx));
                   1112:                BN_CTX_free(ctx);
                   1113:        }
                   1114:        push_number(r);
1.8       otto     1115:        free_number(a);
                   1116:        free_number(b);
                   1117: }
                   1118:
                   1119: static void
                   1120: bdivmod(void)
                   1121: {
                   1122:        struct number   *a, *b;
                   1123:        struct number   *rdiv, *rmod;
                   1124:        u_int           scale;
                   1125:        BN_CTX          *ctx;
                   1126:
                   1127:        a = pop_number();
                   1128:        if (a == NULL) {
                   1129:                return;
                   1130:        }
                   1131:        b = pop_number();
                   1132:        if (b == NULL) {
                   1133:                push_number(a);
                   1134:                return;
                   1135:        }
                   1136:
                   1137:        rdiv = new_number();
                   1138:        rmod = new_number();
                   1139:        rdiv->scale = bmachine.scale;
                   1140:        rmod->scale = max(b->scale, a->scale + bmachine.scale);
                   1141:        scale = max(a->scale, b->scale);
                   1142:
                   1143:        if (BN_is_zero(a->number))
                   1144:                warnx("divide by zero");
                   1145:        else {
                   1146:                normalize(a, scale);
                   1147:                normalize(b, scale + bmachine.scale);
                   1148:
                   1149:                ctx = BN_CTX_new();
                   1150:                bn_checkp(ctx);
                   1151:                bn_check(BN_div(rdiv->number, rmod->number,
                   1152:                    b->number, a->number, ctx));
                   1153:                BN_CTX_free(ctx);
                   1154:        }
                   1155:        push_number(rdiv);
                   1156:        push_number(rmod);
1.1       otto     1157:        free_number(a);
                   1158:        free_number(b);
                   1159: }
                   1160:
                   1161: static void
                   1162: bexp(void)
                   1163: {
                   1164:        struct number   *a, *p;
                   1165:        struct number   *r;
                   1166:        bool            neg;
                   1167:        u_int           scale;
                   1168:
                   1169:        p = pop_number();
                   1170:        if (p == NULL) {
                   1171:                return;
                   1172:        }
                   1173:        a = pop_number();
                   1174:        if (a == NULL) {
                   1175:                push_number(p);
                   1176:                return;
                   1177:        }
                   1178:
1.43    ! otto     1179:        if (p->scale != 0) {
        !          1180:                BIGNUM *i, *f;
        !          1181:                i = BN_new();
        !          1182:                bn_checkp(i);
        !          1183:                f = BN_new();
        !          1184:                bn_checkp(f);
        !          1185:                split_number(p, i, f);
        !          1186:                if (!BN_is_zero(f))
        !          1187:                        warnx("Runtime warning: non-zero fractional part in exponent");
        !          1188:                BN_free(i);
        !          1189:                BN_free(f);
        !          1190:        }
        !          1191:
1.1       otto     1192:        normalize(p, 0);
                   1193:
                   1194:        neg = false;
1.43    ! otto     1195:        if (BN_is_negative(p->number)) {
1.1       otto     1196:                neg = true;
                   1197:                negate(p);
                   1198:                scale = bmachine.scale;
                   1199:        } else {
1.29      otto     1200:                /* Posix bc says min(a.scale * b, max(a.scale, scale) */
1.1       otto     1201:                u_long  b;
                   1202:                u_int   m;
                   1203:
                   1204:                b = BN_get_word(p->number);
                   1205:                m = max(a->scale, bmachine.scale);
1.34      otto     1206:                scale = a->scale * (u_int)b;
                   1207:                if (scale > m || (a->scale > 0 && (b == BN_MASK2 ||
                   1208:                    b > UINT_MAX)))
1.1       otto     1209:                        scale = m;
                   1210:        }
1.2       deraadt  1211:
1.1       otto     1212:        if (BN_is_zero(p->number)) {
                   1213:                r = new_number();
                   1214:                bn_check(BN_one(r->number));
                   1215:                normalize(r, scale);
                   1216:        } else {
                   1217:                while (!BN_is_bit_set(p->number, 0)) {
                   1218:                        bmul_number(a, a, a);
                   1219:                        bn_check(BN_rshift1(p->number, p->number));
                   1220:                }
                   1221:
                   1222:                r = dup_number(a);
                   1223:                normalize(r, scale);
                   1224:                bn_check(BN_rshift1(p->number, p->number));
                   1225:
                   1226:                while (!BN_is_zero(p->number)) {
                   1227:                        bmul_number(a, a, a);
                   1228:                        if (BN_is_bit_set(p->number, 0))
                   1229:                                bmul_number(r, r, a);
                   1230:                        bn_check(BN_rshift1(p->number, p->number));
                   1231:                }
                   1232:
                   1233:                if (neg) {
                   1234:                        BN_CTX  *ctx;
                   1235:                        BIGNUM  *one;
                   1236:
                   1237:                        one = BN_new();
                   1238:                        bn_checkp(one);
1.31      otto     1239:                        bn_check(BN_one(one));
1.1       otto     1240:                        ctx = BN_CTX_new();
                   1241:                        bn_checkp(ctx);
1.29      otto     1242:                        scale_number(one, r->scale + scale);
                   1243:                        normalize(r, scale);
1.43    ! otto     1244:
        !          1245:                        if (BN_is_zero(r->number))
        !          1246:                                warnx("divide by zero");
        !          1247:                        else
        !          1248:                                bn_check(BN_div(r->number, NULL, one,
        !          1249:                                    r->number, ctx));
1.1       otto     1250:                        BN_free(one);
                   1251:                        BN_CTX_free(ctx);
1.29      otto     1252:                } else
                   1253:                        normalize(r, scale);
1.1       otto     1254:        }
                   1255:        push_number(r);
                   1256:        free_number(a);
                   1257:        free_number(p);
                   1258: }
                   1259:
                   1260: static bool
1.25      otto     1261: bsqrt_stop(const BIGNUM *x, const BIGNUM *y, u_int *onecount)
1.1       otto     1262: {
                   1263:        BIGNUM *r;
                   1264:        bool ret;
                   1265:
                   1266:        r = BN_new();
                   1267:        bn_checkp(r);
                   1268:        bn_check(BN_sub(r, x, y));
1.25      otto     1269:        if (BN_is_one(r))
                   1270:                (*onecount)++;
                   1271:        ret = BN_is_zero(r);
1.1       otto     1272:        BN_free(r);
1.25      otto     1273:        return ret || *onecount > 1;
1.1       otto     1274: }
                   1275:
                   1276: static void
                   1277: bsqrt(void)
                   1278: {
                   1279:        struct number   *n;
                   1280:        struct number   *r;
                   1281:        BIGNUM          *x, *y;
1.25      otto     1282:        u_int           scale, onecount;
1.1       otto     1283:        BN_CTX          *ctx;
                   1284:
1.25      otto     1285:        onecount = 0;
1.1       otto     1286:        n = pop_number();
                   1287:        if (n == NULL) {
                   1288:                return;
                   1289:        }
                   1290:        if (BN_is_zero(n->number)) {
                   1291:                r = new_number();
                   1292:                push_number(r);
                   1293:        } else if (BN_cmp(n->number, &zero) < 0)
                   1294:                warnx("square root of negative number");
                   1295:        else {
                   1296:                scale = max(bmachine.scale, n->scale);
                   1297:                normalize(n, 2*scale);
                   1298:                x = BN_dup(n->number);
                   1299:                bn_checkp(x);
                   1300:                bn_check(BN_rshift(x, x, BN_num_bits(x)/2));
                   1301:                y = BN_new();
                   1302:                bn_checkp(y);
                   1303:                ctx = BN_CTX_new();
                   1304:                bn_checkp(ctx);
                   1305:                for (;;) {
                   1306:                        bn_checkp(BN_copy(y, x));
                   1307:                        bn_check(BN_div(x, NULL, n->number, x, ctx));
                   1308:                        bn_check(BN_add(x, x, y));
                   1309:                        bn_check(BN_rshift1(x, x));
1.25      otto     1310:                        if (bsqrt_stop(x, y, &onecount))
1.1       otto     1311:                                break;
                   1312:                }
                   1313:                r = bmalloc(sizeof(*r));
                   1314:                r->scale = scale;
                   1315:                r->number = y;
                   1316:                BN_free(x);
                   1317:                BN_CTX_free(ctx);
                   1318:                push_number(r);
                   1319:        }
                   1320:
                   1321:        free_number(n);
                   1322: }
                   1323:
                   1324: static void
1.16      otto     1325: not(void)
                   1326: {
                   1327:        struct number   *a;
                   1328:
                   1329:        a = pop_number();
                   1330:        if (a == NULL) {
                   1331:                return;
                   1332:        }
                   1333:        a->scale = 0;
                   1334:        bn_check(BN_set_word(a->number, BN_get_word(a->number) ? 0 : 1));
                   1335:        push_number(a);
                   1336: }
                   1337:
                   1338: static void
1.1       otto     1339: equal(void)
                   1340: {
                   1341:        compare(BCODE_EQUAL);
                   1342: }
                   1343:
                   1344: static void
1.16      otto     1345: equal_numbers(void)
                   1346: {
                   1347:        struct number *a, *b, *r;
                   1348:
                   1349:        a = pop_number();
                   1350:        if (a == NULL) {
                   1351:                return;
                   1352:        }
                   1353:        b = pop_number();
                   1354:        if (b == NULL) {
                   1355:                push_number(a);
                   1356:                return;
                   1357:        }
                   1358:        r = new_number();
                   1359:        bn_check(BN_set_word(r->number,
                   1360:            compare_numbers(BCODE_EQUAL, a, b) ? 1 : 0));
                   1361:        push_number(r);
                   1362: }
                   1363:
                   1364: static void
                   1365: less_numbers(void)
                   1366: {
                   1367:        struct number *a, *b, *r;
                   1368:
                   1369:        a = pop_number();
                   1370:        if (a == NULL) {
                   1371:                return;
                   1372:        }
                   1373:        b = pop_number();
                   1374:        if (b == NULL) {
                   1375:                push_number(a);
                   1376:                return;
                   1377:        }
                   1378:        r = new_number();
                   1379:        bn_check(BN_set_word(r->number,
                   1380:            compare_numbers(BCODE_LESS, a, b) ? 1 : 0));
                   1381:        push_number(r);
                   1382: }
                   1383:
                   1384: static void
                   1385: lesseq_numbers(void)
                   1386: {
                   1387:        struct number *a, *b, *r;
                   1388:
                   1389:        a = pop_number();
                   1390:        if (a == NULL) {
                   1391:                return;
                   1392:        }
                   1393:        b = pop_number();
                   1394:        if (b == NULL) {
                   1395:                push_number(a);
                   1396:                return;
                   1397:        }
                   1398:        r = new_number();
                   1399:        bn_check(BN_set_word(r->number,
                   1400:            compare_numbers(BCODE_NOT_GREATER, a, b) ? 1 : 0));
                   1401:        push_number(r);
                   1402: }
                   1403:
                   1404: static void
1.1       otto     1405: not_equal(void)
                   1406: {
                   1407:        compare(BCODE_NOT_EQUAL);
                   1408: }
                   1409:
                   1410: static void
                   1411: less(void)
                   1412: {
                   1413:        compare(BCODE_LESS);
                   1414: }
                   1415:
                   1416: static void
                   1417: not_compare(void)
                   1418: {
                   1419:        switch (readch()) {
                   1420:        case '<':
                   1421:                not_less();
                   1422:                break;
                   1423:        case '>':
                   1424:                not_greater();
                   1425:                break;
                   1426:        case '=':
                   1427:                not_equal();
                   1428:                break;
1.2       deraadt  1429:        default:
1.1       otto     1430:                unreadch();
                   1431:                bexec(readline());
                   1432:                break;
                   1433:        }
                   1434: }
                   1435:
                   1436: static void
                   1437: not_less(void)
                   1438: {
                   1439:        compare(BCODE_NOT_LESS);
                   1440: }
                   1441:
                   1442: static void
                   1443: greater(void)
                   1444: {
                   1445:        compare(BCODE_GREATER);
                   1446: }
                   1447:
                   1448: static void
                   1449: not_greater(void)
                   1450: {
                   1451:        compare(BCODE_NOT_GREATER);
                   1452: }
                   1453:
1.16      otto     1454: static bool
                   1455: compare_numbers(enum bcode_compare type, struct number *a, struct number *b)
                   1456: {
                   1457:        u_int   scale;
                   1458:        int     cmp;
                   1459:
                   1460:        scale = max(a->scale, b->scale);
                   1461:
                   1462:        if (scale > a->scale)
                   1463:                normalize(a, scale);
1.30      otto     1464:        else if (scale > b->scale)
1.16      otto     1465:                normalize(b, scale);
                   1466:
                   1467:        cmp = BN_cmp(a->number, b->number);
                   1468:
                   1469:        free_number(a);
                   1470:        free_number(b);
                   1471:
                   1472:        switch (type) {
                   1473:        case BCODE_EQUAL:
                   1474:                return cmp == 0;
                   1475:        case BCODE_NOT_EQUAL:
                   1476:                return cmp != 0;
                   1477:        case BCODE_LESS:
                   1478:                return cmp < 0;
                   1479:        case BCODE_NOT_LESS:
                   1480:                return cmp >= 0;
                   1481:        case BCODE_GREATER:
                   1482:                return cmp > 0;
                   1483:        case BCODE_NOT_GREATER:
                   1484:                return cmp <= 0;
                   1485:        }
                   1486:        return false;
                   1487: }
                   1488:
1.1       otto     1489: static void
                   1490: compare(enum bcode_compare type)
                   1491: {
1.32      otto     1492:        int             idx, elseidx;
1.1       otto     1493:        struct number   *a, *b;
                   1494:        bool            ok;
                   1495:        struct value    *v;
                   1496:
1.32      otto     1497:        elseidx = NO_ELSE;
                   1498:        idx = readreg();
1.11      otto     1499:        if (readch() == 'e')
1.32      otto     1500:                elseidx = readreg();
1.11      otto     1501:        else
                   1502:                unreadch();
1.1       otto     1503:
                   1504:        a = pop_number();
1.11      otto     1505:        if (a == NULL)
1.1       otto     1506:                return;
                   1507:        b = pop_number();
                   1508:        if (b == NULL) {
                   1509:                push_number(a);
                   1510:                return;
                   1511:        }
                   1512:
1.16      otto     1513:        ok = compare_numbers(type, a, b);
1.1       otto     1514:
1.32      otto     1515:        if (!ok && elseidx != NO_ELSE)
                   1516:                idx = elseidx;
1.11      otto     1517:
1.32      otto     1518:        if (idx >= 0 && (ok || (!ok && elseidx != NO_ELSE))) {
                   1519:                v = stack_tos(&bmachine.reg[idx]);
1.1       otto     1520:                if (v == NULL)
1.32      otto     1521:                        warnx("register '%c' (0%o) is empty", idx, idx);
1.1       otto     1522:                else {
                   1523:                        switch(v->type) {
                   1524:                        case BCODE_NONE:
1.32      otto     1525:                                warnx("register '%c' (0%o) is empty", idx, idx);
1.1       otto     1526:                                break;
                   1527:                        case BCODE_NUMBER:
                   1528:                                warn("eval called with non-string argument");
                   1529:                                break;
                   1530:                        case BCODE_STRING:
                   1531:                                eval_string(bstrdup(v->u.string));
                   1532:                                break;
                   1533:                        }
                   1534:                }
                   1535:        }
                   1536: }
                   1537:
                   1538:
                   1539: static void
                   1540: nop(void)
                   1541: {
                   1542: }
                   1543:
1.2       deraadt  1544: static void
1.1       otto     1545: quit(void)
                   1546: {
1.2       deraadt  1547:        if (bmachine.readsp < 2)
1.1       otto     1548:                exit(0);
                   1549:        src_free();
                   1550:        bmachine.readsp--;
                   1551:        src_free();
                   1552:        bmachine.readsp--;
                   1553: }
                   1554:
                   1555: static void
                   1556: quitN(void)
                   1557: {
                   1558:        struct number   *n;
                   1559:        u_long          i;
                   1560:
                   1561:        n = pop_number();
                   1562:        if (n == NULL)
                   1563:                return;
                   1564:        i = get_ulong(n);
1.37      otto     1565:        free_number(n);
1.1       otto     1566:        if (i == BN_MASK2 || i == 0)
                   1567:                warnx("Q command requires a number >= 1");
                   1568:        else if (bmachine.readsp < i)
                   1569:                warnx("Q command argument exceeded string execution depth");
                   1570:        else {
                   1571:                while (i-- > 0) {
                   1572:                        src_free();
                   1573:                        bmachine.readsp--;
                   1574:                }
                   1575:        }
                   1576: }
                   1577:
                   1578: static void
1.9       otto     1579: skipN(void)
                   1580: {
                   1581:        struct number   *n;
                   1582:        u_long          i;
                   1583:
                   1584:        n = pop_number();
                   1585:        if (n == NULL)
                   1586:                return;
                   1587:        i = get_ulong(n);
                   1588:        if (i == BN_MASK2)
                   1589:                warnx("J command requires a number >= 0");
                   1590:        else if (i > 0 && bmachine.readsp < i)
                   1591:                warnx("J command argument exceeded string execution depth");
                   1592:        else {
                   1593:                while (i-- > 0) {
                   1594:                        src_free();
                   1595:                        bmachine.readsp--;
                   1596:                }
                   1597:                skip_until_mark();
                   1598:        }
                   1599: }
                   1600:
                   1601: static void
                   1602: skip_until_mark(void)
                   1603: {
                   1604:        int ch;
                   1605:
                   1606:        for (;;) {
                   1607:                ch = readch();
                   1608:                switch (ch) {
                   1609:                case 'M':
                   1610:                        return;
                   1611:                case EOF:
                   1612:                        errx(1, "mark not found");
                   1613:                        return;
                   1614:                case 'l':
                   1615:                case 'L':
                   1616:                case 's':
                   1617:                case 'S':
                   1618:                case ':':
                   1619:                case ';':
                   1620:                case '<':
                   1621:                case '>':
                   1622:                case '=':
1.31      otto     1623:                        (void)readreg();
1.12      otto     1624:                        if (readch() == 'e')
1.31      otto     1625:                                (void)readreg();
1.12      otto     1626:                        else
                   1627:                                unreadch();
1.9       otto     1628:                        break;
                   1629:                case '[':
                   1630:                        free(read_string(&bmachine.readstack[bmachine.readsp]));
                   1631:                        break;
                   1632:                case '!':
                   1633:                        switch (ch = readch()) {
                   1634:                                case '<':
                   1635:                                case '>':
                   1636:                                case '=':
1.31      otto     1637:                                        (void)readreg();
1.12      otto     1638:                                        if (readch() == 'e')
1.31      otto     1639:                                                (void)readreg();
1.12      otto     1640:                                        else
                   1641:                                                unreadch();
1.9       otto     1642:                                        break;
                   1643:                                default:
                   1644:                                        free(readline());
                   1645:                                        break;
                   1646:                        }
                   1647:                        break;
                   1648:                default:
                   1649:                        break;
                   1650:                }
                   1651:        }
                   1652: }
                   1653:
                   1654: static void
1.1       otto     1655: parse_number(void)
                   1656: {
                   1657:        unreadch();
                   1658:        push_number(readnumber(&bmachine.readstack[bmachine.readsp],
                   1659:            bmachine.ibase));
                   1660: }
                   1661:
                   1662: static void
                   1663: unknown(void)
                   1664: {
                   1665:        int ch = bmachine.readstack[bmachine.readsp].lastchar;
                   1666:        warnx("%c (0%o) is unimplemented", ch, ch);
                   1667: }
                   1668:
                   1669: static void
                   1670: eval_string(char *p)
                   1671: {
                   1672:        int ch;
                   1673:
                   1674:        if (bmachine.readsp > 0) {
                   1675:                /* Check for tail call. Do not recurse in that case. */
                   1676:                ch = readch();
                   1677:                if (ch == EOF) {
                   1678:                        src_free();
                   1679:                        src_setstring(&bmachine.readstack[bmachine.readsp], p);
                   1680:                        return;
                   1681:                } else
                   1682:                        unreadch();
                   1683:        }
1.27      otto     1684:        if (bmachine.readsp == bmachine.readstack_sz - 1) {
                   1685:                size_t newsz = bmachine.readstack_sz * 2;
                   1686:                struct source *stack;
                   1687:                stack = realloc(bmachine.readstack, newsz *
                   1688:                    sizeof(struct source));
                   1689:                if (stack == NULL)
                   1690:                        err(1, "recursion too deep");
                   1691:                bmachine.readstack_sz = newsz;
                   1692:                bmachine.readstack = stack;
                   1693:        }
1.1       otto     1694:        src_setstring(&bmachine.readstack[++bmachine.readsp], p);
                   1695: }
                   1696:
                   1697: static void
                   1698: eval_line(void)
                   1699: {
                   1700:        /* Always read from stdin */
                   1701:        struct source   in;
                   1702:        char            *p;
                   1703:
1.38      otto     1704:        clearerr(stdin);
1.1       otto     1705:        src_setstream(&in, stdin);
                   1706:        p = (*in.vtable->readline)(&in);
                   1707:        eval_string(p);
                   1708: }
                   1709:
                   1710: static void
                   1711: eval_tos(void)
                   1712: {
                   1713:        char *p;
                   1714:
                   1715:        p = pop_string();
                   1716:        if (p == NULL)
                   1717:                return;
                   1718:        eval_string(p);
                   1719: }
                   1720:
                   1721: void
                   1722: eval(void)
                   1723: {
                   1724:        int     ch;
                   1725:
                   1726:        for (;;) {
                   1727:                ch = readch();
                   1728:                if (ch == EOF) {
                   1729:                        if (bmachine.readsp == 0)
1.24      otto     1730:                                return;
1.1       otto     1731:                        src_free();
                   1732:                        bmachine.readsp--;
                   1733:                        continue;
1.20      otto     1734:                }
                   1735:                if (bmachine.interrupted) {
                   1736:                        if (bmachine.readsp > 0) {
                   1737:                                src_free();
                   1738:                                bmachine.readsp--;
                   1739:                                continue;
1.22      otto     1740:                        } else
1.20      otto     1741:                                bmachine.interrupted = false;
1.1       otto     1742:                }
1.9       otto     1743: #ifdef DEBUGGING
1.31      otto     1744:                (void)fprintf(stderr, "# %c\n", ch);
1.9       otto     1745:                stack_print(stderr, &bmachine.stack, "* ",
                   1746:                    bmachine.obase);
1.36      otto     1747:                (void)fprintf(stderr, "%zd =>\n", bmachine.readsp);
1.9       otto     1748: #endif
1.1       otto     1749:
                   1750:                if (0 <= ch && ch < UCHAR_MAX)
                   1751:                        (*jump_table[ch])();
                   1752:                else
                   1753:                        warnx("internal error: opcode %d", ch);
                   1754:
1.9       otto     1755: #ifdef DEBUGGING
                   1756:                stack_print(stderr, &bmachine.stack, "* ",
                   1757:                    bmachine.obase);
1.36      otto     1758:                (void)fprintf(stderr, "%zd ==\n", bmachine.readsp);
1.9       otto     1759: #endif
1.1       otto     1760:        }
                   1761: }