1996-06-08 03:43:43 +00:00
|
|
|
/*
|
|
|
|
* units.c Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
* Disclaimer: This software is provided by the author "as is". The author
|
|
|
|
* shall not be liable for any damages caused in any way by this software.
|
|
|
|
*
|
|
|
|
* I would appreciate (though I do not require) receiving a copy of any
|
|
|
|
* improvements you might make to this program.
|
|
|
|
*/
|
|
|
|
|
1997-08-21 06:49:33 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1997-08-21 06:49:33 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-06-08 03:43:43 +00:00
|
|
|
#include <ctype.h>
|
1997-08-21 06:49:33 +00:00
|
|
|
#include <err.h>
|
2014-03-30 16:04:47 +00:00
|
|
|
#include <errno.h>
|
2014-04-14 14:04:59 +00:00
|
|
|
#include <histedit.h>
|
2014-07-04 21:21:05 +00:00
|
|
|
#include <getopt.h>
|
2014-04-14 14:04:59 +00:00
|
|
|
#include <stdbool.h>
|
1996-06-08 03:43:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-08-21 06:49:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1996-06-08 03:43:43 +00:00
|
|
|
|
2014-03-30 16:04:47 +00:00
|
|
|
#include <sys/capsicum.h>
|
|
|
|
|
1996-06-08 03:43:43 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
#ifndef UNITSFILE
|
|
|
|
#define UNITSFILE _PATH_UNITSLIB
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXUNITS 1000
|
2001-02-28 15:57:38 +00:00
|
|
|
#define MAXPREFIXES 100
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
#define MAXSUBUNITS 500
|
|
|
|
|
|
|
|
#define PRIMITIVECHAR '!'
|
|
|
|
|
2011-11-06 08:18:17 +00:00
|
|
|
static const char *powerstring = "^";
|
1996-06-08 03:43:43 +00:00
|
|
|
|
2011-11-06 08:18:17 +00:00
|
|
|
static struct {
|
1996-06-08 03:43:43 +00:00
|
|
|
char *uname;
|
|
|
|
char *uval;
|
|
|
|
} unittable[MAXUNITS];
|
|
|
|
|
|
|
|
struct unittype {
|
|
|
|
char *numerator[MAXSUBUNITS];
|
|
|
|
char *denominator[MAXSUBUNITS];
|
|
|
|
double factor;
|
2008-08-16 16:27:41 +00:00
|
|
|
double offset;
|
|
|
|
int quantity;
|
1996-06-08 03:43:43 +00:00
|
|
|
};
|
|
|
|
|
2011-11-06 08:18:17 +00:00
|
|
|
static struct {
|
1996-06-08 03:43:43 +00:00
|
|
|
char *prefixname;
|
|
|
|
char *prefixval;
|
|
|
|
} prefixtable[MAXPREFIXES];
|
|
|
|
|
|
|
|
|
2011-11-06 08:18:17 +00:00
|
|
|
static char NULLUNIT[] = "";
|
1996-06-08 03:43:43 +00:00
|
|
|
|
2000-07-31 10:49:08 +00:00
|
|
|
#ifdef MSDOS
|
2001-02-28 15:57:38 +00:00
|
|
|
#define SEPARATOR ";"
|
2000-07-31 10:49:08 +00:00
|
|
|
#else
|
|
|
|
#define SEPARATOR ":"
|
|
|
|
#endif
|
|
|
|
|
2011-11-06 08:18:17 +00:00
|
|
|
static int unitcount;
|
|
|
|
static int prefixcount;
|
2014-04-14 20:51:04 +00:00
|
|
|
static bool verbose = false;
|
|
|
|
static const char * havestr;
|
|
|
|
static const char * wantstr;
|
|
|
|
|
2014-04-14 21:07:54 +00:00
|
|
|
static int addsubunit(char *product[], char *toadd);
|
|
|
|
static int addunit(struct unittype *theunit, const char *toadd, int flip, int quantity);
|
|
|
|
static void cancelunit(struct unittype * theunit);
|
|
|
|
static int compare(const void *item1, const void *item2);
|
|
|
|
static int compareproducts(char **one, char **two);
|
|
|
|
static int compareunits(struct unittype * first, struct unittype * second);
|
|
|
|
static int completereduce(struct unittype * unit);
|
|
|
|
static char *dupstr(const char *str);
|
|
|
|
static void initializeunit(struct unittype * theunit);
|
|
|
|
static char *lookupunit(const char *unit);
|
|
|
|
static void readunits(const char *userfile);
|
|
|
|
static int reduceproduct(struct unittype * theunit, int flip);
|
|
|
|
static int reduceunit(struct unittype * theunit);
|
|
|
|
static void showanswer(struct unittype * have, struct unittype * want);
|
|
|
|
static void showunit(struct unittype * theunit);
|
|
|
|
static void sortunit(struct unittype * theunit);
|
|
|
|
static void usage(void);
|
|
|
|
static void zeroerror(void);
|
1996-06-08 03:43:43 +00:00
|
|
|
|
2014-04-14 14:04:59 +00:00
|
|
|
static const char* promptstr = "";
|
|
|
|
|
|
|
|
static const char * prompt(EditLine *e __unused) {
|
|
|
|
return promptstr;
|
|
|
|
}
|
|
|
|
|
1996-06-08 03:43:43 +00:00
|
|
|
char *
|
2001-02-28 15:57:38 +00:00
|
|
|
dupstr(const char *str)
|
1996-06-08 03:43:43 +00:00
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
2014-05-12 05:44:55 +00:00
|
|
|
ret = strdup(str);
|
1997-08-21 06:49:33 +00:00
|
|
|
if (!ret)
|
2014-05-12 05:44:55 +00:00
|
|
|
err(3, "dupstr");
|
1996-06-08 03:43:43 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2001-02-28 15:57:38 +00:00
|
|
|
readunits(const char *userfile)
|
1996-06-08 03:43:43 +00:00
|
|
|
{
|
|
|
|
FILE *unitfile;
|
2001-02-28 15:57:38 +00:00
|
|
|
char line[512], *lineptr;
|
1996-06-08 03:43:43 +00:00
|
|
|
int len, linenum, i;
|
2014-03-30 16:04:47 +00:00
|
|
|
cap_rights_t unitfilerights;
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
unitcount = 0;
|
|
|
|
linenum = 0;
|
|
|
|
|
|
|
|
if (userfile) {
|
|
|
|
unitfile = fopen(userfile, "rt");
|
1997-08-21 06:49:33 +00:00
|
|
|
if (!unitfile)
|
|
|
|
errx(1, "unable to open units file '%s'", userfile);
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
unitfile = fopen(UNITSFILE, "rt");
|
|
|
|
if (!unitfile) {
|
|
|
|
char *direc, *env;
|
|
|
|
char filename[1000];
|
|
|
|
|
|
|
|
env = getenv("PATH");
|
|
|
|
if (env) {
|
2000-07-31 10:49:08 +00:00
|
|
|
direc = strtok(env, SEPARATOR);
|
1996-06-08 03:43:43 +00:00
|
|
|
while (direc) {
|
2000-07-31 10:49:08 +00:00
|
|
|
snprintf(filename, sizeof(filename),
|
|
|
|
"%s/%s", direc, UNITSFILE);
|
1996-06-08 03:43:43 +00:00
|
|
|
unitfile = fopen(filename, "rt");
|
|
|
|
if (unitfile)
|
|
|
|
break;
|
2000-07-31 10:49:08 +00:00
|
|
|
direc = strtok(NULL, SEPARATOR);
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
1997-08-21 06:49:33 +00:00
|
|
|
if (!unitfile)
|
|
|
|
errx(1, "can't find units file '%s'", UNITSFILE);
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-30 16:04:47 +00:00
|
|
|
cap_rights_init(&unitfilerights, CAP_READ, CAP_FSTAT);
|
|
|
|
if (cap_rights_limit(fileno(unitfile), &unitfilerights) < 0
|
|
|
|
&& errno != ENOSYS)
|
|
|
|
err(1, "cap_rights_limit() failed");
|
1996-06-08 03:43:43 +00:00
|
|
|
while (!feof(unitfile)) {
|
2001-02-28 15:57:38 +00:00
|
|
|
if (!fgets(line, sizeof(line), unitfile))
|
1996-06-08 03:43:43 +00:00
|
|
|
break;
|
|
|
|
linenum++;
|
|
|
|
lineptr = line;
|
|
|
|
if (*lineptr == '/')
|
|
|
|
continue;
|
|
|
|
lineptr += strspn(lineptr, " \n\t");
|
|
|
|
len = strcspn(lineptr, " \n\t");
|
|
|
|
lineptr[len] = 0;
|
|
|
|
if (!strlen(lineptr))
|
|
|
|
continue;
|
|
|
|
if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
|
|
|
|
if (prefixcount == MAXPREFIXES) {
|
1997-08-21 06:49:33 +00:00
|
|
|
warnx("memory for prefixes exceeded in line %d", linenum);
|
1996-06-08 03:43:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lineptr[strlen(lineptr) - 1] = 0;
|
|
|
|
prefixtable[prefixcount].prefixname = dupstr(lineptr);
|
|
|
|
for (i = 0; i < prefixcount; i++)
|
|
|
|
if (!strcmp(prefixtable[i].prefixname, lineptr)) {
|
1997-08-21 06:49:33 +00:00
|
|
|
warnx("redefinition of prefix '%s' on line %d ignored",
|
1996-06-08 03:43:43 +00:00
|
|
|
lineptr, linenum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lineptr += len + 1;
|
|
|
|
lineptr += strspn(lineptr, " \n\t");
|
|
|
|
len = strcspn(lineptr, "\n\t");
|
2001-02-28 15:57:38 +00:00
|
|
|
if (len == 0) {
|
|
|
|
warnx("unexpected end of prefix on line %d",
|
|
|
|
linenum);
|
|
|
|
continue;
|
|
|
|
}
|
1996-06-08 03:43:43 +00:00
|
|
|
lineptr[len] = 0;
|
|
|
|
prefixtable[prefixcount++].prefixval = dupstr(lineptr);
|
|
|
|
}
|
|
|
|
else { /* it's not a prefix */
|
|
|
|
if (unitcount == MAXUNITS) {
|
1997-08-21 06:49:33 +00:00
|
|
|
warnx("memory for units exceeded in line %d", linenum);
|
1996-06-08 03:43:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unittable[unitcount].uname = dupstr(lineptr);
|
|
|
|
for (i = 0; i < unitcount; i++)
|
|
|
|
if (!strcmp(unittable[i].uname, lineptr)) {
|
1997-08-21 06:49:33 +00:00
|
|
|
warnx("redefinition of unit '%s' on line %d ignored",
|
1996-06-08 03:43:43 +00:00
|
|
|
lineptr, linenum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lineptr += len + 1;
|
|
|
|
lineptr += strspn(lineptr, " \n\t");
|
|
|
|
if (!strlen(lineptr)) {
|
2001-02-28 15:57:38 +00:00
|
|
|
warnx("unexpected end of unit on line %d",
|
|
|
|
linenum);
|
1996-06-08 03:43:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len = strcspn(lineptr, "\n\t");
|
|
|
|
lineptr[len] = 0;
|
|
|
|
unittable[unitcount++].uval = dupstr(lineptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(unitfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initializeunit(struct unittype * theunit)
|
|
|
|
{
|
|
|
|
theunit->numerator[0] = theunit->denominator[0] = NULL;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->factor = 1.0;
|
|
|
|
theunit->offset = 0.0;
|
|
|
|
theunit->quantity = 0;
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
addsubunit(char *product[], char *toadd)
|
|
|
|
{
|
|
|
|
char **ptr;
|
|
|
|
|
|
|
|
for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
|
|
|
|
if (ptr >= product + MAXSUBUNITS) {
|
1997-08-21 06:49:33 +00:00
|
|
|
warnx("memory overflow in unit reduction");
|
1996-06-08 03:43:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!*ptr)
|
2014-04-14 21:09:47 +00:00
|
|
|
*(ptr + 1) = NULL;
|
1996-06-08 03:43:43 +00:00
|
|
|
*ptr = dupstr(toadd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
showunit(struct unittype * theunit)
|
|
|
|
{
|
|
|
|
char **ptr;
|
|
|
|
int printedslash;
|
|
|
|
int counter = 1;
|
|
|
|
|
2014-04-14 20:51:04 +00:00
|
|
|
printf("%.8g", theunit->factor);
|
2008-08-16 16:27:41 +00:00
|
|
|
if (theunit->offset)
|
|
|
|
printf("&%.8g", theunit->offset);
|
1996-06-08 03:43:43 +00:00
|
|
|
for (ptr = theunit->numerator; *ptr; ptr++) {
|
|
|
|
if (ptr > theunit->numerator && **ptr &&
|
|
|
|
!strcmp(*ptr, *(ptr - 1)))
|
|
|
|
counter++;
|
|
|
|
else {
|
|
|
|
if (counter > 1)
|
|
|
|
printf("%s%d", powerstring, counter);
|
|
|
|
if (**ptr)
|
|
|
|
printf(" %s", *ptr);
|
|
|
|
counter = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (counter > 1)
|
|
|
|
printf("%s%d", powerstring, counter);
|
|
|
|
counter = 1;
|
|
|
|
printedslash = 0;
|
|
|
|
for (ptr = theunit->denominator; *ptr; ptr++) {
|
|
|
|
if (ptr > theunit->denominator && **ptr &&
|
|
|
|
!strcmp(*ptr, *(ptr - 1)))
|
|
|
|
counter++;
|
|
|
|
else {
|
|
|
|
if (counter > 1)
|
|
|
|
printf("%s%d", powerstring, counter);
|
|
|
|
if (**ptr) {
|
|
|
|
if (!printedslash)
|
|
|
|
printf(" /");
|
|
|
|
printedslash = 1;
|
|
|
|
printf(" %s", *ptr);
|
|
|
|
}
|
|
|
|
counter = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 05:35:00 +00:00
|
|
|
if (counter > 1)
|
1996-06-08 03:43:43 +00:00
|
|
|
printf("%s%d", powerstring, counter);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2001-02-28 15:57:38 +00:00
|
|
|
zeroerror(void)
|
1996-06-08 03:43:43 +00:00
|
|
|
{
|
1997-08-21 06:49:33 +00:00
|
|
|
warnx("unit reduces to zero");
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Adds the specified string to the unit.
|
|
|
|
Flip is 0 for adding normally, 1 for adding reciprocal.
|
2008-08-16 16:27:41 +00:00
|
|
|
Quantity is 1 if this is a quantity to be converted rather than a pure unit.
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
Returns 0 for successful addition, nonzero on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2014-04-14 14:04:59 +00:00
|
|
|
addunit(struct unittype * theunit, const char *toadd, int flip, int quantity)
|
1996-06-08 03:43:43 +00:00
|
|
|
{
|
|
|
|
char *scratch, *savescr;
|
|
|
|
char *item;
|
2008-08-16 16:27:41 +00:00
|
|
|
char *divider, *slash, *offset;
|
1996-06-08 03:43:43 +00:00
|
|
|
int doingtop;
|
|
|
|
|
2000-07-31 10:49:08 +00:00
|
|
|
if (!strlen(toadd))
|
|
|
|
return 1;
|
|
|
|
|
1996-06-08 03:43:43 +00:00
|
|
|
savescr = scratch = dupstr(toadd);
|
|
|
|
for (slash = scratch + 1; *slash; slash++)
|
|
|
|
if (*slash == '-' &&
|
|
|
|
(tolower(*(slash - 1)) != 'e' ||
|
|
|
|
!strchr(".0123456789", *(slash + 1))))
|
|
|
|
*slash = ' ';
|
|
|
|
slash = strchr(scratch, '/');
|
|
|
|
if (slash)
|
|
|
|
*slash = 0;
|
|
|
|
doingtop = 1;
|
|
|
|
do {
|
|
|
|
item = strtok(scratch, " *\t\n/");
|
|
|
|
while (item) {
|
|
|
|
if (strchr("0123456789.", *item)) { /* item is a number */
|
2008-08-16 16:27:41 +00:00
|
|
|
double num, offsetnum;
|
|
|
|
|
|
|
|
if (quantity)
|
|
|
|
theunit->quantity = 1;
|
|
|
|
|
|
|
|
offset = strchr(item, '&');
|
|
|
|
if (offset) {
|
|
|
|
*offset = 0;
|
|
|
|
offsetnum = atof(offset+1);
|
|
|
|
} else
|
|
|
|
offsetnum = 0.0;
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
divider = strchr(item, '|');
|
|
|
|
if (divider) {
|
|
|
|
*divider = 0;
|
|
|
|
num = atof(item);
|
|
|
|
if (!num) {
|
|
|
|
zeroerror();
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-16 16:27:41 +00:00
|
|
|
if (doingtop ^ flip) {
|
1996-06-08 03:43:43 +00:00
|
|
|
theunit->factor *= num;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->offset *= num;
|
|
|
|
} else {
|
1996-06-08 03:43:43 +00:00
|
|
|
theunit->factor /= num;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->offset /= num;
|
|
|
|
}
|
1996-06-08 03:43:43 +00:00
|
|
|
num = atof(divider + 1);
|
|
|
|
if (!num) {
|
|
|
|
zeroerror();
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-16 16:27:41 +00:00
|
|
|
if (doingtop ^ flip) {
|
1996-06-08 03:43:43 +00:00
|
|
|
theunit->factor /= num;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->offset /= num;
|
|
|
|
} else {
|
1996-06-08 03:43:43 +00:00
|
|
|
theunit->factor *= num;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->offset *= num;
|
|
|
|
}
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
num = atof(item);
|
|
|
|
if (!num) {
|
|
|
|
zeroerror();
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-16 16:27:41 +00:00
|
|
|
if (doingtop ^ flip) {
|
1996-06-08 03:43:43 +00:00
|
|
|
theunit->factor *= num;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->offset *= num;
|
|
|
|
} else {
|
1996-06-08 03:43:43 +00:00
|
|
|
theunit->factor /= num;
|
2008-08-16 16:27:41 +00:00
|
|
|
theunit->offset /= num;
|
|
|
|
}
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
2008-08-16 16:27:41 +00:00
|
|
|
if (doingtop ^ flip)
|
|
|
|
theunit->offset += offsetnum;
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
else { /* item is not a number */
|
|
|
|
int repeat = 1;
|
|
|
|
|
|
|
|
if (strchr("23456789",
|
|
|
|
item[strlen(item) - 1])) {
|
|
|
|
repeat = item[strlen(item) - 1] - '0';
|
|
|
|
item[strlen(item) - 1] = 0;
|
|
|
|
}
|
|
|
|
for (; repeat; repeat--)
|
|
|
|
if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
item = strtok(NULL, " *\t/\n");
|
|
|
|
}
|
|
|
|
doingtop--;
|
|
|
|
if (slash) {
|
|
|
|
scratch = slash + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
doingtop--;
|
|
|
|
} while (doingtop >= 0);
|
|
|
|
free(savescr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
compare(const void *item1, const void *item2)
|
|
|
|
{
|
2002-07-28 16:23:28 +00:00
|
|
|
return strcmp(*(const char * const *)item1, *(const char * const *)item2);
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
sortunit(struct unittype * theunit)
|
|
|
|
{
|
|
|
|
char **ptr;
|
2001-02-28 15:57:38 +00:00
|
|
|
unsigned int count;
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
|
|
|
|
qsort(theunit->numerator, count, sizeof(char *), compare);
|
|
|
|
for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
|
|
|
|
qsort(theunit->denominator, count, sizeof(char *), compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
cancelunit(struct unittype * theunit)
|
|
|
|
{
|
|
|
|
char **den, **num;
|
|
|
|
int comp;
|
|
|
|
|
|
|
|
den = theunit->denominator;
|
|
|
|
num = theunit->numerator;
|
|
|
|
|
|
|
|
while (*num && *den) {
|
|
|
|
comp = strcmp(*den, *num);
|
|
|
|
if (!comp) {
|
|
|
|
/* if (*den!=NULLUNIT) free(*den);
|
|
|
|
if (*num!=NULLUNIT) free(*num);*/
|
|
|
|
*den++ = NULLUNIT;
|
|
|
|
*num++ = NULLUNIT;
|
|
|
|
}
|
|
|
|
else if (comp < 0)
|
|
|
|
den++;
|
|
|
|
else
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Looks up the definition for the specified unit.
|
|
|
|
Returns a pointer to the definition or a null pointer
|
|
|
|
if the specified unit does not appear in the units table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char buffer[100]; /* buffer for lookupunit answers with
|
|
|
|
prefixes */
|
|
|
|
|
|
|
|
char *
|
2001-02-28 15:57:38 +00:00
|
|
|
lookupunit(const char *unit)
|
1996-06-08 03:43:43 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *copy;
|
|
|
|
|
|
|
|
for (i = 0; i < unitcount; i++) {
|
|
|
|
if (!strcmp(unittable[i].uname, unit))
|
|
|
|
return unittable[i].uval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unit[strlen(unit) - 1] == '^') {
|
|
|
|
copy = dupstr(unit);
|
|
|
|
copy[strlen(copy) - 1] = 0;
|
|
|
|
for (i = 0; i < unitcount; i++) {
|
|
|
|
if (!strcmp(unittable[i].uname, copy)) {
|
2000-07-31 10:49:08 +00:00
|
|
|
strlcpy(buffer, copy, sizeof(buffer));
|
1996-06-08 03:43:43 +00:00
|
|
|
free(copy);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(copy);
|
|
|
|
}
|
|
|
|
if (unit[strlen(unit) - 1] == 's') {
|
|
|
|
copy = dupstr(unit);
|
|
|
|
copy[strlen(copy) - 1] = 0;
|
|
|
|
for (i = 0; i < unitcount; i++) {
|
|
|
|
if (!strcmp(unittable[i].uname, copy)) {
|
2000-07-31 10:49:08 +00:00
|
|
|
strlcpy(buffer, copy, sizeof(buffer));
|
1996-06-08 03:43:43 +00:00
|
|
|
free(copy);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (copy[strlen(copy) - 1] == 'e') {
|
|
|
|
copy[strlen(copy) - 1] = 0;
|
|
|
|
for (i = 0; i < unitcount; i++) {
|
|
|
|
if (!strcmp(unittable[i].uname, copy)) {
|
2000-07-31 10:49:08 +00:00
|
|
|
strlcpy(buffer, copy, sizeof(buffer));
|
1996-06-08 03:43:43 +00:00
|
|
|
free(copy);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(copy);
|
|
|
|
}
|
|
|
|
for (i = 0; i < prefixcount; i++) {
|
2001-02-28 15:57:38 +00:00
|
|
|
size_t len = strlen(prefixtable[i].prefixname);
|
|
|
|
if (!strncmp(prefixtable[i].prefixname, unit, len)) {
|
|
|
|
if (!strlen(unit + len) || lookupunit(unit + len)) {
|
2000-07-31 10:49:08 +00:00
|
|
|
snprintf(buffer, sizeof(buffer), "%s %s",
|
2001-02-28 15:57:38 +00:00
|
|
|
prefixtable[i].prefixval, unit + len);
|
1996-06-08 03:43:43 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
reduces a product of symbolic units to primitive units.
|
|
|
|
The three low bits are used to return flags:
|
|
|
|
|
|
|
|
bit 0 (1) set on if reductions were performed without error.
|
|
|
|
bit 1 (2) set on if no reductions are performed.
|
|
|
|
bit 2 (4) set on if an unknown unit is discovered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define ERROR 4
|
|
|
|
|
|
|
|
int
|
|
|
|
reduceproduct(struct unittype * theunit, int flip)
|
|
|
|
{
|
|
|
|
|
|
|
|
char *toadd;
|
|
|
|
char **product;
|
|
|
|
int didsomething = 2;
|
|
|
|
|
|
|
|
if (flip)
|
|
|
|
product = theunit->denominator;
|
|
|
|
else
|
|
|
|
product = theunit->numerator;
|
|
|
|
|
|
|
|
for (; *product; product++) {
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!strlen(*product))
|
|
|
|
break;
|
|
|
|
toadd = lookupunit(*product);
|
|
|
|
if (!toadd) {
|
|
|
|
printf("unknown unit '%s'\n", *product);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
if (strchr(toadd, PRIMITIVECHAR))
|
|
|
|
break;
|
|
|
|
didsomething = 1;
|
|
|
|
if (*product != NULLUNIT) {
|
|
|
|
free(*product);
|
|
|
|
*product = NULLUNIT;
|
|
|
|
}
|
2008-08-16 16:27:41 +00:00
|
|
|
if (addunit(theunit, toadd, flip, 0))
|
1996-06-08 03:43:43 +00:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return didsomething;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Reduces numerator and denominator of the specified unit.
|
|
|
|
Returns 0 on success, or 1 on unknown unit error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
reduceunit(struct unittype * theunit)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
while (ret & 1) {
|
|
|
|
ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
|
|
|
|
if (ret & 4)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
compareproducts(char **one, char **two)
|
|
|
|
{
|
|
|
|
while (*one || *two) {
|
|
|
|
if (!*one && *two != NULLUNIT)
|
|
|
|
return 1;
|
|
|
|
if (!*two && *one != NULLUNIT)
|
|
|
|
return 1;
|
|
|
|
if (*one == NULLUNIT)
|
|
|
|
one++;
|
|
|
|
else if (*two == NULLUNIT)
|
|
|
|
two++;
|
|
|
|
else if (strcmp(*one, *two))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
one++, two++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return zero if units are compatible, nonzero otherwise */
|
|
|
|
|
|
|
|
int
|
|
|
|
compareunits(struct unittype * first, struct unittype * second)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
compareproducts(first->numerator, second->numerator) ||
|
|
|
|
compareproducts(first->denominator, second->denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
completereduce(struct unittype * unit)
|
|
|
|
{
|
|
|
|
if (reduceunit(unit))
|
|
|
|
return 1;
|
|
|
|
sortunit(unit);
|
|
|
|
cancelunit(unit);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
showanswer(struct unittype * have, struct unittype * want)
|
|
|
|
{
|
2014-04-14 20:51:04 +00:00
|
|
|
double ans;
|
|
|
|
|
1996-06-08 03:43:43 +00:00
|
|
|
if (compareunits(have, want)) {
|
|
|
|
printf("conformability error\n");
|
2014-04-14 20:51:04 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("\t%s = ", havestr);
|
|
|
|
else
|
|
|
|
printf("\t");
|
1996-06-08 03:43:43 +00:00
|
|
|
showunit(have);
|
2014-04-14 20:51:04 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("\t%s = ", wantstr);
|
|
|
|
else
|
|
|
|
printf("\t");
|
1996-06-08 03:43:43 +00:00
|
|
|
showunit(want);
|
|
|
|
}
|
2008-08-16 16:27:41 +00:00
|
|
|
else if (have->offset != want->offset) {
|
|
|
|
if (want->quantity)
|
|
|
|
printf("WARNING: conversion of non-proportional quantities.\n");
|
|
|
|
if (have->quantity)
|
2014-04-14 20:51:04 +00:00
|
|
|
printf("\t%.8g\n",
|
2008-08-16 16:27:41 +00:00
|
|
|
(have->factor + have->offset-want->offset)/want->factor);
|
2014-04-14 20:51:04 +00:00
|
|
|
else {
|
|
|
|
printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
|
2008-08-16 16:27:41 +00:00
|
|
|
have->factor / want->factor,
|
|
|
|
(have->offset-want->offset)/want->factor,
|
|
|
|
want->factor / have->factor,
|
|
|
|
(want->offset - have->offset)/have->factor);
|
2014-04-14 20:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ans = have->factor / want->factor;
|
|
|
|
if (verbose)
|
|
|
|
printf("\t%s = %.8g * %s\n", havestr, ans, wantstr);
|
|
|
|
else
|
|
|
|
printf("\t* %.8g\n", ans);
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr);
|
|
|
|
else
|
|
|
|
printf("\t/ %.8g\n", 1/ans);
|
2008-08-16 16:27:41 +00:00
|
|
|
}
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2001-02-28 15:57:38 +00:00
|
|
|
usage(void)
|
1996-06-08 03:43:43 +00:00
|
|
|
{
|
1997-08-21 06:49:33 +00:00
|
|
|
fprintf(stderr,
|
2014-04-14 16:55:09 +00:00
|
|
|
"usage: units [-f unitsfile] [-UVq] [from-unit to-unit]\n");
|
1996-06-08 03:43:43 +00:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:21:05 +00:00
|
|
|
static struct option longopts[] = {
|
|
|
|
{"file", required_argument, NULL, 'f'},
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{"unitsfile", no_argument, NULL, 'U'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct unittype have, want;
|
|
|
|
int optchar;
|
2014-04-14 14:04:59 +00:00
|
|
|
bool quiet;
|
2014-04-14 16:43:36 +00:00
|
|
|
bool readfile;
|
2014-04-14 14:04:59 +00:00
|
|
|
History *inhistory;
|
|
|
|
EditLine *el;
|
|
|
|
HistEvent ev;
|
|
|
|
int inputsz;
|
|
|
|
|
|
|
|
quiet = false;
|
2014-04-14 16:43:36 +00:00
|
|
|
readfile = false;
|
2014-07-04 21:21:05 +00:00
|
|
|
while ((optchar = getopt_long(argc, argv, "+f:qvUV", longopts, NULL)) != -1) {
|
1996-06-08 03:43:43 +00:00
|
|
|
switch (optchar) {
|
|
|
|
case 'f':
|
2014-04-14 16:43:36 +00:00
|
|
|
readfile = true;
|
|
|
|
if (strlen(optarg) == 0)
|
|
|
|
readunits(NULL);
|
|
|
|
else
|
|
|
|
readunits(optarg);
|
1996-06-08 03:43:43 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
2014-04-14 14:04:59 +00:00
|
|
|
quiet = true;
|
1996-06-08 03:43:43 +00:00
|
|
|
break;
|
2014-04-14 20:51:04 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
2014-05-12 06:14:14 +00:00
|
|
|
case 'V':
|
|
|
|
fprintf(stderr, "FreeBSD units\n");
|
|
|
|
/* FALLTHROUGH */
|
2014-04-14 16:55:09 +00:00
|
|
|
case 'U':
|
|
|
|
if (access(UNITSFILE, F_OK) == 0)
|
|
|
|
printf("%s\n", UNITSFILE);
|
|
|
|
else
|
|
|
|
printf("Units data file not found");
|
|
|
|
exit(0);
|
|
|
|
break;
|
1996-06-08 03:43:43 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 16:43:36 +00:00
|
|
|
if (!readfile)
|
|
|
|
readunits(NULL);
|
1996-06-08 03:43:43 +00:00
|
|
|
|
2014-04-14 14:04:59 +00:00
|
|
|
inhistory = history_init();
|
|
|
|
el = el_init(argv[0], stdin, stdout, stderr);
|
|
|
|
el_set(el, EL_PROMPT, &prompt);
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
|
|
|
el_set(el, EL_SIGNAL, 1);
|
|
|
|
el_set(el, EL_HIST, history, inhistory);
|
2014-04-14 14:40:12 +00:00
|
|
|
el_source(el, NULL);
|
2014-04-14 14:04:59 +00:00
|
|
|
history(inhistory, &ev, H_SETSIZE, 800);
|
|
|
|
if (inhistory == 0)
|
2014-05-15 05:35:00 +00:00
|
|
|
err(1, "Could not initialize history");
|
2014-04-14 16:43:36 +00:00
|
|
|
|
|
|
|
if (cap_enter() < 0 && errno != ENOSYS)
|
|
|
|
err(1, "unable to enter capability mode");
|
1996-06-08 03:43:43 +00:00
|
|
|
|
|
|
|
if (optind == argc - 2) {
|
2014-04-14 14:04:59 +00:00
|
|
|
havestr = argv[optind];
|
|
|
|
wantstr = argv[optind + 1];
|
1996-06-08 03:43:43 +00:00
|
|
|
initializeunit(&have);
|
2008-08-16 16:27:41 +00:00
|
|
|
addunit(&have, havestr, 0, 1);
|
1996-06-08 03:43:43 +00:00
|
|
|
completereduce(&have);
|
|
|
|
initializeunit(&want);
|
2008-08-16 16:27:41 +00:00
|
|
|
addunit(&want, wantstr, 0, 1);
|
1996-06-08 03:43:43 +00:00
|
|
|
completereduce(&want);
|
|
|
|
showanswer(&have, &want);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!quiet)
|
1997-08-21 06:49:33 +00:00
|
|
|
printf("%d units, %d prefixes\n", unitcount,
|
1996-06-08 03:43:43 +00:00
|
|
|
prefixcount);
|
|
|
|
for (;;) {
|
|
|
|
do {
|
|
|
|
initializeunit(&have);
|
|
|
|
if (!quiet)
|
2014-04-14 14:04:59 +00:00
|
|
|
promptstr = "You have: ";
|
|
|
|
havestr = el_gets(el, &inputsz);
|
|
|
|
if (havestr == NULL)
|
1996-06-08 03:43:43 +00:00
|
|
|
exit(0);
|
2014-04-14 14:04:59 +00:00
|
|
|
if (inputsz > 0)
|
|
|
|
history(inhistory, &ev, H_ENTER,
|
|
|
|
havestr);
|
2008-08-16 16:27:41 +00:00
|
|
|
} while (addunit(&have, havestr, 0, 1) ||
|
1996-06-08 03:43:43 +00:00
|
|
|
completereduce(&have));
|
|
|
|
do {
|
|
|
|
initializeunit(&want);
|
|
|
|
if (!quiet)
|
2014-04-14 14:04:59 +00:00
|
|
|
promptstr = "You want: ";
|
|
|
|
wantstr = el_gets(el, &inputsz);
|
|
|
|
if (wantstr == NULL)
|
1996-06-08 03:43:43 +00:00
|
|
|
exit(0);
|
2014-04-14 14:04:59 +00:00
|
|
|
if (inputsz > 0)
|
|
|
|
history(inhistory, &ev, H_ENTER,
|
|
|
|
wantstr);
|
2008-08-16 16:27:41 +00:00
|
|
|
} while (addunit(&want, wantstr, 0, 1) ||
|
1996-06-08 03:43:43 +00:00
|
|
|
completereduce(&want));
|
|
|
|
showanswer(&have, &want);
|
|
|
|
}
|
|
|
|
}
|
1996-06-08 04:30:06 +00:00
|
|
|
|
2014-04-14 14:04:59 +00:00
|
|
|
history_end(inhistory);
|
2014-05-12 07:14:09 +00:00
|
|
|
el_end(el);
|
1996-06-08 04:30:06 +00:00
|
|
|
return(0);
|
1996-06-08 03:43:43 +00:00
|
|
|
}
|