From 8d61f393a31983dc980c0486eea841bc23f2a2fd Mon Sep 17 00:00:00 2001 From: Eitan Adler Date: Sat, 5 Jul 2014 03:17:57 +0000 Subject: [PATCH] units(1): Add 'terse' support terse output is used when calling units from another script. --- usr.bin/units/units.1 | 4 ++++ usr.bin/units/units.c | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1 index 7e88efb8b86f..4b73da4eb096 100644 --- a/usr.bin/units/units.1 +++ b/usr.bin/units/units.1 @@ -26,6 +26,10 @@ If not, print .Qo Units data file not found .Qc +.It Fl t No , Fl -terse +Only print the result. This is used when calling +.Nm +from other programs for easy to parse results. .It Fl v No , Fl -verbose Print the units in the conversion output. Be more verbose in general. diff --git a/usr.bin/units/units.c b/usr.bin/units/units.c index baf2a5942391..f8e4f11bb244 100644 --- a/usr.bin/units/units.c +++ b/usr.bin/units/units.c @@ -78,6 +78,7 @@ static char NULLUNIT[] = ""; static int unitcount; static int prefixcount; static bool verbose = false; +static bool terse = false; static const char * havestr; static const char * wantstr; @@ -657,14 +658,16 @@ showanswer(struct unittype * have, struct unittype * want) printf("conformability error\n"); if (verbose) printf("\t%s = ", havestr); - else + else if (!terse) printf("\t"); showunit(have); - if (verbose) - printf("\t%s = ", wantstr); - else - printf("\t"); - showunit(want); + if (!terse) { + if (verbose) + printf("\t%s = ", wantstr); + else + printf("\t"); + showunit(want); + } } else if (have->offset != want->offset) { if (want->quantity) @@ -684,12 +687,14 @@ showanswer(struct unittype * have, struct unittype * want) ans = have->factor / want->factor; if (verbose) printf("\t%s = %.8g * %s\n", havestr, ans, wantstr); - else + else if (terse) + printf("%.8g\n", ans); + else printf("\t* %.8g\n", ans); if (verbose) printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr); - else + else if (!terse) printf("\t/ %.8g\n", 1/ans); } } @@ -707,8 +712,9 @@ static struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"file", required_argument, NULL, 'f'}, {"quiet", no_argument, NULL, 'q'}, - {"verbose", no_argument, NULL, 'v'}, + {"terse", no_argument, NULL, 't'}, {"unitsfile", no_argument, NULL, 'U'}, + {"verbose", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'V'}, { 0, 0, 0, 0 } }; @@ -729,7 +735,7 @@ main(int argc, char **argv) quiet = false; readfile = false; - while ((optchar = getopt_long(argc, argv, "+hf:qvUV", longopts, NULL)) != -1) { + while ((optchar = getopt_long(argc, argv, "+hf:qtvUV", longopts, NULL)) != -1) { switch (optchar) { case 'f': readfile = true; @@ -741,6 +747,9 @@ main(int argc, char **argv) case 'q': quiet = true; break; + case 't': + terse = true; + break; case 'v': verbose = true; break; @@ -825,5 +834,5 @@ main(int argc, char **argv) history_end(inhistory); el_end(el); - return(0); + return (0); }