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
.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

View File

@ -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\t/ %.8g\n", have->factor / want->factor,
want->factor / have->factor);
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);
}
}
@ -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);