64 Commits

Author SHA1 Message Date
marcnava-42cursus
ba40670ace Fixed variable expansion errors 2026-02-14 13:33:44 +01:00
marcnava-42cursus
dd6101edec A POR TODAAS 2026-02-14 06:14:59 +01:00
e2b734cf0c save commit 2026-02-14 05:48:18 +01:00
32b3bd72b5 fix: prompt was not being requested to hashmap 2026-02-14 02:05:49 +01:00
c95703b42b update: replaced set_env by set_var in export builtin 2026-02-14 02:02:52 +01:00
f4cfae1107 feat: added internal variables 2026-02-14 02:01:10 +01:00
marcnava-42cursus
6453abfda3 Fixed all norme from lexer, parser. minishell and builtins 2026-02-14 00:02:03 +01:00
marcnava-42cursus
7862f3e131 chore: stop tracking minishell-codex 2026-02-13 22:17:55 +01:00
marcnava-42cursus
637391470b Fixed exit using old code + updates 2026-02-13 22:05:25 +01:00
marcnava-42cursus
217505e3b0 Restored exit but adding isatty 2026-02-13 21:54:00 +01:00
5c33afb20a update: removed minishell_tester submodule 2026-02-13 21:05:30 +01:00
3f17f7789c update: added isatty support and prompt usage 2026-02-13 20:18:26 +01:00
5df1520224 chore: added testers 2026-02-13 18:42:54 +01:00
marcnava-42cursus
c166d0f77a feat: Added signals
Added functionality of the signals ctrl+d, ctrl+c and ctr+\
2026-02-13 18:11:48 +01:00
3e31447073 fix: fixed crash on non-existent variables
index was being moved to -1 if token was only the variable, this
probably works too for variables without value
2026-02-13 16:42:50 +01:00
3ead1ad547 chore: updated docs to v10.0 2026-02-13 16:38:36 +01:00
c99e0a17a3 update: renamed variable 2026-02-13 15:08:41 +01:00
dc772d10c2 fix: syntax error on pipe at start was not being detected 2026-02-13 13:48:28 +01:00
e5d7fcf050 fix: fixed leak on variable expansion 2026-02-13 13:33:32 +01:00
d248819dfe fix: commands list was not being freed if the command line ended in pipe
error was reported but commands were being executed as the list was not
being freed
2026-02-13 09:29:24 +01:00
8f92cf269c fix: fixed crash when reading variables with underscore 2026-02-13 08:20:27 +01:00
8353d3f64f chore: removed unused and test files 2026-02-13 08:18:25 +01:00
marcnava-42cursus
4756edb727 Added errors file after merge 2026-02-12 22:03:59 +01:00
d06e49274b Merge branch 'feature/executor-gl' into solo 2026-02-12 21:26:12 +01:00
83553a57d5 feat: added variable expansion
quote removal is not implemented yet in this commit
2026-02-12 21:18:50 +01:00
marcnava-42cursus
ff5edf543f Updated AGENTS file with redirections options 2026-02-12 20:32:12 +01:00
marcnava-42cursus
518ec87e60 Deleted tester 2026-02-12 20:28:29 +01:00
marcnava-42cursus
6bc2eab19b feat: Redirections applied
Now redirections work.
> redirects to a file
< reads the content of a file and set it to the input of a command
>> append the output of a command into a file
2026-02-12 20:28:05 +01:00
marcnava-42cursus
3c7ee5b161 Merge branch 'feature/builtins-gl' into feature/executor-gl
# Conflicts:
#	src/parser/parser.c
2026-02-12 18:36:05 +01:00
marcnava-42cursus
d1aad3ed20 Merge branch 'solo' into feature/executor-gl
# Conflicts:
#	src/parser/parser.c
2026-02-12 18:34:26 +01:00
8882929423 update: command list is now freed completely if any error ocurrs 2026-02-12 03:09:45 +01:00
843bdc0061 fix: invalid syntax error cause shown when there was no token following a redirection 2026-02-12 02:57:15 +01:00
ac4965ca6d fix: fixed multiple syntax error logging when redirection value is not a word
pointer is moved to the last position to avoid double logging
2026-02-11 10:12:20 +01:00
9d457f1040 norminette: added space after keyword 2026-02-11 08:37:02 +01:00
6b3d2ba5e0 fix: fixed segfault caused by invalid read
NULL value caused the error logger to crash
2026-02-11 08:36:05 +01:00
c493979a18 update: parser now uses tokens from lexer
fixes pending:
 - some functions are longer than norminette allows
 - find solution to a list of commands being returned, even though a
syntax error is found when processing tokens (maybe delegate some work
to the lexer and return only a syntax-valid list?)
2026-02-11 02:51:30 +01:00
marcnava-42cursus
df6ed1c5cc Executor working, need to validate with the parser fixed, all norme fixed 2026-02-11 02:29:45 +01:00
marcnava-42cursus
e02613253a Fixed command exit status and exec bugs, read relative and absolute paths and correctly resolved 2026-02-11 02:17:40 +01:00
marcnava-42cursus
ae578867b2 Moved find command in path to executor, fixed env save code 2026-02-11 02:09:12 +01:00
marcnava-42cursus
328737c557 Updated AGENTS with info of the current (this branch) parser status, need to be updated again when parser works correctly 2026-02-11 01:38:41 +01:00
1715f2dd40 typo: fixed typo 2026-02-10 12:34:56 +01:00
63dd69b01c update: removed const modifier on token value
following steps on the parsing process can change its content
2026-02-10 09:24:18 +01:00
609a644fa9 update: token is now stored as value of the token instead of null
this change is made so that it is easier to print an error message when
invalid syntax
2026-02-10 09:20:46 +01:00
3d97d6506a fix: removed putendl on malloc 2026-02-10 08:54:11 +01:00
1f0f38e42f fix: removed putendl on free 2026-02-10 08:52:24 +01:00
97d8a6838f norminette: fixed line too long 2026-02-10 08:45:31 +01:00
74111e3da5 update: removed unused macros
not used anymore by lexer
2026-02-09 23:12:04 +01:00
6ad68a8752 update: added lexer 2026-02-09 23:11:33 +01:00
marcnava-42cursus
d39eca2c94 Fixed compilation 2026-02-09 22:48:12 +01:00
marcnava-42cursus
084fa4759c Created allowed.txt to track allowed functions for minishell and updated AGENTS.md to tell codex to not use any forbidden functions, also updated AGENTS.md to add all builtins changes 2026-02-09 22:37:09 +01:00
marcnava-42cursus
778e0c0481 Builtins fixed
The builtins wasnt protected, now all data received is protected, the hashmap addition is protected and added functionality of env, export and unset (not implemented in this version). Added fixed details documentation in docs/builtins_fixes.md generated by codex and created tests/builtins_edge_cases.sh to test all the builtins to work correctly
2026-02-09 22:08:45 +01:00
280fa51f94 save commit 2026-02-09 20:47:43 +01:00
e983f7fe64 update: builtins hashmap initialized and moved env hashmap initialization to set_envp() 2025-12-02 09:13:59 +01:00
b1cf1d8560 update: integrated hashmaps in builtins, and replaced command calls by builtins 2025-12-02 09:11:01 +01:00
dce51960b1 fix: fixed ignored result compilation errro 2025-12-01 16:31:02 +01:00
e9a07a6bfb fix: fixed compilation error 2025-12-01 14:12:15 +01:00
a9ba8492a2 fix: executor.c/handle_child was not using the real envp 2025-12-01 14:09:32 +01:00
83d006b40d fix: Makefile's mandatory rule was not cloning ft_args 2025-12-01 14:06:33 +01:00
027fd2ac5e Merge branch 'feature/builtins' into develop 2025-12-01 14:01:24 +01:00
30ce96284a fix: get_env call was not using minishell struct 2025-12-01 14:01:08 +01:00
956505c06a Merge branch 'feature/builtins' into develop 2025-12-01 14:00:13 +01:00
212c4e3fee update: updated getenv calls by get_env 2025-12-01 13:59:52 +01:00
50736d533d update: changed getenv calls by get_env 2025-12-01 13:52:28 +01:00
daad208a00 update: added hashmap functionality to environment variables 2025-12-01 13:47:17 +01:00
64 changed files with 31692 additions and 276 deletions

2
.gitignore vendored
View File

@@ -69,3 +69,5 @@ dkms.conf
# debug information files
*.dwo
minishell-codex/

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "tests/42_minishell_tester"]
path = tests/42_minishell_tester
url = https://github.com/zstenger93/42_minishell_tester.git

72
AGENTS.md Normal file
View File

@@ -0,0 +1,72 @@
# Repository Guidelines
## Project Structure & Module Organization
- `src/` holds the shell implementation, with submodules for `builtins/`, `executor/`, `parser/`, and `variables/`.
- `src/builtins/` currently includes: `cd`, `echo`, `env`, `exit`, `export`, `pwd`, `unset`.
- `src/executor/` includes pipeline/process orchestration plus file redirections in `redirections.c`.
- `include/` contains public headers used across modules.
- `lib/` is populated at build time with third-party 42 libraries (libft, get_next_line, ft_printf, ft_args).
- `docs/` stores project references and manual test notes (see `docs/tests.md`).
- `build/` is the object output directory generated by `make`.
## Build, Test, and Development Commands
- `make` or `make all`: build `minishell` (auto-clones required libs into `lib/`).
- `make clean`: remove objects in `build/`.
- `make fclean`: remove objects and the `minishell` binary (also cleans libs).
- `make re`: full rebuild.
- `./minishell`: run locally after a build.
- `make DEBUG=lldb` or `make DEBUG=valgrind` or `make DEBUG=address`: rebuild with debug/ASan-friendly flags.
## Coding Style & Naming Conventions
- The codebase follows 42 **Norminette v4** rules. Run `norminette *.c *.h` (or on specific files) before submitting changes.
- Keep file names lowercase with underscores (e.g., `src/builtins/echo/echo.c`).
- Keep headers in `include/` and expose only what modules need.
- Before adding or changing code, check `allowed.txt` to know which functions are permitted.
- Any function not listed in `allowed.txt` is not allowed in this project.
## Parser & Lexer Functionality (Current `src/parser`)
- Runtime entrypoint is `parse(line, minishell)` from `src/minishell.c` (`readline -> parse -> execute`).
- `parse` calls `lex(line)` and then converts token lists to `t_command` nodes with `parse_tokens`.
- `command_new` builds one command from tokens up to `TOKEN_PIPE`.
- `words_add` stores consecutive `TOKEN_WORD` tokens in `command->argv` and increments `argc`.
- `expand_envs` is currently a TODO (no `$VAR` expansion is applied in parser stage).
- Redirection tokens are converted into `t_redirection` and stored in `t_command.redirections`; heredocs are stored in `t_command.heredocs`.
- Path resolution is handled in executor (`executor_resolve_command_path`) before `execve`.
- `src/parser/lexer.c` provides a separate lexer (`lex`) that tokenizes into `TOKEN_WORD`, `TOKEN_PIPE`, `TOKEN_REDIRECT_IN`, `TOKEN_REDIRECT_OUT`, `TOKEN_APPEND`, and `TOKEN_HEREDOC`.
- The lexer tracks single/double quote context so metacharacters inside quotes remain part of words.
- Meta runs are read as contiguous chunks in `read_token` (for example, repeated `|`/`<`/`>` are captured as one token value).
- Current parser flow consumes lexer output directly.
## Executor Redirections (Current `src/executor`)
- File redirections are applied in `src/executor/redirections.c` via `open` + `dup2` before command execution.
- Supported file redirections: input `<`, output truncate `>`, output append `>>`.
- Redirections are applied for both forked commands (child path) and single builtins executed in parent.
- Parent-builtin redirections save/restore `STDIN_FILENO` and `STDOUT_FILENO` after builtin execution.
## Parser & Lexer Known Gaps
- Heredoc tokens are parsed and stored, but runtime heredoc execution/input feeding is still pending in executor.
- No explicit unmatched-quote syntax error handling is implemented in parser/lexer path.
## Testing Guidelines
- There is no automated test runner. Use manual checks in `docs/tests.md` and basic shell behavior checks (pipes, redirects, builtins).
- A local builtin edge-case script exists at `tests/builtins_edge_cases.sh` (expects a compiled `./minishell`).
- When debugging memory issues, run under valgrind and use the suppression file in `valgrind/readline.supp`.
## Builtins Status
- `cd`: handles `HOME` fallback, `cd -` via `OLDPWD`, updates `PWD`/`OLDPWD`, and returns failure on invalid usage (`too many arguments`, missing `HOME`/`OLDPWD`).
- `echo`: supports repeated `-n` flags (`-n`, `-nnn`) and prints remaining args preserving spaces between tokens.
- `env`: prints current environment as `KEY=VALUE`; returns failure when called with extra arguments.
- `exit`: validates numeric argument (with overflow checks), returns `1` on `too many arguments` without exiting, and exits with `2` on non-numeric argument.
- `export`: supports `NAME=VALUE` and `NAME` (stored as empty value), validates identifiers, and returns failure when any identifier is invalid.
- `pwd`: prints working directory using dynamic `getcwd` and returns failure if `getcwd` fails.
- `unset`: validates identifiers and removes matching variables from the environment map.
## Commit & Pull Request Guidelines
- Commit messages in this repo use a simple `type: summary` format (examples: `update: ...`, `fix: ...`). Keep summaries short and specific.
- For PRs, include:
1. What changed and why.
2. How to test (commands or manual steps).
3. Notes on any parser/executor/builtin behavior changes.
## Configuration Tips
- The project depends on `readline`; ensure your system has `libreadline-dev` or equivalent before building.

View File

@@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ #
# By: marcnava <marcnava@student.42madrid.com +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/07/30 20:22:21 by sede-san #+# #+# #
# Updated: 2025/11/01 19:30:37 by sede-san ### ########.fr #
# Updated: 2026/02/09 22:44:34 by marcnava ### ########.fr #
# #
# **************************************************************************** #
@@ -17,15 +17,15 @@ NAME = minishell
# ************************** Compilation variables *************************** #
CC = cc
CFLAGS = -Wall -Wextra -Werror
CFLAGS = -Wall -Wextra #-Werror
HEADERS = -I $(INCLUDE_PATH) $(LIBS_INCLUDE)
ifeq ($(DEBUG), lldb) # debug with LLDB
CFLAGS += -g3
CFLAGS = -g3
else ifeq ($(DEBUG), valgrind) # debug with valgrind
CFLAGS += -g3
CFLAGS = -g3
else ifeq ($(DEBUG), address) # use AdressSanitize
CFLAGS += -fsanitize=address -g3
CFLAGS = -fsanitize=address -g3
else # apply optimization flags if no debugging is being done
CFLAGS += -O3
endif
@@ -55,7 +55,7 @@ $(OBJS_PATH)/%.o: $(SRC_PATH)/%.c
all: mandatory
.PHONY: all
mandatory: libft get_next_line ft_printf $(NAME)
mandatory: libft get_next_line ft_printf ft_args $(NAME)
.PHONY: mandatory
$(NAME): $(OBJS)

177
allowed.txt Normal file
View File

