/*
 * Copyright 2011 Marian Ohligs, Chair for Operating Systems,
 *                               RWTH Aachen University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#undef errno
extern int errno;

void showlogo() {
	printf("\n\n");
	printf("================================================================================\n");
	printf("                               m(etalsvm)shell\n\n");
	printf("================================================================================\n");
}

void help() {
	printf("possible commands: \n");
	printf("exit > exit shell \n");
}

int main(int argc, char** argv)
{
	char* command = malloc(1024*sizeof(char));

	int size = 0, status = 0;
	pid_t pid;
	system("clear");
	showlogo();
	while(1) {
		printf("$ ");
		do {
			size = scanf("%s", command);
		} while(size <= 0);
		if(!strcmp(command, "exit")) {
			return 0;
		}
		if(!strcmp(command, "help")) {
			help();
		} else {
			pid = fork();
			if (pid == 0) { //child
				char* newargv[] = {command, NULL};
				char* newenv[] = {"USER=root", "PATH=/bin:/sbin:/usr/bin", "PWD=/", "TEMP=/tmp", NULL};

				execve(command, newargv, newenv);
				return errno;
			} else {
				wait(&status);
			}
		}	
	}
	return errno;
}