-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/137 perser #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feature/137 perser #162
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6849ed2
add perser (empty)
5c82d17
wip
db8f760
Merge branch 'main' into feature/137_perser
253fbb8
add create_cmds
72db763
rename
95932bc
next処理追加
868b70d
リファクタリング
f08615c
add setup_cmds
8ee249b
割当失敗時のハンドリング追加
3b8a42c
call setup_cmds
f06b7c1
test追加
817fad5
add allocate_argv
27e521a
fix test
598b29e
fix norm
12208f7
fix norm (TOO MANY LINES以外)
fbb02cd
pickup _next_cmd
aa2955c
mainに組込み
888d31e
fix setup_cmds
dayano74 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 # | ||
| # # | ||
| # **************************************************************************** # | ||
|
|
||
|
|
@@ -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/ | ||
|
|
@@ -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 | ||
|
|
@@ -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 $@ | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
| /* */ | ||
| /* ************************************************************************** */ | ||
|
|
||
|
|
@@ -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> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, ">>") | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| cmds[++cmd_i] = NULL; | ||
| return (cmds); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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になることはないと思います。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!