@@ -0,0 +1,177 @@
[minishell_allowed]
readline
rl_clear_history
rl_on_new_line
rl_replace_line
rl_redisplay
add_history
printf
malloc
free
write
access
open
read
close
fork
wait
waitpid
wait3
wait4
signal
sigaction
sigemptyset
sigaddset
kill
exit
getcwd
chdir
stat
lstat
fstat
unlink
execve
dup
dup2
pipe
opendir
readdir
closedir
strerror
perror
isatty
ttyname
ttyslot
ioctl
getenv
tcsetattr
tcgetattr
tgetent
tgetflag
tgetnum
tgetstr
tgoto
tputs
[libft]
ft_atoi
ft_atoi_base
ft_atol
ft_bzero
ft_calloc
ft_cdlstadd_back
ft_cdlstadd_front
ft_cdlstclear
ft_cdlstdelone
ft_cdlstiter
ft_cdlstlast
ft_cdlstmap
ft_cdlstnew
ft_cdlstsize
ft_clstadd_back
ft_clstadd_front
ft_clstclear
ft_clstdelone
ft_clstiter
ft_clstlast
ft_clstmap
ft_clstnew
ft_clstsize
ft_dlstadd_back
ft_dlstadd_front
ft_dlstclear
ft_dlstdelone
ft_dlstiter
ft_dlstlast
ft_dlstmap
ft_dlstnew
ft_dlstsize
ft_eputchar
ft_eputendl
ft_eputnbr
ft_eputstr
ft_free
ft_free_split
ft_hashmap_clear
ft_hashmap_clear_keys
ft_hashmap_contains_key
ft_hashmap_entries
ft_hashmap_get
ft_hashmap_hashstr
ft_hashmap_new
ft_hashmap_put
ft_hashmap_remove
ft_hashmap_strcmp
ft_hashstr
ft_iabs
ft_imin
ft_isalnum
ft_isalpha
ft_isascii
ft_iscntrl
ft_isdigit
ft_islower
ft_isprint
ft_isspace
ft_isupper
ft_itoa
ft_itoa_base
ft_lstadd_back
ft_lstadd_front
ft_lstclear
ft_lstclear_nodes
ft_lstdelone
ft_lstiter
ft_lstlast
ft_lstmap
ft_lstnew
ft_lstsize
ft_ltoa
ft_memchr
ft_memcmp
ft_memcpy
ft_memmove
ft_memset
ft_nsplit
ft_pow
ft_putchar
ft_putchar_fd
ft_putendl
ft_putendl_fd
ft_putnbr
ft_putnbr_fd
ft_putstr
ft_putstr_fd
ft_realloc
ft_split
ft_strchr
ft_strcmp
ft_strdup
ft_strisnum
ft_striteri
ft_strjoin
ft_strlcat
ft_strlcpy
ft_strlen
ft_strmapi
ft_strncmp
ft_strncpy
ft_strnjoin
ft_strnstr
ft_strrchr
ft_strtrim
ft_substr
ft_swap
ft_tolower
ft_toupper
ft_uitoa
ft_uitoa_base
ft_ultoa_base
[get_next_line]
get_next_line
[ft_printf]
ft_eprintf
ft_fprintf
ft_printf

151
docs/builtins_fixes.md Normal file
View File

@@ -0,0 +1,151 @@
# Correcciones en Builtins
Este documento resume los fallos detectados en los builtins actuales y la
solucion aplicada en codigo.
## 1) Infraestructura de builtins (`include/builtins.h`, `src/builtins/builtins.c`)
### Fallo
- Uso inconsistente de tipos (`u_int8_t`/`unsigned char` frente a `uint8_t`).
- `set_builtins` no comprobaba fallos de registro por builtin (duplicado de
clave o insercion en hashmap), pudiendo dejar estado parcial.
- `is_builtin` podia dereferenciar punteros nulos.
### Por que fallaba
- `u_int8_t` no es el tipo estandar C99 y depende de plataforma/headers.
- Si falla una insercion, la tabla quedaba inicializada parcialmente sin
rollback.
- En errores de inicializacion, consultar `is_builtin` podia romper.
### Solucion
- Unificacion de firmas a `uint8_t`.
- Nuevo helper `register_builtin()` con validacion tras `ft_hashmap_put`.
- Si falla cualquier alta: limpieza de `minishell->builtins` y retorno de
error.
- Guardas nulas en `is_builtin`.
## 2) `cd` (`src/builtins/cd/cd.c`)
### Fallo
- `cd` con demasiados argumentos devolvia `2` (bash devuelve `1`).
- `cd` sin `HOME` acababa llamando a `chdir(NULL)`.
- El manejo de error usaba comprobaciones invertidas (`access`) y codigos
incorrectos.
- No se actualizaban `PWD` y `OLDPWD` tras `chdir` exitoso.
- `cd -` (usar `OLDPWD`) no estaba soportado.
### Por que fallaba
- Codigos de salida incompatibles con el comportamiento esperado del shell.
- `HOME` no definido no se controlaba antes del `chdir`.
- La logica de `access` estaba al reves y mezclaba condiciones.
- Variables de entorno del directorio quedaban desincronizadas.
- Faltaba resolver el caso especial de `-` hacia `OLDPWD`.
### Solucion
- Refactor en `resolve_cd_path()` para validar argumentos y `HOME`.
- Retorno `EXIT_FAILURE` en `too many arguments` y `HOME not set`.
- Error de `chdir` simplificado a `perror("minishell: cd")` + retorno `1`.
- Actualizacion de `OLDPWD` y `PWD` mediante `getcwd(NULL, 0)` + `set_env()`.
- Soporte de `cd -`: usa `OLDPWD`, valida `OLDPWD not set` e imprime el nuevo
directorio tras el cambio.
## 3) `exit` (`src/builtins/exit/exit.c`)
### Fallo
- Habia un `printf` de debug en ejecucion real.
- `exit <no_numerico>` devolvia `2` pero no cerraba el shell.
- `exit n m` devolvia `2`; en bash es `1` y no sale del shell.
- Validacion numerica basada en `ft_strisnum` sin control de overflow.
- Se mostraba `exit` incluso en contexto no interactivo.
### Por que fallaba
- Debug residual contamina salida.
- Semantica de `exit` incompleta respecto a bash.
- Valores fuera de rango podian tratarse como validos por conversion directa.
- Mensaje `exit` debe mostrarse solo en shell interactivo.
### Solucion
- Eliminado debug print.
- Nuevo flujo `resolve_exit_status()`:
- Sin argumentos: usa `msh->exit_status`.
- Argumento no numerico o fuera de `long`: mensaje
`numeric argument required`, `msh->exit = true`, estado `2`.
- Demasiados argumentos: mensaje de error y estado `1`, sin salir.
- Parser numerico propio (`get_uint8_from_num` + `has_overflow`) con soporte
de signo y control de overflow.
- `ft_eputendl("exit")` solo si `isatty(STDIN_FILENO)`.
## 4) `pwd` (`src/builtins/pwd/pwd.c`)
### Fallo
- Si `getcwd` fallaba, el builtin devolvia `EXIT_SUCCESS`.
- Uso de buffer fijo (`PATH_MAX`) menos robusto para rutas largas.
### Por que fallaba
- El shell reportaba exito aunque no pudiera obtener el directorio.
- Un buffer fijo puede truncar o fallar en escenarios de rutas profundas.
### Solucion
- Cambio a `getcwd(NULL, 0)` con memoria dinamica.
- Si falla, `perror("minishell: pwd")` y retorno `EXIT_FAILURE`.
- `free()` del buffer dinamico tras imprimir.
## 5) `echo` (`src/builtins/echo/echo.c`, `src/builtins/echo/echo_def.c`)
### Cambio aplicado
- Ajuste de tipos de retorno auxiliares a `uint8_t` para mantener consistencia
con `builtins.h`.
### Nota
- No se detectaron fallos funcionales criticos adicionales en la logica actual
de `echo` durante esta revision.
## 6) Builtins faltantes: `env`, `export`, `unset`
### Fallo
- Los builtins `env`, `export` y `unset` no estaban implementados ni
registrados en `set_builtins`.
### Por que fallaba
- Comandos basicos de shell no existian en la tabla de builtins.
- Cualquier prueba/flujo que dependiera de gestion de variables exportadas
fallaba (listar, crear y eliminar variables de entorno).
### Solucion
- Nuevos builtins implementados:
- `src/builtins/env/env.c`
- `src/builtins/export/export.c`
- `src/builtins/unset/unset.c`
- Registro en `src/builtins/builtins.c` (tabla ampliada de 4 a 7 entradas).
- Nuevos prototipos en `include/builtins.h`.
- Soporte de borrado real de entorno mediante `unset_env`:
- Declaracion en `include/core.h`
- Implementacion en `src/variables/environment_unset.c`
## 7) Comportamiento aplicado en los nuevos builtins
### `env`
- Si recibe argumentos, devuelve error (`minishell: env: too many arguments`)
y estado `1`.
- Sin argumentos, lista `KEY=VALUE` de las variables de entorno actuales.
### `export`
- `export NAME=VALUE`: crea/actualiza la variable.
- `export NAME`: crea/actualiza con valor vacio (`NAME=`) para que aparezca
en `env`.
- Identificadores invalidos (`1A=2`, etc.) devuelven error
`not a valid identifier` y estado `1`.
- Sin argumentos, reutiliza el listado de `env`.
### `unset`
- Elimina variables validas del entorno en memoria.
- Identificadores invalidos devuelven error
`not a valid identifier` y estado `1`.
- Si el identificador es valido y no existe, no falla (comportamiento shell).
## Validacion realizada
- `norminette` ejecutado sobre los archivos modificados: `OK`.
- Build completa no ejecutable en este entorno por falta de acceso de red al
clonado de librerias (`github.com`), por lo que no se pudo validar runtime
del binario en esta sesion.

View File

@@ -0,0 +1,77 @@
# Minishell - Resumen para defensa
Este documento es un guion breve para explicar el proyecto con claridad.
## 1. Que es minishell
- Un shell interactivo basico: lee comandos, los parsea y los ejecuta.
- Objetivo didactico: procesos, pipes, fds, señales y parsing.
## 2. Flujo general (en 20 segundos)
1. Prompt con readline.
2. Lexer divide la linea en tokens validos (respetando comillas).
3. Parser crea comandos, argv y redirecciones.
4. Expansion de variables ($VAR, $?).
5. Resolver PATH.
6. Ejecutar con fork/execve y pipes.
7. Actualizar exit status.
## 3. Puntos clave que suelen preguntar
### Comillas
- Comilla simple: todo literal, no expansion.
- Comilla doble: expansion de $VAR, pero no metacaracteres.
- Si una comilla no se cierra: error y no se ejecuta nada.
### Redirecciones
- <, >, >>, <<.
- Se resuelven antes de ejecutar el comando.
- Heredoc: lectura hasta delimitador, sin historial.
### Pipes
- Conecto stdout del comando i con stdin del comando i+1.
- Uso pipe() y dup2() en el child.
- El padre cierra fds que no usa.
### Builtins
- echo -n, cd, pwd, export, unset, env, exit.
- Si no hay pipeline, se ejecutan en el padre para afectar el estado.
- En pipeline, se ejecutan en el child.
### Variables
- $VAR: entorno.
- $?: ultimo exit status.
### Senales
- Una sola variable global para la senal.
- ctrl-C: nueva linea y prompt.
- ctrl-D: salir del shell.
- ctrl-\: no hace nada.
## 4. Ejemplos rapidos para demostrar
1. Pipes
- `ls | wc -l`
2. Redirecciones
- `echo hola > out.txt`
- `cat < out.txt`
3. Heredoc
- `cat << EOF`
- escribir lineas
- `EOF`
4. Expansion
- `echo $HOME`
- `echo $?`
## 5. Errores y robustez
- No ejecuto si hay errores de parseo.
- Mensajes con perror cuando fallan syscalls.
- exit_status siempre actualizado.
## 6. Mensajes finales recomendados
- "He separado parsing y ejecucion para que el codigo sea mantenible y
la defensa mas clara".
- "Sigo el comportamiento de bash como referencia".

Binary file not shown.

View File

@@ -0,0 +1,56 @@
# Errores detectados: parser, expansión y ejecutor
## Críticos
1. Segmentation fault al mezclar palabras y redirecciones en un mismo comando.
- Síntoma: `echo a > /tmp/x b` puede crashear.
- Referencias: `src/parser/parser.c:365`, `src/parser/parser.c:371`, `src/parser/parser.c:187`.
2. Bucle infinito al recibir EOF en modo no interactivo.
- Síntoma: con `printf 'echo ok\n' | ./minishell` entra en loop imprimiendo prompt.
- Referencias: `src/minishell.c:40`, `src/minishell.c:42`.
3. Parseo de pipes inválidos permite ejecución parcial o comandos inválidos.
- Síntoma: casos como `| echo x` o `echo hi |` no se bloquean correctamente.
- Referencias: `src/parser/parser.c:83`, `src/parser/parser.c:101`, `src/parser/parser.c:221`.
## Altos
4. Expansión de variables incompleta e incorrecta en casos especiales.
- Síntoma: `$FOO_BAR` y `$_X` fallan; `$?` y `$$` no se expanden correctamente.
- Referencias: `src/parser/parser.c:122`, `src/parser/parser.c:156`.
5. No se eliminan comillas tras parseo/expansión.
- Síntoma: `echo "$HOME"` imprime comillas literales.
- Referencias: `src/parser/lexer.c:161`, `src/parser/parser.c:142`.
6. No hay expansión en targets de redirección.
- Síntoma: `echo hi > $HOME_test` crea archivo literal `$HOME_test`.
- Referencias: `src/parser/parser.c:187`, `src/parser/parser.c:263`.
7. Heredoc parseado pero no ejecutado en runtime.
- Síntoma: se almacena en `heredocs` pero no se aplica en ejecución.
- Referencias: `src/parser/parser.c:397`, `src/executor/redirections.c:68`, `src/executor/process_helpers.c:110`.
8. Lexer acepta operadores inválidos (`||`, `>>>`, `><`) sin validación formal.
- Síntoma: tokenización por “runs” de metacaracteres produce entradas inválidas no rechazadas de forma robusta.
- Referencias: `src/parser/lexer.c:140`, `src/parser/lexer.c:77`.
9. Errores de sintaxis no siempre fijan `exit_status` correcto (debería ser `2`).
- Síntoma: algunos casos acaban con salida `0`.
- Referencias: `src/executor/executor.c:20`, `src/parser/parser.c:101`.
## Medios
10. Resolución de rutas explícitas reporta mal algunos errores de permisos/ejecución.
- Síntoma: ruta existente no ejecutable puede terminar como `command not found`.
- Referencias: `src/executor/path_resolver.c:57`, `src/executor/path_resolver.c:59`, `src/executor/command_exec.c:81`.
11. Check de puntero nulo defectuoso en el loop principal.
- Síntoma: `if (minishell == NULL)` desreferencia igualmente el puntero.
- Referencia: `src/minishell.c:35`.
12. Falta manejo explícito de comillas no cerradas.
- Síntoma: entrada con comillas abiertas no genera error sintáctico dedicado.
- Referencia: `src/parser/lexer.c:159`.

Binary file not shown.

292
docs/guia_obligatoria_es.md Normal file
View File

