2026-01-24 17:35:06 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2026-01-10 17:15:16 +01:00
|
|
|
#include "execution.h"
|
|
|
|
|
|
2026-01-17 19:11:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2026-01-10 17:15:16 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2026-01-17 19:11:55 +01:00
|
|
|
#include <string.h>
|
2026-01-10 17:15:16 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2026-01-22 12:09:18 +00:00
|
|
|
#include "../expansion/expansion.h"
|
2026-01-10 17:15:16 +01:00
|
|
|
#include "../utils/ast/ast.h"
|
2026-01-22 12:09:18 +00:00
|
|
|
#include "../utils/hash_map/hash_map.h"
|
2026-01-10 17:15:16 +01:00
|
|
|
|
2026-01-24 17:35:06 +01:00
|
|
|
// Refactored: delegates to helpers in execution_helpers.c
|
|
|
|
|
#include "execution_helpers.h"
|
2026-01-10 17:15:16 +01:00
|
|
|
|
2026-01-22 12:09:18 +00:00
|
|
|
int execution(struct ast *ast, struct hash_map *vars)
|
2026-01-10 17:15:16 +01:00
|
|
|
{
|
|
|
|
|
if (!ast)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
switch (ast->type)
|
|
|
|
|
{
|
2026-01-22 13:10:23 +00:00
|
|
|
case AST_VOID:
|
2026-01-24 17:35:06 +01:00
|
|
|
case AST_END:
|
2026-01-10 17:15:16 +01:00
|
|
|
return 0;
|
|
|
|
|
case AST_CMD: {
|
2026-01-15 18:49:42 +01:00
|
|
|
struct ast_command *command = ast_get_command(ast);
|
2026-01-22 12:09:18 +00:00
|
|
|
if (!expand(command, vars))
|
|
|
|
|
fprintf(stderr, "Error: Variable expansion failed\n");
|
2026-01-29 18:18:04 +01:00
|
|
|
|
2026-01-24 17:35:06 +01:00
|
|
|
return exec_ast_command(command, vars);
|
|
|
|
|
}
|
|
|
|
|
case AST_IF:
|
|
|
|
|
return exec_ast_if(ast_get_if(ast), vars);
|
|
|
|
|
case AST_LIST:
|
|
|
|
|
return exec_ast_list(ast_get_list(ast), vars);
|
|
|
|
|
case AST_AND_OR:
|
|
|
|
|
return exec_ast_and_or(ast_get_and_or(ast), vars);
|
|
|
|
|
default:
|
2026-01-17 17:33:22 +01:00
|
|
|
return 127;
|
2026-01-10 17:15:16 +01:00
|
|
|
}
|
2026-01-24 14:52:37 +01:00
|
|
|
}
|