calc.c

来自「Calc Software Package for Number Calc」· C语言 代码 · 共 812 行 · 第 1/2 页

C
812
字号
								exit (1);							}							*bp++ =  *cp++;							cmdlen++;						} while (*cp != '\0' &&							 *cp != ';' &&							 *cp != ' ');					}					if (*cp == ';')						cp++;					*bp++ = ';';					cmdlen++;					s_flag = TRUE;	/* -f implies -s */					break;				case 's':					s_flag = TRUE;					maxindex = index + 1;					break;				default:					/*					 * we are too early in processing to					 * call libcalc_call_me_last() -					 * nothing to cleanup					 */					fprintf(stderr, "Illegal option -%c\n",							c);					fprintf(stderr,		"usage: %s [-c] [-C] [-d] [-e] [-h] [-i] [-m mode]\n"		"\t[-D calc_debug[:resource_debug[:user_debug]]]\n"		"\t[-O] [-p] [-q] [-s] [-u] [-v] "		"[--] [calc_cmd ...]\n"		"usage: %s ... -f filename\n"		"1st cscript line: #/path/to/calc ... -f\n",		program, program);					exit(1);			}			if (havearg)				break;			cp++;			}			while (*cp == ' ')				cp++;			if (*cp == '\0') {				index++;				break;			}		}	}	while (index < maxindex) {		size_t cplen;		if (cmdlen > 0)			cmdbuf[cmdlen++] = ' ';		cplen = strlen(cp);		newcmdlen = cmdlen + cplen;		if (newcmdlen > MAXCMD) {			fprintf(stderr,				"%s: commands too long\n",				program);			exit(1);		}		strncpy(cmdbuf + cmdlen, cp, cplen+1);		cmdlen = newcmdlen;		index++;		if (index < maxindex)			cp = argv[index];	}	havecommands = (cmdlen > 0);	if (havecommands) {		cmdbuf[cmdlen++] = '\n';		cmdbuf[cmdlen] = '\0';	}	argc_value = argc - maxindex;	argv_value = argv + maxindex;	/*	 * unbuffered mode	 */	if (u_flag) {		setbuf(stdin, NULL);		setbuf(stdout, NULL);	}	/*	 * initialize	 */	libcalc_call_me_first();	stdin_tty = isatty(0);		/* assume stdin is on fd 0 */	if (conf->calc_debug & CALCDBG_TTY)		printf("main: stdin_tty is %d\n", stdin_tty);	if (want_defhelp) {		givehelp(DEFAULTCALCHELP);		libcalc_call_me_last();		exit(0);	}	/*	 * if allowed or needed, print version and setup bindings	 */	if (!havecommands && stdin_tty) {		if (!d_flag) {			printf("%s (version %s)\n", CALC_TITLE, version());			printf("Calc is open software. For license details "			       "type:  help copyright\n");			printf("[%s]\n\n",			    "Type \"exit\" to exit, or \"help\" for help.");		}		switch (hist_init(calcbindings)) {		case HIST_NOFILE:			fprintf(stderr,			    "%s: Cannot open bindings file \"%s\", "			    "fancy editing disabled.\n",			     program, calcbindings);			break;		case HIST_NOTTY:			fprintf(stderr,				"%s: Cannot set terminal modes, "				"fancy editing disabled\n", program);			break;		}	}	/*	 * establish error longjump point with initial conditions	 */	if (setjmp(jmpbuf) == 0) {		/*		 * reset/initialize the computing environment		 */		if (post_init)			initialize();		post_init = TRUE;	}	/*	 * (re)establish the interrupt handler	 */	(void) signal(SIGINT, intint);	/*	 * execute calc code based on the run state	 */	if (run_state == RUN_BEGIN) {		if (!q_flag && allow_read) {			set_run_state(RUN_RCFILES);			runrcfiles();		}		set_run_state(RUN_PRE_CMD_ARGS);	}	while (run_state == RUN_RCFILES) {		fprintf(stderr, "Error in rcfiles\n");		if ((c_flag && !stoponerror) || stoponerror < 0) {			getcommands(FALSE);			if (inputlevel() == 0) {				closeinput();				runrcfiles();				set_run_state(RUN_PRE_CMD_ARGS);			} else {				closeinput();			}		} else {			if ((havecommands && !i_flag) || !stdin_tty) {				set_run_state(RUN_EXIT_WITH_ERROR);			} else {				set_run_state(RUN_PRE_CMD_ARGS);			}		}	}	if (run_state == RUN_PRE_CMD_ARGS) {		if (havecommands) {			set_run_state(RUN_CMD_ARGS);			(void) openstring(cmdbuf, strlen(cmdbuf));			getcommands(FALSE);			closeinput();		}		set_run_state(RUN_PRE_TOP_LEVEL);	}	while (run_state == RUN_CMD_ARGS) {		fprintf(stderr, "Error in commands\n");		if ((c_flag && !stoponerror) || stoponerror < 0) {			getcommands(FALSE);			if (inputlevel() == 0)				set_run_state(RUN_PRE_TOP_LEVEL);			closeinput();		} else {			closeinput();			if (!stdin_tty || !i_flag || p_flag) {				set_run_state(RUN_EXIT_WITH_ERROR);			} else {				set_run_state(RUN_PRE_TOP_LEVEL);			}		}	}	if (run_state == RUN_PRE_TOP_LEVEL) {		if (stdin_tty &&		    (((havecommands) && !i_flag) || p_flag)) {			set_run_state(RUN_EXIT);		} else {			if (stdin_tty) {				reinitialize();			} else {				resetinput();				openterminal();			}			set_run_state(RUN_TOP_LEVEL);			getcommands(TRUE);		}		if (p_flag || (!i_flag && havecommands))			set_run_state(RUN_EXIT);	}	while (run_state == RUN_TOP_LEVEL) {		if (conf->calc_debug & CALCDBG_RUNSTATE)			printf("main: run_state = TOP_LEVEL\n");		if ((c_flag && !stoponerror) || stoponerror < 0) {			getcommands(TRUE);			if (!inputisterminal()) {				closeinput();				continue;			}			if (!p_flag && i_flag && !stdin_tty) {				closeinput();				if(!freopen("/dev/tty", "r", stdin)) {#if defined (_WIN32)					fprintf(stderr,						"/dev/tty does not exist on "						"this operating system.  "						"Change operating systems\n"						"or don't use this calc mode "						"in the future, sorry!\n");#else /* Windoz free systems */					fprintf(stderr,						"Unable to associate stdin"						" with /dev/tty");#endif /* Windoz free systems */					set_run_state(RUN_EXIT_WITH_ERROR);					break;				}				stdin_tty = TRUE;				if (conf->calc_debug & CALCDBG_TTY)					printf("main: stdin_tty is %d\n",					       stdin_tty);				reinitialize();			}		} else {			if (stdin_tty) {				reinitialize();				getcommands(TRUE);			} else if (inputisterminal() &&					!p_flag && (!havecommands||i_flag)) {				closeinput();				if(!freopen("/dev/tty", "r", stdin)) {#if defined (_WIN32)					fprintf(stderr,						"/dev/tty does not exist on "						"this operating system.  "						"Change operating systems\n"						"or don't use this calc mode "						"in the future, sorry!\n");#else /* Windoz free systems */					fprintf(stderr,						"Unable to associate stdin"						" with /dev/tty");#endif /* Windoz free systems */					set_run_state(RUN_EXIT_WITH_ERROR);					break;				}				stdin_tty = TRUE;				if (conf->calc_debug & CALCDBG_TTY)					printf("main: stdin_tty is %d\n",					       stdin_tty);				reinitialize();			} else {				set_run_state(RUN_EXIT_WITH_ERROR);			}		}	}	if (conf->calc_debug & CALCDBG_RUNSTATE)		printf("main: run_state = %s\n", run_state_name(run_state));	/*	 * all done	 */	libcalc_call_me_last();	return (run_state == RUN_EXIT_WITH_ERROR ||		run_state == RUN_ZERO) ? 1 : 0;}/* * Interrupt routine. * * given: *	arg		to keep ANSI C happy *//*ARGSUSED*/static voidintint(int UNUSED arg){	(void) signal(SIGINT, intint);	if (inputwait || (++abortlevel >= ABORT_NOW)) {		math_error("\nABORT");		/*NOTREACHED*/	}	if (abortlevel >= ABORT_MATH)		_math_abort_ = TRUE;	printf("\n[Abort level %d]\n", abortlevel);}/* * Routine called on any runtime error, to complain about it (with possible * arguments), and then longjump back to the top level command scanner. */voidmath_error(char *fmt, ...){	va_list ap;	char buf[MAXERROR+1];	if (funcname && (*funcname != '*'))		fprintf(stderr, "\"%s\": ", funcname);	if (funcline && ((funcname && (*funcname != '*')) ||	    !inputisterminal()))		fprintf(stderr, "line %ld: ", funcline);	va_start(ap, fmt);	vsprintf(buf, fmt, ap);	va_end(ap);	fprintf(stderr, "%s\n", buf);	funcname = NULL;	if (post_init) {		longjmp(jmpbuf, 1);	} else {		fprintf(stderr, "It is too early provide a command line prompt "				"so we must simply exit.  Sorry!\n");		/*		 * don't call libcalc_call_me_last() -- we might loop		 * and besides ... this is an unusual internal error case		 */		exit(3);	}}static intnextcp(char **cpp, int *ip, int argc, char **argv, BOOL haveendstr){	char *cp;	int index;	cp = *cpp;	index = *ip;	if (haveendstr) {		index++;		*ip = index;		if (index >= argc)			return 1;		*cpp = argv[index];		return 0;	}	if (*cp != '\0')		cp++;	for (;;) {		if (*cp == '\0') {			index++;			*ip = index;			if (index >= argc)				return 1;			cp = argv[index];		}		while (*cp == ' ')			cp++;		if (*cp != '\0')			break;	}	*cpp = cp;	return 0;}static voidset_run_state(run state){	if (conf->calc_debug & CALCDBG_RUNSTATE)		printf("main: run_state from %s to %s\n",			    run_state_name(run_state),			    run_state_name(state));	run_state = state;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?