@@ -0,0 +1,292 @@
# Minishell - Guia de estructura y pasos (parte obligatoria)
Objetivo de este documento
- Proponer una estructura de archivos y datos.
- Describir el proceso paso a paso como tutorial.
- Servir de guion para defensa (explicaciones claras y ordenadas).
Este documento no implementa nada. Solo define el plan y el por que.
---
## 1. Vision general del flujo
El shell se puede explicar como una tuberia de fases:
1. Lectura interactiva (prompt + historial).
2. Tokenizacion (lexing) con comillas y metacaracteres.
3. Parseo (construccion de comandos, redirecciones, pipes).
4. Expansion de variables ($VAR y $?).
5. Preparacion de ejecucion (resolucion de rutas, heredoc).
6. Ejecucion (builtins o execve) con pipes y redirecciones.
7. Gestion de exit status y señales.
Esta separacion permite explicar en defensa cada pieza por separado y justificar
las decisiones tecnicas.
---
## 2. Propuesta de estructuras de datos
Estas estructuras son solo guia. Adapta nombres a tu estilo y Norminette.
### 2.1 Token (lexer)
Representa una unidad del input (palabra, pipe, redireccion, etc.).
- enum e_tokentype
- TOK_WORD
- TOK_PIPE
- TOK_REDIR_IN (<)
- TOK_REDIR_OUT (>)
- TOK_REDIR_APPEND (>>)
- TOK_HEREDOC (<<)
- struct s_token
- char *text
- t_tokentype type
- struct s_token *next
Uso en defensa: el lexer separa el input en unidades, respetando comillas.
### 2.2 Redireccion
Guarda los datos de redireccion por comando.
- enum e_redirtype
- REDIR_IN
- REDIR_OUT
- REDIR_APPEND
- REDIR_HEREDOC
- struct s_redir
- t_redirtype type
- char *target
- int fd
- struct s_redir *next
Notas:
- target es el filename o delimitador de heredoc.
- fd se resuelve en fase de preparacion (open o pipe temporal).
### 2.3 Comando
- struct s_command
- char **argv
- int argc
- char *path
- t_redir *redirs
### 2.4 Pipeline
Una lista de comandos en orden, unidos por pipes.
- struct s_pipeline
- t_command **cmds
- size_t count
---
## 3. Lexer: reglas y pasos
Reglas clave del subject:
- No interpretar comillas sin cerrar.
- Comilla simple: no se expanden variables ni metacaracteres.
- Comilla doble: se expanden variables, pero se respetan caracteres normales.
- Metacaracteres: |, <, >, <<, >> separan tokens.
Pasos recomendados:
1. Recorrer la linea caracter a caracter.
2. Mantener estado: in_single, in_double.
3. Cuando no estas en comillas, detectar metacaracteres y cortar tokens.
4. Construir TOK_WORD con el texto exacto (sin eliminar comillas aun).
5. Si llegas a fin de linea con in_single o in_double activo, error de parseo.
Explicacion para defensa:
- El lexer no sabe de ejecucion, solo separa en tokens validos.
- El manejo de comillas se hace aqui para respetar la sintaxis del shell.
---
## 4. Parser: construccion de comandos
Objetivo: transformar tokens en una estructura ejecutable.
Pasos:
1. Recorrer lista de tokens.
2. Cada TOK_PIPE cierra un comando actual y abre el siguiente.
3. TOK_WORD se agrega a argv.
4. TOK_REDIR_* consume el siguiente token (debe ser TOK_WORD) como target.
5. Construir lista de redirecciones para cada comando.
6. Validar errores: pipe inicial/final, redireccion sin target, etc.
Explicacion para defensa:
- El parser aplica reglas de orden y construye una estructura clara.
- Separar argv y redirecciones evita mezclar logica en executor.
---
## 5. Expansion de variables
Reglas:
- $VAR se sustituye por getenv/tabla interna.
- $? se sustituye por el exit_status anterior.
- En comilla simple no se expande.
- En comilla doble si se expande.
Proceso recomendado:
1. Durante tokenizacion, guardar el texto con sus comillas o bien
marcar segmentos con estado de comillas.
2. En expansion, recorrer cada palabra y reemplazar $...
3. Si variable no existe, reemplazar por string vacio.
4. Eliminar comillas despues de la expansion.
Explicacion para defensa:
- La expansion es una fase separada para no complicar el parser.
- $?, variable especial, refleja el estado de la ultima ejecucion.
---
## 6. Redirecciones y heredoc
Redirecciones basicas:
- <: open(file, O_RDONLY)
- >: open(file, O_WRONLY | O_CREAT | O_TRUNC)
- >>: open(file, O_WRONLY | O_CREAT | O_APPEND)
Heredoc (<<):
1. Leer lineas hasta delimitador exacto.
2. Guardar el contenido en un pipe o fichero temporal.
3. Usar el extremo de lectura como STDIN del comando.
4. No guardar el contenido en historial.
Explicacion para defensa:
- Las redirecciones se resuelven antes de ejecutar el proceso.
- Heredoc es una fuente especial de entrada.
---
## 7. Resolucion de comandos y PATH
Reglas:
- Si argv[0] es una ruta absoluta o relativa (/, ./, ../), usarla tal cual.
- Si no, buscar en PATH separando por ':'.
- Si es builtin, no se necesita path real.
Proceso:
1. Detectar builtin.
2. Si no builtin y no es ruta, recorrer PATH y usar access().
3. Guardar path en t_command->path.
---
## 8. Ejecucion
Caso 1: comando unico builtin
- Ejecutar en el proceso padre para que pueda modificar estado del shell
(ej: cd, export, unset, exit).
Caso 2: pipeline o comando externo
- Usar fork + execve.
- Crear pipes entre comandos.
- Aplicar redirecciones antes de ejecutar.
Proceso para pipeline:
1. Para cada comando, crear pipe si hay siguiente.
2. fork.
3. En child: dup2 para redirecciones y pipes, luego ejecutar.
4. En parent: cerrar FDs innecesarios y seguir.
5. Esperar procesos, guardar exit status del ultimo comando.
Explicacion para defensa:
- Las pipes conectan stdout del comando i con stdin del comando i+1.
- Los builtins dentro de pipeline se ejecutan en child.
---
## 9. Builtins obligatorios
- echo con -n
- cd (ruta relativa o absoluta)
- pwd
- export (sin opciones)
- unset (sin opciones)
- env (sin opciones o argumentos)
- exit
Notas de defensa:
- export/unset trabajan sobre la tabla de variables del shell.
- env imprime variables de entorno.
- exit debe actualizar exit_status y terminar el loop principal.
---
## 10. Senales
Requisitos interactivos:
- ctrl-C: imprime nueva linea y muestra prompt.
- ctrl-D: termina el shell.
- ctrl-\: no hace nada.
Regla del subject:
- Solo una variable global para indicar la senal recibida.
Proceso:
1. Definir una variable global int g_signal.
2. Configurar handlers con sigaction.
3. En handler: actualizar g_signal y escribir un '\n' si procede.
4. En el loop principal: si g_signal indica SIGINT, resetear lineas de readline.
---
## 11. Manejo de errores y salida
- Mostrar errores con perror o mensajes consistentes.
- Si parseo falla, no ejecutar nada.
- Mantener exit_status actualizado.
Explicacion en defensa:
- Un shell robusto evita ejecutar comandos parcialmente parseados.
- exit_status es clave para $?.
---
## 12. Checklist para defensa (guion rapido)
1. Explico el flujo completo: lectura -> lexer -> parser -> expansion -> exec.
2. Explico como manejo comillas y metacaracteres.
3. Explico como construyo argv y redirecciones.
4. Explico expansion de $VAR y $?.
5. Explico pipes y redirecciones con dup2.
6. Explico por que los builtins se ejecutan en parent o child.
7. Explico manejo de senales y la variable global unica.
8. Explico exit_status y comportamiento de $?.
---
## 13. Sugerencia de estructura de archivos
- include/
- minishell.h
- core.h (estructuras globales y estado)
- parser.h (tokens, parser)
- executor.h
- builtins.h
- src/
- core/ (init, signals, util)
- parser/ (lexer.c, parser.c, expand.c)
- executor/ (executor.c, redirs.c)
- builtins/ (echo, cd, pwd, exit, env, export, unset)
- variables/ (environment.c)
- minishell.c (loop principal)
- main.c
Esto es solo una guia; no es obligatorio seguirla al pie de la letra.
---
## 14. Consejos para la defensa
- Usa bash como referencia de comportamiento.
- Demuestra un par de ejemplos: pipe, redireccion y expansion.
- Si algo falla, explica que el parser previene ejecucion parcial.
- Recalca el manejo correcto de ctrl-C y ctrl-\.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,26 @@
Tests eliminados del reporte por ser extras (\ y/o ~):
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:52 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:54 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:56 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:58 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:60 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:62 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:64 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:7 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:59 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:61 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:63 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:65 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:67 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:69 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:71 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:314 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:328 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:337 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:606 | motivo: \, ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_redirs.sh:208 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_path_check.sh:30 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/8_syntax_errors.sh:19 | motivo: ~
Total casos eliminados (extras): 22

View File

@@ -0,0 +1,59 @@
Tests eliminados del reporte por estar fuera del obligatorio:
- \ (barra invertida no especificada)
- ~ (tilde expansion no obligatoria)
- $"..." (extension bash, no requerida)
- ; (separador no obligatorio segun subject)
- && / || (bonus)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:26 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:28 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:32 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:34 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:52 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:54 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:56 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:58 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:60 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:62 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:64 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/10_parsing_hell.sh:301 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:7 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:31 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:33 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:37 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:39 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:59 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:61 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:63 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:65 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:67 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:69 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:71 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:314 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:328 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:337 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:536 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:538 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:552 | motivo: ; (no obligatorio)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:606 | motivo: \, ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_redirs.sh:208 | motivo: \
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:40 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:42 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:44 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:48 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:60 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:62 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:129 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:139 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:141 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:143 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:147 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:159 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:161 | motivo: $"..." (bash extension)
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_path_check.sh:30 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/8_syntax_errors.sh:19 | motivo: ~
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/8_syntax_errors.sh:68 | motivo: &&/|| (bonus)
Total casos evaluados en reporte original: 195
Total casos eliminados (extras): 48
Total casos restantes (mandatory): 147

View File

@@ -6,34 +6,56 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/29 22:09:51 by sede-san #+# #+# */
/* Updated: 2025/11/02 03:13:46 by sede-san ### ########.fr */
/* Updated: 2026/02/14 01:15:34 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTINS_H
# define BUILTINS_H
# include "minishell.h"
# include "ft_args.h"
# include "minishell.h"
# include "core.h"
# include "variables.h"
typedef uint8_t (*t_builtin_func)(t_command cmd, t_minishell *minishell);
/******************************************************************************/
/* Functions */
/******************************************************************************/
// cd.c
/* builtins.c */
extern u_int8_t builtin_cd(t_command cmd, t_minishell *msh);
extern uint8_t set_builtins(t_minishell *minishell);
// echo.c
extern uint8_t is_builtin(const char *command_name, t_minishell *minishell);
extern u_int8_t builtin_echo(t_command cmd);
/* cd.c */
// exit.c
extern uint8_t builtin_cd(t_command cmd, t_minishell *minishell);
extern u_int8_t builtin_exit(t_command cmd, t_minishell *msh);
/* echo.c */
// pwd.c
extern uint8_t builtin_echo(t_command cmd, t_minishell *minishell);
extern u_int8_t builtin_pwd(t_command cmd, t_minishell *msh);
/* exit.c */
extern uint8_t builtin_exit(t_command cmd, t_minishell *minishell);
/* pwd.c */
extern uint8_t builtin_pwd(t_command cmd, t_minishell *minishell);
/* env.c */
extern uint8_t builtin_env(t_command cmd, t_minishell *minishell);
/* export.c */
extern uint8_t builtin_export(t_command cmd, t_minishell *minishell);
/* unset.c */
extern uint8_t builtin_unset(t_command cmd, t_minishell *minishell);
#endif /* BUILTINS_H */

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 19:10:13 by sede-san #+# #+# */
/* Updated: 2025/10/30 16:05:48 by sede-san ### ########.fr */
/* Updated: 2026/02/14 01:25:43 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,9 +19,37 @@
/* Structures & Data Types */
/******************************************************************************/
typedef struct s_minishell t_minishell;
typedef struct s_variables t_variables;
typedef struct s_command t_command;
# define TOKENS_COUNT 5
typedef enum e_token_type
{
TOKEN_WORD,
TOKEN_PIPE,
TOKEN_REDIRECT_IN,
TOKEN_REDIRECT_OUT,
TOKEN_APPEND,
TOKEN_HEREDOC
} t_token_type;
typedef struct s_token
{
t_token_type type;
char *value;
} t_token;
typedef enum e_redirection_type
{
REDIRECT_IN,
REDIRECT_OUT,
APPEND,
HEREDOC
} t_redirection_type;
typedef struct s_redirection
{
t_token_type type;
char *target;
} t_redirection;
/**
* @brief Structure that holds both environment and internal variables
@@ -34,8 +62,8 @@ typedef struct s_command t_command;
*/
typedef struct s_variables
{
char **environment;
// char **internal;
t_hashmap *environment;
t_hashmap *internal;
} t_variables;
/**
@@ -49,8 +77,9 @@ typedef struct s_variables
typedef struct s_minishell
{
t_variables variables;
u_int8_t exit_status;
u_int8_t exit;
t_hashmap *builtins;
uint8_t exit_status;
bool exit;
} t_minishell;
/**
@@ -68,18 +97,53 @@ typedef struct s_command
int argc;
char **argv;
char *path;
t_list *redirections;
t_list *heredocs;
} t_command;
/**
* @brief Default shell prompt string
*/
# define DEFAULT_PS1 "minishell > "
/**
* @brief Default secondary prompt string for multiline input
*/
# define DEFAULT_PS2 "> "
/******************************************************************************/
/* Functions */
/******************************************************************************/
/* minishell.c */
extern int minishell_init(t_minishell *minishell, char **envp);
extern void minishell_init(t_minishell *minishell, char **envp);
extern u_int8_t minishell_run(t_minishell *minishell);
extern void minishell_run(t_minishell *minishell);
extern void minishell_clear(t_minishell *minishell);
extern void minishell_set_interactive_signals(void);
extern void minishell_set_execution_signals(void);
extern void minishell_set_child_signals(void);
extern bool minishell_consume_sigint(void);
/* environment.c */
extern void set_envp(char **envp, t_minishell *msh);
extern void set_env(const char *env_name, char *env_value,
t_minishell *msh);
extern char **get_envp(t_minishell *msh);
extern void free_envp(char **envp);
void handle_sigint_status(t_minishell *minishell);
bool handle_eof(char *line, t_minishell *minishell);
#endif /* CORE_H */

