fix(execution): reimplemented the builtins
This commit is contained in:
parent
8a5c589742
commit
a8b4e5d6df
3 changed files with 91 additions and 28 deletions
|
|
@ -32,7 +32,7 @@ run_cmd "Running autoreconf" autoreconf --force --verbose --install
|
||||||
if [[ "$(uname)" == "Darwin" ]]; then
|
if [[ "$(uname)" == "Darwin" ]]; then
|
||||||
run_cmd "Configuring for MacOS" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'
|
run_cmd "Configuring for MacOS" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'
|
||||||
else
|
else
|
||||||
run_cmd "Configuring for Linux" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'
|
run_cmd "Configuring for Linux" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
run_cmd "Cleaning build" make clean
|
run_cmd "Cleaning build" make clean
|
||||||
|
|
|
||||||
|
|
@ -14,32 +14,7 @@
|
||||||
#include "../utils/ast/ast.h"
|
#include "../utils/ast/ast.h"
|
||||||
#include "../utils/hash_map/hash_map.h"
|
#include "../utils/hash_map/hash_map.h"
|
||||||
|
|
||||||
// --- Helpers ---
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief converts a linked list of command arguments to an argv array. Don't
|
|
||||||
* forget to free the result
|
|
||||||
*
|
|
||||||
* @param command_list Linked list of command arguments
|
|
||||||
* @return char** Array of command arguments suitable for execvp. Terminated by
|
|
||||||
* NULL
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Tries to execute a builtin command if the command matches a builtin
|
|
||||||
*
|
|
||||||
* @param argv Array of command arguments
|
|
||||||
* @return int Exit status of the builtin command, or -1 if not a builtin
|
|
||||||
*/
|
|
||||||
|
|
||||||
// --- Execution Core ---
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Executes a command represented by an ast_command structure
|
|
||||||
*
|
|
||||||
* @param command The command to execute
|
|
||||||
* @return int The exit status of the command
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Refactored: delegates to helpers in execution_helpers.c
|
// Refactored: delegates to helpers in execution_helpers.c
|
||||||
#include "execution_helpers.h"
|
#include "execution_helpers.h"
|
||||||
|
|
|
||||||
|
|
@ -208,9 +208,97 @@ int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dummy try_builtin for linking
|
// --- Builtins ---
|
||||||
static int try_builtin(char **argv)
|
|
||||||
|
static int builtin_echo(char **argv)
|
||||||
|
{
|
||||||
|
bool newline = true;
|
||||||
|
int i = 1;
|
||||||
|
|
||||||
|
if (argv[1] && strcmp(argv[1], "-n") == 0)
|
||||||
|
{
|
||||||
|
newline = false;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; argv[i]; i++)
|
||||||
|
{
|
||||||
|
printf("%s", argv[i]);
|
||||||
|
if (argv[i + 1])
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
if (newline)
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int builtin_true(char **argv)
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int builtin_false(char **argv)
|
||||||
|
{
|
||||||
|
(void)argv;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int builtin_exit(char **argv)
|
||||||
|
{
|
||||||
|
int exit_val = 0;
|
||||||
|
if (argv[1])
|
||||||
|
exit_val = atoi(argv[1]);
|
||||||
|
exit(exit_val);
|
||||||
|
return exit_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int builtin_cd(char **argv)
|
||||||
|
{
|
||||||
|
const char *path = argv[1];
|
||||||
|
if (!path)
|
||||||
|
{
|
||||||
|
path = getenv("HOME");
|
||||||
|
if (!path)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "cd: HOME not set\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (chdir(path) != 0)
|
||||||
|
{
|
||||||
|
perror("cd");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Tries to execute a builtin command if the command matches a builtin
|
||||||
|
*
|
||||||
|
* @param argv Array of command arguments
|
||||||
|
* @return int Exit status of the builtin command, or -1 if not a builtin
|
||||||
|
*/
|
||||||
|
static int try_builtin(char **argv)
|
||||||
|
{
|
||||||
|
if (!argv || !argv[0])
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (strcmp(argv[0], "echo") == 0)
|
||||||
|
return builtin_echo(argv);
|
||||||
|
if (strcmp(argv[0], "true") == 0)
|
||||||
|
return builtin_true(argv);
|
||||||
|
if (strcmp(argv[0], "false") == 0)
|
||||||
|
return builtin_false(argv);
|
||||||
|
if (strcmp(argv[0], "exit") == 0)
|
||||||
|
return builtin_exit(argv);
|
||||||
|
if (strcmp(argv[0], "cd") == 0)
|
||||||
|
return builtin_cd(argv);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue