Compare commits
2 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adea32552d | ||
|
|
ac851fa895 |
61 changed files with 639 additions and 1140 deletions
47
README.md
47
README.md
|
|
@ -1,12 +1,11 @@
|
|||
# 42sh - A POSIX shell with a bad name
|
||||
|
||||
42sh is a project aiming to implement a POSIX-compliant shell written in C with only the standard library.
|
||||
Source de is fully documented with the doxygen format so you can easily understand how the project works by exploring it.
|
||||
|
||||
> **Note** This is a school project, therefore it probably won't interest you if you are looking for something useful.
|
||||
42sh is a shcool project aiming to implement a POSIX compliant shell in C.
|
||||
|
||||
## Getting started
|
||||
|
||||
TODO
|
||||
|
||||
### Build
|
||||
run this command:
|
||||
`autoreconf --force --verbose --install`
|
||||
|
|
@ -17,43 +16,27 @@ run this command:
|
|||
then:
|
||||
`make`
|
||||
|
||||
#### Build with ASan
|
||||
#### asan
|
||||
run this command:
|
||||
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'`
|
||||
|
||||
or for MacOS (Jean Here):
|
||||
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'`
|
||||
|
||||
|
||||
then:
|
||||
`make check`
|
||||
|
||||
## Project status
|
||||
|
||||
### Implemented features
|
||||
|
||||
* **Command Execution:** `$PATH` search and binary execution (via `fork` and `execvp`) with error return code handling.
|
||||
* **Built-ins:** Native implementation of `echo`, `cd`, `exit`, `export`, `unset`, `set`, `.`, `true`, `false`, as well as loop management with `break` and `continue`.
|
||||
* **Control Structures:** * Conditions: `if / then / elif / else / fi`.
|
||||
* Loops: `while`, `until` and `for`.
|
||||
* **Logical Operators:** Command chaining with `&&`, `||` and negation with `!`.
|
||||
* **Pipelines and Redirections:** * Full management of pipes `|` to connect the output of one process to the input of another.
|
||||
* Single and multiple redirections: `>`, `<`, `>>`, `>&`, `<&`, `<>`.
|
||||
* **Variables Management:** Assignment, variable expansion, and special variables handling like `$?` (return code of the last command).
|
||||
* **Command Grouping:** Execution blocks `{ ... }` and subshells creation `( ... )`.
|
||||
* **Quoting:** Support for weak (`"`) and strong (`'`) quoting for special characters escaping.
|
||||
|
||||
## Architecture
|
||||
|
||||
The shell operates on a classic compilation/interpretation pipeline:
|
||||
|
||||
1. **Lexer (Lexical Analysis):** Reads standard input (or script) character by character and generates a stream of "Tokens" (Words, Operators, Redirections).
|
||||
2. **Parser (Syntax Analysis):** Syntax analyzer that transforms the token stream into a complex Abstract Syntax Tree (AST). This module strictly manages the nesting of control structures and enforces the rigid grammar of the Shell Command Language.
|
||||
3. **Execution (AST Traversal):** The tree is traversed recursively. Redirections modify file descriptors (`dup2`), child processes are created (`fork`), and commands are executed.
|
||||
|
||||
|
||||
## Authors
|
||||
|
||||
- Guillem George
|
||||
- Matteo Flebus
|
||||
- Jean Herail
|
||||
- William Valenduc
|
||||
- Guillem George
|
||||
|
||||
## Project status
|
||||
|
||||
WIP
|
||||
|
||||
## TODO
|
||||
|
||||
# Autotools
|
||||
implement functions in all .c files to see if everything compiles.
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@ bin_PROGRAMS = 42sh
|
|||
parser/libparser.a \
|
||||
lexer/liblexer.a \
|
||||
io_backend/libio_backend.a \
|
||||
utils/libutils.a \
|
||||
execution/libexecution.a \
|
||||
expansion/libexpansion.a
|
||||
expansion/libexpansion.a \
|
||||
utils/libutils.a
|
||||
|
||||
# ================ TESTS ================
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
#include "../expansion/expansion.h"
|
||||
#include "../utils/hash_map/hash_map.h"
|
||||
#include "../utils/vars/vars.h"
|
||||
|
||||
// Refactored: delegates to helpers in execution_helpers.c
|
||||
#include "execution_helpers.h"
|
||||
|
|
@ -19,55 +18,27 @@ int execution(struct ast *ast, struct hash_map *vars)
|
|||
if (!ast)
|
||||
return 0;
|
||||
|
||||
int res;
|
||||
switch (ast->type)
|
||||
{
|
||||
case AST_VOID:
|
||||
case AST_END:
|
||||
res = 0;
|
||||
break;
|
||||
return 0;
|
||||
case AST_CMD: {
|
||||
struct ast_command *command = ast_get_command(ast);
|
||||
if (!expand(command, vars))
|
||||
fprintf(stderr, "Error: Variable expansion failed\n");
|
||||
|
||||
res = exec_ast_command(command, vars);
|
||||
break;
|
||||
return exec_ast_command(command, vars);
|
||||
}
|
||||
case AST_IF:
|
||||
res = exec_ast_if(ast_get_if(ast), vars);
|
||||
break;
|
||||
return exec_ast_if(ast_get_if(ast), vars);
|
||||
case AST_LIST:
|
||||
res = exec_ast_list(ast_get_list(ast), vars);
|
||||
break;
|
||||
return exec_ast_list(ast_get_list(ast), vars);
|
||||
case AST_AND_OR:
|
||||
res = exec_ast_and_or(ast_get_and_or(ast), vars);
|
||||
break;
|
||||
return exec_ast_and_or(ast_get_and_or(ast), 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;
|
||||
return exec_ast_loop(ast_get_loop(ast), vars);
|
||||
default:
|
||||
res = 127;
|
||||
break;
|
||||
}
|
||||
|
||||
if (res == EXEC_SIGNAL_EXIT)
|
||||
{
|
||||
char *exit_val_str = get_var(vars, "EXIT_VALUE");
|
||||
if (exit_val_str == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"Internal error: could not retrieve return value from exit\n");
|
||||
return 2;
|
||||
}
|
||||
return atoi(exit_val_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
return res;
|
||||
return 127;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
#include "execution_helpers.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -14,10 +13,11 @@
|
|||
#include "../utils/vars/vars.h"
|
||||
#include "execution.h"
|
||||
|
||||
// === Static functions
|
||||
|
||||
static int open_redir_file(const struct ast_redir *redir, int *flags, int *mode)
|
||||
{
|
||||
if (redir == NULL || flags == NULL || mode == NULL)
|
||||
return -1;
|
||||
|
||||
*mode = 0644;
|
||||
if (redir->type == AST_REDIR_TYPE_GREAT
|
||||
|| redir->type == AST_REDIR_TYPE_CLOBBER)
|
||||
|
|
@ -44,6 +44,9 @@ static int set_all_redir(struct list *redir_list)
|
|||
{
|
||||
struct ast *redir_node = (struct ast *)redir_list->data;
|
||||
struct ast_redir *redir = ast_get_redir(redir_node);
|
||||
if (redir == NULL)
|
||||
return -1; // TODO log error and free
|
||||
|
||||
int target_fd;
|
||||
if (redir->io_number != -1)
|
||||
{
|
||||
|
|
@ -123,60 +126,26 @@ static char **list_to_argv(struct list *command_list)
|
|||
return argv;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief parses string and returns the represented (unsigned) number
|
||||
* @return the number contained by the string or -1 if number is invalid
|
||||
*/
|
||||
static int atou(char *str)
|
||||
{
|
||||
if (str == NULL || *str == '\0')
|
||||
return -1;
|
||||
|
||||
int result = 0;
|
||||
size_t i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] < '0' || str[i] > '9')
|
||||
return -1;
|
||||
|
||||
result *= 10;
|
||||
result += str[i] - '0';
|
||||
i++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int try_builtin(char **argv, struct hash_map *vars);
|
||||
|
||||
static int builtin_break(char **argv);
|
||||
static int builtin_continue(char **argv);
|
||||
static int builtin_unset(char **argv, struct hash_map *vars);
|
||||
|
||||
static int exec_assignment(struct list *assignment_list, struct hash_map *vars)
|
||||
{
|
||||
while (assignment_list != NULL)
|
||||
{
|
||||
if (!ast_is_assignment(assignment_list->data))
|
||||
struct ast_assignment *assignment =
|
||||
ast_get_assignment(assignment_list->data);
|
||||
if (assignment == NULL)
|
||||
{
|
||||
fprintf(stderr, "list of assignements contains something else");
|
||||
return 1;
|
||||
}
|
||||
struct ast_assignment *assignment =
|
||||
ast_get_assignment(assignment_list->data);
|
||||
|
||||
if (assignment->global)
|
||||
{
|
||||
setenv(assignment->name, assignment->value, 1);
|
||||
}
|
||||
set_var_copy(vars, assignment->name, assignment->value);
|
||||
assignment_list = assignment_list->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// === Functions
|
||||
|
||||
int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||
{
|
||||
exec_assignment(command->assignments, vars);
|
||||
|
|
@ -238,46 +207,10 @@ int exec_ast_if(struct ast_if *if_node, struct hash_map *vars)
|
|||
if (if_node == NULL)
|
||||
return 2;
|
||||
int cond = execution(if_node->condition, vars);
|
||||
if (cond == EXEC_SIGNAL_BREAK || cond == EXEC_SIGNAL_CONTINUE
|
||||
|| cond == EXEC_SIGNAL_EXIT)
|
||||
return cond;
|
||||
if (cond == 0)
|
||||
{
|
||||
int r = execution(if_node->then_clause, vars);
|
||||
return r;
|
||||
}
|
||||
return execution(if_node->then_clause, vars);
|
||||
else
|
||||
{
|
||||
int r = execution(if_node->else_clause, vars);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
return execution(if_node->else_clause, vars);
|
||||
}
|
||||
|
||||
int exec_ast_list(struct ast_list *list_node, struct hash_map *vars)
|
||||
|
|
@ -287,15 +220,8 @@ int exec_ast_list(struct ast_list *list_node, struct hash_map *vars)
|
|||
while (cur)
|
||||
{
|
||||
struct ast *child = (struct ast *)cur->data;
|
||||
if (!ast_is_void(child))
|
||||
{
|
||||
int child_ret = execution(child, vars);
|
||||
if (child_ret == EXEC_SIGNAL_BREAK
|
||||
|| child_ret == EXEC_SIGNAL_CONTINUE
|
||||
|| child_ret == EXEC_SIGNAL_EXIT)
|
||||
return child_ret;
|
||||
ret = child_ret;
|
||||
}
|
||||
if (child->type != AST_VOID)
|
||||
ret = execution(child, vars);
|
||||
cur = cur->next;
|
||||
}
|
||||
return ret;
|
||||
|
|
@ -304,25 +230,16 @@ 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 left_ret = execution(ao_node->left, vars);
|
||||
if (left_ret == EXEC_SIGNAL_BREAK || left_ret == EXEC_SIGNAL_CONTINUE
|
||||
|| left_ret == EXEC_SIGNAL_EXIT)
|
||||
return left_ret;
|
||||
if (ao_node->type == AST_AND_OR_TYPE_AND)
|
||||
{
|
||||
if (left_ret == 0)
|
||||
{
|
||||
int right_ret = execution(ao_node->right, vars);
|
||||
return right_ret;
|
||||
}
|
||||
return execution(ao_node->right, vars);
|
||||
return left_ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (left_ret != 0)
|
||||
{
|
||||
int right_ret = execution(ao_node->right, vars);
|
||||
return right_ret;
|
||||
}
|
||||
return execution(ao_node->right, vars);
|
||||
return left_ret;
|
||||
}
|
||||
}
|
||||
|
|
@ -333,6 +250,9 @@ void unset_all_redir(struct list *redir_list)
|
|||
{
|
||||
struct ast *redir_node = (struct ast *)redir_list->data;
|
||||
struct ast_redir *redir = ast_get_redir(redir_node);
|
||||
if (redir == NULL)
|
||||
return; // TODO log error and free
|
||||
|
||||
int target_fd;
|
||||
if (redir->io_number != -1)
|
||||
{
|
||||
|
|
@ -367,16 +287,6 @@ int exec_ast_loop(struct ast_loop *loop_node, struct hash_map *vars)
|
|||
while (execution(loop_node->condition, vars) == 0)
|
||||
{
|
||||
res = execution(loop_node->body, vars);
|
||||
if (res == EXEC_SIGNAL_BREAK)
|
||||
{
|
||||
res = 0;
|
||||
break;
|
||||
}
|
||||
else if (res == EXEC_SIGNAL_CONTINUE)
|
||||
{
|
||||
res = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
|
|
@ -384,95 +294,20 @@ int exec_ast_loop(struct ast_loop *loop_node, struct hash_map *vars)
|
|||
|
||||
// --- Builtins ---
|
||||
|
||||
static void print_with_escapes(const char *str)
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
if (*str == '\\')
|
||||
{
|
||||
str++;
|
||||
if (*str == 'n')
|
||||
putchar('\n');
|
||||
else if (*str == 't')
|
||||
putchar('\t');
|
||||
else if (*str == '\\')
|
||||
putchar('\\');
|
||||
else if (*str == 'a')
|
||||
putchar('\a');
|
||||
else if (*str == 'b')
|
||||
putchar('\b');
|
||||
else if (*str == 'f')
|
||||
putchar('\f');
|
||||
else if (*str == 'r')
|
||||
putchar('\r');
|
||||
else if (*str == 'v')
|
||||
putchar('\v');
|
||||
else if (*str == 'c')
|
||||
return; // stop printing
|
||||
else
|
||||
{
|
||||
// unrecognized escape, print "\"" and the char
|
||||
putchar('\\');
|
||||
if (*str)
|
||||
putchar(*str);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
putchar(*str);
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
static int builtin_echo(char **argv)
|
||||
{
|
||||
bool newline = true;
|
||||
bool interpret_escapes = false;
|
||||
int i = 1;
|
||||
|
||||
// Parse options
|
||||
while (argv[i] && argv[i][0] == '-')
|
||||
if (argv[1] && strcmp(argv[1], "-n") == 0)
|
||||
{
|
||||
char *opt = argv[i] + 1; // skip "-"
|
||||
bool valid_option = false;
|
||||
while (*opt)
|
||||
{
|
||||
if (*opt == 'n')
|
||||
{
|
||||
newline = false;
|
||||
valid_option = true;
|
||||
}
|
||||
else if (*opt == 'e')
|
||||
{
|
||||
interpret_escapes = true;
|
||||
valid_option = true;
|
||||
}
|
||||
else if (*opt == 'E')
|
||||
{
|
||||
interpret_escapes = false;
|
||||
valid_option = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// invalid option so euh treat as regular argument
|
||||
valid_option = false;
|
||||
break;
|
||||
}
|
||||
opt++;
|
||||
}
|
||||
if (!valid_option)
|
||||
break; // stop parsing options
|
||||
newline = false;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Print arguments
|
||||
for (; argv[i]; i++)
|
||||
{
|
||||
if (interpret_escapes)
|
||||
print_with_escapes(argv[i]);
|
||||
else
|
||||
printf("%s", argv[i]);
|
||||
printf("%s", argv[i]);
|
||||
if (argv[i + 1])
|
||||
printf(" ");
|
||||
}
|
||||
|
|
@ -483,18 +318,6 @@ static int builtin_echo(char **argv)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int builtin_break(char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
return EXEC_SIGNAL_BREAK;
|
||||
}
|
||||
|
||||
static int builtin_continue(char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
return EXEC_SIGNAL_CONTINUE;
|
||||
}
|
||||
|
||||
static int builtin_true(char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
|
|
@ -507,21 +330,13 @@ static int builtin_false(char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int builtin_exit(char **argv, struct hash_map *vars)
|
||||
static int builtin_exit(char **argv)
|
||||
{
|
||||
int exit_val = 0;
|
||||
if (argv[1])
|
||||
{
|
||||
exit_val = atou(argv[1]);
|
||||
if (exit_val == -1)
|
||||
{
|
||||
fprintf(stderr, "exit: Illegal number %s\n", argv[1]);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
set_var_int(vars, "EXIT_VALUE", exit_val);
|
||||
return EXEC_SIGNAL_EXIT;
|
||||
exit_val = atoi(argv[1]);
|
||||
exit(exit_val);
|
||||
return exit_val;
|
||||
}
|
||||
|
||||
static int builtin_cd(char **argv, struct hash_map *vars)
|
||||
|
|
@ -548,23 +363,6 @@ static int builtin_cd(char **argv, struct hash_map *vars)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int builtin_unset(char **argv, struct hash_map *vars)
|
||||
{
|
||||
if (!argv)
|
||||
return 0;
|
||||
for (int i = 1; argv[i]; i++)
|
||||
{
|
||||
const char *name = argv[i];
|
||||
if (name == NULL || name[0] == '\0')
|
||||
continue;
|
||||
// remove from shell variables
|
||||
hash_map_remove(vars, name);
|
||||
// remove from environment variables
|
||||
unsetenv(name);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tries to execute a builtin command if the command matches a builtin
|
||||
*
|
||||
|
|
@ -578,18 +376,12 @@ static int try_builtin(char **argv, struct hash_map *vars)
|
|||
|
||||
if (strcmp(argv[0], "echo") == 0)
|
||||
return builtin_echo(argv);
|
||||
if (strcmp(argv[0], "unset") == 0)
|
||||
return builtin_unset(argv, vars);
|
||||
if (strcmp(argv[0], "true") == 0)
|
||||
return builtin_true(argv);
|
||||
if (strcmp(argv[0], "false") == 0)
|
||||
return builtin_false(argv);
|
||||
if (strcmp(argv[0], "break") == 0)
|
||||
return builtin_break(argv);
|
||||
if (strcmp(argv[0], "continue") == 0)
|
||||
return builtin_continue(argv);
|
||||
if (strcmp(argv[0], "exit") == 0)
|
||||
return builtin_exit(argv, vars);
|
||||
return builtin_exit(argv);
|
||||
if (strcmp(argv[0], "cd") == 0)
|
||||
return builtin_cd(argv, vars);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,18 +4,11 @@
|
|||
#include "../utils/ast/ast.h"
|
||||
#include "../utils/hash_map/hash_map.h"
|
||||
|
||||
// Special execution signals used internally to implement loop control
|
||||
#define EXEC_SIGNAL_CONTINUE (-2)
|
||||
#define EXEC_SIGNAL_BREAK (-3)
|
||||
#define EXEC_SIGNAL_EXIT (-4)
|
||||
|
||||
int exec_ast_command(struct ast_command *command, struct hash_map *vars);
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "expansion.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
|
@ -117,40 +115,6 @@ static bool expand_var(char **str, size_t pos, const struct hash_map *vars)
|
|||
return false;
|
||||
}
|
||||
|
||||
size_t parse_subshell_str(char *str, char **res)
|
||||
{
|
||||
size_t i = 1; // skip the '('
|
||||
int paren_count = 1;
|
||||
|
||||
if (str[i] == ')')
|
||||
{
|
||||
// empty subshell
|
||||
*res = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] == '(')
|
||||
paren_count++;
|
||||
else if (str[i] == ')')
|
||||
{
|
||||
paren_count--;
|
||||
|
||||
if (paren_count == 0)
|
||||
{
|
||||
*res = strndup(str + 1, i - 1);
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// error: parenthesis not closed
|
||||
*res = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool expand(struct ast_command *command, const struct hash_map *vars)
|
||||
{
|
||||
if (command == NULL)
|
||||
|
|
@ -158,42 +122,34 @@ bool expand(struct ast_command *command, const struct hash_map *vars)
|
|||
|
||||
char *str;
|
||||
size_t len;
|
||||
enum quote_state quotes;
|
||||
bool in_quotes;
|
||||
struct list *l = command->command;
|
||||
|
||||
while (l != NULL)
|
||||
{
|
||||
quotes = NO_QUOTE;
|
||||
in_quotes = false;
|
||||
str = (char *)l->data;
|
||||
len = strlen(str);
|
||||
|
||||
for (size_t i = 0; str[i] != 0; i++)
|
||||
{
|
||||
if (str[i] == '\'' || str[i] == '\"')
|
||||
if (str[i] == '\'')
|
||||
{
|
||||
if (quotes == NO_QUOTE)
|
||||
{
|
||||
quotes = (str[i] == '\'') ? SINGLE_QUOTE : DOUBLE_QUOTE;
|
||||
}
|
||||
else if ((quotes == SINGLE_QUOTE && str[i] == '\'')
|
||||
|| (quotes == DOUBLE_QUOTE && str[i] == '\"'))
|
||||
{
|
||||
quotes = NO_QUOTE;
|
||||
}
|
||||
else
|
||||
{
|
||||
// inside the other quote type, do nothing
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove quote
|
||||
// remove single quote
|
||||
in_quotes = !in_quotes;
|
||||
memmove(str + i, str + i + 1, strlen(str + i + 1) + 1);
|
||||
i--;
|
||||
}
|
||||
else if (quotes == SINGLE_QUOTE)
|
||||
else if (in_quotes)
|
||||
{
|
||||
continue; // do nothing
|
||||
}
|
||||
else if (str[i] == '\"')
|
||||
{
|
||||
// remove double quote
|
||||
memmove(str + i, str + i + 1, strlen(str + i + 1) + 1);
|
||||
i--;
|
||||
}
|
||||
else if (str[i] == '$' && str[i + 1] != 0 && !isspace(str[i + 1]))
|
||||
{
|
||||
// variable expansion
|
||||
|
|
@ -205,20 +161,21 @@ bool expand(struct ast_command *command, const struct hash_map *vars)
|
|||
}
|
||||
}
|
||||
|
||||
// if (quotes != NO_QUOTE)
|
||||
// {
|
||||
// // error: quote not closed
|
||||
// fprintf(stderr, "Error: quote not closed in string: %s\n", str);
|
||||
// return false;
|
||||
// }
|
||||
if (in_quotes)
|
||||
{
|
||||
// error: quote not closed
|
||||
fprintf(stderr, "Error: quote not closed in string: %s\n", str);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (len != strlen(str))
|
||||
{
|
||||
char *new_str = realloc(str, strlen(str) + 1);
|
||||
if (new_str == NULL)
|
||||
{
|
||||
// error: realloc fail
|
||||
return false;
|
||||
|
||||
}
|
||||
l->data = new_str;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,13 +7,6 @@
|
|||
#include "../utils/ast/ast.h"
|
||||
#include "../utils/hash_map/hash_map.h"
|
||||
|
||||
enum quote_state
|
||||
{
|
||||
NO_QUOTE,
|
||||
SINGLE_QUOTE,
|
||||
DOUBLE_QUOTE
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse a variable from a string starting with '$'.
|
||||
* @param str The input string starting with '$'. It must start with '$'.
|
||||
|
|
@ -23,15 +16,6 @@ enum quote_state
|
|||
*/
|
||||
size_t parse_var_name(char *str, char **res);
|
||||
|
||||
/**
|
||||
* Parse a subshell string enclosed in parentheses.
|
||||
* @param str The input string starting with '('. It must start with '('.
|
||||
* @param res Pointer to a char pointer that will be set to the extracted
|
||||
* subshell string.
|
||||
* @return The number of characters processed in the input string.
|
||||
*/
|
||||
size_t parse_subshell_str(char *str, char **res);
|
||||
|
||||
/**
|
||||
* Expand variables in an AST command using the provided variable map.
|
||||
* @param command The AST command to expand.
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
|||
tok->type = TOKEN_FOR;
|
||||
else if (strncmp(begin, "while", size) == 0 && size == 5)
|
||||
tok->type = TOKEN_WHILE;
|
||||
else if (strncmp(begin, "until", size) == 0 && size == 5)
|
||||
else if (strncmp(begin, "until", size) == 0 && size == 4)
|
||||
tok->type = TOKEN_UNTIL;
|
||||
else if (strncmp(begin, "do", size) == 0 && size == 2)
|
||||
tok->type = TOKEN_DO;
|
||||
|
|
@ -257,17 +257,16 @@ struct token *new_token(char *begin, ssize_t size, struct token_info *info)
|
|||
return tok;
|
||||
}
|
||||
|
||||
void destroy_lexer_context(struct lexer_context *ctx)
|
||||
void destroy_lexer_context(struct lexer_context **ctx)
|
||||
{
|
||||
struct token *prev = ctx->previous_token;
|
||||
struct token *cur = ctx->current_token;
|
||||
if (ctx == NULL)
|
||||
if (ctx == NULL || *ctx == NULL)
|
||||
return;
|
||||
if (prev != NULL)
|
||||
free_token(&prev);
|
||||
if (cur != NULL)
|
||||
free_token(&cur);
|
||||
free(ctx);
|
||||
if ((*ctx)->previous_token != NULL)
|
||||
free((*ctx)->previous_token);
|
||||
if ((*ctx)->current_token != NULL)
|
||||
free((*ctx)->current_token);
|
||||
free(*ctx);
|
||||
*ctx = NULL;
|
||||
}
|
||||
|
||||
void free_token(struct token **tok)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ struct lexer_context
|
|||
|
||||
/* @brief: frees all fields of ctx and sets ctx to NULL.
|
||||
*/
|
||||
void destroy_lexer_context(struct lexer_context *ctx);
|
||||
void destroy_lexer_context(struct lexer_context **ctx);
|
||||
|
||||
enum lexing_mode
|
||||
{
|
||||
|
|
|
|||
86
src/main.c
86
src/main.c
|
|
@ -7,9 +7,74 @@
|
|||
#include "lexer/lexer.h"
|
||||
#include "parser/parser.h"
|
||||
#include "utils/args/args.h"
|
||||
#include "utils/main_loop/main_loop.h"
|
||||
#include "utils/vars/vars.h"
|
||||
|
||||
// === Error codes
|
||||
|
||||
#define SUCCESS 0
|
||||
#define ERR_INPUT_PROCESSING 2
|
||||
#define ERR_MALLOC 3
|
||||
#define ERR_GENERIC 4
|
||||
|
||||
// === Functions
|
||||
|
||||
/* @brief: frees the hash map.
|
||||
* @return: always ERR_INPUT_PROCESSING.
|
||||
*/
|
||||
static int err_input(struct hash_map **vars)
|
||||
{
|
||||
hash_map_free(vars);
|
||||
return ERR_INPUT_PROCESSING;
|
||||
}
|
||||
|
||||
static int main_loop(struct lexer_context *ctx, struct args_options *options,
|
||||
struct hash_map *vars)
|
||||
{
|
||||
int return_code = SUCCESS;
|
||||
// init parser
|
||||
if (!parser_init())
|
||||
{
|
||||
perror("parser initialization failed.");
|
||||
}
|
||||
|
||||
// Retrieve and build first AST
|
||||
struct ast *command_ast = get_ast(ctx);
|
||||
|
||||
if (options->pretty_print)
|
||||
{
|
||||
ast_print_dot(command_ast);
|
||||
}
|
||||
|
||||
// Main parse-execute loop
|
||||
while (command_ast != NULL && command_ast->type != AST_END)
|
||||
{
|
||||
if (command_ast->type != AST_VOID)
|
||||
{
|
||||
// Execute AST
|
||||
return_code = execution(command_ast, vars);
|
||||
|
||||
// set $? variable
|
||||
set_var_int(vars, "?", return_code);
|
||||
}
|
||||
|
||||
ast_free(&command_ast);
|
||||
|
||||
// Retrieve and build next AST
|
||||
command_ast = get_ast(ctx);
|
||||
}
|
||||
|
||||
if (command_ast == NULL)
|
||||
return err_input(&vars);
|
||||
|
||||
// === free
|
||||
|
||||
ast_free(&command_ast);
|
||||
parser_close();
|
||||
hash_map_free(&vars);
|
||||
|
||||
return return_code;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -25,7 +90,7 @@ int main(int argc, char **argv)
|
|||
if (return_code != 0)
|
||||
{
|
||||
print_usage(stderr, argv[0]);
|
||||
return err_input(&vars, NULL);
|
||||
return err_input(&vars);
|
||||
}
|
||||
// args_print(&options);
|
||||
|
||||
|
|
@ -40,7 +105,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
fprintf(stderr,
|
||||
"Error: Failed to configure IO Backend from arguments\n");
|
||||
return err_input(&vars, NULL);
|
||||
return err_input(&vars);
|
||||
}
|
||||
|
||||
// Init IO Backend (with the context struct)
|
||||
|
|
@ -50,22 +115,13 @@ int main(int argc, char **argv)
|
|||
fprintf(stderr,
|
||||
"Error: IO Backend initialization failed with code %d\n",
|
||||
return_code);
|
||||
return err_input(&vars, NULL);
|
||||
return err_input(&vars);
|
||||
}
|
||||
|
||||
// init lexer context
|
||||
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
|
||||
struct lexer_context ctx = { 0 };
|
||||
|
||||
// init parser
|
||||
if (!parser_init())
|
||||
{
|
||||
perror("parser initialization failed.");
|
||||
return err_input(&vars, ctx);
|
||||
}
|
||||
|
||||
return_code = main_loop(ctx, vars);
|
||||
|
||||
parser_close();
|
||||
return_code = main_loop(&ctx, &options, vars);
|
||||
|
||||
return return_code;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -149,55 +149,12 @@ struct ast *parse_prefix(struct lexer_context *ctx)
|
|||
}
|
||||
}
|
||||
|
||||
// TODO NOT IMPLEMENTED
|
||||
struct ast *parse_funcdec(struct lexer_context *ctx)
|
||||
{
|
||||
struct token *token = PEEK_TOKEN();
|
||||
struct ast *value = NULL;
|
||||
char *func_name = NULL;
|
||||
|
||||
if (token->type != TOKEN_WORD)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// word -> func name
|
||||
POP_TOKEN();
|
||||
func_name = strdup(token->data);
|
||||
|
||||
// (
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type != TOKEN_LEFT_PAREN)
|
||||
{
|
||||
free(func_name);
|
||||
return NULL;
|
||||
}
|
||||
POP_TOKEN();
|
||||
|
||||
// )
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type != TOKEN_RIGHT_PAREN)
|
||||
{
|
||||
free(func_name);
|
||||
return NULL;
|
||||
}
|
||||
POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
|
||||
// { \n }
|
||||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
// shell_command -> value
|
||||
value = parse_shell_command(ctx);
|
||||
if (value == NULL)
|
||||
{
|
||||
free(func_name);
|
||||
return NULL;
|
||||
}
|
||||
return ast_create_function(func_name, value);
|
||||
(void)ctx;
|
||||
perror("Error: usage of a not implemented function (parse_funcdec)");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *parse_for(struct lexer_context *ctx)
|
||||
|
|
@ -220,18 +177,18 @@ struct ast *parse_while(struct lexer_context *ctx)
|
|||
}
|
||||
POP_TOKEN();
|
||||
|
||||
return parse_loop(ctx, false);
|
||||
return parse_loop(ctx, true);
|
||||
}
|
||||
|
||||
struct ast *parse_until(struct lexer_context *ctx)
|
||||
{
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
||||
// 'until'
|
||||
// 'while'
|
||||
if (token->type != TOKEN_UNTIL)
|
||||
{
|
||||
perror(
|
||||
"Internal error: expected a TOKEN_UNTIL but got a different type");
|
||||
"Internal error: expected a TOKEN_WHILE but got a different type");
|
||||
return NULL;
|
||||
}
|
||||
POP_TOKEN();
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ struct ast *parse_prefix(struct lexer_context *ctx);
|
|||
|
||||
/*
|
||||
* @brief parses a funcdec rule
|
||||
* @warning Work in progress
|
||||
* @warning NOT IMPLEMENTED
|
||||
*
|
||||
* @code funcdec = WORD '(' ')' {'\n'} shell_command ;
|
||||
*
|
||||
|
|
@ -36,6 +36,7 @@ struct ast *parse_funcdec(struct lexer_context *ctx);
|
|||
|
||||
/*
|
||||
* @brief parses a for rule
|
||||
* @warning NOT IMPLEMENTED
|
||||
*
|
||||
* @code rule_for = 'for' WORD
|
||||
* ( [';'] | [ {'\n'} 'in' { WORD } ( ';' | '\n' ) ] )
|
||||
|
|
|
|||
|
|
@ -223,6 +223,7 @@ struct ast *parse_command(struct lexer_context *ctx)
|
|||
{
|
||||
result = parse_shell_command(ctx);
|
||||
}
|
||||
// WARNING funcdec seems to require a LL(2) parser
|
||||
else if (is_first(*token, RULE_FUNCDEC))
|
||||
{
|
||||
result = parse_funcdec(ctx);
|
||||
|
|
@ -301,7 +302,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
|||
}
|
||||
|
||||
// Get element type
|
||||
if (ast_is_word(element))
|
||||
if (element->type == AST_WORD)
|
||||
{
|
||||
// Extract word
|
||||
struct ast_word *element_word = ast_get_word(element);
|
||||
|
|
@ -310,7 +311,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
|||
ast_free(&element);
|
||||
command_elements = list_append(command_elements, word);
|
||||
}
|
||||
else if (ast_is_redir(element))
|
||||
else if (element->type == AST_REDIR)
|
||||
{
|
||||
// append redirections to the list of redirections
|
||||
redirections = list_append(redirections, element);
|
||||
|
|
@ -372,45 +373,28 @@ struct ast *parse_shell_command(struct lexer_context *ctx)
|
|||
struct token *token = PEEK_TOKEN();
|
||||
struct ast *result = NULL;
|
||||
|
||||
// '{'
|
||||
if (token->type == TOKEN_LEFT_BRACKET)
|
||||
// Grouping
|
||||
// '(' or '{'
|
||||
if (token->type == TOKEN_LEFT_BRACKET || token->type == TOKEN_LEFT_PAREN)
|
||||
{
|
||||
POP_TOKEN();
|
||||
result = parse_compound_list(ctx);
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
|
||||
// '}'
|
||||
// ')' or '}'
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type == TOKEN_LEFT_BRACKET)
|
||||
if (token->type == TOKEN_LEFT_BRACKET
|
||||
|| token->type == TOKEN_LEFT_PAREN)
|
||||
{
|
||||
ast_free(&result);
|
||||
perror("Syntax error: bracket mismatch");
|
||||
perror("Syntax error: bracket/parenthesis mismatch");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
POP_TOKEN();
|
||||
return result;
|
||||
}
|
||||
// '('
|
||||
else if (token->type == TOKEN_LEFT_PAREN)
|
||||
{
|
||||
POP_TOKEN();
|
||||
result = parse_compound_list(ctx);
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
|
||||
// ')'
|
||||
token = PEEK_TOKEN();
|
||||
if (token->type == TOKEN_LEFT_PAREN)
|
||||
{
|
||||
ast_free(&result);
|
||||
perror("Syntax error: parenthesis mismatch");
|
||||
return NULL;
|
||||
}
|
||||
POP_TOKEN();
|
||||
return ast_create_subshell(result);
|
||||
}
|
||||
else if (is_first(*token, RULE_IF))
|
||||
{
|
||||
return parse_if_rule(ctx);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
lib_LIBRARIES = libutils.a
|
||||
|
||||
libutils_a_SOURCES = \
|
||||
lists/lists1.c \
|
||||
lists/lists2.c \
|
||||
lists/lists3.c \
|
||||
lists/lists_basic.c \
|
||||
lists/lists_advanced.c \
|
||||
lists/lists_very_advanced.c \
|
||||
hash_map/hash_map.c \
|
||||
string_utils/string_utils.c \
|
||||
ast/ast.c \
|
||||
|
|
@ -20,10 +20,7 @@ libutils_a_SOURCES = \
|
|||
ast/ast_loop.c \
|
||||
args/args.c \
|
||||
vars/vars.c \
|
||||
main_loop/main_loop.c \
|
||||
ast/ast_assignment.c \
|
||||
ast/ast_subshell.c \
|
||||
ast/ast_function.c
|
||||
ast/ast_assignment.c
|
||||
|
||||
libutils_a_CPPFLAGS = -I$(top_srcdir)/src
|
||||
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ int args_handler(int argc, char **argv, struct args_options *options,
|
|||
{
|
||||
options->type = INPUT_UNDEFINED;
|
||||
options->input_source = NULL;
|
||||
// options->pretty_print = false;
|
||||
options->pretty_print = false;
|
||||
options->verbose = false;
|
||||
|
||||
struct list *args_list = NULL;
|
||||
|
|
@ -76,11 +76,11 @@ int args_handler(int argc, char **argv, struct args_options *options,
|
|||
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
/* if (strcmp(argv[i], "--pretty-print") == 0)
|
||||
if (strcmp(argv[i], "--pretty-print") == 0)
|
||||
{
|
||||
options->pretty_print = true;
|
||||
} */
|
||||
if (strcmp(argv[i], "--verbose") == 0)
|
||||
}
|
||||
else if (strcmp(argv[i], "--verbose") == 0)
|
||||
{
|
||||
options->verbose = true;
|
||||
}
|
||||
|
|
@ -138,7 +138,7 @@ void args_print(struct args_options *options)
|
|||
: "UNDEFINED");
|
||||
printf("Input source: %s\n",
|
||||
options->input_source ? options->input_source : "NULL");
|
||||
// printf("Pretty print: %s\n", options->pretty_print ? "true" : "false");
|
||||
printf("Pretty print: %s\n", options->pretty_print ? "true" : "false");
|
||||
printf("Verbose: %s\n", options->verbose ? "true" : "false");
|
||||
}
|
||||
|
||||
|
|
@ -147,8 +147,7 @@ void print_usage(FILE *std, const char *program_name)
|
|||
fprintf(std, "Usage: %s [OPTIONS] [SCRIPT] [ARGUMENTS...]\n", program_name);
|
||||
fprintf(std, "Options:\n");
|
||||
fprintf(std, " -c [SCRIPT] Execute the given command string.\n");
|
||||
// fprintf(std, " --pretty-print Enable pretty printing of
|
||||
// outputs.\n");
|
||||
fprintf(std, " --pretty-print Enable pretty printing of outputs.\n");
|
||||
fprintf(std, " --verbose Enable verbose mode.\n");
|
||||
fprintf(std,
|
||||
"If no SCRIPT is provided, input is read from standard input.\n");
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ struct args_options
|
|||
/** Type of the input source */
|
||||
enum input_type type;
|
||||
/** Enable or disable pretty printing of outputs */
|
||||
// bool pretty_print;
|
||||
bool pretty_print;
|
||||
/** Enable or disable verbose mode */
|
||||
bool verbose;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ void ast_free(struct ast **node)
|
|||
if (node == NULL || *node == NULL)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"WARNING: Internal error: failed to free AST node (NULL argument)");
|
||||
"[WARNING] Internal error: failed to free AST node (NULL "
|
||||
"argument)\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -51,16 +52,14 @@ 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;
|
||||
}
|
||||
|
||||
|
|
@ -72,7 +71,10 @@ struct ast *ast_create(enum ast_type type, void *data)
|
|||
{
|
||||
struct ast *node = malloc(sizeof(struct ast));
|
||||
if (!node)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->type = type;
|
||||
node->data = data;
|
||||
|
|
@ -80,7 +82,7 @@ struct ast *ast_create(enum ast_type type, void *data)
|
|||
return node;
|
||||
}
|
||||
|
||||
/* // TODO handle new types (AST_WORD, AST_PIPE, etc.)
|
||||
// TODO handle new types (AST_WORD, AST_PIPE, etc.)
|
||||
static void ast_print_dot_recursive(struct ast *node, FILE *out)
|
||||
{
|
||||
if (!node)
|
||||
|
|
@ -171,4 +173,3 @@ void ast_print_dot(struct ast *ast)
|
|||
fprintf(dot_pipe, "}\n");
|
||||
pclose(dot_pipe);
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
#include "ast_base.h"
|
||||
#include "ast_command.h"
|
||||
#include "ast_end.h"
|
||||
#include "ast_function.h"
|
||||
#include "ast_if.h"
|
||||
#include "ast_list.h"
|
||||
#include "ast_loop.h"
|
||||
|
|
@ -15,6 +14,10 @@
|
|||
#include "ast_redir.h"
|
||||
#include "ast_void.h"
|
||||
#include "ast_word.h"
|
||||
#include "ast_subshell.h"
|
||||
|
||||
/**
|
||||
* Prints the Graphviz DOT representation of the given AST to stdout.
|
||||
*/
|
||||
void ast_print_dot(struct ast *ast);
|
||||
|
||||
#endif /* ! AST_H */
|
||||
|
|
|
|||
|
|
@ -1,30 +1,33 @@
|
|||
#include "ast_and_or.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool ast_is_and_or(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_AND_OR;
|
||||
}
|
||||
|
||||
struct ast_and_or *ast_get_and_or(struct ast *node)
|
||||
{
|
||||
if (ast_is_and_or(node))
|
||||
return (struct ast_and_or *)node->data;
|
||||
if (node != NULL && node->type == AST_AND_OR)
|
||||
return node->data;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *ast_create_and_or(struct ast *left, struct ast *right,
|
||||
enum ast_and_or_type type)
|
||||
{
|
||||
struct ast_and_or *and_or = malloc(sizeof(struct ast_and_or));
|
||||
if (!and_or)
|
||||
struct ast_and_or *and_or_node = malloc(sizeof(struct ast_and_or));
|
||||
if (and_or_node == NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
and_or->left = left;
|
||||
and_or->right = right;
|
||||
and_or->type = type;
|
||||
}
|
||||
and_or_node->left = left;
|
||||
and_or_node->right = right;
|
||||
and_or_node->type = type;
|
||||
|
||||
return ast_create(AST_AND_OR, and_or);
|
||||
struct ast *result = ast_create(AST_AND_OR, and_or_node);
|
||||
if (result == NULL)
|
||||
free(and_or_node);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ast_free_and_or(struct ast_and_or *and_or)
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ struct ast_and_or
|
|||
enum ast_and_or_type type;
|
||||
};
|
||||
|
||||
bool ast_is_and_or(struct ast *node);
|
||||
struct ast_and_or *ast_get_and_or(struct ast *node);
|
||||
struct ast *ast_create_and_or(struct ast *left, struct ast *right,
|
||||
enum ast_and_or_type type);
|
||||
|
|
|
|||
|
|
@ -2,55 +2,93 @@
|
|||
|
||||
#include "ast_assignment.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool ast_is_assignment(struct ast *node)
|
||||
// === Static functions
|
||||
|
||||
/* @brief: splits the assignement 'name=value' into 2 parts,
|
||||
* and fills the fields of ast_assignment with it.
|
||||
*/
|
||||
static bool init_assignments(struct ast_assignment *ast_assignment,
|
||||
char *assignment)
|
||||
{
|
||||
return node != NULL && node->type == AST_ASSIGNMENT;
|
||||
// Split
|
||||
if (assignment == NULL)
|
||||
return false;
|
||||
char *split_pos = strchr(assignment, '=');
|
||||
if (split_pos == NULL)
|
||||
{
|
||||
perror(
|
||||
"Internal error: could not split assignment (no '=' token found)");
|
||||
return false;
|
||||
}
|
||||
*split_pos = '\0';
|
||||
split_pos++; // Points to the beginning of the second string
|
||||
|
||||
// Allocate new strings
|
||||
|
||||
ast_assignment->name = strdup(assignment);
|
||||
if (ast_assignment->name == NULL)
|
||||
{
|
||||
perror("Error: could not duplicate string (is your memory full ?)");
|
||||
return false;
|
||||
}
|
||||
|
||||
ast_assignment->value = strdup(split_pos);
|
||||
if (ast_assignment->name == NULL)
|
||||
{
|
||||
perror("Error: could not duplicate string (is your memory full ?)");
|
||||
free(ast_assignment->name);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// === Functions
|
||||
|
||||
struct ast_assignment *ast_get_assignment(struct ast *node)
|
||||
{
|
||||
if (node == NULL || node->type != AST_ASSIGNMENT)
|
||||
return NULL;
|
||||
return (struct ast_assignment *)node->data;
|
||||
}
|
||||
|
||||
/* @brief: splits the assignement 'name=value' into 2 parts,
|
||||
* and fills the fields of ast_assignment with it.
|
||||
*/
|
||||
static void init_assignments(struct ast_assignment *ast_assignment,
|
||||
char *assignment)
|
||||
{
|
||||
if (assignment == NULL)
|
||||
return;
|
||||
char *split_pos = strchr(assignment, '=');
|
||||
if (split_pos == NULL)
|
||||
return;
|
||||
|
||||
*split_pos = '\0';
|
||||
ast_assignment->name = strdup(assignment);
|
||||
ast_assignment->value = strdup(split_pos + 1);
|
||||
return node->data;
|
||||
}
|
||||
|
||||
struct ast *ast_create_assignment(char *assignment, bool global)
|
||||
{
|
||||
struct ast_assignment *assignment_data =
|
||||
calloc(1, sizeof(struct ast_assignment));
|
||||
if (!assignment_data)
|
||||
if (assignment_data == NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
init_assignments(assignment_data, assignment);
|
||||
assignment_data->global = global;
|
||||
return ast_create(AST_ASSIGNMENT, assignment_data);
|
||||
bool initialized = init_assignments(assignment_data, assignment);
|
||||
if (initialized == false)
|
||||
{
|
||||
free(assignment_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *result = ast_create(AST_ASSIGNMENT, assignment_data);
|
||||
if (result == NULL)
|
||||
free(assignment_data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ast_free_assignment(struct ast_assignment *assignment_data)
|
||||
{
|
||||
if (assignment_data == NULL)
|
||||
return;
|
||||
free(assignment_data->name);
|
||||
free(assignment_data->value);
|
||||
if (assignment_data->name != NULL)
|
||||
free(assignment_data->name);
|
||||
if (assignment_data->value != NULL)
|
||||
free(assignment_data->value);
|
||||
free(assignment_data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ struct ast_assignment
|
|||
bool global;
|
||||
};
|
||||
|
||||
bool ast_is_assignment(struct ast *node);
|
||||
struct ast_assignment *ast_get_assignment(struct ast *node);
|
||||
struct ast *ast_create_assignment(char *assignment, bool global);
|
||||
void ast_free_assignment(struct ast_assignment *assignment_data);
|
||||
|
|
|
|||
|
|
@ -18,8 +18,7 @@ enum ast_type
|
|||
AST_NEG,
|
||||
AST_LOOP,
|
||||
AST_ASSIGNMENT,
|
||||
AST_FUNCTION,
|
||||
AST_SUBSHELL
|
||||
AST_FUNCTION
|
||||
};
|
||||
|
||||
struct ast
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "ast_command.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../lists/lists.h"
|
||||
|
|
@ -11,25 +12,27 @@ struct ast *ast_create_command(struct list *command, struct list *redirections,
|
|||
{
|
||||
struct ast_command *command_data = malloc(sizeof(struct ast_command));
|
||||
if (!command_data)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
command_data->command = command;
|
||||
command_data->redirections = redirections;
|
||||
command_data->assignments = assignments;
|
||||
|
||||
return ast_create(AST_CMD, command_data);
|
||||
struct ast *result = ast_create(AST_CMD, command_data);
|
||||
if (result == NULL)
|
||||
free(command_data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast_command *ast_get_command(struct ast *node)
|
||||
{
|
||||
if (node == NULL || node->type != AST_CMD)
|
||||
return NULL;
|
||||
return (struct ast_command *)node->data;
|
||||
}
|
||||
|
||||
bool ast_is_command(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_CMD;
|
||||
return node->data;
|
||||
}
|
||||
|
||||
void ast_free_command(struct ast_command *command_data)
|
||||
|
|
|
|||
|
|
@ -11,11 +11,6 @@ struct ast_command
|
|||
struct list *assignments; // A list of ASTs, all ast_assignment
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is a command.
|
||||
*/
|
||||
bool ast_is_command(struct ast *node);
|
||||
|
||||
/**
|
||||
* Retrieves the command data from the given AST node.
|
||||
* Assumes that the node is of type AST_CMD.
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ struct ast *ast_create_function(char *name, struct ast *value)
|
|||
if (!function_data)
|
||||
return NULL;
|
||||
|
||||
function_data->name = name;
|
||||
function_data->name = strdup(name);
|
||||
function_data->value = value;
|
||||
|
||||
return ast_create(AST_FUNCTION, function_data);
|
||||
|
|
@ -34,9 +34,7 @@ void ast_free_function(struct ast_function *function_data)
|
|||
if (function_data)
|
||||
{
|
||||
free(function_data->name);
|
||||
// WARNING: this ast will be stored in the function hashmap.
|
||||
// thus, it will be freed from the hashmap.
|
||||
// ast_free(&function_data->value);
|
||||
ast_free(&function_data->value);
|
||||
free(function_data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,20 +9,20 @@ struct ast_function
|
|||
struct ast *value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief: Checks if the given AST node is an ast_function
|
||||
* Checks if the given AST node is an ast_function
|
||||
*/
|
||||
bool ast_is_function(struct ast *node);
|
||||
|
||||
/**
|
||||
* @brief: Retrieves the function data from the given AST node.
|
||||
* Assumes that the node is of type AST_function.
|
||||
* Retrieves the function data from the given AST node.
|
||||
* Assumes that the node is of type AST_function.
|
||||
*/
|
||||
struct ast_function *ast_get_function(struct ast *node);
|
||||
|
||||
/**
|
||||
* @brief: Creates a new AST node representing an AST_function
|
||||
* @warning: name must be already allocated.
|
||||
* Creates a new AST node representing an AST_function
|
||||
*/
|
||||
struct ast *ast_create_function(char *name, struct ast *value);
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "ast_if.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct ast *ast_create_if(struct ast *condition, struct ast *then_clause,
|
||||
|
|
@ -8,13 +9,20 @@ struct ast *ast_create_if(struct ast *condition, struct ast *then_clause,
|
|||
{
|
||||
struct ast_if *if_data = malloc(sizeof(struct ast_if));
|
||||
if (!if_data)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if_data->condition = condition;
|
||||
if_data->then_clause = then_clause;
|
||||
if_data->else_clause = else_clause;
|
||||
|
||||
return ast_create(AST_IF, if_data);
|
||||
struct ast *result = ast_create(AST_IF, if_data);
|
||||
if (result == NULL)
|
||||
free(if_data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast_if *ast_get_if(struct ast *node)
|
||||
|
|
@ -24,11 +32,6 @@ struct ast_if *ast_get_if(struct ast *node)
|
|||
return node->data;
|
||||
}
|
||||
|
||||
bool ast_is_if(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_IF;
|
||||
}
|
||||
|
||||
void ast_free_if(struct ast_if *if_data)
|
||||
{
|
||||
if (if_data == NULL)
|
||||
|
|
|
|||
|
|
@ -10,11 +10,6 @@ struct ast_if
|
|||
struct ast *else_clause;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is an if statement.
|
||||
*/
|
||||
bool ast_is_if(struct ast *node);
|
||||
|
||||
/**
|
||||
* Retrieves the if statement data from the given AST node.
|
||||
* Assumes that the node is of type AST_IF.
|
||||
|
|
|
|||
|
|
@ -1,28 +1,32 @@
|
|||
#include "ast_list.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct ast *ast_create_list(struct list *list)
|
||||
{
|
||||
struct ast_list *ast_list = malloc(sizeof(struct ast_list));
|
||||
if (ast_list == NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ast_list->children = list;
|
||||
|
||||
return ast_create(AST_LIST, ast_list);
|
||||
struct ast *result = ast_create(AST_LIST, ast_list);
|
||||
if (result == NULL)
|
||||
free(ast_list);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast_list *ast_get_list(struct ast *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
if (node == NULL || node->type != AST_LIST)
|
||||
return NULL;
|
||||
return node->data;
|
||||
}
|
||||
|
||||
bool ast_is_list(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_LIST;
|
||||
}
|
||||
|
||||
void ast_free_list(struct ast_list *ast_list)
|
||||
{
|
||||
if (ast_list == NULL)
|
||||
|
|
@ -40,7 +44,7 @@ void ast_list_deep_destroy(struct list *l)
|
|||
{
|
||||
next_elt = elt->next;
|
||||
|
||||
struct ast *node = (struct ast *)elt->data;
|
||||
struct ast *node = elt->data;
|
||||
ast_free(&node);
|
||||
free(elt);
|
||||
elt = next_elt;
|
||||
|
|
|
|||
|
|
@ -29,11 +29,6 @@ struct ast *ast_create_list(struct list *ast_list);
|
|||
*/
|
||||
struct ast_list *ast_get_list(struct ast *node);
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is a command.
|
||||
*/
|
||||
bool ast_is_list(struct ast *node);
|
||||
|
||||
/* @brief: frees the given ast list.
|
||||
*
|
||||
* @warning: should only be called by ast_free() function.
|
||||
|
|
|
|||
|
|
@ -1,30 +1,32 @@
|
|||
#include "ast_loop.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct ast *ast_create_loop(struct ast *condition, struct ast *body)
|
||||
{
|
||||
struct ast_loop *node_data = malloc(sizeof(struct ast_loop));
|
||||
if (!node_data)
|
||||
if (node_data != NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node_data->condition = condition;
|
||||
node_data->body = body;
|
||||
|
||||
return ast_create(AST_LOOP, node_data);
|
||||
struct ast *result = ast_create(AST_LOOP, node_data);
|
||||
if (result == NULL)
|
||||
free(node_data);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast_loop *ast_get_loop(struct ast *node)
|
||||
{
|
||||
if (node == NULL || node->type != AST_LOOP)
|
||||
return NULL;
|
||||
return (struct ast_loop *)node->data;
|
||||
}
|
||||
|
||||
bool ast_is_loop(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_LOOP;
|
||||
return node->data;
|
||||
}
|
||||
|
||||
void ast_free_loop(struct ast_loop *loop_data)
|
||||
|
|
|
|||
|
|
@ -10,11 +10,6 @@ struct ast_loop
|
|||
struct ast *body;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is a loop.
|
||||
*/
|
||||
bool ast_is_loop(struct ast *node);
|
||||
|
||||
/**
|
||||
* Retrieves the loop data from the given AST node.
|
||||
* Assumes that the node is of type AST_LOOP.
|
||||
|
|
@ -24,10 +19,10 @@ struct ast_loop *ast_get_loop(struct ast *node);
|
|||
/**
|
||||
* Creates a new AST node representing a loop.
|
||||
*/
|
||||
struct ast *ast_create_loop(struct ast *condition, struct ast *body);
|
||||
struct ast *ast_create_loop(struct ast* condition, struct ast* body);
|
||||
|
||||
/*
|
||||
* @brief: frees the given ast_loop and sets the pointer to NULL.
|
||||
* @brief frees the given ast_loop and sets the pointer to NULL.
|
||||
*/
|
||||
void ast_free_loop(struct ast_loop *loop_node);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,34 +1,38 @@
|
|||
#include "ast_neg.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool ast_is_neg(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_NEG;
|
||||
}
|
||||
|
||||
struct ast_neg *ast_get_neg(struct ast *node)
|
||||
{
|
||||
if (ast_is_neg(node))
|
||||
return node->data;
|
||||
return NULL;
|
||||
if (node == NULL || node->type != AST_NEG)
|
||||
return NULL;
|
||||
return node->data;
|
||||
}
|
||||
|
||||
struct ast *ast_create_neg(bool negation, struct ast *child)
|
||||
{
|
||||
struct ast_neg *node = malloc(sizeof(struct ast_neg));
|
||||
if (!node)
|
||||
struct ast_neg *neg_data = malloc(sizeof(struct ast_neg));
|
||||
if (neg_data == NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->negation = negation;
|
||||
node->child = child;
|
||||
return ast_create(AST_NEG, node);
|
||||
neg_data->negation = negation;
|
||||
neg_data->child = child;
|
||||
struct ast *result = ast_create(AST_NEG, neg_data);
|
||||
if (result == NULL)
|
||||
free(neg_data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ast_free_neg(struct ast_neg *node)
|
||||
{
|
||||
if (!node)
|
||||
if (node == NULL)
|
||||
return;
|
||||
|
||||
ast_free(&node->child);
|
||||
free(node);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ struct ast_neg
|
|||
struct ast *child;
|
||||
};
|
||||
|
||||
bool ast_is_neg(struct ast *node);
|
||||
struct ast_neg *ast_get_neg(struct ast *node);
|
||||
struct ast *ast_create_neg(bool negation, struct ast *child);
|
||||
void ast_free_neg(struct ast_neg *node);
|
||||
|
|
|
|||
|
|
@ -1,34 +1,39 @@
|
|||
#include "ast_pipe.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool ast_is_pipe(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_REDIR;
|
||||
}
|
||||
|
||||
struct ast_pipe *ast_get_pipe(struct ast *node)
|
||||
{
|
||||
if (ast_is_pipe(node))
|
||||
return node->data;
|
||||
return NULL;
|
||||
if (node == NULL || node->type != AST_REDIR)
|
||||
return NULL;
|
||||
return node->data;
|
||||
}
|
||||
|
||||
struct ast *ast_create_pipe(struct ast *left, struct ast *right)
|
||||
{
|
||||
struct ast_pipe *node = malloc(sizeof(struct ast_pipe));
|
||||
if (!node)
|
||||
struct ast_pipe *ast_pipe = malloc(sizeof(struct ast_pipe));
|
||||
if (ast_pipe != NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
node->left = left;
|
||||
node->right = right;
|
||||
}
|
||||
|
||||
return ast_create(AST_PIPE, node);
|
||||
ast_pipe->left = left;
|
||||
ast_pipe->right = right;
|
||||
|
||||
struct ast *result = ast_create(AST_PIPE, ast_pipe);
|
||||
if (result == NULL)
|
||||
free(ast_pipe);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ast_free_pipe(struct ast_pipe *node)
|
||||
{
|
||||
if (!node)
|
||||
if (node == NULL)
|
||||
return;
|
||||
|
||||
ast_free(&node->left);
|
||||
ast_free(&node->right);
|
||||
free(node);
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ struct ast_pipe
|
|||
// Output of left will be redirected to right stdin
|
||||
};
|
||||
|
||||
bool ast_is_pipe(struct ast *node);
|
||||
struct ast_pipe *ast_get_pipe(struct ast *node);
|
||||
struct ast *ast_create_pipe(struct ast *left, struct ast *right);
|
||||
void ast_free_pipe(struct ast_pipe *node);
|
||||
|
|
|
|||
|
|
@ -1,38 +1,44 @@
|
|||
#include "ast_redir.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool ast_is_redir(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_REDIR;
|
||||
}
|
||||
|
||||
struct ast_redir *ast_get_redir(struct ast *node)
|
||||
{
|
||||
if (ast_is_redir(node))
|
||||
return (struct ast_redir *)node->data;
|
||||
return NULL;
|
||||
if (node == NULL || node->type != AST_REDIR)
|
||||
return NULL;
|
||||
return node->data;
|
||||
}
|
||||
|
||||
struct ast *ast_create_redir(char *filename, int io_number,
|
||||
enum ast_redir_type type)
|
||||
{
|
||||
struct ast_redir *redir = malloc(sizeof(struct ast_redir));
|
||||
if (!redir)
|
||||
struct ast_redir *redir_node = malloc(sizeof(struct ast_redir));
|
||||
if (redir_node == NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
redir->filename =
|
||||
}
|
||||
|
||||
redir_node->filename =
|
||||
filename; // Takes ownership? Usually yes in simple ASTs, or dup. Let's
|
||||
// assume pointer copy for now, but user must manage memory.
|
||||
redir->io_number = io_number;
|
||||
redir->type = type;
|
||||
redir_node->io_number = io_number;
|
||||
redir_node->type = type;
|
||||
|
||||
return ast_create(AST_REDIR, redir);
|
||||
struct ast *result = ast_create(AST_REDIR, redir_node);
|
||||
if (result == NULL)
|
||||
free(redir_node);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ast_free_redir(struct ast_redir *redir)
|
||||
{
|
||||
if (!redir)
|
||||
if (redir == NULL)
|
||||
return;
|
||||
free(redir->filename);
|
||||
|
||||
if (redir->filename != NULL)
|
||||
free(redir->filename);
|
||||
free(redir);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ struct ast_redir
|
|||
int saved_fd; // To store the original FD for restoration (-1 before save)
|
||||
};
|
||||
|
||||
bool ast_is_redir(struct ast *node);
|
||||
struct ast_redir *ast_get_redir(struct ast *node);
|
||||
struct ast *ast_create_redir(char *filename, int io_number,
|
||||
enum ast_redir_type type);
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
#include "ast_subshell.h"
|
||||
|
||||
bool ast_is_subshell(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_SUBSHELL;
|
||||
}
|
||||
|
||||
struct ast_subshell *ast_get_subshell(struct ast *node)
|
||||
{
|
||||
if (ast_is_subshell(node))
|
||||
return (struct ast_subshell *)node->data;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *ast_create_subshell(struct ast *child)
|
||||
{
|
||||
struct ast_subshell *subshell = calloc(1, sizeof(struct ast_subshell));
|
||||
if (subshell == NULL)
|
||||
return NULL;
|
||||
subshell->child = child;
|
||||
|
||||
return ast_create(AST_SUBSHELL, subshell);
|
||||
}
|
||||
|
||||
void ast_free_subshell(struct ast_subshell *subshell)
|
||||
{
|
||||
if (!subshell)
|
||||
return;
|
||||
ast_free(&subshell->child);
|
||||
free(subshell);
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
#ifndef AST_SUBSHELL_H
|
||||
#define AST_SUBSHELL_H
|
||||
|
||||
#include "ast_base.h"
|
||||
|
||||
struct ast_subshell
|
||||
{
|
||||
struct ast *child;
|
||||
};
|
||||
|
||||
bool ast_is_subshell(struct ast *node);
|
||||
|
||||
struct ast_subshell *ast_get_subshell(struct ast *node);
|
||||
|
||||
struct ast *ast_create_subshell(struct ast *child);
|
||||
|
||||
void ast_free_subshell(struct ast_subshell *subshell);
|
||||
|
||||
#endif /* ! AST_SUBSHELL_H */
|
||||
|
|
@ -3,11 +3,6 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool ast_is_void(struct ast *node)
|
||||
{
|
||||
return node != NULL && node->type == AST_VOID;
|
||||
}
|
||||
|
||||
struct ast *ast_create_void(void)
|
||||
{
|
||||
return ast_create(AST_VOID, NULL);
|
||||
|
|
|
|||
|
|
@ -4,11 +4,6 @@
|
|||
#include "../lists/lists.h"
|
||||
#include "ast_base.h"
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is of type AST_VOID.
|
||||
*/
|
||||
bool ast_is_void(struct ast *node);
|
||||
|
||||
/**
|
||||
* Creates a new AST node representing NOTHING
|
||||
* WARNING: data will be a NULL pointer
|
||||
|
|
|
|||
|
|
@ -2,22 +2,33 @@
|
|||
#include "ast_word.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct ast *ast_create_word(char *word)
|
||||
{
|
||||
struct ast_word *ast_node = malloc(sizeof(struct ast_word));
|
||||
if (ast_node == NULL)
|
||||
struct ast_word *word_node = malloc(sizeof(struct ast_word));
|
||||
if (word_node == NULL)
|
||||
{
|
||||
perror("Error: could not allocate more memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ast_node->type = AST_WORD;
|
||||
ast_node->word = strdup(word);
|
||||
struct ast *res = ast_create(AST_WORD, ast_node);
|
||||
word_node->type = AST_WORD;
|
||||
word_node->word = strdup(word);
|
||||
if (word_node->word == NULL)
|
||||
{
|
||||
perror("Error: could not duplicate string (is your memory full ?)");
|
||||
free(word_node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *res = ast_create(AST_WORD, word_node);
|
||||
if (res == NULL)
|
||||
{
|
||||
free(ast_node->word);
|
||||
free(ast_node);
|
||||
free(word_node->word);
|
||||
free(word_node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -32,11 +43,6 @@ struct ast_word *ast_get_word(struct ast *node)
|
|||
return node->data;
|
||||
}
|
||||
|
||||
bool ast_is_word(struct ast *node)
|
||||
{
|
||||
return node && node->type == AST_WORD;
|
||||
}
|
||||
|
||||
void ast_free_word(struct ast_word *ast_node)
|
||||
{
|
||||
if (ast_node == NULL)
|
||||
|
|
|
|||
|
|
@ -9,11 +9,6 @@ struct ast_word
|
|||
char *word;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is a command.
|
||||
*/
|
||||
bool ast_is_word(struct ast *node);
|
||||
|
||||
/**
|
||||
* Retrieves the command data from the given AST node.
|
||||
* Assumes that the node is of type AST_CMD.
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../ast/ast.h"
|
||||
|
||||
/*
|
||||
** Hash the key using FNV-1a 32 bits hash algorithm.
|
||||
*/
|
||||
|
|
@ -38,14 +36,6 @@ static void destroy_pair_list(struct pair_list **p)
|
|||
*p = NULL;
|
||||
}
|
||||
|
||||
static void destroy_pair_list_ast(struct pair_list **p)
|
||||
{
|
||||
free((char *)(*p)->key);
|
||||
ast_free((*p)->value);
|
||||
free((*p));
|
||||
*p = NULL;
|
||||
}
|
||||
|
||||
struct hash_map *hash_map_init(size_t size)
|
||||
{
|
||||
struct hash_map *p = malloc(sizeof(struct hash_map));
|
||||
|
|
@ -130,29 +120,6 @@ void hash_map_free(struct hash_map **hash_map)
|
|||
}
|
||||
}
|
||||
|
||||
void hash_map_free_ast(struct hash_map **hash_map)
|
||||
{
|
||||
struct pair_list *l;
|
||||
struct pair_list *prev;
|
||||
|
||||
if (hash_map != NULL && *hash_map != NULL)
|
||||
{
|
||||
for (size_t i = 0; i < (*hash_map)->size; i++)
|
||||
{
|
||||
l = (*hash_map)->data[i];
|
||||
while (l != NULL)
|
||||
{
|
||||
prev = l;
|
||||
l = l->next;
|
||||
destroy_pair_list_ast(&prev);
|
||||
}
|
||||
}
|
||||
free((*hash_map)->data);
|
||||
free(*hash_map);
|
||||
*hash_map = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void hash_map_foreach(struct hash_map *hash_map,
|
||||
void (*fn)(const char *, const void *))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,106 +10,93 @@ struct list
|
|||
};
|
||||
|
||||
/*
|
||||
** @brief Insert a node containing `value` at the beginning of the list.
|
||||
** @return `NULL` if an error occured.
|
||||
*/
|
||||
* @brief Insert a node containing `value` at the beginning of the list.
|
||||
* @return `NULL` if an error occured.
|
||||
*/
|
||||
struct list *list_prepend(struct list *list, void *value);
|
||||
|
||||
/*
|
||||
** Return the lenght of the list.
|
||||
** Return `0` if the list is empty.
|
||||
*/
|
||||
* @return the length of the list or 0 if the list is empty.
|
||||
*/
|
||||
size_t list_length(struct list *list);
|
||||
|
||||
/*
|
||||
** Display the list contents on `stdout`.
|
||||
** Nothing is displayed if the list is empty.
|
||||
*/
|
||||
* @brief Display the list contents on `stdout`.
|
||||
* Nothing is displayed if the list is empty.
|
||||
*/
|
||||
void list_print(struct list *list);
|
||||
|
||||
/*
|
||||
** Release the memory used by the list.
|
||||
** Does nothing if `list` is `NULL`.
|
||||
*/
|
||||
* @brief Releases the memory used by the list.
|
||||
* Does nothing if `list` is `NULL`.
|
||||
*/
|
||||
void list_destroy(struct list **list);
|
||||
|
||||
/*
|
||||
** Release the memory used by the list and its content
|
||||
** Does nothing if `list` is `NULL`.
|
||||
*/
|
||||
* @brief Releases the memory used by the list and its content
|
||||
* Does nothing if `list` is `NULL`.
|
||||
*/
|
||||
void list_deep_destroy(struct list *l);
|
||||
|
||||
/*
|
||||
** Append a node containing `value` at the end of the list.
|
||||
** Return `NULL` if an error occured.
|
||||
*/
|
||||
// START PROTO list_append
|
||||
* @brief Append a node containing `value` at the end of the list.
|
||||
* @return The head of the list or `NULL` if an error occured.
|
||||
*/
|
||||
struct list *list_append(struct list *list, void *value);
|
||||
// END PROTO list_append
|
||||
|
||||
/*
|
||||
** Insert a node containing `value` at the index `index` in the list.
|
||||
** If the index is greater than the length of the list, the behaviour is the
|
||||
** same as `list_append`.
|
||||
** Return `NULL` if an error occured.
|
||||
*/
|
||||
// START PROTO list_insert
|
||||
* @brief Insert a node containing `value` at the index `index` in the list.
|
||||
* If the index is greater than the length of the list, the behaviour is the
|
||||
* same as `list_append`.
|
||||
* @return The head of the list or `NULL` if an error occured.
|
||||
*/
|
||||
struct list *list_insert(struct list *list, void *value, size_t index);
|
||||
// END PROTO list_insert
|
||||
|
||||
/*
|
||||
** Remove the element at the index `index`.
|
||||
** Return `NULL` if an error occured.
|
||||
*/
|
||||
// START PROTO list_remove
|
||||
* @brief Remove the element at the index `index`.
|
||||
* @return The head of the list or `NULL` if an error occured.
|
||||
*/
|
||||
struct list *list_remove(struct list *list, size_t index);
|
||||
// END PROTO list_remove
|
||||
|
||||
/*
|
||||
** Return the position of the first node containing `value`.
|
||||
** Return `-1` if nothing is found.
|
||||
*/
|
||||
// START PROTO list_find
|
||||
* @return the position of the first node containing `value`
|
||||
* or `-1` if nothing is found.
|
||||
*/
|
||||
int list_find(struct list *list, void *value);
|
||||
// END PROTO list_find
|
||||
|
||||
// NOTE the following functions are commented because no other module
|
||||
// needs them. Feel free to decomment one if you need but keep in
|
||||
// mind the max function exports.
|
||||
|
||||
/*
|
||||
** Concatenate the list `list2` at the end of the list `list`.
|
||||
** Return `list2` if `list` is `NULL`.
|
||||
*/
|
||||
// START PROTO list_concat
|
||||
* Concatenate the list `list2` at the end of the list `list`.
|
||||
* Return `list2` if `list` is `NULL`.
|
||||
*/
|
||||
// struct list *list_concat(struct list *list, struct list *list2);
|
||||
// END PROTO list_concat
|
||||
|
||||
/*
|
||||
** Sort the elements of the list in ascending order.
|
||||
** Return the new list.
|
||||
*/
|
||||
// START PROTO list_sort
|
||||
* Sort the elements of the list in ascending order.
|
||||
* Return the new list.
|
||||
*/
|
||||
// struct list *list_sort(struct list *list);
|
||||
// END PROTO list_sort
|
||||
|
||||
/*
|
||||
** Invert the order of the elements of the list.
|
||||
** Return the new list.
|
||||
*/
|
||||
// START PROTO list_reverse
|
||||
* Invert the order of the elements of the list.
|
||||
* Return the new list.
|
||||
*/
|
||||
// struct list *list_reverse(struct list *list);
|
||||
// END PROTO list_reverse
|
||||
|
||||
/*
|
||||
** Split the list at index `index`.
|
||||
** First part goes in `list` and contains the element at `index`.
|
||||
** Second part is returned.
|
||||
** Return `NULL` if `list` is `NULL` or `index` is invalid.
|
||||
*/
|
||||
// START PROTO list_split
|
||||
* Split the list at index `index`.
|
||||
* First part goes in `list` and contains the element at `index`.
|
||||
* Second part is returned.
|
||||
* Return `NULL` if `list` is `NULL` or `index` is invalid.
|
||||
*/
|
||||
// struct list *list_split(struct list *list, size_t index);
|
||||
// END PROTO list_split
|
||||
|
||||
/**
|
||||
/*
|
||||
* @brief: Folds the list from left to right using func and an accumulator.
|
||||
*/
|
||||
void list_fold(struct list *list, void *acc, void (*func)(void *, void *));
|
||||
|
||||
#endif /* ! LISTS_H */
|
||||
#endif /* ! LISTS_H */
|
||||
|
|
|
|||
|
|
@ -110,7 +110,8 @@ void list_deep_destroy(struct list *l)
|
|||
while (elt != NULL)
|
||||
{
|
||||
next_elt = elt->next;
|
||||
free(elt->data);
|
||||
if (elt->data != NULL)
|
||||
free(elt->data);
|
||||
free(elt);
|
||||
elt = next_elt;
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "main_loop.h"
|
||||
|
||||
// === Includes
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "../../execution/execution.h"
|
||||
#include "../../io_backend/io_backend.h"
|
||||
#include "../../lexer/lexer.h"
|
||||
#include "../../parser/parser.h"
|
||||
#include "../args/args.h"
|
||||
#include "../vars/vars.h"
|
||||
|
||||
// === Functions
|
||||
|
||||
int err_input(struct hash_map **vars, struct lexer_context *ctx)
|
||||
{
|
||||
hash_map_free(vars);
|
||||
destroy_lexer_context(ctx);
|
||||
return ERR_INPUT_PROCESSING;
|
||||
}
|
||||
|
||||
int main_loop(struct lexer_context *ctx, struct hash_map *vars)
|
||||
{
|
||||
int return_code = SUCCESS;
|
||||
|
||||
// Retrieve and build first AST
|
||||
struct ast *command_ast = get_ast(ctx);
|
||||
|
||||
// Main parse-execute loop
|
||||
while (command_ast != NULL && command_ast->type != AST_END)
|
||||
{
|
||||
if (command_ast->type != AST_VOID)
|
||||
{
|
||||
// Execute AST
|
||||
return_code = execution(command_ast, vars);
|
||||
|
||||
// set $? variable
|
||||
set_var_int(vars, "?", return_code);
|
||||
}
|
||||
|
||||
ast_free(&command_ast);
|
||||
|
||||
// Retrieve and build next AST
|
||||
command_ast = get_ast(ctx);
|
||||
}
|
||||
|
||||
if (command_ast == NULL)
|
||||
return err_input(&vars, ctx);
|
||||
|
||||
// === free
|
||||
|
||||
ast_free(&command_ast);
|
||||
hash_map_free(&vars);
|
||||
destroy_lexer_context(ctx);
|
||||
|
||||
return return_code;
|
||||
}
|
||||
|
||||
/* @brief: initializes a lexer context from a command string.
|
||||
* @return: pointer to the lexer context, or NULL on failure.
|
||||
*/
|
||||
static struct lexer_context *lexer_init_from_string(char *command)
|
||||
{
|
||||
// Create a lexer context from the command string
|
||||
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
ctx->end_previous_token = strdup(command);
|
||||
if (ctx->end_previous_token == NULL)
|
||||
{
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
ctx->remaining_chars = strlen(command);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
int start_subshell(struct hash_map **parent_vars, char *command)
|
||||
{
|
||||
int fd = fork();
|
||||
if (fd < 0)
|
||||
return ERR_GENERIC;
|
||||
|
||||
else if (fd == 0) // Child process
|
||||
{
|
||||
struct lexer_context *ctx = lexer_init_from_string(command);
|
||||
if (ctx == NULL)
|
||||
return ERR_MALLOC;
|
||||
int return_code = main_loop(ctx, *parent_vars);
|
||||
exit(return_code);
|
||||
}
|
||||
else // Parent process
|
||||
{
|
||||
int status;
|
||||
if (waitpid(fd, &status, 0) == -1)
|
||||
return ERR_GENERIC;
|
||||
if (WIFEXITED(status))
|
||||
return WEXITSTATUS(status);
|
||||
else
|
||||
return ERR_GENERIC;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
#ifndef MAIN_LOOP_H
|
||||
#define MAIN_LOOP_H
|
||||
|
||||
#include "../../utils/vars/vars.h"
|
||||
#include "../../lexer/lexer.h"
|
||||
|
||||
// === Error codes
|
||||
#define SUCCESS 0
|
||||
#define ERR_INPUT_PROCESSING 2
|
||||
#define ERR_MALLOC 3
|
||||
#define ERR_GENERIC 4
|
||||
|
||||
/* @brief: main loop called from main.
|
||||
* @return: exit code.
|
||||
*/
|
||||
int main_loop(struct lexer_context *ctx, struct hash_map *vars);
|
||||
|
||||
/*
|
||||
* @brief: frees the hash map and lexer context.
|
||||
* @return: ERR_INPUT_PROCESSING.
|
||||
*/
|
||||
int err_input(struct hash_map **vars, struct lexer_context *ctx);
|
||||
|
||||
/*
|
||||
* @brief: starts a subshell and builds the intern lexer context
|
||||
* from the string.
|
||||
* @return: exit code of the subshell.
|
||||
*/
|
||||
int start_subshell(struct hash_map **parent_vars, char *command);
|
||||
|
||||
#endif /* MAIN_LOOP_H */
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
func()
|
||||
{
|
||||
echo hello
|
||||
}
|
||||
|
||||
arg_func()
|
||||
{
|
||||
echo first argument is "$1"
|
||||
}
|
||||
|
||||
func_in_func()
|
||||
{
|
||||
func
|
||||
}
|
||||
|
||||
func_one_line() { echo "this is on one line"; }
|
||||
|
||||
func
|
||||
arg_func "HERE"
|
||||
func_in_func
|
||||
func_one_line
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
echo "starting tests"
|
||||
|
||||
while false;
|
||||
do
|
||||
echo "should NOT be printed"
|
||||
done
|
||||
|
||||
a='yes'
|
||||
while [ "$a" -eq "yes" ];
|
||||
do
|
||||
a="no"
|
||||
echo "should be printed only once"
|
||||
done;
|
||||
|
||||
while true;
|
||||
do
|
||||
echo "yes"
|
||||
done;
|
||||
|
||||
echo "tests done"
|
||||
|
|
@ -269,6 +269,9 @@ echo -e "\n\n""===$BGreen TestsuitatorX Ultra Pro Max+ 365 Premium Gris Sidéral
|
|||
|
||||
|
||||
|
||||
|
||||
#
|
||||
|
||||
echo -e "\n$BBlue=== Builtins ===$Color_Off"
|
||||
# echo
|
||||
test_str "Hello" "echo Hello"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
|
@ -16,7 +17,7 @@ Test(expand, no_expansion)
|
|||
char str[] = "echo something";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
bool ret = expand(ast_command, NULL);
|
||||
|
|
@ -31,7 +32,7 @@ Test(expand, single_quotes_no_expansion)
|
|||
char str[] = "echo '$VAR'";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -50,7 +51,7 @@ Test(expand, single_dollar)
|
|||
char str[] = "echo $ sign";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -69,7 +70,7 @@ Test(expand, empty_braces_no_expansion)
|
|||
char str[] = "echo ${}";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -86,7 +87,7 @@ Test(expand, basic_expansion)
|
|||
char str[] = "echo $VAR";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -105,7 +106,7 @@ Test(expand, multiple_expansion)
|
|||
char str[] = "echo $VAR1 $VAR2 ${VAR3}";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -127,7 +128,7 @@ Test(expand, env_variable)
|
|||
char str[] = "echo $MY_ENV_VAR";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
setenv("MY_ENV_VAR", "environment", 0);
|
||||
|
|
@ -144,7 +145,7 @@ Test(expand, undefined_variable)
|
|||
char str[] = "echo $UNDEFINED";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -162,7 +163,7 @@ Test(expand, nested_expansion)
|
|||
char str[] = "echo $B";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -177,34 +178,34 @@ Test(expand, nested_expansion)
|
|||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
// Test(expand, mixed_quotes_expansion)
|
||||
// {
|
||||
// char str[] = "echo \"$VAR1 and '$VAR2'\"";
|
||||
// char *str_heap = strdup(str);
|
||||
// struct list *list = list_append(NULL, str_heap);
|
||||
// struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
// struct ast_command *ast_command = ast_get_command(ast);
|
||||
Test(expand, mixed_quotes_expansion)
|
||||
{
|
||||
char str[] = "echo \"$VAR1 and '$VAR2'\"";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
// struct hash_map *vars = vars_init();
|
||||
// set_var_copy(vars, "VAR1", "expanded");
|
||||
// set_var_copy(vars, "VAR2", "not_expanded");
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR1", "expanded");
|
||||
set_var_copy(vars, "VAR2", "not_expanded");
|
||||
|
||||
// bool ret = expand(ast_command, vars);
|
||||
// cr_expect(ret, "expansion failed with %s", str);
|
||||
// cr_expect_str_eq((char *)ast_command->command->data,
|
||||
// "echo \"expanded and $VAR2\"",
|
||||
// "Variable in double quotes should expand, while variable "
|
||||
// "in single quotes should not");
|
||||
// ast_free(&ast);
|
||||
// hash_map_free(&vars);
|
||||
// }
|
||||
bool ret = expand(ast_command, vars);
|
||||
cr_expect(ret, "expansion failed with %s", str);
|
||||
cr_expect_str_eq((char *)ast_command->command->data,
|
||||
"echo \"expanded and $VAR2\"",
|
||||
"Variable in double quotes should expand, while variable "
|
||||
"in single quotes should not");
|
||||
ast_free(&ast);
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(expand, adjacent_variables)
|
||||
{
|
||||
char str[] = "echo $VAR1$VAR2";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -224,7 +225,7 @@ Test(expand, random)
|
|||
char str[] = "$RANDOM";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
bool ret = expand(ast_command, NULL);
|
||||
|
|
@ -240,7 +241,7 @@ Test(expand, pid)
|
|||
char str[] = "$$";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -258,7 +259,7 @@ Test(expand, default_last_exit_code)
|
|||
char str[] = "$?";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list, NULL, NULL);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
|
||||
#include "../../../src/expansion/expansion.h"
|
||||
|
||||
TestSuite(parse_subshell_str);
|
||||
|
||||
Test(parse_subshell_str, basic_subshell)
|
||||
{
|
||||
char *input = "(ls -l)";
|
||||
char *extracted_var = NULL;
|
||||
size_t r = parse_subshell_str(input, &extracted_var);
|
||||
|
||||
cr_expect(r == 7);
|
||||
cr_expect_str_eq(extracted_var, "ls -l");
|
||||
free(extracted_var);
|
||||
}
|
||||
|
||||
Test(parse_subshell_str, multi_basic_subshell)
|
||||
{
|
||||
char *input = "(echo hello) and (echo world)";
|
||||
char *extracted_var = NULL;
|
||||
size_t r = parse_subshell_str(input, &extracted_var);
|
||||
|
||||
cr_expect(r == 12);
|
||||
cr_expect_str_eq(extracted_var, "echo hello");
|
||||
free(extracted_var);
|
||||
|
||||
input += r + 5; // skip " and "
|
||||
r = parse_subshell_str(input, &extracted_var);
|
||||
|
||||
cr_expect(r == 12);
|
||||
cr_expect_str_eq(extracted_var, "echo world");
|
||||
free(extracted_var);
|
||||
}
|
||||
|
||||
Test(parse_subshell_str, incomplete_braces)
|
||||
{
|
||||
char *input = "(echo hello";
|
||||
char *extracted_var = NULL;
|
||||
size_t r = parse_subshell_str(input, &extracted_var);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(extracted_var == NULL);
|
||||
}
|
||||
|
||||
Test(parse_subshell_str, empty_braces)
|
||||
{
|
||||
char *input = "()";
|
||||
char *extracted_var = NULL;
|
||||
size_t r = parse_subshell_str(input, &extracted_var);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(extracted_var == NULL);
|
||||
}
|
||||
|
||||
Test(parse_subshell_str, nested_subshell)
|
||||
{
|
||||
char *input = "(echo (nested))";
|
||||
char *extracted_var = NULL;
|
||||
size_t r = parse_subshell_str(input, &extracted_var);
|
||||
|
||||
cr_expect(r == 15);
|
||||
cr_expect_str_eq(extracted_var, "echo (nested)");
|
||||
free(extracted_var);
|
||||
|
||||
char *nested = input + 6; // point to the nested subshell
|
||||
r = parse_subshell_str(nested, &extracted_var);
|
||||
cr_expect(r == 8);
|
||||
cr_expect_str_eq(extracted_var, "nested");
|
||||
free(extracted_var);
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
|
||||
#include "../../../src/expansion/expansion.h"
|
||||
|
||||
|
|
|
|||
141
tests/unit/parser/parser_tests.c
Normal file
141
tests/unit/parser/parser_tests.c
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "io_backend/io_backend.h"
|
||||
#include "lexer/lexer.h"
|
||||
#include "parser/parser.h"
|
||||
#include "utils/ast/ast.h"
|
||||
#include "utils/ast/ast_command.h"
|
||||
|
||||
TestSuite(parser);
|
||||
|
||||
static void init_lexer_ctx(struct lexer_context *ctx)
|
||||
{
|
||||
ctx->end_previous_token = NULL;
|
||||
ctx->remaining_chars = 0;
|
||||
ctx->only_digits = false;
|
||||
ctx->previous_token = NULL;
|
||||
ctx->current_token = NULL;
|
||||
}
|
||||
|
||||
Test(parser, init_success)
|
||||
{
|
||||
int res = parser_init();
|
||||
cr_assert(res == true, "parser_init should return true on success");
|
||||
parser_close();
|
||||
}
|
||||
|
||||
Test(parser, init_twice)
|
||||
{
|
||||
parser_init();
|
||||
int res = parser_init();
|
||||
cr_assert(res == false,
|
||||
"parser_init should return false when called twice");
|
||||
parser_close();
|
||||
}
|
||||
|
||||
Test(parser, close_without_init)
|
||||
{
|
||||
parser_close();
|
||||
}
|
||||
|
||||
Test(parser, close_already_closed)
|
||||
{
|
||||
parser_init();
|
||||
parser_close();
|
||||
parser_close();
|
||||
}
|
||||
|
||||
Test(parser, get_ast_not_init)
|
||||
{
|
||||
struct lexer_context ctx;
|
||||
init_lexer_ctx(&ctx);
|
||||
|
||||
struct ast *ast = get_ast(&ctx);
|
||||
cr_assert_null(ast,
|
||||
"get_ast should return NULL when parser is not initialized");
|
||||
}
|
||||
|
||||
Test(parser, get_ast_closed)
|
||||
{
|
||||
parser_init();
|
||||
parser_close();
|
||||
|
||||
struct lexer_context ctx;
|
||||
init_lexer_ctx(&ctx);
|
||||
|
||||
struct ast *ast = get_ast(&ctx);
|
||||
cr_assert_null(ast, "get_ast should return NULL when parser is closed");
|
||||
}
|
||||
|
||||
Test(parser, get_ast_null_ctx)
|
||||
{
|
||||
parser_init();
|
||||
struct ast *ast = get_ast(NULL);
|
||||
cr_assert_null(ast, "get_ast should return NULL when ctx is NULL");
|
||||
parser_close();
|
||||
}
|
||||
|
||||
Test(parser, get_ast_simple_cmd)
|
||||
{
|
||||
char command[] = "echo hello";
|
||||
struct iob_context iob_ctx = { IOB_MODE_CMD, command };
|
||||
iob_init(&iob_ctx);
|
||||
|
||||
struct lexer_context ctx;
|
||||
init_lexer_ctx(&ctx);
|
||||
|
||||
parser_init();
|
||||
|
||||
struct ast *ast = get_ast(&ctx);
|
||||
|
||||
cr_assert_not_null(ast,
|
||||
"get_ast should return a valid AST for simple command");
|
||||
cr_assert(ast_is_command(ast), "AST root should be a command");
|
||||
|
||||
ast_free(&ast);
|
||||
parser_close();
|
||||
iob_close();
|
||||
}
|
||||
|
||||
Test(parser, get_ast_eof)
|
||||
{
|
||||
char command[] = "";
|
||||
struct iob_context iob_ctx = { IOB_MODE_CMD, command };
|
||||
iob_init(&iob_ctx);
|
||||
|
||||
struct lexer_context ctx;
|
||||
init_lexer_ctx(&ctx);
|
||||
|
||||
parser_init();
|
||||
|
||||
struct ast *ast = get_ast(&ctx);
|
||||
|
||||
cr_assert_not_null(ast,
|
||||
"get_ast should return AST_END node on empty input");
|
||||
cr_assert_eq(ast->type, AST_END, "AST type should be AST_END");
|
||||
|
||||
ast_free(&ast);
|
||||
parser_close();
|
||||
iob_close();
|
||||
}
|
||||
|
||||
Test(parser, get_ast_syntax_error)
|
||||
{
|
||||
char command[] = "if true; then"; // Missing fi
|
||||
struct iob_context iob_ctx = { IOB_MODE_CMD, command };
|
||||
iob_init(&iob_ctx);
|
||||
|
||||
struct lexer_context ctx;
|
||||
init_lexer_ctx(&ctx);
|
||||
|
||||
parser_init();
|
||||
|
||||
struct ast *ast = get_ast(&ctx);
|
||||
|
||||
cr_assert_null(ast, "get_ast should return NULL on syntax error");
|
||||
|
||||
parser_close();
|
||||
iob_close();
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ Test(utils_args, basic_command)
|
|||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
// cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.verbose == false);
|
||||
cr_expect(options.type == INPUT_CMD);
|
||||
cr_expect(eq(options.input_source, "echo Hello, World!"));
|
||||
|
|
@ -30,16 +30,14 @@ Test(utils_args, basic_command_with_flags)
|
|||
{
|
||||
int argc = 5;
|
||||
struct args_options options;
|
||||
/* char *input[] = { "program", "--pretty-print", "-c", "echo Hello,
|
||||
World!",
|
||||
"--verbose" };*/
|
||||
char *input[] = { "program", "-c", "echo Hello, World!", "--verbose" };
|
||||
char *input[] = { "program", "--pretty-print", "-c", "echo Hello, World!",
|
||||
"--verbose" };
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
// cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.verbose == true);
|
||||
cr_expect(options.type == INPUT_CMD);
|
||||
cr_expect(eq(options.input_source, "echo Hello, World!"));
|
||||
|
|
@ -56,7 +54,7 @@ Test(utils_args, basic_file_input)
|
|||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
// cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.verbose == false);
|
||||
cr_expect(options.type == INPUT_FILE);
|
||||
cr_expect(eq(options.input_source, "input.txt"));
|
||||
|
|
@ -67,15 +65,13 @@ Test(utils_args, basic_file_input_with_flags)
|
|||
{
|
||||
int argc = 4;
|
||||
struct args_options options;
|
||||
// char *input[] = { "program", "--verbose", "input.txt", "--pretty-print"
|
||||
// };
|
||||
char *input[] = { "program", "--verbose", "input.txt" };
|
||||
char *input[] = { "program", "--verbose", "input.txt", "--pretty-print" };
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
// cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.verbose == true);
|
||||
cr_expect(options.type == INPUT_FILE);
|
||||
cr_expect(eq(options.input_source, "input.txt"));
|
||||
|
|
@ -92,7 +88,7 @@ Test(utils_args, basic_stdin_input)
|
|||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
// cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.verbose == false);
|
||||
cr_expect(options.type == INPUT_STDIN);
|
||||
cr_expect(options.input_source == NULL);
|
||||
|
|
@ -103,14 +99,13 @@ Test(utils_args, pretty_print_and_verbose_flags)
|
|||
{
|
||||
int argc = 3;
|
||||
struct args_options options;
|
||||
// char *input[] = { "program", "--pretty-print", "--verbose" };
|
||||
char *input[] = { "program", "--verbose" };
|
||||
char *input[] = { "program", "--pretty-print", "--verbose" };
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
// cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.verbose == true);
|
||||
cr_expect(options.type == INPUT_STDIN);
|
||||
cr_expect(options.input_source == NULL);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue