diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-10-18 07:49:01 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-10-18 07:49:01 +0000 |
commit | 3c5eb4e0f06ac4a22a84ba2345ab0b0a2e6b5fde (patch) | |
tree | 511f6af60e44c21f76f8ce439a4c080812ac45fe | |
parent | bc24360311a5e2cbac2179f14bcc0d29307b65ec (diff) |
Implement command line evaluation of expressions by implementing the -e option.
ok weingart@
-rw-r--r-- | usr.bin/dc/bcode.c | 6 | ||||
-rw-r--r-- | usr.bin/dc/dc.1 | 15 | ||||
-rw-r--r-- | usr.bin/dc/dc.c | 39 |
3 files changed, 46 insertions, 14 deletions
diff --git a/usr.bin/dc/bcode.c b/usr.bin/dc/bcode.c index d1bcc7fd19c..df7dba17e6f 100644 --- a/usr.bin/dc/bcode.c +++ b/usr.bin/dc/bcode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcode.c,v 1.23 2004/09/14 22:22:46 deraadt Exp $ */ +/* $OpenBSD: bcode.c,v 1.24 2004/10/18 07:49:00 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.23 2004/09/14 22:22:46 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: bcode.c,v 1.24 2004/10/18 07:49:00 otto Exp $"; #endif /* not lint */ #include <ssl/ssl.h> @@ -1695,7 +1695,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 476e4830ddc..85fdf86890e 100644 --- a/usr.bin/dc/dc.1 +++ b/usr.bin/dc/dc.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: dc.1,v 1.18 2003/12/01 09:13:55 otto Exp $ +.\" $OpenBSD: dc.1,v 1.19 2004/10/18 07:49:00 otto Exp $ .\" .\" Copyright (C) Caldera International Inc. 2001-2002. .\" All rights reserved. @@ -42,6 +42,7 @@ .Sh SYNOPSIS .Nm .Op Fl x +.Op Fl e Ar expression .Op Ar file .Sh DESCRIPTION .Nm @@ -63,6 +64,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 e744836f6f2..094c3f6ca1d 100644 --- a/usr.bin/dc/dc.c +++ b/usr.bin/dc/dc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dc.c,v 1.5 2004/01/13 08:17:41 otto Exp $ */ +/* $OpenBSD: dc.c,v 1.6 2004/10/18 07:49:00 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -17,11 +17,12 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: dc.c,v 1.5 2004/01/13 08:17:41 otto Exp $"; +static const char rcsid[] = "$OpenBSD: dc.c,v 1.6 2004/10/18 07:49:00 otto Exp $"; #endif /* not lint */ #include <err.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include "extern.h" @@ -33,7 +34,7 @@ extern char *__progname; static __dead void usage(void) { - fprintf(stderr, "usage: %s [-x] [file]\n", __progname); + fprintf(stderr, "usage: %s [-x] [-e expr] [file]\n", __progname); exit(1); } @@ -44,10 +45,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; @@ -66,7 +76,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]); @@ -74,14 +92,15 @@ 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(); - return 0; + return (0); } |