24
include/errors.h Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/10 22:28:01 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:11:38 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ERRORS_H
# define ERRORS_H
# include "minishell.h"
# include "core.h"
# include "parser.h"
extern void syntax_error_unexpected_token(t_token *token);
extern void malloc_error(void);
extern void command_not_found_error(const char *command);
#endif /* ERRORS_H */

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/28 13:08:33 by sede-san #+# #+# */
/* Updated: 2025/10/28 14:20:23 by sede-san ### ########.fr */
/* Updated: 2026/02/08 21:32:56 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,13 +14,47 @@
# define EXECUTOR_H
# include "minishell.h"
# include "core.h"
# include <stdint.h>
/******************************************************************************/
/* Functions */
/******************************************************************************/
# define READ_PIPE 0
# define WRITE_PIPE 1
# define PIPE_ERROR -1
# define FORK_ERROR -1
// executor.c
typedef struct s_pipeline
{
int prev_read_fd;
int pipefd[2];
} t_pipeline;
extern u_int8_t execute(t_command command, t_minishell *minishell);
typedef struct s_exec_state
{
uint8_t exit_status;
t_pipeline pipeline;
t_list *current_command;
pid_t last_child_pid;
} t_exec_state;
extern uint8_t execute(t_list *command, t_minishell *minishell);
extern uint8_t executor_execute_command(t_command *cmd, t_minishell *msh);
extern char *executor_resolve_command_path(const t_command *cmd,
t_minishell *msh);
extern bool executor_is_builtin_command(const t_command *cmd,
t_minishell *msh);
extern int executor_create_pipe_if_needed(t_list *node, t_pipeline *pl);
extern bool executor_is_fork_required(t_list *node, const t_pipeline *pl,
t_minishell *msh);
extern void executor_setup_child_input(t_pipeline *pipeline);
extern void executor_setup_child_output(t_list *node, t_pipeline *pl);
extern void executor_child_process(t_list *node, t_pipeline *pl,
t_minishell *msh);
extern void executor_parent_cleanup(t_list *node, t_pipeline *pl);
extern uint8_t executor_wait_for_children(pid_t last_child_pid);
extern void executor_cmdfree(t_command *command);
extern bool executor_apply_redirections(const t_command *command,
int *saved_stdin, int *saved_stdout);
extern void executor_restore_redirections(int saved_stdin,
int saved_stdout);
#endif /* EXECUTOR_H */

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/20 16:35:10 by sede-san #+# #+# */
/* Updated: 2025/10/30 12:20:08 by sede-san ### ########.fr */
/* Updated: 2026/02/14 01:13:42 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,11 +15,11 @@
# include "libft.h"
# include "ft_printf.h"
# include "get_next_line.h"
# include "chardefs.h"
# include "core.h"
# include "parser.h"
# include "executor.h"
# include "builtins.h"
# include <stdbool.h>
# include <stdint.h>
# include <errno.h>
# include <readline/readline.h> // readline(3), rl_clear_history(),
// rl_on_new_line(), rl_replace_line(),
// rl_redisplay()

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 19:03:51 by sede-san #+# #+# */
/* Updated: 2025/10/30 12:44:51 by sede-san ### ########.fr */
/* Updated: 2026/02/12 18:34:11 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,8 @@
# define PARSER_H
# include "minishell.h"
# include "core.h"
# include "builtins.h"
/******************************************************************************/
/* Functions */
@@ -21,6 +23,29 @@
// parser.c
extern t_command parse(char *line, t_minishell *minishell);
extern t_list *parse(char *line, t_minishell *minishell);
// lexer.c
t_token_type get_token_type(const char *str);
t_token *token_new(t_token_type type, char *text);
t_token *read_token(t_token_type type, const char *line,
size_t *i);
t_token *read_word(const char *line, size_t *i);
extern t_list *lex(const char *line);
extern void token_clear(t_token *token);
extern t_command *command_new(t_list **tokens);
extern void command_clear(t_command *command);
extern void command_add_tokens(t_command **command, t_list **tokens);
extern bool is_pipe(t_token *token);
extern bool is_redirection(t_token *token);
void redirection_add(t_list **tokens, t_token *token,
t_command **command);
void words_add(t_list **tokens, t_command **command);
void expand(t_list **commands, t_minishell *minishell);
void redirection_clear(t_redirection *redirection);
#endif /* PARSER_H */

43
include/variables.h Normal file
View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* variables.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:45:35 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:24:49 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VARIABLES_H
# define VARIABLES_H
# include "minishell.h"
# include "core.h"
// variables.c
extern char *get_var(const char *name, t_minishell *minishell);
extern void set_var(const char *name, char *value, t_minishell *minishell);
extern void unset_var(const char *name, t_minishell *minishell);
// environment.c
extern char *get_env(const char *name, t_minishell *minishell);
extern void set_env(const char *name, char *value, t_minishell *minishell);
extern void unset_env(const char *name, t_minishell *minishell);
extern void set_envp(char **envp, t_minishell *minishell);
extern char **get_envp(t_minishell *minishell);
extern void free_envp(char **envp);
// internal.c
extern char *get_int(const char *name, t_minishell *minishell);
extern void set_int(const char *name, char *value, t_minishell *minishell);
extern void unset_int(const char *name, t_minishell *minishell);
extern void set_intp(t_minishell *minishell);
#endif /* VARIABLES_H */

64
src/builtins/builtins.c Normal file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/01 17:04:57 by sede-san #+# #+# */
/* Updated: 2026/02/08 19:51:38 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static uint8_t register_builtin(
t_minishell *minishell,
const char *name,
t_builtin_func builtin
)
{
char *key;
key = ft_strdup(name);
if (key == NULL)
return (0);
ft_hashmap_put(minishell->builtins, key, builtin);
if (!ft_hashmap_contains_key(minishell->builtins, name))
{
free(key);
return (0);
}
return (1);
}
uint8_t set_builtins(
t_minishell *minishell
) {
minishell->builtins
= ft_hashmap_new(7, ft_hashmap_hashstr, ft_hashmap_strcmp);
if (minishell->builtins == NULL)
return (0);
if (!register_builtin(minishell, "cd", builtin_cd)
|| !register_builtin(minishell, "echo", builtin_echo)
|| !register_builtin(minishell, "env", builtin_env)
|| !register_builtin(minishell, "exit", builtin_exit)
|| !register_builtin(minishell, "export", builtin_export)
|| !register_builtin(minishell, "pwd", builtin_pwd)
|| !register_builtin(minishell, "unset", builtin_unset))
{
ft_hashmap_clear_keys(&minishell->builtins);
return (0);
}
return (1);
}
uint8_t is_builtin(
const char *command_name,
t_minishell *minishell
) {
if (command_name == NULL || minishell == NULL
|| minishell->builtins == NULL)
return (0);
return (ft_hashmap_contains_key(minishell->builtins, command_name));
}

View File

@@ -6,47 +6,110 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/31 00:05:22 by sede-san #+# #+# */
/* Updated: 2025/10/31 01:38:14 by sede-san ### ########.fr */
/* Updated: 2025/12/01 14:11:32 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static u_int8_t handle_error(t_command cmd, t_minishell *msh, char *path);
static uint8_t handle_error(void);
static void update_pwd_vars(t_minishell *msh, char *oldpwd);
static char *get_path_from_env(
t_minishell *msh,
const char *env_name,
const char *error,
uint8_t *status
);
static char *resolve_cd_path(
t_command cmd,
t_minishell *msh,
uint8_t *status
);
u_int8_t builtin_cd(
uint8_t builtin_cd(
t_command cmd,
t_minishell *msh
){
char *path;
char *oldpwd;
uint8_t status;
bool show_pwd;
if (cmd.argc > 2)
{
ft_eputendl("minishell: cd: too many arguments");
return (2);
}
else if (cmd.argc == 1)
path = getenv("HOME");
else
path = cmd.argv[1];
path = resolve_cd_path(cmd, msh, &status);
if (status != EXIT_SUCCESS)
return (status);
show_pwd = (cmd.argc == 2 && ft_strcmp(cmd.argv[1], "-") == 0);
oldpwd = getcwd(NULL, 0);
if (chdir(path) == -1)
return (handle_error(cmd, msh, path));
{
free(oldpwd);
return (handle_error());
}
update_pwd_vars(msh, oldpwd);
if (show_pwd && get_env("PWD", msh) != NULL)
ft_putendl(get_env("PWD", msh));
free(oldpwd);
return (EXIT_SUCCESS);
}
static u_int8_t handle_error(
static uint8_t handle_error(void)
{
perror("minishell: cd");
return (EXIT_FAILURE);
}
static void update_pwd_vars(
t_minishell *msh,
char *oldpwd
){
char *newpwd;
if (oldpwd != NULL)
set_env("OLDPWD", oldpwd, msh);
newpwd = getcwd(NULL, 0);
if (newpwd != NULL)
{
set_env("PWD", newpwd, msh);
free(newpwd);
}
}
static char *resolve_cd_path(
t_command cmd,
t_minishell *msh,
char *path
uint8_t *status
){
u_int8_t exit_code;
if (access(path, F_OK) != -1)
// No such file or directory
exit_code = 1;
else if (access(path, X_OK) == -1)
// Permission denied
exit_code = 2;
perror(cmd.argv[0]);
return (exit_code);
if (cmd.argc > 2)
{
ft_eputendl("minishell: cd: too many arguments");
*status = EXIT_FAILURE;
return (NULL);
}
if (cmd.argc == 2 && ft_strcmp(cmd.argv[1], "-") == 0)
return (get_path_from_env(msh, "OLDPWD",
"minishell: cd: OLDPWD not set", status));
if (cmd.argc == 1)
return (get_path_from_env(msh, "HOME",
"minishell: cd: HOME not set", status));
*status = EXIT_SUCCESS;
return (cmd.argv[1]);
}
static char *get_path_from_env(
t_minishell *msh,
const char *env_name,
const char *error,
uint8_t *status
){
char *path;
path = get_env(env_name, msh);
if (path == NULL)
{
ft_eputendl((char *)error);
*status = EXIT_FAILURE;
return (NULL);
}
*status = EXIT_SUCCESS;
return (path);
}

View File

@@ -6,19 +6,21 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/31 02:41:11 by sede-san #+# #+# */
/* Updated: 2025/11/02 03:12:00 by sede-san ### ########.fr */
/* Updated: 2025/12/01 18:05:51 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "echo_def.h"
u_int8_t builtin_echo(
t_command cmd
uint8_t builtin_echo(
t_command cmd,
t_minishell *msh
){
const t_args args = read_args(cmd);
size_t i;
(void)msh;
i = -1;
while (args.strings.data.sp[++i])
{

View File

@@ -13,7 +13,7 @@
#include "echo_def.h"
static void assign_flags(t_args *args, t_command cmd, size_t *i);
static u_int8_t is_valid_flag(t_args *args, char *flag);
static uint8_t is_valid_flag(t_args *args, char *flag);
static void assign_default_flags(
t_args *args
@@ -51,7 +51,7 @@ static void assign_flags(
}
}
static u_int8_t is_valid_flag(
static uint8_t is_valid_flag(
t_args *args,
char *flag
){

58
src/builtins/env/env.c vendored Normal file
View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/09 22:05:00 by codex #+# #+# */
/* Updated: 2026/02/09 22:05:00 by codex ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static void print_entry(t_map_entry *entry);
static void print_env(t_minishell *msh);
uint8_t builtin_env(
t_command cmd,
t_minishell *msh
)
{
if (cmd.argc > 1)
{
ft_eputendl("minishell: env: too many arguments");
return (EXIT_FAILURE);
}
print_env(msh);
return (EXIT_SUCCESS);
}
static void print_entry(
t_map_entry *entry
)
{
ft_putstr((char *)entry->key);
ft_putchar('=');
if (entry->value != NULL)
ft_putstr((char *)entry->value);
ft_putchar('\n');
}
static void print_env(
t_minishell *msh
)
{
t_list *entries;
t_list *current;
entries = ft_hashmap_entries(msh->variables.environment);
current = entries;
while (current != NULL)
{
print_entry((t_map_entry *)current->content);
current = current->next;
}
ft_lstclear_nodes(&entries);
}

View File

@@ -6,39 +6,50 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/30 01:20:48 by sede-san #+# #+# */
/* Updated: 2025/10/30 09:08:02 by sede-san ### ########.fr */
/* Updated: 2026/02/08 21:16:18 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
u_int8_t builtin_exit(
static bool exit_arg_is_invalid(const char *arg)
{
if (arg == NULL)
return (true);
if ((*arg == '+' || *arg == '-') && arg[1] == '\0')
return (true);
if (!ft_strisnum(arg))
return (true);
return (false);
}
uint8_t builtin_exit(
t_command cmd,
t_minishell *msh
){
ft_eputendl("exit");
)
{
if (isatty(STDIN_FILENO))
ft_eputendl("exit");
if (cmd.argc == 1)
{
// msh.exit = 1;
// return the last exit_status, if none 0 is returned
msh->exit = 1;
return (msh->exit_status);
}
else if (!ft_strisnum(cmd.argv[1]))
if (exit_arg_is_invalid(cmd.argv[1]))
{
ft_eputstr("exit: ");
ft_eputendl(cmd.argv[1]);
ft_eputendl(": numeric argument required");
return (2);
ft_eprintf("minishell: exit: %s: numeric argument required\n",
cmd.argv[1]);
msh->exit = 1;
msh->exit_status = 2;
return (msh->exit_status);
}
else if (cmd.argc > 2)
if (cmd.argc > 2)
{
ft_eputendl("exit: too many arguments");
return (2);
}
else
{
// msh.exit = 1;
// cast to u_int8_t causes to return a value between 0 and 255
return ((u_int8_t)ft_atol(cmd.argv[1]));
ft_eputendl("minishell: exit: too many arguments");
msh->exit_status = EXIT_FAILURE;
return (msh->exit_status);
}
msh->exit = 1;
msh->exit_status = (uint8_t)ft_atol(cmd.argv[1]);
return (msh->exit_status);
}

View File

@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/09 22:05:00 by codex #+# #+# */
/* Updated: 2026/02/14 02:02:14 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static uint8_t is_valid_identifier(const char *arg, size_t name_len);
static uint8_t export_one(char *arg, t_minishell *msh);
uint8_t builtin_export(
t_command cmd,
t_minishell *msh
)
{
uint8_t status;
size_t i;
if (cmd.argc == 1)
return (builtin_env(cmd, msh));
status = EXIT_SUCCESS;
i = 0;
while (cmd.argv[++i] != NULL)
if (export_one(cmd.argv[i], msh) != EXIT_SUCCESS)
status = EXIT_FAILURE;
return (status);
}
static uint8_t is_valid_identifier(
const char *arg,
size_t name_len
)
{
size_t i;
if (name_len == 0 || arg == NULL)
return (0);
if (!ft_isalpha(arg[0]) && arg[0] != '_')
return (0);
i = 0;
while (++i < name_len)
if (!ft_isalnum(arg[i]) && arg[i] != '_')
return (0);
return (1);
}
static uint8_t export_one(
char *arg,
t_minishell *msh
)
{
char *eq_pos;
char *name;
size_t name_len;
eq_pos = ft_strchr(arg, '=');
name_len = ft_strlen(arg);
if (eq_pos != NULL)
name_len = (size_t)(eq_pos - arg);
if (!is_valid_identifier(arg, name_len))
return (ft_eprintf("minishell: export: `%s': not a valid identifier\n",
arg), EXIT_FAILURE);
name = ft_substr(arg, 0, name_len);
if (name == NULL)
return (EXIT_FAILURE);
if (eq_pos != NULL)
set_var(name, eq_pos + 1, msh);
else
set_var(name, "", msh);
free(name);
return (EXIT_SUCCESS);
}

View File

@@ -6,21 +6,27 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/29 22:08:55 by sede-san #+# #+# */
/* Updated: 2025/10/30 01:14:09 by sede-san ### ########.fr */
/* Updated: 2025/12/01 16:27:08 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
u_int8_t builtin_pwd(
uint8_t builtin_pwd(
t_command cmd,
t_minishell *msh
){
char cwd[PATH_MAX];
char *cwd;
(void)cmd;
(void)msh;
getcwd(cwd, PATH_MAX);
printf("%s\n", cwd);
cwd = getcwd(NULL, 0);
if (cwd == NULL)
{
perror("minishell: pwd");
return (EXIT_FAILURE);
}
ft_putendl(cwd);
free(cwd);
return (EXIT_SUCCESS);
}

View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/09 22:05:00 by codex #+# #+# */
/* Updated: 2026/02/09 22:05:00 by codex ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static uint8_t is_valid_identifier(const char *arg);
static uint8_t unset_one(char *arg, t_minishell *msh);
uint8_t builtin_unset(
t_command cmd,
t_minishell *msh
)
{
uint8_t status;
size_t i;
status = EXIT_SUCCESS;
i = 0;
while (cmd.argv[++i] != NULL)
if (unset_one(cmd.argv[i], msh) != EXIT_SUCCESS)
status = EXIT_FAILURE;
return (status);
}
static uint8_t is_valid_identifier(
const char *arg
)
{
size_t i;
if (arg == NULL || *arg == '\0')
return (0);
if (!ft_isalpha(arg[0]) && arg[0] != '_')
return (0);
i = 0;
while (arg[++i] != '\0')
if (!ft_isalnum(arg[i]) && arg[i] != '_')
return (0);
return (1);
}
static uint8_t unset_one(
char *arg,
t_minishell *msh
)
{
if (!is_valid_identifier(arg))
return (ft_eprintf("minishell: unset: `%s': not a valid identifier\n",
arg), EXIT_FAILURE);
unset_env(arg, msh);
return (EXIT_SUCCESS);
}

View File

@@ -0,0 +1,11 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/09 18:40:03 by sede-san #+# #+# */
/* Updated: 2026/02/09 18:40:04 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */

66
src/core/signals.c Normal file
View File

@@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signals.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/13 00:00:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "core.h"
static int g_signal = 0;
static void sigint_handler(int signal)
{
g_signal = signal;
write(STDOUT_FILENO, "\n", 1);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}
void minishell_set_interactive_signals(void)
{
struct sigaction action;
ft_bzero(&action, sizeof(action));
action.sa_handler = sigint_handler;
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, NULL);
action.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &action, NULL);
}
void minishell_set_execution_signals(void)
{
struct sigaction action;
ft_bzero(&action, sizeof(action));
action.sa_handler = SIG_IGN;
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
}
void minishell_set_child_signals(void)
{
struct sigaction action;
ft_bzero(&action, sizeof(action));
action.sa_handler = SIG_DFL;
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
}
bool minishell_consume_sigint(void)
{
if (g_signal != SIGINT)
return (false);
g_signal = 0;
return (true);
}

31
src/errors/errors.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/10 22:22:06 by sede-san #+# #+# */
/* Updated: 2026/02/12 02:55:36 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "errors.h"
void syntax_error_unexpected_token(
t_token *token
)
{
char *cause;
if (token == NULL)
cause = "newline";
else
cause = token->value;
ft_eprintf("minishell: syntax error near unexpected token `%s'\n", cause);
}
void malloc_error(void)
{
ft_eprintf("minishell: %s\n", strerror(ENOMEM));
}

View File

@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/11 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:34:37 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
#include "builtins.h"
#include <errno.h>
static uint8_t resolve_execve_status(void)
{
if (errno == ENOENT)
return (127);
if (errno == EACCES || errno == ENOEXEC || errno == EISDIR)
return (126);
return (EXIT_FAILURE);
}
static uint8_t execute_builtin(
const t_command *command,
t_minishell *minishell
)
{
const t_builtin_func builtin
= ft_hashmap_get(minishell->builtins, command->argv[0]);
return (builtin(*command, minishell));
}
static void handle_execve_error(
const t_command *command,
char **envp
)
{
uint8_t exit_status;
size_t i;
exit_status = resolve_execve_status();
i = 0;
while (envp[i] != NULL)
free(envp[i++]);
free(envp);
perror(command->path);
exit(exit_status);
}
static void execute_external_command(
const t_command *command,
t_minishell *minishell
)
{
char **envp;
envp = get_envp(minishell);
if (envp == NULL)
{
perror("get_envp");
exit(EXIT_FAILURE);
}
execve(command->path, command->argv, envp);
handle_execve_error(command, envp);
}
uint8_t executor_execute_command(
t_command *command,
t_minishell *minishell
)
{
if (command == NULL || command->argv == NULL || command->argv[0] == NULL)
return (EXIT_SUCCESS);
if (executor_is_builtin_command(command, minishell))
return (execute_builtin(command, minishell));
if (command->path != NULL)
free(command->path);
command->path = executor_resolve_command_path(command, minishell);
if (command->path == NULL)
{
ft_eprintf("minishell: %s: command not found\n", command->argv[0]);
return (127);
}
execute_external_command(command, minishell);
return (EXIT_FAILURE);
}

View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/13 00:00:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
static void cmdfree_argv(
char **argv
)
{
size_t i;
if (argv == NULL)
return ;
i = 0;
while (argv[i] != NULL)
{
free(argv[i]);
i++;
}
free(argv);
}
static void cmdfree_redirection(
t_redirection *redirection
)
{
if (redirection == NULL)
return ;
free(redirection->target);
free(redirection);
}
void executor_cmdfree(
t_command *command
)
{
if (command == NULL)
return ;
cmdfree_argv(command->argv);
free(command->path);
ft_lstclear(&command->redirections,
(void (*)(void *))cmdfree_redirection);
ft_lstclear(&command->heredocs, (void (*)(void *))cmdfree_redirection);
free(command);
}

View File

@@ -5,119 +5,100 @@
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/28 13:03:44 by sede-san #+# #+# */
/* Updated: 2025/10/29 20:37:09 by sede-san ### ########.fr */
/* Created: 2026/02/08 19:10:47 by sede-san #+# #+# */
/* Updated: 2026/02/11 00:00:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
static char *solve_path(char *cmd_name, t_minishell *msh);
static u_int8_t path_is_solved(char *cmd_name);
static void handle_child(t_command *cmd, t_minishell *msh);
static void handle_parent(pid_t child_pid, t_command *cmd, t_minishell *msh);
static void init_exec_state(
t_exec_state *state,
t_list *command_list
)
{
state->exit_status = EXIT_SUCCESS;
state->pipeline.prev_read_fd = -1;
state->current_command = command_list;
state->last_child_pid = -1;
}
u_int8_t execute(
t_command cmd,
t_minishell *msh
){
pid_t child_pid;
static bool fork_current_command(
t_exec_state *state,
t_minishell *minishell
)
{
pid_t pid;
cmd.path = solve_path(cmd.argv[0], msh);
if (!cmd.path)
{
// command not found
ft_eprintf("minishell: %s: command not found\n", cmd.argv[0]);
return (msh->exit_status = 127, msh->exit_status);
}
if (access(cmd.path, X_OK) != EXIT_SUCCESS)
{
// permission denied
ft_eputstr("minishell: ");
perror(cmd.path);
return (msh->exit_status = 126, msh->exit_status);
}
child_pid = fork();
if (child_pid == -1)
perror("minishell");
else if (child_pid == 0)
handle_child(&cmd, msh);
pid = fork();
if (pid == FORK_ERROR)
return (perror("fork"), state->exit_status = EXIT_FAILURE, false);
if (pid == 0)
executor_child_process(state->current_command, &state->pipeline,
minishell);
state->last_child_pid = pid;
return (true);
}
static void run_command_in_parent(
t_exec_state *state,
t_minishell *minishell
)
{
t_command *command;
int saved_stdin;
int saved_stdout;
command = state->current_command->content;
if (!executor_apply_redirections(command, &saved_stdin, &saved_stdout))
state->exit_status = EXIT_FAILURE;
else
handle_parent(child_pid, &cmd, msh);
return (msh->exit_status);
state->exit_status = executor_execute_command(command, minishell);
executor_restore_redirections(saved_stdin, saved_stdout);
}
static char *solve_path(
char *cmd_name,
t_minishell *msh
){
char *cmd_path;
char **path;
size_t i;
static bool run_current_command(
t_exec_state *state,
t_minishell *minishell
)
{
bool should_fork;
if (path_is_solved(cmd_name))
// return a copy to avoid double free on parent
return (ft_strdup(cmd_name));
//TODO substitute getenv call for own getenv
path = ft_split(getenv("PATH"), COLON);
if (!path)
return (NULL);
(void)msh;
cmd_path = NULL;
i = -1;
while (!cmd_path && path[++i])
if (executor_create_pipe_if_needed(state->current_command,
&state->pipeline) == PIPE_ERROR)
return (state->exit_status = EXIT_FAILURE, false);
should_fork = executor_is_fork_required(state->current_command,
&state->pipeline, minishell);
if (should_fork)
{
cmd_path = ft_strnjoin(3, path[i], "/", cmd_name);
if (!cmd_path)
return (NULL);
/**
* If a command exists but user has no execution permission
* the command is shown as non existant instead of showing the
* last ocurrence found
*
* TLDR: bash shows 'Permission denied'
* and minishell 'command not found'
*
* TEST: execute an existing command without permission to do so
*/
if (access(cmd_path, X_OK) != EXIT_SUCCESS)
ft_free((void **)&cmd_path);
}
ft_free_split((char **)path);
if (!cmd_path)
return (NULL);
return (cmd_path);
}
static u_int8_t path_is_solved(
char *cmd_name
){
return (ft_strncmp(cmd_name, "/", 1) == 0
|| (cmd_name[1] && ft_strncmp(cmd_name, "./", 2) == 0)
|| (cmd_name[2] && ft_strncmp(cmd_name, "../", 3) == 0)
);
}
static void handle_child(
t_command *cmd,
t_minishell *msh
){
execve(cmd->path, cmd->argv, msh->variables.environment);
}
static void handle_parent(
pid_t child_pid,
t_command *cmd,
t_minishell *msh
){
if (waitpid(child_pid, (int *)&msh->exit_status, 0) == EXIT_SUCCESS)
{
// handle success
if (!fork_current_command(state, minishell))
return (false);
}
else
{
// handle error
}
ft_free((void **)&cmd->path);
ft_free_split(cmd->argv);
run_command_in_parent(state, minishell);
executor_parent_cleanup(state->current_command, &state->pipeline);
state->current_command = state->current_command->next;
return (true);
}
uint8_t execute(
t_list *command_list,
t_minishell *minishell
)
{
t_exec_state state;
init_exec_state(&state, command_list);
minishell_set_execution_signals();
while (state.current_command)
{
if (!run_current_command(&state, minishell))
break ;
}
if (state.last_child_pid > 0)
state.exit_status = executor_wait_for_children(state.last_child_pid);
minishell_set_interactive_signals();
minishell->exit_status = state.exit_status;
ft_lstclear(&command_list, (void (*)(void *))executor_cmdfree);
return (state.exit_status);
}

View File

@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path_resolver.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/11 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:17:01 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
#include "variables.h"
static bool is_path_explicit(
const char *command_name
)
{
return (command_name != NULL && ft_strchr(command_name, '/') != NULL);
}
static char *resolve_path_from_env(
const char *command_name,
t_minishell *minishell
)
{
char *command_path;
char **path_env;
char *path_value;
size_t i;
path_value = get_env("PATH", minishell);
if (path_value == NULL)
return (NULL);
path_env = ft_split(path_value, ':');
if (path_env == NULL)
return (NULL);
command_path = NULL;
i = -1;
while (!command_path && path_env[++i] != NULL)
{
command_path = ft_strnjoin(3, path_env[i], "/", command_name);
if (command_path != NULL && access(command_path, X_OK) != EXIT_SUCCESS)
{
free(command_path);
command_path = NULL;
}
}
ft_free_split(path_env);
return (command_path);
}
static char *resolve_explicit_path(
const char *command_name
)
{
if (access(command_name, F_OK) != EXIT_SUCCESS)
return (NULL);
if (access(command_name, X_OK) != EXIT_SUCCESS)
return (NULL);
return (ft_strdup(command_name));
}
char *executor_resolve_command_path(
const t_command *command,
t_minishell *minishell
)
{
const char *command_name;
if (command == NULL || command->argv == NULL || command->argv[0] == NULL)
return (NULL);
command_name = command->argv[0];
if (is_path_explicit(command_name))
return (resolve_explicit_path(command_name));
return (resolve_path_from_env(command_name, minishell));
}

View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipeline_helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/11 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/11 00:00:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
#include "builtins.h"
int executor_create_pipe_if_needed(
t_list *current_command,
t_pipeline *pipeline
)
{
if (!current_command->next)
return (0);
if (pipe(pipeline->pipefd) == PIPE_ERROR)
return (perror("pipe"), PIPE_ERROR);
return (0);
}
bool executor_is_builtin_command(
const t_command *command,
t_minishell *minishell
)
{
if (command == NULL || command->argv == NULL || command->argv[0] == NULL)
return (false);
return (is_builtin(command->argv[0], minishell));
}
bool executor_is_fork_required(
t_list *current_command,
const t_pipeline *pipeline,
t_minishell *minishell
)
{
const t_command *command;
command = current_command->content;
return (pipeline->prev_read_fd != -1 || current_command->next != NULL
|| !executor_is_builtin_command(command, minishell));
}
void executor_setup_child_input(
t_pipeline *pipeline
)
{
if (pipeline->prev_read_fd != -1)
{
dup2(pipeline->prev_read_fd, STDIN_FILENO);
close(pipeline->prev_read_fd);
}
}
void executor_setup_child_output(
t_list *current_command,
t_pipeline *pipeline
)
{
if (current_command->next)
{
dup2(pipeline->pipefd[WRITE_PIPE], STDOUT_FILENO);
close(pipeline->pipefd[READ_PIPE]);
close(pipeline->pipefd[WRITE_PIPE]);
}
}

View File

@@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/11 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/11 00:00:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
static void set_signal_exit_status(
int status,
uint8_t *exit_status
)
{
if (WTERMSIG(status) == SIGINT)
write(STDOUT_FILENO, "\n", 1);
else if (WTERMSIG(status) == SIGQUIT)
{
if (WCOREDUMP(status))
write(STDERR_FILENO, "Quit (core dumped)\n", 19);
else
write(STDERR_FILENO, "Quit\n", 5);
}
*exit_status = 128 + WTERMSIG(status);
}
static void set_last_child_status(
int status,
uint8_t *exit_status
)
{
if (WIFEXITED(status))
*exit_status = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
set_signal_exit_status(status, exit_status);
}
void executor_child_process(
t_list *current_command,
t_pipeline *pipeline,
t_minishell *minishell
)
{
uint8_t exit_status;
t_command *command;
command = current_command->content;
minishell_set_child_signals();
executor_setup_child_input(pipeline);
executor_setup_child_output(current_command, pipeline);
if (!executor_apply_redirections(command, NULL, NULL))
exit(EXIT_FAILURE);
exit_status = executor_execute_command(command, minishell);
exit(exit_status);
}
void executor_parent_cleanup(
t_list *current_command,
t_pipeline *pipeline
)
{
if (pipeline->prev_read_fd != -1)
close(pipeline->prev_read_fd);
if (current_command->next)
{
close(pipeline->pipefd[WRITE_PIPE]);
pipeline->prev_read_fd = pipeline->pipefd[READ_PIPE];
}
else
pipeline->prev_read_fd = -1;
}
uint8_t executor_wait_for_children(
pid_t last_child_pid
)
{
uint8_t exit_status;
int status;
pid_t pid;
exit_status = EXIT_SUCCESS;
pid = wait(&status);
while (last_child_pid > 0 && pid > 0)
{
if (pid == last_child_pid)
set_last_child_status(status, &exit_status);
pid = wait(&status);
}
return (exit_status);
}

111
src/executor/redirections.c Normal file
View File

