units.c revision 1.26 1 1.26 dholland /* $NetBSD: units.c,v 1.26 2016/02/05 03:30:08 dholland 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.26 dholland #include <assert.h>
21 1.3 jtc #include <ctype.h>
22 1.7 lukem #include <err.h>
23 1.21 apb #include <float.h>
24 1.1 cgd #include <stdio.h>
25 1.1 cgd #include <string.h>
26 1.1 cgd #include <stdlib.h>
27 1.8 perry #include <unistd.h>
28 1.1 cgd
29 1.1 cgd #include "pathnames.h"
30 1.1 cgd
31 1.1 cgd #define VERSION "1.0"
32 1.1 cgd
33 1.1 cgd #ifndef UNITSFILE
34 1.1 cgd #define UNITSFILE _PATH_UNITSLIB
35 1.1 cgd #endif
36 1.1 cgd
37 1.1 cgd #define MAXUNITS 1000
38 1.1 cgd #define MAXPREFIXES 50
39 1.1 cgd
40 1.1 cgd #define MAXSUBUNITS 500
41 1.1 cgd
42 1.1 cgd #define PRIMITIVECHAR '!'
43 1.1 cgd
44 1.21 apb static int precision = 8; /* for printf with "%.*g" format */
45 1.21 apb
46 1.21 apb static const char *errprefix = NULL; /* if not NULL, then prepend this
47 1.21 apb * to error messages and send them to
48 1.21 apb * stdout instead of stderr.
49 1.21 apb */
50 1.21 apb
51 1.17 joerg static const char *powerstring = "^";
52 1.1 cgd
53 1.17 joerg static struct {
54 1.16 lukem const char *uname;
55 1.16 lukem const char *uval;
56 1.1 cgd } unittable[MAXUNITS];
57 1.1 cgd
58 1.1 cgd struct unittype {
59 1.16 lukem const char *numerator[MAXSUBUNITS];
60 1.16 lukem const char *denominator[MAXSUBUNITS];
61 1.1 cgd double factor;
62 1.1 cgd };
63 1.1 cgd
64 1.1 cgd struct {
65 1.16 lukem const char *prefixname;
66 1.16 lukem const char *prefixval;
67 1.1 cgd } prefixtable[MAXPREFIXES];
68 1.1 cgd
69 1.1 cgd
70 1.17 joerg static const char *NULLUNIT = "";
71 1.1 cgd
72 1.17 joerg static int unitcount;
73 1.17 joerg static int prefixcount;
74 1.1 cgd
75 1.7 lukem
76 1.17 joerg static int addsubunit(const char *[], const char *);
77 1.17 joerg static int addunit(struct unittype *, const char *, int);
78 1.17 joerg static void cancelunit(struct unittype *);
79 1.17 joerg static int compare(const void *, const void *);
80 1.17 joerg static int compareproducts(const char **, const char **);
81 1.17 joerg static int compareunits(struct unittype *, struct unittype *);
82 1.17 joerg static int compareunitsreciprocal(struct unittype *, struct unittype *);
83 1.17 joerg static int completereduce(struct unittype *);
84 1.17 joerg static void initializeunit(struct unittype *);
85 1.17 joerg static void readerror(int);
86 1.17 joerg static void readunits(const char *);
87 1.17 joerg static int reduceproduct(struct unittype *, int);
88 1.17 joerg static int reduceunit(struct unittype *);
89 1.17 joerg static void showanswer(struct unittype *, struct unittype *);
90 1.17 joerg static void showunit(struct unittype *);
91 1.17 joerg static void sortunit(struct unittype *);
92 1.17 joerg __dead static void usage(void);
93 1.17 joerg static void zeroerror(void);
94 1.17 joerg static char *dupstr(const char *);
95 1.17 joerg static const char *lookupunit(const char *);
96 1.7 lukem
97 1.17 joerg static char *
98 1.16 lukem dupstr(const char *str)
99 1.1 cgd {
100 1.1 cgd char *ret;
101 1.1 cgd
102 1.12 itojun ret = strdup(str);
103 1.7 lukem if (!ret)
104 1.7 lukem err(3, "Memory allocation error");
105 1.1 cgd return (ret);
106 1.1 cgd }
107 1.1 cgd
108 1.1 cgd
109 1.25 joerg static __printflike(1, 2) void
110 1.21 apb mywarnx(const char *fmt, ...)
111 1.21 apb {
112 1.21 apb va_list args;
113 1.21 apb
114 1.21 apb va_start(args, fmt);
115 1.21 apb if (errprefix) {
116 1.21 apb /* warn to stdout, with errprefix prepended */
117 1.21 apb printf("%s", errprefix);
118 1.21 apb vprintf(fmt, args);
119 1.21 apb printf("%s", "\n");
120 1.21 apb } else {
121 1.21 apb /* warn to stderr */
122 1.21 apb vwarnx(fmt, args);
123 1.21 apb }
124 1.21 apb va_end(args);
125 1.21 apb }
126 1.21 apb
127 1.21 apb static void
128 1.1 cgd readerror(int linenum)
129 1.1 cgd {
130 1.21 apb mywarnx("Error in units file '%s' line %d", UNITSFILE, linenum);
131 1.1 cgd }
132 1.1 cgd
133 1.1 cgd
134 1.17 joerg static void
135 1.16 lukem readunits(const char *userfile)
136 1.1 cgd {
137 1.1 cgd FILE *unitfile;
138 1.1 cgd char line[80], *lineptr;
139 1.20 apb int len, linenum, i, isdup;
140 1.1 cgd
141 1.1 cgd unitcount = 0;
142 1.1 cgd linenum = 0;
143 1.1 cgd
144 1.1 cgd if (userfile) {
145 1.1 cgd unitfile = fopen(userfile, "rt");
146 1.7 lukem if (!unitfile)
147 1.7 lukem err(1, "Unable to open units file '%s'", userfile);
148 1.1 cgd }
149 1.1 cgd else {
150 1.1 cgd unitfile = fopen(UNITSFILE, "rt");
151 1.1 cgd if (!unitfile) {
152 1.1 cgd char *direc, *env;
153 1.1 cgd char filename[1000];
154 1.1 cgd char separator[2];
155 1.1 cgd
156 1.1 cgd env = getenv("PATH");
157 1.1 cgd if (env) {
158 1.1 cgd if (strchr(env, ';'))
159 1.12 itojun strlcpy(separator, ";",
160 1.12 itojun sizeof(separator));
161 1.1 cgd else
162 1.12 itojun strlcpy(separator, ":",
163 1.12 itojun sizeof(separator));
164 1.1 cgd direc = strtok(env, separator);
165 1.1 cgd while (direc) {
166 1.12 itojun strlcpy(filename, "", sizeof(filename));
167 1.12 itojun strlcat(filename, direc,
168 1.12 itojun sizeof(filename));
169 1.12 itojun strlcat(filename, "/",
170 1.12 itojun sizeof(filename));
171 1.12 itojun strlcat(filename, UNITSFILE,
172 1.12 itojun sizeof(filename));
173 1.1 cgd unitfile = fopen(filename, "rt");
174 1.1 cgd if (unitfile)
175 1.1 cgd break;
176 1.1 cgd direc = strtok(NULL, separator);
177 1.1 cgd }
178 1.1 cgd }
179 1.7 lukem if (!unitfile)
180 1.7 lukem errx(1, "Can't find units file '%s'",
181 1.1 cgd UNITSFILE);
182 1.1 cgd }
183 1.1 cgd }
184 1.1 cgd while (!feof(unitfile)) {
185 1.1 cgd if (!fgets(line, 79, unitfile))
186 1.1 cgd break;
187 1.1 cgd linenum++;
188 1.1 cgd lineptr = line;
189 1.1 cgd if (*lineptr == '/')
190 1.1 cgd continue;
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 if (!strlen(lineptr))
195 1.1 cgd continue;
196 1.1 cgd if (lineptr[strlen(lineptr) - 1] == '-') { /* it's a prefix */
197 1.1 cgd if (prefixcount == MAXPREFIXES) {
198 1.21 apb mywarnx(
199 1.21 apb "Memory for prefixes exceeded in line %d",
200 1.21 apb linenum);
201 1.1 cgd continue;
202 1.1 cgd }
203 1.1 cgd lineptr[strlen(lineptr) - 1] = 0;
204 1.20 apb for (isdup = 0, i = 0; i < prefixcount; i++) {
205 1.20 apb if (!strcmp(prefixtable[i].prefixname,
206 1.20 apb lineptr)) {
207 1.20 apb isdup = 1;
208 1.20 apb break;
209 1.20 apb }
210 1.20 apb }
211 1.20 apb if (isdup) {
212 1.21 apb mywarnx(
213 1.20 apb "Redefinition of prefix '%s' on line %d ignored",
214 1.20 apb lineptr, linenum);
215 1.20 apb continue;
216 1.20 apb }
217 1.1 cgd prefixtable[prefixcount].prefixname = dupstr(lineptr);
218 1.1 cgd lineptr += len + 1;
219 1.1 cgd if (!strlen(lineptr)) {
220 1.1 cgd readerror(linenum);
221 1.1 cgd continue;
222 1.1 cgd }
223 1.1 cgd lineptr += strspn(lineptr, " \n\t");
224 1.1 cgd len = strcspn(lineptr, "\n\t");
225 1.1 cgd lineptr[len] = 0;
226 1.1 cgd prefixtable[prefixcount++].prefixval = dupstr(lineptr);
227 1.1 cgd }
228 1.1 cgd else { /* it's not a prefix */
229 1.1 cgd if (unitcount == MAXUNITS) {
230 1.21 apb mywarnx("Memory for units exceeded in line %d",
231 1.1 cgd linenum);
232 1.1 cgd continue;
233 1.1 cgd }
234 1.20 apb for (isdup = 0, i = 0; i < unitcount; i++) {
235 1.1 cgd if (!strcmp(unittable[i].uname, lineptr)) {
236 1.20 apb isdup = 1;
237 1.20 apb break;
238 1.20 apb }
239 1.20 apb }
240 1.20 apb if (isdup) {
241 1.21 apb mywarnx(
242 1.7 lukem "Redefinition of unit '%s' on line %d ignored",
243 1.20 apb lineptr, linenum);
244 1.20 apb continue;
245 1.20 apb }
246 1.20 apb unittable[unitcount].uname = dupstr(lineptr);
247 1.1 cgd lineptr += len + 1;
248 1.1 cgd lineptr += strspn(lineptr, " \n\t");
249 1.1 cgd if (!strlen(lineptr)) {
250 1.1 cgd readerror(linenum);
251 1.1 cgd continue;
252 1.1 cgd }
253 1.1 cgd len = strcspn(lineptr, "\n\t");
254 1.1 cgd lineptr[len] = 0;
255 1.1 cgd unittable[unitcount++].uval = dupstr(lineptr);
256 1.1 cgd }
257 1.1 cgd }
258 1.1 cgd fclose(unitfile);
259 1.1 cgd }
260 1.1 cgd
261 1.17 joerg static void
262 1.1 cgd initializeunit(struct unittype * theunit)
263 1.1 cgd {
264 1.1 cgd theunit->factor = 1.0;
265 1.1 cgd theunit->numerator[0] = theunit->denominator[0] = NULL;
266 1.1 cgd }
267 1.1 cgd
268 1.17 joerg static int
269 1.16 lukem addsubunit(const char *product[], const char *toadd)
270 1.1 cgd {
271 1.16 lukem const char **ptr;
272 1.1 cgd
273 1.1 cgd for (ptr = product; *ptr && *ptr != NULLUNIT; ptr++);
274 1.1 cgd if (ptr >= product + MAXSUBUNITS) {
275 1.21 apb mywarnx("Memory overflow in unit reduction");
276 1.1 cgd return 1;
277 1.1 cgd }
278 1.1 cgd if (!*ptr)
279 1.1 cgd *(ptr + 1) = 0;
280 1.1 cgd *ptr = dupstr(toadd);
281 1.1 cgd return 0;
282 1.1 cgd }
283 1.1 cgd
284 1.17 joerg static void
285 1.1 cgd showunit(struct unittype * theunit)
286 1.1 cgd {
287 1.16 lukem const char **ptr;
288 1.1 cgd int printedslash;
289 1.1 cgd int counter = 1;
290 1.1 cgd
291 1.21 apb printf("\t%.*g", precision, theunit->factor);
292 1.1 cgd for (ptr = theunit->numerator; *ptr; ptr++) {
293 1.1 cgd if (ptr > theunit->numerator && **ptr &&
294 1.1 cgd !strcmp(*ptr, *(ptr - 1)))
295 1.1 cgd counter++;
296 1.1 cgd else {
297 1.1 cgd if (counter > 1)
298 1.1 cgd printf("%s%d", powerstring, counter);
299 1.1 cgd if (**ptr)
300 1.1 cgd printf(" %s", *ptr);
301 1.1 cgd counter = 1;
302 1.1 cgd }
303 1.1 cgd }
304 1.1 cgd if (counter > 1)
305 1.1 cgd printf("%s%d", powerstring, counter);
306 1.1 cgd counter = 1;
307 1.1 cgd printedslash = 0;
308 1.1 cgd for (ptr = theunit->denominator; *ptr; ptr++) {
309 1.1 cgd if (ptr > theunit->denominator && **ptr &&
310 1.1 cgd !strcmp(*ptr, *(ptr - 1)))
311 1.1 cgd counter++;
312 1.1 cgd else {
313 1.1 cgd if (counter > 1)
314 1.1 cgd printf("%s%d", powerstring, counter);
315 1.1 cgd if (**ptr) {
316 1.1 cgd if (!printedslash)
317 1.1 cgd printf(" /");
318 1.1 cgd printedslash = 1;
319 1.1 cgd printf(" %s", *ptr);
320 1.1 cgd }
321 1.1 cgd counter = 1;
322 1.1 cgd }
323 1.1 cgd }
324 1.1 cgd if (counter > 1)
325 1.1 cgd printf("%s%d", powerstring, counter);
326 1.1 cgd printf("\n");
327 1.1 cgd }
328 1.1 cgd
329 1.17 joerg static void
330 1.18 matt zeroerror(void)
331 1.1 cgd {
332 1.21 apb mywarnx("Unit reduces to zero");
333 1.1 cgd }
334 1.1 cgd
335 1.1 cgd /*
336 1.1 cgd Adds the specified string to the unit.
337 1.1 cgd Flip is 0 for adding normally, 1 for adding reciprocal.
338 1.1 cgd
339 1.1 cgd Returns 0 for successful addition, nonzero on error.
340 1.1 cgd */
341 1.1 cgd
342 1.17 joerg static int
343 1.16 lukem addunit(struct unittype * theunit, const char *toadd, int flip)
344 1.1 cgd {
345 1.1 cgd char *scratch, *savescr;
346 1.1 cgd char *item;
347 1.3 jtc char *divider, *slash;
348 1.26 dholland char *minus;
349 1.26 dholland size_t pos, len;
350 1.1 cgd int doingtop;
351 1.1 cgd
352 1.1 cgd savescr = scratch = dupstr(toadd);
353 1.26 dholland
354 1.26 dholland /*
355 1.26 dholland * "foot-pound" is the same as "foot pound". But don't
356 1.26 dholland * trash minus signs on numbers.
357 1.26 dholland *
358 1.26 dholland * 20160204 dholland: this used to let through only minus
359 1.26 dholland * signs at the beginning of the string or in the middle of a
360 1.26 dholland * floating constant (e.g. 3.6e-5), and a minus sign at the
361 1.26 dholland * beginning of the string failed further on. I have changed
362 1.26 dholland * it so any minus sign before a digit (or decimal point) is
363 1.26 dholland * treated as going with that digit.
364 1.26 dholland *
365 1.26 dholland * Note that this changed the interpretation of certain
366 1.26 dholland * marginally valid inputs like "3 N-5 s"; that used to be
367 1.26 dholland * interpreted as "3 N 5 s" or 15 N s, but now it reads as
368 1.26 dholland * "3 N -5 s" or -15 N s. However, it also makes negative
369 1.26 dholland * exponents on units work, which used to be silently trashed.
370 1.26 dholland */
371 1.26 dholland for (minus = scratch + 1; *minus; minus++) {
372 1.26 dholland if (*minus != '-') {
373 1.26 dholland continue;
374 1.26 dholland }
375 1.26 dholland if (strchr(".0123456789", *(minus + 1))) {
376 1.26 dholland continue;
377 1.26 dholland }
378 1.26 dholland *minus = ' ';
379 1.26 dholland }
380 1.26 dholland
381 1.26 dholland /* Process up to the next / in one go. */
382 1.26 dholland
383 1.1 cgd slash = strchr(scratch, '/');
384 1.1 cgd if (slash)
385 1.1 cgd *slash = 0;
386 1.1 cgd doingtop = 1;
387 1.1 cgd do {
388 1.1 cgd item = strtok(scratch, " *\t\n/");
389 1.1 cgd while (item) {
390 1.26 dholland if ((*item == '-' && strchr("0123456789.", *(item+1)))
391 1.26 dholland || strchr("0123456789.", *item)) {
392 1.26 dholland
393 1.19 apb /* item starts with a number */
394 1.19 apb char *endptr;
395 1.1 cgd double num;
396 1.1 cgd
397 1.1 cgd divider = strchr(item, '|');
398 1.1 cgd if (divider) {
399 1.1 cgd *divider = 0;
400 1.19 apb num = strtod(item, &endptr);
401 1.1 cgd if (!num) {
402 1.1 cgd zeroerror();
403 1.1 cgd return 1;
404 1.1 cgd }
405 1.19 apb if (endptr != divider) {
406 1.19 apb /* "6foo|2" is an error */
407 1.21 apb mywarnx("Junk before '|'");
408 1.19 apb return 1;
409 1.19 apb }
410 1.1 cgd if (doingtop ^ flip)
411 1.1 cgd theunit->factor *= num;
412 1.1 cgd else
413 1.1 cgd theunit->factor /= num;
414 1.19 apb num = strtod(divider + 1, &endptr);
415 1.1 cgd if (!num) {
416 1.1 cgd zeroerror();
417 1.1 cgd return 1;
418 1.1 cgd }
419 1.1 cgd if (doingtop ^ flip)
420 1.1 cgd theunit->factor /= num;
421 1.1 cgd else
422 1.1 cgd theunit->factor *= num;
423 1.19 apb if (*endptr) {
424 1.19 apb /* "6|2foo" is like "6|2 foo" */
425 1.19 apb item = endptr;
426 1.19 apb continue;
427 1.19 apb }
428 1.1 cgd }
429 1.1 cgd else {
430 1.19 apb num = strtod(item, &endptr);
431 1.1 cgd if (!num) {
432 1.1 cgd zeroerror();
433 1.1 cgd return 1;
434 1.1 cgd }
435 1.1 cgd if (doingtop ^ flip)
436 1.1 cgd theunit->factor *= num;
437 1.1 cgd else
438 1.1 cgd theunit->factor /= num;
439 1.19 apb if (*endptr) {
440 1.19 apb /* "3foo" is like "3 foo" */
441 1.19 apb item = endptr;
442 1.19 apb continue;
443 1.19 apb }
444 1.1 cgd }
445 1.1 cgd }
446 1.1 cgd else { /* item is not a number */
447 1.1 cgd int repeat = 1;
448 1.26 dholland int flipthis = 0;
449 1.1 cgd
450 1.26 dholland pos = len = strlen(item);
451 1.26 dholland assert(pos > 0);
452 1.26 dholland while (strchr("0123456789", item[pos - 1])) {
453 1.26 dholland pos--;
454 1.26 dholland /* string began with non-digit */
455 1.26 dholland assert(pos > 0);
456 1.26 dholland }
457 1.26 dholland if (pos < len) {
458 1.26 dholland if (pos > 0 && item[pos - 1] == '-') {
459 1.26 dholland /* allow negative exponents */
460 1.26 dholland pos--;
461 1.26 dholland }
462 1.26 dholland /* have an exponent */
463 1.26 dholland repeat = strtol(item + pos, NULL, 10);
464 1.26 dholland item[pos] = 0;
465 1.26 dholland if (repeat == 0) {
466 1.26 dholland /* not really the right msg */
467 1.26 dholland zeroerror();
468 1.26 dholland return 1;
469 1.26 dholland }
470 1.26 dholland if (repeat < 0) {
471 1.26 dholland flipthis = 1;
472 1.26 dholland repeat = -repeat;
473 1.26 dholland }
474 1.1 cgd }
475 1.26 dholland flipthis ^= doingtop ^ flip;
476 1.1 cgd for (; repeat; repeat--)
477 1.26 dholland if (addsubunit(flipthis ? theunit->numerator : theunit->denominator, item))
478 1.1 cgd return 1;
479 1.1 cgd }
480 1.1 cgd item = strtok(NULL, " *\t/\n");
481 1.1 cgd }
482 1.1 cgd doingtop--;
483 1.1 cgd if (slash) {
484 1.1 cgd scratch = slash + 1;
485 1.1 cgd }
486 1.1 cgd else
487 1.1 cgd doingtop--;
488 1.1 cgd } while (doingtop >= 0);
489 1.1 cgd free(savescr);
490 1.1 cgd return 0;
491 1.1 cgd }
492 1.1 cgd
493 1.17 joerg static int
494 1.1 cgd compare(const void *item1, const void *item2)
495 1.1 cgd {
496 1.16 lukem return strcmp(*(const char * const *) item1,
497 1.16 lukem *(const char * const *) item2);
498 1.1 cgd }
499 1.1 cgd
500 1.17 joerg static void
501 1.1 cgd sortunit(struct unittype * theunit)
502 1.1 cgd {
503 1.16 lukem const char **ptr;
504 1.1 cgd int count;
505 1.1 cgd
506 1.1 cgd for (count = 0, ptr = theunit->numerator; *ptr; ptr++, count++);
507 1.1 cgd qsort(theunit->numerator, count, sizeof(char *), compare);
508 1.1 cgd for (count = 0, ptr = theunit->denominator; *ptr; ptr++, count++);
509 1.1 cgd qsort(theunit->denominator, count, sizeof(char *), compare);
510 1.1 cgd }
511 1.1 cgd
512 1.17 joerg static void
513 1.1 cgd cancelunit(struct unittype * theunit)
514 1.1 cgd {
515 1.16 lukem const char **den, **num;
516 1.1 cgd int comp;
517 1.1 cgd
518 1.1 cgd den = theunit->denominator;
519 1.1 cgd num = theunit->numerator;
520 1.1 cgd
521 1.1 cgd while (*num && *den) {
522 1.1 cgd comp = strcmp(*den, *num);
523 1.1 cgd if (!comp) {
524 1.1 cgd /* if (*den!=NULLUNIT) free(*den);
525 1.1 cgd if (*num!=NULLUNIT) free(*num);*/
526 1.1 cgd *den++ = NULLUNIT;
527 1.1 cgd *num++ = NULLUNIT;
528 1.1 cgd }
529 1.1 cgd else if (comp < 0)
530 1.1 cgd den++;
531 1.1 cgd else
532 1.1 cgd num++;
533 1.1 cgd }
534 1.1 cgd }
535 1.1 cgd
536 1.1 cgd
537 1.1 cgd
538 1.1 cgd
539 1.1 cgd /*
540 1.1 cgd Looks up the definition for the specified unit.
541 1.1 cgd Returns a pointer to the definition or a null pointer
542 1.1 cgd if the specified unit does not appear in the units table.
543 1.1 cgd */
544 1.1 cgd
545 1.1 cgd static char buffer[100]; /* buffer for lookupunit answers with
546 1.1 cgd prefixes */
547 1.1 cgd
548 1.17 joerg static const char *
549 1.16 lukem lookupunit(const char *unit)
550 1.1 cgd {
551 1.1 cgd int i;
552 1.1 cgd char *copy;
553 1.1 cgd
554 1.1 cgd for (i = 0; i < unitcount; i++) {
555 1.1 cgd if (!strcmp(unittable[i].uname, unit))
556 1.1 cgd return unittable[i].uval;
557 1.1 cgd }
558 1.1 cgd
559 1.1 cgd if (unit[strlen(unit) - 1] == '^') {
560 1.1 cgd copy = dupstr(unit);
561 1.1 cgd copy[strlen(copy) - 1] = 0;
562 1.1 cgd for (i = 0; i < unitcount; i++) {
563 1.1 cgd if (!strcmp(unittable[i].uname, copy)) {
564 1.12 itojun strlcpy(buffer, copy, sizeof(buffer));
565 1.1 cgd free(copy);
566 1.1 cgd return buffer;
567 1.1 cgd }
568 1.1 cgd }
569 1.1 cgd free(copy);
570 1.1 cgd }
571 1.1 cgd if (unit[strlen(unit) - 1] == 's') {
572 1.1 cgd copy = dupstr(unit);
573 1.1 cgd copy[strlen(copy) - 1] = 0;
574 1.1 cgd for (i = 0; i < unitcount; i++) {
575 1.1 cgd if (!strcmp(unittable[i].uname, copy)) {
576 1.12 itojun strlcpy(buffer, copy, sizeof(buffer));
577 1.1 cgd free(copy);
578 1.1 cgd return buffer;
579 1.1 cgd }
580 1.1 cgd }
581 1.1 cgd if (copy[strlen(copy) - 1] == 'e') {
582 1.1 cgd copy[strlen(copy) - 1] = 0;
583 1.1 cgd for (i = 0; i < unitcount; i++) {
584 1.1 cgd if (!strcmp(unittable[i].uname, copy)) {
585 1.12 itojun strlcpy(buffer, copy, sizeof(buffer));
586 1.1 cgd free(copy);
587 1.1 cgd return buffer;
588 1.1 cgd }
589 1.1 cgd }
590 1.1 cgd }
591 1.1 cgd free(copy);
592 1.1 cgd }
593 1.1 cgd for (i = 0; i < prefixcount; i++) {
594 1.1 cgd if (!strncmp(prefixtable[i].prefixname, unit,
595 1.1 cgd strlen(prefixtable[i].prefixname))) {
596 1.1 cgd unit += strlen(prefixtable[i].prefixname);
597 1.1 cgd if (!strlen(unit) || lookupunit(unit)) {
598 1.12 itojun strlcpy(buffer, prefixtable[i].prefixval,
599 1.12 itojun sizeof(buffer));
600 1.12 itojun strlcat(buffer, " ", sizeof(buffer));
601 1.12 itojun strlcat(buffer, unit, sizeof(buffer));
602 1.1 cgd return buffer;
603 1.1 cgd }
604 1.1 cgd }
605 1.1 cgd }
606 1.1 cgd return 0;
607 1.1 cgd }
608 1.1 cgd
609 1.1 cgd
610 1.1 cgd
611 1.1 cgd /*
612 1.1 cgd reduces a product of symbolic units to primitive units.
613 1.1 cgd The three low bits are used to return flags:
614 1.1 cgd
615 1.1 cgd bit 0 (1) set on if reductions were performed without error.
616 1.1 cgd bit 1 (2) set on if no reductions are performed.
617 1.1 cgd bit 2 (4) set on if an unknown unit is discovered.
618 1.1 cgd */
619 1.1 cgd
620 1.1 cgd
621 1.1 cgd #define ERROR 4
622 1.1 cgd
623 1.17 joerg static int
624 1.1 cgd reduceproduct(struct unittype * theunit, int flip)
625 1.1 cgd {
626 1.1 cgd
627 1.16 lukem const char *toadd;
628 1.16 lukem const char **product;
629 1.1 cgd int didsomething = 2;
630 1.1 cgd
631 1.1 cgd if (flip)
632 1.1 cgd product = theunit->denominator;
633 1.1 cgd else
634 1.1 cgd product = theunit->numerator;
635 1.1 cgd
636 1.1 cgd for (; *product; product++) {
637 1.1 cgd
638 1.1 cgd for (;;) {
639 1.1 cgd if (!strlen(*product))
640 1.1 cgd break;
641 1.1 cgd toadd = lookupunit(*product);
642 1.1 cgd if (!toadd) {
643 1.21 apb mywarnx("Unknown unit '%s'", *product);
644 1.1 cgd return ERROR;
645 1.1 cgd }
646 1.1 cgd if (strchr(toadd, PRIMITIVECHAR))
647 1.1 cgd break;
648 1.1 cgd didsomething = 1;
649 1.1 cgd if (*product != NULLUNIT) {
650 1.16 lukem free(__UNCONST(*product));
651 1.1 cgd *product = NULLUNIT;
652 1.1 cgd }
653 1.1 cgd if (addunit(theunit, toadd, flip))
654 1.1 cgd return ERROR;
655 1.1 cgd }
656 1.1 cgd }
657 1.1 cgd return didsomething;
658 1.1 cgd }
659 1.1 cgd
660 1.1 cgd
661 1.1 cgd /*
662 1.1 cgd Reduces numerator and denominator of the specified unit.
663 1.1 cgd Returns 0 on success, or 1 on unknown unit error.
664 1.1 cgd */
665 1.1 cgd
666 1.17 joerg static int
667 1.1 cgd reduceunit(struct unittype * theunit)
668 1.1 cgd {
669 1.1 cgd int ret;
670 1.1 cgd
671 1.1 cgd ret = 1;
672 1.1 cgd while (ret & 1) {
673 1.1 cgd ret = reduceproduct(theunit, 0) | reduceproduct(theunit, 1);
674 1.1 cgd if (ret & 4)
675 1.1 cgd return 1;
676 1.1 cgd }
677 1.1 cgd return 0;
678 1.1 cgd }
679 1.1 cgd
680 1.17 joerg static int
681 1.16 lukem compareproducts(const char **one, const char **two)
682 1.1 cgd {
683 1.1 cgd while (*one || *two) {
684 1.1 cgd if (!*one && *two != NULLUNIT)
685 1.1 cgd return 1;
686 1.1 cgd if (!*two && *one != NULLUNIT)
687 1.1 cgd return 1;
688 1.1 cgd if (*one == NULLUNIT)
689 1.1 cgd one++;
690 1.1 cgd else if (*two == NULLUNIT)
691 1.1 cgd two++;
692 1.15 christos else if (*one && *two && strcmp(*one, *two))
693 1.1 cgd return 1;
694 1.1 cgd else
695 1.1 cgd one++, two++;
696 1.1 cgd }
697 1.1 cgd return 0;
698 1.1 cgd }
699 1.1 cgd
700 1.1 cgd
701 1.1 cgd /* Return zero if units are compatible, nonzero otherwise */
702 1.1 cgd
703 1.17 joerg static int
704 1.1 cgd compareunits(struct unittype * first, struct unittype * second)
705 1.1 cgd {
706 1.1 cgd return
707 1.1 cgd compareproducts(first->numerator, second->numerator) ||
708 1.1 cgd compareproducts(first->denominator, second->denominator);
709 1.1 cgd }
710 1.1 cgd
711 1.17 joerg static int
712 1.13 mycroft compareunitsreciprocal(struct unittype * first, struct unittype * second)
713 1.13 mycroft {
714 1.13 mycroft return
715 1.13 mycroft compareproducts(first->numerator, second->denominator) ||
716 1.13 mycroft compareproducts(first->denominator, second->numerator);
717 1.13 mycroft }
718 1.13 mycroft
719 1.1 cgd
720 1.17 joerg static int
721 1.1 cgd completereduce(struct unittype * unit)
722 1.1 cgd {
723 1.1 cgd if (reduceunit(unit))
724 1.1 cgd return 1;
725 1.1 cgd sortunit(unit);
726 1.1 cgd cancelunit(unit);
727 1.1 cgd return 0;
728 1.1 cgd }
729 1.1 cgd
730 1.1 cgd
731 1.17 joerg static void
732 1.1 cgd showanswer(struct unittype * have, struct unittype * want)
733 1.1 cgd {
734 1.1 cgd if (compareunits(have, want)) {
735 1.13 mycroft if (compareunitsreciprocal(have, want)) {
736 1.13 mycroft printf("conformability error\n");
737 1.13 mycroft showunit(have);
738 1.13 mycroft showunit(want);
739 1.13 mycroft } else {
740 1.13 mycroft printf("\treciprocal conversion\n");
741 1.21 apb printf("\t* %.*g\n\t/ %.*g\n",
742 1.21 apb precision, 1 / (have->factor * want->factor),
743 1.21 apb precision, want->factor * have->factor);
744 1.13 mycroft }
745 1.1 cgd }
746 1.1 cgd else
747 1.21 apb printf("\t* %.*g\n\t/ %.*g\n",
748 1.21 apb precision, have->factor / want->factor,
749 1.21 apb precision, want->factor / have->factor);
750 1.1 cgd }
751 1.1 cgd
752 1.21 apb static int
753 1.21 apb listunits(int expand)
754 1.21 apb {
755 1.21 apb struct unittype theunit;
756 1.21 apb const char *thename;
757 1.21 apb const char *thedefn;
758 1.21 apb int errors = 0;
759 1.21 apb int i;
760 1.21 apb int printexpansion;
761 1.21 apb
762 1.21 apb /*
763 1.21 apb * send error and warning messages to stdout,
764 1.21 apb * and make them look like comments.
765 1.21 apb */
766 1.21 apb errprefix = "/ ";
767 1.21 apb
768 1.21 apb #if 0 /* debug */
769 1.21 apb printf("/ expand=%d precision=%d unitcount=%d prefixcount=%d\n",
770 1.21 apb expand, precision, unitcount, prefixcount);
771 1.21 apb #endif
772 1.21 apb
773 1.21 apb /* 1. Dump all primitive units, e.g. "m !a!", "kg !b!", ... */
774 1.21 apb printf("/ Primitive units\n");
775 1.21 apb for (i = 0; i < unitcount; i++) {
776 1.21 apb thename = unittable[i].uname;
777 1.21 apb thedefn = unittable[i].uval;
778 1.21 apb if (thedefn[0] == PRIMITIVECHAR) {
779 1.21 apb printf("%s\t%s\n", thename, thedefn);
780 1.21 apb }
781 1.21 apb }
782 1.21 apb
783 1.21 apb /* 2. Dump all prefixes, e.g. "yotta- 1e24", "zetta- 1e21", ... */
784 1.21 apb printf("/ Prefixes\n");
785 1.21 apb for (i = 0; i < prefixcount; i++) {
786 1.21 apb printexpansion = expand;
787 1.21 apb thename = prefixtable[i].prefixname;
788 1.21 apb thedefn = prefixtable[i].prefixval;
789 1.21 apb if (expand) {
790 1.21 apb /*
791 1.21 apb * prefix names are sometimes identical to unit
792 1.21 apb * names, so we have to expand thedefn instead of
793 1.21 apb * expanding thename.
794 1.21 apb */
795 1.21 apb initializeunit(&theunit);
796 1.21 apb if (addunit(&theunit, thedefn, 0) != 0
797 1.21 apb || completereduce(&theunit) != 0) {
798 1.21 apb errors++;
799 1.21 apb printexpansion = 0;
800 1.21 apb mywarnx("Error in prefix '%s-'", thename);
801 1.21 apb }
802 1.21 apb }
803 1.21 apb if (printexpansion) {
804 1.21 apb printf("%s-", thename);
805 1.21 apb showunit(&theunit);
806 1.21 apb } else
807 1.21 apb printf("%s-\t%s\n", thename, thedefn);
808 1.21 apb }
809 1.21 apb
810 1.21 apb /* 3. Dump all other units. */
811 1.21 apb printf("/ Other units\n");
812 1.21 apb for (i = 0; i < unitcount; i++) {
813 1.21 apb printexpansion = expand;
814 1.21 apb thename = unittable[i].uname;
815 1.21 apb thedefn = unittable[i].uval;
816 1.21 apb if (thedefn[0] == PRIMITIVECHAR)
817 1.21 apb continue;
818 1.21 apb if (expand) {
819 1.21 apb /*
820 1.21 apb * expand thename, not thedefn, so that
821 1.21 apb * we can catch errors in the name itself.
822 1.21 apb * e.g. a name that contains a hyphen
823 1.22 apb * will be interpreted as multiplication.
824 1.21 apb */
825 1.21 apb initializeunit(&theunit);
826 1.23 apb if (addunit(&theunit, thename, 0) != 0
827 1.21 apb || completereduce(&theunit) != 0) {
828 1.21 apb errors++;
829 1.21 apb printexpansion = 0;
830 1.21 apb mywarnx("Error in unit '%s'", thename);
831 1.21 apb }
832 1.21 apb }
833 1.21 apb if (printexpansion) {
834 1.21 apb printf("%s", thename);
835 1.21 apb showunit(&theunit);
836 1.21 apb } else
837 1.21 apb printf("%s\t%s\n", thename, thedefn);
838 1.21 apb }
839 1.21 apb
840 1.21 apb if (errors)
841 1.21 apb mywarnx("Definitions with errors: %d", errors);
842 1.21 apb return (errors ? 1 : 0);
843 1.21 apb }
844 1.1 cgd
845 1.17 joerg static void
846 1.17 joerg usage(void)
847 1.1 cgd {
848 1.7 lukem fprintf(stderr,
849 1.24 wiz "\nunits [-Llqv] [-f filename] [[count] from-unit to-unit]\n");
850 1.1 cgd fprintf(stderr, "\n -f specify units file\n");
851 1.24 wiz fprintf(stderr, " -L list units in standardized base units\n");
852 1.24 wiz fprintf(stderr, " -l list units\n");
853 1.10 atatat fprintf(stderr, " -q suppress prompting (quiet)\n");
854 1.1 cgd fprintf(stderr, " -v print version number\n");
855 1.1 cgd exit(3);
856 1.1 cgd }
857 1.1 cgd
858 1.4 jtc int
859 1.1 cgd main(int argc, char **argv)
860 1.1 cgd {
861 1.1 cgd
862 1.1 cgd struct unittype have, want;
863 1.1 cgd char havestr[81], wantstr[81];
864 1.5 mark int optchar;
865 1.16 lukem const char *userfile = 0;
866 1.21 apb int list = 0, listexpand = 0;
867 1.1 cgd int quiet = 0;
868 1.1 cgd
869 1.21 apb while ((optchar = getopt(argc, argv, "lLvqf:")) != -1) {
870 1.1 cgd switch (optchar) {
871 1.21 apb case 'l':
872 1.21 apb list = 1;
873 1.21 apb break;
874 1.21 apb case 'L':
875 1.21 apb list = 1;
876 1.21 apb listexpand = 1;
877 1.21 apb precision = DBL_DIG;
878 1.21 apb break;
879 1.1 cgd case 'f':
880 1.1 cgd userfile = optarg;
881 1.1 cgd break;
882 1.1 cgd case 'q':
883 1.1 cgd quiet = 1;
884 1.1 cgd break;
885 1.1 cgd case 'v':
886 1.1 cgd fprintf(stderr, "\n units version %s Copyright (c) 1993 by Adrian Mariano\n",
887 1.1 cgd VERSION);
888 1.1 cgd fprintf(stderr, " This program may be freely distributed\n");
889 1.1 cgd usage();
890 1.1 cgd default:
891 1.1 cgd usage();
892 1.1 cgd break;
893 1.1 cgd }
894 1.1 cgd }
895 1.1 cgd
896 1.10 atatat argc -= optind;
897 1.10 atatat argv += optind;
898 1.10 atatat
899 1.21 apb if ((argc != 3 && argc != 2 && argc != 0)
900 1.21 apb || (list && argc != 0))
901 1.1 cgd usage();
902 1.1 cgd
903 1.21 apb if (list)
904 1.21 apb errprefix = "/ "; /* set this before reading the file */
905 1.21 apb
906 1.1 cgd readunits(userfile);
907 1.1 cgd
908 1.21 apb if (list)
909 1.21 apb return listunits(listexpand);
910 1.21 apb
911 1.10 atatat if (argc == 3) {
912 1.12 itojun strlcpy(havestr, argv[0], sizeof(havestr));
913 1.12 itojun strlcat(havestr, " ", sizeof(havestr));
914 1.12 itojun strlcat(havestr, argv[1], sizeof(havestr));
915 1.10 atatat argc--;
916 1.10 atatat argv++;
917 1.10 atatat argv[0] = havestr;
918 1.10 atatat }
919 1.10 atatat
920 1.10 atatat if (argc == 2) {
921 1.12 itojun strlcpy(havestr, argv[0], sizeof(havestr));
922 1.12 itojun strlcpy(wantstr, argv[1], sizeof(wantstr));
923 1.1 cgd initializeunit(&have);
924 1.1 cgd addunit(&have, havestr, 0);
925 1.1 cgd completereduce(&have);
926 1.1 cgd initializeunit(&want);
927 1.1 cgd addunit(&want, wantstr, 0);
928 1.1 cgd completereduce(&want);
929 1.1 cgd showanswer(&have, &want);
930 1.1 cgd }
931 1.1 cgd else {
932 1.1 cgd if (!quiet)
933 1.1 cgd printf("%d units, %d prefixes\n\n", unitcount,
934 1.1 cgd prefixcount);
935 1.1 cgd for (;;) {
936 1.1 cgd do {
937 1.1 cgd initializeunit(&have);
938 1.1 cgd if (!quiet)
939 1.1 cgd printf("You have: ");
940 1.1 cgd if (!fgets(havestr, 80, stdin)) {
941 1.11 kristerw if (!quiet)
942 1.11 kristerw putchar('\n');
943 1.1 cgd exit(0);
944 1.1 cgd }
945 1.1 cgd } while (addunit(&have, havestr, 0) ||
946 1.1 cgd completereduce(&have));
947 1.1 cgd do {
948 1.1 cgd initializeunit(&want);
949 1.1 cgd if (!quiet)
950 1.1 cgd printf("You want: ");
951 1.1 cgd if (!fgets(wantstr, 80, stdin)) {
952 1.1 cgd if (!quiet)
953 1.1 cgd putchar('\n');
954 1.1 cgd exit(0);
955 1.1 cgd }
956 1.1 cgd } while (addunit(&want, wantstr, 0) ||
957 1.1 cgd completereduce(&want));
958 1.1 cgd showanswer(&have, &want);
959 1.1 cgd }
960 1.1 cgd }
961 1.7 lukem return (0);
962 1.1 cgd }
963