]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/units/units.c
units(1): parse config file after setting defaults
[FreeBSD/FreeBSD.git] / usr.bin / units / units.c
1 /*
2  * units.c   Copyright (c) 1993 by Adrian Mariano (adrian@cam.cornell.edu)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
13  *
14  * I would appreciate (though I do not require) receiving a copy of any
15  * improvements you might make to this program.
16  */
17
18 #ifndef lint
19 static const char rcsid[] =
20   "$FreeBSD$";
21 #endif /* not lint */
22
23 #include <ctype.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <histedit.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include <sys/capsicum.h>
34
35 #include "pathnames.h"
36
37 #ifndef UNITSFILE
38 #define UNITSFILE _PATH_UNITSLIB
39 #endif
40
41 #define MAXUNITS 1000
42 #define MAXPREFIXES 100
43
44 #define MAXSUBUNITS 500
45
46 #define PRIMITIVECHAR '!'
47
48 static const char *powerstring = "^";
49
50 static struct {
51         char *uname;
52         char *uval;
53 }      unittable[MAXUNITS];
54
55 struct unittype {
56         char *numerator[MAXSUBUNITS];
57         char *denominator[MAXSUBUNITS];
58         double factor;
59         double offset;
60         int quantity;
61 };
62
63 static struct {
64         char *prefixname;
65         char *prefixval;
66 }      prefixtable[MAXPREFIXES];
67
68
69 static char NULLUNIT[] = "";
70
71 #ifdef MSDOS
72 #define SEPARATOR      ";"
73 #else
74 #define SEPARATOR      ":"
75 #endif
76
77 static int unitcount;
78 static int prefixcount;
79
80 char    *dupstr(const char *str);
81 void     readunits(const char *userfile);
82 void     initializeunit(struct unittype * theunit);
83 int      addsubunit(char *product[], char *toadd);
84 void     showunit(struct unittype * theunit);
85 void     zeroerror(void);
86 int      addunit(struct unittype *theunit, const char *toadd, int flip, int quantity);
87 int      compare(const void *item1, const void *item2);
88 void     sortunit(struct unittype * theunit);
89 void     cancelunit(struct unittype * theunit);
90 char    *lookupunit(const char *unit);
91 int      reduceproduct(struct unittype * theunit, int flip);
92 int      reduceunit(struct unittype * theunit);
93 int      compareproducts(char **one, char **two);
94 int      compareunits(struct unittype * first, struct unittype * second);
95 int      completereduce(struct unittype * unit);
96 void     showanswer(struct unittype * have, struct unittype * want);
97 void     usage(void);
98
99 static const char* promptstr = "";
100
101 static const char * prompt(EditLine *e __unused) {
102         return promptstr;
103 }
104
105 char *
106 dupstr(const char *str)
107 {
108         char *ret;
109
110         ret = malloc(strlen(str) + 1);
111         if (!ret)
112                 errx(3, "memory allocation error");
113         strcpy(ret, str);
114         return (ret);
115 }
116
117
118 void 
119 readunits(const char *userfile)
120 {
121         FILE *unitfile;
122         char line[512], *lineptr;
123         int len, linenum, i;
124         cap_rights_t unitfilerights;
125
126         unitcount = 0;
127         linenum = 0;
128
129         if (userfile) {
130                 unitfile = fopen(userfile, "rt");
131                 if (!unitfile)
132                         errx(1, "unable to open units file '%s'", userfile);
133         }
134         else {
135                 unitfile = fopen(UNITSFILE, "rt");
136                 if (!unitfile) {
137                         char *direc, *env;
138                         char filename[1000];
139
140                         env = getenv("PATH");
141                         if (env) {
142                                 direc = strtok(env, SEPARATOR);
143                                 while (direc) {
144                                         snprintf(filename, sizeof(filename),
145                                             "%s/%s", direc, UNITSFILE);
146                                         unitfile = fopen(filename, "rt");
147                                         if (unitfile)
148                                                 break;
149                                         direc = strtok(NULL, SEPARATOR);
150                                 }
151                         }
152                         if (!unitfile)
153                                 errx(1, "can't find units file '%s'", UNITSFILE);
154                 }
155         }
156         if (cap_enter() < 0 && errno != ENOSYS)
157                 err(1, "unable to enter capability mode");
158         cap_rights_init(&unitfilerights, CAP_READ, CAP_FSTAT);
159         if (cap_rights_limit(fileno(unitfile), &unitfilerights) < 0
160                 && errno != ENOSYS)
161                 err(1, "cap_rights_limit() failed");
162         while (!feof(unitfile)) {
163                 if (!fgets(line, sizeof(line), unitfile))
164                         break;
165                 linenum++;
166                 lineptr = line;
167                 if (*lineptr == '/')
168                         continue;
169                 lineptr += strspn(lineptr, " \n\t");
170                 len = strcspn(lineptr, " \n\t");
171                 lineptr[len] = 0;
172                 if (!strlen(lineptr))
173                         continue;
174                 if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
175                         if (prefixcount == MAXPREFIXES) {
176                                 warnx("memory for prefixes exceeded in line %d", linenum);
177                                 continue;
178                         }
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",
184                                             lineptr, linenum);
185                                         continue;
186                                 }
187                         lineptr += len + 1;
188                         lineptr += strspn(lineptr, " \n\t");
189                         len = strcspn(lineptr, "\n\t");
190                         if (len == 0) {
191                                 warnx("unexpected end of prefix on line %d",
192                                     linenum);
193                                 continue;
194                         }
195                         lineptr[len] = 0;
196                         prefixtable[prefixcount++].prefixval = dupstr(lineptr);
197                 }
198                 else {          /* it's not a prefix */
199                         if (unitcount == MAXUNITS) {
200                                 warnx("memory for units exceeded in line %d", linenum);
201                                 continue;
202                         }
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",
207                                             lineptr, linenum);
208                                         continue;
209                                 }
210                         lineptr += len + 1;
211                         lineptr += strspn(lineptr, " \n\t");
212                         if (!strlen(lineptr)) {
213                                 warnx("unexpected end of unit on line %d",
214                                     linenum);
215                                 continue;
216                         }
217                         len = strcspn(lineptr, "\n\t");
218                         lineptr[len] = 0;
219                         unittable[unitcount++].uval = dupstr(lineptr);
220                 }
221         }
222         fclose(unitfile);
223 }
224
225 void 
226 initializeunit(struct unittype * theunit)
227 {
228         theunit->numerator[0] = theunit->denominator[0] = NULL;
229         theunit->factor = 1.0;
230         theunit->offset = 0.0;
231         theunit->quantity = 0;
232 }
233
234
235 int 
236 addsubunit(char *product[], char *toadd)
237 {
238         char **ptr;
239
240         for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
241         if (ptr >= product + MAXSUBUNITS) {
242                 warnx("memory overflow in unit reduction");
243                 return 1;
244         }
245         if (!*ptr)
246                 *(ptr + 1) = 0;
247         *ptr = dupstr(toadd);
248         return 0;
249 }
250
251
252 void 
253 showunit(struct unittype * theunit)
254 {
255         char **ptr;
256         int printedslash;
257         int counter = 1;
258
259         printf("\t%.8g", theunit->factor);
260         if (theunit->offset)
261                 printf("&%.8g", theunit->offset);
262         for (ptr = theunit->numerator; *ptr; ptr++) {
263                 if (ptr > theunit->numerator && **ptr &&
264                     !strcmp(*ptr, *(ptr - 1)))
265                         counter++;
266                 else {
267                         if (counter > 1)
268                                 printf("%s%d", powerstring, counter);
269                         if (**ptr)
270                                 printf(" %s", *ptr);
271                         counter = 1;
272                 }
273         }
274         if (counter > 1)
275                 printf("%s%d", powerstring, counter);
276         counter = 1;
277         printedslash = 0;
278         for (ptr = theunit->denominator; *ptr; ptr++) {
279                 if (ptr > theunit->denominator && **ptr &&
280                     !strcmp(*ptr, *(ptr - 1)))
281                         counter++;
282                 else {
283                         if (counter > 1)
284                                 printf("%s%d", powerstring, counter);
285                         if (**ptr) {
286                                 if (!printedslash)
287                                         printf(" /");
288                                 printedslash = 1;
289                                 printf(" %s", *ptr);
290                         }
291                         counter = 1;
292                 }
293         }
294         if (counter > 1)
295                 printf("%s%d", powerstring, counter);
296         printf("\n");
297 }
298
299
300 void 
301 zeroerror(void)
302 {
303         warnx("unit reduces to zero");
304 }
305
306 /*
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.
310
311    Returns 0 for successful addition, nonzero on error.
312 */
313
314 int 
315 addunit(struct unittype * theunit, const char *toadd, int flip, int quantity)
316 {
317         char *scratch, *savescr;
318         char *item;
319         char *divider, *slash, *offset;
320         int doingtop;
321
322         if (!strlen(toadd))
323                 return 1;
324         
325         savescr = scratch = dupstr(toadd);
326         for (slash = scratch + 1; *slash; slash++)
327                 if (*slash == '-' &&
328                     (tolower(*(slash - 1)) != 'e' ||
329                     !strchr(".0123456789", *(slash + 1))))
330                         *slash = ' ';
331         slash = strchr(scratch, '/');
332         if (slash)
333                 *slash = 0;
334         doingtop = 1;
335         do {
336                 item = strtok(scratch, " *\t\n/");
337                 while (item) {
338                         if (strchr("0123456789.", *item)) { /* item is a number */
339                                 double num, offsetnum;
340
341                                 if (quantity)
342                                         theunit->quantity = 1;
343
344                                 offset = strchr(item, '&');
345                                 if (offset) {
346                                         *offset = 0;
347                                         offsetnum = atof(offset+1);
348                                 } else
349                                         offsetnum = 0.0;
350
351                                 divider = strchr(item, '|');
352                                 if (divider) {
353                                         *divider = 0;
354                                         num = atof(item);
355                                         if (!num) {
356                                                 zeroerror();
357                                                 return 1;
358                                         }
359                                         if (doingtop ^ flip) {
360                                                 theunit->factor *= num;
361                                                 theunit->offset *= num;
362                                         } else {
363                                                 theunit->factor /= num;
364                                                 theunit->offset /= num;
365                                         }
366                                         num = atof(divider + 1);
367                                         if (!num) {
368                                                 zeroerror();
369                                                 return 1;
370                                         }
371                                         if (doingtop ^ flip) {
372                                                 theunit->factor /= num;
373                                                 theunit->offset /= num;
374                                         } else {
375                                                 theunit->factor *= num;
376                                                 theunit->offset *= num;
377                                         }
378                                 }
379                                 else {
380                                         num = atof(item);
381                                         if (!num) {
382                                                 zeroerror();
383                                                 return 1;
384                                         }
385                                         if (doingtop ^ flip) {
386                                                 theunit->factor *= num;
387                                                 theunit->offset *= num;
388                                         } else {
389                                                 theunit->factor /= num;
390                                                 theunit->offset /= num;
391                                         }
392                                 }
393                                 if (doingtop ^ flip)
394                                         theunit->offset += offsetnum;
395                         }
396                         else {  /* item is not a number */
397                                 int repeat = 1;
398
399                                 if (strchr("23456789",
400                                     item[strlen(item) - 1])) {
401                                         repeat = item[strlen(item) - 1] - '0';
402                                         item[strlen(item) - 1] = 0;
403                                 }
404                                 for (; repeat; repeat--)
405                                         if (addsubunit(doingtop ^ flip ? theunit->numerator : theunit->denominator, item))
406                                                 return 1;
407                         }
408                         item = strtok(NULL, " *\t/\n");
409                 }
410                 doingtop--;
411                 if (slash) {
412                         scratch = slash + 1;
413                 }
414                 else
415                         doingtop--;
416         } while (doingtop >= 0);
417         free(savescr);
418         return 0;
419 }
420
421
422 int 
423 compare(const void *item1, const void *item2)
424 {
425         return strcmp(*(const char * const *)item1, *(const char * const *)item2);
426 }
427
428
429 void 
430 sortunit(struct unittype * theunit)
431 {
432         char **ptr;
433         unsigned int count;
434
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);
439 }
440
441
442 void 
443 cancelunit(struct unittype * theunit)
444 {
445         char **den, **num;
446         int comp;
447
448         den = theunit->denominator;
449         num = theunit->numerator;
450
451         while (*num && *den) {
452                 comp = strcmp(*den, *num);
453                 if (!comp) {
454 /*      if (*den!=NULLUNIT) free(*den);
455       if (*num!=NULLUNIT) free(*num);*/
456                         *den++ = NULLUNIT;
457                         *num++ = NULLUNIT;
458                 }
459                 else if (comp < 0)
460                         den++;
461                 else
462                         num++;
463         }
464 }
465
466
467
468
469 /*
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.
473 */
474
475 static char buffer[100];        /* buffer for lookupunit answers with
476                                    prefixes */
477
478 char *
479 lookupunit(const char *unit)
480 {
481         int i;
482         char *copy;
483
484         for (i = 0; i < unitcount; i++) {
485                 if (!strcmp(unittable[i].uname, unit))
486                         return unittable[i].uval;
487         }
488
489         if (unit[strlen(unit) - 1] == '^') {
490                 copy = dupstr(unit);
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));
495                                 free(copy);
496                                 return buffer;
497                         }
498                 }
499                 free(copy);
500         }
501         if (unit[strlen(unit) - 1] == 's') {
502                 copy = dupstr(unit);
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));
507                                 free(copy);
508                                 return buffer;
509                         }
510                 }
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));
516                                         free(copy);
517                                         return buffer;
518                                 }
519                         }
520                 }
521                 free(copy);
522         }
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);
529                                 return buffer;
530                         }
531                 }
532         }
533         return 0;
534 }
535
536
537
538 /*
539    reduces a product of symbolic units to primitive units.
540    The three low bits are used to return flags:
541
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.
545 */
546
547
548 #define ERROR 4
549
550 int 
551 reduceproduct(struct unittype * theunit, int flip)
552 {
553
554         char *toadd;
555         char **product;
556         int didsomething = 2;
557
558         if (flip)
559                 product = theunit->denominator;
560         else
561                 product = theunit->numerator;
562
563         for (; *product; product++) {
564
565                 for (;;) {
566                         if (!strlen(*product))
567                                 break;
568                         toadd = lookupunit(*product);
569                         if (!toadd) {
570                                 printf("unknown unit '%s'\n", *product);
571                                 return ERROR;
572                         }
573                         if (strchr(toadd, PRIMITIVECHAR))
574                                 break;
575                         didsomething = 1;
576                         if (*product != NULLUNIT) {
577                                 free(*product);
578                                 *product = NULLUNIT;
579                         }
580                         if (addunit(theunit, toadd, flip, 0))
581                                 return ERROR;
582                 }
583         }
584         return didsomething;
585 }
586
587
588 /*
589    Reduces numerator and denominator of the specified unit.
590    Returns 0 on success, or 1 on unknown unit error.
591 */
592
593 int 
594 reduceunit(struct unittype * theunit)
595 {
596         int ret;
597
598         ret = 1;
599         while (ret & 1) {
600                 ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
601                 if (ret & 4)
602                         return 1;
603         }
604         return 0;
605 }
606
607
608 int 
609 compareproducts(char **one, char **two)
610 {
611         while (*one || *two) {
612                 if (!*one && *two != NULLUNIT)
613                         return 1;
614                 if (!*two && *one != NULLUNIT)
615                         return 1;
616                 if (*one == NULLUNIT)
617                         one++;
618                 else if (*two == NULLUNIT)
619                         two++;
620                 else if (strcmp(*one, *two))
621                         return 1;
622                 else
623                         one++, two++;
624         }
625         return 0;
626 }
627
628
629 /* Return zero if units are compatible, nonzero otherwise */
630
631 int 
632 compareunits(struct unittype * first, struct unittype * second)
633 {
634         return
635         compareproducts(first->numerator, second->numerator) ||
636         compareproducts(first->denominator, second->denominator);
637 }
638
639
640 int 
641 completereduce(struct unittype * unit)
642 {
643         if (reduceunit(unit))
644                 return 1;
645         sortunit(unit);
646         cancelunit(unit);
647         return 0;
648 }
649
650
651 void 
652 showanswer(struct unittype * have, struct unittype * want)
653 {
654         if (compareunits(have, want)) {
655                 printf("conformability error\n");
656                 showunit(have);
657                 showunit(want);
658         }
659         else if (have->offset != want->offset) {
660                 if (want->quantity)
661                         printf("WARNING: conversion of non-proportional quantities.\n");
662                 printf("\t");
663                 if (have->quantity)
664                         printf("%.8g\n",
665                             (have->factor + have->offset-want->offset)/want->factor);
666                 else
667                         printf(" (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
668                             have->factor / want->factor,
669                             (have->offset-want->offset)/want->factor,
670                             want->factor / have->factor,
671                             (want->offset - have->offset)/have->factor);
672         }
673         else
674                 printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,
675                     want->factor / have->factor);
676 }
677
678
679 void 
680 usage(void)
681 {
682         fprintf(stderr,
683                 "usage: units [-f unitsfile] [-q] [-v] [from-unit to-unit]\n");
684         exit(3);
685 }
686
687
688 int
689 main(int argc, char **argv)
690 {
691
692         struct unittype have, want;
693         const char * havestr;
694         const char * wantstr;
695         int optchar;
696         char *userfile;
697         bool quiet;
698         History *inhistory;
699         EditLine *el;
700         HistEvent ev;
701         int inputsz;
702
703         userfile = NULL;
704         quiet = false;
705         while ((optchar = getopt(argc, argv, "Vqf:")) != -1) {
706                 switch (optchar) {
707                 case 'f':
708                         userfile = optarg;
709                         break;
710                 case 'q':
711                         quiet = true;
712                         break;
713                 case 'V':
714                         fprintf(stderr, "FreeBSD units\n");
715                         usage();
716                         break;
717                 default:
718                         usage();
719                 }
720         }
721
722         if (optind != argc - 2 && optind != argc)
723                 usage();
724
725         inhistory = history_init();
726         el = el_init(argv[0], stdin, stdout, stderr);
727         el_set(el, EL_PROMPT, &prompt);
728         el_set(el, EL_EDITOR, "emacs");
729         el_set(el, EL_SIGNAL, 1);
730         el_set(el, EL_HIST, history, inhistory);
731         el_source(el, NULL);
732         history(inhistory, &ev, H_SETSIZE, 800);
733         if (inhistory == 0)
734                 err(1, "Could not initalize history");
735         
736         readunits(userfile);
737
738         if (optind == argc - 2) {
739                 havestr = argv[optind];
740                 wantstr = argv[optind + 1];
741                 initializeunit(&have);
742                 addunit(&have, havestr, 0, 1);
743                 completereduce(&have);
744                 initializeunit(&want);
745                 addunit(&want, wantstr, 0, 1);
746                 completereduce(&want);
747                 showanswer(&have, &want);
748         }
749         else {
750                 if (!quiet)
751                         printf("%d units, %d prefixes\n", unitcount,
752                             prefixcount);
753                 for (;;) {
754                         do {
755                                 initializeunit(&have);
756                                 if (!quiet)
757                                         promptstr = "You have: ";
758                                 havestr = el_gets(el, &inputsz);
759                                 if (havestr == NULL)
760                                         exit(0);
761                                 if (inputsz > 0)
762                                         history(inhistory, &ev, H_ENTER,
763                                         havestr);
764                         } while (addunit(&have, havestr, 0, 1) ||
765                             completereduce(&have));
766                         do {
767                                 initializeunit(&want);
768                                 if (!quiet)
769                                         promptstr = "You want: ";
770                                 wantstr = el_gets(el, &inputsz);
771                                 if (wantstr == NULL)
772                                         exit(0);
773                                 if (inputsz > 0)
774                                         history(inhistory, &ev, H_ENTER,
775                                         wantstr);
776                         } while (addunit(&want, wantstr, 0, 1) ||
777                             completereduce(&want));
778                         showanswer(&have, &want);
779                 }
780         }
781
782         history_end(inhistory);
783         return(0);
784 }