⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eval.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
		if (lp->next) {			if (pipe(pip) < 0) {				close(prevfd);				error("Pipe call failed");			}		}		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {			INTON;			if (prevfd > 0) {				close(0);				copyfd(prevfd, 0);				close(prevfd);			}			if (pip[1] >= 0) {				close(pip[0]);				if (pip[1] != 1) {					close(1);					copyfd(pip[1], 1);					close(pip[1]);				}			}			evaltree(lp->n, EV_EXIT);		}		if (prevfd >= 0)			close(prevfd);		prevfd = pip[0];		close(pip[1]);	}	INTON;	if (n->npipe.backgnd == 0) {		INTOFF;		exitstatus = waitforjob(jp);		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));		INTON;	}}/* * Execute a command inside back quotes.  If it's a builtin command, we * want to save its output in a block obtained from malloc.  Otherwise * we fork off a subprocess and get the output of the command via a pipe. * Should be called with interrupts off. */voidevalbackcmd(n, result)	union node *n;	struct backcmd *result;	{	int pip[2];	struct job *jp;	struct stackmark smark;		/* unnecessary */	setstackmark(&smark);	result->fd = -1;	result->buf = NULL;	result->nleft = 0;	result->jp = NULL;	if (n == NULL) {		/* `` */	} else	if (n->type == NCMD) {		evalcommand(n, EV_BACKCMD, result);	} else {		if (pipe(pip) < 0)			error("Pipe call failed");		jp = makejob(n, 1);		if (forkshell(jp, n, FORK_NOJOB) == 0) {			FORCEINTON;			close(pip[0]);			if (pip[1] != 1) {				close(1);				copyfd(pip[1], 1);				close(pip[1]);			}			evaltree(n, EV_EXIT);		}		close(pip[1]);		result->fd = pip[0];		result->jp = jp;	}	popstackmark(&smark);	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",		result->fd, result->buf, result->nleft, result->jp));}/* * Execute a simple command. */STATIC voidevalcommand(cmd, flags, backcmd)	union node *cmd;	struct backcmd *backcmd;	{	struct stackmark smark;	union node *argp;	struct arglist arglist;	struct arglist varlist;	char **argv;	int argc;	char **envp;	int varflag;	struct strlist *sp;	register char *p;	int mode;	int pip[2];	struct cmdentry cmdentry;	struct job *jp;	struct jmploc jmploc;	struct jmploc *volatile savehandler;	char *volatile savecmdname;	volatile struct shparam saveparam;	struct localvar *volatile savelocalvars;	volatile int e;	char *lastarg;	/* First expand the arguments. */	TRACE(("evalcommand(0x%x, %d) called\n", (int)cmd, flags));	setstackmark(&smark);	arglist.lastp = &arglist.list;	varlist.lastp = &varlist.list;	varflag = 1;	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {		p = argp->narg.text;		if (varflag && is_name(*p)) {			do {				p++;			} while (is_in_name(*p));			if (*p == '=') {				expandarg(argp, &varlist, 0);				continue;			}		}		expandarg(argp, &arglist, 1);		varflag = 0;	}	*arglist.lastp = NULL;	*varlist.lastp = NULL;	expredir(cmd->ncmd.redirect);	argc = 0;	for (sp = arglist.list ; sp ; sp = sp->next)		argc++;	argv = stalloc(sizeof (char *) * (argc + 1));	for (sp = arglist.list ; sp ; sp = sp->next)		*argv++ = sp->text;	*argv = NULL;	lastarg = NULL;	if (iflag && funcnest == 0 && argc > 0)		lastarg = argv[-1];	argv -= argc;	/* Print the command if xflag is set. */	if (xflag) {		outc('+', &errout);		for (sp = varlist.list ; sp ; sp = sp->next) {			outc(' ', &errout);			out2str(sp->text);		}		for (sp = arglist.list ; sp ; sp = sp->next) {			outc(' ', &errout);			out2str(sp->text);		}		outc('\n', &errout);		flushout(&errout);	}	/* Now locate the command. */	if (argc == 0) {		cmdentry.cmdtype = CMDBUILTIN;		cmdentry.u.index = BLTINCMD;	} else {		find_command(argv[0], &cmdentry, 1);		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */			exitstatus = 2;			flushout(&errout);			return;		}		/* implement the bltin builtin here */		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {			for (;;) {				argv++;				if (--argc == 0)					break;				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {					outfmt(&errout, "%s: not found\n", *argv);					exitstatus = 2;					flushout(&errout);					return;				}				if (cmdentry.u.index != BLTINCMD)					break;			}		}	}	/* Fork off a child process if necessary. */	if (cmd->ncmd.backgnd	 || cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0	 || (flags & EV_BACKCMD) != 0	    && (cmdentry.cmdtype != CMDBUILTIN		 || cmdentry.u.index == DOTCMD		 || cmdentry.u.index == EVALCMD)) {		jp = makejob(cmd, 1);		mode = cmd->ncmd.backgnd;		if (flags & EV_BACKCMD) {			mode = FORK_NOJOB;			if (pipe(pip) < 0)				error("Pipe call failed");		}		if (forkshell(jp, cmd, mode) != 0)			goto parent;	/* at end of routine */		if (flags & EV_BACKCMD) {			FORCEINTON;			close(pip[0]);			if (pip[1] != 1) {				close(1);				copyfd(pip[1], 1);				close(pip[1]);			}		}		flags |= EV_EXIT;	}	/* This is the child process if a fork occurred. */	/* Execute the command. */	if (cmdentry.cmdtype == CMDFUNCTION) {		trputs("Shell function:  ");  trargs(argv);		redirect(cmd->ncmd.redirect, REDIR_PUSH);		saveparam = shellparam;		shellparam.malloc = 0;		shellparam.nparam = argc - 1;		shellparam.p = argv + 1;		shellparam.optnext = NULL;		INTOFF;		savelocalvars = localvars;		localvars = NULL;		INTON;		if (setjmp(jmploc.loc)) {			if (exception == EXSHELLPROC)				freeparam((struct shparam *)&saveparam);			else {				freeparam(&shellparam);				shellparam = saveparam;			}			poplocalvars();			localvars = savelocalvars;			handler = savehandler;			longjmp(handler->loc, 1);		}		savehandler = handler;		handler = &jmploc;		for (sp = varlist.list ; sp ; sp = sp->next)			mklocal(sp->text);		funcnest++;		evaltree(cmdentry.u.func, 0);		funcnest--;		INTOFF;		poplocalvars();		localvars = savelocalvars;		freeparam(&shellparam);		shellparam = saveparam;		handler = savehandler;		popredir();		INTON;		if (evalskip == SKIPFUNC) {			evalskip = 0;			skipcount = 0;		}		if (flags & EV_EXIT)			exitshell(exitstatus);	} else if (cmdentry.cmdtype == CMDBUILTIN) {		trputs("builtin command:  ");  trargs(argv);		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;		if (flags == EV_BACKCMD) {			memout.nleft = 0;			memout.nextc = memout.buf;			memout.bufsize = 64;			mode |= REDIR_BACKQ;		}		redirect(cmd->ncmd.redirect, mode);		savecmdname = commandname;		cmdenviron = varlist.list;		e = -1;		if (setjmp(jmploc.loc)) {			e = exception;			exitstatus = (e == EXINT)? SIGINT+128 : 2;			goto cmddone;		}		savehandler = handler;		handler = &jmploc;		commandname = argv[0];		argptr = argv + 1;		optptr = NULL;			/* initialize nextopt */		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);		flushall();cmddone:		out1 = &output;		out2 = &errout;		freestdout();		if (e != EXSHELLPROC) {			commandname = savecmdname;			if (flags & EV_EXIT) {				exitshell(exitstatus);			}		}		handler = savehandler;		if (e != -1) {			if (e != EXERROR || cmdentry.u.index == BLTINCMD					       || cmdentry.u.index == DOTCMD					       || cmdentry.u.index == EVALCMD					       || cmdentry.u.index == EXECCMD)				exraise(e);			FORCEINTON;		}		if (cmdentry.u.index != EXECCMD)			popredir();		if (flags == EV_BACKCMD) {			backcmd->buf = memout.buf;			backcmd->nleft = memout.nextc - memout.buf;			memout.buf = NULL;		}	} else {		trputs("normal command:  ");  trargs(argv);		clearredir();		redirect(cmd->ncmd.redirect, 0);		if (varlist.list) {			p = stalloc(strlen(pathval()) + 1);			scopy(pathval(), p);		} else {			p = pathval();		}		for (sp = varlist.list ; sp ; sp = sp->next)			setvareq(sp->text, VEXPORT|VSTACK);		envp = environment();		shellexec(argv, envp, p, cmdentry.u.index);		/*NOTREACHED*/	}	goto out;parent:	/* parent process gets here (if we forked) */	if (mode == 0) {	/* argument to fork */		INTOFF;		exitstatus = waitforjob(jp);		INTON;	} else if (mode == 2) {		backcmd->fd = pip[0];		close(pip[1]);		backcmd->jp = jp;	}out:	if (lastarg)		setvar("_", lastarg, 0);	popstackmark(&smark);}/* * Search for a command.  This is called before we fork so that the * location of the command will be available in the parent as well as * the child.  The check for "goodname" is an overly conservative * check that the name will not be subject to expansion. */STATIC voidprehash(n)	union node *n;	{	struct cmdentry entry;	if (n->type == NCMD && goodname(n->ncmd.args->narg.text))		find_command(n->ncmd.args->narg.text, &entry, 0);}/* * Builtin commands.  Builtin commands whose functions are closely * tied to evaluation are implemented here. *//* * No command given, or a bltin command with no arguments.  Set the * specified variables. */bltincmd(argc, argv)  char **argv; {	listsetvar(cmdenviron);	return exitstatus;}/* * Handle break and continue commands.  Break, continue, and return are * all handled by setting the evalskip flag.  The evaluation routines * above all check this flag, and if it is set they start skipping * commands rather than executing them.  The variable skipcount is * the number of loops to break/continue, or the number of function * levels to return.  (The latter is always 1.)  It should probably * be an error to break out of more loops than exist, but it isn't * in the standard shell so we don't make it one here. */breakcmd(argc, argv)  char **argv; {	int n;	n = 1;	if (argc > 1)		n = number(argv[1]);	if (n > loopnest)		n = loopnest;	if (n > 0) {		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;		skipcount = n;	}	return 0;}/* * The return command. */returncmd(argc, argv)  char **argv; {	int ret;	ret = exitstatus;	if (argc > 1)		ret = number(argv[1]);	if (funcnest) {		evalskip = SKIPFUNC;		skipcount = 1;	}	return ret;}truecmd(argc, argv)  char **argv; {	return strcmp(argv[0], "false") == 0 ? 1 : 0;}execcmd(argc, argv)  char **argv; {	if (argc > 1) {		iflag = 0;		/* exit on error */		setinteractive(0);#if JOBS		jflag = 0;		setjobctl(0);#endif		shellexec(argv + 1, environment(), pathval(), 0);	}	return 0;}

⌨️ 快捷键说明

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