Home | History | Annotate | Line # | Download | only in units
units.c revision 1.16
      1  1.16     lukem /*	$NetBSD: units.c,v 1.16 2009/04/14 05:55:12 lukem Exp $	*/
      2   1.6   thorpej 
      3   1.1       cgd /*
      4   1.1       cgd  * units.c   Copyright (c) 1993 by Adrian Mariano (adrian (at) cam.cornell.edu)
      5   1.1       cgd  *
      6   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      7   1.1       cgd  * modification, are permitted provided that the following conditions
      8   1.1       cgd  * are met:
      9   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     10   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     11   1.1       cgd  * 2. The name of the author may not be used to endorse or promote products
     12   1.2       jtc  *    derived from this software without specific prior written permission.
     13   1.1       cgd  * Disclaimer:  This software is provided by the author "as is".  The author
     14   1.1       cgd  * shall not be liable for any damages caused in any way by this software.
     15   1.1       cgd  *
     16   1.1       cgd  * I would appreciate (though I do not require) receiving a copy of any
     17   1.1       cgd  * improvements you might make to this program.
     18   1.1       cgd  */
     19   1.1       cgd 
     20   1.3       jtc #include <ctype.h>
     21   1.7     lukem #include <err.h>
     22   1.1       cgd #include <stdio.h>
     23   1.1       cgd #include <string.h>
     24   1.1       cgd #include <stdlib.h>
     25   1.8     perry #include <unistd.h>
     26   1.1       cgd 
     27   1.1       cgd #include "pathnames.h"
     28   1.1       cgd 
     29   1.1       cgd #define VERSION "1.0"
     30   1.1       cgd 
     31   1.1       cgd #ifndef UNITSFILE
     32   1.1       cgd #define UNITSFILE _PATH_UNITSLIB
     33   1.1       cgd #endif
     34   1.1       cgd 
     35   1.1       cgd #define MAXUNITS 1000
     36   1.1       cgd #define MAXPREFIXES 50
     37   1.1       cgd 
     38   1.1       cgd #define MAXSUBUNITS 500
     39   1.1       cgd 
     40   1.1       cgd #define PRIMITIVECHAR '!'
     41   1.1       cgd 
     42  1.16     lukem const char *powerstring = "^";
     43   1.1       cgd 
     44   1.1       cgd struct {
     45  1.16     lukem 	const char *uname;
     46  1.16     lukem 	const char *uval;
     47   1.1       cgd }      unittable[MAXUNITS];
     48   1.1       cgd 
     49   1.1       cgd struct unittype {
     50  1.16     lukem 	const char *numerator[MAXSUBUNITS];
     51  1.16     lukem 	const char *denominator[MAXSUBUNITS];
     52   1.1       cgd 	double factor;
     53   1.1       cgd };
     54   1.1       cgd 
     55   1.1       cgd struct {
     56  1.16     lukem 	const char *prefixname;
     57  1.16     lukem 	const char *prefixval;
     58   1.1       cgd }      prefixtable[MAXPREFIXES];
     59   1.1       cgd 
     60   1.1       cgd 
     61  1.16     lukem const char *NULLUNIT = "";
     62   1.1       cgd 
     63   1.1       cgd int unitcount;
     64   1.1       cgd int prefixcount;
     65   1.1       cgd 
     66   1.1       cgd 
     67  1.16     lukem int	addsubunit __P((const char *[], const char *));
     68  1.16     lukem int	addunit __P((struct unittype *, const char *, int));
     69   1.7     lukem void	cancelunit __P((struct unittype *));
     70   1.7     lukem int	compare __P((const void *, const void *));
     71  1.16     lukem int	compareproducts __P((const char **, const char **));
     72   1.7     lukem int	compareunits __P((struct unittype *, struct unittype *));
     73  1.13   mycroft int	compareunitsreciprocal __P((struct unittype *, struct unittype *));
     74   1.7     lukem int	completereduce __P((struct unittype *));
     75   1.7     lukem void	initializeunit __P((struct unittype *));
     76   1.7     lukem int	main __P((int, char **));
     77   1.7     lukem void	readerror __P((int));
     78  1.16     lukem void	readunits __P((const char *));
     79   1.7     lukem int	reduceproduct __P((struct unittype *, int));
     80   1.7     lukem int	reduceunit __P((struct unittype *));
     81   1.7     lukem void	showanswer __P((struct unittype *, struct unittype *));
     82   1.7     lukem void	showunit __P((struct unittype *));
     83   1.7     lukem void	sortunit __P((struct unittype *));
     84   1.7     lukem void	usage __P((void));
     85   1.7     lukem void	zeroerror __P((void));
     86  1.16     lukem char   *dupstr __P((const char *));
     87  1.16     lukem const char *lookupunit __P((const char *));
     88   1.7     lukem 
     89   1.7     lukem 
     90   1.1       cgd char *
     91  1.16     lukem dupstr(const char *str)
     92   1.1       cgd {
     93   1.1       cgd 	char *ret;
     94   1.1       cgd 
     95  1.12    itojun 	ret = strdup(str);
     96   1.7     lukem 	if (!ret)
     97   1.7     lukem 		err(3, "Memory allocation error");
     98   1.1       cgd 	return (ret);
     99   1.1       cgd }
    100   1.1       cgd 
    101   1.1       cgd 
    102   1.1       cgd void
    103   1.1       cgd readerror(int linenum)
    104   1.1       cgd {
    105   1.7     lukem 	warnx("Error in units file '%s' line %d", UNITSFILE, linenum);
    106   1.1       cgd }
    107   1.1       cgd 
    108   1.1       cgd 
    109   1.1       cgd void
    110  1.16     lukem readunits(const char *userfile)
    111   1.1       cgd {
    112   1.1       cgd 	FILE *unitfile;
    113   1.1       cgd 	char line[80], *lineptr;
    114   1.1       cgd 	int len, linenum, i;
    115   1.1       cgd 
    116   1.1       cgd 	unitcount = 0;
    117   1.1       cgd 	linenum = 0;
    118   1.1       cgd 
    119   1.1       cgd 	if (userfile) {
    120   1.1       cgd 		unitfile = fopen(userfile, "rt");
    121   1.7     lukem 		if (!unitfile)
    122   1.7     lukem 			err(1, "Unable to open units file '%s'", userfile);
    123   1.1       cgd 	}
    124   1.1       cgd 	else {
    125   1.1       cgd 		unitfile = fopen(UNITSFILE, "rt");
    126   1.1       cgd 		if (!unitfile) {
    127   1.1       cgd 			char *direc, *env;
    128   1.1       cgd 			char filename[1000];
    129   1.1       cgd 			char separator[2];
    130   1.1       cgd 
    131   1.1       cgd 			env = getenv("PATH");
    132   1.1       cgd 			if (env) {
    133   1.1       cgd 				if (strchr(env, ';'))
    134  1.12    itojun 					strlcpy(separator, ";",
    135  1.12    itojun 					    sizeof(separator));
    136   1.1       cgd 				else
    137  1.12    itojun 					strlcpy(separator, ":",
    138  1.12    itojun 					    sizeof(separator));
    139   1.1       cgd 				direc = strtok(env, separator);
    140   1.1       cgd 				while (direc) {
    141  1.12    itojun 					strlcpy(filename, "", sizeof(filename));
    142  1.12    itojun 					strlcat(filename, direc,
    143  1.12    itojun 					    sizeof(filename));
    144  1.12    itojun 					strlcat(filename, "/",
    145  1.12    itojun 					    sizeof(filename));
    146  1.12    itojun 					strlcat(filename, UNITSFILE,
    147  1.12    itojun 					    sizeof(filename));
    148   1.1       cgd 					unitfile = fopen(filename, "rt");
    149   1.1       cgd 					if (unitfile)
    150   1.1       cgd 						break;
    151   1.1       cgd 					direc = strtok(NULL, separator);
    152   1.1       cgd 				}
    153   1.1       cgd 			}
    154   1.7     lukem 			if (!unitfile)
    155   1.7     lukem 				errx(1, "Can't find units file '%s'",
    156   1.1       cgd 				    UNITSFILE);
    157   1.1       cgd 		}
    158   1.1       cgd 	}
    159   1.1       cgd 	while (!feof(unitfile)) {
    160   1.1       cgd 		if (!fgets(line, 79, unitfile))
    161   1.1       cgd 			break;
    162   1.1       cgd 		linenum++;
    163   1.1       cgd 		lineptr = line;
    164   1.1       cgd 		if (*lineptr == '/')
    165   1.1       cgd 			continue;
    166   1.1       cgd 		lineptr += strspn(lineptr, " \n\t");
    167   1.1       cgd 		len = strcspn(lineptr, " \n\t");
    168   1.1       cgd 		lineptr[len] = 0;
    169   1.1       cgd 		if (!strlen(lineptr))
    170   1.1       cgd 			continue;
    171   1.1       cgd 		if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
    172   1.1       cgd 			if (prefixcount == MAXPREFIXES) {
    173   1.7     lukem 				warnx("Memory for prefixes exceeded in line %d",
    174   1.1       cgd 				    linenum);
    175   1.1       cgd 				continue;
    176   1.1       cgd 			}
    177   1.1       cgd 			lineptr[strlen(lineptr) - 1] = 0;
    178   1.1       cgd 			prefixtable[prefixcount].prefixname = dupstr(lineptr);
    179   1.1       cgd 			for (i = 0; i < prefixcount; i++)
    180   1.1       cgd 				if (!strcmp(prefixtable[i].prefixname, lineptr)) {
    181   1.7     lukem 					warnx(
    182   1.7     lukem 			"Redefinition of prefix '%s' on line %d ignored",
    183   1.1       cgd 					    lineptr, linenum);
    184   1.1       cgd 					continue;
    185   1.1       cgd 				}
    186   1.1       cgd 			lineptr += len + 1;
    187   1.1       cgd 			if (!strlen(lineptr)) {
    188   1.1       cgd 				readerror(linenum);
    189   1.1       cgd 				continue;
    190   1.1       cgd 			}
    191   1.1       cgd 			lineptr += strspn(lineptr, " \n\t");
    192   1.1       cgd 			len = strcspn(lineptr, "\n\t");
    193   1.1       cgd 			lineptr[len] = 0;
    194   1.1       cgd 			prefixtable[prefixcount++].prefixval = dupstr(lineptr);
    195   1.1       cgd 		}
    196   1.1       cgd 		else {		/* it's not a prefix */
    197   1.1       cgd 			if (unitcount == MAXUNITS) {
    198   1.7     lukem 				warnx("Memory for units exceeded in line %d",
    199   1.1       cgd 				    linenum);
    200   1.1       cgd 				continue;
    201   1.1       cgd 			}
    202   1.1       cgd 			unittable[unitcount].uname = dupstr(lineptr);
    203   1.1       cgd 			for (i = 0; i < unitcount; i++)
    204   1.1       cgd 				if (!strcmp(unittable[i].uname, lineptr)) {
    205   1.7     lukem 					warnx(
    206   1.7     lukem 				"Redefinition of unit '%s' on line %d ignored",
    207   1.1       cgd 					    lineptr, linenum);
    208   1.1       cgd 					continue;
    209   1.1       cgd 				}
    210   1.1       cgd 			lineptr += len + 1;
    211   1.1       cgd 			lineptr += strspn(lineptr, " \n\t");
    212   1.1       cgd 			if (!strlen(lineptr)) {
    213   1.1       cgd 				readerror(linenum);
    214   1.1       cgd 				continue;
    215   1.1       cgd 			}
    216   1.1       cgd 			len = strcspn(lineptr, "\n\t");
    217   1.1       cgd 			lineptr[len] = 0;
    218   1.1       cgd 			unittable[unitcount++].uval = dupstr(lineptr);
    219   1.1       cgd 		}
    220   1.1       cgd 	}
    221   1.1       cgd 	fclose(unitfile);
    222   1.1       cgd }
    223   1.1       cgd 
    224   1.1       cgd void
    225   1.1       cgd initializeunit(struct unittype * theunit)
    226   1.1       cgd {
    227   1.1       cgd 	theunit->factor = 1.0;
    228   1.1       cgd 	theunit->numerator[0] = theunit->denominator[0] = NULL;
    229   1.1       cgd }
    230   1.1       cgd 
    231   1.1       cgd 
    232   1.1       cgd int
    233  1.16     lukem addsubunit(const char *product[], const char *toadd)
    234   1.1       cgd {
    235  1.16     lukem 	const char **ptr;
    236   1.1       cgd 
    237   1.1       cgd 	for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
    238   1.1       cgd 	if (ptr >= product + MAXSUBUNITS) {
    239   1.7     lukem 		warnx("Memory overflow in unit reduction");
    240   1.1       cgd 		return 1;
    241   1.1       cgd 	}
    242   1.1       cgd 	if (!*ptr)
    243   1.1       cgd 		*(ptr + 1) = 0;
    244   1.1       cgd 	*ptr = dupstr(toadd);
    245   1.1       cgd 	return 0;
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.1       cgd 
    249   1.1       cgd void
    250   1.1       cgd showunit(struct unittype * theunit)
    251   1.1       cgd {
    252  1.16     lukem 	const char **ptr;
    253   1.1       cgd 	int printedslash;
    254   1.1       cgd 	int counter = 1;
    255   1.1       cgd 
    256   1.1       cgd 	printf("\t%.8g", theunit->factor);
    257   1.1       cgd 	for (ptr = theunit->numerator; *ptr; ptr++) {
    258   1.1       cgd 		if (ptr > theunit->numerator && **ptr &&
    259   1.1       cgd 		    !strcmp(*ptr, *(ptr - 1)))
    260   1.1       cgd 			counter++;
    261   1.1       cgd 		else {
    262   1.1       cgd 			if (counter > 1)
    263   1.1       cgd 				printf("%s%d", powerstring, counter);
    264   1.1       cgd 			if (**ptr)
    265   1.1       cgd 				printf(" %s", *ptr);
    266   1.1       cgd 			counter = 1;
    267   1.1       cgd 		}
    268   1.1       cgd 	}
    269   1.1       cgd 	if (counter > 1)
    270   1.1       cgd 		printf("%s%d", powerstring, counter);
    271   1.1       cgd 	counter = 1;
    272   1.1       cgd 	printedslash = 0;
    273   1.1       cgd 	for (ptr = theunit->denominator; *ptr; ptr++) {
    274   1.1       cgd 		if (ptr > theunit->denominator && **ptr &&
    275   1.1       cgd 		    !strcmp(*ptr, *(ptr - 1)))
    276   1.1       cgd 			counter++;
    277   1.1       cgd 		else {
    278   1.1       cgd 			if (counter > 1)
    279   1.1       cgd 				printf("%s%d", powerstring, counter);
    280   1.1       cgd 			if (**ptr) {
    281   1.1       cgd 				if (!printedslash)
    282   1.1       cgd 					printf(" /");
    283   1.1       cgd 				printedslash = 1;
    284   1.1       cgd 				printf(" %s", *ptr);
    285   1.1       cgd 			}
    286   1.1       cgd 			counter = 1;
    287   1.1       cgd 		}
    288   1.1       cgd 	}
    289   1.1       cgd 	if (counter > 1)
    290   1.1       cgd 		printf("%s%d", powerstring, counter);
    291   1.1       cgd 	printf("\n");
    292   1.1       cgd }
    293   1.1       cgd 
    294   1.1       cgd 
    295   1.1       cgd void
    296   1.1       cgd zeroerror()
    297   1.1       cgd {
    298   1.7     lukem 	warnx("Unit reduces to zero");
    299   1.1       cgd }
    300   1.1       cgd 
    301   1.1       cgd /*
    302   1.1       cgd    Adds the specified string to the unit.
    303   1.1       cgd    Flip is 0 for adding normally, 1 for adding reciprocal.
    304   1.1       cgd 
    305   1.1       cgd    Returns 0 for successful addition, nonzero on error.
    306   1.1       cgd */
    307   1.1       cgd 
    308   1.1       cgd int
    309  1.16     lukem addunit(struct unittype * theunit, const char *toadd, int flip)
    310   1.1       cgd {
    311   1.1       cgd 	char *scratch, *savescr;
    312   1.1       cgd 	char *item;
    313   1.3       jtc 	char *divider, *slash;
    314   1.1       cgd 	int doingtop;
    315   1.1       cgd 
    316   1.1       cgd 	savescr = scratch = dupstr(toadd);
    317   1.1       cgd 	for (slash = scratch + 1; *slash; slash++)
    318   1.1       cgd 		if (*slash == '-' &&
    319  1.14       dsl 		    (tolower((unsigned char)*(slash - 1)) != 'e' ||
    320   1.1       cgd 		    !strchr(".0123456789", *(slash + 1))))
    321   1.1       cgd 			*slash = ' ';
    322   1.1       cgd 	slash = strchr(scratch, '/');
    323   1.1       cgd 	if (slash)
    324   1.1       cgd 		*slash = 0;
    325   1.1       cgd 	doingtop = 1;
    326   1.1       cgd 	do {
    327   1.1       cgd 		item = strtok(scratch, " *\t\n/");
    328   1.1       cgd 		while (item) {
    329   1.1       cgd 			if (strchr("0123456789.", *item)) { /* item is a number */
    330   1.1       cgd 				double num;
    331   1.1       cgd 
    332   1.1       cgd 				divider = strchr(item, '|');
    333   1.1       cgd 				if (divider) {
    334   1.1       cgd 					*divider = 0;
    335   1.1       cgd 					num = atof(item);
    336   1.1       cgd 					if (!num) {
    337   1.1       cgd 						zeroerror();
    338   1.1       cgd 						return 1;
    339   1.1       cgd 					}
    340   1.1       cgd 					if (doingtop ^ flip)
    341   1.1       cgd 						theunit->factor *= num;
    342   1.1       cgd 					else
    343   1.1       cgd 						theunit->factor /= num;
    344   1.1       cgd 					num = atof(divider + 1);
    345   1.1       cgd 					if (!num) {
    346   1.1       cgd 						zeroerror();
    347   1.1       cgd 						return 1;
    348   1.1       cgd 					}
    349   1.1       cgd 					if (doingtop ^ flip)
    350   1.1       cgd 						theunit->factor /= num;
    351   1.1       cgd 					else
    352   1.1       cgd 						theunit->factor *= num;
    353   1.1       cgd 				}
    354   1.1       cgd 				else {
    355   1.1       cgd 					num = atof(item);
    356   1.1       cgd 					if (!num) {
    357   1.1       cgd 						zeroerror();
    358   1.1       cgd 						return 1;
    359   1.1       cgd 					}
    360   1.1       cgd 					if (doingtop ^ flip)
    361   1.1       cgd 						theunit->factor *= num;
    362   1.1       cgd 					else
    363   1.1       cgd 						theunit->factor /= num;
    364   1.1       cgd 
    365   1.1       cgd 				}
    366   1.1       cgd 			}
    367   1.1       cgd 			else {	/* item is not a number */
    368   1.1       cgd 				int repeat = 1;
    369   1.1       cgd 
    370   1.1       cgd 				if (strchr("23456789",
    371   1.1       cgd 				    item[strlen(item) - 1])) {
    372   1.1       cgd 					repeat = item[strlen(item) - 1] - '0';
    373   1.1       cgd 					item[strlen(item) - 1] = 0;
    374   1.1       cgd 				}
    375   1.1       cgd 				for (; repeat; repeat--)
    376   1.1       cgd 					if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
    377   1.1       cgd 						return 1;
    378   1.1       cgd 			}
    379   1.1       cgd 			item = strtok(NULL, " *\t/\n");
    380   1.1       cgd 		}
    381   1.1       cgd 		doingtop--;
    382   1.1       cgd 		if (slash) {
    383   1.1       cgd 			scratch = slash + 1;
    384   1.1       cgd 		}
    385   1.1       cgd 		else
    386   1.1       cgd 			doingtop--;
    387   1.1       cgd 	} while (doingtop >= 0);
    388   1.1       cgd 	free(savescr);
    389   1.1       cgd 	return 0;
    390   1.1       cgd }
    391   1.1       cgd 
    392   1.1       cgd 
    393   1.1       cgd int
    394   1.1       cgd compare(const void *item1, const void *item2)
    395   1.1       cgd {
    396  1.16     lukem 	return strcmp(*(const char * const *) item1,
    397  1.16     lukem 		      *(const char * const *) item2);
    398   1.1       cgd }
    399   1.1       cgd 
    400   1.1       cgd 
    401   1.1       cgd void
    402   1.1       cgd sortunit(struct unittype * theunit)
    403   1.1       cgd {
    404  1.16     lukem 	const char **ptr;
    405   1.1       cgd 	int count;
    406   1.1       cgd 
    407   1.1       cgd 	for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
    408   1.1       cgd 	qsort(theunit->numerator, count, sizeof(char *), compare);
    409   1.1       cgd 	for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
    410   1.1       cgd 	qsort(theunit->denominator, count, sizeof(char *), compare);
    411   1.1       cgd }
    412   1.1       cgd 
    413   1.1       cgd 
    414   1.1       cgd void
    415   1.1       cgd cancelunit(struct unittype * theunit)
    416   1.1       cgd {
    417  1.16     lukem 	const char **den, **num;
    418   1.1       cgd 	int comp;
    419   1.1       cgd 
    420   1.1       cgd 	den = theunit->denominator;
    421   1.1       cgd 	num = theunit->numerator;
    422   1.1       cgd 
    423   1.1       cgd 	while (*num && *den) {
    424   1.1       cgd 		comp = strcmp(*den, *num);
    425   1.1       cgd 		if (!comp) {
    426   1.1       cgd /*      if (*den!=NULLUNIT) free(*den);
    427   1.1       cgd       if (*num!=NULLUNIT) free(*num);*/
    428   1.1       cgd 			*den++ = NULLUNIT;
    429   1.1       cgd 			*num++ = NULLUNIT;
    430   1.1       cgd 		}
    431   1.1       cgd 		else if (comp < 0)
    432   1.1       cgd 			den++;
    433   1.1       cgd 		else
    434   1.1       cgd 			num++;
    435   1.1       cgd 	}
    436   1.1       cgd }
    437   1.1       cgd 
    438   1.1       cgd 
    439   1.1       cgd 
    440   1.1       cgd 
    441   1.1       cgd /*
    442   1.1       cgd    Looks up the definition for the specified unit.
    443   1.1       cgd    Returns a pointer to the definition or a null pointer
    444   1.1       cgd    if the specified unit does not appear in the units table.
    445   1.1       cgd */
    446   1.1       cgd 
    447   1.1       cgd static char buffer[100];	/* buffer for lookupunit answers with
    448   1.1       cgd 				   prefixes */
    449   1.1       cgd 
    450  1.16     lukem const char *
    451  1.16     lukem lookupunit(const char *unit)
    452   1.1       cgd {
    453   1.1       cgd 	int i;
    454   1.1       cgd 	char *copy;
    455   1.1       cgd 
    456   1.1       cgd 	for (i = 0; i < unitcount; i++) {
    457   1.1       cgd 		if (!strcmp(unittable[i].uname, unit))
    458   1.1       cgd 			return unittable[i].uval;
    459   1.1       cgd 	}
    460   1.1       cgd 
    461   1.1       cgd 	if (unit[strlen(unit) - 1] == '^') {
    462   1.1       cgd 		copy = dupstr(unit);
    463   1.1       cgd 		copy[strlen(copy) - 1] = 0;
    464   1.1       cgd 		for (i = 0; i < unitcount; i++) {
    465   1.1       cgd 			if (!strcmp(unittable[i].uname, copy)) {
    466  1.12    itojun 				strlcpy(buffer, copy, sizeof(buffer));
    467   1.1       cgd 				free(copy);
    468   1.1       cgd 				return buffer;
    469   1.1       cgd 			}
    470   1.1       cgd 		}
    471   1.1       cgd 		free(copy);
    472   1.1       cgd 	}
    473   1.1       cgd 	if (unit[strlen(unit) - 1] == 's') {
    474   1.1       cgd 		copy = dupstr(unit);
    475   1.1       cgd 		copy[strlen(copy) - 1] = 0;
    476   1.1       cgd 		for (i = 0; i < unitcount; i++) {
    477   1.1       cgd 			if (!strcmp(unittable[i].uname, copy)) {
    478  1.12    itojun 				strlcpy(buffer, copy, sizeof(buffer));
    479   1.1       cgd 				free(copy);
    480   1.1       cgd 				return buffer;
    481   1.1       cgd 			}
    482   1.1       cgd 		}
    483   1.1       cgd 		if (copy[strlen(copy) - 1] == 'e') {
    484   1.1       cgd 			copy[strlen(copy) - 1] = 0;
    485   1.1       cgd 			for (i = 0; i < unitcount; i++) {
    486   1.1       cgd 				if (!strcmp(unittable[i].uname, copy)) {
    487  1.12    itojun 					strlcpy(buffer, copy, sizeof(buffer));
    488   1.1       cgd 					free(copy);
    489   1.1       cgd 					return buffer;
    490   1.1       cgd 				}
    491   1.1       cgd 			}
    492   1.1       cgd 		}
    493   1.1       cgd 		free(copy);
    494   1.1       cgd 	}
    495   1.1       cgd 	for (i = 0; i < prefixcount; i++) {
    496   1.1       cgd 		if (!strncmp(prefixtable[i].prefixname, unit,
    497   1.1       cgd 			strlen(prefixtable[i].prefixname))) {
    498   1.1       cgd 			unit += strlen(prefixtable[i].prefixname);
    499   1.1       cgd 			if (!strlen(unit) || lookupunit(unit)) {
    500  1.12    itojun 				strlcpy(buffer, prefixtable[i].prefixval,
    501  1.12    itojun 				    sizeof(buffer));
    502  1.12    itojun 				strlcat(buffer, " ", sizeof(buffer));
    503  1.12    itojun 				strlcat(buffer, unit, sizeof(buffer));
    504   1.1       cgd 				return buffer;
    505   1.1       cgd 			}
    506   1.1       cgd 		}
    507   1.1       cgd 	}
    508   1.1       cgd 	return 0;
    509   1.1       cgd }
    510   1.1       cgd 
    511   1.1       cgd 
    512   1.1       cgd 
    513   1.1       cgd /*
    514   1.1       cgd    reduces a product of symbolic units to primitive units.
    515   1.1       cgd    The three low bits are used to return flags:
    516   1.1       cgd 
    517   1.1       cgd      bit 0 (1) set on if reductions were performed without error.
    518   1.1       cgd      bit 1 (2) set on if no reductions are performed.
    519   1.1       cgd      bit 2 (4) set on if an unknown unit is discovered.
    520   1.1       cgd */
    521   1.1       cgd 
    522   1.1       cgd 
    523   1.1       cgd #define ERROR 4
    524   1.1       cgd 
    525   1.1       cgd int
    526   1.1       cgd reduceproduct(struct unittype * theunit, int flip)
    527   1.1       cgd {
    528   1.1       cgd 
    529  1.16     lukem 	const char *toadd;
    530  1.16     lukem 	const char **product;
    531   1.1       cgd 	int didsomething = 2;
    532   1.1       cgd 
    533   1.1       cgd 	if (flip)
    534   1.1       cgd 		product = theunit->denominator;
    535   1.1       cgd 	else
    536   1.1       cgd 		product = theunit->numerator;
    537   1.1       cgd 
    538   1.1       cgd 	for (; *product; product++) {
    539   1.1       cgd 
    540   1.1       cgd 		for (;;) {
    541   1.1       cgd 			if (!strlen(*product))
    542   1.1       cgd 				break;
    543   1.1       cgd 			toadd = lookupunit(*product);
    544   1.1       cgd 			if (!toadd) {
    545   1.1       cgd 				printf("unknown unit '%s'\n", *product);
    546   1.1       cgd 				return ERROR;
    547   1.1       cgd 			}
    548   1.1       cgd 			if (strchr(toadd, PRIMITIVECHAR))
    549   1.1       cgd 				break;
    550   1.1       cgd 			didsomething = 1;
    551   1.1       cgd 			if (*product != NULLUNIT) {
    552  1.16     lukem 				free(__UNCONST(*product));
    553   1.1       cgd 				*product = NULLUNIT;
    554   1.1       cgd 			}
    555   1.1       cgd 			if (addunit(theunit, toadd, flip))
    556   1.1       cgd 				return ERROR;
    557   1.1       cgd 		}
    558   1.1       cgd 	}
    559   1.1       cgd 	return didsomething;
    560   1.1       cgd }
    561   1.1       cgd 
    562   1.1       cgd 
    563   1.1       cgd /*
    564   1.1       cgd    Reduces numerator and denominator of the specified unit.
    565   1.1       cgd    Returns 0 on success, or 1 on unknown unit error.
    566   1.1       cgd */
    567   1.1       cgd 
    568   1.1       cgd int
    569   1.1       cgd reduceunit(struct unittype * theunit)
    570   1.1       cgd {
    571   1.1       cgd 	int ret;
    572   1.1       cgd 
    573   1.1       cgd 	ret = 1;
    574   1.1       cgd 	while (ret & 1) {
    575   1.1       cgd 		ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
    576   1.1       cgd 		if (ret & 4)
    577   1.1       cgd 			return 1;
    578   1.1       cgd 	}
    579   1.1       cgd 	return 0;
    580   1.1       cgd }
    581   1.1       cgd 
    582   1.1       cgd 
    583   1.1       cgd int
    584  1.16     lukem compareproducts(const char **one, const char **two)
    585   1.1       cgd {
    586   1.1       cgd 	while (*one || *two) {
    587   1.1       cgd 		if (!*one && *two != NULLUNIT)
    588   1.1       cgd 			return 1;
    589   1.1       cgd 		if (!*two && *one != NULLUNIT)
    590   1.1       cgd 			return 1;
    591   1.1       cgd 		if (*one == NULLUNIT)
    592   1.1       cgd 			one++;
    593   1.1       cgd 		else if (*two == NULLUNIT)
    594   1.1       cgd 			two++;
    595  1.15  christos 		else if (*one && *two && strcmp(*one, *two))
    596   1.1       cgd 			return 1;
    597   1.1       cgd 		else
    598   1.1       cgd 			one++, two++;
    599   1.1       cgd 	}
    600   1.1       cgd 	return 0;
    601   1.1       cgd }
    602   1.1       cgd 
    603   1.1       cgd 
    604   1.1       cgd /* Return zero if units are compatible, nonzero otherwise */
    605   1.1       cgd 
    606   1.1       cgd int
    607   1.1       cgd compareunits(struct unittype * first, struct unittype * second)
    608   1.1       cgd {
    609   1.1       cgd 	return
    610   1.1       cgd 	compareproducts(first->numerator, second->numerator) ||
    611   1.1       cgd 	compareproducts(first->denominator, second->denominator);
    612   1.1       cgd }
    613   1.1       cgd 
    614  1.13   mycroft int
    615  1.13   mycroft compareunitsreciprocal(struct unittype * first, struct unittype * second)
    616  1.13   mycroft {
    617  1.13   mycroft 	return
    618  1.13   mycroft 	compareproducts(first->numerator, second->denominator) ||
    619  1.13   mycroft 	compareproducts(first->denominator, second->numerator);
    620  1.13   mycroft }
    621  1.13   mycroft 
    622   1.1       cgd 
    623   1.1       cgd int
    624   1.1       cgd completereduce(struct unittype * unit)
    625   1.1       cgd {
    626   1.1       cgd 	if (reduceunit(unit))
    627   1.1       cgd 		return 1;
    628   1.1       cgd 	sortunit(unit);
    629   1.1       cgd 	cancelunit(unit);
    630   1.1       cgd 	return 0;
    631   1.1       cgd }
    632   1.1       cgd 
    633   1.1       cgd 
    634   1.1       cgd void
    635   1.1       cgd showanswer(struct unittype * have, struct unittype * want)
    636   1.1       cgd {
    637   1.1       cgd 	if (compareunits(have, want)) {
    638  1.13   mycroft 		if (compareunitsreciprocal(have, want)) {
    639  1.13   mycroft 			printf("conformability error\n");
    640  1.13   mycroft 			showunit(have);
    641  1.13   mycroft 			showunit(want);
    642  1.13   mycroft 		} else {
    643  1.13   mycroft 			printf("\treciprocal conversion\n");
    644  1.13   mycroft 			printf("\t* %.8g\n\t/ %.8g\n", 1 / (have->factor * want->factor),
    645  1.13   mycroft 			    want->factor * have->factor);
    646  1.13   mycroft 		}
    647   1.1       cgd 	}
    648   1.1       cgd 	else
    649   1.1       cgd 		printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,
    650   1.1       cgd 		    want->factor / have->factor);
    651   1.1       cgd }
    652   1.1       cgd 
    653   1.1       cgd 
    654   1.1       cgd void
    655   1.1       cgd usage()
    656   1.1       cgd {
    657   1.7     lukem 	fprintf(stderr,
    658   1.7     lukem 	    "\nunits [-f unitsfile] [-q] [-v] [from-unit to-unit]\n");
    659   1.1       cgd 	fprintf(stderr, "\n    -f specify units file\n");
    660  1.10    atatat 	fprintf(stderr, "    -q suppress prompting (quiet)\n");
    661   1.1       cgd 	fprintf(stderr, "    -v print version number\n");
    662   1.1       cgd 	exit(3);
    663   1.1       cgd }
    664   1.1       cgd 
    665   1.1       cgd 
    666   1.4       jtc int
    667   1.1       cgd main(int argc, char **argv)
    668   1.1       cgd {
    669   1.1       cgd 
    670   1.1       cgd 	struct unittype have, want;
    671   1.1       cgd 	char havestr[81], wantstr[81];
    672   1.5      mark 	int optchar;
    673  1.16     lukem 	const char *userfile = 0;
    674   1.1       cgd 	int quiet = 0;
    675   1.1       cgd 
    676   1.5      mark 	while ((optchar = getopt(argc, argv, "vqf:")) != -1) {
    677   1.1       cgd 		switch (optchar) {
    678   1.1       cgd 		case 'f':
    679   1.1       cgd 			userfile = optarg;
    680   1.1       cgd 			break;
    681   1.1       cgd 		case 'q':
    682   1.1       cgd 			quiet = 1;
    683   1.1       cgd 			break;
    684   1.1       cgd 		case 'v':
    685   1.1       cgd 			fprintf(stderr, "\n  units version %s  Copyright (c) 1993 by Adrian Mariano\n",
    686   1.1       cgd 			    VERSION);
    687   1.1       cgd 			fprintf(stderr, "                    This program may be freely distributed\n");
    688   1.1       cgd 			usage();
    689   1.1       cgd 		default:
    690   1.1       cgd 			usage();
    691   1.1       cgd 			break;
    692   1.1       cgd 		}
    693   1.1       cgd 	}
    694   1.1       cgd 
    695  1.10    atatat 	argc -= optind;
    696  1.10    atatat 	argv += optind;
    697  1.10    atatat 
    698  1.10    atatat 	if (argc != 3 && argc != 2 && argc != 0)
    699   1.1       cgd 		usage();
    700   1.1       cgd 
    701   1.1       cgd 	readunits(userfile);
    702   1.1       cgd 
    703  1.10    atatat 	if (argc == 3) {
    704  1.12    itojun 		strlcpy(havestr, argv[0], sizeof(havestr));
    705  1.12    itojun 		strlcat(havestr, " ", sizeof(havestr));
    706  1.12    itojun 		strlcat(havestr, argv[1], sizeof(havestr));
    707  1.10    atatat 		argc--;
    708  1.10    atatat 		argv++;
    709  1.10    atatat 		argv[0] = havestr;
    710  1.10    atatat 	}
    711  1.10    atatat 
    712  1.10    atatat 	if (argc == 2) {
    713  1.12    itojun 		strlcpy(havestr, argv[0], sizeof(havestr));
    714  1.12    itojun 		strlcpy(wantstr, argv[1], sizeof(wantstr));
    715   1.1       cgd 		initializeunit(&have);
    716   1.1       cgd 		addunit(&have, havestr, 0);
    717   1.1       cgd 		completereduce(&have);
    718   1.1       cgd 		initializeunit(&want);
    719   1.1       cgd 		addunit(&want, wantstr, 0);
    720   1.1       cgd 		completereduce(&want);
    721   1.1       cgd 		showanswer(&have, &want);
    722   1.1       cgd 	}
    723   1.1       cgd 	else {
    724   1.1       cgd 		if (!quiet)
    725   1.1       cgd 			printf("%d units, %d prefixes\n\n", unitcount,
    726   1.1       cgd 			    prefixcount);
    727   1.1       cgd 		for (;;) {
    728   1.1       cgd 			do {
    729   1.1       cgd 				initializeunit(&have);
    730   1.1       cgd 				if (!quiet)
    731   1.1       cgd 					printf("You have: ");
    732   1.1       cgd 				if (!fgets(havestr, 80, stdin)) {
    733  1.11  kristerw 					if (!quiet)
    734  1.11  kristerw 						putchar('\n');
    735   1.1       cgd 					exit(0);
    736   1.1       cgd 				}
    737   1.1       cgd 			} while (addunit(&have, havestr, 0) ||
    738   1.1       cgd 			    completereduce(&have));
    739   1.1       cgd 			do {
    740   1.1       cgd 				initializeunit(&want);
    741   1.1       cgd 				if (!quiet)
    742   1.1       cgd 					printf("You want: ");
    743   1.1       cgd 				if (!fgets(wantstr, 80, stdin)) {
    744   1.1       cgd 					if (!quiet)
    745   1.1       cgd 						putchar('\n');
    746   1.1       cgd 					exit(0);
    747   1.1       cgd 				}
    748   1.1       cgd 			} while (addunit(&want, wantstr, 0) ||
    749   1.1       cgd 			    completereduce(&want));
    750   1.1       cgd 			showanswer(&have, &want);
    751   1.1       cgd 		}
    752   1.1       cgd 	}
    753   1.7     lukem 	return (0);
    754   1.1       cgd }
    755