Ejemplo n.º 1
0
//matches a command executes it,
//returns whether the shell should continue running.
bool match_command(struct CommandBuffer * cb,char ** cwd,const char ** path){
    bool res = true;
    if(strcmp(cb->command_buffer,"exit") == 0){
        res = false;
    }else{
        char ** buff = parse_command(cb->command_buffer);
        if(strcmp(buff[0],"cd") == 0){
            change_dir(buff[0],cwd);
        }else{
            bool found_pipe = false;
            size_t pos = 0;
            while(buff[pos]){
                if(strcmp(buff[pos],"|") == 0){
                    found_pipe = true;
                    buff[pos] = NULL;
                    break;
                }
                ++pos;
            }
            if(found_pipe){
                res = execute_pipe(path,buff[0],&buff[0],buff[pos+1],&buff[pos+1]);
            }else{
                res = execute(path,buff[0],&buff[0]);
            }
        }
        free(buff);
    }
    return res;
}
Ejemplo n.º 2
0
void
execute_command(command_t c, int profiling) {
  
  switch (c->type) {
      
    case IF_COMMAND:
      execute_if(c, profiling);
      break;
    case UNTIL_COMMAND:
      execute_until(c, profiling);
      break;
    case WHILE_COMMAND:
      execute_while(c, profiling);
      break;
    case SEQUENCE_COMMAND:
      execute_sequence(c, profiling);
      break;
    case PIPE_COMMAND:
      execute_pipe(c, profiling);
      break;
    case SIMPLE_COMMAND:
      execute_simple(c, profiling);
      break;
    case SUBSHELL_COMMAND:
      execute_subshell(c, profiling);
      break;
    default:
      error (1, 0, "command not found");
  }
  
}
Ejemplo n.º 3
0
int main(void)
{
	int i, background;
	char input[MAX];
	char path[MAX] = ".";
	char entry_dir[MAX] = {0};
	char working_dir[MAX] = {0};
	char *arguments[512] = {0}; /* enter up to 511 arguments, one is reserved for the execution cmd */

	getcwd(entry_dir, sizeof(entry_dir)); /* get the entry directory */

	/* infinite loop until the user enters exit */
	while (1) {
		signal(SIGINT, ctrlc_handler); /* signalhandler to avoid CTRL+C killing the shell */

		prompt(path);
		fgets(input, MAX, stdin); /* read from standardinput */
		i = 0;

		while (input[i] != '\n')
			i++;
		input[i] = '\0'; /* remove newline from read string and replace it with \0 */

		background = parse_input(input, arguments); /* parse string and set background/pipe option */

		if (*arguments != 0){ /* press Enter without SEGfault :P */
			/* all ifs are built-ins for the shell, except for the else */
			if (strcmp(arguments[0], "exit") == 0)
				return 0;
			else if (background == 2) {
				execute_pipe(arguments);
			} else if (strcmp(arguments[0], "cd") == 0) {
				if (arguments[1] == NULL) { /* change to user's HOME if only cd is entered */
					chdir(getenv("HOME"));
					getcwd(working_dir, sizeof(working_dir));
					print_current_dir(entry_dir, working_dir, path);
				} else {
					chdir(arguments[1]);
					getcwd(working_dir, sizeof(working_dir));
					print_current_dir(entry_dir, working_dir, path);
				}
			} else if (strcmp(arguments[0], "wait") == 0) {
				if (arguments[1] == NULL) /* maybe there is no pid given by the user */
					puts("No PID!");
				else {
					keepWaiting = 1;
					waiting(arguments);
				}
			} else {
				execute_cmd(arguments, background);
			}
		}
	}
}
Ejemplo n.º 4
0
int execute_command_type(command_t c)
{
    switch(c->type){
            
        case SIMPLE_COMMAND:
            execute(c);
            break;
            
        case PIPE_COMMAND:
            execute_pipe(c);
            break;
            
        case AND_COMMAND:
            // the first command already fails
            if(execute_command_type(c->u.command[0]) != 0){
                c->status = command_status(c->u.command[0]);
            }else{
                c->status = execute_command_type(c->u.command[1]);
            }
            break;
            
        case OR_COMMAND:
            if(execute_command_type(c->u.command[0]) == 0){
                c->status = 0;
            }else{
                // try the second command if the first fails
                c->status = execute_command_type(c->u.command[1]);
            }
            break;
            
        case SUBSHELL_COMMAND:
            c->status = execute_command_type(c->u.subshell_command);
            break;
            
        case SEQUENCE_COMMAND: // return the status of the last command
            execute_sequence(c);
            break;
    }
    return c->status;
}
void
execute_command (command_t c, bool time_travel)
{
	char cmd_type = ' ';
	switch(c->type) {
    case SIMPLE_COMMAND:
    	execute_simple(c);
    	cmd_type = 's';
    	break;
    case SUBSHELL_COMMAND:
    	execute_subshell(c, time_travel);
    	cmd_type = '(';
    	break;
    case AND_COMMAND:
    	execute_and(c, time_travel);
    	cmd_type = 'a';
    	break;
    case OR_COMMAND:
    	execute_or(c, time_travel);
    	cmd_type = 'o';
    	break;
    case SEQUENCE_COMMAND:
    	execute_sequence(c, time_travel);
    	cmd_type = ';';
    	break;
    case PIPE_COMMAND:
    	execute_pipe(c, time_travel);
    	cmd_type = '|';
    	break;
		default:
			return;
	}
	if (DEBUG) {
		printf("%c cmd - exit status: %d\n", cmd_type, c->status);
	}
}