示例#1
0
static void
eval_string(char *p)
{
	int ch;

	if (bmachine.readsp > 0) {
		/* Check for tail call. Do not recurse in that case. */
		ch = readch();
		if (ch == EOF) {
			src_free();
			src_setstring(&bmachine.readstack[bmachine.readsp], p);
			return;
		} else
			unreadch();
	}
	if (bmachine.readsp == bmachine.readstack_sz - 1) {
		size_t newsz = bmachine.readstack_sz * 2;
		struct source *stack;
		stack = reallocarray(bmachine.readstack, newsz,
		    sizeof(struct source));
		if (stack == NULL)
			err(1, "recursion too deep");
		bmachine.readstack_sz = newsz;
		bmachine.readstack = stack;
	}
	src_setstring(&bmachine.readstack[++bmachine.readsp], p);
}
示例#2
0
文件: dc.c 项目: 2asoft/freebsd
int
main(int argc, char *argv[])
{
	int ch;
	bool extended_regs = false, preproc_done = false;

	/* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
	while ((ch = getopt_long(argc, argv, "e:f:hVx", long_options, NULL)) != -1) {
		switch (ch) {
		case 'e':
			if (!preproc_done)
				init_bmachine(extended_regs);
			src_setstring(&src, optarg);
			reset_bmachine(&src);
			eval();
			preproc_done = true;
			break;
		case 'f':
			if (!preproc_done)
				init_bmachine(extended_regs);
			procfile(optarg);
			preproc_done = true;
			break;
		case 'x':
			extended_regs = true;
			break;
		case 'V':
			fprintf(stderr, "%s (BSD bc) %s\n", __progname, DC_VER);
			exit(0);
			break;
		case '-':
			break;
		case 'h':
			/* FALLTHROUGH */
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (!preproc_done)
		init_bmachine(extended_regs);
	setlinebuf(stdout);
	setlinebuf(stderr);

	if (argc > 1)
		usage();
	if (argc == 1) {
		procfile(argv[0]);
		preproc_done = true;
	}
	if (preproc_done)
		return (0);

	src_setstream(&src, stdin);
	reset_bmachine(&src);
	eval();

	return (0);
}
示例#3
0
文件: dc.c 项目: appleorange1/bitrig
int
main(int argc, char *argv[])
{
	int		ch;
	bool		extended_regs = false;
	FILE		*file;
	struct source	src;
	char		*buf, *p;
	struct stat	st;


	if ((buf = strdup("")) == NULL)
		err(1, NULL);
	/* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
	while ((ch = getopt(argc, argv, "e:x-")) != -1) {
		switch (ch) {
		case 'e':
			p = buf;
			if (asprintf(&buf, "%s %s", buf, optarg) == -1)
				err(1, NULL);
			free(p);
			break;
		case 'x':
			extended_regs = true;
			break;
		case '-':
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	init_bmachine(extended_regs);
	(void)setlinebuf(stdout);
	(void)setlinebuf(stderr);

	if (argc > 1)
		usage();
	if (buf[0] != '\0') {
		src_setstring(&src, buf);
		reset_bmachine(&src);
		eval();
		free(buf);
		if (argc == 0)
			return (0);
	}
	if (argc == 1) {
		file = fopen(argv[0], "r");
		if (file == NULL)
			err(1, "cannot open file %s", argv[0]);
		if (fstat(fileno(file), &st) == -1)
			err(1, "%s", argv[0]);
		if (S_ISDIR(st.st_mode)) {
			errno = EISDIR;
			err(1, "%s", argv[0]);
		}
		src_setstream(&src, file);
		reset_bmachine(&src);
		eval();
		(void)fclose(file);
		/*
		 * BSD and Solaris dc(1) continue with stdin after processing
		 * the file given as the argument. We follow GNU dc(1).
		 */
		 return (0);
	}
	src_setstream(&src, stdin);
	reset_bmachine(&src);
	eval();

	return (0);
}