diff options
author | 2003-11-06 19:48:13 +0000 | |
---|---|---|
committer | 2003-11-06 19:48:13 +0000 | |
commit | 3c65cd632c36428ef6ed5fb823652b3acd8b1dd9 (patch) | |
tree | 585d68f7eaec86d2d59d1b8f5ef70c12846edbca | |
parent | set env PKG_PREFIX for compatibility with old tools. (diff) | |
download | wireguard-openbsd-3c65cd632c36428ef6ed5fb823652b3acd8b1dd9.tar.xz wireguard-openbsd-3c65cd632c36428ef6ed5fb823652b3acd8b1dd9.zip |
Implement non-portable extension # (comment), n (print w/o newline),
and a (byte to char).
-rw-r--r-- | usr.bin/dc/bcode.c | 148 |
1 files changed, 99 insertions, 49 deletions
diff --git a/usr.bin/dc/bcode.c b/usr.bin/dc/bcode.c index 630a5ee4c04..72d6cc8efc5 100644 --- a/usr.bin/dc/bcode.c +++ b/usr.bin/dc/bcode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcode.c,v 1.13 2003/11/04 08:10:06 otto Exp $ */ +/* $OpenBSD: bcode.c,v 1.14 2003/11/06 19:48:13 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -17,7 +17,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: bcode.c,v 1.13 2003/11/04 08:10:06 otto Exp $"; +static const char rcsid[] = "$OpenBSD: bcode.c,v 1.14 2003/11/06 19:48:13 otto Exp $"; #endif /* not lint */ #include <ssl/ssl.h> @@ -66,7 +66,8 @@ static __inline struct number *pop_number(void); static __inline char *pop_string(void); static __inline void clear_stack(void); static __inline void print_tos(void); -static __inline void pop_print(void); +static void pop_print(void); +static void pop_printn(void); static __inline void print_stack(); static __inline void dup(void); static void swap(void); @@ -81,8 +82,9 @@ static void stackdepth(void); static void push_scale(void); static u_int count_digits(const struct number *); static void num_digits(void); - +static void to_ascii(void); static void push_line(void); +static void comment(void); static void bexec(char *); static void badd(void); static void bsub(void); @@ -129,6 +131,15 @@ struct jump_entry { static opcode_function jump_table[UCHAR_MAX]; static const struct jump_entry jump_table_data[] = { + { ' ', nop }, + { '!', not_compare }, + { '#', comment }, + { '%', bmod }, + { '*', bmul }, + { '+', badd }, + { '-', bsub }, + { '.', parse_number }, + { '/', bdiv }, { '0', parse_number }, { '1', parse_number }, { '2', parse_number }, @@ -139,59 +150,55 @@ static const struct jump_entry jump_table_data[] = { { '7', parse_number }, { '8', parse_number }, { '9', parse_number }, + { ':', store_array }, + { ';', load_array }, + { '<', less }, + { '=', equal }, + { '>', greater }, + { '?', eval_line }, { 'A', parse_number }, { 'B', parse_number }, { 'C', parse_number }, { 'D', parse_number }, { 'E', parse_number }, { 'F', parse_number }, - { '_', parse_number }, - { '.', parse_number }, - { '+', badd }, - { '-', bsub }, - { '*', bmul }, - { '/', bdiv }, - { '%', bmod }, - { '~', bdivmod }, - { '^', bexp }, - { 's', store }, - { 'S', store_stack }, - { 'l', load }, + { 'I', get_ibase }, + { 'J', skipN }, + { 'K', get_scale }, { 'L', load_stack }, + { 'M', nop }, + { 'O', get_obase }, { 'd', dup }, { 'r', swap }, { 'p', print_tos }, { 'P', pop_print }, - { 'f', print_stack }, - { 'x', eval_tos }, + { 'Q', quitN }, + { 'S', store_stack }, { 'X', push_scale }, + { 'Z', num_digits }, { '[', push_line }, - { 'q', quit }, - { 'Q', quitN }, - { 'J', skipN }, - { 'M', nop }, - { '<', less }, - { '>', greater }, - { '=', equal }, - { '!', not_compare }, - { 'v', bsqrt }, + { '\f', nop }, + { '\n', nop }, + { '\r', nop }, + { '\t', nop }, + { '^', bexp }, + { '_', parse_number }, + { 'a', to_ascii }, { 'c', clear_stack }, + { 'd', dup }, + { 'f', print_stack }, { 'i', set_ibase }, - { 'I', get_ibase }, - { 'o', set_obase }, - { 'O', get_obase }, { 'k', set_scale }, - { 'K', get_scale }, + { 'l', load }, + { 'n', pop_printn }, + { 'o', set_obase }, + { 'p', print_tos }, + { 'q', quit }, + { 's', store }, + { 'v', bsqrt }, + { 'x', eval_tos }, { 'z', stackdepth }, - { 'Z', num_digits }, - { '?', eval_line }, - { ';', load_array }, - { ':', store_array }, - { ' ', nop }, - { '\t', nop }, - { '\n', nop }, - { '\f', nop }, - { '\r', nop } + { '~', bdivmod } }; #define JUMP_TABLE_DATA_SIZE \ @@ -452,10 +459,11 @@ print_tos(void) warnx("stack empty"); } -static __inline void +static void pop_print(void) { struct value *value = pop(); + if (value != NULL) { switch (value->type) { case BCODE_NONE: @@ -474,6 +482,17 @@ pop_print(void) } } +static void +pop_printn(void) +{ + struct value *value = pop(); + + if (value != NULL) { + print_value(stdout, value, "", bmachine.obase); + stack_free_value(value); + } +} + static __inline void dup(void) { @@ -639,30 +658,55 @@ num_digits(void) { struct value *value; u_int digits; - struct number *n; + struct number *n = NULL; value = pop(); if (value != NULL) { switch (value->type) { case BCODE_NONE: - break; + return; case BCODE_NUMBER: digits = count_digits(value->u.num); n = new_number(); bn_check(BN_set_word(n->number, digits)); - /* free first, then reassign */ - BN_free(value->u.num->number); - push_number(n); break; case BCODE_STRING: digits = strlen(value->u.string); n = new_number(); bn_check(BN_set_word(n->number, digits)); - /* free first, then reassign */ - free(value->u.string); - push_number(n); break; } + stack_free_value(value); + push_number(n); + } +} + +static void +to_ascii(void) +{ + char str[2]; + struct value *value; + struct number *n; + + value = pop(); + if (value != NULL) { + str[1] = '\0'; + switch (value->type) { + case BCODE_NONE: + return; + case BCODE_NUMBER: + n = value->u.num; + normalize(n, 0); + if (BN_num_bits(n->number) > 8) + bn_check(BN_mask_bits(n->number, 8)); + str[0] = BN_get_word(n->number); + break; + case BCODE_STRING: + str[0] = value->u.string[0]; + break; + } + stack_free_value(value); + push_string(bstrdup(str)); } } @@ -819,6 +863,12 @@ push_line(void) } static void +comment(void) +{ + free(readline()); +} + +static void bexec(char *line) { system(line); |