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;
84 char *dupstr(const char *str);
85 void readunits(const char *userfile);
86 void initializeunit(struct unittype * theunit);
87 int addsubunit(char *product[], char *toadd);
88 void showunit(struct unittype * theunit);
90 int addunit(struct unittype *theunit, const char *toadd, int flip, int quantity);
91 int compare(const void *item1, const void *item2);
92 void sortunit(struct unittype * theunit);
93 void cancelunit(struct unittype * theunit);
94 char *lookupunit(const char *unit);
95 int reduceproduct(struct unittype * theunit, int flip);
96 int reduceunit(struct unittype * theunit);
97 int compareproducts(char **one, char **two);
98 int compareunits(struct unittype * first, struct unittype * second);
99 int completereduce(struct unittype * unit);
100 void showanswer(struct unittype * have, struct unittype * want);
103 static const char* promptstr = "";
105 static const char * prompt(EditLine *e __unused) {
110 dupstr(const char *str)
114 ret = malloc(strlen(str) + 1);
116 errx(3, "memory allocation error");
123 readunits(const char *userfile)
126 char line[512], *lineptr;
128 cap_rights_t unitfilerights;
134 unitfile = fopen(userfile, "rt");
136 errx(1, "unable to open units file '%s'", userfile);
139 unitfile = fopen(UNITSFILE, "rt");
144 env = getenv("PATH");
146 direc = strtok(env, SEPARATOR);
148 snprintf(filename, sizeof(filename),
149 "%s/%s", direc, UNITSFILE);
150 unitfile = fopen(filename, "rt");
153 direc = strtok(NULL, SEPARATOR);
157 errx(1, "can't find units file '%s'", UNITSFILE);
160 cap_rights_init(&unitfilerights, CAP_READ, CAP_FSTAT);
161 if (cap_rights_limit(fileno(unitfile), &unitfilerights) < 0
163 err(1, "cap_rights_limit() failed");
164 while (!feof(unitfile)) {
165 if (!fgets(line, sizeof(line), unitfile))
171 lineptr += strspn(lineptr, " \n\t");
172 len = strcspn(lineptr, " \n\t");
174 if (!strlen(lineptr))
176 if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
177 if (prefixcount == MAXPREFIXES) {
178 warnx("memory for prefixes exceeded in line %d", linenum);
181 lineptr[strlen(lineptr) - 1] = 0;
182 prefixtable[prefixcount].prefixname = dupstr(lineptr);
183 for (i = 0; i < prefixcount; i++)
184 if (!strcmp(prefixtable[i].prefixname, lineptr)) {
185 warnx("redefinition of prefix '%s' on line %d ignored",
190 lineptr += strspn(lineptr, " \n\t");
191 len = strcspn(lineptr, "\n\t");
193 warnx("unexpected end of prefix on line %d",
198 prefixtable[prefixcount++].prefixval = dupstr(lineptr);
200 else { /* it's not a prefix */
201 if (unitcount == MAXUNITS) {
202 warnx("memory for units exceeded in line %d", linenum);
205 unittable[unitcount].uname = dupstr(lineptr);
206 for (i = 0; i < unitcount; i++)
207 if (!strcmp(unittable[i].uname, lineptr)) {
208 warnx("redefinition of unit '%s' on line %d ignored",
213 lineptr += strspn(lineptr, " \n\t");
214 if (!strlen(lineptr)) {
215 warnx("unexpected end of unit on line %d",
219 len = strcspn(lineptr, "\n\t");
221 unittable[unitcount++].uval = dupstr(lineptr);
228 initializeunit(struct unittype * theunit)
230 theunit->numerator[0] = theunit->denominator[0] = NULL;
231 theunit->factor = 1.0;
232 theunit->offset = 0.0;
233 theunit->quantity = 0;
238 addsubunit(char *product[], char *toadd)
242 for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
243 if (ptr >= product + MAXSUBUNITS) {
244 warnx("memory overflow in unit reduction");
249 *ptr = dupstr(toadd);
255 showunit(struct unittype * theunit)
261 printf("%.8g", theunit->factor);
263 printf("&%.8g", theunit->offset);
264 for (ptr = theunit->numerator; *ptr; ptr++) {
265 if (ptr > theunit->numerator && **ptr &&
266 !strcmp(*ptr, *(ptr - 1)))
270 printf("%s%d", powerstring, counter);
277 printf("%s%d", powerstring, counter);
280 for (ptr = theunit->denominator; *ptr; ptr++) {
281 if (ptr > theunit->denominator && **ptr &&
282 !strcmp(*ptr, *(ptr - 1)))
286 printf("%s%d", powerstring, counter);
297 printf("%s%d", powerstring, counter);
305 warnx("unit reduces to zero");
309 Adds the specified string to the unit.
310 Flip is 0 for adding normally, 1 for adding reciprocal.
311 Quantity is 1 if this is a quantity to be converted rather than a pure unit.
313 Returns 0 for successful addition, nonzero on error.
317 addunit(struct unittype * theunit, const char *toadd, int flip, int quantity)
319 char *scratch, *savescr;
321 char *divider, *slash, *offset;
327 savescr = scratch = dupstr(toadd);
328 for (slash = scratch + 1; *slash; slash++)
330 (tolower(*(slash - 1)) != 'e' ||
331 !strchr(".0123456789", *(slash + 1))))
333 slash = strchr(scratch, '/');
338 item = strtok(scratch, " *\t\n/");
340 if (strchr("0123456789.", *item)) { /* item is a number */
341 double num, offsetnum;
344 theunit->quantity = 1;
346 offset = strchr(item, '&');
349 offsetnum = atof(offset+1);
353 divider = strchr(item, '|');
361 if (doingtop ^ flip) {
362 theunit->factor *= num;
363 theunit->offset *= num;
365 theunit->factor /= num;
366 theunit->offset /= num;
368 num = atof(divider + 1);
373 if (doingtop ^ flip) {
374 theunit->factor /= num;
375 theunit->offset /= num;
377 theunit->factor *= num;
378 theunit->offset *= num;
387 if (doingtop ^ flip) {
388 theunit->factor *= num;
389 theunit->offset *= num;
391 theunit->factor /= num;
392 theunit->offset /= num;
396 theunit->offset += offsetnum;
398 else { /* item is not a number */
401 if (strchr("23456789",
402 item[strlen(item) - 1])) {
403 repeat = item[strlen(item) - 1] - '0';
404 item[strlen(item) - 1] = 0;
406 for (; repeat; repeat--)
407 if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
410 item = strtok(NULL, " *\t/\n");
418 } while (doingtop >= 0);
425 compare(const void *item1, const void *item2)
427 return strcmp(*(const char * const *)item1, *(const char * const *)item2);
432 sortunit(struct unittype * theunit)
437 for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
438 qsort(theunit->numerator, count, sizeof(char *), compare);
439 for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
440 qsort(theunit->denominator, count, sizeof(char *), compare);
445 cancelunit(struct unittype * theunit)
450 den = theunit->denominator;
451 num = theunit->numerator;
453 while (*num && *den) {
454 comp = strcmp(*den, *num);
456 /* if (*den!=NULLUNIT) free(*den);
457 if (*num!=NULLUNIT) free(*num);*/
472 Looks up the definition for the specified unit.
473 Returns a pointer to the definition or a null pointer
474 if the specified unit does not appear in the units table.
477 static char buffer[100]; /* buffer for lookupunit answers with
481 lookupunit(const char *unit)
486 for (i = 0; i < unitcount; i++) {
487 if (!strcmp(unittable[i].uname, unit))
488 return unittable[i].uval;
491 if (unit[strlen(unit) - 1] == '^') {
493 copy[strlen(copy) - 1] = 0;
494 for (i = 0; i < unitcount; i++) {
495 if (!strcmp(unittable[i].uname, copy)) {
496 strlcpy(buffer, copy, sizeof(buffer));
503 if (unit[strlen(unit) - 1] == 's') {
505 copy[strlen(copy) - 1] = 0;
506 for (i = 0; i < unitcount; i++) {
507 if (!strcmp(unittable[i].uname, copy)) {
508 strlcpy(buffer, copy, sizeof(buffer));
513 if (copy[strlen(copy) - 1] == 'e') {
514 copy[strlen(copy) - 1] = 0;
515 for (i = 0; i < unitcount; i++) {
516 if (!strcmp(unittable[i].uname, copy)) {
517 strlcpy(buffer, copy, sizeof(buffer));
525 for (i = 0; i < prefixcount; i++) {
526 size_t len = strlen(prefixtable[i].prefixname);
527 if (!strncmp(prefixtable[i].prefixname, unit, len)) {
528 if (!strlen(unit + len) || lookupunit(unit + len)) {
529 snprintf(buffer, sizeof(buffer), "%s %s",
530 prefixtable[i].prefixval, unit + len);
541 reduces a product of symbolic units to primitive units.
542 The three low bits are used to return flags:
544 bit 0 (1) set on if reductions were performed without error.
545 bit 1 (2) set on if no reductions are performed.
546 bit 2 (4) set on if an unknown unit is discovered.
553 reduceproduct(struct unittype * theunit, int flip)
558 int didsomething = 2;
561 product = theunit->denominator;
563 product = theunit->numerator;
565 for (; *product; product++) {
568 if (!strlen(*product))
570 toadd = lookupunit(*product);
572 printf("unknown unit '%s'\n", *product);
575 if (strchr(toadd, PRIMITIVECHAR))
578 if (*product != NULLUNIT) {
582 if (addunit(theunit, toadd, flip, 0))
591 Reduces numerator and denominator of the specified unit.
592 Returns 0 on success, or 1 on unknown unit error.
596 reduceunit(struct unittype * theunit)
602 ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
611 compareproducts(char **one, char **two)
613 while (*one || *two) {
614 if (!*one && *two != NULLUNIT)
616 if (!*two && *one != NULLUNIT)
618 if (*one == NULLUNIT)
620 else if (*two == NULLUNIT)
622 else if (strcmp(*one, *two))
631 /* Return zero if units are compatible, nonzero otherwise */
634 compareunits(struct unittype * first, struct unittype * second)
637 compareproducts(first->numerator, second->numerator) ||
638 compareproducts(first->denominator, second->denominator);
643 completereduce(struct unittype * unit)
645 if (reduceunit(unit))
653 showanswer(struct unittype * have, struct unittype * want)
657 if (compareunits(have, want)) {
658 printf("conformability error\n");
660 printf("\t%s = ", havestr);
665 printf("\t%s = ", wantstr);
670 else if (have->offset != want->offset) {
672 printf("WARNING: conversion of non-proportional quantities.\n");
675 (have->factor + have->offset-want->offset)/want->factor);
677 printf("\t (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
678 have->factor / want->factor,
679 (have->offset-want->offset)/want->factor,
680 want->factor / have->factor,
681 (want->offset - have->offset)/have->factor);
685 ans = have->factor / want->factor;
687 printf("\t%s = %.8g * %s\n", havestr, ans, wantstr);
689 printf("\t* %.8g\n", ans);
692 printf("\t%s = (1 / %.8g) * %s\n", havestr, 1/ans, wantstr);
694 printf("\t/ %.8g\n", 1/ans);
703 "usage: units [-f unitsfile] [-UVq] [from-unit to-unit]\n");
709 main(int argc, char **argv)
712 struct unittype have, want;
723 while ((optchar = getopt(argc, argv, "fqvUV:")) != -1) {
727 if (strlen(optarg) == 0)
739 if (access(UNITSFILE, F_OK) == 0)
740 printf("%s\n", UNITSFILE);
742 printf("Units data file not found");
746 fprintf(stderr, "FreeBSD units\n");
757 inhistory = history_init();
758 el = el_init(argv[0], stdin, stdout, stderr);
759 el_set(el, EL_PROMPT, &prompt);
760 el_set(el, EL_EDITOR, "emacs");
761 el_set(el, EL_SIGNAL, 1);
762 el_set(el, EL_HIST, history, inhistory);
764 history(inhistory, &ev, H_SETSIZE, 800);
766 err(1, "Could not initalize history");
768 if (cap_enter() < 0 && errno != ENOSYS)
769 err(1, "unable to enter capability mode");
771 if (optind == argc - 2) {
772 havestr = argv[optind];
773 wantstr = argv[optind + 1];
774 initializeunit(&have);
775 addunit(&have, havestr, 0, 1);
776 completereduce(&have);
777 initializeunit(&want);
778 addunit(&want, wantstr, 0, 1);
779 completereduce(&want);
780 showanswer(&have, &want);
784 printf("%d units, %d prefixes\n", unitcount,
788 initializeunit(&have);
790 promptstr = "You have: ";
791 havestr = el_gets(el, &inputsz);
795 history(inhistory, &ev, H_ENTER,
797 } while (addunit(&have, havestr, 0, 1) ||
798 completereduce(&have));
800 initializeunit(&want);
802 promptstr = "You want: ";
803 wantstr = el_gets(el, &inputsz);
807 history(inhistory, &ev, H_ENTER,
809 } while (addunit(&want, wantstr, 0, 1) ||
810 completereduce(&want));
811 showanswer(&have, &want);
815 history_end(inhistory);