コード例 #1
0
ファイル: main.c プロジェクト: dezelin/kBuild
int
main(int argc, char **argv)
{
	struct jmploc jmploc;
	struct stackmark smark;
	volatile int state;
	char *shinit;

	setlocale(LC_ALL, "");

	/* Just a --version for show. */
	if (argc > 1
	 && argv[1][0] == '-'
	 && argv[1][1] == '-') {
		if (!strcmp(&argv[1][2], "version")) {
			printf("kmk_ash - kBuild version %d.%d.%d\n",
				   KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
			return 0;
		}
		if (!strcmp(&argv[1][2], "help")) {
			printf("usage: kmk_ash [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
				   "               [+o option_name] [command_file [argument ...]]\n"
				   "   or: kmk_ash -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
				   "               [+o option_name] command_string [command_name [argument ...]]\n"
				   "   or: kmk_ash -s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
				   "               [+o option_name] [argument ...]\n"
				   "   or: kmk_ash --help\n"
				   "   or: kmk_ash --version\n",
				   argv[0], argv[0], argv[0], argv[0], argv[0]);
			return 0;
		}
	}

#if PROFILE
	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
#endif
	state = 0;
	if (setjmp(jmploc.loc)) {
		/*
		 * When a shell procedure is executed, we raise the
		 * exception EXSHELLPROC to clean up before executing
		 * the shell procedure.
		 */
		switch (exception) {
		case EXSHELLPROC:
			rootpid = getpid();
			rootshell = 1;
			minusc = NULL;
			state = 3;
			break;

		case EXEXEC:
			exitstatus = exerrno;
			break;

		case EXERROR:
			exitstatus = 2;
			break;

		default:
			break;
		}

		if (exception != EXSHELLPROC) {
			if (state == 0 || iflag == 0 || ! rootshell)
				exitshell(exitstatus);
		}
		reset();
		if (exception == EXINT
#if ATTY
		 && (! attyset() || equal(termval(), "emacs"))
#endif
		 ) {
			out2c('\n');
			flushout(&errout);
		}
		popstackmark(&smark);
		FORCEINTON;				/* enable interrupts */
		if (state == 1)
			goto state1;
		else if (state == 2)
			goto state2;
		else if (state == 3)
			goto state3;
		else
			goto state4;
	}
	handler = &jmploc;
#ifdef DEBUG
#if DEBUG == 2
	debug = 1;
#endif
	opentrace();
	trputs("Shell args:  ");  trargs(argv);
#endif
	rootpid = getpid();
	rootshell = 1;
#ifdef _MSC_VER
    {
        extern void init_syntax(void);
        init_syntax();
    }
#endif
	init();
	setstackmark(&smark);
	procargs(argc, argv);
	if (argv[0] && argv[0][0] == '-') {
		state = 1;
#ifndef KMK
		read_profile("/etc/profile");
#endif
state1:
		state = 2;
#ifndef KMK
		read_profile(".profile");
#endif
	}
state2:
	state = 3;
#ifndef KMK
	if (getuid() == geteuid() && getgid() == getegid()) {
		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
			state = 3;
			read_profile(shinit);
		}
	}
#endif
state3:
	state = 4;
	if (sflag == 0 || minusc) {
		static int sigs[] =  {
		    SIGINT, SIGQUIT, SIGHUP,
#ifdef SIGTSTP
		    SIGTSTP,
#endif
		    SIGPIPE
		};
#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
		int i;

		for (i = 0; i < SIGSSIZE; i++)
		    setsignal(sigs[i], 0);
	}

	if (minusc)
		evalstring(minusc, 0);

	if (sflag || minusc == NULL) {
state4:	/* XXX ??? - why isn't this before the "if" statement */
		cmdloop(1);
	}
#if PROFILE
	monitor(0);
#endif
	exitshell(exitstatus);
	/* NOTREACHED */
}
コード例 #2
0
ファイル: main.c プロジェクト: neonatura/share
int
main(int argc, char **argv)
{
	char *shinit;
	volatile int state;
	struct jmploc jmploc;
	struct stackmark smark;
	int login;
  int i;

  for (i = 1; i < argc; i++) {
    if (0 == strcmp(argv[i], "--help") ||
        0 == strcmp(argv[i], "-h")) {
      sxsh_print_usage();
      return (0);
    }
    if (0 == strcmp(argv[i], "--version") ||
        0 == strcmp(argv[i], "-v")) {
      sxsh_print_version();
      return (0);
    }
  } 

#ifdef __GLIBC__
	dash_errno = __errno_location();
#endif

#if PROFILE
	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
#endif
	state = 0;
	if (unlikely(setjmp(jmploc.loc))) {
		int e;
		int s;

		reset();

		e = exception;

		s = state;
		if (e == EXEXIT || s == 0 || iflag == 0 || shlvl)
			exitshell();

		if (e == EXINT
#if ATTY
		 && (! attyset() || equal(termval(), "emacs"))
#endif
		 ) {
			out2c('\n');
#ifdef FLUSHERR
			flushout(out2);
#endif
		}
		popstackmark(&smark);
		FORCEINTON;				/* enable interrupts */
		if (s == 1)
			goto state1;
		else if (s == 2)
			goto state2;
		else if (s == 3)
			goto state3;
		else
			goto state4;
	}
	handler = &jmploc;
#ifdef DEBUG
	opentrace();
	trputs("Shell args:  ");  trargs(argv);
#endif
	rootpid = getpid();
	init();
	setstackmark(&smark);
	login = procargs(argc, argv);
	if (login) {
		state = 1;
		read_profile("/etc/profile");
state1:
		state = 2;
		read_profile("$HOME/.profile");
	}
state2:
	state = 3;
	if (
#ifndef linux
		getuid() == geteuid() && getgid() == getegid() &&
#endif
		iflag
	) {
		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
			read_profile(shinit);
		}
	}
	popstackmark(&smark);
