Ejemplo n.º 1
0
void		proceed_to_execution(t_var *var)
{
	var->list = lexer(var, var->lex);
	var->list = create_tokens(var->list);
	var->root = parser(var->list);
	execute_tree(var, var->root);
	clean_tree(var->root);
	var->root = NULL;
	var->list = NULL;
	clean_line_pointers(var);
}
Ejemplo n.º 2
0
/* Runs the command in the graph_node in a different process, and sets node->pid
   to the pid of the child process. */
static void dispatch_graph_node(struct graph_node *node)
{
    int p;
    if ((p = fork()) == 0)
    {
        int status = execute_tree(node->cmd);
        _exit(status);
    }
    else if (p < 0)
    {
        ERR("Failed to fork() for dispatch_graph_node()\n");
    }
    node->pid = p;
}
Ejemplo n.º 3
0
int main( int argc, char **argv )
{
	/* Initialize and fill a syntax tree */
	snode_t base,nodes[64];
	symtab_t smt;
	symtab_init( &smt, 128 );
	int i,n;
	char *tok[128];
	char out[512];
	char st[128] = "s1,s2 + t1,t2 - z1,z2 = v1,v2 + w1,w2 + x1,x2 + y1,y2";
	snode_init( &base, 128 );
	snode_set_string( &base, st );
	build_tree( &base );
	stree_print( &base, 0 );
	execute_tree( &base, out, &smt );
	stree_free( &base );

	return 0;
}
Ejemplo n.º 4
0
void
execute_command (command_t c)
{
  execute_tree(c);
}
Ejemplo n.º 5
0
static int execute_tree(command_t cmd)
{
    int status;
    
    if (cmd->type == SIMPLE_COMMAND)
    {
        status = execute_simple_cmd(cmd);
    }
    else if (cmd->type == SEQUENCE_COMMAND) /* Always execute second command */
    {
        execute_tree(cmd->u.command[0]);
        status = execute_tree(cmd->u.command[1]);
    }
    else if (cmd->type == AND_COMMAND)
    {
        status = execute_tree(cmd->u.command[0]);
        if (status == 0)
            status = execute_tree(cmd->u.command[1]);
    }
    else if (cmd->type == OR_COMMAND)
    {
        status = execute_tree(cmd->u.command[0]);
        if (status != 0)
            status = execute_tree(cmd->u.command[1]);
    }
    else if (cmd->type == PIPE_COMMAND)
    {
        /* Set up pipe, spawn two children, and wait for them to finish */
        int fd[2];
        int p_left, p_right;
        
        if (pipe(fd) < 0)
            ERR("Failed to create pipe\n");

        /* Left child writes to fd[0], right reads from fd[1] */
        if ((p_left = fork()) == 0)
        {
            close(fd[0]);
            if (dup2(fd[1], STDOUT_FILENO) < 0)
                ERR("Failed to dup2(%d, %d) for redirect pipe\n", fd[1], STDOUT_FILENO);
            
            status = execute_tree(cmd->u.command[0]);
            close(fd[1]);
            _exit(status);
        }
        else if (p_left < 0)
        {
            ERR("Failed to fork() to execute pipe command\n");
        }
        if ((p_right = fork()) == 0)
        {
            close(fd[1]);
            if (dup2(fd[0], STDIN_FILENO) < 0)
                ERR("Failed to dup2(%d, %d) for redirect pipe\n", fd[0], STDIN_FILENO);
            
            status = execute_tree(cmd->u.command[1]);
            close(fd[0]);
            _exit(status);
        }
        else if (p_right < 0)
        {
            ERR("Failed to fork() to execute pipe command\n");
        }

        /* Parent can close pipe immediately. Return status is status of right command. */
        close(fd[0]);
        close(fd[1]);
        if (waitpid(p_left, &status, 0) < 0)
            ERR("Failed to waitpid() for left pipe child\n");
        if (waitpid(p_right, &status, 0) < 0)
            ERR("Failed to waitpid() for right pipe child\n");
        status = WEXITSTATUS(status);
    }
    else        /* SUBSHELL_COMMAND: spawn new process (with redirects) to execute command */
    {
        int p;
        int fd_in = -1, fd_out = -1;

        if ((p = fork()) == 0)
        {
            if (cmd->input != NULL)
                fd_in = redirect(STDIN_FILENO, cmd->input, FLAGS_READ);
            if (cmd->output != NULL)
                fd_out = redirect(STDOUT_FILENO, cmd->output, FLAGS_WRITE);

            status = execute_tree(cmd->u.subshell_command);
            if (fd_in >= 0)
                close(fd_in);
            if (fd_out >= 0)
                close(fd_out);
            _exit(status);
        }
        else if (p < 0)
        {
            ERR("Failed to fork() to execute subshell command\n");
        }

        if (waitpid(p, &status, 0)  < 0)
            ERR("Failed to waitpid() for child\n");
        status = WEXITSTATUS(status);
    }
    
    cmd->status = status;
    return status;
}