feat: subshell total support
This commit is contained in:
parent
3e42b6fd00
commit
a7065a1d9f
4 changed files with 39 additions and 6 deletions
|
|
@ -46,6 +46,9 @@ int execution(struct ast *ast, struct hash_map *vars)
|
|||
case AST_LOOP:
|
||||
res = exec_ast_loop(ast_get_loop(ast), vars);
|
||||
break;
|
||||
case AST_SUBSHELL:
|
||||
res = exec_ast_subshell(ast_get_subshell(ast), vars);
|
||||
break;
|
||||
default:
|
||||
res = 127;
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -249,6 +249,33 @@ int exec_ast_if(struct ast_if *if_node, struct hash_map *vars)
|
|||
}
|
||||
}
|
||||
|
||||
int exec_ast_subshell(struct ast_subshell *subshell_node,
|
||||
struct hash_map *vars)
|
||||
{
|
||||
pid_t pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
int res = execution(subshell_node->child, vars);
|
||||
_exit(res);
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
waitpid(pid, &status, 0);
|
||||
|
||||
if (WIFEXITED(status))
|
||||
{
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int exec_ast_list(struct ast_list *list_node, struct hash_map *vars)
|
||||
{
|
||||
struct list *cur = list_node->children;
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ int exec_ast_if(struct ast_if *if_node, struct hash_map *vars);
|
|||
int exec_ast_list(struct ast_list *list_node, struct hash_map *vars);
|
||||
int exec_ast_and_or(struct ast_and_or *ao_node, struct hash_map *vars);
|
||||
int exec_ast_loop(struct ast_loop *loop_node, struct hash_map *vars);
|
||||
int exec_ast_subshell(struct ast_subshell *subshell_node,
|
||||
struct hash_map *vars);
|
||||
|
||||
void unset_all_redir(struct list *redir_list);
|
||||
#endif // EXECUTION_HELPERS_H
|
||||
|
|
|
|||
|
|
@ -11,8 +11,7 @@ void ast_free(struct ast **node)
|
|||
{
|
||||
if (node == NULL || *node == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
fprintf(stderr,
|
||||
"WARNING: Internal error: failed to free AST node (NULL argument)");
|
||||
return;
|
||||
}
|
||||
|
|
@ -52,14 +51,16 @@ void ast_free(struct ast **node)
|
|||
case AST_FUNCTION:
|
||||
ast_free_function(ast_get_function(*node));
|
||||
break;
|
||||
case AST_SUBSHELL:
|
||||
ast_free_subshell(ast_get_subshell(*node));
|
||||
break;
|
||||
case AST_VOID:
|
||||
case AST_END:
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"WARNING: Internal error: failed to free an AST node (Unknown "
|
||||
"type)");
|
||||
fprintf(stderr, "WARNING: Internal error:"
|
||||
" failed to free an AST node (Unknown type)");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -170,4 +171,4 @@ void ast_print_dot(struct ast *ast)
|
|||
fprintf(dot_pipe, "}\n");
|
||||
pclose(dot_pipe);
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue