int funcunit(struct unittype *theunit, struct function *fun) { struct unittype angleunit; if (fun->type==ANGLEIN){ err=unit2num(theunit); if (err==E_NOTANUMBER){ initializeunit(&angleunit); angleunit.denominator[0] = dupstr("radian"); angleunit.denominator[1] = 0; err = multunit(theunit, &angleunit); if (!err) err = unit2num(theunit); } if (err) return err; } else if (fun->type==ANGLEOUT || fun->type == DIMENSIONLESS) { if (err=unit2num(theunit)) return err; } else return E_BADFUNCTYPE; errno = 0; theunit->factor = (*(fun->func))(theunit->factor); if (errno) return E_FUNC; if (fun->type==ANGLEOUT) { theunit->numerator[0] = dupstr("radian"); theunit->numerator[1] = 0; } return 0; }
struct unittype * getnewunit() { if (nextunit>=MEMSIZE) return 0; memtable[nextunit] = (struct unittype *) mymalloc(sizeof(struct unittype),"(getnewunit)"); if (!memtable[nextunit]) return 0; initializeunit(memtable[nextunit]); return memtable[nextunit++]; }
int main(int argc, char **argv) { struct unittype have, want; char havestr[81], wantstr[81]; int optchar; const char *userfile = 0; int list = 0, listexpand = 0; int quiet = 0; while ((optchar = getopt(argc, argv, "lLvqf:")) != -1) { switch (optchar) { case 'l': list = 1; break; case 'L': list = 1; listexpand = 1; precision = DBL_DIG; break; case 'f': userfile = optarg; break; case 'q': quiet = 1; break; case 'v': fprintf(stderr, "\n units version %s Copyright (c) 1993 by Adrian Mariano\n", VERSION); fprintf(stderr, " This program may be freely distributed\n"); usage(); default: usage(); break; } } argc -= optind; argv += optind; if ((argc != 3 && argc != 2 && argc != 0) || (list && argc != 0)) usage(); if (list) errprefix = "/ "; /* set this before reading the file */ readunits(userfile); if (list) return listunits(listexpand); if (argc == 3) { strlcpy(havestr, argv[0], sizeof(havestr)); strlcat(havestr, " ", sizeof(havestr)); strlcat(havestr, argv[1], sizeof(havestr)); argc--; argv++; argv[0] = havestr; } if (argc == 2) { strlcpy(havestr, argv[0], sizeof(havestr)); strlcpy(wantstr, argv[1], sizeof(wantstr)); initializeunit(&have); addunit(&have, havestr, 0); completereduce(&have); initializeunit(&want); addunit(&want, wantstr, 0); completereduce(&want); showanswer(&have, &want); } else { if (!quiet) printf("%d units, %d prefixes\n\n", unitcount, prefixcount); for (;;) { do { initializeunit(&have); if (!quiet) printf("You have: "); if (!fgets(havestr, 80, stdin)) { if (!quiet) putchar('\n'); exit(0); } } while (addunit(&have, havestr, 0) || completereduce(&have)); do { initializeunit(&want); if (!quiet) printf("You want: "); if (!fgets(wantstr, 80, stdin)) { if (!quiet) putchar('\n'); exit(0); } } while (addunit(&want, wantstr, 0) || completereduce(&want)); showanswer(&have, &want); } } return (0); }
static int listunits(int expand) { struct unittype theunit; const char *thename; const char *thedefn; int errors = 0; int i; int printexpansion; /* * send error and warning messages to stdout, * and make them look like comments. */ errprefix = "/ "; #if 0 /* debug */ printf("/ expand=%d precision=%d unitcount=%d prefixcount=%d\n", expand, precision, unitcount, prefixcount); #endif /* 1. Dump all primitive units, e.g. "m !a!", "kg !b!", ... */ printf("/ Primitive units\n"); for (i = 0; i < unitcount; i++) { thename = unittable[i].uname; thedefn = unittable[i].uval; if (thedefn[0] == PRIMITIVECHAR) { printf("%s\t%s\n", thename, thedefn); } } /* 2. Dump all prefixes, e.g. "yotta- 1e24", "zetta- 1e21", ... */ printf("/ Prefixes\n"); for (i = 0; i < prefixcount; i++) { printexpansion = expand; thename = prefixtable[i].prefixname; thedefn = prefixtable[i].prefixval; if (expand) { /* * prefix names are sometimes identical to unit * names, so we have to expand thedefn instead of * expanding thename. */ initializeunit(&theunit); if (addunit(&theunit, thedefn, 0) != 0 || completereduce(&theunit) != 0) { errors++; printexpansion = 0; mywarnx("Error in prefix '%s-'", thename); } } if (printexpansion) { printf("%s-", thename); showunit(&theunit); } else printf("%s-\t%s\n", thename, thedefn); } /* 3. Dump all other units. */ printf("/ Other units\n"); for (i = 0; i < unitcount; i++) { printexpansion = expand; thename = unittable[i].uname; thedefn = unittable[i].uval; if (thedefn[0] == PRIMITIVECHAR) continue; if (expand) { /* * expand thename, not thedefn, so that * we can catch errors in the name itself. * e.g. a name that contains a hyphen * will be interpreted as multiplication. */ initializeunit(&theunit); if (addunit(&theunit, thename, 0) != 0 || completereduce(&theunit) != 0) { errors++; printexpansion = 0; mywarnx("Error in unit '%s'", thename); } } if (printexpansion) { printf("%s", thename); showunit(&theunit); } else printf("%s\t%s\n", thename, thedefn); } if (errors) mywarnx("Definitions with errors: %d", errors); return (errors ? 1 : 0); }
int main(int argc, char **argv) { struct unittype have, want; char havestr[81], wantstr[81]; int optchar; char *userfile = 0; int quiet = 0; while ((optchar = getopt(argc, argv, "vqf:")) != -1) { switch (optchar) { case 'f': userfile = optarg; break; case 'q': quiet = 1; break; case 'v': fprintf(stderr, "\n units version %s Copyright (c) 1993 by Adrian Mariano\n", VERSION); fprintf(stderr, " This program may be freely distributed\n"); usage(); default: usage(); break; } } if (optind != argc - 2 && optind != argc) usage(); readunits(userfile); if (optind == argc - 2) { strlcpy(havestr, argv[optind], sizeof(havestr)); strlcpy(wantstr, argv[optind + 1], sizeof(wantstr)); initializeunit(&have); addunit(&have, havestr, 0, 1); completereduce(&have); initializeunit(&want); addunit(&want, wantstr, 0, 1); completereduce(&want); showanswer(&have, &want); } else { if (!quiet) printf("%d units, %d prefixes\n", unitcount, prefixcount); for (;;) { do { initializeunit(&have); if (!quiet) printf("You have: "); if (!fgets(havestr, sizeof(havestr), stdin)) { if (!quiet) putchar('\n'); exit(0); } } while (addunit(&have, havestr, 0, 1) || completereduce(&have)); do { initializeunit(&want); if (!quiet) printf("You want: "); if (!fgets(wantstr, sizeof(wantstr), stdin)) { if (!quiet) putchar('\n'); exit(0); } } while (addunit(&want, wantstr, 0, 1) || completereduce(&want)); showanswer(&have, &want); } } return(0); }
int main(int argc, char **argv) { struct unittype have, want; char havestr[81], wantstr[81]; int optchar; char *userfile = 0; int quiet = 0; extern char *optarg; extern int optind; if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); while ((optchar = getopt(argc, argv, "vqf:")) != -1) { switch (optchar) { case 'f': userfile = optarg; break; case 'q': quiet = 1; break; case 'v': fprintf(stderr, "units version %s Copyright (c) 1993 by Adrian Mariano\n", VERSION); fprintf(stderr, "This program may be freely distributed\n"); usage(); default: usage(); break; } } argc -= optind; argv += optind; if (argc != 3 && argc != 2 && argc != 0) usage(); readunits(userfile); if (pledge("stdio", NULL) == -1) err(1, "pledge"); if (argc == 3) { strlcpy(havestr, argv[0], sizeof(havestr)); strlcat(havestr, " ", sizeof(havestr)); strlcat(havestr, argv[1], sizeof(havestr)); argc--; argv++; argv[0] = havestr; } if (argc == 2) { strlcpy(havestr, argv[0], sizeof(havestr)); strlcpy(wantstr, argv[1], sizeof(wantstr)); initializeunit(&have); addunit(&have, havestr, 0); completereduce(&have); initializeunit(&want); addunit(&want, wantstr, 0); completereduce(&want); showanswer(&have, &want); } else { if (!quiet) printf("%d units, %d prefixes\n", unitcount, prefixcount); for (;;) { do { initializeunit(&have); if (!quiet) printf("You have: "); if (!fgets(havestr, sizeof(havestr), stdin)) { if (!quiet) putchar('\n'); exit(0); } } while (addunit(&have, havestr, 0) || completereduce(&have)); do { initializeunit(&want); if (!quiet) printf("You want: "); if (!fgets(wantstr, sizeof(wantstr), stdin)) { if (!quiet) putchar('\n'); exit(0); } } while (addunit(&want, wantstr, 0) || completereduce(&want)); showanswer(&have, &want); } } return (0); }