From owner-svn-src-head@FreeBSD.ORG Thu Jul 17 06:54:13 2014 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 02C482C4; Thu, 17 Jul 2014 06:54:13 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E4EED242F; Thu, 17 Jul 2014 06:54:12 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s6H6sCnH087430; Thu, 17 Jul 2014 06:54:12 GMT (envelope-from eadler@svn.freebsd.org) Received: (from eadler@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s6H6sCSR087427; Thu, 17 Jul 2014 06:54:12 GMT (envelope-from eadler@svn.freebsd.org) Message-Id: <201407170654.s6H6sCSR087427@svn.freebsd.org> From: Eitan Adler Date: Thu, 17 Jul 2014 06:54:12 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r268792 - head/usr.bin/units X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Jul 2014 06:54:13 -0000 Author: eadler Date: Thu Jul 17 06:54:12 2014 New Revision: 268792 URL: http://svnweb.freebsd.org/changeset/base/268792 Log: 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. Modified: head/usr.bin/units/units.1 head/usr.bin/units/units.c Modified: head/usr.bin/units/units.1 ============================================================================== --- head/usr.bin/units/units.1 Thu Jul 17 06:36:22 2014 (r268791) +++ head/usr.bin/units/units.1 Thu Jul 17 06:54:12 2014 (r268792) @@ -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 re .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 Modified: head/usr.bin/units/units.c ============================================================================== --- head/usr.bin/units/units.c Thu Jul 17 06:36:22 2014 (r268791) +++ head/usr.bin/units/units.c Thu Jul 17 06:54:12 2014 (r268792) @@ -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, struc 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, struc } 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;