state3:
	state = 4;
	if (minusc)
		evalstring(minusc, sflag ? 0 : EV_EXIT);

	if (sflag || minusc == NULL) {
state4:	/* XXX ??? - why isn't this before the "if" statement */
		cmdloop(1);
	}
#if PROFILE
	monitor(0);
#endif
#if GPROF
	{
		extern void _mcleanup(void);
		_mcleanup();
	}
#endif
	exitshell();
	/* NOTREACHED */
}
コード例 #3
0
ファイル: main.c プロジェクト: dezelin/kBuild
SH_NORETURN_1 void
shell_main(shinstance *psh, int argc, char **argv)
{
	struct jmploc jmploc;
	struct stackmark smark;
	volatile int state;
	char *shinit;

	state = 0;
	if (setjmp(jmploc.loc)) {
		/*
		 * When a shell procedure is executed, we raise the
		 * exception EXSHELLPROC to clean up before executing
		 * the shell procedure.
		 */
		switch (psh->exception) {
		case EXSHELLPROC:
			psh->rootpid = /*getpid()*/ psh->pid;
			psh->rootshell = 1;
			psh->minusc = NULL;
			state = 3;
			break;

		case EXEXEC:
			psh->exitstatus = psh->exerrno;
			break;

		case EXERROR:
			psh->exitstatus = 2;
			break;

		default:
			break;
		}

		if (psh->exception != EXSHELLPROC) {
			if (state == 0 || iflag(psh) == 0 || ! psh->rootshell)
				exitshell(psh, psh->exitstatus);
		}
		reset(psh);
		if (psh->exception == EXINT
#if ATTY
		 && (! attyset(psh) || equal(termval(psh), "emacs"))
#endif
		 ) {
			out2c(psh, '\n');
			flushout(&psh->errout);
		}
		popstackmark(psh, &smark);
		FORCEINTON;				/* enable interrupts */
		if (state == 1)
			goto state1;
		else if (state == 2)
			goto state2;
		else if (state == 3)
			goto state3;
		else
			goto state4;
	}
	psh->handler = &jmploc;
	psh->rootpid = /*getpid()*/ psh->pid;
	psh->rootshell = 1;
#ifdef DEBUG
#if DEBUG == 2
	debug(psh) = 1;
#endif
	opentrace(psh);
	trputs(psh, "Shell args:  ");  trargs(psh, argv);
#endif

	init(psh);
	setstackmark(psh, &smark);
	procargs(psh, argc, argv);
	if (argv[0] && argv[0][0] == '-') {
		state = 1;
		read_profile(psh, "/etc/profile");
state1:
		state = 2;
		read_profile(psh, ".profile");
	}
state2:
	state = 3;
	if (sh_getuid(psh) == sh_geteuid(psh) && sh_getgid(psh) == sh_getegid(psh)) {
		if ((shinit = lookupvar(psh, "ENV")) != NULL && *shinit != '\0') {
			state = 3;
			read_profile(psh, shinit);
		}
	}
state3:
	state = 4;
	if (sflag(psh) == 0 || psh->minusc) {
		static int sigs[] =  {
		    SIGINT, SIGQUIT, SIGHUP,
#ifdef SIGTSTP
		    SIGTSTP,
#endif
		    SIGPIPE
		};
#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
		unsigned i;

		for (i = 0; i < SIGSSIZE; i++)
		    setsignal(psh, sigs[i], 0);
	}

	if (psh->minusc)
		evalstring(psh, psh->minusc, 0);

	if (sflag(psh) || psh->minusc == NULL) {
state4:	/* XXX ??? - why isn't this before the "if" statement */
		cmdloop(psh, 1);
	}
	exitshell(psh, psh->exitstatus);
	/* NOTREACHED */
}