Skip to content
Merged
9 changes: 8 additions & 1 deletion 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/04/24 12:11:25 by ttsubo ### ########.fr #
# Updated: 2025/04/24 15:32:30 by ttsubo ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -15,6 +15,7 @@ CC = cc

SRC_DIR = src/
TOKENIZER_SRC_DIR = src/tokenizer/
PERSER_SRC_DIR = src/perser/
BUILTIN_SRC_DIR = src/builtin/
INC_DIR = inc/
OBJ_DIR = obj/
Expand All @@ -28,14 +29,17 @@ L_FLG = -lreadline -lft
SRC = main.c minish_signal.c initialize.c debug.c
TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \
is_quote_closed.c get_token_capa.c is_redirect_validate.c
PERSER_SRC = allocate_cmds.c perser.c perser_utils.c setup_cmds.c
BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c \
env_utils.c env_utils_2.c builtin_utils.c

SRCS = $(addprefix $(SRC_DIR), $(SRC))
SRCS += $(addprefix $(TOKENIZER_SRC_DIR), $(TOKENIZER_SRC))
SRCS += $(addprefix $(PERSER_SRC_DIR), $(PERSER_SRC))
SRCS += $(addprefix $(BUILTIN_SRC_DIR), $(BUILTIN_SRC))
OBJS = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(TOKENIZER_SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(PERSER_SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(BUILTIN_SRC:.c=.o))

LIBFT=libft.a
Expand All @@ -54,6 +58,9 @@ $(OBJ_DIR)%.o: $(SRC_DIR)%.c
$(OBJ_DIR)%.o: $(TOKENIZER_SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

$(OBJ_DIR)%.o: $(PERSER_SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

$(OBJ_DIR)%.o: $(BUILTIN_SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

Expand Down
3 changes: 2 additions & 1 deletion inc/main.h
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:50:38 by ttsubo #+# #+# */
/* Updated: 2025/04/24 12:13:49 by ttsubo ### ########.fr */
/* Updated: 2025/04/24 15:28:00 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,6 +24,7 @@
# include "minish_signal.h"
# include "struct.h"
# include "tokenizer.h"
# include "perser.h"
# include <readline/history.h>
# include <readline/readline.h>
# include <stdbool.h>
Expand Down
26 changes: 26 additions & 0 deletions inc/perser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* perser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */
/* Updated: 2025/04/24 15:22:00 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PERSER_H
# define PERSER_H

# include "cmd.h"
# include "libft.h"

t_cmd **perser(char **tokens);
size_t cmds_len(t_cmd **cmds);
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);

#endif
40 changes: 26 additions & 14 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,36 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */
/* Updated: 2025/04/24 12:10:52 by ttsubo ### ########.fr */
/* Updated: 2025/04/24 15:34:41 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

/**
* @brief debug用 tokenを表示します。 後で削除予定。
* @brief debug用 cmdを表示します。 後で削除予定。
*
* @param tokens
*/
static void _dbg_show_tokens(char **tokens)
static void _dbg_show_cmd(t_cmd **cmds)
{
printf("DEBUG: show tokens\n");
printf("[");
while (*tokens)
printf("\"%s\", ", *tokens++);
printf("NULL]\n");
size_t cmd_i;
size_t arg_i;

cmd_i = 0;
printf("DEBUG: show cmds\n");
while (cmds[cmd_i])
{
arg_i = 0;
printf("cmd[%zu]\n", cmd_i);
printf("\tcmd->type=%d\n", cmds[cmd_i]->type);
printf("\tcmd->argc=%d\n", cmds[cmd_i]->argc);
printf("\tcmd->argv=[");
while (cmds[cmd_i]->argv[arg_i])
printf("%s,", cmds[cmd_i]->argv[arg_i++]);
printf("NULL]\n");
cmd_i++;
}
}

static void _free_tokens(char **tokens)
Expand Down Expand Up @@ -58,25 +70,25 @@ static bool prompt(char *program_name, t_minish *minish, int *status)
{
char *line;
char **tokens;
t_cmd *cmd;
t_cmd **cmds;

(void) status;
line = readline("minish>");
if (!line)
return (false);
if (line[0] != '\0')
add_history(line);
tokens = tokenizer(line);
_dbg_show_tokens(tokens);
cmd = parse_command_line(line);
if (!cmd)
cmds = perser(tokens);
if (!cmds)
{
error_mes(program_name, ": syntax error\n");
cleanup_minish(minish);
return (false);
}
if (cmd->argc > 0)
*status = invoke_commands(cmd);
_dbg_show_cmd(cmds);
_free_tokens(tokens);
free_cmds(cmds, cmds_len(cmds));
free(line);
return (true);
}
Expand Down
61 changes: 61 additions & 0 deletions src/perser/allocate_cmds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocate_cmds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 12:19:28 by ttsubo #+# #+# */
/* Updated: 2025/04/24 14:59:36 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "perser.h"

static size_t _count_cmds(char **tokens)
{
size_t i;
size_t cmd_count;

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

static t_cmd **_allocate_cmds(size_t count)
{
size_t i;
t_cmd **cmds;

i = 0;
cmds = ft_calloc(count, sizeof(t_cmd *) + 1);
if (!cmds)
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);
}

t_cmd **allocate_cmds(char **tokens)
{
t_cmd **cmds;

cmds = _allocate_cmds(_count_cmds(tokens));
if (!cmds)
return (NULL);
return (cmds);
}
32 changes: 32 additions & 0 deletions src/perser/perser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* perser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 14:14:32 by ttsubo #+# #+# */
/* Updated: 2025/04/24 14:38:19 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "perser.h"

/**
* @brief
*
* @param tokens
* @return t_cmd**
*/
t_cmd **perser(char **tokens)
{
t_cmd **cmds;

cmds = allocate_cmds(tokens);
if (!cmds)
return (NULL);
cmds = setup_cmds(cmds, tokens);
if (!cmds)
return (NULL);
return (cmds);
}
51 changes: 51 additions & 0 deletions src/perser/perser_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* perser_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:40:56 by ttsubo #+# #+# */
/* Updated: 2025/04/24 15:00:46 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "perser.h"

size_t cmds_len(t_cmd **cmds)
{
size_t i;

i = 0;
while (cmds[i])
i++;
return (i);
}

void free_cmds(t_cmd **cmds, size_t count)
{
size_t cmd_i;
size_t arg_i;

cmd_i = 0;
while (cmd_i < count)
{
arg_i = 0;
while (cmds[cmd_i]->argv && cmds[cmd_i]->argv[arg_i])
free(cmds[cmd_i]->argv[arg_i++]);
free(cmds[cmd_i]->argv);
free(cmds[cmd_i]);
cmd_i++;
}
free(cmds);
}

int is_separator(char *token)
{
return (!ft_strcmp(token, "|")
|| !ft_strcmp(token, "<")
|| !ft_strcmp(token, ">")
|| !ft_strcmp(token, "<<")
|| !ft_strcmp(token, ">>")
);
}
84 changes: 84 additions & 0 deletions src/perser/setup_cmds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setup_cmds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:38:36 by ttsubo #+# #+# */
/* Updated: 2025/04/24 22:06:17 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "perser.h"

static int _allocate_argv(t_cmd *cmd, char **tokens)
{
size_t token_i;
size_t argc;

token_i = 0;
argc = 0;
while (tokens[token_i] && !is_separator(tokens[token_i]))
{
token_i++;
argc++;
}
cmd->argv = ft_calloc(argc + 1, sizeof(char *));
if (!cmd->argv)
return (1);
return (0);
}

static void _set_type(t_cmd *cmd, char *token)
{
if (!ft_strcmp(token, ">"))
cmd->type = REDIR_OUT;
else if (!ft_strcmp(token, ">>"))
cmd->type = REDIR_APPEND;
else if (!ft_strcmp(token, "<"))
cmd->type = REDIR_IN;
else if (!ft_strcmp(token, "<<"))
cmd->type = REDIR_HEREDOC;
else
cmd->type = REDIR_NONE;
}

static void _next_cmd(t_cmd **cmds, size_t *cmd_i, size_t *arg_i, char *token)
{
cmds[*cmd_i]->argv[*arg_i] = NULL;
cmds[*cmd_i]->argc = *arg_i;
(*arg_i) = 0;
(*cmd_i)++;
_set_type(cmds[*cmd_i], token);
_allocate_argv(cmds[*cmd_i], &token);
}

t_cmd **setup_cmds(t_cmd **cmds, char **tokens)
{
size_t token_i;
size_t cmd_i;
size_t arg_i;

token_i = 0;
cmd_i = 0;
arg_i = 0;
_set_type(cmds[0], tokens[token_i]);
_allocate_argv(cmds[0], tokens);
while (tokens[token_i])
{
if (is_separator(tokens[token_i]))
_next_cmd(cmds, &cmd_i, &arg_i, tokens[token_i]);
else
{
cmds[cmd_i]->argv[arg_i] = ft_strdup(tokens[token_i]);
if (!cmds[cmd_i]->argv[arg_i])
return (free_cmds(cmds, cmds_len(cmds)), NULL);
arg_i++;
}
token_i++;
}
cmds[cmd_i]->argc = arg_i;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmds[cmd_i]->argc = arg_i;を追加ました。コマンド単体のときにargcが0になることはないと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!

cmds[++cmd_i] = NULL;
return (cmds);
}
Loading