Skip to content
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

builtinコマンド exitを追加 #72

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BUILTIN_PATH = builtin
SRC_PATHS = $(CURRENT_PATH) $(EXPANDER_PATH) $(BUILTIN_PATH) $(EXECUTE_PATH) $(PARSER_PATH) $(LEXER_PATH) $(AST_PATH) $(UTILS_PATH)
SRCS = $(foreach path, $(SRC_PATHS), $(wildcard $(path)/*.c))

VPATH = $(CURRENT_PATH):$(BUILTIN_PATH):$(EXECUTE_PATH):$(PARSER_PATH):$(LEXER_PATH):$(AST_PATH):$(UTILS_PATH)
VPATH = $(CURRENT_PATH):$(BUILTIN_PATH):$(EXECUTE_PATH):$(PARSER_PATH):$(LEXER_PATH):$(AST_PATH):$(UTILS_PATH):$(EXPANDER_PATH)

OBJDIR = ./obj
OBJS = $(addprefix $(OBJDIR)/, $(notdir $(SRCS:.c=.o)))
Expand Down
3 changes: 3 additions & 0 deletions builtin/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include "../libft/libft.h"
#include "../utils/utils.h"

int cd(int argc, char **argv);
int pwd();
int builtin_exit(int argc, char **argv);

#endif //BUILTIN_H
23 changes: 23 additions & 0 deletions builtin/builtin_exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "builtin.h"

int builtin_exit(int argc, char **argv)
{
int num;

ft_putendl_fd("exit", STDERR_FILENO);
if (argc == 1)
exit(EXIT_SUCCESS);
if (argc == 2)
{
if (!atoi_strict(argv[1], &num))
{
ft_putendl_fd("minishell: exit: ", STDERR_FILENO);
ft_putendl_fd(argv[1], STDERR_FILENO);
ft_putendl_fd(": numeric argument required", STDERR_FILENO);
return (EXIT_FAILURE);
Copy link
Collaborator

Choose a reason for hiding this comment

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

$ exit aaa || echo hello
exit
bash: exit: aaa: numeric argument required
[MY_ZSH]$

exitをパイプで繋いだ時に、コマンドの成功・失敗の判定じゃなくてプログラム自体をexitしてる気がします。

Copy link
Collaborator

Choose a reason for hiding this comment

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

[20:16:42 minishell]$ exit 0 aaa || echo hello
exit
bash: exit: too many arguments
[20:16:46 minishell]$ exit 0 aaa && echo hello
exit
bash: exit: too many arguments
$

このパターンがかなり謎いですね。。
exitが失敗して||で繋いでもecho helloが実行されないという。。。

Copy link
Owner Author

Choose a reason for hiding this comment

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

$ exit aaa || echo hello

このケースはパイプ(?)・OR IFの影響じゃなくて、exit aaaがシンプルに成功して、exitしてるだけかと思います。ちなみにだるかったので対応してないです。(fukadaさんのテストケース見ると、exit -- 42が成功するとかよく分からんテストケースがありますww)

exit 0 aaa || echo hello

このパターン謎いっすね。考えられるものとしては、exitがきたあとは何も読まないとかですかね... PRの方にも書いたんですけど、とりあえずexit 0`とかで終了できるようにしたかっただけなので、ここら辺あんまりまだ対応する気が起きないです。Issueにでも上げておきます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

#73 ここに記載しておきましたー

}
exit((unsigned char)num); //todo: free?
}
ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO);
return (EXIT_FAILURE);
}
8 changes: 8 additions & 0 deletions execute/execute_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ bool execute_builtin(t_executor *e, int argc, char **argv, bool islast)
pwd(argc, argv);
return (true);
}
else if (!ft_strcmp(argv[0], "exit"))
{
if (islast)
e->exit_status = builtin_exit(argc, argv);
else
builtin_exit(argc, argv);
return (true);
}
return (false);
}

Expand Down
4 changes: 4 additions & 0 deletions libft/ft_isdigit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int ft_isdigit(int c)
{
return ('0' <= c && c <= '9');
}
10 changes: 3 additions & 7 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,9 @@ int minishell(char *line)
while (1)
{
line = readline("minishell> ");
// line can be NULL when Ctrl+d
if (!line || ft_strncmp(line, "exit", 4) == 0)
{
free(line);
ft_putendl_fd("exit", STDERR_FILENO);
if (!line)
break ;
}
// line can be NULL when Ctrl+d
t_token *token = lex(line);
t_ast_node *node = parse(token);
if (!node)
Expand All @@ -63,7 +59,7 @@ int minishell(char *line)
add_history(line);
free(line);
}
return (0);
return (EXIT_FAILURE);
Copy link
Collaborator

Choose a reason for hiding this comment

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

bashをCtrl-Dで終了させた場合の終了ステータスを試してみたら

$ exit
$ echo $?
0

ってなったので、return(EXIT_SUCCESS);だと思われます!

Copy link
Owner Author

@takumihara takumihara Oct 10, 2021

Choose a reason for hiding this comment

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

readlineがエラーの時にNULLでも返すのかなーとか思いながら適当にやってました笑ありがとうございます

}

int main(int argc, char **argv)
Expand Down
3 changes: 3 additions & 0 deletions test/cases/exit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exit 0
exit 1
exit -1
29 changes: 29 additions & 0 deletions utils/atoi_strict.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "utils.h"

bool atoi_strict(const char *str, int *num)
{
long long converted;
int sign;

converted = 0;
sign = 1;
while (('\t' <= *str && *str <= '\r') || *str == ' ')
str++;
if ((*str == '+' || *str == '-') && *str++ == '-')
sign = -1;
if (*str == '\0')
return (false);
while (ft_isdigit(*str))
{
if ((converted * 10 + *str - '0') / 10 != converted)
return (false);
converted = converted * 10 + *str - '0';
str++;
}
if (*str == '\0')
{
*num = (int)(sign * converted);
return (true);
}
return (false);
}
1 change: 1 addition & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ char *strappend(char *dst, const char *src, size_t l);
char *strldup(const char *str, size_t size);
void free_2d_array(void ***array);
char **split_by_delims(char const *str, const char *delims);
bool atoi_strict(const char *str, int *num);

# endif //UTILS_H