Compare commits

..

No commits in common. "dev" and "exec-exit" have entirely different histories.

23 changed files with 151 additions and 548 deletions

View file

@ -1,12 +1,11 @@
# 42sh - A POSIX shell with a bad name # 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. 42sh is a shcool project aiming to implement a POSIX compliant shell in C.
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.
## Getting started ## Getting started
TODO
### Build ### Build
run this command: run this command:
`autoreconf --force --verbose --install` `autoreconf --force --verbose --install`
@ -17,43 +16,27 @@ run this command:
then: then:
`make` `make`
#### Build with ASan #### asan
run this command: run this command:
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'` `./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'`
or for MacOS (Jean Here): or for MacOS (Jean Here):
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'` `./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'`
then: then:
`make check` `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 ## Authors
- Guillem George
- Matteo Flebus - Matteo Flebus
- Jean Herail - Jean Herail
- William Valenduc - William Valenduc
- Guillem George
## Project status
WIP
## TODO
# Autotools
implement functions in all .c files to see if everything compiles.

View file

@ -18,9 +18,9 @@ bin_PROGRAMS = 42sh
parser/libparser.a \ parser/libparser.a \
lexer/liblexer.a \ lexer/liblexer.a \
io_backend/libio_backend.a \ io_backend/libio_backend.a \
utils/libutils.a \
execution/libexecution.a \ execution/libexecution.a \
expansion/libexpansion.a expansion/libexpansion.a \
utils/libutils.a
# ================ TESTS ================ # ================ TESTS ================

View file

@ -46,9 +46,6 @@ int execution(struct ast *ast, struct hash_map *vars)
case AST_LOOP: case AST_LOOP:
res = exec_ast_loop(ast_get_loop(ast), vars); res = exec_ast_loop(ast_get_loop(ast), vars);
break; break;
case AST_SUBSHELL:
res = exec_ast_subshell(ast_get_subshell(ast), vars);
break;
default: default:
res = 127; res = 127;
break; break;

View file

@ -151,7 +151,6 @@ static int try_builtin(char **argv, struct hash_map *vars);
static int builtin_break(char **argv); static int builtin_break(char **argv);
static int builtin_continue(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) static int exec_assignment(struct list *assignment_list, struct hash_map *vars)
{ {
@ -165,10 +164,6 @@ static int exec_assignment(struct list *assignment_list, struct hash_map *vars)
struct ast_assignment *assignment = struct ast_assignment *assignment =
ast_get_assignment(assignment_list->data); ast_get_assignment(assignment_list->data);
if (assignment->global)
{
setenv(assignment->name, assignment->value, 1);
}
set_var_copy(vars, assignment->name, assignment->value); set_var_copy(vars, assignment->name, assignment->value);
assignment_list = assignment_list->next; assignment_list = assignment_list->next;
} }
@ -253,33 +248,6 @@ int exec_ast_if(struct ast_if *if_node, struct hash_map *vars)
} }
} }
int exec_ast_subshell(struct ast_subshell *subshell_node,
struct hash_map *vars)
{
pid_t pid = fork();
if (pid < 0)
{
perror("fork");
return 1;
}
if (pid == 0)
{
int res = execution(subshell_node->child, vars);
_exit(res);
}
int status = 0;
waitpid(pid, &status, 0);
if (WIFEXITED(status))
{
return WEXITSTATUS(status);
}
return 1;
}
int exec_ast_list(struct ast_list *list_node, struct hash_map *vars) int exec_ast_list(struct ast_list *list_node, struct hash_map *vars)
{ {
struct list *cur = list_node->children; struct list *cur = list_node->children;
@ -548,23 +516,6 @@ static int builtin_cd(char **argv, struct hash_map *vars)
return 0; 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 * @brief Tries to execute a builtin command if the command matches a builtin
* *
@ -578,8 +529,6 @@ static int try_builtin(char **argv, struct hash_map *vars)
if (strcmp(argv[0], "echo") == 0) if (strcmp(argv[0], "echo") == 0)
return builtin_echo(argv); return builtin_echo(argv);
if (strcmp(argv[0], "unset") == 0)
return builtin_unset(argv, vars);
if (strcmp(argv[0], "true") == 0) if (strcmp(argv[0], "true") == 0)
return builtin_true(argv); return builtin_true(argv);
if (strcmp(argv[0], "false") == 0) if (strcmp(argv[0], "false") == 0)

View file

@ -14,8 +14,6 @@ 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_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_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_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); void unset_all_redir(struct list *redir_list);
#endif // EXECUTION_HELPERS_H #endif // EXECUTION_HELPERS_H

View file

@ -1,6 +1,4 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include "expansion.h"
#include <ctype.h> #include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@ -117,40 +115,6 @@ static bool expand_var(char **str, size_t pos, const struct hash_map *vars)
return false; 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) bool expand(struct ast_command *command, const struct hash_map *vars)
{ {
if (command == NULL) if (command == NULL)
@ -158,42 +122,34 @@ bool expand(struct ast_command *command, const struct hash_map *vars)
char *str; char *str;
size_t len; size_t len;
enum quote_state quotes; bool in_quotes;
struct list *l = command->command; struct list *l = command->command;
while (l != NULL) while (l != NULL)
{ {
quotes = NO_QUOTE; in_quotes = false;
str = (char *)l->data; str = (char *)l->data;
len = strlen(str); len = strlen(str);
for (size_t i = 0; str[i] != 0; i++) for (size_t i = 0; str[i] != 0; i++)
{ {
if (str[i] == '\'' || str[i] == '\"') if (str[i] == '\'')
{ {
if (quotes == NO_QUOTE) // remove single quote
{ in_quotes = !in_quotes;
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
memmove(str + i, str + i + 1, strlen(str + i + 1) + 1); memmove(str + i, str + i + 1, strlen(str + i + 1) + 1);
i--; i--;
} }
else if (quotes == SINGLE_QUOTE) else if (in_quotes)
{ {
continue; // do nothing 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])) else if (str[i] == '$' && str[i + 1] != 0 && !isspace(str[i + 1]))
{ {
// variable expansion // variable expansion
@ -205,20 +161,21 @@ bool expand(struct ast_command *command, const struct hash_map *vars)
} }
} }
// if (quotes != NO_QUOTE) if (in_quotes)
// { {
// // error: quote not closed // error: quote not closed
// fprintf(stderr, "Error: quote not closed in string: %s\n", str); fprintf(stderr, "Error: quote not closed in string: %s\n", str);
// return false; return false;
// } }
if (len != strlen(str)) if (len != strlen(str))
{ {
char *new_str = realloc(str, strlen(str) + 1); char *new_str = realloc(str, strlen(str) + 1);
if (new_str == NULL) if (new_str == NULL)
{
// error: realloc fail // error: realloc fail
return false; return false;
}
l->data = new_str; l->data = new_str;
} }

View file

@ -7,13 +7,6 @@
#include "../utils/ast/ast.h" #include "../utils/ast/ast.h"
#include "../utils/hash_map/hash_map.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 '$'. * Parse a variable from a string starting with '$'.
* @param str The input string starting with '$'. It must start 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); 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. * Expand variables in an AST command using the provided variable map.
* @param command The AST command to expand. * @param command The AST command to expand.

View file

@ -76,7 +76,7 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
tok->type = TOKEN_FOR; tok->type = TOKEN_FOR;
else if (strncmp(begin, "while", size) == 0 && size == 5) else if (strncmp(begin, "while", size) == 0 && size == 5)
tok->type = TOKEN_WHILE; 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; tok->type = TOKEN_UNTIL;
else if (strncmp(begin, "do", size) == 0 && size == 2) else if (strncmp(begin, "do", size) == 0 && size == 2)
tok->type = TOKEN_DO; tok->type = TOKEN_DO;

View file

@ -7,9 +7,70 @@
#include "lexer/lexer.h" #include "lexer/lexer.h"
#include "parser/parser.h" #include "parser/parser.h"
#include "utils/args/args.h" #include "utils/args/args.h"
#include "utils/main_loop/main_loop.h"
#include "utils/vars/vars.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, struct lexer_context *ctx)
{
hash_map_free(vars);
destroy_lexer_context(ctx);
return ERR_INPUT_PROCESSING;
}
static int main_loop(struct lexer_context *ctx, 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);
// 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);
parser_close();
hash_map_free(&vars);
destroy_lexer_context(ctx);
return return_code;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -56,16 +117,7 @@ int main(int argc, char **argv)
// init lexer context // init lexer context
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context)); struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
// init parser
if (!parser_init())
{
perror("parser initialization failed.");
return err_input(&vars, ctx);
}
return_code = main_loop(ctx, vars); return_code = main_loop(ctx, vars);
parser_close();
return return_code; return return_code;
} }

View file

@ -220,18 +220,18 @@ struct ast *parse_while(struct lexer_context *ctx)
} }
POP_TOKEN(); POP_TOKEN();
return parse_loop(ctx, false); return parse_loop(ctx, true);
} }
struct ast *parse_until(struct lexer_context *ctx) struct ast *parse_until(struct lexer_context *ctx)
{ {
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
// 'until' // 'while'
if (token->type != TOKEN_UNTIL) if (token->type != TOKEN_UNTIL)
{ {
perror( 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; return NULL;
} }
POP_TOKEN(); POP_TOKEN();

View file

@ -372,45 +372,28 @@ struct ast *parse_shell_command(struct lexer_context *ctx)
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
struct ast *result = NULL; struct ast *result = NULL;
// '{' // Grouping
if (token->type == TOKEN_LEFT_BRACKET) // '(' or '{'
if (token->type == TOKEN_LEFT_BRACKET || token->type == TOKEN_LEFT_PAREN)
{ {
POP_TOKEN(); POP_TOKEN();
result = parse_compound_list(ctx); result = parse_compound_list(ctx);
if (result == NULL) if (result == NULL)
return NULL; return NULL;
// '}' // ')' or '}'
token = PEEK_TOKEN(); token = PEEK_TOKEN();
if (token->type == TOKEN_LEFT_BRACKET) if (token->type == TOKEN_LEFT_BRACKET
|| token->type == TOKEN_LEFT_PAREN)
{ {
ast_free(&result); ast_free(&result);
perror("Syntax error: bracket mismatch"); perror("Syntax error: bracket/parenthesis mismatch");
return NULL; return NULL;
} }
POP_TOKEN(); POP_TOKEN();
return result; 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)) else if (is_first(*token, RULE_IF))
{ {
return parse_if_rule(ctx); return parse_if_rule(ctx);

View file

@ -20,9 +20,7 @@ libutils_a_SOURCES = \
ast/ast_loop.c \ ast/ast_loop.c \
args/args.c \ args/args.c \
vars/vars.c \ vars/vars.c \
main_loop/main_loop.c \
ast/ast_assignment.c \ ast/ast_assignment.c \
ast/ast_subshell.c \
ast/ast_function.c ast/ast_function.c
libutils_a_CPPFLAGS = -I$(top_srcdir)/src libutils_a_CPPFLAGS = -I$(top_srcdir)/src

View file

@ -11,7 +11,8 @@ void ast_free(struct ast **node)
{ {
if (node == NULL || *node == NULL) if (node == NULL || *node == NULL)
{ {
fprintf(stderr, fprintf(
stderr,
"WARNING: Internal error: failed to free AST node (NULL argument)"); "WARNING: Internal error: failed to free AST node (NULL argument)");
return; return;
} }
@ -51,16 +52,14 @@ void ast_free(struct ast **node)
case AST_FUNCTION: case AST_FUNCTION:
ast_free_function(ast_get_function(*node)); ast_free_function(ast_get_function(*node));
break; break;
case AST_SUBSHELL:
ast_free_subshell(ast_get_subshell(*node));
break;
case AST_VOID: case AST_VOID:
case AST_END: case AST_END:
break; break;
default: default:
fprintf(stderr, "WARNING: Internal error:" fprintf(stderr,
" failed to free an AST node (Unknown type)"); "WARNING: Internal error: failed to free an AST node (Unknown "
"type)");
return; return;
} }

View file

@ -15,6 +15,5 @@
#include "ast_redir.h" #include "ast_redir.h"
#include "ast_void.h" #include "ast_void.h"
#include "ast_word.h" #include "ast_word.h"
#include "ast_subshell.h"
#endif /* ! AST_H */ #endif /* ! AST_H */

View file

@ -18,8 +18,7 @@ enum ast_type
AST_NEG, AST_NEG,
AST_LOOP, AST_LOOP,
AST_ASSIGNMENT, AST_ASSIGNMENT,
AST_FUNCTION, AST_FUNCTION
AST_SUBSHELL
}; };
struct ast struct ast

View file

@ -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);
}

View file

@ -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 */

View file

@ -7,8 +7,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../ast/ast.h"
/* /*
** Hash the key using FNV-1a 32 bits hash algorithm. ** 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; *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 *hash_map_init(size_t size)
{ {
struct hash_map *p = malloc(sizeof(struct hash_map)); 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 hash_map_foreach(struct hash_map *hash_map,
void (*fn)(const char *, const void *)) void (*fn)(const char *, const void *))
{ {

View file

@ -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;
}
}

View file

@ -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 */

View file

@ -1,6 +1,7 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <criterion/criterion.h> #include <criterion/criterion.h>
#include <criterion/new/assert.h> #include <criterion/new/assert.h>
#include <criterion/redirect.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -16,7 +17,7 @@ Test(expand, no_expansion)
char str[] = "echo something"; char str[] = "echo something";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
bool ret = expand(ast_command, NULL); bool ret = expand(ast_command, NULL);
@ -31,7 +32,7 @@ Test(expand, single_quotes_no_expansion)
char str[] = "echo '$VAR'"; char str[] = "echo '$VAR'";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -50,7 +51,7 @@ Test(expand, single_dollar)
char str[] = "echo $ sign"; char str[] = "echo $ sign";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -69,7 +70,7 @@ Test(expand, empty_braces_no_expansion)
char str[] = "echo ${}"; char str[] = "echo ${}";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -86,7 +87,7 @@ Test(expand, basic_expansion)
char str[] = "echo $VAR"; char str[] = "echo $VAR";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -105,7 +106,7 @@ Test(expand, multiple_expansion)
char str[] = "echo $VAR1 $VAR2 ${VAR3}"; char str[] = "echo $VAR1 $VAR2 ${VAR3}";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -127,7 +128,7 @@ Test(expand, env_variable)
char str[] = "echo $MY_ENV_VAR"; char str[] = "echo $MY_ENV_VAR";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
setenv("MY_ENV_VAR", "environment", 0); setenv("MY_ENV_VAR", "environment", 0);
@ -144,7 +145,7 @@ Test(expand, undefined_variable)
char str[] = "echo $UNDEFINED"; char str[] = "echo $UNDEFINED";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -162,7 +163,7 @@ Test(expand, nested_expansion)
char str[] = "echo $B"; char str[] = "echo $B";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -177,34 +178,34 @@ Test(expand, nested_expansion)
hash_map_free(&vars); hash_map_free(&vars);
} }
// Test(expand, mixed_quotes_expansion) Test(expand, mixed_quotes_expansion)
// { {
// char str[] = "echo \"$VAR1 and '$VAR2'\""; char str[] = "echo \"$VAR1 and '$VAR2'\"";
// char *str_heap = strdup(str); char *str_heap = strdup(str);
// struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
// struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
// set_var_copy(vars, "VAR1", "expanded"); set_var_copy(vars, "VAR1", "expanded");
// set_var_copy(vars, "VAR2", "not_expanded"); set_var_copy(vars, "VAR2", "not_expanded");
// bool ret = expand(ast_command, vars); bool ret = expand(ast_command, vars);
// cr_expect(ret, "expansion failed with %s", str); cr_expect(ret, "expansion failed with %s", str);
// cr_expect_str_eq((char *)ast_command->command->data, cr_expect_str_eq((char *)ast_command->command->data,
// "echo \"expanded and $VAR2\"", "echo \"expanded and $VAR2\"",
// "Variable in double quotes should expand, while variable " "Variable in double quotes should expand, while variable "
// "in single quotes should not"); "in single quotes should not");
// ast_free(&ast); ast_free(&ast);
// hash_map_free(&vars); hash_map_free(&vars);
// } }
Test(expand, adjacent_variables) Test(expand, adjacent_variables)
{ {
char str[] = "echo $VAR1$VAR2"; char str[] = "echo $VAR1$VAR2";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -224,7 +225,7 @@ Test(expand, random)
char str[] = "$RANDOM"; char str[] = "$RANDOM";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
bool ret = expand(ast_command, NULL); bool ret = expand(ast_command, NULL);
@ -240,7 +241,7 @@ Test(expand, pid)
char str[] = "$$"; char str[] = "$$";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();
@ -258,7 +259,7 @@ Test(expand, default_last_exit_code)
char str[] = "$?"; char str[] = "$?";
char *str_heap = strdup(str); char *str_heap = strdup(str);
struct list *list = list_append(NULL, str_heap); 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 ast_command *ast_command = ast_get_command(ast);
struct hash_map *vars = vars_init(); struct hash_map *vars = vars_init();

View file

@ -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);
}

View file

@ -1,5 +1,6 @@
#include <criterion/criterion.h> #include <criterion/criterion.h>
#include <criterion/new/assert.h> #include <criterion/new/assert.h>
#include <criterion/redirect.h>
#include "../../../src/expansion/expansion.h" #include "../../../src/expansion/expansion.h"