@@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirections.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/12 00:00:00 by sede-san #+# #+# */
/* Updated: 2026/02/12 00:00:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "executor.h"
#include <errno.h>
static int open_redirection_target(
const t_redirection *redirection
)
{
if (redirection->type == TOKEN_REDIRECT_IN)
return (open(redirection->target, O_RDONLY));
if (redirection->type == TOKEN_REDIRECT_OUT)
return (open(redirection->target, O_WRONLY | O_CREAT | O_TRUNC, 0644));
if (redirection->type == TOKEN_APPEND)
return (open(redirection->target, O_WRONLY | O_CREAT | O_APPEND, 0644));
errno = EINVAL;
return (-1);
}
static bool backup_stream_if_needed(
const t_redirection *redirection,
int *saved_stdin,
int *saved_stdout
)
{
if (redirection->type == TOKEN_REDIRECT_IN && saved_stdin != NULL
&& *saved_stdin == -1)
{
*saved_stdin = dup(STDIN_FILENO);
if (*saved_stdin == -1)
return (perror("dup"), false);
}
if ((redirection->type == TOKEN_REDIRECT_OUT
|| redirection->type == TOKEN_APPEND) && saved_stdout != NULL
&& *saved_stdout == -1)
{
*saved_stdout = dup(STDOUT_FILENO);
if (*saved_stdout == -1)
return (perror("dup"), false);
}
return (true);
}
bool executor_apply_redirections(
const t_command *command,
int *saved_stdin,
int *saved_stdout
)
{
t_list *node;
t_redirection *redirection;
int fd;
if (saved_stdin != NULL)
*saved_stdin = -1;
if (saved_stdout != NULL)
*saved_stdout = -1;
if (command == NULL || command->redirections == NULL)
return (true);
node = command->redirections;
while (node != NULL)
{
redirection = (t_redirection *)node->content;
if (redirection == NULL || redirection->target == NULL)
return (false);
if (!backup_stream_if_needed(redirection, saved_stdin, saved_stdout))
return (false);
fd = open_redirection_target(redirection);
if (fd == -1)
return (perror(redirection->target), false);
if (redirection->type == TOKEN_REDIRECT_IN
&& dup2(fd, STDIN_FILENO) == -1)
return (close(fd), perror("dup2"), false);
if ((redirection->type == TOKEN_REDIRECT_OUT
|| redirection->type == TOKEN_APPEND)
&& dup2(fd, STDOUT_FILENO) == -1)
return (close(fd), perror("dup2"), false);
close(fd);
node = node->next;
}
return (true);
}
void executor_restore_redirections(
int saved_stdin,
int saved_stdout
)
{
if (saved_stdin != -1)
{
if (dup2(saved_stdin, STDIN_FILENO) == -1)
perror("dup2");
close(saved_stdin);
}
if (saved_stdout != -1)
{
if (dup2(saved_stdout, STDOUT_FILENO) == -1)
perror("dup2");
close(saved_stdout);
}
}

View File

@@ -3,14 +3,15 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: padan-pe <padan-pe@student.42.fr> +#+ +:+ +#+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/20 16:34:42 by sede-san #+# #+# */
/* Updated: 2025/10/23 17:10:17 by padan-pe ### ########.fr */
/* Updated: 2026/02/09 18:46:21 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "core.h"
int main(
int argc,
@@ -18,19 +19,14 @@ int main(
char **envp
){
t_minishell minishell;
u_int8_t exit_status;
if (argc != 1 || argv[1] || !envp)
{
printf("Usage: ./minishell\n");
return (EXIT_FAILURE);
}
if (!minishell_init(&minishell, envp))
{
printf("Error: %s\n", "failed to initialize minishell");
return (EXIT_FAILURE);
}
exit_status = minishell_run(&minishell);
minishell_init(&minishell, envp);
minishell_run(&minishell);
minishell_clear(&minishell);
return (exit_status);
return (minishell.exit_status);
}

View File

@@ -6,45 +6,64 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/20 20:51:33 by sede-san #+# #+# */
/* Updated: 2025/10/30 22:36:03 by sede-san ### ########.fr */
/* Updated: 2026/02/14 02:23:17 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "core.h"
#include "parser.h"
#include "executor.h"
int minishell_init(
void minishell_init(
t_minishell *minishell,
char **envp
){
ft_bzero(minishell, sizeof(t_minishell));
minishell->variables.environment = envp;
return (1);
set_intp(minishell);
set_envp(envp, minishell);
set_builtins(minishell);
if (minishell->variables.environment == NULL || minishell->builtins == NULL)
minishell_clear(minishell);
}
u_int8_t minishell_run(
void minishell_run(
t_minishell *minishell
){
char *line;
t_command command;
)
{
char *line;
t_list *commands;
line = NULL;
if (minishell == NULL)
return ;
minishell_set_interactive_signals();
while (!minishell->exit)
{
line = readline("minishell > ");
if (*line)
if (isatty(STDIN_FILENO))
line = readline(get_var("PS1", minishell));
else
line = get_next_line(STDIN_FILENO);
handle_sigint_status(minishell);
if (handle_eof(line, minishell))
continue ;
if (*line != '\0')
{
add_history(line);
command = parse(line, minishell);
execute(command, minishell);
commands = parse(line, minishell);
minishell->exit_status = execute(commands, minishell);
}
ft_free((void **)&line);
free(line);
}
return (minishell->exit_status);
}
void minishell_clear(
t_minishell *minishell
){
rl_clear_history();
ft_bzero(minishell, sizeof(t_minishell));
if (minishell->variables.internal != NULL)
ft_hashmap_clear(&minishell->variables.internal, free);
if (minishell->variables.environment != NULL)
ft_hashmap_clear(&minishell->variables.environment, free);
if (minishell->builtins != NULL)
ft_hashmap_clear_keys(&minishell->builtins);
}

36
src/minishell_helpers.c Normal file
View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell_helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:09:23 by sede-san #+# #+# */
/* Updated: 2026/02/13 21:09:23 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "core.h"
void handle_sigint_status(
t_minishell *minishell
)
{
if (!minishell_consume_sigint())
return ;
minishell->exit_status = 130;
}
bool handle_eof(
char *line,
t_minishell *minishell
)
{
if (line != NULL)
return (false);
if (isatty(STDIN_FILENO))
ft_putendl("exit");
minishell->exit = true;
return (true);
}

69
src/parser/lexer.c Normal file
View File

@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/09 18:56:41 by sede-san #+# #+# */
/* Updated: 2026/02/11 02:06:36 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "core.h"
#include "parser.h"
static t_token *tokenize(const char *line, size_t *start);
/**
* @brief Converts a command line string into a list of tokens.
*
* @return A list of tokens or NULL on error.
*/
t_list *lex(
const char *line
)
{
t_list *tokens;
t_token *token;
size_t i;
tokens = NULL;
i = 0;
while (line[i] != '\0')
{
while (ft_isspace(line[i]))
i++;
if (line[i] == '\0')
break ;
token = tokenize(line, &i);
ft_lstadd_back(&tokens, ft_lstnew(token));
if (token == NULL)
{
ft_lstclear(&tokens, (void (*)(void *))token_clear);
return (NULL);
}
}
return (tokens);
}
/**
* @return A new token or NULL on error.
*/
static t_token *tokenize(
const char *line,
size_t *start
)
{
t_token *token;
t_token_type type;
if (line == NULL || line[*start] == '\0')
return (NULL);
type = get_token_type(line + *start);
if (type != TOKEN_WORD)
token = read_token(type, line, start);
else
token = read_word(line, start);
return (token);
}

64
src/parser/lexer_reader.c Normal file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:13:23 by sede-san #+# #+# */
/* Updated: 2026/02/13 21:13:23 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
static inline bool is_meta(char c);
t_token *read_token(
t_token_type type,
const char *line,
size_t *i
)
{
const size_t start = *i;
size_t end;
while (is_meta(line[*i]))
(*i)++;
end = *i;
while (ft_isspace(line[*i]))
(*i)++;
return (token_new(type, ft_substr(line, start, end - start)));
}
t_token *read_word(
const char *line,
size_t *i
)
{
const size_t start = *i;
bool in_single_quote;
bool in_double_quote;
in_single_quote = false;
in_double_quote = false;
while (line[*i] != '\0')
{
if (line[*i] == '\'' && !in_double_quote)
in_single_quote = !in_single_quote;
else if (line[*i] == '"' && !in_single_quote)
in_double_quote = !in_double_quote;
else if (!in_single_quote && !in_double_quote
&& (isspace(line[*i]) || is_meta(line[*i])))
break ;
(*i)++;
}
return (token_new(TOKEN_WORD, ft_substr(line, start, *i - start)));
}
static inline bool is_meta(
char c
)
{
return (c == '|' || c == '<' || c == '>');
}

67
src/parser/lexer_token.c Normal file
View File

@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:12:49 by sede-san #+# #+# */
/* Updated: 2026/02/13 21:12:49 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
t_token_type get_token_type(
const char *str
)
{
if (str == NULL || str[0] == '\0')
return (TOKEN_WORD);
if (str[0] == '|')
return (TOKEN_PIPE);
if (str[0] == '<')
{
if (str[1] == '<')
return (TOKEN_HEREDOC);
return (TOKEN_REDIRECT_IN);
}
if (str[0] == '>')
{
if (str[1] == '>')
return (TOKEN_APPEND);
return (TOKEN_REDIRECT_OUT);
}
return (TOKEN_WORD);
}
t_token *token_new(
t_token_type type,
char *text
)
{
t_token *token;
token = (t_token *)malloc(sizeof(t_token));
if (token == NULL)
return (NULL);
token->type = type;
token->value = text;
if (token->type == TOKEN_WORD && token->value == NULL)
{
free(token);
return (NULL);
}
return (token);
}
void token_clear(
t_token *token
)
{
if (token != NULL)
{
free(token->value);
free(token);
}
}

View File

@@ -6,73 +6,89 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 18:37:38 by sede-san #+# #+# */
/* Updated: 2025/10/23 22:53:35 by sede-san ### ########.fr */
/* Updated: 2026/02/13 21:22:06 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "parser.h"
#include "errors.h"
static char **expand_envs(char **argv);
static int count_argv(char **argv);
static t_list *parse_tokens(t_list *tokens);
char *parse(
/**
* @brief Converts a command line string into a list of commands.
*
* @param line The command line string to parse.
* @param minishell The minishell instance.
*
* @return A list of commands or `NULL` on error.
*/
t_list *parse(
char *line,
t_minishell *minishell
){
t_command command;
)
{
t_list *commands;
t_list *tokens;
if (!line || !*line)
if (line == NULL)
return (NULL);
(void)minishell;
command.argv = expand_envs(ft_split(line, SPACE));
command.argc = count_argv(command.argv);
if (!command.argc)
return (NULL);
////////////////////////////////////////////////////////////////////////////////
//* DEBUG
// int i = -1;
// while (command.argv[++i])
// printf("argv[%i]: %s\n", i, command.argv[i]);
////////////////////////////////////////////////////////////////////////////////
return (NULL);
tokens = lex(line);
commands = parse_tokens(tokens);
ft_lstclear(&tokens, (void (*)(void *))token_clear);
expand(&commands, minishell);
return (commands);
}
static char **expand_envs(
char **argv
){
int i;
char *env;
/**
* @brief Converts a list of tokens into a list of commands.
*
* @param tokens The list of tokens to parse.
*
* @return A list of commands or `NULL` on error.
*/
static t_list *parse_tokens(
t_list *tokens
)
{
t_list *commands;
t_command *command;
t_list *current_token;
t_list *new_command;
if (!argv)
if (tokens == NULL)
return (NULL);
else if (!*argv) // check if ft_split returned and empty matrix
commands = NULL;
current_token = tokens;
if (((t_token *)current_token->content)->type == TOKEN_PIPE)
return (syntax_error_unexpected_token(
(t_token *)current_token->content), NULL);
while (current_token != NULL)
{
ft_free_split(argv);
return (NULL);
command = command_new(&current_token);
if (command == NULL)
{
ft_lstclear(&commands, (void (*)(void *))command_clear);
return (NULL);
}
new_command = ft_lstnew(command);
if (new_command == NULL)
{
command_clear(command);
ft_lstclear(&commands, (void (*)(void *))command_clear);
return (malloc_error(), NULL);
}
ft_lstadd_back(&commands, new_command);
if (current_token != NULL)
{
if (current_token->next == NULL)
{
ft_lstclear(&commands, (void (*)(void *))command_clear);
return (syntax_error_unexpected_token(
(t_token *)current_token->content), NULL);
}
current_token = current_token->next;
}
}
i = -1;
while (argv[++i])
{
if (!ft_strchr(argv[i], DOLLAR)
|| (ft_strchr(argv[i], DOLLAR) && ft_strchr(argv[i], SINGLE_QUOTE) && ft_strchr(argv[i] + (ft_strchr(argv[i], SINGLE_QUOTE) + 1 - argv[i]), SINGLE_QUOTE))) // env is surrounded by single quote
continue ;
env = getenv(ft_strchr(argv[i], DOLLAR) + 1);
free(argv[i]);
if (env)
argv[i] = ft_strdup(env);
else
argv[i] = ft_strdup("");
}
return (argv);
}
static int count_argv(
char **argv
){
int i;
i = 0;
while (argv[i])
i++;
return (i);
return (commands);
}

120
src/parser/parser_command.c Normal file
View File

@@ -0,0 +1,120 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:26:42 by sede-san #+# #+# */
/* Updated: 2026/02/13 21:26:42 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
static t_list *ft_lstfind(t_list *lst, bool (*pre)(void *));
/**
* @brief Creates a new command from a list of tokens.
*
* @param tokens The list of tokens to create the command from.
*
* @return A new command or NULL on error.
*
* @note The `tokens` pointer is moved to the next command's tokens.
*/
t_command *command_new(
t_list **tokens
)
{
t_command *command;
t_list *current_token;
t_list *delimiter_token;
command = (t_command *)ft_calloc(1, sizeof(t_command));
if (command == NULL)
return (NULL);
current_token = *tokens;
delimiter_token = ft_lstfind(current_token, (bool (*)(void *))is_pipe);
while (command != NULL && current_token != delimiter_token)
command_add_tokens(&command, &current_token);
*tokens = current_token;
return (command);
}
/**
* @brief Adds a token to a command, updating the command's arguments and
* redirections as necessary.
*
* @param command The command to add the token to.
* @param tokens The list of tokens to add to the command.
*
* @note The `command` pointer can be free'd if there is an error while adding
* the token.
*/
void command_add_tokens(
t_command **command,
t_list **tokens
)
{
t_token *token;
token = (t_token *)(*tokens)->content;
if (is_redirection(token))
redirection_add(tokens, token, command);
else
words_add(tokens, command);
}
/**
* @brief Checks if a token is a redirection token.
*
* @param token The token to check.
*
* @return `true` if the token is a redirection token, `false` otherwise.
*/
bool is_redirection(
t_token *token
)
{
return (token->type == TOKEN_REDIRECT_IN
|| token->type == TOKEN_REDIRECT_OUT
|| token->type == TOKEN_APPEND
|| token->type == TOKEN_HEREDOC);
}
/**
* @brief Checks if a token is a pipe token.
*
* @param token The token to check.
*
* @return `true` if the token is a pipe token, `false` otherwise.
*/
bool is_pipe(
t_token *token
)
{
return (token->type == TOKEN_PIPE);
}
/**
* @brief Finds a node in a linked list that satisfies a given predicate.
*
* @param lst The linked list to search through.
* @param pre The predicate function to apply to each node's content.
*
* @returns The first node that satisfies the predicate or `NULL` if no such
* node exists or if the list is `NULL`.
*/
static t_list *ft_lstfind(
t_list *lst,
bool (*pre)(void *))
{
while (lst != NULL)
{
if (pre(lst->content))
return (lst);
lst = lst->next;
}
return (NULL);
}

139
src/parser/parser_expand.c Normal file
View File

@@ -0,0 +1,139 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:24:45 by sede-san #+# #+# */
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser_expand_internal.h"
static void command_clear_argv_expand(
t_command *command
)
{
int i;
i = 0;
while (i < command->argc)
{
free(command->argv[i]);
i++;
}
free(command->argv);
}
static bool command_set_expanded_argv(
t_command *command,
t_list *expanded_args,
t_minishell *minishell
)
{
char **argv;
t_list *current;
int argc;
int i;
argc = ft_lstsize(expanded_args);
argv = (char **)malloc(sizeof(char *) * (argc + 1));
if (argv == NULL)
return (ft_lstclear(&expanded_args, free),
parser_expand_malloc_error(minishell), false);
i = 0;
current = expanded_args;
while (current != NULL)
{
argv[i++] = (char *)current->content;
current = current->next;
}
argv[i] = NULL;
ft_lstclear_nodes(&expanded_args);
command_clear_argv_expand(command);
command->argc = argc;
command->argv = argv;
return (true);
}
static bool expand_argv(
t_command *command,
t_minishell *minishell
)
{
int i;
t_list *expanded_args;
t_list *fields;
t_list *last;
i = 0;
expanded_args = NULL;
while (i < command->argc)
{
fields = NULL;
if (!parser_expand_word_fields(command->argv[i], minishell,
true, &fields))
return (ft_lstclear(&expanded_args, free), false);
if (expanded_args == NULL)
expanded_args = fields;
else
{
last = ft_lstlast(expanded_args);
if (last != NULL)
last->next = fields;
}
i++;
}
return (command_set_expanded_argv(command, expanded_args, minishell));
}
static bool expand_redirections(
t_list *redirections,
t_minishell *minishell,
bool expand_vars
)
{
t_redirection *redirection;
char *expanded;
while (redirections != NULL)
{
redirection = (t_redirection *)redirections->content;
expanded = parser_expand_word(redirection->target, minishell,
expand_vars);
if (expanded == NULL)
return (false);
free(redirection->target);
redirection->target = expanded;
redirections = redirections->next;
}
return (true);
}
void expand(
t_list **commands,
t_minishell *minishell
)
{
t_list *current;
t_command *command;
if (commands == NULL || *commands == NULL)
return ;
current = *commands;
while (current != NULL)
{
command = (t_command *)current->content;
if (!expand_argv(command, minishell)
|| !expand_redirections(command->redirections, minishell, true)
|| !expand_redirections(command->heredocs, minishell, false))
{
ft_lstclear(commands, (void (*)(void *))command_clear);
*commands = NULL;
return ;
}
current = current->next;
}
}

View File

@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand_fields.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 13:15:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 13:15:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser_expand_internal.h"
static t_fields_ctx init_fields_ctx(
t_list **fields,
char **current,
t_field_state *state
)
{
t_fields_ctx ctx;
ctx.fields = fields;
ctx.current = current;
ctx.touched = &state->touched;
ctx.in_single_quote = &state->in_single_quote;
ctx.in_double_quote = &state->in_double_quote;
return (ctx);
}
static bool finish_word_fields(
t_fields_ctx ctx
)
{
if (*ctx.in_single_quote || *ctx.in_double_quote)
{
ft_lstclear(ctx.fields, free);
free(*ctx.current);
syntax_error_unexpected_token(NULL);
ctx.minishell->exit_status = 2;
return (false);
}
if ((*ctx.touched || (*ctx.current)[0] != '\0')
&& !parser_fields_push_field(ctx))
return (false);
free(*ctx.current);
return (true);
}
static bool process_word_fields(
const char *word,
t_fields_ctx ctx
)
{
size_t i;
i = 0;
while (word[i] != '\0')
{
if (!parser_fields_step(word, &i, ctx))
return (false);
}
return (true);
}
bool parser_expand_word_fields(
const char *word,
t_minishell *minishell,
bool expand_vars,
t_list **fields
)
{
char *current;
t_field_state state;
t_fields_ctx ctx;
*fields = NULL;
current = ft_strdup("");
if (current == NULL)
return (parser_expand_malloc_error(minishell), false);
state.touched = false;
state.in_single_quote = false;
state.in_double_quote = false;
ctx = init_fields_ctx(fields, &current, &state);
ctx.minishell = minishell;
ctx.expand_vars = expand_vars;
if (!process_word_fields(word, ctx))
return (ft_lstclear(fields, free), free(current), false);
return (finish_word_fields(ctx));
}

View File

@@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand_fields_step.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 14:12:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 14:12:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser_expand_internal.h"
static bool handle_quote_char(
const char *word,
size_t *i,
t_fields_ctx ctx
)
{
if (word[*i] == '\'' && !*ctx.in_double_quote)
{
*ctx.in_single_quote = !*ctx.in_single_quote;
*ctx.touched = true;
(*i)++;
return (true);
}
if (word[*i] == '\"' && !*ctx.in_single_quote)
{
*ctx.in_double_quote = !*ctx.in_double_quote;
*ctx.touched = true;
(*i)++;
return (true);
}
return (false);
}
static bool skip_dollar_quote_prefix(
const char *word,
size_t *i,
t_fields_ctx ctx
)
{
if (word[*i] == '$' && !*ctx.in_single_quote && !*ctx.in_double_quote
&& word[*i + 1] != '\0'
&& (word[*i + 1] == '\'' || word[*i + 1] == '\"'))
{
(*i)++;
return (true);
}
return (false);
}
static bool expand_dollar_token(
const char *word,
size_t *i,
t_fields_ctx ctx,
bool *handled
)
{
char *expanded;
*handled = false;
if (word[*i] != '$' || *ctx.in_single_quote || !ctx.expand_vars)
return (true);
*handled = true;
expanded = parser_expand_variable(word, i, ctx.minishell);
if (expanded == NULL)
return (false);
if (!*ctx.in_double_quote
&& !parser_fields_expand_unquoted_value(ctx, expanded))
return (free(expanded), false);
if (*ctx.in_double_quote && !parser_fields_append_text(ctx.current,
expanded, ctx.minishell))
return (free(expanded), false);
if (*ctx.in_double_quote && expanded[0] != '\0')
*ctx.touched = true;
free(expanded);
return (true);
}
bool parser_fields_step(
const char *word,
size_t *i,
t_fields_ctx ctx
)
{
char value[2];
bool handled;
if (handle_quote_char(word, i, ctx))
return (true);
if (skip_dollar_quote_prefix(word, i, ctx))
return (true);
if (!expand_dollar_token(word, i, ctx, &handled))
return (false);
if (handled)
return (true);
value[0] = word[(*i)++];
value[1] = '\0';
if (!parser_fields_append_text(ctx.current, value, ctx.minishell))
return (false);
*ctx.touched = true;
return (true);
}

View File

@@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand_fields_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 14:12:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 14:12:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser_expand_internal.h"
bool parser_fields_append_text(
char **current,
const char *value,
t_minishell *minishell
)
{
char *joined;
joined = ft_strnjoin(2, *current, (char *)value);
if (joined == NULL)
{
free(*current);
*current = NULL;
return (parser_expand_malloc_error(minishell), false);
}
free(*current);
*current = joined;
return (true);
}
static bool is_ifs_char(
char c
)
{
return (c == ' ' || c == '\t' || c == '\n');
}
bool parser_fields_push_field(
t_fields_ctx ctx
)
{
t_list *node;
node = ft_lstnew(*ctx.current);
if (node == NULL)
{
free(*ctx.current);
*ctx.current = NULL;
return (parser_expand_malloc_error(ctx.minishell), false);
}
ft_lstadd_back(ctx.fields, node);
*ctx.current = ft_strdup("");
if (*ctx.current == NULL)
return (ft_lstclear(ctx.fields, free),
parser_expand_malloc_error(ctx.minishell), false);
*ctx.touched = false;
return (true);
}
static void skip_ifs(
const char *expanded,
size_t *i
)
{
while (expanded[*i] != '\0' && is_ifs_char(expanded[*i]))
(*i)++;
}
bool parser_fields_expand_unquoted_value(
t_fields_ctx ctx,
const char *expanded
)
{
size_t i;
char value[2];
i = 0;
while (expanded[i] != '\0')
{
if (is_ifs_char(expanded[i]))
{
if ((*ctx.touched || (*ctx.current)[0] != '\0')
&& !parser_fields_push_field(ctx))
return (false);
skip_ifs(expanded, &i);
continue ;
}
value[0] = expanded[i];
value[1] = '\0';
if (!parser_fields_append_text(ctx.current, value, ctx.minishell))
return (false);
*ctx.touched = true;
i++;
}
return (true);
}

View File

@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 06:56:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSER_EXPAND_INTERNAL_H
# define PARSER_EXPAND_INTERNAL_H
# include "parser.h"
# include "errors.h"
# include "variables.h"
typedef struct s_fields_ctx t_fields_ctx;
void parser_expand_malloc_error(t_minishell *minishell);
char *parser_expand_variable(const char *word, size_t *i,
t_minishell *minishell);
char *parser_expand_word(const char *word, t_minishell *minishell,
bool expand_vars);
bool parser_expand_word_fields(const char *word, t_minishell *minishell,
bool expand_vars, t_list **fields);
bool parser_fields_append_text(char **current, const char *value,
t_minishell *minishell);
bool parser_fields_push_field(t_fields_ctx ctx);
bool parser_fields_expand_unquoted_value(t_fields_ctx ctx,
const char *expanded);
bool parser_fields_step(const char *word, size_t *i, t_fields_ctx ctx);
typedef struct s_word_ctx
{
char **result;
bool *in_single_quote;
bool *in_double_quote;
t_minishell *minishell;
bool expand_vars;
} t_word_ctx;
typedef struct s_fields_ctx
{
t_list **fields;
char **current;
bool *touched;
bool *in_single_quote;
bool *in_double_quote;
t_minishell *minishell;
bool expand_vars;
} t_fields_ctx;
typedef struct s_field_state
{
bool touched;
bool in_single_quote;
bool in_double_quote;
} t_field_state;
#endif

View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand_variable.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 06:56:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser_expand_internal.h"
void parser_expand_malloc_error(
t_minishell *minishell
)
{
minishell->exit = true;
malloc_error();
}
static bool is_var_char(
char c
)
{
return (ft_isalnum(c) || c == '_');
}
static char *dup_or_error(
const char *value,
t_minishell *minishell
)
{
char *dup;
dup = ft_strdup(value);
if (dup == NULL)
return (parser_expand_malloc_error(minishell), NULL);
return (dup);
}
static char *expand_named_variable(
const char *word,
size_t *i,
t_minishell *minishell
)
{
char *name;
char *value;
size_t start;
start = *i;
while (word[*i] != '\0' && is_var_char(word[*i]))
(*i)++;
name = ft_substr(word, start, *i - start);
if (name == NULL)
return (parser_expand_malloc_error(minishell), NULL);
value = get_var(name, minishell);
free(name);
if (value == NULL)
value = "";
return (dup_or_error(value, minishell));
}
char *parser_expand_variable(const char *word, size_t *i,
t_minishell *minishell)
{
char *expanded;
(*i)++;
if (word[*i] == '?')
{
expanded = ft_itoa(minishell->exit_status);
if (expanded == NULL)
return (parser_expand_malloc_error(minishell), NULL);
return ((*i)++, expanded);
}
if (word[*i] == '\0' || !is_var_char(word[*i]))
return (dup_or_error("$", minishell));
return (expand_named_variable(word, i, minishell));
}

View File

@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand_word.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 06:56:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser_expand_internal.h"
static bool word_toggle_quotes(
char c,
bool *in_single_quote,
bool *in_double_quote
)
{
if (c == '\'' && !*in_double_quote)
return (*in_single_quote = !*in_single_quote, true);
if (c == '\"' && !*in_single_quote)
return (*in_double_quote = !*in_double_quote, true);
return (false);
}
static bool word_append(
char **result,
const char *value,
t_minishell *minishell
)
{
char *joined;
joined = ft_strnjoin(2, *result, (char *)value);
if (joined == NULL)
{
free(*result);
*result = NULL;
return (parser_expand_malloc_error(minishell), false);
}
free(*result);
*result = joined;
return (true);
}
static bool word_step(
const char *word,
size_t *i,
t_word_ctx ctx
)
{
char *expanded;
char value[2];
if (word_toggle_quotes(word[*i], ctx.in_single_quote, ctx.in_double_quote))
return ((*i)++, true);
if (word[*i] == '$' && word[*i + 1] != '\0'
&& !*ctx.in_single_quote && !*ctx.in_double_quote
&& (word[*i + 1] == '\'' || word[*i + 1] == '\"'))
return ((*i)++, true);
if (word[*i] == '$' && !*ctx.in_single_quote && ctx.expand_vars)
{
expanded = parser_expand_variable(word, i, ctx.minishell);
if (expanded == NULL || !word_append(ctx.result, expanded,
ctx.minishell))
return (free(expanded), false);
return (free(expanded), true);
}
value[0] = word[*i];
value[1] = '\0';
(*i)++;
return (word_append(ctx.result, value, ctx.minishell));
}
char *parser_expand_word(
const char *word,
t_minishell *minishell,
bool expand_vars
)
{
char *result;
size_t i;
bool in_single_quote;
bool in_double_quote;
result = ft_strdup("");
if (result == NULL)
return (parser_expand_malloc_error(minishell), NULL);
i = 0;
in_single_quote = false;
in_double_quote = false;
while (word[i] != '\0')
{
if (!word_step(word, &i, (t_word_ctx){&result, &in_single_quote,
&in_double_quote, minishell, expand_vars}))
return (NULL);
}
if (in_single_quote || in_double_quote)
return (free(result), syntax_error_unexpected_token(NULL),
minishell->exit_status = 2, NULL);
return (result);
}

View File

@@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:28:35 by sede-san #+# #+# */
/* Updated: 2026/02/13 21:28:35 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
#include "errors.h"
static t_redirection *redirection_new(t_list **tokens);
/**
* @brief Creates a new redirection from a list of tokens.
*
* @param tokens The list of tokens to create the redirection from.
*
* @return A new redirection or `NULL` on error.
*/
static t_redirection *redirection_new(
t_list **tokens
)
{
t_redirection *redirection;
t_token *token;
redirection = (t_redirection *)malloc(sizeof(t_redirection));
if (redirection == NULL)
return (malloc_error(), NULL);
token = (t_token *)(*tokens)->content;
redirection->type = token->type;
*tokens = (*tokens)->next;
if (*tokens == NULL)
{
free(redirection);
return (syntax_error_unexpected_token(NULL), NULL);
}
token = (t_token *)(*tokens)->content;
if (token->type != TOKEN_WORD)
{
free(redirection);
while (*tokens != NULL)
*tokens = (*tokens)->next;
return (syntax_error_unexpected_token(token), NULL);
}
redirection->target = ft_strdup(token->value);
if (redirection->target == NULL)
{
free(redirection);
return (malloc_error(), NULL);
}
*tokens = (*tokens)->next;
return (redirection);
}
void redirection_clear(
t_redirection *redirection
)
{
if (redirection != NULL)
{
free(redirection->target);
free(redirection);
}
}
void redirection_add(
t_list **tokens,
t_token *token,
t_command **command
)
{
t_redirection *redirection;
t_list *redirection_tokens;
redirection = redirection_new(tokens);
if (redirection == NULL)
{
command_clear(*command);
*command = NULL;
return ;
}
redirection_tokens = ft_lstnew(redirection);
if (redirection_tokens == NULL)
{
free(redirection);
return (malloc_error());
}
if (token->type == TOKEN_HEREDOC)
ft_lstadd_back(&(*command)->heredocs, redirection_tokens);
else
ft_lstadd_back(&(*command)->redirections, redirection_tokens);
}

121
src/parser/parser_words.c Normal file
View File

@@ -0,0 +1,121 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_words.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:29:44 by sede-san #+# #+# */
/* Updated: 2026/02/14 03:34:38 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
char **args_to_array(t_list **args, size_t argc);
void command_clear_argv(t_command *command);
/**
* @brief Converts a list of arguments into an array.
*
* @param args The list of arguments to convert.
* @param argc The number of arguments in the list.
*
* @return An array of arguments or `NULL` on error.
*
* @note The `args` list is cleared after the conversion and set to NULL.
*/
char **args_to_array(
t_list **args,
size_t argc
)
{
char **argv;
t_list *arg;
size_t i;
argv = (char **)malloc(sizeof(char *) * (argc + 1));
if (argv == NULL)
return (NULL);
i = 0;
arg = *args;
while (arg != NULL)
{
argv[i] = (char *)arg->content;
arg = arg->next;
i++;
}
argv[i] = NULL;
ft_lstclear_nodes(args);
return (argv);
}
/**
* @brief Adds all consecutive word tokens to a command's argv and updates its
* argc accordingly.
*
* @param command The command to add the word tokens to.
* @param tokens The list of tokens to add to the command.
*/
void words_add(
t_list **tokens,
t_command **command
)
{
t_list *args;
t_list *arg;
t_token *token;
args = NULL;
arg = *tokens;
token = (t_token *)arg->content;
while (arg != NULL && token->type == TOKEN_WORD)
{
ft_lstadd_back(&args, ft_lstnew(ft_strdup(token->value)));
(*command)->argc++;
arg = arg->next;
if (arg != NULL)
token = (t_token *)arg->content;
}
*tokens = arg;
(*command)->argv = args_to_array(&args, (*command)->argc);
ft_lstclear_nodes(&args);
}
void command_clear_argv(
t_command *command
)
{
int i;
if (command->argv != NULL)
{
i = 0;
while (i < command->argc)
{
free(command->argv[i]);
i++;
}
free(command->argv);
command->argv = NULL;
}
}
/**
* @brief Clears a command, freeing all associated memory.
*
* @param command The command to clear.
*/
void command_clear(
t_command *command
)
{
if (command != NULL)
{
command_clear_argv(command);
ft_lstclear(&command->redirections,
(void (*)(void *))redirection_clear);
ft_lstclear(&command->heredocs, (void (*)(void *))redirection_clear);
free(command);
}
}

View File

@@ -0,0 +1,201 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* environment.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/01 09:12:39 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:32:42 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "core.h"
#include "variables.h"
#include "errors.h"
/**
* @brief Retrieves the value of an environment variable from the shell's
* environment hashmap.
*
* This function searches for the specified environment variable name in the
* minishell's environment variable hashmap and returns its associated value.
*
* @param name The name of the environment variable to retrieve.
* @param minishell Pointer to the minishell object.
*
* @return The value of the environment variable if found, NULL if not found
*/
char *get_env(
const char *name,
t_minishell *minishell
)
{
return (ft_hashmap_get(minishell->variables.environment, name));
}
/**
* @brief Sets an environment variable in the minishell's environment hashmap.
*
* This function adds a new environment variable or updates an existing one
* in the minishell's environment hashmap. If the variable already exists,
* the old value is freed to prevent memory leaks. If the variable is new,
* a duplicate of the key name is created for storage.
*
* @param name The name of the environment variable to set
* @param value The value to assign to the environment variable
* @param minishell Pointer to the minishell object.
*/
void set_env(
const char *name,
char *value,
t_minishell *minishell
)
{
t_hashmap *environment;
char *key;
char *val;
char *old_value;
environment = minishell->variables.environment;
key = (char *)name;
if (key != NULL && !ft_hashmap_contains_key(environment, key))
{
key = ft_strdup(name);
if (key == NULL)
{
minishell->exit = true;
malloc_error();
return ;
}
}
val = value;
if (val == NULL)
val = ft_strdup("");
else
val = ft_strdup(value);
if (val == NULL)
{
if (key != name)
free(key);
minishell->exit = true;
malloc_error();
return ;
}
old_value = ft_hashmap_put(environment, key, val);
if (old_value != NULL)
free(old_value);
}
/**
* @brief Removes an environment variable by name.
*
* @param name The name of the environment variable to remove.
* @param minishell Pointer to the minishell structure.
*
* @note If the environment variable exists, it will be removed from the hashmap
* and its associated value will be freed.
*/
void unset_env(
const char *name,
t_minishell *minishell
)
{
t_hashmap *environment;
char *val;
environment = minishell->variables.environment;
val = ft_hashmap_remove(environment, (void *)name);
if (val != NULL)
free(val);
}
/**
* @brief Converts the environment variables hashmap to an envp array format.
*
* This function extracts all environment variables from the minishell's
* environment hashmap and converts them into a NULL-terminated array of
* strings in the format "KEY=VALUE".
*
* @param minishell Pointer to the minishell structure containing the environment
* variables hashmap.
*
* @return A dynamically allocated array of strings representing environment
* variables in "KEY=VALUE" format, terminated by NULL. Returns NULL
* if memory allocation fails. The caller is responsible for freeing
* the returned array and its individual string elements using
* the `free_envp()` function.
*
* @note The function allocates memory for both the array and individual
* strings using malloc and ft_strnjoin respectively.
* @note The returned array size is environment->size + 1 to accommodate
* the NULL terminator.
*/
char **get_envp(
t_minishell *minishell
)
{
char **envp;
t_list *env_list;
t_list *env;
t_map_entry *entry;
size_t i;
env_list = ft_hashmap_entries(minishell->variables.environment);
envp = (char **)malloc(
(minishell->variables.environment->size + 1) * sizeof(char *)
);
if (envp != NULL)
{
i = 0;
env = env_list;
while (env != NULL)
{
entry = env->content;
envp[i++] = ft_strnjoin(3, entry->key, "=", entry->value);
env = env->next;
}
envp[i] = NULL;
}
ft_lstclear_nodes(&env_list);
return (envp);
}
/**
* @brief Parses and stores environment variables from envp array into a hashmap
*
* This function iterates through the environment variables array (envp) and
* splits each variable string on the '=' delimiter to separate the variable
* name from its value. Each name-value pair is then stored in the minishell's
* environment hashmap for later retrieval.
*
* @param envp Array of environment variable strings in "NAME=value" format
* @param minishell Pointer to the minishell structure containing the environment
* hashmap
*
* @note The function assumes envp strings are in the standard format
* "NAME=value"
*/
void set_envp(
char **envp,
t_minishell *minishell
)
{
t_hashmap **environment;
char **key_value;
if (minishell == NULL || envp == NULL)
return ;
environment = &minishell->variables.environment;
*environment = ft_hashmap_new(32, ft_hashmap_hashstr, ft_hashmap_strcmp);
if (*environment == NULL)
return ;
while (*envp != NULL)
{
key_value = ft_split(*envp, '=');
set_env(key_value[0], key_value[1], minishell);
ft_free_split(key_value);
envp++;
}
}

View File

@@ -0,0 +1,120 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* internal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:29:43 by sede-san #+# #+# */
/* Updated: 2026/02/14 01:39:36 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "core.h"
#include "errors.h"
/**
* @brief Retrieves the value of an internal variable by name.
*
* @param name The name of the internal variable to retrieve.
* @param minishell Pointer to the minishell structure.
*
* @return The value of the internal variable if found, or NULL if not found.
*/
char *get_int(
const char *name,
t_minishell *minishell
)
{
return (ft_hashmap_get(minishell->variables.internal, name));
}
/**
* @brief Stores a variable as internal.
*
* @param name The name of the internal variable to set.
* @param value The value to associate with the internal variable.
* @param minishell Pointer to the minishell structure.
*
* @note If the internal variable already exists, its value will be updated and
* the old value will be freed. If it does not exist, a new internal
* variable will be created.
*/
void set_int(
const char *name,
char *value,
t_minishell *minishell
)
{
t_hashmap *internal;
char *key;
char *val;
char *old_val;
internal = minishell->variables.internal;
key = (char *)name;
if (key != NULL && !ft_hashmap_contains_key(internal, key))
{
key = ft_strdup(name);
if (key == NULL)
return (minishell->exit = true, malloc_error());
}
val = value;
if (val != NULL)
val = ft_strdup(value);
if (val == NULL)
{
if (key != name)
free(key);
return (minishell->exit = true, malloc_error());
}
old_val = ft_hashmap_put(internal, key, val);
if (old_val != NULL)
free(old_val);
}
/**
* @brief Removes an internal variable by name.
*
* @param name The name of the internal variable to remove.
* @param minishell Pointer to the minishell structure.
*
* @note If the internal variable exists, it will be removed from the hashmap
* and its associated value will be freed.
*/
void unset_int(
const char *name,
t_minishell *minishell
)
{
char *value;
value = ft_hashmap_remove(minishell->variables.internal, (void *)name);
if (value != NULL)
free(value);
}
/**
* @brief Initializes the internal variables hashmap with default values.
*
* @param minishell Pointer to the minishell structure to initialize.
*
* @warning This function must be called only once during the initialization of
* the minishell.
*/
void set_intp(
t_minishell *minishell
)
{
if (minishell == NULL)
return ;
minishell->variables.internal
= ft_hashmap_new(16, ft_hashmap_hashstr, ft_hashmap_strcmp);
if (minishell->variables.internal == NULL)
return ;
set_int("?", "0", minishell);
set_int("_", "minishell", minishell);
set_int("PS1", DEFAULT_PS1, minishell);
set_int("PS2", DEFAULT_PS2, minishell);
}

50
src/variables/variables.c Normal file
View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* variables.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 21:41:44 by sede-san #+# #+# */
/* Updated: 2026/02/14 00:52:12 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "variables.h"
/**
* @brief Retrieves the value of a variable by name.
*
* @param name The name of the variable to retrieve.
* @param minishell Pointer to the minishell structure.
*
* @return The value of the variable if found, or NULL if not found.
*/
char *get_var(
const char *name,
t_minishell *minishell
)
{
char *value;
value = get_int(name, minishell);
if (value == NULL)
value = get_env(name, minishell);
return (value);
}
void set_var(const char *name, char *value, t_minishell *minishell)
{
if (ft_hashmap_contains_key(minishell->variables.internal, name))
set_int(name, value, minishell);
set_env(name, value, minishell);
}
void unset_var(
const char *name,
t_minishell *minishell
)
{
unset_int(name, minishell);
unset_env(name, minishell);
}

204
tests/builtins_edge_cases.sh Executable file
View File

@@ -0,0 +1,204 @@
#!/usr/bin/env bash
set -u
ROOT_DIR="$(cd -- "$(dirname -- "$0")/.." && pwd)"
MSH_BIN="$ROOT_DIR/minishell"
TMP_DIR="$(mktemp -d /tmp/minishell-builtins-tests.XXXXXX)"
PASS_COUNT=0
FAIL_COUNT=0
CASE_OUT=""
CASE_ERR=""
CASE_EC=0
CASE_NAME=""
CASE_FAILED=0
cleanup() {
rm -rf "$TMP_DIR"
}
pass() {
printf "PASS: %s\n" "$1"
PASS_COUNT=$((PASS_COUNT + 1))
}
fail() {
printf "FAIL: %s\n" "$1"
FAIL_COUNT=$((FAIL_COUNT + 1))
CASE_FAILED=1
}
assert_exit_code() {
local expected="$1"
if [ "$CASE_EC" -ne "$expected" ]; then
fail "$CASE_NAME -> exit code esperado $expected, obtenido $CASE_EC"
fi
}
assert_err_contains() {
local text="$1"
if ! grep -Fq -- "$text" "$CASE_ERR"; then
fail "$CASE_NAME -> stderr no contiene: $text"
fi
}
assert_out_contains() {
local text="$1"
if ! grep -Fq -- "$text" "$CASE_OUT"; then
fail "$CASE_NAME -> stdout no contiene: $text"
fi
}
assert_out_regex_count() {
local regex="$1"
local expected="$2"
local count
count="$(grep -Ec -- "$regex" "$CASE_OUT" || true)"
if [ "$count" -ne "$expected" ]; then
fail "$CASE_NAME -> regex [$regex] esperado $expected, obtenido $count"
fi
}
run_case() {
local name="$1"
local input="$2"
local expected_exit="$3"
local clean_env="${4:-0}"
CASE_NAME="$name"
CASE_OUT="$TMP_DIR/$name.out"
CASE_ERR="$TMP_DIR/$name.err"
CASE_EC=0
CASE_FAILED=0
if [ "$clean_env" -eq 1 ]; then
printf "%b" "$input" | env -i PATH="$PATH" "$MSH_BIN" >"$CASE_OUT" 2>"$CASE_ERR"
else
printf "%b" "$input" | "$MSH_BIN" >"$CASE_OUT" 2>"$CASE_ERR"
fi
CASE_EC=$?
assert_exit_code "$expected_exit"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME"
fi
}
run_pwd_deleted_dir_case() {
local tmpd
tmpd="$TMP_DIR/pwd_deleted_dir"
mkdir -p "$tmpd"
run_case "pwd_deleted_dir" \
"cd $tmpd\n/bin/rmdir $tmpd\npwd\nexit\n" \
1
assert_err_contains "minishell: pwd:"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
}
trap cleanup EXIT
if [ ! -x "$MSH_BIN" ]; then
printf "Compilando minishell...\n"
if ! make -C "$ROOT_DIR" >/dev/null; then
printf "No se pudo compilar minishell\n"
exit 1
fi
fi
run_case "exit_non_numeric" "exit abc\n" 2
assert_err_contains "numeric argument required"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "exit_sign_only" "exit +\n" 2
assert_err_contains "numeric argument required"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "exit_negative_wrap" "exit -1\n" 255
run_case "exit_too_many_args" "exit 7 8\npwd\nexit\n" 0
assert_err_contains "too many arguments"
assert_out_contains "$ROOT_DIR"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "cd_home_not_set" "cd\nexit\n" 1 1
assert_err_contains "HOME not set"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "cd_invalid_path" "cd /definitely/not/here\nexit\n" 1
assert_err_contains "No such file or directory"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "cd_dash_roundtrip" "cd /tmp\ncd -\npwd\nexit\n" 0
assert_out_contains "$ROOT_DIR"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_pwd_deleted_dir_case
run_case "echo_flags" "echo -nn hi\necho hi-there\nexit\n" 0
assert_out_contains "himinishell >"
assert_out_contains "hi-there"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "env_builtin_with_arg" "env EXTRA\nexit\n" 1
assert_err_contains "too many arguments"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "export_unset_cycle" \
"export FOO=bar\n/usr/bin/env\nunset FOO\n/usr/bin/env\nexit\n" \
0
assert_out_regex_count "^FOO=bar$" 1
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "env_builtin_lists_exported" \
"export FOO=bar\nenv\nunset FOO\nenv\nexit\n" \
0
assert_out_regex_count "^FOO=bar$" 1
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "export_invalid_identifier" "export 1A=2\nexit\n" 1
assert_err_contains "not a valid identifier"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "unset_invalid_identifier" "unset 1A\nexit\n" 1
assert_err_contains "not a valid identifier"
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
run_case "export_without_value" "unset ZZZ\nexport ZZZ\n/usr/bin/env\nexit\n" 0
assert_out_regex_count "^ZZZ=$" 1
if [ "$CASE_FAILED" -eq 0 ]; then
pass "$CASE_NAME assertions"
fi
printf "\nResumen tests builtins: PASS=%d FAIL=%d\n" "$PASS_COUNT" "$FAIL_COUNT"
if [ "$FAIL_COUNT" -ne 0 ]; then
exit 1
fi
exit 0