2 * units.c Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. The name of the author may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
11 * Disclaimer: This software is provided by the author "as is". The author
12 * shall not be liable for any damages caused in any way by this software.
14 * I would appreciate (though I do not require) receiving a copy of any
15 * improvements you might make to this program.
19 static const char rcsid[] =
33 #include <sys/capsicum.h>
35 #include "pathnames.h"
38 #define UNITSFILE _PATH_UNITSLIB
42 #define MAXPREFIXES 100
44 #define MAXSUBUNITS 500
46 #define PRIMITIVECHAR '!'
48 static const char *powerstring = "^";
53 } unittable[MAXUNITS];
56 char *numerator[MAXSUBUNITS];
57 char *denominator[MAXSUBUNITS];
66 } prefixtable[MAXPREFIXES];
69 static char NULLUNIT[] = "";
78 static int prefixcount;
79 static bool verbose = false;
80 static const char * havestr;
81 static const char * wantstr;
83 static int addsubunit(char *product[], char *toadd);
84 static int addunit(struct unittype *theunit, const char *toadd, int flip, int quantity);
85 static void cancelunit(struct unittype * theunit);
86 static int compare(const void *item1, const void *item2);
87 static int compareproducts(char **one, char **two);
88 static int compareunits(struct unittype * first, struct unittype * second);
89 static int completereduce(struct unittype * unit);
90 static char *dupstr(const char *str);
91 static void initializeunit(struct unittype * theunit);
92 static char *lookupunit(const char *unit);
93 static void readunits(const char *userfile);
94 static int reduceproduct(struct unittype * theunit, int flip);
95 static int reduceunit(struct unittype * theunit);
96 static void showanswer(struct unittype * have, struct unittype * want);
97 static void showunit(struct unittype * theunit);
98 static void sortunit(struct unittype * theunit);
99 static void usage(void);
100 static void zeroerror(void);
102 static const char* promptstr = "";
104 static const char * prompt(EditLine *e __unused) {
109 dupstr(const char *str)
121 readunits(const char *userfile)
124 char line[512], *lineptr;
126 cap_rights_t unitfilerights;
132 unitfile = fopen(userfile, "rt");
134 errx(1, "unable to open units file '%s'", userfile);
137 unitfile = fopen(UNITSFILE, "rt");
142 env = getenv("PATH");
144 direc = strtok(env, SEPARATOR);
146 snprintf(filename, sizeof(filename),
147 "%s/%s", direc, UNITSFILE);
148 unitfile = fopen(filename, "rt");
151 direc = strtok(NULL, SEPARATOR);
155 errx(1, "can't find units file '%s'", UNITSFILE);
158 cap_rights_init(&unitfilerights, CAP_READ, CAP_FSTAT);
159 if (cap_rights_limit(fileno(unitfile), &unitfilerights) < 0
161 err(1, "cap_rights_limit() failed");
162 while (!feof(unitfile)) {
163 if (!fgets(line, sizeof(line), unitfile))
169 lineptr += strspn(lineptr, " \n\t");
170 len = strcspn(lineptr, " \n\t");
172 if (!strlen(lineptr))
174 if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
175 if (prefixcount == MAXPREFIXES) {
176 warnx("memory for prefixes exceeded in line %d", linenum);
179 lineptr[strlen(lineptr) - 1] = 0;
180 prefixtable[prefixcount].prefixname = dupstr(lineptr);
181 for (i = 0; i < prefixcount; i++)
182 if (!strcmp(prefixtable[i].prefixname, lineptr)) {
183 warnx("redefinition of prefix '%s' on line %d ignored",
188 lineptr += strspn(lineptr, " \n\t");
189 len = strcspn(lineptr, "\n\t");
191 warnx("unexpected end of prefix on line %d",
196 prefixtable[prefixcount++].prefixval = dupstr(lineptr);
198 else { /* it's not a prefix */
199 if (unitcount == MAXUNITS) {
200 warnx("memory for units exceeded in line %d", linenum);
203 unittable[unitcount].uname = dupstr(lineptr);
204 for (i = 0; i < unitcount; i++)
205 if (!strcmp(unittable[i].uname, lineptr)) {
206 warnx("redefinition of unit '%s' on line %d ignored",
211 lineptr += strspn(lineptr, " \n\t");
212 if (!strlen(lineptr)) {
213 warnx("unexpected end of unit on line %d",
217 len = strcspn(lineptr, "\n\t");
219 unittable[unitcount++].uval = dupstr(lineptr);
226 initializeunit(struct unittype * theunit)
228 theunit->numerator[0] = theunit->denominator[0] = NULL;
229 theunit->factor = 1.0;
230 theunit->offset = 0.0;
231 theunit->quantity = 0;
236 addsubunit(char *product[], char *toadd)
240 for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
241 if (ptr >= product + MAXSUBUNITS) {
242 warnx("memory overflow in unit reduction");
247 *ptr = dupstr(toadd);
253 showunit(struct unittype * theunit)
259 printf("%.8g", theunit->factor);
261 printf("&%.8g", theunit->offset);
262 for (ptr = theunit->numerator; *ptr; ptr++) {
263 if (ptr > theunit->numerator && **ptr &&
264 !strcmp(*ptr, *(ptr - 1)))
268 printf("%s%d", powerstring, counter);
275 printf("%s%d", powerstring, counter);
278 for (ptr = theunit->denominator; *ptr; ptr++) {
279 if (ptr > theunit->denominator && **ptr &&
280 !strcmp(*ptr, *(ptr - 1)))
284 printf("%s%d", powerstring, counter);
295 printf("%s%d", powerstring, counter);
303 warnx("unit reduces to zero");
307 Adds the specified string to the unit.
308 Flip is 0 for adding normally, 1 for adding reciprocal.
309 Quantity is 1 if this is a quantity to be converted rather than a pure unit.
311 Returns 0 for successful addition, nonzero on error.
315 addunit(struct unittype * theunit, const char *toadd, int flip, int quantity)
317 char *scratch, *savescr;
319 char *divider, *slash, *offset;
325 savescr = scratch = dupstr(toadd);
326 for (slash = scratch + 1; *slash; slash++)
328 (tolower(*(slash - 1)) != 'e' ||
329 !strchr(".0123456789", *(slash + 1))))
331 slash = strchr(scratch, '/');
336 item = strtok(scratch, " *\t\n/");
338 if (strchr("0123456789.", *item)) { /* item is a number */
339 double num, offsetnum;
342 theunit->quantity = 1;
344 offset = strchr(item, '&');
347 offsetnum = atof(offset+1);
351 divider = strchr(item, '|');
359 if (doingtop ^ flip) {
360 theunit->factor *= num;
361 theunit->offset *= num;
363 theunit->factor /= num;
364 theunit->offset /= num;
366 num = atof(divider + 1);
371 if (doingtop ^ flip) {
372 theunit->factor /= num;
373 theunit->offset /= num;
375 theunit->factor *= num;
376 theunit->offset *= num;
385 if (doingtop ^ flip) {
386 theunit->factor *= num;
387 theunit->offset *= num;
389 theunit->factor /= num;
390 theunit->offset /= num;
394 theunit->offset += offsetnum;
396 else { /* item is not a number */
399 if (strchr("23456789",
400 item[strlen(item) - 1])) {
401 repeat = item[strlen(item) - 1] - '0';
402 item[strlen(item) - 1] = 0;
404 for (; repeat; repeat--)
405 if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
408 item = strtok(NULL, " *\t/\n");
416 } while (doingtop >= 0);
423 compare(const void *item1, const void *item2)
425 return strcmp(*(const char * const *)item1, *(const char * const *)item2);
430 sortunit(struct unittype * theunit)
435 for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
436 qsort(theunit->numerator, count, sizeof(char *), compare);
437 for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
438 qsort(theunit->denominator, count, sizeof(char *), compare);
443 cancelunit(struct unittype * theunit)
448 den = theunit->denominator;
449 num = theunit->numerator;
451 while (*num && *den) {
452 comp = strcmp(*den, *num);
454 /* if (*den!=NULLUNIT) free(*den);
455 if (*num!=NULLUNIT) free(*num);*/
470 Looks up the definition for the specified unit.
471 Returns a pointer to the definition or a null pointer
472 if the specified unit does not appear in the units table.
475 static char buffer[100]; /* buffer for lookupunit answers with
479 lookupunit(const char *unit)
484 for (i = 0; i < unitcount; i++) {
485 if (!strcmp(unittable[i].uname, unit))
486 return unittable[i].uval;
489 if (unit[strlen(unit) - 1] == '^') {
491 copy[strlen(copy) - 1] = 0;
492 for (i = 0; i < unitcount; i++) {
493 if (!strcmp(unittable[i].uname, copy)) {
494 strlcpy(buffer, copy, sizeof(buffer));
501 if (unit[strlen(unit) - 1] == 's') {
503 copy[strlen(copy) - 1] = 0;
504 for (i = 0; i < unitcount; i++) {
505 if (!strcmp(unittable[i].uname, copy)) {
506 strlcpy(buffer, copy, sizeof(buffer));
511 if (copy[strlen(copy) - 1] == 'e') {
512 copy[strlen(copy) - 1] = 0;
513 for (i = 0; i < unitcount; i++) {
514 if (!strcmp(unittable[i].uname, copy)) {
515 strlcpy(buffer, copy, sizeof(buffer));
523 for (i = 0; i < prefixcount; i++) {
524 size_t len = strlen(prefixtable[i].prefixname);
525 if (!strncmp(prefixtable[i].prefixname, unit, len)) {
526 if (!strlen(unit + len) || lookupunit(unit + len)) {
527 snprintf(buffer, sizeof(buffer), "%s %s",
528 prefixtable[i].prefixval, unit + len);
539 reduces a product of symbolic units to primitive units.
540 The three low bits are used to return flags:
542 bit 0 (1) set on if reductions were performed without error.
543 bit 1 (2) set on if no reductions are performed.
544 bit 2 (4) set on if an unknown unit is discovered.
551 reduceproduct(struct unittype * theunit, int flip)
556 int didsomething = 2;
559 product = theunit->denominator;
561 product = theunit->numerator;
563 for (; *product; product++) {
566 if (!strlen(*product))
568 toadd = lookupunit(*product);
570 printf("unknown unit '%s'\n", *product);
573 if (strchr(toadd, PRIMITIVECHAR))
576 if (*product != NULLUNIT) {
580 if (addunit(theunit, toadd, flip, 0))
589 Reduces numerator and denominator of the specified unit.
590 Returns 0 on success, or 1 on unknown unit error.
594 reduceunit(struct unittype * theunit)
600 ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
609 compareproducts(char **one, char **two)
611 while (*one || *two) {
612 if (!*one && *two != NULLUNIT)
614 if (!*two && *one != NULLUNIT)
616 if (*one == NULLUNIT)
618 else if (*two == NULLUNIT)
620 else if (strcmp(*one, *two))
629 /* Return zero if units are compatible, nonzero otherwise */
632 compareunits(struct unittype * first, struct unittype * second)
635 compareproducts(first->numerator, second->numerator) ||
636 compareproducts(first->denominator, second->denominator);
641 completereduce(struct unittype * unit)
643 if (reduceunit(unit))
651 showanswer(struct unittype * have, struct unittype * want)
655 if (compareunits(have, want)) {
656 printf("conformability error\n");
658 printf("\t%s = ", havestr);
663 printf("\t%s = ", wantstr);
668 else if (have->offset != want->offset) {
670 printf("WARNING: conversion of non-proportional quantities.\n");
673 (have->factor + have->offset-want->offset)/want->factor);
675 printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
676 have->factor / want->factor,
677 (have->offset-want->offset)/want->factor,
678 want->factor / have->factor,
679 (want->offset - have->offset)/have->factor);
683 ans = have->factor / want->factor;
685 printf("\t%s = %.8g * %s\n", havestr, ans, wantstr);
687 printf("\t* %.8g\n", ans);
690 printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr);
692 printf("\t/ %.8g\n", 1/ans);
701 "usage: units [-f unitsfile] [-UVq] [from-unit to-unit]\n");
707 main(int argc, char **argv)
710 struct unittype have, want;
721 while ((optchar = getopt(argc, argv, "f:qvUV")) != -1) {
725 if (strlen(optarg) == 0)
737 fprintf(stderr, "FreeBSD units\n");
740 if (access(UNITSFILE, F_OK) == 0)
741 printf("%s\n", UNITSFILE);
743 printf("Units data file not found");
754 inhistory = history_init();
755 el = el_init(argv[0], stdin, stdout, stderr);
756 el_set(el, EL_PROMPT, &prompt);
757 el_set(el, EL_EDITOR, "emacs");
758 el_set(el, EL_SIGNAL, 1);
759 el_set(el, EL_HIST, history, inhistory);
761 history(inhistory, &ev, H_SETSIZE, 800);
763 err(1, "Could not initialize history");
765 if (cap_enter() < 0 && errno != ENOSYS)
766 err(1, "unable to enter capability mode");
768 if (optind == argc - 2) {
769 havestr = argv[optind];
770 wantstr = argv[optind + 1];
771 initializeunit(&have);
772 addunit(&have, havestr, 0, 1);
773 completereduce(&have);
774 initializeunit(&want);
775 addunit(&want, wantstr, 0, 1);
776 completereduce(&want);
777 showanswer(&have, &want);
781 printf("%d units, %d prefixes\n", unitcount,
785 initializeunit(&have);
787 promptstr = "You have: ";
788 havestr = el_gets(el, &inputsz);
792 history(inhistory, &ev, H_ENTER,
794 } while (addunit(&have, havestr, 0, 1) ||
795 completereduce(&have));
797 initializeunit(&want);
799 promptstr = "You want: ";
800 wantstr = el_gets(el, &inputsz);
804 history(inhistory, &ev, H_ENTER,
806 } while (addunit(&want, wantstr, 0, 1) ||
807 completereduce(&want));
808 showanswer(&have, &want);
812 history_end(inhistory);