calc.c

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

C
812
字号
/* * calc - arbitrary precision calculator * * Copyright (C) 1999-2006  David I. Bell, Landon Curt Noll and Ernest Bowen * * Primary author:  David I. Bell * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL.  You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA. * * @(#) $Revision: 29.14 $ * @(#) $Id: calc.c,v 29.14 2007/02/07 00:37:25 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/RCS/calc.c,v $ * * Under source code control:	1990/02/15 01:48:11 * File existed as early as:	before 1990 * * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/ */#include <stdio.h>#include <signal.h>#if !defined (_WIN32)# include <pwd.h>#endif#include <sys/types.h>#include <ctype.h>#if defined(_WIN32)# include <io.h>/* * getopt.h file is from the Cygwin GNU library * * See: *	http://sources.redhat.com/cygwin/ */# include "../getopt/getopt.h"# define strdup _strdup# define isatty _isatty#endif /* Windoz */#define CALC_C#include "calc.h"#include "hist.h"#include "func.h"#include "opcodes.h"#include "conf.h"#include "token.h"#include "symbol.h"#include "have_uid_t.h"#include "have_const.h"#include "custom.h"#include "math_error.h"#include "args.h"#include "zmath.h"#include "have_unistd.h"#if defined(HAVE_UNISTD_H)#include <unistd.h>#endif#include "have_stdlib.h"#if defined(HAVE_STDLIB_H)#include <stdlib.h>#endif#include "have_strdup.h"#if !defined(HAVE_STRDUP)# define strdup(x) calc_strdup((CONST char *)(x))#endif /* HAVE_STRDUP */#include "have_unused.h"/* * static definitions and functions */static void intint(int arg);	/* interrupt routine */static int nextcp(char **cpp, int *ip, int argc, char **argv, BOOL haveendstr);static void set_run_state(run state);/* * Top level calculator routine. */intmain(int argc, char **argv){	int want_defhelp = 0;	/* 1=> we only want the default help */	int cmdlen;		/* length of the command string */	int newcmdlen;	int c;			/* option */	int index;	int maxindex;	char *cp;	char *endcp;	char *bp;	BOOL done = FALSE;	BOOL havearg;	BOOL haveendstr;	size_t len;	/*	 * parse args	 */	program = argv[0];	cmdbuf[0] = '\0';	cmdlen = 0;	/* process command line options */	index = 1;	cp = endcp = NULL;	maxindex = argc;	havecommands = FALSE;	while (index < maxindex && !done) {		cp = argv[index];		if (*cp == '\0') {			index++;			continue;		}		for (;;) {			havearg = FALSE;			if (*cp != '-') {				done = TRUE;				break;			}			++cp;			if (*cp == '-') {				cp++;				while (*cp == ' ')					++cp;				done = TRUE;				break;			}			for (;;) {				c = *cp;				if (c == '\0' || c == ' ')					break;				switch (c) {				case 'C':#if defined(CUSTOM)					allow_custom = TRUE;					break;#else /* CUSTOM */					/*					 * we are too early in processing to					 * call libcalc_call_me_last() -					 * nothing to cleanup					 */					fprintf(stderr,					    "%s: calc was built with custom"					    " functions disabled, -C usage is"					    " disallowed\n", program);					exit(1);#endif /* CUSTOM */				case 'e':					no_env = TRUE;					break;				case 'h':					want_defhelp = 1;					break;				case 'i':					i_flag = TRUE;					break;				case 'm':					cp++;					while (*cp == ' ' || *cp == '\t')						cp++;					if (*cp == '\0') {						index++;						if (index >= argc) {							fprintf(stderr,								"-m expects"								" argument");							exit (1);						}						cp = argv[index];					}					if (*cp < '0' || *cp > '7') {						/*						 * we are too early in						 * processing to call						 * libcalc_call_me_last()						 * nothing to cleanup						 */						fprintf(stderr,							"%s: unknown -m arg\n",							program);						exit(1);					}					allow_read = (((*cp-'0') & 04) > 0);					allow_write = (((*cp-'0') & 02) > 0);					allow_exec = (((*cp-'0') & 01) > 0);					cp++;					if (*cp != ' ' && *cp != '\0') {						fprintf(stderr, "??? m-arg");						exit(1);					}					havearg = TRUE;					break;				case 'n':					/*					 * -n is deprecated and may be reused					 * for another purpose in the future					 */					break;				case 'O':					use_old_std = TRUE;					break;				case 'p':					p_flag = TRUE;					break;				case 'q':					q_flag = TRUE;					break;				case 'u':					u_flag = TRUE;					break;				case 'c':					c_flag = TRUE;					break;				case 'd':					d_flag = TRUE;					break;				case 'v':					/*					 * we are too early in processing to					 * call libcalc_call_me_last() -					 * nothing to cleanup					 */					printf("%s (version %s)\n",					       CALC_TITLE, version());					exit(0);				case 'D':					/*					 * parse the -D arg					 *					 * Could be:					 *					 * calc_debug					 * calc_debug:resource_debug					 * calc_debug:resource_debug:user_debug					 */					if (nextcp(&cp, &index, argc, argv,						   FALSE)) {					    fprintf(stderr,					    	    "-D expects argument\n");					    exit (1);					}					havearg = TRUE;					if (*cp != ':') {						if (*cp < '0' || *cp > '9') {						    fprintf(stderr,						    	    "-D expects"							    " integer\n");						    exit (1);						}						calc_debug = cp;						(void) strtol(cp, &endcp, 10);						cp = endcp;						if (*cp != '\0' &&						    *cp != ' ' && *cp != ':') {						    fprintf(stderr,						    	    "Bad syntax im -D"							    " arg\n");						    exit (1);						}						if (*cp != ':') {						    if (nextcp(&cp, &index,						    	       argc, argv,							       FALSE)								|| *cp != ':')							break;						}					}					if (nextcp(&cp, &index, argc, argv,						   FALSE)) {						fprintf(stderr,							"-D : expects"							" argument\n");						exit (1);					}					if (*cp != ':') {						if (*cp < '0' || *cp > '9') {						    fprintf(stderr,						    	    "-D : expects"							    " integer\n");						    exit (1);						}						resource_debug = cp;						(void) strtol(cp, &endcp, 10);						cp = endcp;						if (*cp != '\0' &&						    *cp != ' ' && *cp != ':') {						    fprintf(stderr,						    	    "Bad syntax im -D"							    " : arg\n");						    exit (1);						}						if (*cp != ':') {							if (nextcp(&cp, &index,						    	           argc, argv,							           FALSE)							    || *cp != ':') {								break;							}						}					}					if (nextcp(&cp, &index, argc, argv,						   FALSE)) {					    fprintf(stderr, "-D : : expects"					    	    " argument\n");					    exit (1);					}					if (*cp < '0' || *cp > '9') {					    fprintf(stderr, "-D :: expects"					    	    " integer\n");					    exit (1);					}					user_debug = cp;					(void) strtol(cp, &endcp, 10);					cp = endcp;					if (*cp != '\0' && *cp != ' ') {						fprintf(stderr, "Bad syntax in"							" -D : : arg\n");						exit (1);					}					break;				case 'f':					haveendstr = (cp[1] == '\0');					if (nextcp(&cp, &index, argc, argv,						   haveendstr)) {					    fprintf(stderr, "-f expects"						    " filename\n");					    exit (1);					}					if (*cp == ';') {						fprintf(stderr,							"-f expects"							" filename\n");						exit (1);					}					havearg = TRUE;					if (cmdlen > 0)						cmdbuf[cmdlen++] = ' ';					strcpy(cmdbuf + cmdlen, "read ");					cmdlen += 5;					if (strncmp(cp, "-once", 5) == 0 &&					    (cp[5] == '\0' || cp[5] == ' ')) {						cp += 5;						haveendstr = (*cp == '\0');						strcpy(cmdbuf+cmdlen, "-once ");						cmdlen += 6;						if (nextcp(&cp, &index, argc,							   argv, haveendstr)) {						    fprintf(stderr, "-f -once"						    	    " expects"							    " filename\n");						    exit (1);						}					}					bp = cmdbuf + cmdlen;					if (haveendstr) {						len = strlen(cp);						if (len == 0) {							fprintf(stderr,								"Null"								" filename!");							exit (1);						}						if (cmdlen + len + 2 > MAXCMD) {							fprintf(stderr,								"Commands too"								" long");							exit (1);						}						/* XXX What if *cp = '\''? */						*bp++ = '\'';						strncpy(bp, cp, len+1);						bp += len;						*bp++ = '\'';						cp += len;						cmdlen += len + 2;					} else {						do {							if (cmdlen > MAXCMD) {								fprintf(stderr,								   "Commands"								   " too long");

⌨️ 快捷键说明

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