From owner-svn-src-all@FreeBSD.ORG Sat Jul 5 03:17:58 2014 Return-Path: Delivered-To: svn-src-all@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 863F6A7E; Sat, 5 Jul 2014 03:17:58 +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 66CFB2DF7; Sat, 5 Jul 2014 03:17:58 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s653HwXh062181; Sat, 5 Jul 2014 03:17:58 GMT (envelope-from eadler@svn.freebsd.org) Received: (from eadler@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s653HwJV062179; Sat, 5 Jul 2014 03:17:58 GMT (envelope-from eadler@svn.freebsd.org) Message-Id: <201407050317.s653HwJV062179@svn.freebsd.org> From: Eitan Adler Date: Sat, 5 Jul 2014 03:17:57 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r268277 - 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-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Jul 2014 03:17:58 -0000 Author: eadler Date: Sat Jul 5 03:17:57 2014 New Revision: 268277 URL: http://svnweb.freebsd.org/changeset/base/268277 Log: units(1): Add 'terse' support terse output is used when calling units from another script. 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 Sat Jul 5 02:38:53 2014 (r268276) +++ head/usr.bin/units/units.1 Sat Jul 5 03:17:57 2014 (r268277) @@ -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. Modified: head/usr.bin/units/units.c ============================================================================== --- head/usr.bin/units/units.c Sat Jul 5 02:38:53 2014 (r268276) +++ head/usr.bin/units/units.c Sat Jul 5 03:17:57 2014 (r268277) @@ -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, struc 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, struc 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); }