summaryrefslogtreecommitdiffstats
path: root/usr.bin/units
diff options
context:
space:
mode:
authorcharnier <charnier@FreeBSD.org>1997-08-21 06:49:33 +0000
committercharnier <charnier@FreeBSD.org>1997-08-21 06:49:33 +0000
commit85fa6720fe80acac578ef7b06c6594bcba3e78e9 (patch)
tree4ffe790673a09247497fe96c6c822805094042d9 /usr.bin/units
parent097df86e90be3a2e36b69368eec4c848be3fd247 (diff)
downloadFreeBSD-src-85fa6720fe80acac578ef7b06c6594bcba3e78e9.zip
FreeBSD-src-85fa6720fe80acac578ef7b06c6594bcba3e78e9.tar.gz
Use err(3). Cosmetic in usage(). Rewrote man page in mdoc format.
Diffstat (limited to 'usr.bin/units')
-rw-r--r--usr.bin/units/units.1161
-rw-r--r--usr.bin/units/units.c59
2 files changed, 105 insertions, 115 deletions
diff --git a/usr.bin/units/units.1 b/usr.bin/units/units.1
index 4ca4aca..795c508 100644
--- a/usr.bin/units/units.1
+++ b/usr.bin/units/units.1
@@ -1,35 +1,40 @@
-.\" $Id: units.1,v 1.4 1997/02/22 19:57:31 peter Exp $
-.TH UNITS 1 "14 July 1993"
-.SH NAME
-units - conversion program
-.SH SYNTAX
-.B units
-[-f filename] [-q] [to-unit from-unit]
-.SH SUMMARY
-.TP 4
-.B -f filename
-Specifies the name of the units data file to load.
-.LP
-.TP 4
-.B -q
-Suppresses prompting of the user for units and the display of statistics
+.\" $Id: units.1,v 1.5 1997/05/06 03:31:14 alex Exp $
+.Dd July 14, 1993
+.Dt UNITS 1
+.Os
+.Sh NAME
+.Nm units
+.Nd conversion program
+.Sh SYNTAX
+.Nm
+.Op Fl f Ar filename
+.Op Fl qv
+.Op Ar to-unit from-unit
+.Sh SUMMARY
+The following options are available:
+.Bl -tag -width indent
+.It Fl f Ar filename
+Specify the name of the units data file to load.
+.It Fl q
+Suppress prompting of the user for units and the display of statistics
about the number of units loaded.
-.LP
-.TP 4
-.B from-unit to-unit
-Allows a single unit conversion to be done directly from the command
+.It Fl v
+Print the version number.
+.It Ar from-unit to-unit
+Allow a single unit conversion to be done directly from the command
line. The program will not print prompts. It will print out the
result of the single specified conversion.
-
-
-.SH DESCRIPTION
-The units program converts quantities expressed in various scales to
-their equivalents in other scales. The units program can only
+.Sh DESCRIPTION
+The
+.Nm
+program converts quantities expressed in various scales to
+their equivalents in other scales. The
+.Nm
+program can only
handle multiplicative scale changes. It cannot convert Centigrade
to Fahrenheit, for example. It works interactively by prompting
the user for input:
-.nf
-
+.Bd -literal
You have: meters
You want: feet
* 3.2808399
@@ -49,8 +54,8 @@ the user for input:
You want: cm
* 1.27
/ 0.78740157
-
-.fi
+.Ed
+.Pp
Powers of units can be specified using the '^' character as shown in
the example, or by simple concatenation: 'cm3' is equivalent to 'cm^3'.
Multiplication of units can be specified by using spaces, a dash or
@@ -63,96 +68,92 @@ units program would interpret that as equivalent to '0.5/meter'.
If you enter incompatible unit types, the units program will
print a message indicating that the units are not conformable and
it will display the reduced form for each unit:
-.nf
-
+.Bd -literal
You have: ergs/hour
You want: fathoms kg^2 / day
conformability error
2.7777778e-11 kg m^2 / sec^3
2.1166667e-05 kg^2 m / sec
-.fi
-.LP
+.Ed
+.Pp
The conversion information is read from a units data file. The default
file includes definitions for most familiar units, abbreviations and
metric prefixes. Some constants of nature included are:
-.in +4m
-.ta
-.ta 9m +
-.nf
-
-pi ratio of circumference to diameter
-c speed of light
-e charge on an electron
-g acceleration of gravity
-force same as g
-mole Avogadro's number
-water pressure per unit height of water
-mercury pressure per unit height of mercury
-au astronomical unit
-
-.fi
-.in -4m
+.Pp
+.Bl -inset -offset indent -compact
+.It pi ratio of circumference to diameter
+.It c speed of light
+.It e charge on an electron
+.It g acceleration of gravity
+.It force same as g
+.It mole Avogadro's number
+.It water pressure per unit height of water
+.It mercury pressure per unit height of mercury
+.It au astronomical unit
+.El
+.Pp
The unit 'pound' is a unit of mass. Compound names are run together
so 'poundforce' is a unit of force. The unit 'ounce' is also a unit
of mass. The fluid ounce is 'floz'. British units that differ from
their US counterparts are prefixed with 'br', and currency is prefixed
with its country name: 'belgiumfranc', 'britainpound'. When searching
for a unit, if the specified string does not appear exactly as a unit
-name, then the units program will try to remove a trailing 's' or a
+name, then
+.Nm
+will try to remove a trailing 's' or a
trailing 'es' and check again for a match.
-.LP
+.Pp
To find out what units are available read the standard units file.
If you want to add your own units you can supply your own file.
A unit is specified on a single line by
giving its name and an equivalence. Be careful to define
new units in terms of old ones so that a reduction leads to the
primitive units which are marked with '!' characters.
-The units program will not detect infinite loops that could be caused
+The
+.Nm
+program will not detect infinite loops that could be caused
by careless unit definitions. Comments in the unit definition file
begin with a '/' character at the beginning of a line.
-.LP
+.Pp
Prefixes are defined in the same was as standard units, but with
a trailing dash at the end of the prefix name. If a unit is not found
even after removing trailing 's' or 'es', then it will be checked
against the list of prefixes. Prefixes will be removed until a legal
base unit is identified.
-.LP
+.Pp
Here is an example of a short units file that defines some basic
units.
-.in 17m
-.ta
-.ta 9m +
-.nf
-
-m !a!
-sec !b!
-micro- 1e-6
-minute 60 sec
-hour 60 min
-inch 0.0254 m
-ft 12 inches
-mile 5280 ft
-.fi
-.in -17m
-
-.SH BUGS
-.LP
+.Pp
+.Bl -inset -offset indent -compact
+.It m !a!
+.It sec !b!
+.It micro- 1e-6
+.It minute 60 sec
+.It hour 60 min
+.It inch 0.0254 m
+.It ft 12 inches
+.It mile 5280 ft
+.El
+.Sh BUGS
The effect of including a '/' in a prefix is surprising.
-.LP
+.Pp
Exponents entered by the user can be only one digit.
You can work around this by multiplying several terms.
-.LP
+.Pp
The user must use | to indicate division of numbers and / to
indicate division of symbols. This distinction should not
be necessary.
-.LP
+.Pp
The program contains various arbitrary limits on the length
of the units converted and on the length of the data file.
-.LP
+.Pp
The program should use a hash table to store units so that
it doesn't take so long to load the units list and check
for duplication.
-.SH FILES
-/usr/share/misc/units.lib - the standard units library
-.SH AUTHOR
-Adrian Mariano (adrian@cam.cornell.edu)
+.Sh FILES
+.Bl -tag -width /usr/share/misc/units.lib -compact
+.It Pa /usr/share/misc/units.lib
+the standard units library
+.El
+.Sh AUTHOR
+.An Adrian Mariano Aq adrian@cam.cornell.edu
diff --git a/usr.bin/units/units.c b/usr.bin/units/units.c
index 8ecc077..3eae64b 100644
--- a/usr.bin/units/units.c
+++ b/usr.bin/units/units.c
@@ -1,5 +1,3 @@
-/* $Id$ */
-
/*
* units.c Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu)
*
@@ -17,10 +15,17 @@
* improvements you might make to this program.
*/
+#ifndef lint
+static const char rcsid[] =
+ "$Id$";
+#endif /* not lint */
+
#include <ctype.h>
+#include <err.h>
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
#include "pathnames.h"
@@ -68,10 +73,8 @@ dupstr(char *str)
char *ret;
ret = malloc(strlen(str) + 1);
- if (!ret) {
- fprintf(stderr, "Memory allocation error\n");
- exit(3);
- }
+ if (!ret)
+ errx(3, "memory allocation error");
strcpy(ret, str);
return (ret);
}
@@ -80,8 +83,7 @@ dupstr(char *str)
void
readerror(int linenum)
{
- fprintf(stderr, "Error in units file '%s' line %d\n", UNITSFILE,
- linenum);
+ warnx("error in units file '%s' line %d", UNITSFILE, linenum);
}
@@ -97,11 +99,8 @@ readunits(char *userfile)
if (userfile) {
unitfile = fopen(userfile, "rt");
- if (!unitfile) {
- fprintf(stderr, "Unable to open units file '%s'\n",
- userfile);
- exit(1);
- }
+ if (!unitfile)
+ errx(1, "unable to open units file '%s'", userfile);
}
else {
unitfile = fopen(UNITSFILE, "rt");
@@ -130,11 +129,8 @@ readunits(char *userfile)
direc = strtok(NULL, separator);
}
}
- if (!unitfile) {
- fprintf(stderr, "Can't find units file '%s'\n",
- UNITSFILE);
- exit(1);
- }
+ if (!unitfile)
+ errx(1, "can't find units file '%s'", UNITSFILE);
}
}
while (!feof(unitfile)) {
@@ -151,15 +147,14 @@ readunits(char *userfile)
continue;
if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
if (prefixcount == MAXPREFIXES) {
- fprintf(stderr, "Memory for prefixes exceeded in line %d\n",
- linenum);
+ warnx("memory for prefixes exceeded in line %d", linenum);
continue;
}
lineptr[strlen(lineptr) - 1] = 0;
prefixtable[prefixcount].prefixname = dupstr(lineptr);
for (i = 0; i < prefixcount; i++)
if (!strcmp(prefixtable[i].prefixname, lineptr)) {
- fprintf(stderr, "Redefinition of prefix '%s' on line %d ignored\n",
+ warnx("redefinition of prefix '%s' on line %d ignored",
lineptr, linenum);
continue;
}
@@ -175,14 +170,13 @@ readunits(char *userfile)
}
else { /* it's not a prefix */
if (unitcount == MAXUNITS) {
- fprintf(stderr, "Memory for units exceeded in line %d\n",
- linenum);
+ warnx("memory for units exceeded in line %d", linenum);
continue;
}
unittable[unitcount].uname = dupstr(lineptr);
for (i = 0; i < unitcount; i++)
if (!strcmp(unittable[i].uname, lineptr)) {
- fprintf(stderr, "Redefinition of unit '%s' on line %d ignored\n",
+ warnx("redefinition of unit '%s' on line %d ignored",
lineptr, linenum);
continue;
}
@@ -215,7 +209,7 @@ addsubunit(char *product[], char *toadd)
for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
if (ptr >= product + MAXSUBUNITS) {
- fprintf(stderr, "Memory overflow in unit reduction\n");
+ warnx("memory overflow in unit reduction");
return 1;
}
if (!*ptr)
@@ -274,7 +268,7 @@ showunit(struct unittype * theunit)
void
zeroerror()
{
- fprintf(stderr, "Unit reduces to zero\n");
+ warnx("unit reduces to zero");
}
/*
@@ -617,10 +611,8 @@ showanswer(struct unittype * have, struct unittype * want)
void
usage()
{
- fprintf(stderr, "\nunits [-f unitsfile] [-q] [-v] [from-unit to-unit]\n");
- fprintf(stderr, "\n -f specify units file\n");
- fprintf(stderr, " -q supress prompting (quiet)\n");
- fprintf(stderr, " -v print version number\n");
+ fprintf(stderr,
+ "usage: units [-f unitsfile] [-q] [-v] [from-unit to-unit]\n");
exit(3);
}
@@ -635,9 +627,6 @@ main(int argc, char **argv)
char *userfile = 0;
int quiet = 0;
- extern char *optarg;
- extern int optind;
-
while ((optchar = getopt(argc, argv, "vqf:")) != -1) {
switch (optchar) {
case 'f':
@@ -675,7 +664,7 @@ main(int argc, char **argv)
}
else {
if (!quiet)
- printf("%d units, %d prefixes\n\n", unitcount,
+ printf("%d units, %d prefixes\n", unitcount,
prefixcount);
for (;;) {
do {
OpenPOWER on IntegriCloud