mirror of
https://git.FreeBSD.org/src.git
synced 2024-10-19 02:29:40 +00:00
units(1): Add support for output-format
Add support for the output-format argument. This also exposes subtle rounding differences between GNU units and our units.
This commit is contained in:
parent
2814ea66e3
commit
cae148478c
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=268792
@ -17,6 +17,8 @@ The following options are available:
|
||||
Show an overview of options
|
||||
.It Fl f Ar filename No , Fl -file Ar filename
|
||||
Specify the name of the units data file to load.
|
||||
.It Fl e , Fl -exponential
|
||||
Behave as if -o '%6e' was typed.
|
||||
.It Fl q No , Fl -quiet
|
||||
Suppress prompting of the user for units and the display of statistics
|
||||
about the number of units loaded.
|
||||
@ -33,6 +35,8 @@ 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.
|
||||
.It Fl o Ar format No , Fl -output-format Ar format
|
||||
Select the output format string by which numbers are printed.
|
||||
.It Fl V No , Fl -version
|
||||
Print the version number, usage, and then exit.
|
||||
.It Ar from-unit to-unit
|
||||
|
@ -75,6 +75,7 @@ static int unitcount;
|
||||
static int prefixcount;
|
||||
static bool verbose = false;
|
||||
static bool terse = false;
|
||||
static const char * outputformat;
|
||||
static const char * havestr;
|
||||
static const char * wantstr;
|
||||
|
||||
@ -649,6 +650,7 @@ static void
|
||||
showanswer(struct unittype * have, struct unittype * want)
|
||||
{
|
||||
double ans;
|
||||
char* oformat;
|
||||
|
||||
if (compareunits(have, want)) {
|
||||
printf("conformability error\n");
|
||||
@ -668,11 +670,16 @@ showanswer(struct unittype * have, struct unittype * want)
|
||||
else if (have->offset != want->offset) {
|
||||
if (want->quantity)
|
||||
printf("WARNING: conversion of non-proportional quantities.\n");
|
||||
if (have->quantity)
|
||||
printf("\t%.8g\n",
|
||||
if (have->quantity) {
|
||||
asprintf(&oformat, "\t%s\n", outputformat);
|
||||
printf(oformat,
|
||||
(have->factor + have->offset-want->offset)/want->factor);
|
||||
free(oformat);
|
||||
}
|
||||
else {
|
||||
printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
|
||||
asprintf(&oformat, "\t (-> x*%sg %sg)\n\t (<- y*%sg %sg)\n",
|
||||
outputformat, outputformat, outputformat, outputformat);
|
||||
printf(oformat,
|
||||
have->factor / want->factor,
|
||||
(have->offset-want->offset)/want->factor,
|
||||
want->factor / have->factor,
|
||||
@ -681,17 +688,33 @@ showanswer(struct unittype * have, struct unittype * want)
|
||||
}
|
||||
else {
|
||||
ans = have->factor / want->factor;
|
||||
if (verbose)
|
||||
printf("\t%s = %.8g * %s\n", havestr, ans, wantstr);
|
||||
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 if (!terse)
|
||||
printf("\t/ %.8g\n", 1/ans);
|
||||
if (verbose) {
|
||||
printf("\t%s = ", havestr);
|
||||
printf(outputformat, ans);
|
||||
printf(" * %s", wantstr);
|
||||
printf("\n");
|
||||
}
|
||||
else if (terse) {
|
||||
printf(outputformat, ans);
|
||||
printf("\n");
|
||||
}
|
||||
else {
|
||||
printf("\t* ");
|
||||
printf(outputformat, ans);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
printf("\t%s = (1 / ", havestr);
|
||||
printf(outputformat, 1/ans);
|
||||
printf(") * %s\n", wantstr);
|
||||
}
|
||||
else if (!terse) {
|
||||
printf("\t/ ");
|
||||
printf(outputformat, 1/ans);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -706,7 +729,9 @@ usage(void)
|
||||
|
||||
static struct option longopts[] = {
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{"exponential", no_argument, NULL, 'e'},
|
||||
{"file", required_argument, NULL, 'f'},
|
||||
{"output-format", required_argument, NULL, 'o'},
|
||||
{"quiet", no_argument, NULL, 'q'},
|
||||
{"terse", no_argument, NULL, 't'},
|
||||
{"unitsfile", no_argument, NULL, 'U'},
|
||||
@ -731,8 +756,12 @@ main(int argc, char **argv)
|
||||
|
||||
quiet = false;
|
||||
readfile = false;
|
||||
while ((optchar = getopt_long(argc, argv, "+hf:qtvUV", longopts, NULL)) != -1) {
|
||||
outputformat = "%.8g";
|
||||
while ((optchar = getopt_long(argc, argv, "+ehf:oqtvUV", longopts, NULL)) != -1) {
|
||||
switch (optchar) {
|
||||
case 'e':
|
||||
outputformat = "%6e";
|
||||
break;
|
||||
case 'f':
|
||||
readfile = true;
|
||||
if (strlen(optarg) == 0)
|
||||
@ -746,6 +775,9 @@ main(int argc, char **argv)
|
||||
case 't':
|
||||
terse = true;
|
||||
break;
|
||||
case 'o':
|
||||
outputformat = optarg;
|
||||
break;
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user