Пример #1
0
void Router::displayMenu(int position)
{
	std::cout << "----- " << this->getTitle() << "----- " << std::endl;
	int size = items.size();
	for (int i = 0; i < size; i++)
		std::cout << ((position == i) ? "> " : "  ") << items[i].getTitle() << std::endl;
	if (parent != NULL)
		std::cout << (is_back(position) ? "> " : "  ") << "Back." << std::endl;
	std::cout << (is_exit(position) ? "> " : "  ") << "Exit program." << std::endl;
}
Пример #2
0
void Router::setActive(int* position)
{
	if (is_exit(*position))
		exit(0);
	if (is_back(*position)){
		if (parent != NULL)
			parent->navigate();
		*position = 0;
		return;
	}

	Router* router = &(items[*position]);
	router->run();
	*position = 0;
	router->navigate();

}
Пример #3
0
void do_pipe(char * input)
{		
	int status,i,j,k,**fd,back=0,len;
	char **command;
	int *child;
	
	back=is_back(input);
	len=strlen(input);
	k=pipe_number(input);
	
	command=(char **)malloc((k+1)*sizeof(char *));
	for(i=0;i<k+1;i++)
		command[i]=(char *)malloc((len+1)*sizeof(char));

	child=(int *)malloc((k+1)*sizeof(char *));
	
	fd=(int **)malloc(k*sizeof(int *));
	for(i=0;i<k;i++)
		fd[i]=(int *)malloc(2*sizeof(int));
	

	k=0;
	j=0;
	for (i=0;i<=len;i++)
	{
		if (input[i]!='|')
		{
			command[k][j]=input[i];
			j++;
		}
		else
		{
			command[k][j]='\0';
			k++;
			j=0;
		}
	}

	//create the pipe
	for(i=0;i<k;i++)
		if(pipe(fd[i]) == -1)
		{
			fprintf(stderr, "Open pipe error !\n");
			//printf("Open pipe error !\n");
			//return 0;
		}
	//create the first child
	i=0;
	if((child[i]=fork())==0)
	{
		close(fd[i][0]);
		if(fd[i][1] != STDOUT_FILENO) 
		{
			if(dup2(fd[i][1], STDOUT_FILENO) == -1) 
			{
				fprintf(stderr, "Redirect Standard Out error !\n");
				//printf("Redirect Standard Out error !\n");
				//return -1;
			}
			close(fd[i][1]);
		}
		redirect(command[i]);	
		exit(1);	//child1 exit
	}
	else
	{
		//wait for child
		waitpid(child[i],&status,0);
		close(fd[i][1]);
	}
	i++;
	while(i<k)
	{
		if ((child[i]=fork())==0)
		{
			if(fd[i][0] != STDIN_FILENO) 
			{
				if(dup2(fd[i-1][0], STDIN_FILENO) == -1) 				
				{
					fprintf(stderr, "Redirect Standard In error !\n");
					//printf("Redirect Standard In Error !\n");
					//return -1;
				}
				close(fd[i-1][0]);
				
				if(dup2(fd[i][1], STDOUT_FILENO) == -1) 				
				{
					fprintf(stderr, "Redirect Standard Out error !\n");
					//printf("Redirect Standard Out error !\n");
					//return -1;
				}
				close(fd[i][1]);
			}
			redirect(command[i]);
			exit(1);
		}
		else
		{
			//wait for child
			waitpid(child[i],&status,0);
			close(fd[i][1]);
			i++;
		}
	}
	//create the last child
	if((child[i] = fork()) == 0) 
	{
		close(fd[i-1][1]);
		if(fd[i-1][0] != STDIN_FILENO) 
		{
			if(dup2(fd[i-1][0], STDIN_FILENO) == -1) 
			{
				fprintf(stderr, "Redirect Standard In error !\n");
				//printf("Redirect Standard In Error !\n");
				//return -1;
			}
			close(fd[i-1][0]);
		}
		redirect(command[i]);
		exit(1);
	}
	else
	{
		if(back==0)  
		{
			waitpid(child[i], NULL, 0);
			close(fd[i-1][1]);
		}
	}

	for(i=0;i<k;i++)
		free(fd[i]);
	free(fd);
	
	for(i=0;i<k+1;i++)
		free(command[i]);
	free(command);
	free(child);
}
Пример #4
0
int do_redirection(char *input)	//redircetion
{
	char *command_path,*real_command;
	char *out_filename,*in_filename;
	char **parsed_command;
	int len,status,i,j,k,back=0,fd_out,fd_in,flag_out=0,flag_in=0;
	pid_t pid;
	
	back=is_back(input);
	len=strlen(input);


	out_filename=(char *)malloc((len+1)*(sizeof(char)));
	in_filename=(char *)malloc((len+1)*(sizeof(char)));
	real_command=(char *)malloc((len+1)*(sizeof(char)));
	
	
	

	for(i=0;i<len;i++){
		if ((input[i]!='>') && (input[i]!='<'))
			real_command[i]=input[i];
		else{
			if (input[i]=='>')
				flag_out=1;
			if (input[i]=='<')
				flag_in=1;
			break;
		}
	}
	
	real_command[i]='\0';
	i++;
	
	


	if(flag_out==1 && input[i]=='>')
	{
		flag_out=2;
		i++;
	}
	else if (flag_in==1 && input[i]=='<')
	{
		flag_in=2;
		i++;
	}


	while ((input[i]==' '||input[i]=='	') && i<len)
		i++;
	
	j=0;
	out_filename[0]='\0';
	in_filename[0]='\0';

	if(flag_out>0){	
		while (i<=len){
			if(input[i]=='<'){
				out_filename[j]='\0';
				break;
			}
			out_filename[j]=input[i];
			i++;
			j++;
		}
	}
	if(flag_in>0){
		while (i<=len){
			if (input[i]=='>'){
				in_filename[j]='\0';
				break;
			}
			in_filename[j]=input[i];
			i++;
			j++;
		}
	}

	if (i<len){
		j=0;
		if (flag_out>0 && input[i]=='<'){
			i++;
			flag_in=1;
			if(input[i]=='>'){
				flag_in=2;
				i++;
			}

			while ((input[i]==' ' || input[i]=='	') && i<len)
				i++;
			while (i<=len){
				in_filename[j]=input[i];
				i++;
				j++;
			}
		}
		else if (flag_in>0 && input[i]=='>'){
			i++;
			flag_out=1;
			if(input[i]=='>'){
				flag_out=2;
				i++;
			}

			while ((input[i]==' ' || input[i]=='	') && i<len)
				i++;
			while (i<=len){
				out_filename[j]=input[i];
				i++;
				j++;
			}
		}
		else{
			fprintf(stderr,"ERROR! Invalid file name!\n");
			return -1;
		}
	}
	
	
	
	k=number(real_command);
	parsed_command=parse(real_command);
	
	if(!find_command(real_command)){
		fprintf(stderr, "%s: Command not found.\n", real_command);
		return -1;
	}
	
	if(strcmp(parsed_command[0], "time") == 0)
        {
        	clock();
                Do_time(real_command);
        }
	if(strcmp(parsed_command[0], "cd") == 0) //cd
        {
                Do_cd(parsed_command[1]);
        }
        if(strcmp(parsed_command[0], "echo") == 0) //echo
        {
                Do_echo(real_command);
        }
        if(strcmp(parsed_command[0], "viewproc") == 0) //viewproc
        {
                Do_viewproc(real_command);
        }
	if(strcmp(parsed_command[0], "ls") == 0){
		Do_ls();
	}
	if(strcmp(parsed_command[0], "grep") == 0){
                Do_grep(parsed_command[1], parsed_command[2]);
        }
	if(strcmp(parsed_command[0], "mkdir") == 0){
		Do_mkdir(parsed_command[1]);
	}

	if((pid = fork()) == 0) {
		if(flag_out==1)
			fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
		if(flag_out==2)
			fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
		if(flag_in==1)
			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
		if(flag_in==2)
			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
       		
       		if(fd_out==-1) {
			printf("Open out %s error \n", out_filename);
			return -1;
		}
		if(fd_in==-1) {
			fprintf(stderr,"Open in %s error \n", in_filename);
			return -1;
		}

		if(flag_out>0){
			if(dup2(fd_out, STDOUT_FILENO) == -1){
				fprintf(stderr,"Redirect Standard Out Error !\n");
				exit(1);
			}
		}
		if(flag_in>0){
			if (dup2(fd_in,STDIN_FILENO)==-1){
				fprintf(stderr,"Redirect Standard Out Error !\n");
				exit(1);
			}
		}
		execv(command_path,parsed_command); // CANT DO THIS FUNCTION !
		exit(1);
	}
	else{                     //parent
		if(back==0)
			pid = waitpid(pid, &status, 0);
		else
			pid = waitpid(pid, &status, WNOHANG);
	}
	//free space;
	/*free(out_filename);
	free(in_filename);
	free(command_path);
	for(i=0;i<k;i++)   
		free(parsed_command[i]);
	free(parsed_command);
	*/	
return 1;
}
Пример #5
0
int do_redirection(char *input)	//redircetion
{
	char *command_path,*real_command;
	char *out_filename,*in_filename;
	char **parsed_command;
	int len,status,i,j,k,back=0,fd_out=0,fd_in=0,flag_out=0,flag_in=0;
	pid_t pid;

	back=is_back(input);
	len=strlen(input);
	
	out_filename=(char *)malloc((len+1)*(sizeof(char)));
	in_filename=(char *)malloc((len+1)*(sizeof(char)));
	real_command=(char *)malloc((len+1)*(sizeof(char)));

	for(i=0; i<len; i++)
	{
		if (input[i]!='>' && input[i]!='<')
			real_command[i]=input[i];
		else
		{
			if (input[i]=='>')
				flag_out=1;
			if (input[i]=='<')
				flag_in=1;
			break;
		}
	}
	
	real_command[i]='\0';
	i++;
	
	if(flag_out==1 && input[i]=='>')
	{
		flag_out=2;
		i++;
	}
	else if (flag_in==1 && input[i]=='<')
	{
		flag_in=2;
		i++;
	}

	while ((input[i]==' ' || input[i]=='	') && i<len)
		i++;
	j=0;
	out_filename[0]='\0';
	in_filename[0]='\0';

	if(flag_out>0)
	{	
		while (i<=len)
		{
			if(input[i]=='<')
			{
				out_filename[j]='\0';
				break;
			}
			out_filename[j]=input[i];
			i++;
			j++;
		}
	}
	
	if(flag_in>0)
	{
		while (i<=len)
		{
			if (input[i]=='>')
			{
				in_filename[j]='\0';
				break;
			}
			in_filename[j]=input[i];
			i++;
			j++;
		}
	}
	if (i<len)
	{
		j=0;
		if (flag_out>0 && input[i]=='<')
		{
			i++;
			flag_in=1;
			if(input[i]=='>')
			{
				flag_in=2;
				i++;
			}

			while ((input[i]==' ' || input[i]=='	') && i<len)
				i++;
			while (i<=len)
			{
				in_filename[j]=input[i];
				i++;
				j++;
			}
		}
		else if (flag_in>0 && input[i]=='>')
		{
			i++;
			flag_out=1;
			if(input[i]=='>')
			{
				flag_out=2;
				i++;
			}

			while ((input[i]==' ' || input[i]=='	') && i<len)
				i++;
			while (i<=len)
			{
				out_filename[j]=input[i];
				i++;
				j++;
			}
		}
		else
		{
			fprintf(stderr,"ERROR!can't find the file!\n");
			return -1;
		}
	}
	
	//for debug
	/*printf("real_command: %s\n",real_command);
	printf("out_filename: %s\n",out_filename);
	printf("in_filename: %s\n",in_filename);*/

	k=number(real_command);
	parsed_command=parse(real_command);
	
	if(strcmp(parsed_command[0],"cd")==0 || strcmp(parsed_command[0], "echo") == 0 || strcmp(parsed_command[0], "viewproc") == 0 || strcmp(parsed_command[0], "time")==0) //build-in functions
	{
			if(strcmp(parsed_command[0], "cd") == 0) //cd
			{
				do_cd(parsed_command);
			}
			else if(strcmp(parsed_command[0], "echo") == 0) //echo
			{
				if (pid=fork() == 0)
				{
					if(flag_out==1)
						fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
					if(flag_out==2)
						fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
					if(fd_out==-1) 
					{
						printf("Open out %s error \n", out_filename);
						return -1;
					}
					if(flag_out>0)
					{
						if(dup2(fd_out, STDOUT_FILENO) == -1)
						{
							fprintf(stderr,"Redirect Standard Out Error !\n");
							exit(1);
						}
					}
					do_echo(real_command);
					exit(1);
				}
				else
				{                     //parent
					if(back==0)
						pid=waitpid(pid, &status, 0);
					else
						pid=waitpid(pid, &status, WNOHANG);
				}
			}
			else if(strcmp(parsed_command[0], "viewproc") == 0) //viewproc
			{
				if (pid=fork() == 0)
				{
					if(flag_out==1)
						fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
					if(flag_out==2)
						fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
					if(fd_out==-1) 
					{
						printf("Open out %s error \n", out_filename);
						return -1;
					}
					if(flag_out>0)
					{
						if(dup2(fd_out, STDOUT_FILENO) == -1)
						{
							fprintf(stderr,"Redirect Standard Out Error !\n");
							exit(1);
						}
					}
					do_viewproc(parsed_command[1]);
					close(flag_out);
					exit(1);
				}
				else
				{                     //parent
					if(back==0)
						pid=waitpid(pid, &status, 0);
					else
						pid=waitpid(pid, &status, WNOHANG);
				}
			}
			else if(strcmp(parsed_command[0], "time") == 0) //time
			{
				gettimeofday(&tpstart,0);
				do_time(real_command);
			}
			for(i=0;i<k;i++)
				free(parsed_command[i]);
			free(parsed_command);
			free(real_command);
			return 1;
	}
	
	command_path=is_file_exist(parsed_command[0]);
	if(command_path==NULL)	//can't find the order
	{
		fprintf(stderr,"This is command is not founded ?!\n");
		// free space
		for(i=0;i<k;i++)
			free(parsed_command[i]);
		free(parsed_command);
		free(real_command);
		return -1;
	}
	
	if((pid = fork()) == 0) 
	{
		if(flag_out==1)
			fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
		if(flag_out==2)
			fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
		if(flag_in==1)
			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
		if(flag_in==2)
			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
       		if(fd_out==-1) 
		{
			printf("Open out %s error \n", out_filename);
			return -1;
		}
		if(fd_in==-1) 
		{
			fprintf(stderr,"Open in %s error \n", in_filename);
			return -1;
		}

		if(flag_out>0)
		{
			if(dup2(fd_out, STDOUT_FILENO) == -1)
			{
				fprintf(stderr,"Redirect Standard Out Error !\n");
				exit(1);
			}
		}
		if(flag_in>0)
		{
			if (dup2(fd_in,STDIN_FILENO)==-1)
			{
				fprintf(stderr,"Redirect Standard Out Error !\n");
				exit(1);
			}
		}
			execv(command_path,parsed_command);		
			exit(1);
	}		
	else
	{                     //parent
		if(back==0)
			pid=waitpid(pid, &status, 0);
		else
			pid=waitpid(pid, &status, WNOHANG);
	}
	//free space
	free(out_filename);
	free(in_filename);
	free(command_path);
	for(i=0;i<k;i++)   
		free(parsed_command[i]);
	free(parsed_command);
	return 1;
}