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.
This commit is contained in:
eadler 2014-04-14 20:51:04 +00:00
parent ff97df6be2
commit 9ba0aa30f1
2 changed files with 39 additions and 14 deletions

View File

@ -8,7 +8,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl f Ar filename .Op Fl f Ar filename
.Op Fl qUV .Op Fl qvUV
.Op Ar from-unit to-unit .Op Ar from-unit to-unit
.Sh OPTIONS .Sh OPTIONS
The following options are available: The following options are available:
@ -31,6 +31,8 @@ line.
The program will not print prompts. The program will not print prompts.
It will print out the It will print out the
result of the single specified conversion. result of the single specified conversion.
.It Fl v
Print the units in the conversion output. Be more verbose in general.
.El .El
.Sh DESCRIPTION .Sh DESCRIPTION
The The

View File

@ -76,6 +76,10 @@ static char NULLUNIT[] = "";
static int unitcount; static int unitcount;
static int prefixcount; static int prefixcount;
static bool verbose = false;
static const char * havestr;
static const char * wantstr;
char *dupstr(const char *str); char *dupstr(const char *str);
void readunits(const char *userfile); void readunits(const char *userfile);
@ -254,7 +258,7 @@ showunit(struct unittype * theunit)
int printedslash; int printedslash;
int counter = 1; int counter = 1;
printf("\t%.8g", theunit->factor); printf("%.8g", theunit->factor);
if (theunit->offset) if (theunit->offset)
printf("&%.8g", theunit->offset); printf("&%.8g", theunit->offset);
for (ptr = theunit->numerator; *ptr; ptr++) { for (ptr = theunit->numerator; *ptr; ptr++) {
@ -289,7 +293,7 @@ showunit(struct unittype * theunit)
counter = 1; counter = 1;
} }
} }
if (counter > 1) if ( counter > 1)
printf("%s%d", powerstring, counter); printf("%s%d", powerstring, counter);
printf("\n"); printf("\n");
} }
@ -645,32 +649,50 @@ completereduce(struct unittype * unit)
return 0; return 0;
} }
void void
showanswer(struct unittype * have, struct unittype * want) showanswer(struct unittype * have, struct unittype * want)
{ {
double ans;
if (compareunits(have, want)) { if (compareunits(have, want)) {
printf("conformability error\n"); printf("conformability error\n");
if (verbose)
printf("\t%s = ", havestr);
else
printf("\t");
showunit(have); showunit(have);
if (verbose)
printf("\t%s = ", wantstr);
else
printf("\t");
showunit(want); showunit(want);
} }
else if (have->offset != want->offset) { else if (have->offset != want->offset) {
if (want->quantity) if (want->quantity)
printf("WARNING: conversion of non-proportional quantities.\n"); printf("WARNING: conversion of non-proportional quantities.\n");
printf("\t");
if (have->quantity) if (have->quantity)
printf("%.8g\n", printf("\t%.8g\n",
(have->factor + have->offset-want->offset)/want->factor); (have->factor + have->offset-want->offset)/want->factor);
else else {
printf(" (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n", printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
have->factor / want->factor, have->factor / want->factor,
(have->offset-want->offset)/want->factor, (have->offset-want->offset)/want->factor,
want->factor / have->factor, want->factor / have->factor,
(want->offset - have->offset)/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 else
printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor, printf("\t* %.8g\n", ans);
want->factor / have->factor);
if (verbose)
printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr);
else
printf("\t/ %.8g\n", 1/ans);
}
} }
@ -688,8 +710,6 @@ main(int argc, char **argv)
{ {
struct unittype have, want; struct unittype have, want;
const char * havestr;
const char * wantstr;
int optchar; int optchar;
bool quiet; bool quiet;
bool readfile; bool readfile;
@ -700,7 +720,7 @@ main(int argc, char **argv)
quiet = false; quiet = false;
readfile = false; readfile = false;
while ((optchar = getopt(argc, argv, "UVqf:")) != -1) { while ((optchar = getopt(argc, argv, "fqvUV:")) != -1) {
switch (optchar) { switch (optchar) {
case 'f': case 'f':
readfile = true; readfile = true;
@ -712,6 +732,9 @@ main(int argc, char **argv)
case 'q': case 'q':
quiet = true; quiet = true;
break; break;
case 'v':
verbose = true;
break;
case 'U': case 'U':
if (access(UNITSFILE, F_OK) == 0) if (access(UNITSFILE, F_OK) == 0)
printf("%s\n", UNITSFILE); printf("%s\n", UNITSFILE);