From 9ba0aa30f1828f0df9a6a08b39d6c0c8f83d5f29 Mon Sep 17 00:00:00 2001 From: eadler Date: Mon, 14 Apr 2014 20:51:04 +0000 Subject: [PATCH] units(1): Add v option: verbose For increased compatibility with GNU units: support a -v option which produces more verbose output when spitting out the answer. GNU -v does additional work in the version, information, and check output which we do not (yet?) replicate. --- usr.bin/units/units.1 | 4 +++- usr.bin/units/units.c | 49 +++++++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1 index 4b485f3167c8..83f2d9ed094f 100644 --- a/usr.bin/units/units.1 +++ b/usr.bin/units/units.1 @@ -8,7 +8,7 @@ .Sh SYNOPSIS .Nm .Op Fl f Ar filename -.Op Fl qUV +.Op Fl qvUV .Op Ar from-unit to-unit .Sh OPTIONS The following options are available: @@ -31,6 +31,8 @@ line. The program will not print prompts. It will print out the result of the single specified conversion. +.It Fl v +Print the units in the conversion output. Be more verbose in general. .El .Sh DESCRIPTION The diff --git a/usr.bin/units/units.c b/usr.bin/units/units.c index c0659411fd31..b1741b8f697a 100644 --- a/usr.bin/units/units.c +++ b/usr.bin/units/units.c @@ -76,6 +76,10 @@ static char NULLUNIT[] = ""; static int unitcount; static int prefixcount; +static bool verbose = false; +static const char * havestr; +static const char * wantstr; + char *dupstr(const char *str); void readunits(const char *userfile); @@ -254,7 +258,7 @@ showunit(struct unittype * theunit) int printedslash; int counter = 1; - printf("\t%.8g", theunit->factor); + printf("%.8g", theunit->factor); if (theunit->offset) printf("&%.8g", theunit->offset); for (ptr = theunit->numerator; *ptr; ptr++) { @@ -289,7 +293,7 @@ showunit(struct unittype * theunit) counter = 1; } } - if (counter > 1) + if ( counter > 1) printf("%s%d", powerstring, counter); printf("\n"); } @@ -645,32 +649,50 @@ completereduce(struct unittype * unit) return 0; } - void showanswer(struct unittype * have, struct unittype * want) { + double ans; + if (compareunits(have, want)) { printf("conformability error\n"); + if (verbose) + printf("\t%s = ", havestr); + else + printf("\t"); showunit(have); + if (verbose) + printf("\t%s = ", wantstr); + else + printf("\t"); showunit(want); } else if (have->offset != want->offset) { if (want->quantity) printf("WARNING: conversion of non-proportional quantities.\n"); - printf("\t"); if (have->quantity) - printf("%.8g\n", + printf("\t%.8g\n", (have->factor + have->offset-want->offset)/want->factor); - else - printf(" (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n", + else { + printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n", have->factor / want->factor, (have->offset-want->offset)/want->factor, want->factor / have->factor, (want->offset - have->offset)/have->factor); + } + } + else { + ans = have->factor / want->factor; + if (verbose) + printf("\t%s = %.8g * %s\n", havestr, ans, wantstr); + else + printf("\t* %.8g\n", ans); + + if (verbose) + printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr); + else + printf("\t/ %.8g\n", 1/ans); } - else - printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor, - want->factor / have->factor); } @@ -688,8 +710,6 @@ main(int argc, char **argv) { struct unittype have, want; - const char * havestr; - const char * wantstr; int optchar; bool quiet; bool readfile; @@ -700,7 +720,7 @@ main(int argc, char **argv) quiet = false; readfile = false; - while ((optchar = getopt(argc, argv, "UVqf:")) != -1) { + while ((optchar = getopt(argc, argv, "fqvUV:")) != -1) { switch (optchar) { case 'f': readfile = true; @@ -712,6 +732,9 @@ main(int argc, char **argv) case 'q': quiet = true; break; + case 'v': + verbose = true; + break; case 'U': if (access(UNITSFILE, F_OK) == 0) printf("%s\n", UNITSFILE); -- 2.41.0