From abcef8f016d28dc537c03a26b8968265582f18cf Mon Sep 17 00:00:00 2001 From: Sascha Wildner Date: Thu, 21 Apr 2005 18:50:50 +0000 Subject: [PATCH] Sync with OpenBSD. bcode.c - obsd rev. 1.29 dc.1 - obsd rev. 1.19 dc.c - obsd rev. 1.6 inout.c - obsd rev. 1.12 stack.c - obsd rev. 1.7 --- usr.bin/dc/bcode.c | 60 ++++++++++++++++++++++++++++++---------------- usr.bin/dc/dc.1 | 19 ++++++++++++--- usr.bin/dc/dc.c | 41 ++++++++++++++++++++++--------- usr.bin/dc/inout.c | 32 ++++++++++++++++++------- usr.bin/dc/stack.c | 10 ++++---- 5 files changed, 113 insertions(+), 49 deletions(-) diff --git a/usr.bin/dc/bcode.c b/usr.bin/dc/bcode.c index 0f927ff11b..87dc9a512b 100644 --- a/usr.bin/dc/bcode.c +++ b/usr.bin/dc/bcode.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: bcode.c,v 1.22 2004/02/11 20:44:31 otto Exp $ - * $DragonFly: src/usr.bin/dc/bcode.c,v 1.1 2004/09/20 04:20:39 dillon Exp $ + * $OpenBSD: bcode.c,v 1.29 2005/04/02 18:05:04 otto Exp $ + * $DragonFly: src/usr.bin/dc/bcode.c,v 1.2 2005/04/21 18:50:50 swildner Exp $ */ /* @@ -34,7 +34,7 @@ BIGNUM zero; /* #define DEBUGGING */ #define MAX_ARRAY_INDEX 2048 -#define RECURSION_STACK_SIZE 100 +#define READSTACK_SIZE 8 #define NO_ELSE -2 /* -1 is EOF */ #define REG_ARRAY_SIZE_SMALL (UCHAR_MAX + 1) @@ -45,12 +45,13 @@ struct bmachine { u_int scale; u_int obase; u_int ibase; - int readsp; + size_t readsp; bool extended_regs; size_t reg_array_size; struct stack *reg; - volatile bool interrupted; - struct source readstack[RECURSION_STACK_SIZE]; + volatile sig_atomic_t interrupted; + struct source *readstack; + size_t readstack_sz; }; static struct bmachine bmachine; @@ -100,7 +101,7 @@ static void bdiv(void); static void bmod(void); static void bdivmod(void); static void bexp(void); -static bool bsqrt_stop(const BIGNUM *, const BIGNUM *); +static bool bsqrt_stop(const BIGNUM *, const BIGNUM *, u_int *); static void bsqrt(void); static void not(void); static void equal_numbers(void); @@ -222,7 +223,7 @@ static const struct jump_entry jump_table_data[] = { (sizeof(jump_table_data)/sizeof(jump_table_data[0])) static void -sighandler(int ignored) +sighandler(int ignored __unused) { bmachine.interrupted = true; } @@ -251,6 +252,11 @@ init_bmachine(bool extended_registers) for (i = 0; i < bmachine.reg_array_size; i++) stack_init(&bmachine.reg[i]); + bmachine.readstack_sz = READSTACK_SIZE; + bmachine.readstack = malloc(sizeof(struct source) * + bmachine.readstack_sz); + if (bmachine.readstack == NULL) + err(1, NULL); bmachine.obase = bmachine.ibase = 10; BN_init(&zero); bn_check(BN_zero(&zero)); @@ -299,7 +305,7 @@ src_free(void) #ifdef DEBUGGING void -pn(const char * str, const struct number *n) +pn(const char *str, const struct number *n) { char *p = BN_bn2dec(n->number); if (p == NULL) @@ -310,7 +316,7 @@ pn(const char * str, const struct number *n) } void -pbn(const char * str, const BIGNUM *n) +pbn(const char *str, const BIGNUM *n) { char *p = BN_bn2dec(n); if (p == NULL) @@ -1223,12 +1229,13 @@ bexp(void) BN_one(one); ctx = BN_CTX_new(); bn_checkp(ctx); - r->scale = scale; - scale_number(one, r->scale); + scale_number(one, r->scale + scale); + normalize(r, scale); bn_check(BN_div(r->number, NULL, one, r->number, ctx)); BN_free(one); BN_CTX_free(ctx); - } + } else + normalize(r, scale); } push_number(r); free_number(a); @@ -1236,7 +1243,7 @@ bexp(void) } static bool -bsqrt_stop(const BIGNUM *x, const BIGNUM *y) +bsqrt_stop(const BIGNUM *x, const BIGNUM *y, u_int *onecount) { BIGNUM *r; bool ret; @@ -1244,9 +1251,11 @@ bsqrt_stop(const BIGNUM *x, const BIGNUM *y) r = BN_new(); bn_checkp(r); bn_check(BN_sub(r, x, y)); - ret = BN_is_one(r) || BN_is_zero(r); + if (BN_is_one(r)) + (*onecount)++; + ret = BN_is_zero(r); BN_free(r); - return ret; + return ret || *onecount > 1; } static void @@ -1255,9 +1264,10 @@ bsqrt(void) struct number *n; struct number *r; BIGNUM *x, *y; - u_int scale; + u_int scale, onecount; BN_CTX *ctx; + onecount = 0; n = pop_number(); if (n == NULL) { return; @@ -1282,7 +1292,7 @@ bsqrt(void) bn_check(BN_div(x, NULL, n->number, x, ctx)); bn_check(BN_add(x, x, y)); bn_check(BN_rshift1(x, x)); - if (bsqrt_stop(x, y)) + if (bsqrt_stop(x, y, &onecount)) break; } r = bmalloc(sizeof(*r)); @@ -1656,8 +1666,16 @@ eval_string(char *p) } else unreadch(); } - if (bmachine.readsp == RECURSION_STACK_SIZE-1) - errx(1, "recursion too deep"); + if (bmachine.readsp == bmachine.readstack_sz - 1) { + size_t newsz = bmachine.readstack_sz * 2; + struct source *stack; + stack = realloc(bmachine.readstack, newsz * + sizeof(struct source)); + if (stack == NULL) + err(1, "recursion too deep"); + bmachine.readstack_sz = newsz; + bmachine.readstack = stack; + } src_setstring(&bmachine.readstack[++bmachine.readsp], p); } @@ -1693,7 +1711,7 @@ eval(void) ch = readch(); if (ch == EOF) { if (bmachine.readsp == 0) - exit(0); + return; src_free(); bmachine.readsp--; continue; diff --git a/usr.bin/dc/dc.1 b/usr.bin/dc/dc.1 index d9b6491b90..353943d997 100644 --- a/usr.bin/dc/dc.1 +++ b/usr.bin/dc/dc.1 @@ -1,5 +1,5 @@ -.\" $OpenBSD: dc.1,v 1.18 2003/12/01 09:13:55 otto Exp $ -.\" $DragonFly: src/usr.bin/dc/dc.1,v 1.1 2004/09/20 04:20:39 dillon Exp $ +.\" $OpenBSD: dc.1,v 1.19 2004/10/18 07:49:00 otto Exp $ +.\" $DragonFly: src/usr.bin/dc/dc.1,v 1.2 2005/04/21 18:50:50 swildner Exp $ .\" .\" Copyright (C) Caldera International Inc. 2001-2002. .\" All rights reserved. @@ -35,7 +35,7 @@ .\" .\" @(#)dc.1 8.1 (Berkeley) 6/6/93 .\" -.Dd June 6, 1993 +.Dd October 10, 2004 .Dt DC 1 .Sh NAME .Nm dc @@ -43,6 +43,7 @@ .Sh SYNOPSIS .Nm .Op Fl x +.Op Fl e Ar expression .Op Ar file .Sh DESCRIPTION .Nm @@ -64,6 +65,18 @@ which implements functions and reasonable control structures for programs. The options are as follows: .Bl -tag -width Ds +.It Fl e Ar expression +Evaluate +.Ar expression . +If multiple +.Fl e +options are specified, they will be processed in the order given. +If no +.Ar file +argument is given, execution will stop after processing the expressions +given on the command line, +otherwise processing will continue with the contents of +.Ar file . .It Fl x Enable extended register mode. This mode is used by diff --git a/usr.bin/dc/dc.c b/usr.bin/dc/dc.c index 65f29cc063..4c47b5b64b 100644 --- a/usr.bin/dc/dc.c +++ b/usr.bin/dc/dc.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: dc.c,v 1.5 2004/01/13 08:17:41 otto Exp $ - * $DragonFly: src/usr.bin/dc/dc.c,v 1.1 2004/09/20 04:20:39 dillon Exp $ + * $OpenBSD: dc.c,v 1.6 2004/10/18 07:49:00 otto Exp $ + * $DragonFly: src/usr.bin/dc/dc.c,v 1.2 2005/04/21 18:50:50 swildner Exp $ */ /* @@ -21,18 +21,19 @@ #include #include +#include #include #include "extern.h" -static void usage(void); +static __dead2 void usage(void); extern char *__progname; -static void +static __dead2 void usage(void) { - fprintf(stderr, "usage: %s [-x] [file]\n", __progname); + fprintf(stderr, "usage: %s [-x] [-e expr] [file]\n", __progname); exit(1); } @@ -43,10 +44,19 @@ main(int argc, char *argv[]) bool extended_regs = false; FILE *file; struct source src; + char *buf, *p; + if ((buf = strdup("")) == NULL) + err(1, NULL); /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */ - while ((ch = getopt(argc, argv, "x-")) != -1) { + while ((ch = getopt(argc, argv, "e:x-")) != -1) { switch (ch) { + case 'e': + p = buf; + if (asprintf(&buf, "%s %s", buf, optarg) == -1) + err(1, NULL); + free(p); + break; case 'x': extended_regs = true; break; @@ -65,7 +75,15 @@ main(int argc, char *argv[]) if (argc > 1) usage(); - else if (argc == 1) { + if (buf[0] != '\0') { + src_setstring(&src, buf); + reset_bmachine(&src); + eval(); + free(buf); + if (argc == 0) + return (0); + } + if (argc == 1) { file = fopen(argv[0], "r"); if (file == NULL) err(1, "cannot open file %s", argv[0]); @@ -73,11 +91,12 @@ main(int argc, char *argv[]) reset_bmachine(&src); eval(); fclose(file); + /* + * BSD and Solaris dc(1) continue with stdin after processing + * the file given as the argument. We follow GNU dc(1). + */ + return (0); } - /* - * BSD dc and Solaris dc continue with stdin after processing - * the file given as the argument. - */ src_setstream(&src, stdin); reset_bmachine(&src); eval(); diff --git a/usr.bin/dc/inout.c b/usr.bin/dc/inout.c index abd30e32eb..657efea83a 100644 --- a/usr.bin/dc/inout.c +++ b/usr.bin/dc/inout.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: inout.c,v 1.8 2003/11/14 20:18:47 otto Exp $ - * $DragonFly: src/usr.bin/dc/inout.c,v 1.1 2004/09/20 04:20:39 dillon Exp $ + * $OpenBSD: inout.c,v 1.12 2005/03/29 10:53:54 otto Exp $ + * $DragonFly: src/usr.bin/dc/inout.c,v 1.2 2005/04/21 18:50:50 swildner Exp $ */ /* @@ -28,8 +28,8 @@ #define MAX_CHARS_PER_LINE 68 -static int charCount; - +static int lastchar; +static int charcount; static int src_getcharstream(struct source *); static int src_ungetcharstream(struct source *); @@ -39,6 +39,7 @@ static int src_getcharstring(struct source *); static int src_ungetcharstring(struct source *); static char *src_getlinestring(struct source *); static void src_freestring(struct source *); +static void flushwrap(FILE *); static void putcharwrap(FILE *, int); static void printwrap(FILE *, const char *); static char *get_digit(u_long, int, u_int); @@ -150,14 +151,25 @@ src_freestring(struct source *src) free(src->u.string.buf); } +static void +flushwrap(FILE *f) +{ + if (lastchar != -1) + putc(lastchar, f); +} + static void putcharwrap(FILE *f, int ch) { - putc(ch, f); - if (++charCount > MAX_CHARS_PER_LINE) { - charCount = 0; + if (charcount >= MAX_CHARS_PER_LINE) { + charcount = 0; fputs("\\\n", f); } + if (lastchar != -1) { + charcount++; + putc(lastchar, f); + } + lastchar = ch; } static void @@ -166,7 +178,7 @@ printwrap(FILE *f, const char *p) char buf[12]; char *q = buf; - snprintf(buf, sizeof(buf), "%s", p); + strlcpy(buf, p, sizeof(buf)); while (*q) putcharwrap(f, *q++); } @@ -282,6 +294,8 @@ printnumber(FILE *f, const struct number *b, u_int base) struct stack stack; char *p; + charcount = 0; + lastchar = -1; if (BN_is_zero(b->number)) putcharwrap(f, '0'); @@ -304,7 +318,6 @@ printnumber(FILE *f, const struct number *b, u_int base) i++; } sz = i; - charCount = 0; if (BN_cmp(b->number, &zero) < 0) putcharwrap(f, '-'); for (i = 0; i < sz; i++) { @@ -352,6 +365,7 @@ printnumber(FILE *f, const struct number *b, u_int base) BN_free(&mult); BN_free(&stop); } + flushwrap(f); free_number(int_part); free_number(fract_part); } diff --git a/usr.bin/dc/stack.c b/usr.bin/dc/stack.c index 705f77392c..a47e56b3ec 100644 --- a/usr.bin/dc/stack.c +++ b/usr.bin/dc/stack.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: stack.c,v 1.6 2003/11/26 19:30:52 otto Exp $ - * $DragonFly: src/usr.bin/dc/stack.c,v 1.1 2004/09/20 04:20:39 dillon Exp $ + * $OpenBSD: stack.c,v 1.7 2005/03/28 17:39:20 deraadt Exp $ + * $DragonFly: src/usr.bin/dc/stack.c,v 1.2 2005/04/21 18:50:50 swildner Exp $ */ /* @@ -43,7 +43,7 @@ stack_init(struct stack *stack) } static __inline bool -stack_empty(const struct stack * stack) +stack_empty(const struct stack *stack) { bool empty = stack->sp == -1; if (empty) @@ -96,7 +96,7 @@ stack_dup_value(const struct value *a, struct value *copy) } int -stack_size(const struct stack * stack) +stack_size(const struct stack *stack) { return stack->sp + 1; } @@ -196,7 +196,7 @@ stack_set_tos(struct stack *stack, struct value *v) stack_free_value(&stack->stack[stack->sp]); stack->stack[stack->sp] = *v; stack->stack[stack->sp].array = v->array == NULL ? - NULL : array_dup(v->array); + NULL : array_dup(v->array); } } -- 2.41.0