summaryrefslogtreecommitdiffstats
path: root/usr.bin/units
diff options
context:
space:
mode:
authoreadler <eadler@FreeBSD.org>2014-07-05 03:17:57 +0000
committereadler <eadler@FreeBSD.org>2014-07-05 03:17:57 +0000
commit739ad2052de5cad026c698e289008ba7492ba801 (patch)
tree67c9bdb8f4f628f5182c9fcd71e84dcb89c38b45 /usr.bin/units
parent08d74d148228f96d6a3209f489daa64497c008e8 (diff)
downloadFreeBSD-src-739ad2052de5cad026c698e289008ba7492ba801.zip
FreeBSD-src-739ad2052de5cad026c698e289008ba7492ba801.tar.gz
units(1): Add 'terse' support
terse output is used when calling units from another script.
Diffstat (limited to 'usr.bin/units')
-rw-r--r--usr.bin/units/units.14
-rw-r--r--usr.bin/units/units.c31
2 files changed, 24 insertions, 11 deletions
diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1
index 7e88efb..4b73da4 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 baf2a59..f8e4f11 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);
}
OpenPOWER on IntegriCloud