Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ttsubo <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/04/03 12:55:20 by ttsubo #+# #+# #
# Updated: 2025/05/02 14:33:45 by ttsubo ### ########.fr #
# Updated: 2025/05/02 22:01:21 by ttsubo ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -53,8 +53,9 @@ INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \
redirect.c fds.c wait_pipeline.c
TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \
is_quote_closed.c get_token_capa.c is_redirect_validate.c
PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c \
expand_env.c
PARSER_SRC = allocate_cmds.c parser.c parser_free.c parser_utils.c setup_cmds.c \
allocate_cmds_utils.c allocate_cmds_utils_2.c \
expand_env.c expand_tokens.c
BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c export.c \
env_utils.c env_utils_2.c env_utils_3.c builtin_utils.c \
export_exec.c export_print_sorted_env.c export_error.c split_key_value.c
Expand Down
17 changes: 13 additions & 4 deletions inc/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */
/* Updated: 2025/04/29 12:17:18 by ttsubo ### ########.fr */
/* Updated: 2025/05/02 21:25:06 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_H
# define PARSER_H

# include "cmd.h"
# include "libft.h"
# include "parser_free.h"
# include "expand_env.h"
# include "libft.h"

size_t count_cmds(char **tokens);
char **next_cmd_start(char **token_ptr);
t_cmd **calloc_cmds(char **tokens);
t_cmd *calloc_cmd(void);
char **calloc_argv(char **tokens);
char *calloc_arg(char *token);

t_cmd **parser(char **tokens, t_minish *minish);
char **expand_tokens(char **tokens, t_minish *minish);
void set_cmd_type(t_cmd *cmd, char *token);
size_t cmds_len(t_cmd **cmds);
size_t count_args_until_separator(char **tokens);
int is_separator(char *token);
void free_cmds(t_cmd **cmds, size_t count);
t_cmd **allocate_cmds(char **tokens);
t_cmd **setup_cmds(t_cmd **cmds, char **tokens, t_minish *minish);
t_cmd **setup_cmds(t_cmd **cmds, char **tokens);

#endif
20 changes: 20 additions & 0 deletions inc/parser_free.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_free.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/02 21:22:56 by ttsubo #+# #+# */
/* Updated: 2025/05/02 21:27:46 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_FREE_H
# define PARSER_FREE_H

void free_tokens(char **tokens);
void free_cmd(t_cmd *cmd);
void free_cmds(t_cmd **cmds);

#endif
10 changes: 6 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */
/* Updated: 2025/04/29 13:06:39 by dayano ### ########.fr */
/* Updated: 2025/05/02 21:25:49 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -61,7 +61,7 @@ static bool prompt(char *program_name, t_minish *minish)
if (cmds[0]->argc > 0)
minish->last_status = invoke_commands(cmds[0], minish);
_free_tokens(tokens);
free_cmds(cmds, cmds_len(cmds));
free_cmds(cmds);
free(line);
return (true);
}
Expand All @@ -70,13 +70,15 @@ int main(int argc, char **argv, char **envp)
{
t_minish *minish;
char *program_name;
int last_status;

(void)argc;
program_name = argv[0];
minish = initialize(envp);
minish_signal();
while (prompt(program_name, minish))
;
last_status = minish->last_status;
destroy_minish(minish);
return (minish->last_status);
return (last_status);
}
73 changes: 44 additions & 29 deletions src/parser/allocate_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,71 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 12:19:28 by ttsubo #+# #+# */
/* Updated: 2025/04/26 20:39:41 by ttsubo ### ########.fr */
/* Updated: 2025/05/02 21:26:21 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "parser.h"

static size_t _count_cmds(char **tokens)
static int _allocate_argv(t_cmd *cmd, char **token_ptr)
{
size_t i;
size_t cmd_count;
size_t arg_i;
size_t len;

i = 0;
cmd_count = 1;
while (tokens[i])
cmd->argv = calloc_argv(token_ptr);
if (!cmd->argv)
return (1);
arg_i = 0;
len = count_args_until_separator(token_ptr);
while (arg_i < len)
{
if (is_separator(tokens[i]))
cmd_count++;
i++;
cmd->argv[arg_i] = calloc_arg(token_ptr[arg_i]);
if (!cmd->argv[arg_i])
{
while (0 < arg_i)
free(cmd->argv[--arg_i]);
free(cmd->argv);
return (1);
}
arg_i++;
}
return (cmd_count);
return (0);
}

static t_cmd **_allocate_cmds(size_t count)
static t_cmd *_allocate_cmd(char **token_ptr)
{
size_t i;
t_cmd **cmds;
t_cmd *cmd;

i = 0;
cmds = ft_calloc(count + 1, sizeof(t_cmd *));
if (!cmds)
cmd = calloc_cmd();
if (!cmd)
return (NULL);
while (i < count)
{
cmds[i] = ft_calloc(1, sizeof(t_cmd));
if (!cmds[i])
return (free_cmds(cmds, i), NULL);
if (i > 0)
cmds[i - 1]->next = cmds[i];
i++;
}
cmds[i] = NULL;
return (cmds);
if (_allocate_argv(cmd, token_ptr))
return (free(cmd), NULL);
return (cmd);
}

t_cmd **allocate_cmds(char **tokens)
{
char **token_ptr;
t_cmd **cmds;
size_t cmd_i;
size_t len;

cmds = _allocate_cmds(_count_cmds(tokens));
cmds = calloc_cmds(tokens);
if (!cmds)
return (NULL);
cmd_i = 0;
len = count_cmds(tokens);
token_ptr = tokens;
while (cmd_i < len)
{
cmds[cmd_i] = _allocate_cmd(token_ptr);
if (!cmds[cmd_i])
return (free_cmds(cmds), NULL);
if (cmd_i > 0)
cmds[cmd_i - 1]->next = cmds[cmd_i];
token_ptr = next_cmd_start(token_ptr);
cmd_i++;
}
return (cmds);
}
63 changes: 63 additions & 0 deletions src/parser/allocate_cmds_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocate_cmds_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 12:19:28 by ttsubo #+# #+# */
/* Updated: 2025/05/02 18:12:06 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "parser.h"

char **next_cmd_start(char **token_ptr)
{
size_t token_i;

token_i = 0;
while (token_ptr[token_i] && !is_separator(token_ptr[token_i]))
token_i++;
if (token_ptr[token_i])
token_i++;
return (&token_ptr[token_i]);
}

t_cmd **calloc_cmds(char **tokens)
{
t_cmd **cmds;
size_t cmd_i;
size_t token_i;

token_i = 0;
cmd_i = 1;
while (tokens[token_i])
{
if (is_separator(tokens[token_i]))
cmd_i++;
token_i++;
}
cmds = ft_calloc(sizeof(t_cmd *), cmd_i + 1);
return (cmds);
}

t_cmd *calloc_cmd(void)
{
return (ft_calloc(sizeof(t_cmd), 1));
}

char **calloc_argv(char **tokens)
{
size_t token_i;

token_i = 0;
while (tokens[token_i] && !is_separator(tokens[token_i]))
token_i++;
return (ft_calloc(sizeof(char *), token_i + 1));
}

char *calloc_arg(char *token)
{
return (ft_strdup(token));
}
29 changes: 29 additions & 0 deletions src/parser/allocate_cmds_utils_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocate_cmds_utils_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 12:19:28 by ttsubo #+# #+# */
/* Updated: 2025/05/02 16:58:45 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "parser.h"

size_t count_cmds(char **tokens)
{
size_t i;
size_t count;

i = 0;
count = 1;
while (tokens[i])
{
if (is_separator(tokens[i]))
count++;
i++;
}
return (count);
}
Loading