コード例 #1
0
ファイル: units.c プロジェクト: ajinkya93/OpenBSD
int
reduceproduct(struct unittype *theunit, int flip)
{
	char *toadd, **product;
	int didsomething = 2;

	if (flip)
		product = theunit->denominator;
	else
		product = theunit->numerator;

	for (; *product; product++) {

		for (;;) {
			if (!strlen(*product))
				break;
			toadd = lookupunit(*product);
			if (!toadd) {
				printf("unknown unit '%s'\n", *product);
				return ERROR;
			}
			if (strchr(toadd, PRIMITIVECHAR))
				break;
			didsomething = 1;
			if (*product != NULLUNIT) {
				free(*product);
				*product = NULLUNIT;
			}
			if (addunit(theunit, toadd, flip))
				return ERROR;
		}
	}
	return didsomething;
}
コード例 #2
0
ファイル: preenlib.c プロジェクト: andreiw/polaris
/*
 * add the given driver/unit reference to the `rawdev' structure identified
 * by `cookie'
 * If a new `driver' structure needs to be created, associate the given
 * choosing function and driver private data with it.
 */
void
preen_addunit(
	void    *cookie,
	char	*dname,		/* driver name */
	int	(*cf)(),	/* candidate choosing function */
	void	*datap,		/* driver private data */
	uint_t	unit)		/* unit number */
{
	int drvid;
	struct driver *dp;
	struct onedev *devp;
	struct rawdev *rdp = (struct rawdev *)cookie;

	/*
	 * locate the driver struct
	 */
	dp = NULL;
	for (drvid = 0; drvid < ndrivers; drvid++) {
		if (strcmp(dlist[drvid].name, dname) == 0) {
			dp = &dlist[drvid];
			break;
		}
	}

	if (dp == NULL) {
		/*
		 * driver struct doesn't exist yet -- create one
		 */
		if (cf == NULL)
			cf = chooseone;
		drvid = alloc_driver(dname, cf, datap);
		dp = &dlist[drvid];
	}

	for (devp = rdp->alldevs; devp != NULL; devp = devp->nxtdev) {
		/*
		 * see if this device already references the given driver
		 */
		if (devp->drvid == drvid)
			break;
	}

	if (devp == NULL) {
		/*
		 * allocate a new `struct onedev' and chain it in
		 * rdp->alldevs...
		 */
		devp = alloc_dev(drvid);
		devp->nxtdev = rdp->alldevs;
		rdp->alldevs = devp;
	}

	/*
	 * add `unit' to the unitmap in devp
	 */
	addunit(devp, unit);
}
コード例 #3
0
ファイル: units.c プロジェクト: Hooman3/minix
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);
}
コード例 #4
0
ファイル: units.c プロジェクト: Hooman3/minix
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);
}
コード例 #5
0
ファイル: units.c プロジェクト: edgar-pek/PerspicuOS
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);
}
コード例 #6
0
ファイル: units.c プロジェクト: ajinkya93/OpenBSD
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);
}