summaryrefslogtreecommitdiffstats
path: root/usr.bin/units
diff options
context:
space:
mode:
authoreadler <eadler@FreeBSD.org>2014-04-14 20:51:04 +0000
committereadler <eadler@FreeBSD.org>2014-04-14 20:51:04 +0000
commit9ba0aa30f1828f0df9a6a08b39d6c0c8f83d5f29 (patch)
tree273ac2bdf6c1c8c1d5ab463e0837de2b98f4d55e /usr.bin/units
parentff97df6be22e57bf2e09acf8efe0fc99e289ad3c (diff)
downloadFreeBSD-src-9ba0aa30f1828f0df9a6a08b39d6c0c8f83d5f29.zip
FreeBSD-src-9ba0aa30f1828f0df9a6a08b39d6c0c8f83d5f29.tar.gz
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.
Diffstat (limited to 'usr.bin/units')
-rw-r--r--usr.bin/units/units.14
-rw-r--r--usr.bin/units/units.c49
2 files changed, 39 insertions, 14 deletions
diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1
index 4b485f3..83f2d9e 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 c065941..b1741b8 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);
OpenPOWER on IntegriCloud