Example #1
0
int
main(int argc, char *argv[])
{
	struct stackmark smark, smark2;
	volatile int state;
	char *shinit;

	(void) setlocale(LC_ALL, "");
	initcharset();
	state = 0;
	if (setjmp(main_handler.loc)) {
		switch (exception) {
		case EXEXEC:
			exitstatus = exerrno;
			break;

		case EXERROR:
			exitstatus = 2;
			break;

		default:
			break;
		}

		if (state == 0 || iflag == 0 || ! rootshell ||
		    exception == EXEXIT)
			exitshell(exitstatus);
		reset();
		if (exception == EXINT)
			out2fmt_flush("\n");
		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 = &main_handler;
#ifdef DEBUG
	opentrace();
	trputs("Shell args:  ");  trargs(argv);
#endif
	rootpid = getpid();
	rootshell = 1;
	init();
	setstackmark(&smark);
	setstackmark(&smark2);
	procargs(argc, argv);
	pwd_init(iflag);
	if (iflag)
		chkmail(1);
	if (argv[0] && argv[0][0] == '-') {
		state = 1;
		read_profile("/etc/profile");
state1:
		state = 2;
		if (privileged == 0)
			read_profile("${HOME-}/.profile");
		else
			read_profile("/etc/suid_profile");
	}
state2:
	state = 3;
	if (!privileged && iflag) {
		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
			state = 3;
			read_profile(shinit);
		}
	}
state3:
	state = 4;
	popstackmark(&smark2);
	if (minusc) {
		evalstring(minusc, sflag ? 0 : EV_EXIT);
	}
	if (sflag || minusc == NULL) {
state4:	/* XXX ??? - why isn't this before the "if" statement */
		cmdloop(1);
	}
	exitshell(exitstatus);
	/*NOTREACHED*/
	return 0;
}
Example #2
0
File: main.c Project: cbsd/cbsd
int
main(int argc, char *argv[])
{
	struct stackmark smark, smark2;
	volatile int state;
	char *shinit;

#ifdef CBSD
	char *MY_APP = NULL;
	char *cbsdpath = NULL;
	char *workdir = NULL;
	char *cbsd_disable_history = NULL; //getenv
	chdir("/var/empty");
	/* Only use history when stdin is a tty. */
	if ( isatty(0) && isatty(1) ) {
		cbsd_enable_history = 1;
	}
#endif

	(void) setlocale(LC_ALL, "");
	initcharset();
	state = 0;
	if (setjmp(main_handler.loc)) {
		switch (exception) {
		case EXEXEC:
			exitstatus = exerrno;
			break;

		case EXERROR:
			exitstatus = 2;
			break;

		default:
			break;
		}

		if (state == 0 || iflag == 0 || ! rootshell ||
		    exception == EXEXIT)
			exitshell(exitstatus);
		reset();
		if (exception == EXINT)
			out2fmt_flush("\n");
		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 = &main_handler;
#ifdef DEBUG
	opentrace();
	trputs("Shell args:  ");  trargs(argv);
#endif
	rootpid = getpid();
	rootshell = 1;
	INTOFF;
	initvar();
	setstackmark(&smark);
	setstackmark(&smark2);

#ifdef CBSD
	if (argc>1) {
		if (!strcmp(argv[1],"--help")) {
			system("/usr/local/bin/cbsd help");
			exit(0);
		} else {
			if (!strcmp(argv[1],"version")) {
				printf("%s\n",VERSION);
				exit(0);
			}
		}
	}

	cbsd_disable_history=lookupvar("NO_CBSD_HISTORY");

	if ( cbsd_disable_history != NULL ) cbsd_enable_history=0;

	workdir=lookupvar("workdir");

	if ( workdir == NULL )  {
		read_profile("/etc/rc.conf");
		setvarsafe("workdir", lookupvar("cbsd_workdir"), 0);
	}

	workdir=lookupvar("workdir");
	if ( workdir == NULL ) {
		out2fmt_flush("cbsd: no workdir defined\n");
		exitshell(1);
	}

	setvarsafe("PS1","cbsd@\\h> ",1);
	setvarsafe("workdir",workdir,1);
	workdir=lookupvar("workdir"); //  ^^ after "setsave*" original is free
	cbsdpath = calloc(MAXPATHLEN, sizeof(char *));

	if (argv[1]) {
		setvarsafe("CBSD_APP",basename(argv[1]),1);
	}

	if (cbsdpath == NULL) {
		out2fmt_flush("cbsd: out of memory for cbsdpath\n");
		exitshell(1);
	}

	// %s/modules must be first for opportunity to have a module commands greater priority than the original CBSD command.
	// This makes it possible to write a 3rd party modules with altered functionality of the original code.
	sprintf(cbsdpath,"%s/modules:%s/bin:%s/sbin:%s/tools:%s/jailctl:%s/nodectl:%s/system:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",workdir,cbsd_distdir,cbsd_distdir,cbsd_distdir,cbsd_distdir,cbsd_distdir,cbsd_distdir);
	setvarsafe("PATH",cbsdpath,1);
	ckfree(cbsdpath);

	// read global params first (disable/enable colors, repos etc..)
	read_profile("${workdir}/etc/defaults/global.conf");
	read_profile("${workdir}/etc/global.conf");

	if (lookupvar("NOCOLOR") != NULL ) {
		putenv("NOCOLOR=1");
	}

	// non-interactive global env
	if (lookupvar("NOINTER") != NULL ) {
		setvarsafe("inter","1",1);
		putenv("inter=0");
	}

	read_profile("/usr/local/cbsd/cbsd.conf");
	read_profile("${workdir}/etc/defaults/logger.conf");
	read_profile("${workdir}/etc/logger.conf");

	if (cbsd_enable_history==1) {
		cbsd_history_file=calloc(MAXPATHLEN, sizeof(char *));
		sprintf(cbsd_history_file,"%s/%s",workdir,CBSD_HISTORYFILE);
	}

#endif

	procargs(argc, argv);
	pwd_init(iflag);
	INTON;
#ifndef CBSD
	if (iflag)
		chkmail(1);
#endif
	if (argv[0] && argv[0][0] == '-') {
		state = 1;
		read_profile("/etc/profile");
state1:
		state = 2;
		if (privileged == 0)
			read_profile("${HOME-}/.profile");
		else
			read_profile("/etc/suid_profile");
	}
state2:
	state = 3;
	if (!privileged && iflag) {
		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
			state = 3;
			read_profile(shinit);
		}
	}
state3:
	state = 4;
	popstackmark(&smark2);
	if (minusc) {
		evalstring(minusc, sflag ? 0 : EV_EXIT);
	}
state4:
	if (sflag || minusc == NULL) {
		cmdloop(1);
	}
	exitshell(exitstatus);
	/*NOTREACHED*/
	return 0;
}