-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from LeaYeh/feat-builtin-exit
[FEAT] Implement builtin of exit
- Loading branch information
Showing
17 changed files
with
200 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,37 @@ | ||
#include "builtins.h" | ||
#include "utils.h" | ||
#include "clean.h" | ||
|
||
void handle_exit( | ||
t_shell *shell, t_final_cmd_table *final_cmd_table, int args_error) | ||
{ | ||
if (shell->subshell_level == 0) | ||
printf("exit\n"); | ||
if (args_error == TOO_MANY_ARGS) | ||
{ | ||
ft_dprintf(2, ERROR_EXIT_TOO_MANY_ARGS, PROGRAM_NAME, "exit"); | ||
if (shell->subshell_level == 0) | ||
return ; | ||
} | ||
else if (args_error == NOT_NUMERIC) | ||
ft_dprintf(2, ERROR_EXIT_NUMERIC_ARG, | ||
PROGRAM_NAME, "exit", final_cmd_table->simple_cmd[1]); | ||
free_final_cmd_table(&final_cmd_table); | ||
ft_clean_and_exit_shell(shell, shell->exit_code, NULL); | ||
} | ||
|
||
void exec_exit(t_shell *shell, t_final_cmd_table *final_cmd_table) | ||
{ | ||
int args_error; | ||
char **args; | ||
|
||
args = &final_cmd_table->simple_cmd[1]; | ||
args_error = get_args_error(args); | ||
if (args_error == NO_ARGS) | ||
shell->exit_code = SUCCESS; | ||
else if (args_error == NORM_ARGS) | ||
shell->exit_code = (ft_atol(args[0])) % 256; | ||
else | ||
shell->exit_code = args_error; | ||
handle_exit(shell, final_cmd_table, args_error); | ||
} |
This file contains 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,76 @@ | ||
#include "defines.h" | ||
#include "utils.h" | ||
#include "builtins.h" | ||
|
||
bool is_sign(char c) | ||
{ | ||
if (c == '+' || c == '-') | ||
return (true); | ||
return (false); | ||
} | ||
|
||
bool valid_number(char *str) | ||
{ | ||
int i; | ||
|
||
if (!str) | ||
return (false); | ||
i = 0; | ||
if (ft_strlen(str) > 1 && is_sign(str[i])) | ||
i++; | ||
while (str[i]) | ||
{ | ||
if (ft_isdigit(str[i])) | ||
i++; | ||
else | ||
return (false); | ||
} | ||
return (true); | ||
} | ||
|
||
bool is_atol_overflow(char *str) | ||
{ | ||
int i; | ||
char *long_max; | ||
|
||
i = 0; | ||
if (str[i] == '-') | ||
long_max = "9223372036854775808"; | ||
else | ||
long_max = "9223372036854775807"; | ||
if (is_sign(str[i])) | ||
i++; | ||
while (str[i] == '0') | ||
i++; | ||
if (ft_strlen(&str[i]) < ft_strlen(long_max) || \ | ||
ft_strcmp(&str[i], long_max) <= 0) | ||
return (false); | ||
return (true); | ||
} | ||
|
||
int get_args_error(char **args) | ||
{ | ||
int type; | ||
// int i; | ||
|
||
if (!*args) | ||
return (NO_ARGS); | ||
type = NORM_ARGS; | ||
if (!valid_number(args[0]) || is_atol_overflow(args[0])) | ||
type = NOT_NUMERIC; | ||
else if (get_array_len(args) > 1) | ||
{ | ||
type = TOO_MANY_ARGS; | ||
// i = 1; | ||
// while (args[i]) | ||
// { | ||
// if (!valid_number(args[i]) || is_overflow(args[i])) | ||
// { | ||
// type = NOT_NUMERIC; | ||
// break ; | ||
// } | ||
// i++; | ||
// } | ||
} | ||
return (type); | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.