[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.48

1.48    ! otto        1: /*     $OpenBSD: bcode.c,v 1.47 2015/02/16 20:53:34 jca 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 <err.h>
                     20: #include <limits.h>
1.20      otto       21: #include <signal.h>
1.1       otto       22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
                     26: #include "extern.h"
                     27:
1.9       otto       28: /* #define     DEBUGGING */
1.1       otto       29:
                     30: #define MAX_ARRAY_INDEX                2048
1.27      otto       31: #define READSTACK_SIZE         8
1.1       otto       32:
1.11      otto       33: #define NO_ELSE                        -2      /* -1 is EOF */
1.18      otto       34: #define REG_ARRAY_SIZE_SMALL   (UCHAR_MAX + 1)
                     35: #define REG_ARRAY_SIZE_BIG     (UCHAR_MAX + 1 + USHRT_MAX + 1)
1.11      otto       36:
1.1       otto       37: struct bmachine {
                     38:        struct stack            stack;
                     39:        u_int                   scale;
                     40:        u_int                   obase;
                     41:        u_int                   ibase;
1.27      otto       42:        size_t                  readsp;
1.18      otto       43:        bool                    extended_regs;
                     44:        size_t                  reg_array_size;
                     45:        struct stack            *reg;
1.26      otto       46:        volatile sig_atomic_t   interrupted;
1.27      otto       47:        struct source           *readstack;
                     48:        size_t                  readstack_sz;
1.1       otto       49: };
                     50:
                     51: static struct bmachine bmachine;
1.20      otto       52: static void sighandler(int);
1.1       otto       53:
                     54: static __inline int    readch(void);
1.31      otto       55: static __inline void   unreadch(void);
1.1       otto       56: static __inline char   *readline(void);
                     57: static __inline void   src_free(void);
                     58:
                     59: static __inline u_int  max(u_int, u_int);
                     60: static u_long          get_ulong(struct number *);
                     61:
                     62: static __inline void   push_number(struct number *);
                     63: static __inline void   push_string(char *);
                     64: static __inline void   push(struct value *);
                     65: static __inline struct value *tos(void);
                     66: static __inline struct number  *pop_number(void);
                     67: static __inline char   *pop_string(void);
                     68: static __inline void   clear_stack(void);
                     69: static __inline void   print_tos(void);
1.14      otto       70: static void            pop_print(void);
                     71: static void            pop_printn(void);
1.18      otto       72: static __inline void   print_stack(void);
1.1       otto       73: static __inline void   dup(void);
1.13      otto       74: static void            swap(void);
1.17      otto       75: static void            drop(void);
1.1       otto       76:
                     77: static void            get_scale(void);
                     78: static void            set_scale(void);
                     79: static void            get_obase(void);
                     80: static void            set_obase(void);
                     81: static void            get_ibase(void);
                     82: static void            set_ibase(void);
                     83: static void            stackdepth(void);
                     84: static void            push_scale(void);
                     85: static u_int           count_digits(const struct number *);
                     86: static void            num_digits(void);
1.14      otto       87: static void            to_ascii(void);
1.1       otto       88: static void            push_line(void);
1.14      otto       89: static void            comment(void);
1.1       otto       90: static void            badd(void);
                     91: static void            bsub(void);
                     92: static void            bmul(void);
                     93: static void            bdiv(void);
                     94: static void            bmod(void);
1.8       otto       95: static void            bdivmod(void);
1.1       otto       96: static void            bexp(void);
1.25      otto       97: static bool            bsqrt_stop(const BIGNUM *, const BIGNUM *, u_int *);
1.1       otto       98: static void            bsqrt(void);
1.16      otto       99: static void            not(void);
                    100: static void            equal_numbers(void);
                    101: static void            less_numbers(void);
                    102: static void            lesseq_numbers(void);
1.1       otto      103: static void            equal(void);
                    104: static void            not_equal(void);
                    105: static void            less(void);
                    106: static void            not_less(void);
                    107: static void            greater(void);
                    108: static void            not_greater(void);
                    109: static void            not_compare(void);
1.16      otto      110: static bool            compare_numbers(enum bcode_compare, struct number *,
                    111:                            struct number *);
1.1       otto      112: static void            compare(enum bcode_compare);
1.18      otto      113: static int             readreg(void);
1.1       otto      114: static void            load(void);
                    115: static void            store(void);
                    116: static void            load_stack(void);
                    117: static void            store_stack(void);
                    118: static void            load_array(void);
                    119: static void            store_array(void);
                    120: static void            nop(void);
                    121: static void            quit(void);
                    122: static void            quitN(void);
1.9       otto      123: static void            skipN(void);
                    124: static void            skip_until_mark(void);
1.1       otto      125: static void            parse_number(void);
                    126: static void            unknown(void);
                    127: static void            eval_string(char *);
                    128: static void            eval_line(void);
                    129: static void            eval_tos(void);
                    130:
                    131:
                    132: typedef void           (*opcode_function)(void);
                    133:
                    134: struct jump_entry {
                    135:        u_char          ch;
                    136:        opcode_function f;
                    137: };
                    138:
                    139: static opcode_function jump_table[UCHAR_MAX];
                    140:
                    141: static const struct jump_entry jump_table_data[] = {
1.14      otto      142:        { ' ',  nop             },
                    143:        { '!',  not_compare     },
                    144:        { '#',  comment         },
                    145:        { '%',  bmod            },
1.16      otto      146:        { '(',  less_numbers    },
1.14      otto      147:        { '*',  bmul            },
                    148:        { '+',  badd            },
                    149:        { '-',  bsub            },
                    150:        { '.',  parse_number    },
                    151:        { '/',  bdiv            },
1.1       otto      152:        { '0',  parse_number    },
                    153:        { '1',  parse_number    },
                    154:        { '2',  parse_number    },
                    155:        { '3',  parse_number    },
                    156:        { '4',  parse_number    },
                    157:        { '5',  parse_number    },
                    158:        { '6',  parse_number    },
                    159:        { '7',  parse_number    },
                    160:        { '8',  parse_number    },
                    161:        { '9',  parse_number    },
1.14      otto      162:        { ':',  store_array     },
                    163:        { ';',  load_array      },
                    164:        { '<',  less            },
                    165:        { '=',  equal           },
                    166:        { '>',  greater         },
                    167:        { '?',  eval_line       },
1.1       otto      168:        { 'A',  parse_number    },
                    169:        { 'B',  parse_number    },
                    170:        { 'C',  parse_number    },
                    171:        { 'D',  parse_number    },
                    172:        { 'E',  parse_number    },
                    173:        { 'F',  parse_number    },
1.16      otto      174:        { 'G',  equal_numbers   },
1.14      otto      175:        { 'I',  get_ibase       },
                    176:        { 'J',  skipN           },
                    177:        { 'K',  get_scale       },
1.1       otto      178:        { 'L',  load_stack      },
1.14      otto      179:        { 'M',  nop             },
1.16      otto      180:        { 'N',  not             },
1.14      otto      181:        { 'O',  get_obase       },
1.1       otto      182:        { 'P',  pop_print       },
1.14      otto      183:        { 'Q',  quitN           },
1.17      otto      184:        { 'R',  drop            },
1.14      otto      185:        { 'S',  store_stack     },
1.1       otto      186:        { 'X',  push_scale      },
1.14      otto      187:        { 'Z',  num_digits      },
1.1       otto      188:        { '[',  push_line       },
1.14      otto      189:        { '\f', nop             },
                    190:        { '\n', nop             },
                    191:        { '\r', nop             },
                    192:        { '\t', nop             },
                    193:        { '^',  bexp            },
                    194:        { '_',  parse_number    },
                    195:        { 'a',  to_ascii        },
1.1       otto      196:        { 'c',  clear_stack     },
1.14      otto      197:        { 'd',  dup             },
                    198:        { 'f',  print_stack     },
1.1       otto      199:        { 'i',  set_ibase       },
1.14      otto      200:        { 'k',  set_scale       },
                    201:        { 'l',  load            },
                    202:        { 'n',  pop_printn      },
1.1       otto      203:        { 'o',  set_obase       },
1.16      otto      204:        { 'p',  print_tos       },
1.14      otto      205:        { 'q',  quit            },
1.16      otto      206:        { 'r',  swap            },
1.14      otto      207:        { 's',  store           },
                    208:        { 'v',  bsqrt           },
                    209:        { 'x',  eval_tos        },
1.1       otto      210:        { 'z',  stackdepth      },
1.16      otto      211:        { '{',  lesseq_numbers  },
1.14      otto      212:        { '~',  bdivmod         }
1.1       otto      213: };
                    214:
                    215: #define JUMP_TABLE_DATA_SIZE \
                    216:        (sizeof(jump_table_data)/sizeof(jump_table_data[0]))
                    217:
1.23      deraadt   218: /* ARGSUSED */
1.20      otto      219: static void
                    220: sighandler(int ignored)
                    221: {
                    222:        bmachine.interrupted = true;
                    223: }
                    224:
1.1       otto      225: void
1.18      otto      226: init_bmachine(bool extended_registers)
1.1       otto      227: {
                    228:        int i;
                    229:
1.18      otto      230:        bmachine.extended_regs = extended_registers;
                    231:        bmachine.reg_array_size = bmachine.extended_regs ?
                    232:            REG_ARRAY_SIZE_BIG : REG_ARRAY_SIZE_SMALL;
                    233:
1.35      deraadt   234:        bmachine.reg = calloc(bmachine.reg_array_size,
1.18      otto      235:            sizeof(bmachine.reg[0]));
                    236:        if (bmachine.reg == NULL)
                    237:                err(1, NULL);
                    238:
1.3       deraadt   239:        for (i = 0; i < UCHAR_MAX; i++)
1.1       otto      240:                jump_table[i] = unknown;
                    241:        for (i = 0; i < JUMP_TABLE_DATA_SIZE; i++)
                    242:                jump_table[jump_table_data[i].ch] = jump_table_data[i].f;
                    243:
                    244:        stack_init(&bmachine.stack);
                    245:
1.18      otto      246:        for (i = 0; i < bmachine.reg_array_size; i++)
1.1       otto      247:                stack_init(&bmachine.reg[i]);
                    248:
1.27      otto      249:        bmachine.readstack_sz = READSTACK_SIZE;
1.35      deraadt   250:        bmachine.readstack = calloc(sizeof(struct source),
1.27      otto      251:            bmachine.readstack_sz);
                    252:        if (bmachine.readstack == NULL)
                    253:                err(1, NULL);
1.1       otto      254:        bmachine.obase = bmachine.ibase = 10;
1.31      otto      255:        (void)signal(SIGINT, sighandler);
1.1       otto      256: }
                    257:
1.45      otto      258: u_int
                    259: bmachine_scale(void)
                    260: {
                    261:        return bmachine.scale;
                    262: }
                    263:
1.1       otto      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: {
1.44      otto      430:        BN_set_negative(n->number, !BN_is_negative(n->number));
1.1       otto      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) {
1.44      otto      572:                if (BN_is_negative(n->number))
1.1       otto      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.44      otto      868:                if (BN_is_negative(inumber->number))
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.44      otto      907:                if (BN_is_negative(inumber->number)) {
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: badd(void)
                    935: {
                    936:        struct number   *a, *b;
                    937:        struct number   *r;
                    938:
                    939:        a = pop_number();
                    940:        if (a == NULL) {
                    941:                return;
                    942:        }
                    943:        b = pop_number();
                    944:        if (b == NULL) {
                    945:                push_number(a);
                    946:                return;
                    947:        }
                    948:
                    949:        r = new_number();
                    950:        r->scale = max(a->scale, b->scale);
                    951:        if (r->scale > a->scale)
                    952:                normalize(a, r->scale);
                    953:        else if (r->scale > b->scale)
                    954:                normalize(b, r->scale);
                    955:        bn_check(BN_add(r->number, a->number, b->number));
                    956:        push_number(r);
                    957:        free_number(a);
                    958:        free_number(b);
                    959: }
                    960:
                    961: static void
                    962: bsub(void)
                    963: {
                    964:        struct number   *a, *b;
                    965:        struct number   *r;
                    966:
                    967:        a = pop_number();
                    968:        if (a == NULL) {
                    969:                return;
                    970:        }
                    971:        b = pop_number();
                    972:        if (b == NULL) {
                    973:                push_number(a);
                    974:                return;
                    975:        }
                    976:
                    977:        r = new_number();
                    978:
                    979:        r->scale = max(a->scale, b->scale);
                    980:        if (r->scale > a->scale)
                    981:                normalize(a, r->scale);
                    982:        else if (r->scale > b->scale)
                    983:                normalize(b, r->scale);
                    984:        bn_check(BN_sub(r->number, b->number, a->number));
                    985:        push_number(r);
                    986:        free_number(a);
                    987:        free_number(b);
                    988: }
                    989:
                    990: void
1.45      otto      991: bmul_number(struct number *r, struct number *a, struct number *b, u_int scale)
1.1       otto      992: {
                    993:        BN_CTX          *ctx;
                    994:
                    995:        /* Create copies of the scales, since r might be equal to a or b */
                    996:        u_int ascale = a->scale;
                    997:        u_int bscale = b->scale;
                    998:        u_int rscale = ascale + bscale;
                    999:
                   1000:        ctx = BN_CTX_new();
                   1001:        bn_checkp(ctx);
                   1002:        bn_check(BN_mul(r->number, a->number, b->number, ctx));
                   1003:        BN_CTX_free(ctx);
                   1004:
1.45      otto     1005:        r->scale = rscale;
                   1006:        if (rscale > bmachine.scale && rscale > ascale && rscale > bscale)
                   1007:                normalize(r, max(scale, max(ascale, bscale)));
1.1       otto     1008: }
                   1009:
                   1010: static void
                   1011: bmul(void)
                   1012: {
                   1013:        struct number   *a, *b;
                   1014:        struct number   *r;
                   1015:
                   1016:        a = pop_number();
                   1017:        if (a == NULL) {
                   1018:                return;
                   1019:        }
                   1020:        b = pop_number();
                   1021:        if (b == NULL) {
                   1022:                push_number(a);
                   1023:                return;
                   1024:        }
                   1025:
                   1026:        r = new_number();
1.45      otto     1027:        bmul_number(r, a, b, bmachine.scale);
1.1       otto     1028:
                   1029:        push_number(r);
                   1030:        free_number(a);
                   1031:        free_number(b);
                   1032: }
                   1033:
                   1034: static void
                   1035: bdiv(void)
                   1036: {
                   1037:        struct number   *a, *b;
                   1038:        struct number   *r;
                   1039:        u_int           scale;
                   1040:        BN_CTX          *ctx;
                   1041:
                   1042:        a = pop_number();
                   1043:        if (a == NULL) {
                   1044:                return;
                   1045:        }
                   1046:        b = pop_number();
                   1047:        if (b == NULL) {
                   1048:                push_number(a);
                   1049:                return;
                   1050:        }
                   1051:
                   1052:        r = new_number();
                   1053:        r->scale = bmachine.scale;
                   1054:        scale = max(a->scale, b->scale);
                   1055:
                   1056:        if (BN_is_zero(a->number))
                   1057:                warnx("divide by zero");
                   1058:        else {
                   1059:                normalize(a, scale);
                   1060:                normalize(b, scale + r->scale);
                   1061:
                   1062:                ctx = BN_CTX_new();
                   1063:                bn_checkp(ctx);
                   1064:                bn_check(BN_div(r->number, NULL, b->number, a->number, ctx));
                   1065:                BN_CTX_free(ctx);
                   1066:        }
                   1067:        push_number(r);
                   1068:        free_number(a);
                   1069:        free_number(b);
                   1070: }
                   1071:
                   1072: static void
                   1073: bmod(void)
                   1074: {
                   1075:        struct number   *a, *b;
                   1076:        struct number   *r;
                   1077:        u_int           scale;
                   1078:        BN_CTX          *ctx;
                   1079:
                   1080:        a = pop_number();
                   1081:        if (a == NULL) {
                   1082:                return;
                   1083:        }
                   1084:        b = pop_number();
                   1085:        if (b == NULL) {
                   1086:                push_number(a);
                   1087:                return;
                   1088:        }
                   1089:
                   1090:        r = new_number();
                   1091:        scale = max(a->scale, b->scale);
                   1092:        r->scale = max(b->scale, a->scale + bmachine.scale);
                   1093:
                   1094:        if (BN_is_zero(a->number))
                   1095:                warnx("remainder by zero");
                   1096:        else {
                   1097:                normalize(a, scale);
                   1098:                normalize(b, scale + bmachine.scale);
                   1099:
                   1100:                ctx = BN_CTX_new();
                   1101:                bn_checkp(ctx);
                   1102:                bn_check(BN_mod(r->number, b->number, a->number, ctx));
                   1103:                BN_CTX_free(ctx);
                   1104:        }
                   1105:        push_number(r);
1.8       otto     1106:        free_number(a);
                   1107:        free_number(b);
                   1108: }
                   1109:
                   1110: static void
                   1111: bdivmod(void)
                   1112: {
                   1113:        struct number   *a, *b;
                   1114:        struct number   *rdiv, *rmod;
                   1115:        u_int           scale;
                   1116:        BN_CTX          *ctx;
                   1117:
                   1118:        a = pop_number();
                   1119:        if (a == NULL) {
                   1120:                return;
                   1121:        }
                   1122:        b = pop_number();
                   1123:        if (b == NULL) {
                   1124:                push_number(a);
                   1125:                return;
                   1126:        }
                   1127:
                   1128:        rdiv = new_number();
                   1129:        rmod = new_number();
                   1130:        rdiv->scale = bmachine.scale;
                   1131:        rmod->scale = max(b->scale, a->scale + bmachine.scale);
                   1132:        scale = max(a->scale, b->scale);
                   1133:
                   1134:        if (BN_is_zero(a->number))
                   1135:                warnx("divide by zero");
                   1136:        else {
                   1137:                normalize(a, scale);
                   1138:                normalize(b, scale + bmachine.scale);
                   1139:
                   1140:                ctx = BN_CTX_new();
                   1141:                bn_checkp(ctx);
                   1142:                bn_check(BN_div(rdiv->number, rmod->number,
                   1143:                    b->number, a->number, ctx));
                   1144:                BN_CTX_free(ctx);
                   1145:        }
                   1146:        push_number(rdiv);
                   1147:        push_number(rmod);
1.1       otto     1148:        free_number(a);
                   1149:        free_number(b);
                   1150: }
                   1151:
                   1152: static void
                   1153: bexp(void)
                   1154: {
                   1155:        struct number   *a, *p;
                   1156:        struct number   *r;
                   1157:        bool            neg;
1.45      otto     1158:        u_int           rscale;
1.1       otto     1159:
                   1160:        p = pop_number();
                   1161:        if (p == NULL) {
                   1162:                return;
                   1163:        }
                   1164:        a = pop_number();
                   1165:        if (a == NULL) {
                   1166:                push_number(p);
                   1167:                return;
                   1168:        }
                   1169:
1.43      otto     1170:        if (p->scale != 0) {
                   1171:                BIGNUM *i, *f;
                   1172:                i = BN_new();
                   1173:                bn_checkp(i);
                   1174:                f = BN_new();
                   1175:                bn_checkp(f);
                   1176:                split_number(p, i, f);
                   1177:                if (!BN_is_zero(f))
                   1178:                        warnx("Runtime warning: non-zero fractional part in exponent");
                   1179:                BN_free(i);
                   1180:                BN_free(f);
                   1181:        }
                   1182:
1.1       otto     1183:        normalize(p, 0);
                   1184:
                   1185:        neg = false;
1.43      otto     1186:        if (BN_is_negative(p->number)) {
1.1       otto     1187:                neg = true;
                   1188:                negate(p);
1.45      otto     1189:                rscale = bmachine.scale;
1.1       otto     1190:        } else {
1.29      otto     1191:                /* Posix bc says min(a.scale * b, max(a.scale, scale) */
1.1       otto     1192:                u_long  b;
                   1193:                u_int   m;
                   1194:
                   1195:                b = BN_get_word(p->number);
                   1196:                m = max(a->scale, bmachine.scale);
1.45      otto     1197:                rscale = a->scale * (u_int)b;
                   1198:                if (rscale > m || (a->scale > 0 && (b == BN_MASK2 ||
1.34      otto     1199:                    b > UINT_MAX)))
1.45      otto     1200:                        rscale = m;
1.1       otto     1201:        }
1.2       deraadt  1202:
1.1       otto     1203:        if (BN_is_zero(p->number)) {
                   1204:                r = new_number();
                   1205:                bn_check(BN_one(r->number));
1.45      otto     1206:                normalize(r, rscale);
1.1       otto     1207:        } else {
1.45      otto     1208:                u_int ascale, mscale;
                   1209:
                   1210:                ascale = a->scale;
1.1       otto     1211:                while (!BN_is_bit_set(p->number, 0)) {
1.45      otto     1212:                        ascale *= 2;
                   1213:                        bmul_number(a, a, a, ascale);
1.1       otto     1214:                        bn_check(BN_rshift1(p->number, p->number));
                   1215:                }
                   1216:
                   1217:                r = dup_number(a);
                   1218:                bn_check(BN_rshift1(p->number, p->number));
                   1219:
1.45      otto     1220:                mscale = ascale;
1.1       otto     1221:                while (!BN_is_zero(p->number)) {
1.45      otto     1222:                        ascale *= 2;
                   1223:                        bmul_number(a, a, a, ascale);
                   1224:                        if (BN_is_bit_set(p->number, 0)) {
                   1225:                                mscale += ascale;
                   1226:                                bmul_number(r, r, a, mscale);
                   1227:                        }
1.1       otto     1228:                        bn_check(BN_rshift1(p->number, p->number));
                   1229:                }
                   1230:
                   1231:                if (neg) {
                   1232:                        BN_CTX  *ctx;
                   1233:                        BIGNUM  *one;
                   1234:
                   1235:                        one = BN_new();
                   1236:                        bn_checkp(one);
1.31      otto     1237:                        bn_check(BN_one(one));
1.1       otto     1238:                        ctx = BN_CTX_new();
                   1239:                        bn_checkp(ctx);
1.45      otto     1240:                        scale_number(one, r->scale + rscale);
1.43      otto     1241:
                   1242:                        if (BN_is_zero(r->number))
                   1243:                                warnx("divide by zero");
                   1244:                        else
                   1245:                                bn_check(BN_div(r->number, NULL, one,
                   1246:                                    r->number, ctx));
1.1       otto     1247:                        BN_free(one);
                   1248:                        BN_CTX_free(ctx);
1.45      otto     1249:                        r->scale = rscale;
1.29      otto     1250:                } else
1.45      otto     1251:                        normalize(r, rscale);
1.1       otto     1252:        }
                   1253:        push_number(r);
                   1254:        free_number(a);
                   1255:        free_number(p);
                   1256: }
                   1257:
                   1258: static bool
1.25      otto     1259: bsqrt_stop(const BIGNUM *x, const BIGNUM *y, u_int *onecount)
1.1       otto     1260: {
                   1261:        BIGNUM *r;
                   1262:        bool ret;
                   1263:
                   1264:        r = BN_new();
                   1265:        bn_checkp(r);
                   1266:        bn_check(BN_sub(r, x, y));
1.25      otto     1267:        if (BN_is_one(r))
                   1268:                (*onecount)++;
                   1269:        ret = BN_is_zero(r);
1.1       otto     1270:        BN_free(r);
1.25      otto     1271:        return ret || *onecount > 1;
1.1       otto     1272: }
                   1273:
                   1274: static void
                   1275: bsqrt(void)
                   1276: {
                   1277:        struct number   *n;
                   1278:        struct number   *r;
                   1279:        BIGNUM          *x, *y;
1.25      otto     1280:        u_int           scale, onecount;
1.1       otto     1281:        BN_CTX          *ctx;
                   1282:
1.25      otto     1283:        onecount = 0;
1.1       otto     1284:        n = pop_number();
                   1285:        if (n == NULL) {
                   1286:                return;
                   1287:        }
                   1288:        if (BN_is_zero(n->number)) {
                   1289:                r = new_number();
                   1290:                push_number(r);
1.44      otto     1291:        } else if (BN_is_negative(n->number))
1.1       otto     1292:                warnx("square root of negative number");
                   1293:        else {
                   1294:                scale = max(bmachine.scale, n->scale);
                   1295:                normalize(n, 2*scale);
                   1296:                x = BN_dup(n->number);
                   1297:                bn_checkp(x);
                   1298:                bn_check(BN_rshift(x, x, BN_num_bits(x)/2));
                   1299:                y = BN_new();
                   1300:                bn_checkp(y);
                   1301:                ctx = BN_CTX_new();
                   1302:                bn_checkp(ctx);
                   1303:                for (;;) {
                   1304:                        bn_checkp(BN_copy(y, x));
                   1305:                        bn_check(BN_div(x, NULL, n->number, x, ctx));
                   1306:                        bn_check(BN_add(x, x, y));
                   1307:                        bn_check(BN_rshift1(x, x));
1.25      otto     1308:                        if (bsqrt_stop(x, y, &onecount))
1.1       otto     1309:                                break;
                   1310:                }
                   1311:                r = bmalloc(sizeof(*r));
                   1312:                r->scale = scale;
                   1313:                r->number = y;
                   1314:                BN_free(x);
                   1315:                BN_CTX_free(ctx);
                   1316:                push_number(r);
                   1317:        }
                   1318:
                   1319:        free_number(n);
                   1320: }
                   1321:
                   1322: static void
1.16      otto     1323: not(void)
                   1324: {
                   1325:        struct number   *a;
                   1326:
                   1327:        a = pop_number();
                   1328:        if (a == NULL) {
                   1329:                return;
                   1330:        }
                   1331:        a->scale = 0;
                   1332:        bn_check(BN_set_word(a->number, BN_get_word(a->number) ? 0 : 1));
                   1333:        push_number(a);
                   1334: }
                   1335:
                   1336: static void
1.1       otto     1337: equal(void)
                   1338: {
                   1339:        compare(BCODE_EQUAL);
                   1340: }
                   1341:
                   1342: static void
1.16      otto     1343: equal_numbers(void)
                   1344: {
                   1345:        struct number *a, *b, *r;
                   1346:
                   1347:        a = pop_number();
                   1348:        if (a == NULL) {
                   1349:                return;
                   1350:        }
                   1351:        b = pop_number();
                   1352:        if (b == NULL) {
                   1353:                push_number(a);
                   1354:                return;
                   1355:        }
                   1356:        r = new_number();
                   1357:        bn_check(BN_set_word(r->number,
                   1358:            compare_numbers(BCODE_EQUAL, a, b) ? 1 : 0));
                   1359:        push_number(r);
                   1360: }
                   1361:
                   1362: static void
                   1363: less_numbers(void)
                   1364: {
                   1365:        struct number *a, *b, *r;
                   1366:
                   1367:        a = pop_number();
                   1368:        if (a == NULL) {
                   1369:                return;
                   1370:        }
                   1371:        b = pop_number();
                   1372:        if (b == NULL) {
                   1373:                push_number(a);
                   1374:                return;
                   1375:        }
                   1376:        r = new_number();
                   1377:        bn_check(BN_set_word(r->number,
                   1378:            compare_numbers(BCODE_LESS, a, b) ? 1 : 0));
                   1379:        push_number(r);
                   1380: }
                   1381:
                   1382: static void
                   1383: lesseq_numbers(void)
                   1384: {
                   1385:        struct number *a, *b, *r;
                   1386:
                   1387:        a = pop_number();
                   1388:        if (a == NULL) {
                   1389:                return;
                   1390:        }
                   1391:        b = pop_number();
                   1392:        if (b == NULL) {
                   1393:                push_number(a);
                   1394:                return;
                   1395:        }
                   1396:        r = new_number();
                   1397:        bn_check(BN_set_word(r->number,
                   1398:            compare_numbers(BCODE_NOT_GREATER, a, b) ? 1 : 0));
                   1399:        push_number(r);
                   1400: }
                   1401:
                   1402: static void
1.1       otto     1403: not_equal(void)
                   1404: {
                   1405:        compare(BCODE_NOT_EQUAL);
                   1406: }
                   1407:
                   1408: static void
                   1409: less(void)
                   1410: {
                   1411:        compare(BCODE_LESS);
                   1412: }
                   1413:
                   1414: static void
                   1415: not_compare(void)
                   1416: {
                   1417:        switch (readch()) {
                   1418:        case '<':
                   1419:                not_less();
                   1420:                break;
                   1421:        case '>':
                   1422:                not_greater();
                   1423:                break;
                   1424:        case '=':
                   1425:                not_equal();
                   1426:                break;
1.2       deraadt  1427:        default:
1.1       otto     1428:                unreadch();
1.48    ! otto     1429:                (void)fprintf(stderr, "! command is deprecated\n");
1.1       otto     1430:                break;
                   1431:        }
                   1432: }
                   1433:
                   1434: static void
                   1435: not_less(void)
                   1436: {
                   1437:        compare(BCODE_NOT_LESS);
                   1438: }
                   1439:
                   1440: static void
                   1441: greater(void)
                   1442: {
                   1443:        compare(BCODE_GREATER);
                   1444: }
                   1445:
                   1446: static void
                   1447: not_greater(void)
                   1448: {
                   1449:        compare(BCODE_NOT_GREATER);
                   1450: }
                   1451:
1.16      otto     1452: static bool
                   1453: compare_numbers(enum bcode_compare type, struct number *a, struct number *b)
                   1454: {
                   1455:        u_int   scale;
                   1456:        int     cmp;
                   1457:
                   1458:        scale = max(a->scale, b->scale);
                   1459:
                   1460:        if (scale > a->scale)
                   1461:                normalize(a, scale);
1.30      otto     1462:        else if (scale > b->scale)
1.16      otto     1463:                normalize(b, scale);
                   1464:
                   1465:        cmp = BN_cmp(a->number, b->number);
                   1466:
                   1467:        free_number(a);
                   1468:        free_number(b);
                   1469:
                   1470:        switch (type) {
                   1471:        case BCODE_EQUAL:
                   1472:                return cmp == 0;
                   1473:        case BCODE_NOT_EQUAL:
                   1474:                return cmp != 0;
                   1475:        case BCODE_LESS:
                   1476:                return cmp < 0;
                   1477:        case BCODE_NOT_LESS:
                   1478:                return cmp >= 0;
                   1479:        case BCODE_GREATER:
                   1480:                return cmp > 0;
                   1481:        case BCODE_NOT_GREATER:
                   1482:                return cmp <= 0;
                   1483:        }
                   1484:        return false;
                   1485: }
                   1486:
1.1       otto     1487: static void
                   1488: compare(enum bcode_compare type)
                   1489: {
1.32      otto     1490:        int             idx, elseidx;
1.1       otto     1491:        struct number   *a, *b;
                   1492:        bool            ok;
                   1493:        struct value    *v;
                   1494:
1.32      otto     1495:        elseidx = NO_ELSE;
                   1496:        idx = readreg();
1.11      otto     1497:        if (readch() == 'e')
1.32      otto     1498:                elseidx = readreg();
1.11      otto     1499:        else
                   1500:                unreadch();
1.1       otto     1501:
                   1502:        a = pop_number();
1.11      otto     1503:        if (a == NULL)
1.1       otto     1504:                return;
                   1505:        b = pop_number();
                   1506:        if (b == NULL) {
                   1507:                push_number(a);
                   1508:                return;
                   1509:        }
                   1510:
1.16      otto     1511:        ok = compare_numbers(type, a, b);
1.1       otto     1512:
1.32      otto     1513:        if (!ok && elseidx != NO_ELSE)
                   1514:                idx = elseidx;
1.11      otto     1515:
1.32      otto     1516:        if (idx >= 0 && (ok || (!ok && elseidx != NO_ELSE))) {
                   1517:                v = stack_tos(&bmachine.reg[idx]);
1.1       otto     1518:                if (v == NULL)
1.32      otto     1519:                        warnx("register '%c' (0%o) is empty", idx, idx);
1.1       otto     1520:                else {
                   1521:                        switch(v->type) {
                   1522:                        case BCODE_NONE:
1.32      otto     1523:                                warnx("register '%c' (0%o) is empty", idx, idx);
1.1       otto     1524:                                break;
                   1525:                        case BCODE_NUMBER:
                   1526:                                warn("eval called with non-string argument");
                   1527:                                break;
                   1528:                        case BCODE_STRING:
                   1529:                                eval_string(bstrdup(v->u.string));
                   1530:                                break;
                   1531:                        }
                   1532:                }
                   1533:        }
                   1534: }
                   1535:
                   1536:
                   1537: static void
                   1538: nop(void)
                   1539: {
                   1540: }
                   1541:
1.2       deraadt  1542: static void
1.1       otto     1543: quit(void)
                   1544: {
1.2       deraadt  1545:        if (bmachine.readsp < 2)
1.1       otto     1546:                exit(0);
                   1547:        src_free();
                   1548:        bmachine.readsp--;
                   1549:        src_free();
                   1550:        bmachine.readsp--;
                   1551: }
                   1552:
                   1553: static void
                   1554: quitN(void)
                   1555: {
                   1556:        struct number   *n;
                   1557:        u_long          i;
                   1558:
                   1559:        n = pop_number();
                   1560:        if (n == NULL)
                   1561:                return;
                   1562:        i = get_ulong(n);
1.37      otto     1563:        free_number(n);
1.1       otto     1564:        if (i == BN_MASK2 || i == 0)
                   1565:                warnx("Q command requires a number >= 1");
                   1566:        else if (bmachine.readsp < i)
                   1567:                warnx("Q command argument exceeded string execution depth");
                   1568:        else {
                   1569:                while (i-- > 0) {
                   1570:                        src_free();
                   1571:                        bmachine.readsp--;
                   1572:                }
                   1573:        }
                   1574: }
                   1575:
                   1576: static void
1.9       otto     1577: skipN(void)
                   1578: {
                   1579:        struct number   *n;
                   1580:        u_long          i;
                   1581:
                   1582:        n = pop_number();
                   1583:        if (n == NULL)
                   1584:                return;
                   1585:        i = get_ulong(n);
                   1586:        if (i == BN_MASK2)
                   1587:                warnx("J command requires a number >= 0");
                   1588:        else if (i > 0 && bmachine.readsp < i)
                   1589:                warnx("J command argument exceeded string execution depth");
                   1590:        else {
                   1591:                while (i-- > 0) {
                   1592:                        src_free();
                   1593:                        bmachine.readsp--;
                   1594:                }
                   1595:                skip_until_mark();
                   1596:        }
                   1597: }
                   1598:
                   1599: static void
                   1600: skip_until_mark(void)
                   1601: {
                   1602:        int ch;
                   1603:
                   1604:        for (;;) {
                   1605:                ch = readch();
                   1606:                switch (ch) {
                   1607:                case 'M':
                   1608:                        return;
                   1609:                case EOF:
                   1610:                        errx(1, "mark not found");
                   1611:                        return;
                   1612:                case 'l':
                   1613:                case 'L':
                   1614:                case 's':
                   1615:                case 'S':
                   1616:                case ':':
                   1617:                case ';':
                   1618:                case '<':
                   1619:                case '>':
                   1620:                case '=':
1.31      otto     1621:                        (void)readreg();
1.12      otto     1622:                        if (readch() == 'e')
1.31      otto     1623:                                (void)readreg();
1.12      otto     1624:                        else
                   1625:                                unreadch();
1.9       otto     1626:                        break;
                   1627:                case '[':
                   1628:                        free(read_string(&bmachine.readstack[bmachine.readsp]));
                   1629:                        break;
                   1630:                case '!':
                   1631:                        switch (ch = readch()) {
                   1632:                                case '<':
                   1633:                                case '>':
                   1634:                                case '=':
1.31      otto     1635:                                        (void)readreg();
1.12      otto     1636:                                        if (readch() == 'e')
1.31      otto     1637:                                                (void)readreg();
1.12      otto     1638:                                        else
                   1639:                                                unreadch();
1.9       otto     1640:                                        break;
                   1641:                                default:
                   1642:                                        free(readline());
                   1643:                                        break;
                   1644:                        }
                   1645:                        break;
                   1646:                default:
                   1647:                        break;
                   1648:                }
                   1649:        }
                   1650: }
                   1651:
                   1652: static void
1.1       otto     1653: parse_number(void)
                   1654: {
                   1655:        unreadch();
                   1656:        push_number(readnumber(&bmachine.readstack[bmachine.readsp],
                   1657:            bmachine.ibase));
                   1658: }
                   1659:
                   1660: static void
                   1661: unknown(void)
                   1662: {
                   1663:        int ch = bmachine.readstack[bmachine.readsp].lastchar;
                   1664:        warnx("%c (0%o) is unimplemented", ch, ch);
                   1665: }
                   1666:
                   1667: static void
                   1668: eval_string(char *p)
                   1669: {
                   1670:        int ch;
                   1671:
                   1672:        if (bmachine.readsp > 0) {
                   1673:                /* Check for tail call. Do not recurse in that case. */
                   1674:                ch = readch();
                   1675:                if (ch == EOF) {
                   1676:                        src_free();
                   1677:                        src_setstring(&bmachine.readstack[bmachine.readsp], p);
                   1678:                        return;
                   1679:                } else
                   1680:                        unreadch();
                   1681:        }
1.27      otto     1682:        if (bmachine.readsp == bmachine.readstack_sz - 1) {
                   1683:                size_t newsz = bmachine.readstack_sz * 2;
                   1684:                struct source *stack;
1.46      doug     1685:                stack = reallocarray(bmachine.readstack, newsz,
1.27      otto     1686:                    sizeof(struct source));
                   1687:                if (stack == NULL)
                   1688:                        err(1, "recursion too deep");
                   1689:                bmachine.readstack_sz = newsz;
                   1690:                bmachine.readstack = stack;
                   1691:        }
1.1       otto     1692:        src_setstring(&bmachine.readstack[++bmachine.readsp], p);
                   1693: }
                   1694:
                   1695: static void
                   1696: eval_line(void)
                   1697: {
                   1698:        /* Always read from stdin */
                   1699:        struct source   in;
                   1700:        char            *p;
                   1701:
1.38      otto     1702:        clearerr(stdin);
1.1       otto     1703:        src_setstream(&in, stdin);
                   1704:        p = (*in.vtable->readline)(&in);
                   1705:        eval_string(p);
                   1706: }
                   1707:
                   1708: static void
                   1709: eval_tos(void)
                   1710: {
                   1711:        char *p;
                   1712:
                   1713:        p = pop_string();
                   1714:        if (p == NULL)
                   1715:                return;
                   1716:        eval_string(p);
                   1717: }
                   1718:
                   1719: void
                   1720: eval(void)
                   1721: {
                   1722:        int     ch;
                   1723:
                   1724:        for (;;) {
                   1725:                ch = readch();
                   1726:                if (ch == EOF) {
                   1727:                        if (bmachine.readsp == 0)
1.24      otto     1728:                                return;
1.1       otto     1729:                        src_free();
                   1730:                        bmachine.readsp--;
                   1731:                        continue;
1.20      otto     1732:                }
                   1733:                if (bmachine.interrupted) {
                   1734:                        if (bmachine.readsp > 0) {
                   1735:                                src_free();
                   1736:                                bmachine.readsp--;
                   1737:                                continue;
1.22      otto     1738:                        } else
1.20      otto     1739:                                bmachine.interrupted = false;
1.1       otto     1740:                }
1.9       otto     1741: #ifdef DEBUGGING
1.31      otto     1742:                (void)fprintf(stderr, "# %c\n", ch);
1.9       otto     1743:                stack_print(stderr, &bmachine.stack, "* ",
                   1744:                    bmachine.obase);
1.36      otto     1745:                (void)fprintf(stderr, "%zd =>\n", bmachine.readsp);
1.9       otto     1746: #endif
1.1       otto     1747:
                   1748:                if (0 <= ch && ch < UCHAR_MAX)
                   1749:                        (*jump_table[ch])();
                   1750:                else
                   1751:                        warnx("internal error: opcode %d", ch);
                   1752:
1.9       otto     1753: #ifdef DEBUGGING
                   1754:                stack_print(stderr, &bmachine.stack, "* ",
                   1755:                    bmachine.obase);
1.36      otto     1756:                (void)fprintf(stderr, "%zd ==\n", bmachine.readsp);
1.9       otto     1757: #endif
1.1       otto     1758:        }
                   1759: }