2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * units.c Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 * Disclaimer: This software is provided by the author "as is". The author
14 * shall not be liable for any damages caused in any way by this software.
16 * I would appreciate (though I do not require) receiving a copy of any
17 * improvements you might make to this program.
21 static const char rcsid[] =
36 #include <sys/capsicum.h>
39 #define UNITSFILE "/usr/share/misc/definitions.units"
43 #define MAXPREFIXES 100
45 #define MAXSUBUNITS 500
47 #define PRIMITIVECHAR '!'
49 static const char *powerstring = "^";
50 static const char *numfmt = "%.8g";
55 } unittable[MAXUNITS];
58 char *numerator[MAXSUBUNITS];
59 char *denominator[MAXSUBUNITS];
68 } prefixtable[MAXPREFIXES];
71 static char NULLUNIT[] = "";
76 static int prefixcount;
77 static bool verbose = false;
78 static bool terse = false;
79 static const char * outputformat;
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, "r");
134 errx(1, "unable to open units file '%s'", userfile);
137 unitfile = fopen(UNITSFILE, "r");
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))
167 if (*lineptr == '/' || *lineptr == '#')
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(numfmt, 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, '|');
360 if (doingtop ^ flip) {
361 theunit->factor *= num;
362 theunit->offset *= num;
364 theunit->factor /= num;
365 theunit->offset /= num;
367 num = atof(divider + 1);
373 if (doingtop ^ flip) {
374 theunit->factor /= num;
375 theunit->offset /= num;
377 theunit->factor *= num;
378 theunit->offset *= num;
388 if (doingtop ^ flip) {
389 theunit->factor *= num;
390 theunit->offset *= num;
392 theunit->factor /= num;
393 theunit->offset /= num;
397 theunit->offset += offsetnum;
399 else { /* item is not a number */
402 if (strchr("23456789",
403 item[strlen(item) - 1])) {
404 repeat = item[strlen(item) - 1] - '0';
405 item[strlen(item) - 1] = 0;
407 for (; repeat; repeat--) {
408 if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item)) {
414 item = strtok(NULL, " *\t/\n");
422 } while (doingtop >= 0);
429 compare(const void *item1, const void *item2)
431 return strcmp(*(const char * const *)item1, *(const char * const *)item2);
436 sortunit(struct unittype * theunit)
441 for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
442 qsort(theunit->numerator, count, sizeof(char *), compare);
443 for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
444 qsort(theunit->denominator, count, sizeof(char *), compare);
449 cancelunit(struct unittype * theunit)
454 den = theunit->denominator;
455 num = theunit->numerator;
457 while (*num && *den) {
458 comp = strcmp(*den, *num);
460 /* if (*den!=NULLUNIT) free(*den);
461 if (*num!=NULLUNIT) free(*num);*/
476 Looks up the definition for the specified unit.
477 Returns a pointer to the definition or a null pointer
478 if the specified unit does not appear in the units table.
481 static char buffer[100]; /* buffer for lookupunit answers with
485 lookupunit(const char *unit)
490 for (i = 0; i < unitcount; i++) {
491 if (!strcmp(unittable[i].uname, unit))
492 return unittable[i].uval;
495 if (unit[strlen(unit) - 1] == '^') {
497 copy[strlen(copy) - 1] = 0;
498 for (i = 0; i < unitcount; i++) {
499 if (!strcmp(unittable[i].uname, copy)) {
500 strlcpy(buffer, copy, sizeof(buffer));
507 if (unit[strlen(unit) - 1] == 's') {
509 copy[strlen(copy) - 1] = 0;
510 for (i = 0; i < unitcount; i++) {
511 if (!strcmp(unittable[i].uname, copy)) {
512 strlcpy(buffer, copy, sizeof(buffer));
517 if (copy[strlen(copy) - 1] == 'e') {
518 copy[strlen(copy) - 1] = 0;
519 for (i = 0; i < unitcount; i++) {
520 if (!strcmp(unittable[i].uname, copy)) {
521 strlcpy(buffer, copy, sizeof(buffer));
529 for (i = 0; i < prefixcount; i++) {
530 size_t len = strlen(prefixtable[i].prefixname);
531 if (!strncmp(prefixtable[i].prefixname, unit, len)) {
532 if (!strlen(unit + len) || lookupunit(unit + len)) {
533 snprintf(buffer, sizeof(buffer), "%s %s",
534 prefixtable[i].prefixval, unit + len);
545 reduces a product of symbolic units to primitive units.
546 The three low bits are used to return flags:
548 bit 0 (1) set on if reductions were performed without error.
549 bit 1 (2) set on if no reductions are performed.
550 bit 2 (4) set on if an unknown unit is discovered.
557 reduceproduct(struct unittype * theunit, int flip)
562 int didsomething = 2;
565 product = theunit->denominator;
567 product = theunit->numerator;
569 for (; *product; product++) {
572 if (!strlen(*product))
574 toadd = lookupunit(*product);
576 printf("unknown unit '%s'\n", *product);
579 if (strchr(toadd, PRIMITIVECHAR))
582 if (*product != NULLUNIT) {
586 if (addunit(theunit, toadd, flip, 0))
595 Reduces numerator and denominator of the specified unit.
596 Returns 0 on success, or 1 on unknown unit error.
600 reduceunit(struct unittype * theunit)
606 ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
615 compareproducts(char **one, char **two)
617 while (*one || *two) {
618 if (!*one && *two != NULLUNIT)
620 if (!*two && *one != NULLUNIT)
622 if (*one == NULLUNIT)
624 else if (*two == NULLUNIT)
626 else if (strcmp(*one, *two))
635 /* Return zero if units are compatible, nonzero otherwise */
638 compareunits(struct unittype * first, struct unittype * second)
641 compareproducts(first->numerator, second->numerator) ||
642 compareproducts(first->denominator, second->denominator);
647 completereduce(struct unittype * unit)
649 if (reduceunit(unit))
657 showanswer(struct unittype * have, struct unittype * want)
662 if (compareunits(have, want)) {
663 printf("conformability error\n");
665 printf("\t%s = ", havestr);
671 printf("\t%s = ", wantstr);
677 else if (have->offset != want->offset) {
679 printf("WARNING: conversion of non-proportional quantities.\n");
680 if (have->quantity) {
681 asprintf(&oformat, "\t%s\n", outputformat);
683 (have->factor + have->offset-want->offset)/want->factor);
687 asprintf(&oformat, "\t (-> x*%sg %sg)\n\t (<- y*%sg %sg)\n",
688 outputformat, outputformat, outputformat, outputformat);
690 have->factor / want->factor,
691 (have->offset-want->offset)/want->factor,
692 want->factor / have->factor,
693 (want->offset - have->offset)/have->factor);
697 ans = have->factor / want->factor;
700 printf("\t%s = ", havestr);
701 printf(outputformat, ans);
702 printf(" * %s", wantstr);
706 printf(outputformat, ans);
711 printf(outputformat, ans);
716 printf("\t%s = (1 / ", havestr);
717 printf(outputformat, 1/ans);
718 printf(") * %s\n", wantstr);
722 printf(outputformat, 1/ans);
733 "usage: units [-f unitsfile] [-H historyfile] [-UVq] [from-unit to-unit]\n");
737 static struct option longopts[] = {
738 {"help", no_argument, NULL, 'h'},
739 {"exponential", no_argument, NULL, 'e'},
740 {"file", required_argument, NULL, 'f'},
741 {"history", required_argument, NULL, 'H'},
742 {"output-format", required_argument, NULL, 'o'},
743 {"quiet", no_argument, NULL, 'q'},
744 {"terse", no_argument, NULL, 't'},
745 {"unitsfile", no_argument, NULL, 'U'},
746 {"verbose", no_argument, NULL, 'v'},
747 {"version", no_argument, NULL, 'V'},
753 main(int argc, char **argv)
756 struct unittype have, want;
765 char const * history_file;
770 outputformat = numfmt;
772 while ((optchar = getopt_long(argc, argv, "+ehf:o:qtvH:UV", longopts, NULL)) != -1) {
775 outputformat = "%6e";
779 if (strlen(optarg) == 0)
785 history_file = optarg;
794 outputformat = optarg;
800 fprintf(stderr, "FreeBSD units\n");
803 if (access(UNITSFILE, F_OK) == 0)
804 printf("%s\n", UNITSFILE);
806 printf("Units data file not found");
820 if (optind == argc - 2) {
821 if (cap_enter() < 0 && errno != ENOSYS)
822 err(1, "unable to enter capability mode");
824 havestr = argv[optind];
825 wantstr = argv[optind + 1];
826 initializeunit(&have);
827 addunit(&have, havestr, 0, 1);
828 completereduce(&have);
829 initializeunit(&want);
830 addunit(&want, wantstr, 0, 1);
831 completereduce(&want);
832 showanswer(&have, &want);
834 inhistory = history_init();
835 el = el_init(argv[0], stdin, stdout, stderr);
836 el_set(el, EL_PROMPT, &prompt);
837 el_set(el, EL_EDITOR, "emacs");
838 el_set(el, EL_SIGNAL, 1);
839 el_set(el, EL_HIST, history, inhistory);
841 history(inhistory, &ev, H_SETSIZE, 800);
843 err(1, "Could not initialize history");
845 if (cap_enter() < 0 && errno != ENOSYS)
846 err(1, "unable to enter capability mode");
849 printf("%d units, %d prefixes\n", unitcount,
853 initializeunit(&have);
855 promptstr = "You have: ";
856 havestr = el_gets(el, &inputsz);
857 if (havestr == NULL) {
862 history(inhistory, &ev, H_ENTER,
864 } while (addunit(&have, havestr, 0, 1) ||
865 completereduce(&have));
870 initializeunit(&want);
872 promptstr = "You want: ";
873 wantstr = el_gets(el, &inputsz);
874 if (wantstr == NULL) {
879 history(inhistory, &ev, H_ENTER,
881 } while (addunit(&want, wantstr, 0, 1) ||
882 completereduce(&want));
886 showanswer(&have, &want);
889 history_end(inhistory);