diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9c5567a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "sstream": "c" + } +} \ No newline at end of file diff --git a/Makefile b/Makefile index 7e13e38..087b924 100644 --- a/Makefile +++ b/Makefile @@ -66,8 +66,12 @@ FUNC = main \ calc \ is_key_arrow \ append_history_list \ +<<<<<<< HEAD + handle_sig_in_gnl +======= handle_sig_in_proc \ handle_sig +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 OBJDIR = ./objs INCDIR = ./incs @@ -82,6 +86,16 @@ OBJS = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) all: $(NAME) $(NAME): $(OBJS) +<<<<<<< HEAD + $(MAKE) -C $(LIBDIR) all + $(CC) $(CFLAGS) -lcurses -o $(NAME) $(LIBFT) $(OBJS) #2> /dev/null || true + echo "Start Program" + +#$(OBJS): $(SRCS) +$(OBJDIR)/%.o: $(SRCDIR)/%.c + mkdir $(OBJDIR) 2> /dev/null || true + $(CC) $(CFLAGS) -I$(INCDIR) -lcurses -o $@ -c $< #2> /dev/null || true +======= @$(MAKE) -C $(LIBDIR) all @$(CC) $(CFLAGS) -lcurses -o $(NAME) $(LIBFT) $(OBJS) 2> /dev/null || true @echo "Start Program" @@ -90,6 +104,7 @@ $(NAME): $(OBJS) $(OBJDIR)/%.o: $(SRCDIR)/%.c @mkdir $(OBJDIR) 2> /dev/null || true @$(CC) $(CFLAGS) -I$(INCDIR) -lcurses -o $@ -c $< 2> /dev/null || true +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 clean: @rm -rf $(OBJDIR) diff --git a/backup/Makefile b/backup/Makefile new file mode 100644 index 0000000..5cc412e --- /dev/null +++ b/backup/Makefile @@ -0,0 +1,106 @@ +NAME = minishell + +CC = gcc +#CFLAGS = -Wall -Wextra -Werror +CFLAGS = + +FUNC = main \ + init_global \ + init_minishell \ + pk_split \ + handle_quote \ + handle_bit \ + inc_shlvl \ + get_next_line \ + gen_set_str_list \ + gen_set_list \ + del_quote \ + get_eval \ + gen_set \ + print_set \ + free_set \ + set_list_to_darr \ + print_darr \ + free_darr \ + categorize_cmd \ + select_sh_bti \ + redo_sh_bti \ + run_bti \ + get_bti_path \ + mod_eval \ + gen_env \ + free_env \ + sh_bti_export \ + sh_bti_unset \ + sh_bti_env \ + sh_bti_echo \ + sh_bti_cd \ + sh_bti_pwd \ + sh_bti_exit \ + get_max_strlen \ + get_dollar_eval \ + cvt_char_to_str \ + is_squote \ + is_dquote \ + handle_arg \ + exact_strncmp \ + gen_elist \ + get_elist \ + add_elist \ + free_elist \ + print_elist \ + print_slist \ + set_darr_to_list \ + print_list \ + run_cmd \ + show_error \ + exit_fatal \ + exit_shell \ + handle_sig_init \ + handle_sig_proc \ + is_bracket \ + set_bracket_type \ + set_fd \ + is_valid_fd \ + open_valid_fd \ + get_next_line_tc \ + ft_cursor \ + ft_cursor2 \ + ft_putchar_tc \ + calc \ + is_key_arrow \ + append_history_list \ + +OBJDIR = ./objs +INCDIR = ./incs +SRCDIR = ./srcs +LIBDIR = ./libft +LIBFT = -L./libft -lft + +SRC = $(addsuffix .c, $(FUNC)) +SRCS = $(addprefix $(SRCDIR)/, $(SRC)) +OBJS = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) + +all: $(NAME) + +$(NAME): $(OBJS) + @$(MAKE) -C $(LIBDIR) all + @$(CC) $(CFLAGS) -lcurses -o $(NAME) $(LIBFT) $(OBJS) #2> /dev/null || true + @echo "Start Program" + +#$(OBJS): $(SRCS) +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir $(OBJDIR) 2> /dev/null || true + @$(CC) $(CFLAGS) -I$(INCDIR) -lcurses -o $@ -c $< #2> /dev/null || true + +clean: + @rm -rf $(OBJDIR) + @$(MAKE) -C $(LIBDIR) clean + +fclean: clean + @rm -rf $(NAME) + @$(MAKE) -C $(LIBDIR) fclean + +re: fclean all + +.PHONY: all clean fclean re diff --git a/backup/README.md b/backup/README.md new file mode 100644 index 0000000..64ff444 --- /dev/null +++ b/backup/README.md @@ -0,0 +1,2 @@ +# mongshell +yekim juepark diff --git a/backup/backup_files/categorize_cmd_bak.c b/backup/backup_files/categorize_cmd_bak.c new file mode 100644 index 0000000..02f49d2 --- /dev/null +++ b/backup/backup_files/categorize_cmd_bak.c @@ -0,0 +1,30 @@ +#include "../incs/minishell.h" + +int categorize_cmd(t_info *info) +{ + int ret; + int run_check; + int idx; + + ret = 1; + run_check = 0; + if (del_quote(&(info->set->set[0]))) + { + printf("ERROR: trim_cmd malloc error\n"); + return (1); + } + info->set->cmd = info->set->set[0]; + if (!(ret = select_sh_bti(info))) + printf("SHELL BUILTIN CASE\n"); + else if (ret && !(ret = run_bti(info))) + printf("BUILTIN CASE\n"); + + //if(bti_path) -> fork + //else bti_path is NULL -> show error message. + //after using bti function with execve, free path. + //arg_beg = arg_list->next; + //make list to arr function.(arg_beg, env_list) + //after using arg_beg array, env array, free.(pk_split_free) + //strncmp(find one) + return (ret); +} diff --git a/backup/backup_files/check.c b/backup/backup_files/check.c new file mode 100644 index 0000000..da81a1e --- /dev/null +++ b/backup/backup_files/check.c @@ -0,0 +1,21 @@ +#include "../incs/minishell.h" + +static int check_dquote_open(char *line) +{ + // add single quote case + // ", ' check pair + return (0); +} + +int check_dquote(char *line, int flag) +{ + int ret; + + ret = 0; + if (flag == DQUOTE_OPEN) + ret = check_dquote_open(line); + if (ret == 0 && ...) + ret = + + return (ret); +} diff --git a/backup/backup_files/del_quote_bak.c b/backup/backup_files/del_quote_bak.c new file mode 100644 index 0000000..2e33552 --- /dev/null +++ b/backup/backup_files/del_quote_bak.c @@ -0,0 +1,54 @@ +#include "../incs/minishell.h" + +static int is_quote(char c) +{ + if (c == '\"' || c == '\'') + return (1); + return (0); +} + +static int get_cmd_size(char *str) +{ + int ret; + + ret = 0; + while (*str) + { + if (!is_quote(*str)) + ++ret; + ++str; + } + return (ret); +} + +static char *copy_without_quote(char *str) +{ + char *ret; + int ret_size; + int str_idx; + int ret_idx; + + ret_size = get_cmd_size(str); + if (!(ret = (char *)malloc(sizeof(char) * (ret_size + 1)))) + return (NULL); + ret[ret_size] = '\0'; + str_idx = -1; + ret_idx = -1; + while (str[++str_idx]) + { + if (!is_quote(str[str_idx])) + ret[++ret_idx] = str[str_idx]; + } + return (ret); +} + +int del_quote(char **str) +{ + char *ret; + + if (!(ret = copy_without_quote(*str))) + return (1); + free(*str); + *str = ret; + return (0); +} diff --git a/backup/backup_files/free_elist_bak.c b/backup/backup_files/free_elist_bak.c new file mode 100644 index 0000000..1d649a4 --- /dev/null +++ b/backup/backup_files/free_elist_bak.c @@ -0,0 +1,13 @@ +#include "../../incs/minishell.h" + +void free_elist(t_list *list_head) +{ + t_list *tmp; + t_env *tmp_env; + + tmp = list_head; + tmp_env =(t_env *)(list_head->data); + free_env(tmp_env); + list_head->data = NULL; + ft_lstdelone(tmp, &free); +} diff --git a/backup/backup_files/free_env_bak.c b/backup/backup_files/free_env_bak.c new file mode 100644 index 0000000..64c1ed6 --- /dev/null +++ b/backup/backup_files/free_env_bak.c @@ -0,0 +1,20 @@ +#include "../incs/minishell.h" + +void free_env(t_env *env) +{ + if (env->key) + { + free(env->key); + env->key = NULL; + } + if (env->val) + { + free(env->val); + env->val = NULL; + } + if (env) + { + free(env); + env = NULL; + } +} diff --git a/backup/backup_files/free_list.c b/backup/backup_files/free_list.c new file mode 100644 index 0000000..a54a4d9 --- /dev/null +++ b/backup/backup_files/free_list.c @@ -0,0 +1,16 @@ +#include "../../incs/minishell.h" + +void free_list(t_list **list_head) +{ + t_list *cur; + t_list *tmp; + + cur = *list_head; + while (cur) + { + tmp = cur; + cur = cur->next; + ft_lstdelone(tmp, &free); + } + *list_head = NULL; +} diff --git a/backup/backup_files/free_set_bak.c b/backup/backup_files/free_set_bak.c new file mode 100644 index 0000000..7cae007 --- /dev/null +++ b/backup/backup_files/free_set_bak.c @@ -0,0 +1,7 @@ +#include "../incs/minishell.h" + +void free_set(t_set *set) +{ + free_darr(set->set, INF); + free(set); +} diff --git a/backup/backup_files/garage/free_alist.c b/backup/backup_files/garage/free_alist.c new file mode 100644 index 0000000..79fa126 --- /dev/null +++ b/backup/backup_files/garage/free_alist.c @@ -0,0 +1,19 @@ +#include "../../incs/minishell.h" + +void free_alist(t_list **list_head) +{ + t_list *cur; + t_list *tmp; + + cur = *list_head; + while (cur) + { + tmp = cur; + tmp_env =(t_env *)(tmp->data); + cur = cur->next; + free(tmp_env->key); + free(tmp_env->val); + ft_lstdelone(tmp, &free); + } + *list_head = NULL; +} diff --git a/backup/backup_files/garage/parse_set.c b/backup/backup_files/garage/parse_set.c new file mode 100644 index 0000000..c8d9f99 --- /dev/null +++ b/backup/backup_files/garage/parse_set.c @@ -0,0 +1,60 @@ +#include "../incs/minishell.h" + +// malloc for original cmd(count size), +// remove " or ' +// use strcmp for checking if word is cmd or not + +//arg_list->data : echo " a" char *arg = echo " a" char *arg_cpy = echo "0a" args = [echo, " a"] +//-> { echo " a" } + +// line: single command and argument set +// word: [command, argument1, ...] +// in case that cmd = "ec""ho" .. + +static int get_alist( + t_list **list_head, + char **str) +{ + int ret; + int len; + t_list *tmp_list; + char *tmp_set; + + ret = 0; + len = 0; + while (ret == 0 && str[len]) + len++; + while (ret == 0 && --len >= 0) + { + if (!(tmp_set = ft_strdup(str[len]))) + ret = 1; + tmp_list = ft_lstnew(tmp_set); + ft_lstadd_front(list_head, tmp_list); + } + return (ret); +} + +t_list *gen_set_list(char *set) +{ + char *set_cpy; + char **arg_part; + int error_num; + t_list *ret; + + ret = NULL; + error_num = 0; + if (!(set_cpy = ft_strdup(set))) + return (NULL); + if (handle_quote(set, &set_cpy, ' ') > 0) + error_num = turn_on_bit(error_num, 0); + if (!(arg_part = pk_split(set, set_cpy, ' ', INF))) + error_num = turn_on_bit(error_num, 1); + if (!error_num && get_alist(&ret, arg_part)) + error_num = turn_on_bit(error_num, 2); + free (set_cpy); + if (!check_bit(error_num, 1)) + free_darr(arg_part, INF); + if (error_num) + return (NULL); + return (ret); +} diff --git a/backup/backup_files/garage/print_alist.c b/backup/backup_files/garage/print_alist.c new file mode 100644 index 0000000..7f6b5d3 --- /dev/null +++ b/backup/backup_files/garage/print_alist.c @@ -0,0 +1,13 @@ +#include "../../incs/minishell.h" + +void print_alist(t_list *head) +{ + t_list *cur; + + cur = head; + while (cur) + { + printf("%s\n", (char *)cur->data); + cur = cur->next; + } +} diff --git a/backup/backup_files/get_dollar_eval_back.c b/backup/backup_files/get_dollar_eval_back.c new file mode 100644 index 0000000..e55bf05 --- /dev/null +++ b/backup/backup_files/get_dollar_eval_back.c @@ -0,0 +1,39 @@ +#include "../incs/minishell.h" +static int get_dollar_len(char *arg) +{ + int idx; + int len; + char *var; + char *ret; + + idx = -1; + len = 0; + while (arg[++idx] && arg[idx] != '\'' && arg[idx] != '\"' && arg[idx] != '$') + len++; + return (len); +} + +char *get_dollar_eval(char *arg, t_list *env_list) +{ + int idx; + int len; + char *var; + char *ret; + + idx = -1; + len = get_dollar_len(arg); +// while (arg[++idx] && arg[idx] != '\'' && arg[idx] != '\"' && arg[idx] != '$') +// len++; + if (len == 0) + return (NULL); + if (!(var = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + var[len] = '\0'; + idx = -1; + while(++idx < len) + var[idx] = arg[idx]; + if (!(ret = get_eval(env_list, var))) + return (NULL); + free(var); + return(ret); +} diff --git a/backup/backup_files/get_list_data.c b/backup/backup_files/get_list_data.c new file mode 100644 index 0000000..b9a09dc --- /dev/null +++ b/backup/backup_files/get_list_data.c @@ -0,0 +1,18 @@ +#include "../../incs/minishell.h" + +char* get_list_data( + t_list *list_head, + char *target) +{ + t_list *cur; + + cur = list_head; + while (cur) + { + if (!!(ft_strnstr(cur->data, target, ft_strlen(target)))) + return (cur->data); + cur = cur->next; + } + return (NULL); +} + diff --git a/backup/backup_files/get_list_index.c b/backup/backup_files/get_list_index.c new file mode 100644 index 0000000..329fc60 --- /dev/null +++ b/backup/backup_files/get_list_index.c @@ -0,0 +1,20 @@ +#include "../../incs/minishell.h" + +int get_list_index( + t_list *list_head, + char *target) +{ + t_list *cur; + int i; + + i = -1; + cur = list_head; + while (cur) + { + ++i; + if (!!(ft_strnstr(cur->data, target, ft_strlen(target)))) + return (i); + cur = cur->next; + } + return (-1); +} diff --git a/backup/backup_files/handle_arg_bak.c b/backup/backup_files/handle_arg_bak.c new file mode 100644 index 0000000..72c009f --- /dev/null +++ b/backup/backup_files/handle_arg_bak.c @@ -0,0 +1,109 @@ +#include "../incs/minishell.h" + +static char *handle_nquote_part( + char **str_addr, + t_list *env_list) +{ + int idx; + char *str; + char *prev_ret; + char *tmp; + char *ret; + + str = *str_addr; + idx = 0; + if (is_dquote(str[idx]) || is_squote(str[idx])) + return (NULL); + ret = ft_strdup(""); + prev_ret = ret; + while (!is_dquote(str[idx]) && !is_squote(str[idx]) && str[idx]) + { + if (str[idx] == '$') + tmp = get_dollar_eval(&str[idx], env_list, &idx); + else + tmp = change_char2str(str[idx++]); + ret = ft_strjoin(ret, tmp); + free(prev_ret); + prev_ret = ret; + free(tmp); + } + *str_addr = *str_addr + idx; + return (ret); +} + +static char *handle_squote_part(char **str_addr) +{ + int idx; + char *str; + char *ret; + + str = *str_addr; + idx = 0; + if (!is_squote(str[idx++])) + return (NULL); + while (!is_squote(str[idx])) + ++idx; + ret = ft_substr(str, 1, idx - 1); + *str_addr = *str_addr + idx; + return (ret); +} + +static char *handle_dquote_part( + char **str_addr, + t_list *env_list) +{ + int idx; + char *str; + char *prev_ret; + char *tmp; + char *ret; + + str = *str_addr; + idx = 0; + if (!is_dquote(str[idx++])) + return (NULL); + ret = ft_strdup(""); + prev_ret = ret; + while (!is_dquote(str[idx])) + { + if (str[idx] == '$') + tmp = get_dollar_eval(&str[idx], env_list, &idx); + else + tmp = change_char2str(str[idx++]); + ret = ft_strjoin(ret, tmp); + free(prev_ret); + prev_ret = ret; + free(tmp); + } + *str_addr = *str_addr + idx; + return (ret); +} + +char *handle_arg(char *arg, t_list *env_list) +{ + char *ret; + char *prev_ret; + char *tmp; + + ret = ft_strdup(""); + prev_ret = ret; + while (arg && *arg) + { + if (is_dquote(*arg)) + tmp = handle_dquote_part(&arg, env_list); + else if (is_squote(*arg)) + tmp = handle_squote_part(&arg); + else + { + tmp = handle_nquote_part(&arg, env_list); + if (*arg == '\0') + --arg; + } + ret = ft_strjoin(ret, tmp); + free(prev_ret); + prev_ret = ret; + free(tmp); + ++arg; + } + return (ret); +} diff --git a/backup/backup_files/handle_list_data.c b/backup/backup_files/handle_list_data.c new file mode 100644 index 0000000..eefd58f --- /dev/null +++ b/backup/backup_files/handle_list_data.c @@ -0,0 +1,67 @@ +#include "../../incs/minishell.h" + +char* get_list_data( + t_list *list_head, + char *target) +{ + t_list *cur; + + cur = list_head; + while (cur) + { + if (!!(ft_strnstr(cur->data, target, ft_strlen(target)))) + return (cur->data); + cur = cur->next; + } + return (NULL); +} + +static char *get_new_data( + char *key, + char *value) +{ + char *ret; + char *tmp; + + tmp = ft_strjoin(key, "="); + ret = ft_strjoin(tmp, value); + free(tmp); + return (ret); +} + +int modify_list_data( + t_list **list_head, + char *key, char *value) +{ + int index; + int ret; + t_list *cur; + + ret = 0; + if ((index = get_list_index(*list_head, key)) == -1) + ret = 1; + if (ret == 0) + { + cur = *list_head; + while (index--) + cur = cur->next; + free(cur->data); + cur->data = get_new_data(key, value); + } + if (!(cur->data)) + ret = 1; + return (ret); +} + +int add_list_data( + t_list **list_head, + char *key, char *value) +{ + t_list *tmp; + + if(list_head == 0) + return (1); + tmp = ft_lstnew(get_new_data(key, value)); + ft_lstadd_back(list_head, tmp); + return (0); +} diff --git a/backup/backup_files/handle_list_index.c b/backup/backup_files/handle_list_index.c new file mode 100644 index 0000000..329fc60 --- /dev/null +++ b/backup/backup_files/handle_list_index.c @@ -0,0 +1,20 @@ +#include "../../incs/minishell.h" + +int get_list_index( + t_list *list_head, + char *target) +{ + t_list *cur; + int i; + + i = -1; + cur = list_head; + while (cur) + { + ++i; + if (!!(ft_strnstr(cur->data, target, ft_strlen(target)))) + return (i); + cur = cur->next; + } + return (-1); +} diff --git a/backup/backup_files/inc_shlvl_bak.c b/backup/backup_files/inc_shlvl_bak.c new file mode 100644 index 0000000..a195db3 --- /dev/null +++ b/backup/backup_files/inc_shlvl_bak.c @@ -0,0 +1,14 @@ +#include "../incs/minishell.h" + +int inc_shlvl(t_list **env_head) +{ + t_list *tmp_elist; + t_env *tmp_env; + + if (!(tmp_elist = get_elist(*env_head, "SHLVL"))) + return (1); + tmp_env = (t_env *)(tmp_elist->data); + if (mod_eval(tmp_env, ft_itoa(ft_atoi(tmp_env->val) + 1))) + return (1); + return (0); +} diff --git a/backup/backup_files/parse_env.c.bak b/backup/backup_files/parse_env.c.bak new file mode 100644 index 0000000..2cec314 --- /dev/null +++ b/backup/backup_files/parse_env.c.bak @@ -0,0 +1,71 @@ +#include "../incs/minishell.h" + +static int get_eq_idx(char *set_cpy) +{ + int ret; + + ret = 0; + while (*set_cpy) + { + if (*set_cpy == '=') + return (ret); + ++set_cpy; + ++ret; + } + return (-1); +} +static t_env *gen_elist_data(char *key, char *val) +{ + t_env *ret; + + if (!(ret = (t_env *)malloc(sizeof(t_env)))) + return (NULL); + if (!(ret->key = ft_strdup(key))) + return (NULL); + if (!(ret->val = ft_strdup(val))) + return (NULL); + return (ret); +} + +char *get_elist_data_part( + char *set, + char *set_cpy, + char charset, + int limit) +{ + char **part; + char *ret; + int err_num; + + err_num = 0; + if ((part = pk_split(set, set_cpy, charset, limit)) == 0) + err_num = turn_on_bit(err_num, 0); + if (!check_bit(err_num, 0) && !(ret = ft_strdup(part[0]))) + err_num = turn_on_bit(err_num, 1); + if (!check_bit(err_num, 0)) + free_darr(part, 1); + if (!ret) + ret = ft_strdup(""); + return (ret); +} + +t_env *parse_env(char *set) +{ + char *key; + char *val; + char *set_cpy; + int tmp; + t_env *ret; + + if (!(set_cpy = ft_strdup(set))) + return (NULL); + if (handle_quote(set, &set_cpy, '=') > 0) + return (NULL); + key = get_elist_data_part(set, set_cpy, '=', 1); + tmp = get_eq_idx(set_cpy) + 1; + val = get_elist_data_part(&set[tmp], &set_cpy[tmp], ' ', 1); + if (!(ret = gen_elist_data(key, val))) + return (NULL); + free(set_cpy); + return (ret); +} diff --git a/backup/backup_files/parse_set_bak.c b/backup/backup_files/parse_set_bak.c new file mode 100644 index 0000000..ac14239 --- /dev/null +++ b/backup/backup_files/parse_set_bak.c @@ -0,0 +1,26 @@ +#include "../incs/minishell.h" + +t_set *gen_set_list(char *set_str) +{ + char *set_str_cpy; + int error_num; + char **tmp_set; + t_set *ret; + + ret = NULL; + error_num = 0; + if (!(set_str_cpy = ft_strdup(set_str))) + return (NULL); + if (handle_quote(set_str, &set_str_cpy, ' ') > 0) + error_num = turn_on_bit(error_num, 0); + if (!(tmp_set = pk_split(set_str, set_str_cpy, ' ', INF))) + error_num = turn_on_bit(error_num, 1); + if (!(ret = (t_set *)malloc(sizeof(t_set)))) + return (NULL); + ret->set = tmp_set; + ret->cmd = tmp_set[0]; + ret->args = &tmp_set[1]; + if (error_num) + return (NULL); + return (ret); +} diff --git a/backup/backup_files/sh_bti_echo_bak.c b/backup/backup_files/sh_bti_echo_bak.c new file mode 100644 index 0000000..301d65f --- /dev/null +++ b/backup/backup_files/sh_bti_echo_bak.c @@ -0,0 +1,40 @@ +#include "../incs/minishell.h" + +static int is_n_flag(char *str) +{ + int ret; + int idx; + + ret = 0; + idx = 0; + if (str[idx] == '-') + ret = 1; + while (ret == 1 && str[++idx]) + { + if (str[idx] != 'n') + ret = 0; + } + return (ret); +} + +int sh_bti_echo(char **args, t_list *env_list) +{ + int idx; + int n_flag; + + n_flag = 0; + idx = -1; + while (args[++idx] && is_n_flag(args[idx])) + n_flag = 1; + idx--; + while (args[++idx]) + { + if (idx >= 1) + break; + char *tmp = handle_arg(args[idx], env_list); + write(STDOUT_FILENO, tmp, ft_strlen(tmp)); + } + if (!n_flag) + write(1, "\n", 1); + return (0); +} diff --git a/backup/incs/minishell.h b/backup/incs/minishell.h new file mode 100644 index 0000000..1d7cd80 --- /dev/null +++ b/backup/incs/minishell.h @@ -0,0 +1,538 @@ +#ifndef _MINISHELL_H +# define _MINISHELL_H + +# include "../libft/libft.h" +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# define TYPE_PIPE 1 +# define TYPE_BREAK 2 +# define TYPE_REIN 4 +# define TYPE_REOUT 8 +# define TYPE_REOUT_D 16 +# define TYPE_ERROR 32 + +# define KEY_BACKSPACE 127 +# define KEY_LEFT_ARROW 4479771 +# define KEY_RIGHT_ARROW 4414235 +# define KEY_UP_ARROW 4283163 +# define KEY_DOWN_ARROW 4348699 + +# define TC_CM 0 +# define TC_DL 1 +# define TC_CE 2 + +# define BUFFER_SIZE 1024 + +//# define FLAG_FD_OPEN 1 + +# define OPEN 1 +# define CLOSE 0 +// head of data is cmd + +typedef struct s_prompt +{ + char *data; + int size; +} t_prompt; + +typedef struct s_env +{ + char *key; + char *val; +} t_env; + +typedef struct s_cursor +{ + int row; + int col; +} t_cursor; + +typedef struct s_tc +{ + struct termios term; + char *tc_str[3]; + t_cursor cursor; +} t_tc; + +typedef struct s_set +{ + char **set; + char *cmd; + char **args; + int type; + int fds[2]; + int fd_in_idx; + int fd_in[1024]; + int fd_out_idx; + int fd_out[1024]; +} t_set; + +typedef struct s_info +{ + t_list *env_list; + t_list *set_list; + t_list *set_str_list; + t_list *history; + t_list *history_ptr; + t_set *set; + int exit; + int ret; + int dollar_ret; + int inst_buf[1024]; + t_tc tc; + t_prompt prompt; +} t_info; +//global return value +int g_ret; +int g_fsh_buf; + +#define BIT_SQUOTE 1 +#define BIT_DQUOTE 2 +#define ON 0x00001 +#define OFF 0x00000 +#define INF 987654321 +#define BUF_SIZE 1024 + +#define STDERR 2 + +/* +** handle_quote.c +*/ +int handle_quote( + char *str, + char **str_cpy, + char c); + +/* +** pk_split.c +*/ +char **pk_split( + const char *s, + const char *s_cpy, + char c, + int limit); + +/* +** get_next_line.c +*/ +int get_next_line(t_info *info, char **line); + +/* +** handle_bit.c +*/ +int turn_on_bit( + int val, + int n); +int turn_off_bit( + int val, + int n); +int check_bit( + int val, + int n); + +/* +** init_minishell.c +*/ +int init_minishell( + t_info *info, + char **env); + +/* +** inc_shlvl.c +*/ +int inc_shlvl(t_list **env_head); + +/* +** gen_elist +*/ +t_list *gen_elist(char **str); + +/* +** get_elist.c +*/ +t_list *get_elist( + t_list *list_head, + char *tar); + +/* +** gen_slist +*/ +t_list *gen_slist(t_info *info, char **str); + +/* +** get_eval.c +*/ +char *get_eval( + t_list *list_head, + char *tar); + +int mod_eval( + t_env *env, + char *new_val); + +/* +** add_elist.c +*/ +int add_elist( + t_list **list_head, + char *key, + char *val); + +/* +** mod_elist.c +*/ +int mod_elist( + t_list **list_head, + char *key, + char *val); + +/* +** print_list.c +*/ +void print_list( + t_list *head); + +/* +** print_set.c +*/ +void print_set(t_set *set); + +/* +** print_slist.c +*/ +void print_slist( + t_list *head); + +/* +** print_elist.c +*/ +void print_elist( + t_list *head); +/* +** sort_env.c +*/ +void print_sorted_env(t_list *env_list); + +/* +** free_elist.c +*/ +void free_elist(t_list *list_head); + +/* +** free_elist.c +*/ +void free_set(void *_set); + +/* +** gen_set_str_list.c +*/ +t_list *gen_set_str_list(char *line); + +/* +** gen_set.c +*/ +t_set *gen_set(t_info *info, char *set_str); + +/* +** gen_env.c +*/ +t_env *gen_env(char *set); + +/* +** free_env.c +*/ +void free_env(void *_env); + +/* +** categorize_cmd.c +*/ +int categorize_cmd( + t_set *set, + t_info *info); + +/* +** del_quote.c +*/ +int del_quote(char **str); + +/* +** cvt_list_to_arr +*/ +char **set_list_to_darr(t_list *env_list); + +/* +** print_darr +*/ +void print_darr(char **str); + +/* +** free_darr +*/ +char **free_darr(char **tab, int limit); + +/* +** select_sh_bti.c +*/ +int select_sh_bti( + t_set *set, + t_info *info); + +/* +** run_bti.c +*/ +int run_bti( + t_set *set, + t_list *env_list); +/* +** get_bti_path.c +*/ +char *get_bti_path(char *cand, char *cmd); + +/* +** sh_bti_export.c +*/ +int sh_bti_export( + char **args, + t_list **env_list); + +/* +** sh_bti_unset.c +*/ +int sh_bti_unset( + char **args, + t_list **env_list); + +/* +** sh_bti_env.c +*/ +int sh_bti_env(t_list *env_list); + +/* +** sh_bti_pwd.c +*/ +int sh_bti_pwd(char **args); + +/* +** sh_bti_echo.c +*/ +int sh_bti_echo(char **args, t_list *env_list); + +/* +** sh_bti_exit.c +*/ +int sh_bti_exit(t_info *info); + +/* +** get_max_strlen.c +*/ +int get_max_strlen(char *str1, char *str2); + +/* +** get_dollar_eval.c +*/ +char *get_dollar_eval( + char *part, + t_list *env_list, + int *idx); + +/* +** sh_bti_echo.c +*/ +int sh_bti_echo(char **args, t_list *env_list); + +/* +** sh_bti_cd.c +*/ +int sh_bti_cd(char **args, t_list *env_list); + +/* +** cvt_char_to_str.c +*/ +char *cvt_char_to_str(char c); + +/* +** is_squote.c +*/ +int is_squote(char c); + +/* +** is_dquote.c +*/ +int is_dquote(char c); + +/* +** handle_arg.c +*/ +char *handle_arg( + char *arg, + t_list *env_list); + +/* +** exact_strncmp.c +*/ +int exact_strncmp(char *str1, char *str2); + +/* +** get_list.c +*/ +int set_darr_to_list( + t_list **list_head, + char **str); +/* +** gen_set_list.c +*/ +t_list *gen_set_list(t_info *info); + +/* +** run_cmd.c +*/ +int run_cmd( + t_info *info); + +/* +** show_error.c +*/ +int show_error(char const *str); + + +/* +** exit_shell.c +*/ +int exit_shell(t_info *info); + +/* +** exit_fatal.c +*/ +int exit_fatal(void); + +/* +** redo_sh_bti.c +*/ +int redo_sh_bti(t_set *set, t_info *info); + + +/* +** init_global.c +*/ +void init_global(); + +/* +** handle_sig_init.c +*/ +void handle_sig_init(t_info *info); + +/* +** handle_sig_proc.c +*/ +void handle_sig_proc(int pid); + +/* +** set_bracet_type.c +*/ +int set_bracket_type(char *str, int *idx); +/* +** set_fd.c +*/ +char *set_fd(t_set *set, char *set_str); + +/* +** set_fd_info.c +*/ +int set_fd_info( + t_set *set, + char *str, + int curr_type); +/* +** is_bracket.c +*/ +int is_bracket(char c); + +/* +** is_valid_fd.c +*/ +int is_valid_fd( + t_set *set, + char *str, + int *idx, + int type); + +/* +** open_valid_fd.c +*/ +int open_valid_fd( + t_set *set, + char *set_str, + int *idx, + int type); + +int ft_putchar_tc(int tc); + + +/* +** ft_cursor.c +*/ +int ft_cursor_mv_left( + int col, + int left_limit); +int ft_cursor_mv_right( + int col, + int right_limit); +int ft_cursor_clr_line_end( + t_tc tc, + int left_limit); + +int ft_cursor_mv_head(t_tc tc); +int ft_cursor_clr_line_all(t_tc tc); + + + + +char *get_next_line_tc(t_info *info); +//int test(t_info *info, t_prompt prompt); +int is_key_arrow(long c); + +/* +** ft_cursor2.c +*/ +void get_cursor_pos(int *col, int *row); + + +/* +** ft_cursor2.c +*/ +void append_history_list( + t_list **history, + char *line); + +/* +** calc.c +*/ +int calc_min(int num1, int num2); +int calc_max(int num1, int num2); +#endif + +#if 0 + + +/* +** pk_lst.c +*/ +void pk_lstadd_front( + t_slist **set_head, + t_slist *new_list); + +t_slist *pk_lstnew( + void *data); + +void pk_lstdelone( + t_slist *target, + void (*del)(void *)); +#endif diff --git a/backup/libft/Makefile b/backup/libft/Makefile new file mode 100644 index 0000000..d212af7 --- /dev/null +++ b/backup/libft/Makefile @@ -0,0 +1,76 @@ +CC = gcc +CFLAGS = -Wall -Wextra -Werror +AR = ar +ARFLAGS = rcs +RM = rm -f +NAME = libft.a + +FUNCS = ft_memset \ + ft_bzero \ + ft_memcpy \ + ft_memccpy \ + ft_memmove \ + ft_memchr \ + ft_memcmp \ + ft_strlen \ + ft_strlcpy \ + ft_strlcat \ + ft_strchr \ + ft_strrchr \ + ft_strnstr \ + ft_strncmp \ + ft_atoi \ + ft_isalpha \ + ft_isdigit \ + ft_isalnum \ + ft_isascii \ + ft_isprint \ + ft_toupper \ + ft_tolower \ + ft_calloc \ + ft_strdup \ + ft_substr \ + ft_strjoin \ + ft_strtrim \ + ft_split \ + ft_itoa \ + ft_strmapi \ + ft_putchar_fd \ + ft_putstr_fd \ + ft_putendl_fd \ + ft_putnbr_fd +FUNCS_B = ft_lstnew \ + ft_lstadd_front \ + ft_lstsize \ + ft_lstlast \ + ft_lstadd_back \ + ft_lstdelone \ + ft_lstclear \ + ft_lstiter \ + ft_lstmap + +SRCS = $(addsuffix .c, $(FUNCS)) +SRCS_B = $(addsuffix .c, $(FUNCS_B)) +OBJS = $(SRCS:.c=.o) +OBJS_B = $(SRCS_B:.c=.o) + +.c.o : + @$(CC) $(CFLAGS) -c $< -o $@ + +$(NAME) : $(OBJS) + @$(AR) $(ARFLAGS) $@ $^ + +bonus: $(NAME) $(OBJS_B) + @$(AR) $(ARFLAGS) $(NAME) $(OBJS_B) + +all : bonus + +clean : + @$(RM) $(OBJS) $(OBJS_B) + +fclean : clean + @$(RM) $(NAME) + +re : fclean all + +.PHONY: bonus all clean fclean re diff --git a/backup/libft/ft_atoi.c b/backup/libft/ft_atoi.c new file mode 100644 index 0000000..856fe82 --- /dev/null +++ b/backup/libft/ft_atoi.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 18:23:43 by yekim #+# #+# */ +/* Updated: 2020/10/10 07:48:48 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isspace(char c) +{ + if (c == '\f' || c == '\n' || c == '\r' + || c == '\t' || c == '\v' || c == ' ') + return (1); + return (0); +} + +int ft_atoi(const char *nptr) +{ + long long ret; + int sign; + + sign = 0; + while (ft_isspace(*nptr)) + ++nptr; + if (*nptr == '-' || *nptr == '+') + { + if (*nptr == '-') + ++sign; + ++nptr; + } + ret = 0; + while (ft_isdigit(*nptr)) + ret = ret * 10 + (long long)(*(nptr++) - '0'); + ret = sign % 2 == 0 ? ret : -ret; + if (ret > LONG_MAX) + return (-1); + if (ret < LONG_MIN) + return (0); + return ((int)ret); +} diff --git a/backup/libft/ft_bzero.c b/backup/libft/ft_bzero.c new file mode 100644 index 0000000..a177181 --- /dev/null +++ b/backup/libft/ft_bzero.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:04:45 by yekim #+# #+# */ +/* Updated: 2020/10/04 07:58:54 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + size_t idx; + + idx = 0; + while (idx < n) + ((unsigned char *)s)[idx++] = '\0'; +} diff --git a/backup/libft/ft_calloc.c b/backup/libft/ft_calloc.c new file mode 100644 index 0000000..ebb7a80 --- /dev/null +++ b/backup/libft/ft_calloc.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 18:22:51 by yekim #+# #+# */ +/* Updated: 2020/10/07 07:14:59 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ret; + size_t total_size; + + total_size = nmemb * size; + if (!(ret = malloc(total_size))) + return (NULL); + ft_bzero(ret, total_size); + return (ret); +} diff --git a/backup/libft/ft_isalnum.c b/backup/libft/ft_isalnum.c new file mode 100644 index 0000000..d0aa357 --- /dev/null +++ b/backup/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:32:17 by yekim #+# #+# */ +/* Updated: 2020/09/29 16:32:18 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/backup/libft/ft_isalpha.c b/backup/libft/ft_isalpha.c new file mode 100644 index 0000000..31527fb --- /dev/null +++ b/backup/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:32:20 by yekim #+# #+# */ +/* Updated: 2020/09/29 16:32:21 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return (((c >= 65) && (c <= 90)) || ((c >= 97) && (c <= 122))); +} diff --git a/backup/libft/ft_isascii.c b/backup/libft/ft_isascii.c new file mode 100644 index 0000000..086c3d3 --- /dev/null +++ b/backup/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:32:24 by yekim #+# #+# */ +/* Updated: 2020/10/01 06:13:50 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/backup/libft/ft_isdigit.c b/backup/libft/ft_isdigit.c new file mode 100644 index 0000000..8056c71 --- /dev/null +++ b/backup/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:32:28 by yekim #+# #+# */ +/* Updated: 2020/09/29 16:32:29 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= 48 && c <= 57); +} diff --git a/backup/libft/ft_isprint.c b/backup/libft/ft_isprint.c new file mode 100644 index 0000000..2f2a82a --- /dev/null +++ b/backup/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:32:31 by yekim #+# #+# */ +/* Updated: 2020/10/04 13:25:51 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (c >= 32 && c <= 126); +} diff --git a/backup/libft/ft_itoa.c b/backup/libft/ft_itoa.c new file mode 100644 index 0000000..b026ebc --- /dev/null +++ b/backup/libft/ft_itoa.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:45:10 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:45:11 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t handle_nbr(unsigned int n, char *n_buf, int k, int flag) +{ + size_t ret; + int mod; + + ret = 1; + if (n >= 10) + ret += handle_nbr(n / 10, n_buf, k - 1, flag); + if (flag) + { + mod = n % 10; + n_buf[k] = (char)(mod + '0'); + } + return (ret); +} + +char *ft_itoa(int n) +{ + unsigned int tmp_n; + size_t len_ret; + char *ret; + + tmp_n = n < 0 ? -n : n; + len_ret = handle_nbr(tmp_n, NULL, 0, 0); + len_ret = n < 0 ? len_ret + 1 : len_ret; + if (!(ret = (char *)malloc(sizeof(char) * (len_ret + 1)))) + return (NULL); + ret[len_ret] = '\0'; + handle_nbr(tmp_n, ret, (int)len_ret - 1, 1); + ret[0] = n < 0 ? '-' : ret[0]; + return (ret); +} diff --git a/backup/libft/ft_lstadd_back.c b/backup/libft/ft_lstadd_back.c new file mode 100644 index 0000000..2ed8140 --- /dev/null +++ b/backup/libft/ft_lstadd_back.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:04 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:43:18 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new_lst) +{ + t_list *last; + + if (lst == 0 || new_lst == NULL) + return ; + if (*lst == NULL) + { + *lst = new_lst; + return ; + } + last = ft_lstlast(*lst); + new_lst->next = last->next; + last->next = new_lst; + new_lst->prev = last; +} diff --git a/backup/libft/ft_lstadd_front.c b/backup/libft/ft_lstadd_front.c new file mode 100644 index 0000000..37226d3 --- /dev/null +++ b/backup/libft/ft_lstadd_front.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:24 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:43:28 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +#include +void ft_lstadd_front(t_list **lst, t_list *new_lst) +{ + if (lst == 0 || new_lst == NULL) + return ; + if (*lst != NULL) + (*lst)->prev = new_lst; + new_lst->next = *lst; + *lst = new_lst; +} diff --git a/backup/libft/ft_lstclear.c b/backup/libft/ft_lstclear.c new file mode 100644 index 0000000..b0aceb0 --- /dev/null +++ b/backup/libft/ft_lstclear.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:32 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:43:35 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + if (lst == 0 || del == NULL) + return ; + if (*lst == NULL) + return ; + ft_lstclear(&((*lst)->next), del); + del((*lst)->data); + free(*lst); + *lst = NULL; +} diff --git a/backup/libft/ft_lstdelone.c b/backup/libft/ft_lstdelone.c new file mode 100644 index 0000000..b8c21b0 --- /dev/null +++ b/backup/libft/ft_lstdelone.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:39 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:43:41 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +#include +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (lst == NULL || del == NULL) + return ; + if (lst->data) + del(lst->data); + free(lst); + lst = NULL; +} diff --git a/backup/libft/ft_lstiter.c b/backup/libft/ft_lstiter.c new file mode 100644 index 0000000..4a75a3e --- /dev/null +++ b/backup/libft/ft_lstiter.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:44 by yekim #+# #+# */ +/* Updated: 2021/02/10 11:57:01 by jackjoo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (lst == NULL || f == NULL) + return ; + ft_lstiter(lst->next, f); + f(lst->data); +} diff --git a/backup/libft/ft_lstlast.c b/backup/libft/ft_lstlast.c new file mode 100644 index 0000000..356002e --- /dev/null +++ b/backup/libft/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:49 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:43:51 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (NULL); + if (lst->next == NULL) + return (lst); + return (ft_lstlast(lst->next)); +} diff --git a/backup/libft/ft_lstmap.c b/backup/libft/ft_lstmap.c new file mode 100644 index 0000000..090ba46 --- /dev/null +++ b/backup/libft/ft_lstmap.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:54 by yekim #+# #+# */ +/* Updated: 2021/02/10 11:57:29 by jackjoo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *ret; + t_list *map_lst; + + if (lst == NULL || f == NULL) + return (NULL); + ret = NULL; + while (lst) + { + if (!(map_lst = ft_lstnew((*f)(lst->data)))) + { + ft_lstclear(&ret, del); + return (NULL); + } + ft_lstadd_back(&ret, map_lst); + lst = lst->next; + } + return (ret); +} diff --git a/backup/libft/ft_lstnew.c b/backup/libft/ft_lstnew.c new file mode 100644 index 0000000..0806846 --- /dev/null +++ b/backup/libft/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:43:59 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:44:00 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *data) +{ + t_list *ret; + + if (!(ret = (t_list *)malloc(sizeof(t_list)))) + return (NULL); + ret->data = data; + ret->next = NULL; + ret->prev = NULL; + return (ret); +} diff --git a/backup/libft/ft_lstsize.c b/backup/libft/ft_lstsize.c new file mode 100644 index 0000000..fab5012 --- /dev/null +++ b/backup/libft/ft_lstsize.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:44:06 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:44:07 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + if (lst == NULL) + return (0); + return (1 + ft_lstsize(lst->next)); +} diff --git a/backup/libft/ft_memccpy.c b/backup/libft/ft_memccpy.c new file mode 100644 index 0000000..cc409bf --- /dev/null +++ b/backup/libft/ft_memccpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:03 by yekim #+# #+# */ +/* Updated: 2020/10/07 08:05:52 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dest, const void *src, int c, size_t n) +{ + unsigned char *ucsrc; + unsigned char *ucdest; + size_t idx; + + ucsrc = (unsigned char*)src; + ucdest = (unsigned char*)dest; + idx = 0; + while (idx < n) + { + ucdest[idx] = ucsrc[idx]; + if (ucsrc[idx] == (unsigned char)c) + return (dest + idx + 1); + ++idx; + } + return (NULL); +} diff --git a/backup/libft/ft_memchr.c b/backup/libft/ft_memchr.c new file mode 100644 index 0000000..b56791c --- /dev/null +++ b/backup/libft/ft_memchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:06 by yekim #+# #+# */ +/* Updated: 2020/10/07 07:52:19 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + while (n--) + { + if (*((unsigned char*)s) == (unsigned char)c) + return ((void *)s); + ++s; + } + return (NULL); +} diff --git a/backup/libft/ft_memcmp.c b/backup/libft/ft_memcmp.c new file mode 100644 index 0000000..5a76ca1 --- /dev/null +++ b/backup/libft/ft_memcmp.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:10 by yekim #+# #+# */ +/* Updated: 2020/10/07 09:03:07 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *ucs1; + unsigned char *ucs2; + + ucs1 = (unsigned char *)s1; + ucs2 = (unsigned char *)s2; + while (n--) + { + if (*ucs1 != *ucs2) + return (*ucs1 - *ucs2); + ++ucs1; + ++ucs2; + } + return (0); +} diff --git a/backup/libft/ft_memcpy.c b/backup/libft/ft_memcpy.c new file mode 100644 index 0000000..e976c41 --- /dev/null +++ b/backup/libft/ft_memcpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:13 by yekim #+# #+# */ +/* Updated: 2020/10/07 08:02:27 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + unsigned char *ucsrc; + unsigned char *ucdest; + + if (dest == NULL && src == NULL) + return (NULL); + ucsrc = (unsigned char *)src; + ucdest = (unsigned char *)dest; + while (n--) + *(ucdest++) = *(ucsrc++); + return (dest); +} diff --git a/backup/libft/ft_memmove.c b/backup/libft/ft_memmove.c new file mode 100644 index 0000000..361dc8e --- /dev/null +++ b/backup/libft/ft_memmove.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:16 by yekim #+# #+# */ +/* Updated: 2020/10/07 08:05:18 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned char *ucsrc; + unsigned char *ucdest; + size_t idx; + + if (dest == src || n == 0) + return (dest); + ucsrc = (unsigned char*)src; + ucdest = (unsigned char*)dest; + idx = 0; + if (dest >= src) + while (idx++ < n) + *(ucdest-- + n - 1) = *(ucsrc-- + n - 1); + else + while (idx++ < n) + *(ucdest++) = *(ucsrc++); + return (dest); +} diff --git a/backup/libft/ft_memset.c b/backup/libft/ft_memset.c new file mode 100644 index 0000000..923e02d --- /dev/null +++ b/backup/libft/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:19 by yekim #+# #+# */ +/* Updated: 2020/10/07 08:06:10 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char *ucs; + + ucs = (unsigned char*)s; + while (n--) + *(ucs++) = (unsigned char)c; + return (s); +} diff --git a/backup/libft/ft_putchar_fd.c b/backup/libft/ft_putchar_fd.c new file mode 100644 index 0000000..210db89 --- /dev/null +++ b/backup/libft/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:44:32 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:44:33 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + if (fd < 0) + return ; + write(fd, &c, sizeof(char)); +} diff --git a/backup/libft/ft_putendl_fd.c b/backup/libft/ft_putendl_fd.c new file mode 100644 index 0000000..ca22d29 --- /dev/null +++ b/backup/libft/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:44:36 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:44:37 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (s == NULL || fd < 0) + return ; + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/backup/libft/ft_putnbr_fd.c b/backup/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..c0234fb --- /dev/null +++ b/backup/libft/ft_putnbr_fd.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:44:40 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:44:41 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void print_nbr(unsigned int n, int fd) +{ + if (n >= 10) + print_nbr(n / 10, fd); + ft_putchar_fd(n % 10 + '0', fd); +} + +void ft_putnbr_fd(int n, int fd) +{ + int sign; + unsigned int n_tmp; + + if (fd < 0) + return ; + sign = n < 0 ? 1 : 0; + if (sign) + { + ft_putchar_fd('-', fd); + n_tmp = -n; + } + else + n_tmp = n; + print_nbr(n_tmp, fd); +} diff --git a/backup/libft/ft_putstr_fd.c b/backup/libft/ft_putstr_fd.c new file mode 100644 index 0000000..0107b83 --- /dev/null +++ b/backup/libft/ft_putstr_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:44:44 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:44:45 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (s == NULL || fd < 0) + return ; + write(fd, s, ft_strlen(s)); +} diff --git a/backup/libft/ft_split.c b/backup/libft/ft_split.c new file mode 100644 index 0000000..c71ae15 --- /dev/null +++ b/backup/libft/ft_split.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:45:14 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:45:16 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t get_size(const char *s, char c) +{ + size_t ret; + + ret = 0; + while (*s) + { + while ((*s != '\0') && (*s == c)) + ++s; + if ((*s != '\0') && (*s != c)) + ++ret; + while ((*s != '\0') && (*s != c)) + ++s; + } + return (ret); +} + +static char *get_next(const char **s, size_t *len_word, char c) +{ + char *ret; + + while ((**s != '\0') && (**s == c)) + ++(*s); + ret = (char *)*s; + *len_word = 0; + while ((**s != '\0') && (**s != c)) + { + ++(*len_word); + ++(*s); + } + return (ret); +} + +static char **free_all(char **tab, int k) +{ + int idx; + + idx = 0; + while (idx < k) + { + free(tab[idx]); + ++idx; + } + free(tab); + return (NULL); +} + +char **ft_split(char const *s, char c) +{ + char **ret; + char *beg_word; + size_t len_word; + size_t size; + size_t k; + + if (s == NULL) + return (0); + size = get_size(s, c); + if (!(ret = (char **)malloc(sizeof(char *) * (size + 1)))) + return (NULL); + ret[size] = 0; + k = 0; + beg_word = (char *)s; + while (k < size) + { + beg_word = get_next(&s, &len_word, c); + if (!(ret[k] = (char *)malloc(sizeof(char) * (len_word + 1)))) + return (free_all(ret, k)); + ft_strlcpy(ret[k], beg_word, len_word + 1); + ++k; + } + return (ret); +} diff --git a/backup/libft/ft_strchr.c b/backup/libft/ft_strchr.c new file mode 100644 index 0000000..fcd9422 --- /dev/null +++ b/backup/libft/ft_strchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 17:43:54 by yekim #+# #+# */ +/* Updated: 2020/10/11 06:29:17 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + size_t idx; + + idx = 0; + while (s[idx]) + { + if (s[idx] == (unsigned char)c) + return ((char *)s + idx); + ++idx; + } + return (c == 0 ? (char *)s + idx : NULL); +} diff --git a/backup/libft/ft_strdup.c b/backup/libft/ft_strdup.c new file mode 100644 index 0000000..0582d67 --- /dev/null +++ b/backup/libft/ft_strdup.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 16:01:38 by yekim #+# #+# */ +/* Updated: 2020/10/06 07:41:01 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *ret; + size_t len; + + len = ft_strlen((char *)s); + if (!(ret = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + ft_strlcpy(ret, s, len + 1); + return (ret); +} diff --git a/backup/libft/ft_strjoin.c b/backup/libft/ft_strjoin.c new file mode 100644 index 0000000..1aba0c9 --- /dev/null +++ b/backup/libft/ft_strjoin.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/16 06:45:18 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:45:19 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t len; + size_t idx; + char *ret; + + if (s1 == NULL || s2 == NULL) + return (NULL); + len = ft_strlen((char *)s1) + ft_strlen((char *)s2); + if (!(ret = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + idx = ft_strlcpy(ret, s1, len + 1); + ft_strlcpy(ret + idx, s2, len + 1); + return (ret); +} diff --git a/backup/libft/ft_strlcat.c b/backup/libft/ft_strlcat.c new file mode 100644 index 0000000..dde2957 --- /dev/null +++ b/backup/libft/ft_strlcat.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 17:43:57 by yekim #+# #+# */ +/* Updated: 2020/10/06 10:29:19 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + size_t len_dest; + size_t len_src; + + len_dest = ft_strlen((char *)dest); + len_src = ft_strlen((char *)src); + if (size <= len_dest) + return (len_src + size); + ft_strlcpy(&(dest[len_dest]), src, size - len_dest); + return (len_src + len_dest); +} diff --git a/backup/libft/ft_strlcpy.c b/backup/libft/ft_strlcpy.c new file mode 100644 index 0000000..b9517ff --- /dev/null +++ b/backup/libft/ft_strlcpy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 17:44:00 by yekim #+# #+# */ +/* Updated: 2020/10/06 11:31:45 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + size_t ret; + size_t len_src; + + ret = 0; + if (src == NULL) + return (ret); + len_src = ft_strlen(src); + if (dest == NULL || size == 0) + return (len_src); + while (ret < len_src && ret < size - 1) + { + dest[ret] = src[ret]; + ++ret; + } + dest[ret] = '\0'; + return (len_src); +} diff --git a/backup/libft/ft_strlen 2.o b/backup/libft/ft_strlen 2.o new file mode 100644 index 0000000..63022cc Binary files /dev/null and b/backup/libft/ft_strlen 2.o differ diff --git a/backup/libft/ft_strlen.c b/backup/libft/ft_strlen.c new file mode 100644 index 0000000..d295394 --- /dev/null +++ b/backup/libft/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:22 by yekim #+# #+# */ +/* Updated: 2021/03/01 13:45:03 by jackjoo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + size_t ret; + + ret = 0; + if (str == NULL) + return (0); + while (*(str++)) + ++ret; + return (ret); +} diff --git a/backup/libft/ft_strmapi.c b/backup/libft/ft_strmapi.c new file mode 100644 index 0000000..d71aece --- /dev/null +++ b/backup/libft/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:45:21 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:45:22 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *ret; + size_t idx; + size_t len; + + if (s == NULL || f == NULL) + return (NULL); + len = ft_strlen((char *)s); + if (!(ret = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + ret[len] = '\0'; + idx = 0; + while (s[idx]) + { + ret[idx] = f(idx, s[idx]); + ++idx; + } + return (ret); +} diff --git a/backup/libft/ft_strncmp.c b/backup/libft/ft_strncmp.c new file mode 100644 index 0000000..5fdacab --- /dev/null +++ b/backup/libft/ft_strncmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 17:44:06 by yekim #+# #+# */ +/* Updated: 2020/10/06 16:50:05 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t idx; + unsigned char *us1; + unsigned char *us2; + + us1 = (unsigned char*)s1; + us2 = (unsigned char*)s2; + idx = 0; + while ((us1[idx] || us2[idx]) && idx < n) + { + if (us1[idx] != us2[idx]) + break ; + ++idx; + } + return (idx == n ? 0 : (int)(us1[idx] - us2[idx])); +} diff --git a/backup/libft/ft_strnstr.c b/backup/libft/ft_strnstr.c new file mode 100644 index 0000000..f8cca7a --- /dev/null +++ b/backup/libft/ft_strnstr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 17:44:09 by yekim #+# #+# */ +/* Updated: 2020/10/11 06:25:18 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t n) +{ + size_t len_haystack; + size_t len_needle; + size_t count; + + if (*needle == '\0') + return ((char *)haystack); + count = 0; + len_haystack = ft_strlen((char *)haystack); + len_haystack = len_haystack >= n ? n : len_haystack; + len_needle = ft_strlen((char *)needle); + if (len_needle > len_haystack) + return (NULL); + while (*haystack) + { + if (!ft_strncmp(haystack, needle, ft_strlen((char *)needle))) + return ((char *)haystack); + if (count >= (len_haystack - len_needle)) + break ; + ++haystack; + ++count; + } + return (NULL); +} diff --git a/backup/libft/ft_strrchr.c b/backup/libft/ft_strrchr.c new file mode 100644 index 0000000..c21f58a --- /dev/null +++ b/backup/libft/ft_strrchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 17:44:12 by yekim #+# #+# */ +/* Updated: 2020/10/06 16:40:07 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t idx; + + idx = ft_strlen(s); + while (idx > 0) + { + if (s[idx] == (unsigned char)c) + return ((char *)s + idx); + --idx; + } + return (s[idx] == c ? (char *)s + idx : NULL); +} diff --git a/backup/libft/ft_strtrim.c b/backup/libft/ft_strtrim.c new file mode 100644 index 0000000..2abe0c7 --- /dev/null +++ b/backup/libft/ft_strtrim.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:45:24 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:45:25 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int get_first(const char *s1, const char *set) +{ + size_t len; + size_t idx; + + len = ft_strlen(s1); + idx = 0; + while (idx < len) + { + if (ft_strchr(set, s1[idx]) == 0) + break ; + idx++; + } + return (idx); +} + +static int get_last(const char *s1, const char *set) +{ + size_t len; + size_t idx; + + len = ft_strlen(s1); + idx = 0; + while (idx < len) + { + if (ft_strchr(set, s1[len - idx - 1]) == 0) + break ; + idx++; + } + return (len - idx); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + int first; + int last; + char *ret; + + if (s1 == NULL) + return (NULL); + if (set == NULL) + return (ft_strdup(s1)); + first = get_first((char *)s1, set); + last = get_last((char *)s1, set); + if (first >= last) + return (ft_strdup("")); + if (!(ret = (char *)malloc(sizeof(char) * (last - first + 1)))) + return (NULL); + ft_strlcpy(ret, s1 + first, last - first + 1); + return (ret); +} diff --git a/backup/libft/ft_substr.c b/backup/libft/ft_substr.c new file mode 100644 index 0000000..f685dc4 --- /dev/null +++ b/backup/libft/ft_substr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/16 06:45:27 by yekim #+# #+# */ +/* Updated: 2020/10/16 06:45:27 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t len_s; + char *ret; + + if (s == NULL) + return (NULL); + len_s = ft_strlen((char *)s); + if (start >= (unsigned int)len_s) + return (ft_calloc(1, sizeof(char))); + len = (len > (len_s - start)) ? (len_s - start) : len; + if (!(ret = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + ft_strlcpy(ret, s + start, len + 1); + return (ret); +} diff --git a/backup/libft/ft_tolower.c b/backup/libft/ft_tolower.c new file mode 100644 index 0000000..bbdc00e --- /dev/null +++ b/backup/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 18:22:43 by yekim #+# #+# */ +/* Updated: 2020/09/29 18:22:44 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + return (c - 65 + 97); + return (c); +} diff --git a/backup/libft/ft_toupper.c b/backup/libft/ft_toupper.c new file mode 100644 index 0000000..49cffb7 --- /dev/null +++ b/backup/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 18:22:45 by yekim #+# #+# */ +/* Updated: 2020/09/29 18:22:46 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + return (c - 97 + 65); + return (c); +} diff --git a/backup/libft/libft.h b/backup/libft/libft.h new file mode 100644 index 0000000..1e9d7e3 --- /dev/null +++ b/backup/libft/libft.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yekim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/29 16:06:24 by yekim #+# #+# */ +/* Updated: 2020/10/10 07:47:56 by yekim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include + +typedef struct s_list +{ + void *data; + struct s_list *next; + struct s_list *prev; +} t_list; + +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memccpy(void *dest, const void *src, int c, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *str); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlcat(char *dest, const char *src, size_t size); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strnstr(const char *haystack, + const char *needle, size_t n); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *nptr); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_toupper(int c); +int ft_tolower(int c); +void *ft_calloc(size_t nmemb, size_t size); +char *ft_strdup(const char *s); + +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void *data); + +void *ft_memset(void *b, int c, size_t len); +void ft_lstadd_front(t_list **lst, t_list *new_lst); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new_node); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, + void *(*f)(void *), void (*del)(void *)); + +#endif diff --git a/backup/pipe_test/a.out b/backup/pipe_test/a.out new file mode 100755 index 0000000..a552570 Binary files /dev/null and b/backup/pipe_test/a.out differ diff --git a/backup/pipe_test/main1 b/backup/pipe_test/main1 new file mode 100644 index 0000000..e69de29 diff --git a/backup/pipe_test/main2 b/backup/pipe_test/main2 new file mode 100644 index 0000000..e69de29 diff --git a/backup/pipe_test/main3 b/backup/pipe_test/main3 new file mode 100644 index 0000000..e69de29 diff --git a/backup/pipe_test/main4 b/backup/pipe_test/main4 new file mode 100644 index 0000000..e69de29 diff --git a/backup/pipe_test/microshell.c b/backup/pipe_test/microshell.c new file mode 100644 index 0000000..81af80a --- /dev/null +++ b/backup/pipe_test/microshell.c @@ -0,0 +1,288 @@ +#include +#include +#include +#include + +#define SIDE_OUT 0 +#define SIDE_IN 1 + +#define STDIN 0 +#define STDOUT 1 +#define STDERR 2 + +#define TYPE_END 0 +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +#ifdef TEST_SH +# define TEST 1 +#else +# define TEST 0 +#endif + +typedef struct s_list +{ + char **args; + int length; + int type; + int pipes[2]; + struct s_list *previous; + struct s_list *next; +} t_list; + +int ft_strlen(char const *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int show_error(char const *str) +{ + if (str) + write(STDERR, str, ft_strlen(str)); + return (EXIT_FAILURE); +} + +int exit_fatal(void) +{ + show_error("error: fatal\n"); + exit(EXIT_FAILURE); + return (EXIT_FAILURE); +} + +void *exit_fatal_ptr(void) +{ + exit_fatal(); + exit(EXIT_FAILURE); + return (NULL); +} + +char *ft_strdup(char const *str) +{ + char *copy; + int i; + + if (!(copy = (char*)malloc(sizeof(*copy) * (ft_strlen(str) + 1)))) + return (exit_fatal_ptr()); + i = 0; + while (str[i]) + { + copy[i] = str[i]; + i++; + } + copy[i] = 0; + return (copy); +} + +// make new list and add arguments in new list +int list_push(t_list **list, char *arg) +{ + t_list *new; + int add_arg(); + + if (!(new = (t_list*)malloc(sizeof(*new)))) + return (exit_fatal()); + new->args = NULL; + new->length = 0; + new->type = TYPE_END; + new->previous = NULL; + new->next = NULL; + if (*list) + { + (*list)->next = new; + new->previous = *list; + } + *list = new; + return (add_arg(new, arg)); +} + +int list_rewind(t_list **list) +{ + while (*list && (*list)->previous) + *list = (*list)->previous; + return (EXIT_SUCCESS); +} + +int list_clear(t_list **cmds) +{ + t_list *tmp; + int i; + + list_rewind(cmds); + while (*cmds) + { + tmp = (*cmds)->next; + i = 0; + while (i < (*cmds)->length) + free((*cmds)->args[i++]); + free((*cmds)->args); + free(*cmds); + *cmds = tmp; + } + *cmds = NULL; + return (EXIT_SUCCESS); +} + +int add_arg(t_list *cmd, char *arg) +{ + char **tmp; + int i; + + i = 0; + tmp = NULL; + if (!(tmp = (char**)malloc(sizeof(*tmp) * (cmd->length + 2)))) + return (exit_fatal()); + while (i < cmd->length) + { + tmp[i] = cmd->args[i]; + i++; + } + if (cmd->length > 0) + free(cmd->args); + cmd->args = tmp; + cmd->args[i++] = ft_strdup(arg); + cmd->args[i] = 0; + cmd->length++; +#if 0 + printf("cmd: %s\n", cmd->args[0]); + for (int i = 0; cmd->args[i]; ++i) + printf("arg[%d]: %s\n", i, cmd->args[i]); +#endif + return (EXIT_SUCCESS); +} + +int parse_arg(t_list **cmds, char *arg) +{ + int is_break; + + is_break = (strcmp(";", arg) == 0); + if (is_break && !*cmds) + return (EXIT_SUCCESS); + else if (!is_break && (!*cmds || (*cmds)->type > TYPE_END)) + { + printf("before list_push function\n"); + return (list_push(cmds, arg)); + } + else if (strcmp("|", arg) == 0) + { + printf("pipe case\n"); + (*cmds)->type = TYPE_PIPE; + } + else if (is_break) + { + printf("break case\n"); + (*cmds)->type = TYPE_BREAK; + } + else + { + printf("add_arg function start\n"); + return (add_arg(*cmds, arg)); + } + return (EXIT_SUCCESS); +} + +int exec_cmd(t_list *cmd, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (cmd->type == TYPE_PIPE || (cmd->previous && cmd->previous->type == TYPE_PIPE)) + { + pipe_open = 1; + if (pipe(cmd->pipes)) + return (exit_fatal()); + } + pid = fork(); + if (pid < 0) + return (exit_fatal()); + else if (pid == 0) + { + if (cmd->type == TYPE_PIPE + && dup2(cmd->pipes[SIDE_IN], STDOUT) < 0) + return (exit_fatal()); + if (cmd->previous && cmd->previous->type == TYPE_PIPE + && dup2(cmd->previous->pipes[SIDE_OUT], STDIN) < 0) + return (exit_fatal()); + if ((ret = execve(cmd->args[0], cmd->args, env)) < 0) + { + show_error("error: cannot execute "); + show_error(cmd->args[0]); + show_error("\n"); + } + exit(ret); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(cmd->pipes[SIDE_IN]); + if (!cmd->next || cmd->type == TYPE_BREAK) + close(cmd->pipes[SIDE_OUT]); + } + if (cmd->previous && cmd->previous->type == TYPE_PIPE) + close(cmd->previous->pipes[SIDE_OUT]); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + } + return (ret); +} + +int exec_cmds(t_list **cmds, char **env) +{ + t_list *crt; + int ret; + + ret = EXIT_SUCCESS; + list_rewind(cmds); + while (*cmds) + { + crt = *cmds; + if (strcmp("cd", crt->args[0]) == 0) + { + ret = EXIT_SUCCESS; + if (crt->length < 2) + ret = show_error("error: cd: bad arguments\n"); + else if (chdir(crt->args[1])) + { + ret = show_error("error: cd: cannot change directory to "); + show_error(crt->args[1]); + show_error("\n"); + } + } + else + ret = exec_cmd(crt, env); + if (!(*cmds)->next) + break ; + *cmds = (*cmds)->next; + } + return (ret); +} + +int main(int argc, char **argv, char **env) +{ + t_list *cmds; + int i; + int ret; + + ret = EXIT_SUCCESS; + cmds = NULL; + i = 1; + printf("Program Start\n"); + while (i < argc) + parse_arg(&cmds, argv[i++]); + if (cmds) + ret = exec_cmds(&cmds, env); + list_clear(&cmds); + if (TEST) + while (1); + return (ret); +} diff --git a/backup/pipe_test/pipe_basic.c b/backup/pipe_test/pipe_basic.c new file mode 100644 index 0000000..55b2414 --- /dev/null +++ b/backup/pipe_test/pipe_basic.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + printf("DEBUG=========================\n"); + dup2(fds[1], STDOUT_FILENO); + write(STDOUT_FILENO, DATA, DATA_LEN); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + return (0); +} diff --git a/backup/pipe_test/pipe_ex.c b/backup/pipe_test/pipe_ex.c new file mode 100644 index 0000000..03c0bee --- /dev/null +++ b/backup/pipe_test/pipe_ex.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 3 +#define TYPE_REOUT 4 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd; + struct s_set *next; + struct s_set *prev; +} t_set; + +int run(t_set *set, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (set->type == TYPE_PIPE || (set->prev && set->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + pipe(set->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + if (set->type == TYPE_PIPE) + dup2(set->fds[1], STDOUT_FILENO); + + if (set->type == TYPE_BREAK) + dup2(set->fd, STDIN_FILENO); + + if (set->prev && (set->prev->type == TYPE_PIPE)) + dup2(set->prev->fds[0], STDIN_FILENO); + if ((ret = execve(set->args[0], set->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + close(set->fds[1]); + if (!set->next || set->type == TYPE_BREAK) + close(set->fds[0]); + } + if (set->prev && set->prev->type == TYPE_PIPE) + close(set->prev->fds[0]); + if (set->type == TYPE_BREAK) + close(set->fd); + printf("parent process finished==============\n"); + } + return (ret); +} +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_set set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_set set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + set2.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} +#endif + +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_set set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + printf("first run\n"); + run(&set1, env); + return (0); +} +#endif +int main(int argc, char *argv[], char *env[]) { + t_set set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_RDONLY); + + run(&set1, env); + return (0); +} diff --git a/backup/pipe_test/pipe_redir_ex.c b/backup/pipe_test/pipe_redir_ex.c new file mode 100644 index 0000000..6e14efc --- /dev/null +++ b/backup/pipe_test/pipe_redir_ex.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 4 +#define TYPE_REOUT 8 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd[2]; + struct s_set *next; + struct s_set *prev; +} t_set; + +int run(t_set *set, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if ((set->type & TYPE_PIPE) || (set->prev && (set->prev->type & TYPE_PIPE))) + { + pipe_open = 1; + pipe(set->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + if (set->type & TYPE_PIPE) + dup2(set->fds[1], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (set->type & TYPE_REIN) + dup2(set->fd[0], STDIN_FILENO); + if (set->type & TYPE_REIN) + dup2(set->fd[1], STDIN_FILENO); + if (set->type & TYPE_REOUT) + dup2(set->fd[0], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (set->prev && (set->prev->type & TYPE_PIPE)) + dup2(set->prev->fds[0], STDIN_FILENO); + if ((ret = execve(set->args[0], set->args, env) < 0)) + exit(0); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(set->fds[1]); + if (!set->next || (set->type & TYPE_BREAK)) + close(set->fds[0]); + } + if (set->prev && (set->prev->type & TYPE_PIPE)) + close(set->prev->fds[0]); +#if 1 + if (set->type & TYPE_REIN) + close(set->fd[0]); + if (set->type & TYPE_REIN) + close(set->fd[1]); + if (set->type & TYPE_REOUT) + close(set->fd[0]); +#endif + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_set set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK | TYPE_REIN; + set1.fd[0] = open("test.txt", O_RDONLY); + set1.fd[1] = open("test2.txt", O_RDONLY); + +#if 0 + t_set set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "a"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK | TYPE_REOUT; + set2.fd = open("test2.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + run(&set1, env); + run(&set2, env); +#endif + run(&set1, env); + + return (0); +} diff --git a/backup/pipe_test/redir_ex.c b/backup/pipe_test/redir_ex.c new file mode 100644 index 0000000..241b401 --- /dev/null +++ b/backup/pipe_test/redir_ex.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + struct s_set *next; + struct s_set *prev; +} t_set; + +#if 0 +void add_args(char **args, char **buf) { + int i; + i = 1; + while (buf[i - 1]) + { + args[i] = buf[i - 1]; + ++i; + } + args[i] = NULL; +} +#endif + +int run(t_set *set, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (set->type == TYPE_PIPE || (set->prev && set->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + // both set1 and set2 use pipe function to connect fds[0], fds[1] + pipe(set->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + // fds[1] of set1 has result of ls -al + if (set->type == TYPE_PIPE) + dup2(set->fds[1], STDOUT_FILENO); + // fds[0] of set1 has input of ls -al for grep + if (set->prev && (set->prev->type == TYPE_PIPE)) + dup2(set->prev->fds[0], STDIN_FILENO); + if ((ret = execve(set->args[0], set->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + // close fds[1] of set1 + // close fds[1] of set2 + close(set->fds[1]); + // fds[0] of set1 is still open + if (!set->next || set->type == TYPE_BREAK) + // fds[0] of set2 is closed + close(set->fds[0]); + } + if (set->prev && set->prev->type == TYPE_PIPE) + // fds[0] of set1 is closed + close(set->prev->fds[0]); + printf("parent process finished==============\n"); + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_set set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_set set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} + +#if 0 +int run1(t_set *set, char **env) +{ + pipe(set->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_set(set); + dup2(set->fds[0], STDOUT_FILENO); + if (execve(set->args[0], set->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(set->fds[1]); + } + return (0); +} + +int run2(t_set *set, char **env) +{ + pipe(set->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_set(set); + dup2(set->fds[1], STDIN_FILENO); + if (execve(set->args[0], set->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(set->fds[0]); + } + return (0); +} +#endif diff --git a/backup/pipe_test/test.c b/backup/pipe_test/test.c new file mode 100644 index 0000000..80873d9 --- /dev/null +++ b/backup/pipe_test/test.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + pid_t pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) { // child process + close(fds[0]); + write(fds[1], DATA, DATA_LEN); + } + else { // parents process + waitpid(pid, &status, 0); + close(fds[1]); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + close(fds[0]); + } + return (0); +} diff --git a/backup/pipe_test/test.txt b/backup/pipe_test/test.txt new file mode 100644 index 0000000..30908be --- /dev/null +++ b/backup/pipe_test/test.txt @@ -0,0 +1,4 @@ +apple +banana +mango +pineapple diff --git a/backup/pipe_test/test2.txt b/backup/pipe_test/test2.txt new file mode 100644 index 0000000..89eb6b9 --- /dev/null +++ b/backup/pipe_test/test2.txt @@ -0,0 +1,3 @@ +orange +grape +tomato diff --git a/backup/playground/test_env_list.c b/backup/playground/test_env_list.c new file mode 100644 index 0000000..5513353 --- /dev/null +++ b/backup/playground/test_env_list.c @@ -0,0 +1,8 @@ +#include "../minishell.h" + +void test_env_list( + t_list *env_list) +{ + ps("======== TEST print sorted env ======\n"); + print_sorted_env(env_list); +} diff --git a/backup/playground/test_env_list.o b/backup/playground/test_env_list.o new file mode 100644 index 0000000..4495cf1 Binary files /dev/null and b/backup/playground/test_env_list.o differ diff --git a/backup/playground/test_pk_lst.c b/backup/playground/test_pk_lst.c new file mode 100644 index 0000000..8c66152 --- /dev/null +++ b/backup/playground/test_pk_lst.c @@ -0,0 +1,53 @@ +#include "../minishell.h" + +void test_pk_lst() +{ + t_slist *set_list; + t_slist *head; + t_slist *tmp; + + + ps("===TEST pk_lstnew ===\n"); + + set_list = NULL; + set_list = pk_lstnew(ft_strdup("test new1")); + printf(" %s\n", set_list->data->data); + printf(" %x\n", (unsigned int)set_list->next); + pk_lstdelone(set_list, &free); + + set_list = NULL; + set_list = pk_lstnew(ft_strdup("test new2")); + printf(" %s\n", set_list->data->data); + printf(" %x\n", (unsigned int)set_list->next); + pk_lstdelone(set_list, &free); + + ps("===TEST pk_lstadd_front===\n"); + + set_list = NULL; + pk_lstadd_front(&set_list, pk_lstnew(ft_strdup("test1"))); + pk_lstadd_front(&set_list, pk_lstnew(ft_strdup("test2"))); + pk_lstadd_front(&set_list, pk_lstnew(ft_strdup("test3"))); + + head = set_list; + while (head) + { + printf(" data: %s\n", head->data->data); + printf(" next: %x\n", (unsigned int)head->next); + head = head->next; + } + + ps("===TEST pk_delone===\n"); + head = set_list; + while (head) + { + printf(" data: %s\n", head->data->data); + printf(" next: %x\n", (unsigned int)head->next); + tmp = head; + head = head->next; + pk_lstdelone(tmp, &free); + } + + ps("======leaks check=====\n"); + while (1) + ; +} diff --git a/backup/playground/test_pk_lst.o b/backup/playground/test_pk_lst.o new file mode 100644 index 0000000..3c93f44 Binary files /dev/null and b/backup/playground/test_pk_lst.o differ diff --git a/backup/playground/test_pk_split.c b/backup/playground/test_pk_split.c new file mode 100644 index 0000000..26dd7c7 --- /dev/null +++ b/backup/playground/test_pk_split.c @@ -0,0 +1,17 @@ +#include "../minishell.h" + +#include +int main(void) { + char **res; + char *str = "abcd efg hI"; + char *str_cpy = "abcd efg__hi"; + + printf("str : %s\n", str); + printf("str_cpy: %s\n", str_cpy); + + res = pk_split(str, str_cpy, ' '); + for (int i = 0; res[i]; ++i) { + printf("res[%d]: %s\n", i, res[i]); + } + return (0); +} diff --git a/backup/srcs/.main..swp b/backup/srcs/.main..swp new file mode 100644 index 0000000..09ebcd8 Binary files /dev/null and b/backup/srcs/.main..swp differ diff --git a/backup/srcs/add_elist.c b/backup/srcs/add_elist.c new file mode 100644 index 0000000..638320a --- /dev/null +++ b/backup/srcs/add_elist.c @@ -0,0 +1,22 @@ +#include "../incs/minishell.h" + +int add_elist( + t_list **list_head, + char *key, char *val) +{ + t_list *tmp; + t_env *tmp_env; + + printf("KEY: %s, VALUE: %s\n", key, val); + if(list_head == 0) + return (1); + if (!(tmp_env = (t_env *)malloc(sizeof(t_env)))) + return (1); + if (!(tmp_env->key = ft_strdup(key))) + return (1); + if (!(tmp_env->val = ft_strdup(val))) + return (1); + tmp = ft_lstnew(tmp_env); + ft_lstadd_back(list_head, tmp); + return (0); +} diff --git a/backup/srcs/append_history_list.c b/backup/srcs/append_history_list.c new file mode 100644 index 0000000..509522d --- /dev/null +++ b/backup/srcs/append_history_list.c @@ -0,0 +1,18 @@ +#include "../incs/minishell.h" + +void append_history_list(t_list **history, char *line) +{ + t_list *new_list; + t_list *head; + t_list *head_next; + + new_list = ft_lstnew(line); + head = *history; + head_next = head->next; + new_list->next = head_next; + new_list->prev = head; + + head->next = new_list; + head_next->prev = new_list; + *history = head; +} diff --git a/backup/srcs/backup/gen_slist.c b/backup/srcs/backup/gen_slist.c new file mode 100644 index 0000000..dfbe3fe --- /dev/null +++ b/backup/srcs/backup/gen_slist.c @@ -0,0 +1,28 @@ +#include "../incs/minishell.h" + +//static int set_type(char **str, ) + +//t_list *gen_slist(char **str) +t_list *gen_slist(t_info *info, char **str) +{ + int error_num; + int idx; + t_set *tmp_set; + t_list *ret; + + ret = NULL; + error_num = 0; + idx = -1; + while (error_num == 0 && str[++idx]) + { + if (!(tmp_set = gen_set(info, str[idx]))) + error_num = 1; + if (str[idx + 1] == 0) + tmp_set->type = TYPE_BREAK; + else + tmp_set->type = TYPE_PIPE; + ft_lstadd_back(&ret, ft_lstnew(tmp_set)); + } + return (ret); +} + diff --git a/backup/srcs/backup/handle_redirect.bak b/backup/srcs/backup/handle_redirect.bak new file mode 100644 index 0000000..75f2833 --- /dev/null +++ b/backup/srcs/backup/handle_redirect.bak @@ -0,0 +1,109 @@ +#include "../incs/minishell.h" +static void remove_bracket(char **tmp_set, int tmp_idx) +{ + while (tmp_set[tmp_idx]) + { + printf("free: %s\n", tmp_set[tmp_idx]); + free(tmp_set[tmp_idx]); + tmp_set[tmp_idx++] = 0; + } +} + +static int is_bracket(char *c) +{ + if (exact_strncmp(c, "<") == 0) + return (TYPE_REIN); + if (exact_strncmp(c, ">") == 0) + return (TYPE_REOUT); + if (exact_strncmp(c, ">>") == 0) + return (TYPE_REOUT_D); + return (0); +} + +static int is_valid_filename(char *file_name, int type) +{ + char s[PATH_MAX]; + char *cwd; + char *path; + int ret; + + ret = 0; + if (file_name == NULL) + printf("zsh: parse error near `\n'"); + else if (is_bracket(file_name)) + printf("zsh: parse error near `%s'\n", file_name); + else if (type == TYPE_REIN) + { + cwd = getcwd(s, PATH_MAX); + path = get_bti_path(cwd, file_name); + if (!path) + printf("zsh: no such file or directory: %s\n", file_name); + free(path); + } + else + ret = 1; + return (ret); +} + +static int modify_fd(char *file_name, int type) +{ + int ret; + + ret = 1; + printf("modify fd!\n"); + if (type == TYPE_REIN) // < + { + printf("type: <, %s is open\n", file_name); +// g_fds[g_k] = open(file_name, O_CREAT | O_TRUNC | O_RDONLY, 0755); +// dup2(g_fds[g_k++], STDIN_FILENO); + } + else if (type == TYPE_REOUT) // > + { + printf("type: >, %s is open\n", file_name); +// g_fds[g_k] = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0755); +// dup2(g_fds[g_k++], STDOUT_FILENO); + } + else if (type == TYPE_REOUT_D) // >> + { +// printf("type: >>, %s is open\n", file_name); + g_fds[g_k] = open(file_name, O_CREAT | O_APPEND | O_WRONLY, 0755); + if (g_k == 0) + dup2(g_fds[g_k], STDOUT_FILENO); + else + g_fds[g_k] = dup(g_fds[g_k - 1]); + g_k++; + } + else + ret = 0; + return (ret); +} + +// test after closing fd write something on STDOUT +char **handle_redirect(char **tmp_set) +{ + int idx; + int tmp_idx; + int type; + int ret; + + ret = 0; + idx = 0; + while (tmp_set[idx] && !(is_bracket(tmp_set[idx]))) + idx++; + tmp_idx = idx; + while (ret > -1 && tmp_set[idx]) + { + type = is_bracket(tmp_set[idx]); + if (type) + { + if (is_valid_filename(tmp_set[idx + 1], type)) + ret = modify_fd(tmp_set[++idx], type); + else + ret = -1; + } + idx++; + } + if (ret > 0) + remove_bracket(tmp_set, tmp_idx); + return (tmp_set); +} diff --git a/backup/srcs/backup/handle_redirect.c b/backup/srcs/backup/handle_redirect.c new file mode 100644 index 0000000..c8bd977 --- /dev/null +++ b/backup/srcs/backup/handle_redirect.c @@ -0,0 +1,99 @@ +#include "../incs/minishell.h" +static void remove_bracket(char **tmp_set, int tmp_idx) +{ + while (tmp_set[tmp_idx]) + { + printf("free: %s\n", tmp_set[tmp_idx]); + free(tmp_set[tmp_idx]); + tmp_set[tmp_idx++] = 0; + } +} + + +static int is_valid_filename(char *file_name, int type) +{ + char s[PATH_MAX]; + char *cwd; + char *path; + int ret; + + ret = 0; + if (file_name == NULL) + printf("zsh: parse error near `\n'"); + else if (is_bracket(file_name)) + printf("zsh: parse error near `%s'\n", file_name); + else if (type == TYPE_REIN) + { + cwd = getcwd(s, PATH_MAX); + path = get_bti_path(cwd, file_name); + if (!path) + printf("zsh: no such file or directory: %s\n", file_name); + free(path); + } + else + ret = 1; + return (ret); +} + +static int modify_fd(char *file_name, int type) +{ + int ret; + + ret = 1; + printf("modify fd!\n"); + if (type == TYPE_REIN) // < + { + printf("type: <, %s is open\n", file_name); +// g_fds[g_k] = open(file_name, O_CREAT | O_TRUNC | O_RDONLY, 0755); +// dup2(g_fds[g_k++], STDIN_FILENO); + } + else if (type == TYPE_REOUT) // > + { + printf("type: >, %s is open\n", file_name); +// g_fds[g_k] = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0755); +// dup2(g_fds[g_k++], STDOUT_FILENO); + } + else if (type == TYPE_REOUT_D) // >> + { +// printf("type: >>, %s is open\n", file_name); + g_fds[g_k] = open(file_name, O_CREAT | O_APPEND | O_WRONLY, 0755); + if (g_k == 0) + dup2(g_fds[g_k], STDOUT_FILENO); + else + g_fds[g_k] = dup(g_fds[g_k - 1]); + g_k++; + } + else + ret = 0; + return (ret); +} + +// test after closing fd write something on STDOUT +char **handle_redirect(char **tmp_set) +{ + int idx; + int tmp_idx; + int type; + int ret; + + ret = 0; + idx = 0; + while (tmp_set[idx] && !(is_bracket(tmp_set[idx]))) + idx++; + tmp_idx = idx; + while (ret > -1 && tmp_set[idx]) + { + type = is_bracket(tmp_set[idx]); + if (type) + { + if (is_valid_filename(tmp_set[idx + 1], type)) + ret = modify_fd(tmp_set[++idx], type); + else + ret = -1; + } + idx++; + } + if (ret > 0) + remove_bracket(tmp_set, tmp_idx); + return (tmp_set); +} diff --git a/backup/srcs/backup/mod_elist.c b/backup/srcs/backup/mod_elist.c new file mode 100644 index 0000000..4b6ee23 --- /dev/null +++ b/backup/srcs/backup/mod_elist.c @@ -0,0 +1,22 @@ +#include "../incs/minishell.h" + +int mod_elist( + t_list **list_head, + char *key, char *val) +{ + int index; + int ret; + t_list *tar; + t_env *tmp_env; + + ret = 0; + if ((tar = get_elist(*list_head, key))) + { + tmp_env = (t_env *)tar->data; + free(tmp_env->val); + if (!(tmp_env->val = ft_strdup(val))) + ret = 1; + } + return (ret); +} + diff --git a/backup/srcs/backup/set_fd_info.c b/backup/srcs/backup/set_fd_info.c new file mode 100644 index 0000000..c08201d --- /dev/null +++ b/backup/srcs/backup/set_fd_info.c @@ -0,0 +1,89 @@ +#include "../incs/minishell.h" + +static int is_valid_filename(char *filename, int curr_type) +{ + char s[PATH_MAX]; + char *cwd; + char *path; + int ret; + + ret = 0; + if (filename == NULL) + printf("zsh: parse error near `\n'"); + else if (curr_type & TYPE_REIN) + { + cwd = getcwd(s, PATH_MAX); + path = get_bti_path(cwd, filename); + if (!path) + printf("zsh: no such file or directory: %s\n", filename); + else + { + free(path); + ret = 1; + } + } + else + ret = 1; + return (ret); +} + +static int seek_filename( + char *str, + char **filename) +{ + int idx; + int ret; + int beg; + int end; + + idx = 0; + while (str[idx] && str[idx] == ' ') + idx++; + beg = idx; + while (str[idx] && str[idx] != ' ' && !is_bracket(str[idx])) + idx++; + end = idx; + *filename = ft_substr(str, beg, end - beg); + if (!filename) + return (0); + return (end); +} + +static int set_fd_info_part(t_set *set, char *filename) +{ + int fd; + + + if (set->type & TYPE_REIN) + fd = open(filename, O_RDONLY); + if (set->type & TYPE_REOUT) + fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0755); + if (set->type & TYPE_REOUT_D) + fd = open(filename, O_CREAT | O_APPEND | O_WRONLY, 0755); + if (fd < 0) + return (0); + if (set->type & TYPE_REIN) + set->fd_in[set->fd_in_idx++] = fd; + if (set->type & (TYPE_REOUT | TYPE_REOUT_D)) + set->fd_out[set->fd_out_idx++] = fd; + return (1); +} + +int set_fd_info(t_set *set, char *str, int curr_type) +{ + int ret; + int idx_inc; + char *filename; + + filename = NULL; + idx_inc = seek_filename(str, &filename); + if (idx_inc == 0) + return (0); + ret = is_valid_filename(filename, curr_type); + if (ret) + ret = set_fd_info_part(set, filename); + if (ret) + ret = idx_inc; + free(filename); + return (ret); +} diff --git a/backup/srcs/calc.c b/backup/srcs/calc.c new file mode 100644 index 0000000..b1fa8ae --- /dev/null +++ b/backup/srcs/calc.c @@ -0,0 +1,15 @@ +#include "../incs/minishell.h" + +int calc_min(int num1, int num2) +{ + if (num1 > num2) + return (num2); + return (num1); +} + +int calc_max(int num1, int num2) +{ + if (num1 > num2) + return (num1); + return (num2); +} diff --git a/backup/srcs/categorize_cmd.c b/backup/srcs/categorize_cmd.c new file mode 100644 index 0000000..946e8de --- /dev/null +++ b/backup/srcs/categorize_cmd.c @@ -0,0 +1,23 @@ +#include "../incs/minishell.h" + +int categorize_cmd(t_set *set, t_info *info) +{ + int ret; + int run_check; + int idx; + + ret = 1; + run_check = 0; + if (del_quote(&(set->cmd))) + { + printf("ERROR: trim_cmd malloc error\n"); + return (1); + } + if (!(ret = select_sh_bti(set, info))) + printf("SHELL BUILTIN CASE\n"); + else if (ret && !(ret = run_bti(set, info->env_list))) + printf("BUILTIN CASE\n"); + else + printf("zsh: command not found: %s\n", set->cmd); + return (ret); +} diff --git a/backup/srcs/cvt_char_to_str.c b/backup/srcs/cvt_char_to_str.c new file mode 100644 index 0000000..08535f8 --- /dev/null +++ b/backup/srcs/cvt_char_to_str.c @@ -0,0 +1,13 @@ +#include "../incs/minishell.h" + +char *cvt_char_to_str(char c) +{ + char *ret; + + if (!(ret = (char *)malloc(sizeof(char) * 2))) + return (NULL); + ret[0] = c; + ret[1] = '\0'; + return (ret); +} + diff --git a/backup/srcs/del_quote.c b/backup/srcs/del_quote.c new file mode 100644 index 0000000..2e33552 --- /dev/null +++ b/backup/srcs/del_quote.c @@ -0,0 +1,54 @@ +#include "../incs/minishell.h" + +static int is_quote(char c) +{ + if (c == '\"' || c == '\'') + return (1); + return (0); +} + +static int get_cmd_size(char *str) +{ + int ret; + + ret = 0; + while (*str) + { + if (!is_quote(*str)) + ++ret; + ++str; + } + return (ret); +} + +static char *copy_without_quote(char *str) +{ + char *ret; + int ret_size; + int str_idx; + int ret_idx; + + ret_size = get_cmd_size(str); + if (!(ret = (char *)malloc(sizeof(char) * (ret_size + 1)))) + return (NULL); + ret[ret_size] = '\0'; + str_idx = -1; + ret_idx = -1; + while (str[++str_idx]) + { + if (!is_quote(str[str_idx])) + ret[++ret_idx] = str[str_idx]; + } + return (ret); +} + +int del_quote(char **str) +{ + char *ret; + + if (!(ret = copy_without_quote(*str))) + return (1); + free(*str); + *str = ret; + return (0); +} diff --git a/backup/srcs/env/sort_env.c b/backup/srcs/env/sort_env.c new file mode 100644 index 0000000..4749c0a --- /dev/null +++ b/backup/srcs/env/sort_env.c @@ -0,0 +1,91 @@ +#include "../../incs/minishell.h" + +static int has_to_change(char *s1, char*s2) +{ + int s1_len; + int s2_len; + int max_len; + + s1_len = ft_strlen(s1); + s2_len = ft_strlen(s2); + max_len = (s1_len > s2_len) ? s1_len : s2_len; + return (ft_strncmp(s1, s2, max_len)); +} + +static int get_str_len( + char **str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +static char** sort_env( + char **env, + int env_len) +{ + int i; + int sorted_flag; + char *tmp; + + sorted_flag = 1; + while (--env_len && sorted_flag) + { + i = -1; + sorted_flag = 0; + while (++i < env_len - 1) + { + if (has_to_change(env[i], env[i + 1]) > 0) + { + tmp = env[i]; + env[i] = env[i + 1]; + env[i + 1] = tmp; + sorted_flag = 1; + } + } + } + return (env); +} + +static char *convert_list2str( + t_list *list) +{ + t_list *cur; + char *str; + char *tmp; + + cur = list; + str = ft_strdup(""); + while (cur) + { + tmp = ft_strjoin(str, cur->data); + free(str); + str = ft_strjoin(tmp, "\n"); + free(tmp); + cur = cur->next; + } + return (str); +} + +void print_sorted_env( + t_list *env_list) +{ + char *tmp; + char **str; + int i; + + tmp = convert_list2str(env_list); + str = ft_split(tmp, '\n'); + str = sort_env(str, get_str_len(str) + 1); + i = -1; + while (str[++i]) + { + printf("%s\n", str[i]); + free(str[i]); + } + free(tmp); + free(str); +} diff --git a/backup/srcs/exact_strncmp.c b/backup/srcs/exact_strncmp.c new file mode 100644 index 0000000..5e951ec --- /dev/null +++ b/backup/srcs/exact_strncmp.c @@ -0,0 +1,9 @@ +#include "../incs/minishell.h" + +int exact_strncmp(char *str1, char *str2) +{ + int len; + + len = get_max_strlen(str1, str2); + return (ft_strncmp(str1, str2, len)); +} diff --git a/backup/srcs/exit_fatal.c b/backup/srcs/exit_fatal.c new file mode 100644 index 0000000..ca73902 --- /dev/null +++ b/backup/srcs/exit_fatal.c @@ -0,0 +1,7 @@ +#include "../incs/minishell.h" +int exit_fatal(void) +{ + show_error("error: fatal\n"); + exit(EXIT_FAILURE); + return (EXIT_FAILURE); +} diff --git a/backup/srcs/exit_shell.c b/backup/srcs/exit_shell.c new file mode 100644 index 0000000..1fd1dd1 --- /dev/null +++ b/backup/srcs/exit_shell.c @@ -0,0 +1,23 @@ +#include "../incs/minishell.h" + +int exit_shell(t_info *info) +{ + int ret; + int i; + + ret = 0; + + ft_lstclear(&(info->set_str_list), &free); + ft_lstclear(&(info->set_list), &free_set); + ft_lstclear(&(info->env_list), &free_env); + ft_lstclear(&(info->history), &free_env); + if (info->env_list) + { + ft_lstclear(&(info->env_list), &free_env); + free(info->env_list); + info->env_list = NULL; + } + return (ret); +} + + diff --git a/backup/srcs/free_darr.c b/backup/srcs/free_darr.c new file mode 100644 index 0000000..df050b9 --- /dev/null +++ b/backup/srcs/free_darr.c @@ -0,0 +1,13 @@ +#include "../incs/minishell.h" + +char **free_darr(char **tab, int limit) +{ + int idx; + + idx = -1; + while (tab[++idx] && limit--) + free(tab[idx]); + free(tab); + tab = NULL; + return (0); +} diff --git a/backup/srcs/free_elist.c b/backup/srcs/free_elist.c new file mode 100644 index 0000000..3a763ed --- /dev/null +++ b/backup/srcs/free_elist.c @@ -0,0 +1,10 @@ +#include "../incs/minishell.h" + +void free_elist(t_list *list_head) +{ + t_list *tmp; + + tmp = list_head; + list_head->data = NULL; + ft_lstdelone(tmp, &free_env); +} diff --git a/backup/srcs/free_env.c b/backup/srcs/free_env.c new file mode 100644 index 0000000..79348b4 --- /dev/null +++ b/backup/srcs/free_env.c @@ -0,0 +1,14 @@ +#include "../incs/minishell.h" + +void free_env(void *_env) +{ + t_env *env; + + env = (t_env *)_env; + free(env->key); + env->key = NULL; + free(env->val); + env->val = NULL; + free(env); + env = NULL; +} diff --git a/backup/srcs/free_set.c b/backup/srcs/free_set.c new file mode 100644 index 0000000..74c88b8 --- /dev/null +++ b/backup/srcs/free_set.c @@ -0,0 +1,13 @@ +#include "../incs/minishell.h" + +void free_set(void *_set) +{ + t_set *set; + + set = (t_set *)_set; + free_darr(set->set, INF); + free(set->cmd); + set->cmd = NULL; + free(set); + set = NULL; +} diff --git a/backup/srcs/ft_cursor.c b/backup/srcs/ft_cursor.c new file mode 100644 index 0000000..6472676 --- /dev/null +++ b/backup/srcs/ft_cursor.c @@ -0,0 +1,61 @@ +#include "../incs/minishell.h" + + +#define TC_CURSOR_UP "\033[A" +#define TC_CURSOR_DOWN "\033[B" +#define TC_CURSOR_RIGHT "\033[C" +#define TC_CURSOR_LEFT "\033[D" +#define TC_ERASE_LINE "\033[1K" + +int ft_cursor_mv_head(t_tc tc) +{ + char *str; + + tputs(tgoto(tc.tc_str[TC_CM], 0, tc.cursor.row), 1, ft_putchar_tc); + return (0); +} + +int ft_cursor_mv_left( + int col, + int left_limit) +{ + char *str; + int itr; + + str = "\033[D"; + if (col > left_limit + 1) + write(STDIN_FILENO, str, ft_strlen(str)); + return (0); +} + +int ft_cursor_mv_right( + int col, + int right_limit) +{ + char *str; + int itr; + + str = "\033[C"; + if (col <= right_limit) + write(STDIN_FILENO, str, ft_strlen(str)); + return (0); +} + +int ft_cursor_clr_line_all(t_tc tc) +{ + tputs(tc.tc_str[TC_DL], 1, ft_putchar_tc); + tputs(tgoto(tc.tc_str[TC_CM], 0, tc.cursor.row), 1, ft_putchar_tc); + return (0); +} + +int ft_cursor_clr_line_end( + t_tc tc, + int left_limit) +{ + if (tc.cursor.col > left_limit + 1) + { + ft_putchar_fd('\b', STDOUT_FILENO); + tputs(tc.tc_str[TC_CE], 1, ft_putchar_tc); + } + return (0); +} diff --git a/backup/srcs/ft_cursor2.c b/backup/srcs/ft_cursor2.c new file mode 100644 index 0000000..ab9b184 --- /dev/null +++ b/backup/srcs/ft_cursor2.c @@ -0,0 +1,24 @@ +#include "../incs/minishell.h" + +void get_cursor_pos(int *col, int *row) +{ + int cnt; + int idx; + char buf[255]; + int read_size; + + write(STDIN_FILENO, "\033[6n", ft_strlen("\033[6n")); + read_size = read(STDIN_FILENO, buf, 255); + buf[read_size] = '\0'; + idx = -1; + while (!ft_isdigit(buf[idx])) + ++idx; + *row = 0; + while (ft_isdigit(buf[idx])) + *row = *row * 10 + buf[idx++] - '0'; + ++idx; + *col = 0; + while (ft_isdigit(buf[idx])) + *col = *col * 10 + buf[idx++] - '0'; +} + diff --git a/backup/srcs/ft_putchar_tc.c b/backup/srcs/ft_putchar_tc.c new file mode 100644 index 0000000..f548f9f --- /dev/null +++ b/backup/srcs/ft_putchar_tc.c @@ -0,0 +1,8 @@ +#include "../incs/minishell.h" + +int ft_putchar_tc(int tc) +{ + write(STDOUT_FILENO, &tc, 1); + return (0); +} + diff --git a/backup/srcs/gen_elist.c b/backup/srcs/gen_elist.c new file mode 100644 index 0000000..d6df579 --- /dev/null +++ b/backup/srcs/gen_elist.c @@ -0,0 +1,20 @@ +#include "../incs/minishell.h" + +t_list *gen_elist(char **str) +{ + int error_num; + int idx; + t_env *tmp_env; + t_list *ret; + + ret = NULL; + error_num = 0; + idx = -1; + while (error_num == 0 && str[++idx]) + { + if (!(tmp_env = gen_env(str[idx]))) + error_num = 1; + ft_lstadd_back(&ret, ft_lstnew(tmp_env)); + } + return (ret); +} diff --git a/backup/srcs/gen_env.c b/backup/srcs/gen_env.c new file mode 100644 index 0000000..2ac5176 --- /dev/null +++ b/backup/srcs/gen_env.c @@ -0,0 +1,67 @@ +#include "../incs/minishell.h" + +static int get_eq_idx(char *set_cpy) +{ + int ret; + + ret = 0; + while (*set_cpy) + { + if (*set_cpy == '=') + return (ret); + ++set_cpy; + ++ret; + } + return (-1); +} + +static char *get_item(char *str, char *str_cpy, char delimit) +{ + char **tab; + char *ret; + + if ((tab = pk_split(str, str_cpy, delimit, 1)) == 0) + return (NULL); + ret = ft_strdup(tab[0]); + free_darr(tab, 1); + return (ret); +} + +static t_env *get_env_form(char *key, char *val) +{ + t_env *ret; + + ret = NULL; + if (!(ret = (t_env *)malloc(sizeof(t_env)))) + return (ret); + ret->key = key; + ret->val = val; + return (ret); +} + +t_env *gen_env(char *str) +{ + char *str_cpy; + char *key; + char *val; + int eq_idx; + t_list *tmp_elist; + + if (!(str_cpy = ft_strdup(str))) + return (NULL); + if (handle_quote(str, &str_cpy, '=') > 0) + return (NULL); + if (str_cpy[0] == '=') + key = ft_strdup(""); + else if (!(key = get_item(str, str_cpy, '='))) + return (NULL); + eq_idx = get_eq_idx(str_cpy) + 1; + // in case that export has only key without val + if (eq_idx == 0) + val = ft_strdup(""); + else if (!(val = get_item(&str[eq_idx], &str_cpy[eq_idx], ' '))) + return (NULL); + // in case that finding key in env_list + free(str_cpy); + return (get_env_form(key, val)); +} diff --git a/backup/srcs/gen_set.c b/backup/srcs/gen_set.c new file mode 100644 index 0000000..1638951 --- /dev/null +++ b/backup/srcs/gen_set.c @@ -0,0 +1,25 @@ +#include "../incs/minishell.h" + +t_set *gen_set(t_info *info, char *set_str) +{ + char *set_fd_res; + char *set_fd_res_cpy; + char **tmp_set; + t_set *ret; + + if (!(ret = (t_set *)malloc(sizeof(t_set)))) + return (NULL); + set_fd_res = set_fd(ret, set_str); + if (set_fd_res == NULL) + return (NULL); + set_fd_res_cpy = ft_strdup(set_fd_res); + handle_quote(set_str, &set_fd_res_cpy, ' '); + if (!(tmp_set = pk_split(set_fd_res, set_fd_res_cpy, ' ', INF))) + return (NULL); + ret->set = tmp_set; + ret->cmd = ft_strdup(tmp_set[0]); + ret->args = &tmp_set[1]; + free(set_fd_res); + free(set_fd_res_cpy); + return (ret); +} diff --git a/backup/srcs/gen_set_list.c b/backup/srcs/gen_set_list.c new file mode 100644 index 0000000..0c0e627 --- /dev/null +++ b/backup/srcs/gen_set_list.c @@ -0,0 +1,55 @@ +#include "../incs/minishell.h" + +t_list *gen_slist(t_info *info, char **str) +{ + int idx; + t_set *tmp_set; + t_list *ret; + + ret = NULL; + idx = -1; + while (str[++idx]) + { + tmp_set = gen_set(info, str[idx]); + if (tmp_set == NULL) + { + ft_lstclear(&ret, &free_set); + return (NULL); + } + else + { + if (str[idx + 1] == NULL) + tmp_set->type |= TYPE_BREAK; + else + tmp_set->type |= TYPE_PIPE; + ft_lstadd_back(&ret, ft_lstnew(tmp_set)); + } + } + return (ret); +} + +t_list *gen_set_list(t_info *info) +{ + char *line; + char *line_cpy; + char **line_part; + t_list *ret; + int error_num; + + line = info->set_str_list->data; + ret = NULL; + error_num = 0; + if (!(line_cpy = ft_strdup(line))) + return (NULL); + if (handle_quote(line, &line_cpy, '|')) + error_num = turn_on_bit(error_num, 0); + if (!(line_part = pk_split(line, line_cpy, '|', INF))) + error_num = turn_on_bit(error_num, 1); + ret = gen_slist(info, line_part); + free (line_cpy); + if (!check_bit(error_num, 1)) + free_darr(line_part, INF); + if (error_num) + return (NULL); + return (ret); +} diff --git a/backup/srcs/gen_set_str_list.c b/backup/srcs/gen_set_str_list.c new file mode 100644 index 0000000..f148502 --- /dev/null +++ b/backup/srcs/gen_set_str_list.c @@ -0,0 +1,28 @@ +#include "../incs/minishell.h" + +// devide cmd, args set by ; +// return line_list as linked_list form +t_list *gen_set_str_list(char *line) +{ + char *line_cpy; + char **line_part; + t_list *ret; + int error_num; + + ret = NULL; + error_num = 0; + if (!(line_cpy = ft_strdup(line))) + return (NULL); + if (handle_quote(line, &line_cpy, ';')) + error_num = turn_on_bit(error_num, 0); + if (!(line_part = pk_split(line, line_cpy, ';', INF))) + error_num = turn_on_bit(error_num, 1); + if (!error_num && set_darr_to_list(&ret, line_part)) + error_num = turn_on_bit(error_num, 2); + free (line_cpy); + if (!check_bit(error_num, 1)) + free_darr(line_part, INF); + if (error_num) + return (NULL); + return (ret); +} diff --git a/backup/srcs/get_bti_path.c b/backup/srcs/get_bti_path.c new file mode 100644 index 0000000..8946a0c --- /dev/null +++ b/backup/srcs/get_bti_path.c @@ -0,0 +1,26 @@ +#include "../incs/minishell.h" + +char *get_bti_path(char *cand, char *cmd) +{ + DIR *dir_name; + struct dirent *item; + char *ret; + char *tmp; + + ret = NULL; + if(!(dir_name = opendir(cand))) + return (NULL); + while ((item = readdir(dir_name))) + { + if (!exact_strncmp(item->d_name, cmd)) + { + ret = ft_strjoin(cand, "/"); + tmp = ret; + ret = ft_strjoin(ret, item->d_name); + free(tmp); + return (ret); + } + } + closedir(dir_name); + return (ret); +} diff --git a/backup/srcs/get_dollar_eval.c b/backup/srcs/get_dollar_eval.c new file mode 100644 index 0000000..1361f5e --- /dev/null +++ b/backup/srcs/get_dollar_eval.c @@ -0,0 +1,56 @@ +#include "../incs/minishell.h" + +static int is_finish_cond(char c) +{ + if (c == '\"') + return (1); if (c == '\'') return (1); + if (c == ' ') + return (1); + return (0); +} + +static int get_word_len(char *part) +{ + int ret; + + ret = 0; + while (part[ret]) + { + if (is_finish_cond(part[ret])) + return (ret); + ++ret; + } + return (ret); +} + +char *get_dollar_eval(char *part, t_list *env_list, int *idx) +{ + char *ret; + int word_len; + int dollor_eval_len; + char *tmp_key; + + if (part[0] != '$') + return (NULL); + word_len = get_word_len(part); + //$ + if (word_len == 1) + { + *idx = *idx + word_len; + return (ft_strdup("$")); + } + //$$ + //$? + if ((ft_strncmp("$?", part, get_max_strlen("$?", part))) == 0) + { + *idx = *idx + word_len; + return (ft_itoa(g_ret)); + } + //$HOME + tmp_key = ft_substr(part, 1, word_len - 1); + ret = ft_strdup(get_eval(env_list, tmp_key)); + *idx = *idx + word_len; + free(tmp_key); + tmp_key = NULL; + return (ret); +} diff --git a/backup/srcs/get_elist.c b/backup/srcs/get_elist.c new file mode 100644 index 0000000..15219f5 --- /dev/null +++ b/backup/srcs/get_elist.c @@ -0,0 +1,21 @@ +#include "../incs/minishell.h" + +t_list *get_elist( + t_list *list_head, + char *tar) +{ + t_list *cur; + t_env *tmp_env; + int cmp_len; + + cur = list_head; + while (cur) + { + tmp_env = (t_env *)(cur->data); + cmp_len = get_max_strlen(tmp_env->key, tar); + if (!(ft_strncmp(tmp_env->key, tar, cmp_len))) + return (cur); + cur = cur->next; + } + return (NULL); +} diff --git a/backup/srcs/get_eval.c b/backup/srcs/get_eval.c new file mode 100644 index 0000000..4c19864 --- /dev/null +++ b/backup/srcs/get_eval.c @@ -0,0 +1,16 @@ +#include "../incs/minishell.h" + +char *get_eval( + t_list *list_head, + char *tar) +{ + char *ret; + t_list *tmp_list; + t_env *tmp_env; + + if (!(tmp_list = get_elist(list_head, tar))) + return (NULL); + if (!(tmp_env = (t_env *)(tmp_list->data))) + return (NULL); + return (tmp_env->val); +} diff --git a/backup/srcs/get_max_strlen.c b/backup/srcs/get_max_strlen.c new file mode 100644 index 0000000..4287cb4 --- /dev/null +++ b/backup/srcs/get_max_strlen.c @@ -0,0 +1,13 @@ +#include "../incs/minishell.h" + +int get_max_strlen(char *str1, char *str2) +{ + int str1_len; + int str2_len; + + str1_len = ft_strlen(str1); + str2_len = ft_strlen(str2); + if (str1_len < str2_len) + return (str2_len); + return (str1_len); +} diff --git a/backup/srcs/get_next_line.c b/backup/srcs/get_next_line.c new file mode 100644 index 0000000..0afb4a2 --- /dev/null +++ b/backup/srcs/get_next_line.c @@ -0,0 +1,68 @@ +#include "../incs/minishell.h" + +static char handle_enter( + int backup_len, + char buf, + int read_size) +{ + int ret; + + ret = 0; + if((backup_len == 0) && buf == '\n' && read_size == 1) + { + //display_prompt(); + ret = 1; + } + return (ret); +} + +static char *handle_fflush(char *backup) +{ + char *ret; + + free(backup); + backup = NULL; + ret = ft_strjoin("", ""); + g_fsh_buf = 0; + return (ret); +} + +static void handle_eof(t_info *info, char *backup) +{ + if (ft_strlen(backup) == 0) + { + free(backup); + g_ret = 0; + g_ret = exit_shell(info); + exit(0); + } + ft_putstr_fd(" \b\b", STDERR); +} + +int get_next_line(t_info *info, char **line) +{ + int read_size = 1; + char buf[2]; + char *backup; + + backup = ft_strjoin("", ""); + while (read_size >= 0 && buf[0] != '\n') + { + read_size = read(STDIN_FILENO, buf, 1); + buf[1] = '\0'; + if (g_fsh_buf) + backup = handle_fflush(backup); + if (read_size == 0) + handle_eof(info, backup); + else if (buf[0] != '\n' && read_size > 0) + { + char *tmp = backup; + backup = ft_strjoin(backup, buf); + free(tmp); + } + else if (handle_enter(ft_strlen(backup), buf[0], read_size)) + buf[0] = '\0'; + } + *line = backup; + return (read_size); +} diff --git a/backup/srcs/get_next_line_tc.c b/backup/srcs/get_next_line_tc.c new file mode 100644 index 0000000..27c746e --- /dev/null +++ b/backup/srcs/get_next_line_tc.c @@ -0,0 +1,118 @@ +#include "../incs/minishell.h" + +static char *get_str_by_inst_arr(long inst_arr[], int inst_arr_size) +{ + int idx; + char *ret; + char *tmp_ret; + int tmp_ret_idx; + + tmp_ret = (char *)malloc(sizeof(char) * (inst_arr_size + 1)); + tmp_ret[inst_arr_size] = '\0'; + tmp_ret_idx = 0; + idx = -1; + while (++idx < inst_arr_size) + { + if (inst_arr[idx] == KEY_BACKSPACE) + { + tmp_ret_idx = calc_max(tmp_ret_idx - 1, 0); + tmp_ret[tmp_ret_idx] = '\0'; + } + else if (is_key_arrow(inst_arr[idx])) + continue ; + else + tmp_ret[tmp_ret_idx++] = (char)inst_arr[idx]; + } + ret = ft_substr(tmp_ret, 0, tmp_ret_idx); + free(tmp_ret); + return (ret); +} + +static int join_history_line( + t_info *info, + long inst_arr[], + long key_arrow) +{ + int ret; + char *history_line; + + ft_cursor_clr_line_all(info->tc); + ft_cursor_mv_head(info->tc); + ft_memset(inst_arr, 0, BUFFER_SIZE); + if (key_arrow == KEY_UP_ARROW && info->history_ptr->next != NULL) + info->history_ptr = info->history_ptr->next; + if (key_arrow == KEY_DOWN_ARROW && info->history_ptr->prev != NULL) + info->history_ptr = info->history_ptr->prev; + history_line = (char *)(info->history_ptr->data); + write(1, info->prompt.data, info->prompt.size); + ft_putstr_fd(history_line, 1); + ret = -1; + while (++ret < ft_strlen(history_line)) + inst_arr[ret] = (long)history_line[ret]; + return (ret); +} + +static int handle_key_arrow( + t_info *info, + long arr[], + long c, + int cursor_right_limit) +{ + int ret; + char *tmp; + + if (info->history_ptr == NULL) + return (0); + ret = 0; + if (c == KEY_LEFT_ARROW) + ft_cursor_mv_left(info->tc.cursor.col, info->prompt.size); + else if (c == KEY_RIGHT_ARROW) + ft_cursor_mv_right(info->tc.cursor.col, cursor_right_limit); + else if (c == KEY_UP_ARROW || c == KEY_DOWN_ARROW) + ret = join_history_line(info, arr, c) - 1; + return (ret); +} + +static int set_inst_arr_in_loop( + t_info *info, + long *arr) +{ + long c; + int buf_len; + int idx; + + buf_len = 0; + idx = -1; + c = 0; + while ((read(STDIN_FILENO, &c, sizeof(c)) > 0) && (c != '\n')) + { + get_cursor_pos(&(info->tc.cursor.col), &(info->tc.cursor.row)); + if (c <= 4500000) + arr[++idx] = c; + if (ft_isprint(c) && ++buf_len) + ft_putchar_fd(c, 1); + else if (is_key_arrow(c)) + idx = handle_key_arrow(info, arr, c, info->prompt.size + buf_len); + else if (c == KEY_BACKSPACE && --buf_len) + ft_cursor_clr_line_end(info->tc, info->prompt.size); + c = 0; + } + return (idx + 1); +} + +char *get_next_line_tc(t_info *info) +{ + t_tc tc; + char *ret; + long inst_arr[BUFFER_SIZE]; + int inst_arr_size; + + tc = info->tc; + get_cursor_pos(&tc.cursor.col, &tc.cursor.row); + ft_cursor_mv_head(tc); + write(STDOUT_FILENO, info->prompt.data, info->prompt.size); + ft_memset(inst_arr, 0, BUFFER_SIZE); + inst_arr_size = set_inst_arr_in_loop(info, inst_arr); + ret = get_str_by_inst_arr(inst_arr, inst_arr_size); + return (ret); +} diff --git a/backup/srcs/handle_arg.c b/backup/srcs/handle_arg.c new file mode 100644 index 0000000..b75b886 --- /dev/null +++ b/backup/srcs/handle_arg.c @@ -0,0 +1,108 @@ +#include "../incs/minishell.h" + +static char *handle_nquote_part( + char **str_addr, + t_list *env_list) +{ + int idx; char *str; + char *prev_ret; + char *tmp; + char *ret; + + str = *str_addr; + idx = 0; + if (is_dquote(str[idx]) || is_squote(str[idx])) + return (NULL); + ret = ft_strdup(""); + prev_ret = ret; + while (!is_dquote(str[idx]) && !is_squote(str[idx]) && str[idx]) + { + if (str[idx] == '$') + tmp = get_dollar_eval(&str[idx], env_list, &idx); + else + tmp = cvt_char_to_str(str[idx++]); + ret = ft_strjoin(ret, tmp); + free(prev_ret); + prev_ret = ret; + free(tmp); + } + *str_addr = *str_addr + idx; + return (ret); +} + +static char *handle_squote_part(char **str_addr) +{ + int idx; + char *str; + char *ret; + + str = *str_addr; + idx = 0; + if (!is_squote(str[idx++])) + return (NULL); + while (!is_squote(str[idx])) + ++idx; + ret = ft_substr(str, 1, idx - 1); + *str_addr = *str_addr + idx; + return (ret); +} + +static char *handle_dquote_part( + char **str_addr, + t_list *env_list) +{ + int idx; + char *str; + char *prev_ret; + char *tmp; + char *ret; + + str = *str_addr; + idx = 0; + if (!is_dquote(str[idx++])) + return (NULL); + ret = ft_strdup(""); + prev_ret = ret; + while (!is_dquote(str[idx])) + { + if (str[idx] == '$') + tmp = get_dollar_eval(&str[idx], env_list, &idx); + else + tmp = cvt_char_to_str(str[idx++]); + ret = ft_strjoin(ret, tmp); + free(prev_ret); + prev_ret = ret; + free(tmp); + } + *str_addr = *str_addr + idx; + return (ret); +} + +char *handle_arg(char *arg, t_list *env_list) +{ + char *ret; + char *prev_ret; + char *tmp; + + ret = ft_strdup(""); + prev_ret = ret; + while (arg && *arg) + { + if (is_dquote(*arg)) + tmp = handle_dquote_part(&arg, env_list); + else if (is_squote(*arg)) + tmp = handle_squote_part(&arg); + else + { + tmp = handle_nquote_part(&arg, env_list); + if (*arg == '\0') + --arg; + } + ret = ft_strjoin(ret, tmp); + free(prev_ret); + prev_ret = ret; + free(tmp); + ++arg; + } + return (ret); +} diff --git a/backup/srcs/handle_bit.c b/backup/srcs/handle_bit.c new file mode 100644 index 0000000..b38e863 --- /dev/null +++ b/backup/srcs/handle_bit.c @@ -0,0 +1,19 @@ +#include "../incs/minishell.h" + +int turn_on_bit(int val, int n) +{ + return (val |= (1 << n)); +} + +int turn_off_bit(int val, int n) +{ + return (val &= ~(1 << n)); +} + +int check_bit(int val, int n) +{ + if ((val >> n) & ON) + return (ON); + return (OFF); +} + diff --git a/backup/srcs/handle_quote.c b/backup/srcs/handle_quote.c new file mode 100644 index 0000000..576fcbf --- /dev/null +++ b/backup/srcs/handle_quote.c @@ -0,0 +1,53 @@ +#include "../incs/minishell.h" + +static int handle_squote_in_dquote(int val) +{ + int ret; + + if (check_bit(val, BIT_DQUOTE)) + ret = turn_off_bit(val, BIT_SQUOTE); + else + ret = turn_on_bit(val, BIT_SQUOTE); + return (ret); +} + +static int handle_dquote_in_squote(int val) +{ + int ret; + + if (check_bit(val, BIT_SQUOTE)) + ret = turn_off_bit(val, BIT_DQUOTE); + else + ret = turn_on_bit(val, BIT_DQUOTE); + return (ret); +} + +int handle_quote( + char *str, + char **str_cpy, + char c) +{ + int ret; + int str_cpy_idx; + int tmp_flag; + + ret = 0; + str_cpy_idx = -1; + while (*str) + { + // when DQUOTE is opened, SQUOTE is useless as str starter + if (!check_bit(ret, BIT_SQUOTE) && *str == '\'') + ret = handle_squote_in_dquote(ret); + else if (!check_bit(ret, BIT_DQUOTE) && *str == '\"') + ret = handle_dquote_in_squote(ret); + else if (check_bit(ret, BIT_SQUOTE) && *str == '\'') + ret = turn_off_bit(ret, BIT_SQUOTE); + else if (check_bit(ret, BIT_DQUOTE) && *str == '\"') + ret = turn_off_bit(ret, BIT_DQUOTE); + // when SQUOTE or DQUOTE is on + if (((*str_cpy)[++str_cpy_idx] == c && ret)) + (*str_cpy)[str_cpy_idx] = '_'; + ++str; + } + return (ret); +} diff --git a/backup/srcs/handle_rest_redir.c b/backup/srcs/handle_rest_redir.c new file mode 100644 index 0000000..cd97cd3 --- /dev/null +++ b/backup/srcs/handle_rest_redir.c @@ -0,0 +1,3 @@ +#include "../incs/minishell.h" + + diff --git a/backup/srcs/handle_sig_init.c b/backup/srcs/handle_sig_init.c new file mode 100644 index 0000000..2c7a8ec --- /dev/null +++ b/backup/srcs/handle_sig_init.c @@ -0,0 +1,27 @@ +#include "../incs/minishell.h" + +static void sigint_handler(int signo) +{ + //remove signal, and change line + ft_putstr_fd("\b\b \n", STDERR); +// display_prompt(); + //change dollar_ret + g_ret = 130; + g_fsh_buf = 1; +} + +static void sigquit_handler(int signo) +{ + //remove signal(nothing to change on the screen) + ft_putstr_fd("\b\b \b\b", STDERR); + //do nothing + //no change dollar_ret +} + +void handle_sig_init(t_info *info) +{ + if (signal(SIGINT, (void *)sigint_handler) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-C", 2); + if (signal(SIGQUIT, (void *)sigquit_handler) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-\\", 2); +} diff --git a/backup/srcs/handle_sig_proc.c b/backup/srcs/handle_sig_proc.c new file mode 100644 index 0000000..8e0feac --- /dev/null +++ b/backup/srcs/handle_sig_proc.c @@ -0,0 +1,32 @@ +#include "../incs/minishell.h" + +static void sigint_handler_proc(int signo) +{ + //no need to remove signal + //stop process(minishell is still running) + //change dollar_ret; + g_ret = 130; + //display prompt + ft_putstr_fd("\n", STDERR); +} + +static void sigquit_handler_proc(int signo) +{ + //no need to remove signal + //put messege + ft_putstr_fd("Quit: 3", STDERR); + //stop process(minishell is still running) + //change dollar_ret + g_ret = 131; + //display prompt + ft_putstr_fd("\n", STDERR); +} + +//when signal pressed in processing. +void handle_sig_proc(int pid) +{ + if (signal(SIGINT, (void *)sigint_handler_proc) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-C", STDERR); + if (signal(SIGQUIT, (void *)sigquit_handler_proc) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-\\", STDERR); +} diff --git a/backup/srcs/inc_shlvl.c b/backup/srcs/inc_shlvl.c new file mode 100644 index 0000000..4a3f16a --- /dev/null +++ b/backup/srcs/inc_shlvl.c @@ -0,0 +1,18 @@ +#include "../incs/minishell.h" + +int inc_shlvl(t_list **env_head) +{ + t_list *tmp_elist; + t_env *tmp_env; + char *new_val; + + if (!(tmp_elist = get_elist(*env_head, "SHLVL"))) + return (1); + tmp_env = (t_env *)(tmp_elist->data); + new_val = ft_itoa(ft_atoi(tmp_env->val) + 1); + if (mod_eval(tmp_env, new_val)) + return (1); + free(new_val); + new_val = NULL; + return (0); +} diff --git a/backup/srcs/init_global.c b/backup/srcs/init_global.c new file mode 100644 index 0000000..2a154f2 --- /dev/null +++ b/backup/srcs/init_global.c @@ -0,0 +1,7 @@ +#include "../incs/minishell.h" + +void init_global() +{ + g_ret = 0; + g_fsh_buf = 0; +} diff --git a/backup/srcs/init_minishell.c b/backup/srcs/init_minishell.c new file mode 100644 index 0000000..dce78c1 --- /dev/null +++ b/backup/srcs/init_minishell.c @@ -0,0 +1,56 @@ +#include "../incs/minishell.h" + +static t_tc gen_tc() +{ + t_tc ret; + + tcgetattr(STDIN_FILENO, &(ret.term)); + ret.term.c_lflag &= ~ICANON; + ret.term.c_lflag &= ~ECHO; + ret.term.c_cc[VMIN] = 1; + ret.term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &(ret.term)); + + ret.cursor.row = 0; + ret.cursor.col = 0; + + tgetent(NULL, "xterm"); + ret.tc_str[TC_CM] = tgetstr("cm", NULL); + ret.tc_str[TC_DL] = tgetstr("dl", NULL); + ret.tc_str[TC_CE] = tgetstr("ce", NULL); + return (ret); +} + +static void init_info( + t_info *info) +{ + info->set_list = NULL; + info->set_str_list = NULL; + info->env_list = NULL; + info->exit = 0; + info->ret = 0; + info->history = NULL; + ft_lstadd_front(&(info->history), ft_lstnew(ft_strdup(""))); + ft_lstadd_front(&(info->history), ft_lstnew(ft_strdup(""))); + info->history_ptr = info->history; + info->tc = gen_tc(); +} + +//env_list has to be sort with printing. +int init_minishell + (t_info *info, + char **env) +{ + int ret; + + ret = 0; + + init_info(info); + if (ret == 0) + if (!(info->env_list = gen_elist(env))) + ret = 1; + if (ret == 0) + ret = inc_shlvl(&(info->env_list)); + //print_elist(info->env_list); + return (ret); +} diff --git a/backup/srcs/is_bracket.c b/backup/srcs/is_bracket.c new file mode 100644 index 0000000..ee4ee6e --- /dev/null +++ b/backup/srcs/is_bracket.c @@ -0,0 +1,10 @@ +#include "../incs/minishell.h" + +int is_bracket(char c) +{ + if (c == '>') + return (1); + if (c == '<') + return (1); + return (0); +} diff --git a/backup/srcs/is_dquote.c b/backup/srcs/is_dquote.c new file mode 100644 index 0000000..dca1ada --- /dev/null +++ b/backup/srcs/is_dquote.c @@ -0,0 +1,8 @@ +#include "../incs/minishell.h" + +int is_dquote(char c) +{ + if (c == '\"') + return (1); + return (0); +} diff --git a/backup/srcs/is_key_arrow.c b/backup/srcs/is_key_arrow.c new file mode 100644 index 0000000..3ace92c --- /dev/null +++ b/backup/srcs/is_key_arrow.c @@ -0,0 +1,14 @@ +#include "../incs/minishell.h" + +int is_key_arrow(long c) +{ + if (c == KEY_LEFT_ARROW) + return (1); + if (c == KEY_RIGHT_ARROW) + return (1); + if (c == KEY_UP_ARROW) + return (1); + if (c == KEY_DOWN_ARROW) + return (1); + return (0); +} diff --git a/backup/srcs/is_squote.c b/backup/srcs/is_squote.c new file mode 100644 index 0000000..7f594fd --- /dev/null +++ b/backup/srcs/is_squote.c @@ -0,0 +1,9 @@ +#include "../incs/minishell.h" + +int is_squote(char c) +{ + if (c == '\'') + return (1); + return (0); +} + diff --git a/backup/srcs/is_valid_fd.c b/backup/srcs/is_valid_fd.c new file mode 100644 index 0000000..fec316f --- /dev/null +++ b/backup/srcs/is_valid_fd.c @@ -0,0 +1,71 @@ +#include "../incs/minishell.h" + +static int is_valid_file_name(char *file_name, int type) +{ + char s[PATH_MAX]; + char *cwd; + char *path; + int ret; + + ret = 0; + if (file_name == NULL) + printf("zsh: parse error near `\n'"); + else if (type & TYPE_REIN) + { + cwd = getcwd(s, PATH_MAX); + path = get_bti_path(cwd, file_name); + if (!path) + printf("zsh: no such file or directory: %s\n", file_name); + else + { + free(path); + ret = 1; + } + } + else + ret = 1; + return (ret); +} + +static char *get_file_name( + char *str, + int *inc_idx) +{ + int beg; + int end; + char *ret; + + while (str[*inc_idx] && str[*inc_idx] == ' ') + ++(*inc_idx); + beg = *inc_idx; + while (str[*inc_idx] && str[*inc_idx] != ' ' + && !is_bracket(str[*inc_idx])) + ++(*inc_idx); + end = *inc_idx; + ret = ft_substr(str, beg, end - beg); + if (!ret) + return (NULL); + return (ret); +} + +int is_valid_fd( + t_set *set, + char *str, + int *idx, + int type) +{ + int ret; + char *file_name; + int inc_idx; + + ret = 0; + inc_idx = 0; + file_name = get_file_name(str, &inc_idx); + printf("is_valid_fd::file_name: %s\n", file_name); + if (!file_name) + return (0); + ret = is_valid_file_name(file_name, type); + free(file_name); + *idx += inc_idx; + return (ret); +} diff --git a/backup/srcs/main.c b/backup/srcs/main.c new file mode 100644 index 0000000..a3866d8 --- /dev/null +++ b/backup/srcs/main.c @@ -0,0 +1,149 @@ +#include "../incs/minishell.h" //memory free about set +void change_head(t_info *info) +{ + t_list *tmp; + int i; + + ft_lstclear(&(info->set_list), &free_set); + tmp = (info->set_str_list)->next; + ft_lstdelone(info->set_str_list, &free); + info->set_str_list = tmp; + +} + +int run(t_info *info) +{ + char *line; + t_list *next; + + while (info->exit == 0) + { + if (info->set_str_list == NULL) + { + handle_sig_init(info); + if ((get_next_line(info, &line)) == -1) + return -1; + if (!(info->set_str_list = gen_set_str_list(line))) + return -1; + free(line); + } + info->set_list = gen_set_list(info); + while (info->set_list) + { + int flag = 0; + run_cmd(info); + next = info->set_list->next; + ft_lstdelone(info->set_list, &free_set); + info->set_list = next; + } + //handle_rest_redir(); + change_head(info); + } + + return (0); +} + +int main(int argc, char *argv[], char *env[]) +{ + (void)argc; + (void)argv; + int err_num; + t_info info; + + err_num = 0; + init_minishell(&info, env); + init_global(); +// run(&info); + + #if 1 + + t_prompt prompt; + prompt.data = ">> ";//shell💃we🕺dance===> + prompt.size = ft_strlen(prompt.data); + info.prompt = prompt; + //test(&info, prompt); + + char *line; + char *tmp; + while (1) + { + if ((line = get_next_line_tc(&info)) == NULL) + { + printf("EMPTY LINE======================================\n"); + break ; + } + if (exact_strncmp(line,"") != 0) + { + append_history_list(&(info.history), line); + info.history_ptr = info.history; + } +#if 0 + print_list(info.history); + print_list(info.history_ptr); +#endif + } + + #endif +#if 0 + char *str = "echo abc >>text.txt args args2"; + char *str_cpy = "echo abc >>text.txt args args2"; + parse_redir(&info, str, str_cpy); + + + char *set_str = "echo abc \" >> << \"def>> edf"; + + gen_set(&info, set_str); +#endif + +#if 0 + int idx_inc = 0; + char *str = " test.txt >> test2.txt"; + int res = is_valid_fd(str, &idx_inc, TYPE_REIN); + printf("result of idx_inc: %d\n", idx_inc); + printf("rest string: %s\n", &str[idx_inc]); + printf("result of res: %d\n", res); +#endif + +#if 0 + char *str = " echo abc >> test.txt >> test2.txt defg hi"; + t_set *res = gen_set(&info, str); + //print_set(res); + +#endif + exit_shell(&info); + return (0); +} + +#if 0 // get_dollar_eval test + char *test = "$HOME abc"; + char *ret = get_dollar_eval(&test, info.env_list); + printf("part: %s\n", test); + printf("ret: %s\n", ret); +#endif + +#if 0 + char arg1[100] = "\"abc$HOME $HOMEdef\"\'abc\'efg"; + //char arg1[100] = "\"abc$HOME $HOMEdef\"\'ghi\'"; + //char arg1[100] = "\"abc def\"\'abc\'"; + char arg2[100] = "\"abc$HOMEdef\""; + char *args[2] = {arg1, arg2}; + sh_bti_echo(args, info.env_list); +#endif + +#if 0 + char arg1[100] = "\"-\""; + char arg2[100] = "\"abc$HOME $HOMEdef\"\'ghi\'"; + char arg3[100] = "\"abc def\"\'abc\'"; + char *args[2] = {NULL, NULL}; + sh_bti_cd(args, info.env_list); +#endif + +#if 0 + char *set_ex = "test=abcd====\"\"==="; + printf("result: %d\n", export_env(&info.env_list, set_ex)); + print_list(info.env_list); + + if ((err_num = run(&info))) + return (-1); + printf("err_num: %d\n", err_num); +#endif diff --git a/backup/srcs/mod_eval.c b/backup/srcs/mod_eval.c new file mode 100644 index 0000000..62a9349 --- /dev/null +++ b/backup/srcs/mod_eval.c @@ -0,0 +1,10 @@ +#include "../incs/minishell.h" + +int mod_eval(t_env *env, char *new_val) +{ + free(env->val); + env->val = NULL; + if (!(env->val = ft_strdup(new_val))) + return (1); + return (0); +} diff --git a/backup/srcs/open_valid_fd.c b/backup/srcs/open_valid_fd.c new file mode 100644 index 0000000..14404d6 --- /dev/null +++ b/backup/srcs/open_valid_fd.c @@ -0,0 +1,48 @@ +#include "../incs/minishell.h" + +static char *get_file_name( + char *str, + int *inc_idx) +{ + int beg; + int end; + char *ret; + + while (str[*inc_idx] && str[*inc_idx] == ' ') + ++(*inc_idx); + beg = *inc_idx; + while (str[*inc_idx] && str[*inc_idx] != ' ' + && !is_bracket(str[*inc_idx])) + ++(*inc_idx); + end = *inc_idx; + ret = ft_substr(str, beg, end - beg); + if (!ret) + return (NULL); + return (ret); +} + +int open_valid_fd(t_set *set, char *set_str, int *idx, int type) +{ + int fd; + char *file_name; + int inc_idx; + + inc_idx = 0; + file_name = get_file_name(set_str, &inc_idx); + printf("open_valid_fd::file_name: %s\n", file_name); + if (type & TYPE_REIN) + fd = open(file_name, O_RDONLY); + if (type & TYPE_REOUT) + fd = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0755); + if (type & TYPE_REOUT_D) + fd = open(file_name, O_CREAT | O_APPEND | O_WRONLY, 0755); + if (fd < 0) + return (0); + if (type & TYPE_REIN) + set->fd_in[set->fd_in_idx++] = fd; + if (type & (TYPE_REOUT | TYPE_REOUT_D)) + set->fd_out[set->fd_out_idx++] = fd; + free(file_name); + *idx += inc_idx; + return (1); +} diff --git a/backup/srcs/pk_split.c b/backup/srcs/pk_split.c new file mode 100644 index 0000000..d3b7326 --- /dev/null +++ b/backup/srcs/pk_split.c @@ -0,0 +1,72 @@ +#include "../incs/minishell.h" + +static size_t get_size(const char *s, char c) +{ + size_t ret; + + ret = 0; + while (*s) + { + while ((*s != '\0') && (*s == c)) + ++s; + if ((*s != '\0') && (*s != c)) + ++ret; + while ((*s != '\0') && (*s != c)) + ++s; + } + return (ret); +} + +static int get_next_idx( + const char *s, + int begin, + size_t *len_word, + char c) +{ + int idx; + int ret; + + ret = begin; + while ((s[ret] != '\0') && (s[ret] == c)) + ret++; + *len_word = 0; + idx = ret; + while ((s[idx] != '\0') && (s[idx] != c)) + { + ++(*len_word); + ++idx; + } + return (ret); +} + +char **pk_split( + const char *s, + const char *s_cpy, + char c, + int limit) +{ + char **ret; + int begin; + size_t len_word; + size_t size; + size_t k; + + if (s == NULL) + return (0); + size = get_size(s_cpy, c); + if (!(ret = (char **)malloc(sizeof(char *) * (size + 1)))) + return (NULL); + ret[size] = 0; + k = 0; + begin = 0; + while (k < size && limit--) + { + begin = get_next_idx(s_cpy, begin, &len_word, c); + if (!(ret[k] = (char *)malloc(sizeof(char) * (len_word + 1)))) + return (free_darr(ret, k)); + ft_strlcpy(ret[k], &s[begin], len_word + 1); + begin = begin + len_word; + ++k; + } + return (ret); +} diff --git a/backup/srcs/print_darr.c b/backup/srcs/print_darr.c new file mode 100644 index 0000000..3aeee14 --- /dev/null +++ b/backup/srcs/print_darr.c @@ -0,0 +1,10 @@ +#include "../incs/minishell.h" + +void print_darr(char **str) +{ + int idx; + + idx = -1; + while (str[++idx]) + printf("str[%d]: %s\n", idx, str[idx]); +} diff --git a/backup/srcs/print_elist.c b/backup/srcs/print_elist.c new file mode 100644 index 0000000..bba0d54 --- /dev/null +++ b/backup/srcs/print_elist.c @@ -0,0 +1,15 @@ +#include "../incs/minishell.h" + +void print_elist(t_list *head) +{ + t_list *cur; + t_env *tmp_env; + + cur = head; + while (cur) + { + tmp_env = (t_env *)(cur->data); + printf("%s=%s\n", tmp_env->key, tmp_env->val); + cur = cur->next; + } +} diff --git a/backup/srcs/print_list.c b/backup/srcs/print_list.c new file mode 100644 index 0000000..1bf2424 --- /dev/null +++ b/backup/srcs/print_list.c @@ -0,0 +1,13 @@ +#include "../incs/minishell.h" + +void print_list(t_list *head) +{ + t_list *now; + + now = head; + while (now) + { + printf("%s\n", (char *)now->data); + now = now->next; + } +} diff --git a/backup/srcs/print_set.c b/backup/srcs/print_set.c new file mode 100644 index 0000000..18e5fa4 --- /dev/null +++ b/backup/srcs/print_set.c @@ -0,0 +1,14 @@ +#include "../incs/minishell.h" + +void print_set(t_set *set) +{ + int idx; + + printf("cmd: %s\n", set->cmd); + idx = -1; + while (set->args[++idx]) + { + printf("arg[%d]: %s\n", idx, set->args[idx]); + } + printf("type: %d\n", set->type); +} diff --git a/backup/srcs/print_slist.c b/backup/srcs/print_slist.c new file mode 100644 index 0000000..c0eae44 --- /dev/null +++ b/backup/srcs/print_slist.c @@ -0,0 +1,14 @@ +#include "../incs/minishell.h" + +void print_slist(t_list *head) { + t_list *cur; + t_set *tmp_set; + + cur = head; + while (cur) + { + tmp_set = (t_set *)(cur->data); + print_set(tmp_set); + cur = cur->next; + } +} diff --git a/backup/srcs/redo_sh_bti.c b/backup/srcs/redo_sh_bti.c new file mode 100644 index 0000000..79e7059 --- /dev/null +++ b/backup/srcs/redo_sh_bti.c @@ -0,0 +1,24 @@ +#include "../incs/minishell.h" + +int redo_sh_bti(t_set *set, t_info *info) +{ + char *cmd; + char **args; + int ret; + int cmp_len; + + ret = 1; + cmd = set->cmd; + args = set->args; + if (!exact_strncmp(cmd, "export")) + ret = sh_bti_export(args, &(info->env_list)); + else if (!exact_strncmp(cmd, "unset")) + ret = sh_bti_unset(args, &(info->env_list)); + else if (!exact_strncmp(cmd, "cd")) + ret = sh_bti_cd(args, info->env_list); + else if (!exact_strncmp(cmd, "exit")) + ret = sh_bti_exit(info); + else + ret = 0; + return (ret); +} diff --git a/backup/srcs/rewrite_fd.c b/backup/srcs/rewrite_fd.c new file mode 100644 index 0000000..e045e2a --- /dev/null +++ b/backup/srcs/rewrite_fd.c @@ -0,0 +1,43 @@ +#include "../incs/minishell.h" + +static int get_next_line_fd(char **line, int fd) +{ + int read_size = 1; + char buf[2]; + char *backup; + + backup = ft_strjoin("", ""); + while (read_size > 0 && buf[0] != '\n') { + read_size = read(fd, buf, 1); + buf[1] = '\0'; + if (buf[0] != '\n' && read_size > 0) { + char *tmp = backup; + backup = ft_strjoin(backup, buf); + free(tmp); + } + } + *line = backup; + return (read_size); +} + +int rewrite_fd(t_info *info, char *file_name, int fd2) +{ + int fd; + int ret; + char *line; + + if ((fd = open(file_name, O_RDONLY)) < 0) + { + printf("file_name not opened\n"); + return (1); + } + while (get_next_line_fd(&line, fd) > 0) + { + ft_putstr_fd(line, fd2); + write(fd2, "\n", 1); + free(line); + } + free(line); + + return (ret); +} diff --git a/backup/srcs/run_bti.c b/backup/srcs/run_bti.c new file mode 100644 index 0000000..3e8303d --- /dev/null +++ b/backup/srcs/run_bti.c @@ -0,0 +1,30 @@ +#include "../incs/minishell.h" + +int run_bti(t_set *set, t_list *env_list) +{ + char *func_path; + char **cand_arr; + int idx; + char *bti_path; + char **env_arr; + + if (!(func_path = get_eval(env_list, "PATH"))) + printf("ERROR: func_path is NULL\n"); + cand_arr = pk_split(func_path, func_path, ':', INF); + idx = -1; + bti_path = NULL; + while (cand_arr[++idx]) + { + if ((bti_path = get_bti_path(cand_arr[idx], set->cmd))) + break ; + } + free_darr(cand_arr, INF); + if (!bti_path) + return (1); + env_arr = set_list_to_darr(env_list); + if (execve(bti_path, set->set, env_arr) < 0) + return (1); + free(bti_path); + free_darr(env_arr, INF); + return (0); +} diff --git a/backup/srcs/run_cmd.c b/backup/srcs/run_cmd.c new file mode 100644 index 0000000..c2eff60 --- /dev/null +++ b/backup/srcs/run_cmd.c @@ -0,0 +1,107 @@ +#include "../incs/minishell.h" + +void show_cmd_error(char *cmd) +{ + show_error("error: cannot execute "); + show_error(cmd); + show_error("\n"); +} + +static void close_redir_fd(t_set *curr) +{ + if (curr->type & TYPE_REOUT_D) + close(curr->fd_out[0]); + if (curr->type & TYPE_REOUT) + close(curr->fd_out[0]); + if (curr->type & TYPE_REIN) + close(curr->fd_in[0]); +} + +static void open_redir_fd(t_set *curr) +{ + if (curr->type & TYPE_REOUT) + dup2(curr->fd_out[0], STDOUT_FILENO); + if (curr->type & TYPE_REOUT_D) + dup2(curr->fd_out[0], STDOUT_FILENO); + if (curr->type & TYPE_REIN) + dup2(curr->fd_in[0], STDIN_FILENO); +} + +static int close_fds(pid_t pid, t_set *curr, t_set *prev, int pipe_open) +{ + int status; + int ret; + + waitpid(pid, &status, 0); + if (pipe_open) + { + close(curr->fds[1]); + if (curr->type & TYPE_BREAK) + close(curr->fds[0]); + } + if (prev && (prev->type & TYPE_PIPE)) + close(prev->fds[0]); + close_redir_fd(curr); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + return (ret); +} + +static int run_cmd_part( + t_set *curr, + t_set *prev, + t_info *info, + int pipe_open) +{ + pid_t pid; + int ret; + + pid = fork(); + handle_sig_proc(pid); + if (pid < 0) + return (exit_fatal()); + if (pid == 0) + { + if ((curr->type & TYPE_PIPE) + && dup2(curr->fds[1], STDOUT_FILENO) < 0) + return (exit_fatal()); + if (prev && (prev->type & TYPE_PIPE) + && dup2(prev->fds[0], STDIN_FILENO) < 0) + return (exit_fatal()); + open_redir_fd(curr); + if ((ret = categorize_cmd(curr, info)) != 0) + show_cmd_error(curr->cmd); + exit(ret); + } + else + ret = close_fds(pid, curr, prev, pipe_open); + return (ret); +} + +int run_cmd(t_info *info) +{ + int ret; + int status; + int pipe_open; + t_set *curr; + t_set *prev; + + ret = EXIT_FAILURE; + pipe_open = 0; + curr = (t_set *)(info->set_list->data); + if (!(info->set_list->prev)) + prev = NULL; + else + prev = (t_set *)(info->set_list->prev->data); + if ((curr->type & TYPE_PIPE) \ + || (prev && (prev->type & TYPE_PIPE))) + { + pipe_open = 1; + if (pipe(curr->fds)) + return (exit_fatal()); + } + ret = run_cmd_part(curr, prev, info, pipe_open); + if (ret != EXIT_FAILURE && (curr->type & TYPE_BREAK)) + ret = redo_sh_bti(curr, info); + return (ret); +} diff --git a/backup/srcs/select_sh_bti.c b/backup/srcs/select_sh_bti.c new file mode 100644 index 0000000..a2e7bd4 --- /dev/null +++ b/backup/srcs/select_sh_bti.c @@ -0,0 +1,29 @@ +#include "../incs/minishell.h" + +int select_sh_bti(t_set *set, t_info *info) +{ + char *cmd; + char **args; + int ret; + + ret = 1; + cmd = set->cmd; + args = set->args; + if (!exact_strncmp(cmd, "export")) + ret = sh_bti_export(args, &(info->env_list)); + if (!exact_strncmp(cmd, "unset")) + ret = sh_bti_unset(args, &(info->env_list)); + if (!exact_strncmp(cmd, "env")) + ret = sh_bti_env(info->env_list); + if (!exact_strncmp(cmd, "cd")) + ret = sh_bti_cd(args, info->env_list); + if (!exact_strncmp(cmd, "pwd")) + ret = sh_bti_pwd(args); + if (!exact_strncmp(cmd, "echo")) + ret = sh_bti_echo(args, info->env_list); + if (!exact_strncmp(cmd, "unset")) + ret = sh_bti_echo(args, info->env_list); + if (!exact_strncmp(cmd, "exit")) + ret = sh_bti_exit(info); + return (ret); +} diff --git a/backup/srcs/set_bracket_type.c b/backup/srcs/set_bracket_type.c new file mode 100644 index 0000000..07fc81e --- /dev/null +++ b/backup/srcs/set_bracket_type.c @@ -0,0 +1,34 @@ +#include "../incs/minishell.h" + +static int is_bracket_error( + char *str, + int *idx, + char bracket, + int *cnt) +{ + while (str[*idx] && str[*idx] == bracket && (*cnt)--) + ++(*idx); + if (str[*idx] == bracket) + return (TYPE_ERROR); + return (0); +} + +int set_bracket_type(char *str, int *idx) +{ + int ret; + int cnt; + + cnt = 2; + if (is_bracket_error(str, idx, '>', &cnt)) + return (TYPE_ERROR); + if (cnt == 1) + return (TYPE_REOUT); + if (cnt == 0) + return (TYPE_REOUT_D); + cnt = 1; + if (is_bracket_error(str, idx, '<', &cnt)) + return (TYPE_ERROR); + if (cnt == 0) + return (TYPE_REIN); + return (0); +} diff --git a/backup/srcs/set_darr_to_list.c b/backup/srcs/set_darr_to_list.c new file mode 100644 index 0000000..877a3a3 --- /dev/null +++ b/backup/srcs/set_darr_to_list.c @@ -0,0 +1,24 @@ +#include "../incs/minishell.h" + +int set_darr_to_list( + t_list **list_head, + char **str) +{ + int ret; + int len; + char *tmp_tokens; + + ret = 0; + len = 0; + while (ret == 0 && str[len]) + len++; + while (ret == 0 && --len >= 0) + { + if ((tmp_tokens = ft_strdup(str[len])) == NULL) + ret = 1; + ft_lstadd_front(list_head, ft_lstnew(tmp_tokens)); + } + return (ret); + +} + diff --git a/backup/srcs/set_fd.c b/backup/srcs/set_fd.c new file mode 100644 index 0000000..ff6cc51 --- /dev/null +++ b/backup/srcs/set_fd.c @@ -0,0 +1,88 @@ +#include "../incs/minishell.h" + +static char *show_type_error(char *str) +{ + char c[2]; + + show_error("zsh: parse error near"); + show_error(" `"); + c[0] = str[0]; + c[1] = '\0'; + show_error(c); + if (str[1] && is_bracket(str[1])) + { c[0] = str[1]; + c[1] = '\0'; + show_error(c); + } + show_error("'\n"); + return (NULL); +} + +static int join_char_to_args(char **str, char c, int *idx) +{ + int ret; + char *tmp; + char *c_str; + + c_str = cvt_char_to_str(c); + tmp = ft_strjoin(*str, c_str); + if (!tmp) + return (0); + free(c_str); + free(*str); + *str = tmp; + *idx += 1; + return (1); +} + +static char *set_set_type( + t_set *set, + char *str, + char *str_cpy, + int (*fp)()) +{ + char *ret; + int idx; + int tmp_type; + int inc_flag; + + idx = 0; + inc_flag = 1; + ret = ft_strdup(""); + while (inc_flag && str_cpy[idx]) + { + inc_flag = 1; + tmp_type = set_bracket_type(&str_cpy[0], &idx); + set->type |= tmp_type; + if (set->type & TYPE_ERROR) + return (show_type_error(&str_cpy[idx])); + if (tmp_type & (TYPE_REIN | TYPE_REOUT | TYPE_REOUT_D)) + inc_flag = fp(set, &str_cpy[idx], &idx, tmp_type); + else + inc_flag = join_char_to_args(&ret, str[idx], &idx); + } + if (inc_flag == 0) + return (NULL); + return (ret); +} + +char *set_fd(t_set *set, char *set_str) +{ + char *ret; + char *set_str_cpy; + + set_str_cpy = ft_strdup(set_str); + handle_quote(set_str, &set_str_cpy, ' '); + handle_quote(set_str_cpy, &set_str_cpy, '<'); + handle_quote(set_str_cpy, &set_str_cpy, '>'); + set->type = 0; + set->fd_in_idx = 0; + set->fd_out_idx = 0; + ret = set_set_type(set, set_str, set_str_cpy, &is_valid_fd); + if (ret != NULL) + { + set_set_type(set, set_str, set_str_cpy, &open_valid_fd); + } + free(set_str_cpy); + return (ret); +} diff --git a/backup/srcs/set_list_to_darr.c b/backup/srcs/set_list_to_darr.c new file mode 100644 index 0000000..ecb137f --- /dev/null +++ b/backup/srcs/set_list_to_darr.c @@ -0,0 +1,32 @@ +#include "../incs/minishell.h" + +char **set_list_to_darr(t_list *env_list) +{ + char **ret; + int idx; + int list_size; + char *tmp_str; + t_env *tmp_env; + + list_size = ft_lstsize(env_list); + if (!(ret = (char **)malloc(sizeof(char *) * (list_size + 1)))) + return (0); + ret[list_size] = NULL; + idx = 0; + while (env_list) + { + tmp_env = (t_env *)(env_list->data); + ret[idx] = ft_strjoin(tmp_env->key, "="); + tmp_str = ret[idx]; + ret[idx] = ft_strjoin(ret[idx], tmp_env->val); + free(tmp_str); + if (!ret[idx]) + { + printf("ERROR: strdup error in convert_list_to_arr\n"); + return (free_darr(ret, idx)); + } + ++idx; + env_list = env_list->next; + } + return (ret); +} diff --git a/backup/srcs/sh_bti_cd.c b/backup/srcs/sh_bti_cd.c new file mode 100644 index 0000000..47ea4ad --- /dev/null +++ b/backup/srcs/sh_bti_cd.c @@ -0,0 +1,69 @@ +#include "../incs/minishell.h" +# define PATH_MAX 1024 + +static char *handle_shortcut(char *arg, t_list *env_list) +{ + char *ret; + + ret = arg; + if (!ft_strlen(ret) || !exact_strncmp(ret, "~")) + { + free(ret); + ret = ft_strdup(get_eval(env_list, "HOME")); + } + else if (!exact_strncmp(ret, "-")) + { + free(ret); + ret = ft_strdup(get_eval(env_list, "OLDPWD")); + } + return (ret); +} + +static char *get_path(char *arg, t_list *env_list) +{ + char *ret; + char *tmp_path; + + tmp_path = handle_arg(arg, env_list); + ret = handle_shortcut(tmp_path, env_list); + return (ret); +} + +static int renew_pwd(t_list *env_list) +{ + int ret; + t_list *tmp_list; + char cwd[PATH_MAX]; + + ret = 0; + tmp_list = get_elist(env_list, "OLDPWD"); + ret = mod_eval((t_env *)tmp_list->data, get_eval(env_list, "PWD")); + tmp_list = get_elist(env_list, "PWD"); + ret = mod_eval((t_env *)tmp_list->data, getcwd(cwd, PATH_MAX)); + return (ret); +} + +int sh_bti_cd(char **args, t_list *env_list) +{ + char *path; + int ret; + + ret = 0; + print_darr(args); + if (args[0] && args[1]) + { + printf("cd: string not in pwd: %s\n", args[0]); + return (1); + } + if (!(path = get_path(args[0], env_list))) + return (1); + if (chdir(path) == -1) + { + printf("cd: no such file or directory: %s\n", path); + ret = 1; + } + free(path); + if (!ret) + ret = renew_pwd(env_list); + return (ret); +} diff --git a/backup/srcs/sh_bti_echo.c b/backup/srcs/sh_bti_echo.c new file mode 100644 index 0000000..c7cb145 --- /dev/null +++ b/backup/srcs/sh_bti_echo.c @@ -0,0 +1,41 @@ +#include "../incs/minishell.h" + +static int is_n_flag(char *str) +{ + int ret; + int idx; + + ret = 0; + idx = 0; + if (str[idx] == '-') + ret = 1; + while (ret == 1 && str[++idx]) + { + if (str[idx] != 'n') + ret = 0; + } + return (ret); +} + +int sh_bti_echo(char **args, t_list *env_list) +{ + int idx; + int n_flag; + + n_flag = 0; + idx = -1; + while (args[++idx] && is_n_flag(args[idx])) + n_flag = 1; + idx--; + while (args[++idx]) + { + char *tmp = handle_arg(args[idx], env_list); + write(STDOUT_FILENO, tmp, ft_strlen(tmp)); + if (args[idx + 1] != 0) + write(1, " ", 1); + free(tmp); + } + if (!n_flag) + write(1, "\n", 1); + return (0); +} diff --git a/backup/srcs/sh_bti_env.c b/backup/srcs/sh_bti_env.c new file mode 100644 index 0000000..3bd8297 --- /dev/null +++ b/backup/srcs/sh_bti_env.c @@ -0,0 +1,7 @@ +#include "../incs/minishell.h" + +int sh_bti_env(t_list *env_list) +{ + print_elist(env_list); + return (0); +} diff --git a/backup/srcs/sh_bti_exit.c b/backup/srcs/sh_bti_exit.c new file mode 100644 index 0000000..6201370 --- /dev/null +++ b/backup/srcs/sh_bti_exit.c @@ -0,0 +1,10 @@ +#include "../incs/minishell.h" + +int sh_bti_exit(t_info *info) +{ + g_ret = 0; + //if child process -> exit child process only + g_ret = exit_shell(info); + exit(0); + return (g_ret); +} diff --git a/backup/srcs/sh_bti_export.c b/backup/srcs/sh_bti_export.c new file mode 100644 index 0000000..097e1f0 --- /dev/null +++ b/backup/srcs/sh_bti_export.c @@ -0,0 +1,119 @@ +#include "../incs/minishell.h" + +#define ERR_EXPORT_EQUAL 1 +#define ERR_EXPORT_EMPTY_KEY 4 +#define CASE_EXPORT_EMPTY_VAL 2 +#define CASE_EXPORT_NUMBER_KEY 3 + +int g_equal_in_key; + +static int copy_str_key_to_val(t_env *env) +{ + int idx; + char *tmp_key; + char *new_val; + + idx = -1; + while (env->key[++idx]) + { + if (env->key[idx] == '=') + break ; + } + if (idx == ft_strlen(env->key)) + return (0); + if (!(env->val = ft_strjoin(&(env->key[idx + 1]), env->val))) + return (1); + //free(&(env->key[idx])); + tmp_key = env->key; + env->key = ft_substr(env->key, 0, idx); + free(tmp_key); + return (0); +} + +static int sh_bti_export_err_case(t_env *env) +{ + if ((env->key)[0] == '\0') + return (ERR_EXPORT_EMPTY_KEY); + if (ft_isdigit((env->key)[0])) + return (CASE_EXPORT_NUMBER_KEY); + if (env->val[0] == '=') + return (ERR_EXPORT_EQUAL); + if (!g_equal_in_key && (env->val == NULL || env->val[0] == '\0')) + return (CASE_EXPORT_EMPTY_VAL); + return (0); +} + +static int handle_err_case(int err_case, t_env *env) +{ + if (err_case == CASE_EXPORT_NUMBER_KEY) + { + printf("export: not an identifier: %s\n", env->key); + return (1); + } + if (err_case == ERR_EXPORT_EQUAL) + { + printf("zsh: %s not found\n", &(env->val[1])); + return (1); + } + if (err_case == ERR_EXPORT_EMPTY_KEY) + { + printf("export: not an identifier: %s\n", env->val); + return (1); + } + if (err_case == CASE_EXPORT_EMPTY_VAL) + { + free_env(env); + return (0); + } + return (0); +} + +static int run_export(t_list *env_list, char *arg) +{ + t_list *tmp_elist; + int ret; + int err_case; + t_env *tmp_env; + char *tmp_val; + + if (!(tmp_env = gen_env(arg))) + return (1); + printf("tmp_env->key: %s, tmp_env->val: %s\n", tmp_env->key, tmp_env->val); + if (del_quote(&(tmp_env->key))) + return (1); + err_case = sh_bti_export_err_case(tmp_env); + tmp_val = tmp_env->val; + tmp_env->val = handle_arg(tmp_env->val, env_list); + free(tmp_val); + printf("after handle_arg, val: %s\n", tmp_env->val); + if (copy_str_key_to_val(tmp_env)) // if key has equal sign, mv after = to value + return (1); + printf("tmp_env->key: %s, tmp_env->val: %s\n", tmp_env->key, tmp_env->val); + if (err_case) + return (handle_err_case(err_case, tmp_env)); + tmp_elist = get_elist(env_list, tmp_env->key); + if (tmp_elist) + mod_eval((t_env *)(tmp_elist->data), tmp_env->val); + else + add_elist(&env_list, tmp_env->key, tmp_env->val); + return (ret); +} + +int sh_bti_export(char **args, t_list **env_list) +{ + int idx; + + if (!args) + return (1); + idx = -1; + while (args[++idx]) + { + g_equal_in_key = 0; + if (ft_strchr(args[idx], '=')) + g_equal_in_key = 1; + run_export(*env_list, args[idx]); + } + if (idx == 0) + print_elist(*env_list); + return (0); +} diff --git a/backup/srcs/sh_bti_pwd.c b/backup/srcs/sh_bti_pwd.c new file mode 100644 index 0000000..e315951 --- /dev/null +++ b/backup/srcs/sh_bti_pwd.c @@ -0,0 +1,14 @@ +#include "../incs/minishell.h" +# define PATH_MAX 1024 +int sh_bti_pwd(char **args) +{ + char s[PATH_MAX]; + + if (*args) + { + printf("%s\n", "pwd: too many arguments"); + return (1); + } + printf("%s\n", getcwd(s, PATH_MAX)); + return 0; +} diff --git a/backup/srcs/sh_bti_unset.c b/backup/srcs/sh_bti_unset.c new file mode 100644 index 0000000..b09fe57 --- /dev/null +++ b/backup/srcs/sh_bti_unset.c @@ -0,0 +1,54 @@ +#include "../incs/minishell.h" + +static int cmp_key(t_list *env_list, char *key2) +{ + t_env *tmp_env; + char *key; + + tmp_env = (t_env *)(env_list->data); + key = tmp_env->key; + return (exact_strncmp(key, key2)); +} + +static void del_elist_if( + t_list **env_list, + char *key) +{ + t_list *prev; + t_list *next; + t_list *tmp; + + while (*env_list && !cmp_key(*env_list, key)) + { + tmp = *env_list; + *env_list = (*env_list)->next; + free_elist(tmp); + } + prev = *env_list; + while (prev && prev->next) + { + next = prev->next; + if (!cmp_key(next, key)) + { + tmp = next; + prev->next = next->next; + free_elist(tmp); + } + else + prev = next; + } +} + +int sh_bti_unset( + char **args, + t_list **env_list) +{ + int idx; + + if (!args) + return (1); + idx = -1; + while (args[++idx]) + del_elist_if(env_list, args[idx]); + return (0); +} diff --git a/backup/srcs/show_error.c b/backup/srcs/show_error.c new file mode 100644 index 0000000..c9017ad --- /dev/null +++ b/backup/srcs/show_error.c @@ -0,0 +1,8 @@ +#include "../incs/minishell.h" + +int show_error(char const *str) +{ + if (str) + write(STDERR_FILENO, str, ft_strlen(str)); + return (EXIT_FAILURE); +} diff --git a/backup/termcap_test/a.out b/backup/termcap_test/a.out new file mode 100755 index 0000000..23a3606 Binary files /dev/null and b/backup/termcap_test/a.out differ diff --git a/backup/termcap_test/cursor.c b/backup/termcap_test/cursor.c new file mode 100644 index 0000000..fd6fdf0 --- /dev/null +++ b/backup/termcap_test/cursor.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +#include + +#define LEFT_ARROW 4479771 +#define RIGHT_ARROW 4414235 + +int ft_putchar_tc(int tc) +{ + write(STDOUT_FILENO, &tc, 1); + return (0); +} + +int ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} + +int ft_strlen(char *str) +{ + int ret; + + ret = 0; + while (str[ret]) + ++ret; + return (ret); +} + +void get_cursor_pos(int *row, int *col) +{ + int cnt; + int idx; + char buf[255]; + int read_size; + + write(STDIN_FILENO, "\033[6n", ft_strlen("\033[6n")); + read_size = read(STDIN_FILENO, buf, 255); + buf[read_size] = '\0'; + idx = -1; + while (!ft_isdigit(buf[idx])) + ++idx; + *row = 0; + while (ft_isdigit(buf[idx])) + *row = *row * 10 + buf[idx++] - '0'; + ++idx; + *col = 0; + while (ft_isdigit(buf[idx])) + *col = *col * 10 + buf[idx++] - '0'; +} + +void move_cursor_left() +{ + write(STDIN_FILENO, "\033[D", 4); +} + +void move_cursor_right() +{ + write(STDIN_FILENO, "\033[C", 4); +} + +int main() +{ + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif +#if 0 + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#endif +#if 1 + int col; + int row; + char *prompt; + + long c; + prompt = ">> "; + while (1) + { + write(1, prompt, ft_strlen(prompt)); + c = 0; + while (read(0, &c, sizeof(c)) > 0) + { + //printf("keycode: %ld\n", c); + if (c == '\n') + { + printf("DEBUG=========================\n"); + break ; + } + get_cursor_pos(&row, &col); + if (col > 3 && c == LEFT_ARROW) + write(STDIN_FILENO, "\033[D", ft_strlen("\033[D")); + else if (c == RIGHT_ARROW) + write(STDIN_FILENO, "\033[C", ft_strlen("\033[C")); + else + write(1, &c, 1); + } + } +#endif +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/backup/termcap_test/ft_putchar_tc.c b/backup/termcap_test/ft_putchar_tc.c new file mode 100644 index 0000000..22164b7 --- /dev/null +++ b/backup/termcap_test/ft_putchar_tc.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + char c; + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/backup/termcap_test/test.c b/backup/termcap_test/test.c new file mode 100644 index 0000000..03e93f0 --- /dev/null +++ b/backup/termcap_test/test.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +struct termios old_settings; + +int main() +{ + struct termios new_settings; + + int vdisable; + char buf[255]; +#if 0 + if ((vdisable = fpathconf(0, _PC_VDISABLE)) < 0) { + perror("error "); + exit(0); + } + printf("vdisable: %d\n", vdisable); +#endif + + tcgetattr(0, &new_settings); + old_settings = new_settings; + new_settings.c_cc[VEOF] = 255; + tcsetattr(0, TCSAFLUSH, &new_settings); + memset(buf, 0x00, 255); + read(0, buf, 255); + printf("%s\n", buf); + tcsetattr(0, TCSAFLUSH, &old_settings); + exit(0); + return (0); +} diff --git a/backup/termcap_test/testtest.c b/backup/termcap_test/testtest.c new file mode 100644 index 0000000..ba5ea89 --- /dev/null +++ b/backup/termcap_test/testtest.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +# define BACKSPACE 127 +# define LEFT_ARROW 4479771 +# define RIGHT_ARROW 4414235 + +int nbr_length(int n) +{ + int i = 0; + + if (n <= 0) + i++; + while (n != 0) + { + n /= 10; + i++; + } + return (i); +} + +void get_cursor_position(int *col, int *rows) +{ + int a = 0; + int i = 1; + char buf[255]; + int ret; + int temp; + + write(0, "\033[6n", 4); //report cursor location + ret = read(0, buf, 254); + buf[ret] = '\0'; + while (buf[i]) + { + if (buf[i] >= '0' && buf[i] <= '9') + { + if (a == 0) + *rows = atoi(&buf[i]) - 1; + else + { + temp = atoi(&buf[i]); + *col = temp - 1; + } + a++; + i += nbr_length(temp) - 1; + } + i++; + } +} + +int putchar_tc(int tc) +{ + write(1, &tc, 1); + return (0); +} + +void move_cursor_left(int *col, int *row, char *cm) +{ + if (*col == 0) + return ; + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void move_cursor_right(int *col, int *row, char *cm) +{ + ++(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void delete_end(int *col, int *row, char *cm, char *ce) +{ + if (*col != 0) + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + tputs(ce, 1, putchar_tc); +} + +int main(void) +{ + /* change term settings */ + struct termios term; + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + /* init termcap */ + tgetent(NULL, "xterm"); + char *cm = tgetstr("cm", NULL); //cursor motion + char *ce = tgetstr("ce", NULL); //clear line from cursor + + int c = 0; + int row; + int col; + + while (1) + { + write(1, ">> ", 3); + while (read(0, &c, sizeof(c)) > 0) + { + if (c == '\n') + { + write(1, "\n", 1); + break; + } + get_cursor_position(&col, &row); + if (c == LEFT_ARROW) + move_cursor_left(&col, &row, cm); + else if (c == RIGHT_ARROW) + move_cursor_right(&col, &row, cm); + else if (c == BACKSPACE) + delete_end(&col, &row, cm, ce); + else + { + col++; + write(0, &c, 1); + } + c = 0; //flush buffer + } + } +} diff --git a/backup/test/pipe_test/a 2.out b/backup/test/pipe_test/a 2.out new file mode 100755 index 0000000..a552570 Binary files /dev/null and b/backup/test/pipe_test/a 2.out differ diff --git a/backup/test/pipe_test/a.out b/backup/test/pipe_test/a.out new file mode 100755 index 0000000..a552570 Binary files /dev/null and b/backup/test/pipe_test/a.out differ diff --git a/backup/test/pipe_test/main1 b/backup/test/pipe_test/main1 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main1 2 b/backup/test/pipe_test/main1 2 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main2 b/backup/test/pipe_test/main2 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main2 2 b/backup/test/pipe_test/main2 2 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main3 b/backup/test/pipe_test/main3 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main3 2 b/backup/test/pipe_test/main3 2 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main4 b/backup/test/pipe_test/main4 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/main4 2 b/backup/test/pipe_test/main4 2 new file mode 100644 index 0000000..e69de29 diff --git a/backup/test/pipe_test/microshell 2.c b/backup/test/pipe_test/microshell 2.c new file mode 100644 index 0000000..0344177 --- /dev/null +++ b/backup/test/pipe_test/microshell 2.c @@ -0,0 +1,288 @@ +#include +#include +#include +#include + +#define SIDE_OUT 0 +#define SIDE_IN 1 + +#define STDIN 0 +#define STDOUT 1 +#define STDERR_FILENO 2 + +#define TYPE_END 0 +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +#ifdef TEST_SH +# define TEST 1 +#else +# define TEST 0 +#endif + +typedef struct s_list +{ + char **args; + int length; + int type; + int pipes[2]; + struct s_list *previous; + struct s_list *next; +} t_list; + +int ft_strlen(char const *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int show_error(char const *str) +{ + if (str) + write(STDERR_FILENO, str, ft_strlen(str)); + return (EXIT_FAILURE); +} + +int exit_fatal(void) +{ + show_error("error: fatal\n"); + exit(EXIT_FAILURE); + return (EXIT_FAILURE); +} + +void *exit_fatal_ptr(void) +{ + exit_fatal(); + exit(EXIT_FAILURE); + return (NULL); +} + +char *ft_strdup(char const *str) +{ + char *copy; + int i; + + if (!(copy = (char*)malloc(sizeof(*copy) * (ft_strlen(str) + 1)))) + return (exit_fatal_ptr()); + i = 0; + while (str[i]) + { + copy[i] = str[i]; + i++; + } + copy[i] = 0; + return (copy); +} + +// make new list and add arguments in new list +int list_push(t_list **list, char *arg) +{ + t_list *new; + int add_arg(); + + if (!(new = (t_list*)malloc(sizeof(*new)))) + return (exit_fatal()); + new->args = NULL; + new->length = 0; + new->type = TYPE_END; + new->previous = NULL; + new->next = NULL; + if (*list) + { + (*list)->next = new; + new->previous = *list; + } + *list = new; + return (add_arg(new, arg)); +} + +int list_rewind(t_list **list) +{ + while (*list && (*list)->previous) + *list = (*list)->previous; + return (EXIT_SUCCESS); +} + +int list_clear(t_list **cmds) +{ + t_list *tmp; + int i; + + list_rewind(cmds); + while (*cmds) + { + tmp = (*cmds)->next; + i = 0; + while (i < (*cmds)->length) + free((*cmds)->args[i++]); + free((*cmds)->args); + free(*cmds); + *cmds = tmp; + } + *cmds = NULL; + return (EXIT_SUCCESS); +} + +int add_arg(t_list *cmd, char *arg) +{ + char **tmp; + int i; + + i = 0; + tmp = NULL; + if (!(tmp = (char**)malloc(sizeof(*tmp) * (cmd->length + 2)))) + return (exit_fatal()); + while (i < cmd->length) + { + tmp[i] = cmd->args[i]; + i++; + } + if (cmd->length > 0) + free(cmd->args); + cmd->args = tmp; + cmd->args[i++] = ft_strdup(arg); + cmd->args[i] = 0; + cmd->length++; +#if 0 + printf("cmd: %s\n", cmd->args[0]); + for (int i = 0; cmd->args[i]; ++i) + printf("arg[%d]: %s\n", i, cmd->args[i]); +#endif + return (EXIT_SUCCESS); +} + +int parse_arg(t_list **cmds, char *arg) +{ + int is_break; + + is_break = (strcmp(";", arg) == 0); + if (is_break && !*cmds) + return (EXIT_SUCCESS); + else if (!is_break && (!*cmds || (*cmds)->type > TYPE_END)) + { + printf("before list_push function\n"); + return (list_push(cmds, arg)); + } + else if (strcmp("|", arg) == 0) + { + printf("pipe case\n"); + (*cmds)->type = TYPE_PIPE; + } + else if (is_break) + { + printf("break case\n"); + (*cmds)->type = TYPE_BREAK; + } + else + { + printf("add_arg function start\n"); + return (add_arg(*cmds, arg)); + } + return (EXIT_SUCCESS); +} + +int exec_cmd(t_list *cmd, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (cmd->type == TYPE_PIPE || (cmd->previous && cmd->previous->type == TYPE_PIPE)) + { + pipe_open = 1; + if (pipe(cmd->pipes)) + return (exit_fatal()); + } + pid = fork(); + if (pid < 0) + return (exit_fatal()); + else if (pid == 0) + { + if (cmd->type == TYPE_PIPE + && dup2(cmd->pipes[SIDE_IN], STDOUT) < 0) + return (exit_fatal()); + if (cmd->previous && cmd->previous->type == TYPE_PIPE + && dup2(cmd->previous->pipes[SIDE_OUT], STDIN) < 0) + return (exit_fatal()); + if ((ret = execve(cmd->args[0], cmd->args, env)) < 0) + { + show_error("error: cannot execute "); + show_error(cmd->args[0]); + show_error("\n"); + } + exit(ret); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(cmd->pipes[SIDE_IN]); + if (!cmd->next || cmd->type == TYPE_BREAK) + close(cmd->pipes[SIDE_OUT]); + } + if (cmd->previous && cmd->previous->type == TYPE_PIPE) + close(cmd->previous->pipes[SIDE_OUT]); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + } + return (ret); +} + +int exec_cmds(t_list **cmds, char **env) +{ + t_list *crt; + int ret; + + ret = EXIT_SUCCESS; + list_rewind(cmds); + while (*cmds) + { + crt = *cmds; + if (strcmp("cd", crt->args[0]) == 0) + { + ret = EXIT_SUCCESS; + if (crt->length < 2) + ret = show_error("error: cd: bad arguments\n"); + else if (chdir(crt->args[1])) + { + ret = show_error("error: cd: cannot change directory to "); + show_error(crt->args[1]); + show_error("\n"); + } + } + else + ret = exec_cmd(crt, env); + if (!(*cmds)->next) + break ; + *cmds = (*cmds)->next; + } + return (ret); +} + +int main(int argc, char **argv, char **env) +{ + t_list *cmds; + int i; + int ret; + + ret = EXIT_SUCCESS; + cmds = NULL; + i = 1; + printf("Program Start\n"); + while (i < argc) + parse_arg(&cmds, argv[i++]); + if (cmds) + ret = exec_cmds(&cmds, env); + list_clear(&cmds); + if (TEST) + while (1); + return (ret); +} diff --git a/backup/test/pipe_test/microshell.c b/backup/test/pipe_test/microshell.c new file mode 100644 index 0000000..0344177 --- /dev/null +++ b/backup/test/pipe_test/microshell.c @@ -0,0 +1,288 @@ +#include +#include +#include +#include + +#define SIDE_OUT 0 +#define SIDE_IN 1 + +#define STDIN 0 +#define STDOUT 1 +#define STDERR_FILENO 2 + +#define TYPE_END 0 +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +#ifdef TEST_SH +# define TEST 1 +#else +# define TEST 0 +#endif + +typedef struct s_list +{ + char **args; + int length; + int type; + int pipes[2]; + struct s_list *previous; + struct s_list *next; +} t_list; + +int ft_strlen(char const *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int show_error(char const *str) +{ + if (str) + write(STDERR_FILENO, str, ft_strlen(str)); + return (EXIT_FAILURE); +} + +int exit_fatal(void) +{ + show_error("error: fatal\n"); + exit(EXIT_FAILURE); + return (EXIT_FAILURE); +} + +void *exit_fatal_ptr(void) +{ + exit_fatal(); + exit(EXIT_FAILURE); + return (NULL); +} + +char *ft_strdup(char const *str) +{ + char *copy; + int i; + + if (!(copy = (char*)malloc(sizeof(*copy) * (ft_strlen(str) + 1)))) + return (exit_fatal_ptr()); + i = 0; + while (str[i]) + { + copy[i] = str[i]; + i++; + } + copy[i] = 0; + return (copy); +} + +// make new list and add arguments in new list +int list_push(t_list **list, char *arg) +{ + t_list *new; + int add_arg(); + + if (!(new = (t_list*)malloc(sizeof(*new)))) + return (exit_fatal()); + new->args = NULL; + new->length = 0; + new->type = TYPE_END; + new->previous = NULL; + new->next = NULL; + if (*list) + { + (*list)->next = new; + new->previous = *list; + } + *list = new; + return (add_arg(new, arg)); +} + +int list_rewind(t_list **list) +{ + while (*list && (*list)->previous) + *list = (*list)->previous; + return (EXIT_SUCCESS); +} + +int list_clear(t_list **cmds) +{ + t_list *tmp; + int i; + + list_rewind(cmds); + while (*cmds) + { + tmp = (*cmds)->next; + i = 0; + while (i < (*cmds)->length) + free((*cmds)->args[i++]); + free((*cmds)->args); + free(*cmds); + *cmds = tmp; + } + *cmds = NULL; + return (EXIT_SUCCESS); +} + +int add_arg(t_list *cmd, char *arg) +{ + char **tmp; + int i; + + i = 0; + tmp = NULL; + if (!(tmp = (char**)malloc(sizeof(*tmp) * (cmd->length + 2)))) + return (exit_fatal()); + while (i < cmd->length) + { + tmp[i] = cmd->args[i]; + i++; + } + if (cmd->length > 0) + free(cmd->args); + cmd->args = tmp; + cmd->args[i++] = ft_strdup(arg); + cmd->args[i] = 0; + cmd->length++; +#if 0 + printf("cmd: %s\n", cmd->args[0]); + for (int i = 0; cmd->args[i]; ++i) + printf("arg[%d]: %s\n", i, cmd->args[i]); +#endif + return (EXIT_SUCCESS); +} + +int parse_arg(t_list **cmds, char *arg) +{ + int is_break; + + is_break = (strcmp(";", arg) == 0); + if (is_break && !*cmds) + return (EXIT_SUCCESS); + else if (!is_break && (!*cmds || (*cmds)->type > TYPE_END)) + { + printf("before list_push function\n"); + return (list_push(cmds, arg)); + } + else if (strcmp("|", arg) == 0) + { + printf("pipe case\n"); + (*cmds)->type = TYPE_PIPE; + } + else if (is_break) + { + printf("break case\n"); + (*cmds)->type = TYPE_BREAK; + } + else + { + printf("add_arg function start\n"); + return (add_arg(*cmds, arg)); + } + return (EXIT_SUCCESS); +} + +int exec_cmd(t_list *cmd, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (cmd->type == TYPE_PIPE || (cmd->previous && cmd->previous->type == TYPE_PIPE)) + { + pipe_open = 1; + if (pipe(cmd->pipes)) + return (exit_fatal()); + } + pid = fork(); + if (pid < 0) + return (exit_fatal()); + else if (pid == 0) + { + if (cmd->type == TYPE_PIPE + && dup2(cmd->pipes[SIDE_IN], STDOUT) < 0) + return (exit_fatal()); + if (cmd->previous && cmd->previous->type == TYPE_PIPE + && dup2(cmd->previous->pipes[SIDE_OUT], STDIN) < 0) + return (exit_fatal()); + if ((ret = execve(cmd->args[0], cmd->args, env)) < 0) + { + show_error("error: cannot execute "); + show_error(cmd->args[0]); + show_error("\n"); + } + exit(ret); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(cmd->pipes[SIDE_IN]); + if (!cmd->next || cmd->type == TYPE_BREAK) + close(cmd->pipes[SIDE_OUT]); + } + if (cmd->previous && cmd->previous->type == TYPE_PIPE) + close(cmd->previous->pipes[SIDE_OUT]); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + } + return (ret); +} + +int exec_cmds(t_list **cmds, char **env) +{ + t_list *crt; + int ret; + + ret = EXIT_SUCCESS; + list_rewind(cmds); + while (*cmds) + { + crt = *cmds; + if (strcmp("cd", crt->args[0]) == 0) + { + ret = EXIT_SUCCESS; + if (crt->length < 2) + ret = show_error("error: cd: bad arguments\n"); + else if (chdir(crt->args[1])) + { + ret = show_error("error: cd: cannot change directory to "); + show_error(crt->args[1]); + show_error("\n"); + } + } + else + ret = exec_cmd(crt, env); + if (!(*cmds)->next) + break ; + *cmds = (*cmds)->next; + } + return (ret); +} + +int main(int argc, char **argv, char **env) +{ + t_list *cmds; + int i; + int ret; + + ret = EXIT_SUCCESS; + cmds = NULL; + i = 1; + printf("Program Start\n"); + while (i < argc) + parse_arg(&cmds, argv[i++]); + if (cmds) + ret = exec_cmds(&cmds, env); + list_clear(&cmds); + if (TEST) + while (1); + return (ret); +} diff --git a/backup/test/pipe_test/pipe_basic 2.c b/backup/test/pipe_test/pipe_basic 2.c new file mode 100644 index 0000000..55b2414 --- /dev/null +++ b/backup/test/pipe_test/pipe_basic 2.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + printf("DEBUG=========================\n"); + dup2(fds[1], STDOUT_FILENO); + write(STDOUT_FILENO, DATA, DATA_LEN); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + return (0); +} diff --git a/backup/test/pipe_test/pipe_basic.c b/backup/test/pipe_test/pipe_basic.c new file mode 100644 index 0000000..55b2414 --- /dev/null +++ b/backup/test/pipe_test/pipe_basic.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + printf("DEBUG=========================\n"); + dup2(fds[1], STDOUT_FILENO); + write(STDOUT_FILENO, DATA, DATA_LEN); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + return (0); +} diff --git a/backup/test/pipe_test/pipe_ex 2.c b/backup/test/pipe_test/pipe_ex 2.c new file mode 100644 index 0000000..5ee5d86 --- /dev/null +++ b/backup/test/pipe_test/pipe_ex 2.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 3 +#define TYPE_REOUT 4 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + + if (tokens->type == TYPE_BREAK) + dup2(tokens->fd, STDIN_FILENO); + + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || tokens->type == TYPE_BREAK) + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + close(tokens->prev->fds[0]); + if (tokens->type == TYPE_BREAK) + close(tokens->fd); + printf("parent process finished==============\n"); + } + return (ret); +} +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + set2.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} +#endif + +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + printf("first run\n"); + run(&set1, env); + return (0); +} +#endif +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_RDONLY); + + run(&set1, env); + return (0); +} diff --git a/backup/test/pipe_test/pipe_ex.c b/backup/test/pipe_test/pipe_ex.c new file mode 100644 index 0000000..5ee5d86 --- /dev/null +++ b/backup/test/pipe_test/pipe_ex.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 3 +#define TYPE_REOUT 4 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + + if (tokens->type == TYPE_BREAK) + dup2(tokens->fd, STDIN_FILENO); + + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || tokens->type == TYPE_BREAK) + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + close(tokens->prev->fds[0]); + if (tokens->type == TYPE_BREAK) + close(tokens->fd); + printf("parent process finished==============\n"); + } + return (ret); +} +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + set2.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} +#endif + +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + printf("first run\n"); + run(&set1, env); + return (0); +} +#endif +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_RDONLY); + + run(&set1, env); + return (0); +} diff --git a/backup/test/pipe_test/pipe_redir_ex 2.c b/backup/test/pipe_test/pipe_redir_ex 2.c new file mode 100644 index 0000000..41fb788 --- /dev/null +++ b/backup/test/pipe_test/pipe_redir_ex 2.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 4 +#define TYPE_REOUT 8 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd[2]; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if ((tokens->type & TYPE_PIPE) || (tokens->prev && (tokens->prev->type & TYPE_PIPE))) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + if (tokens->type & TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[0], STDIN_FILENO); + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[1], STDIN_FILENO); + if (tokens->type & TYPE_REOUT) + dup2(tokens->fd[0], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || (tokens->type & TYPE_BREAK)) + close(tokens->fds[0]); + } + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + close(tokens->prev->fds[0]); +#if 1 + if (tokens->type & TYPE_REIN) + close(tokens->fd[0]); + if (tokens->type & TYPE_REIN) + close(tokens->fd[1]); + if (tokens->type & TYPE_REOUT) + close(tokens->fd[0]); +#endif + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK | TYPE_REIN; + set1.fd[0] = open("test.txt", O_RDONLY); + set1.fd[1] = open("test2.txt", O_RDONLY); + +#if 0 + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "a"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK | TYPE_REOUT; + set2.fd = open("test2.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + run(&set1, env); + run(&set2, env); +#endif + run(&set1, env); + + return (0); +} diff --git a/backup/test/pipe_test/pipe_redir_ex.c b/backup/test/pipe_test/pipe_redir_ex.c new file mode 100644 index 0000000..41fb788 --- /dev/null +++ b/backup/test/pipe_test/pipe_redir_ex.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 4 +#define TYPE_REOUT 8 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd[2]; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if ((tokens->type & TYPE_PIPE) || (tokens->prev && (tokens->prev->type & TYPE_PIPE))) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + if (tokens->type & TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[0], STDIN_FILENO); + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[1], STDIN_FILENO); + if (tokens->type & TYPE_REOUT) + dup2(tokens->fd[0], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || (tokens->type & TYPE_BREAK)) + close(tokens->fds[0]); + } + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + close(tokens->prev->fds[0]); +#if 1 + if (tokens->type & TYPE_REIN) + close(tokens->fd[0]); + if (tokens->type & TYPE_REIN) + close(tokens->fd[1]); + if (tokens->type & TYPE_REOUT) + close(tokens->fd[0]); +#endif + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK | TYPE_REIN; + set1.fd[0] = open("test.txt", O_RDONLY); + set1.fd[1] = open("test2.txt", O_RDONLY); + +#if 0 + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "a"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK | TYPE_REOUT; + set2.fd = open("test2.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + run(&set1, env); + run(&set2, env); +#endif + run(&set1, env); + + return (0); +} diff --git a/backup/test/pipe_test/redir_ex 2.c b/backup/test/pipe_test/redir_ex 2.c new file mode 100644 index 0000000..abab671 --- /dev/null +++ b/backup/test/pipe_test/redir_ex 2.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +#if 0 +void add_args(char **args, char **buf) { + int i; + i = 1; + while (buf[i - 1]) + { + args[i] = buf[i - 1]; + ++i; + } + args[i] = NULL; +} +#endif + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + // both set1 and set2 use pipe function to connect fds[0], fds[1] + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + // fds[1] of set1 has result of ls -al + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + // fds[0] of set1 has input of ls -al for grep + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + // close fds[1] of set1 + // close fds[1] of set2 + close(tokens->fds[1]); + // fds[0] of set1 is still open + if (!tokens->next || tokens->type == TYPE_BREAK) + // fds[0] of set2 is closed + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + // fds[0] of set1 is closed + close(tokens->prev->fds[0]); + printf("parent process finished==============\n"); + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} + +#if 0 +int run1(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[0], STDOUT_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[1]); + } + return (0); +} + +int run2(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[1], STDIN_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[0]); + } + return (0); +} +#endif diff --git a/backup/test/pipe_test/redir_ex.c b/backup/test/pipe_test/redir_ex.c new file mode 100644 index 0000000..abab671 --- /dev/null +++ b/backup/test/pipe_test/redir_ex.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +#if 0 +void add_args(char **args, char **buf) { + int i; + i = 1; + while (buf[i - 1]) + { + args[i] = buf[i - 1]; + ++i; + } + args[i] = NULL; +} +#endif + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + // both set1 and set2 use pipe function to connect fds[0], fds[1] + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + // fds[1] of set1 has result of ls -al + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + // fds[0] of set1 has input of ls -al for grep + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + // close fds[1] of set1 + // close fds[1] of set2 + close(tokens->fds[1]); + // fds[0] of set1 is still open + if (!tokens->next || tokens->type == TYPE_BREAK) + // fds[0] of set2 is closed + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + // fds[0] of set1 is closed + close(tokens->prev->fds[0]); + printf("parent process finished==============\n"); + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} + +#if 0 +int run1(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[0], STDOUT_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[1]); + } + return (0); +} + +int run2(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[1], STDIN_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[0]); + } + return (0); +} +#endif diff --git a/backup/test/pipe_test/test 2.c b/backup/test/pipe_test/test 2.c new file mode 100644 index 0000000..80873d9 --- /dev/null +++ b/backup/test/pipe_test/test 2.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + pid_t pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) { // child process + close(fds[0]); + write(fds[1], DATA, DATA_LEN); + } + else { // parents process + waitpid(pid, &status, 0); + close(fds[1]); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + close(fds[0]); + } + return (0); +} diff --git a/backup/test/pipe_test/test 2.txt b/backup/test/pipe_test/test 2.txt new file mode 100644 index 0000000..30908be --- /dev/null +++ b/backup/test/pipe_test/test 2.txt @@ -0,0 +1,4 @@ +apple +banana +mango +pineapple diff --git a/backup/test/pipe_test/test.c b/backup/test/pipe_test/test.c new file mode 100644 index 0000000..80873d9 --- /dev/null +++ b/backup/test/pipe_test/test.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + pid_t pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) { // child process + close(fds[0]); + write(fds[1], DATA, DATA_LEN); + } + else { // parents process + waitpid(pid, &status, 0); + close(fds[1]); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + close(fds[0]); + } + return (0); +} diff --git a/backup/test/pipe_test/test.txt b/backup/test/pipe_test/test.txt new file mode 100644 index 0000000..30908be --- /dev/null +++ b/backup/test/pipe_test/test.txt @@ -0,0 +1,4 @@ +apple +banana +mango +pineapple diff --git a/backup/test/pipe_test/test2 2.txt b/backup/test/pipe_test/test2 2.txt new file mode 100644 index 0000000..89eb6b9 --- /dev/null +++ b/backup/test/pipe_test/test2 2.txt @@ -0,0 +1,3 @@ +orange +grape +tomato diff --git a/backup/test/pipe_test/test2.txt b/backup/test/pipe_test/test2.txt new file mode 100644 index 0000000..89eb6b9 --- /dev/null +++ b/backup/test/pipe_test/test2.txt @@ -0,0 +1,3 @@ +orange +grape +tomato diff --git a/backup/test/termcap_test/a 2.out b/backup/test/termcap_test/a 2.out new file mode 100755 index 0000000..132a4f9 Binary files /dev/null and b/backup/test/termcap_test/a 2.out differ diff --git a/backup/test/termcap_test/a.out b/backup/test/termcap_test/a.out new file mode 100755 index 0000000..132a4f9 Binary files /dev/null and b/backup/test/termcap_test/a.out differ diff --git a/backup/test/termcap_test/cursor 2.c b/backup/test/termcap_test/cursor 2.c new file mode 100644 index 0000000..fd6fdf0 --- /dev/null +++ b/backup/test/termcap_test/cursor 2.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +#include + +#define LEFT_ARROW 4479771 +#define RIGHT_ARROW 4414235 + +int ft_putchar_tc(int tc) +{ + write(STDOUT_FILENO, &tc, 1); + return (0); +} + +int ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} + +int ft_strlen(char *str) +{ + int ret; + + ret = 0; + while (str[ret]) + ++ret; + return (ret); +} + +void get_cursor_pos(int *row, int *col) +{ + int cnt; + int idx; + char buf[255]; + int read_size; + + write(STDIN_FILENO, "\033[6n", ft_strlen("\033[6n")); + read_size = read(STDIN_FILENO, buf, 255); + buf[read_size] = '\0'; + idx = -1; + while (!ft_isdigit(buf[idx])) + ++idx; + *row = 0; + while (ft_isdigit(buf[idx])) + *row = *row * 10 + buf[idx++] - '0'; + ++idx; + *col = 0; + while (ft_isdigit(buf[idx])) + *col = *col * 10 + buf[idx++] - '0'; +} + +void move_cursor_left() +{ + write(STDIN_FILENO, "\033[D", 4); +} + +void move_cursor_right() +{ + write(STDIN_FILENO, "\033[C", 4); +} + +int main() +{ + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif +#if 0 + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#endif +#if 1 + int col; + int row; + char *prompt; + + long c; + prompt = ">> "; + while (1) + { + write(1, prompt, ft_strlen(prompt)); + c = 0; + while (read(0, &c, sizeof(c)) > 0) + { + //printf("keycode: %ld\n", c); + if (c == '\n') + { + printf("DEBUG=========================\n"); + break ; + } + get_cursor_pos(&row, &col); + if (col > 3 && c == LEFT_ARROW) + write(STDIN_FILENO, "\033[D", ft_strlen("\033[D")); + else if (c == RIGHT_ARROW) + write(STDIN_FILENO, "\033[C", ft_strlen("\033[C")); + else + write(1, &c, 1); + } + } +#endif +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/backup/test/termcap_test/cursor.c b/backup/test/termcap_test/cursor.c new file mode 100644 index 0000000..fd6fdf0 --- /dev/null +++ b/backup/test/termcap_test/cursor.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +#include + +#define LEFT_ARROW 4479771 +#define RIGHT_ARROW 4414235 + +int ft_putchar_tc(int tc) +{ + write(STDOUT_FILENO, &tc, 1); + return (0); +} + +int ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} + +int ft_strlen(char *str) +{ + int ret; + + ret = 0; + while (str[ret]) + ++ret; + return (ret); +} + +void get_cursor_pos(int *row, int *col) +{ + int cnt; + int idx; + char buf[255]; + int read_size; + + write(STDIN_FILENO, "\033[6n", ft_strlen("\033[6n")); + read_size = read(STDIN_FILENO, buf, 255); + buf[read_size] = '\0'; + idx = -1; + while (!ft_isdigit(buf[idx])) + ++idx; + *row = 0; + while (ft_isdigit(buf[idx])) + *row = *row * 10 + buf[idx++] - '0'; + ++idx; + *col = 0; + while (ft_isdigit(buf[idx])) + *col = *col * 10 + buf[idx++] - '0'; +} + +void move_cursor_left() +{ + write(STDIN_FILENO, "\033[D", 4); +} + +void move_cursor_right() +{ + write(STDIN_FILENO, "\033[C", 4); +} + +int main() +{ + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif +#if 0 + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#endif +#if 1 + int col; + int row; + char *prompt; + + long c; + prompt = ">> "; + while (1) + { + write(1, prompt, ft_strlen(prompt)); + c = 0; + while (read(0, &c, sizeof(c)) > 0) + { + //printf("keycode: %ld\n", c); + if (c == '\n') + { + printf("DEBUG=========================\n"); + break ; + } + get_cursor_pos(&row, &col); + if (col > 3 && c == LEFT_ARROW) + write(STDIN_FILENO, "\033[D", ft_strlen("\033[D")); + else if (c == RIGHT_ARROW) + write(STDIN_FILENO, "\033[C", ft_strlen("\033[C")); + else + write(1, &c, 1); + } + } +#endif +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/backup/test/termcap_test/ft_putchar_tc 2.c b/backup/test/termcap_test/ft_putchar_tc 2.c new file mode 100644 index 0000000..22164b7 --- /dev/null +++ b/backup/test/termcap_test/ft_putchar_tc 2.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + char c; + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/backup/test/termcap_test/ft_putchar_tc.c b/backup/test/termcap_test/ft_putchar_tc.c new file mode 100644 index 0000000..22164b7 --- /dev/null +++ b/backup/test/termcap_test/ft_putchar_tc.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + char c; + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/backup/test/termcap_test/test 2.c b/backup/test/termcap_test/test 2.c new file mode 100644 index 0000000..03e93f0 --- /dev/null +++ b/backup/test/termcap_test/test 2.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +struct termios old_settings; + +int main() +{ + struct termios new_settings; + + int vdisable; + char buf[255]; +#if 0 + if ((vdisable = fpathconf(0, _PC_VDISABLE)) < 0) { + perror("error "); + exit(0); + } + printf("vdisable: %d\n", vdisable); +#endif + + tcgetattr(0, &new_settings); + old_settings = new_settings; + new_settings.c_cc[VEOF] = 255; + tcsetattr(0, TCSAFLUSH, &new_settings); + memset(buf, 0x00, 255); + read(0, buf, 255); + printf("%s\n", buf); + tcsetattr(0, TCSAFLUSH, &old_settings); + exit(0); + return (0); +} diff --git a/backup/test/termcap_test/test.c b/backup/test/termcap_test/test.c new file mode 100644 index 0000000..03e93f0 --- /dev/null +++ b/backup/test/termcap_test/test.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +struct termios old_settings; + +int main() +{ + struct termios new_settings; + + int vdisable; + char buf[255]; +#if 0 + if ((vdisable = fpathconf(0, _PC_VDISABLE)) < 0) { + perror("error "); + exit(0); + } + printf("vdisable: %d\n", vdisable); +#endif + + tcgetattr(0, &new_settings); + old_settings = new_settings; + new_settings.c_cc[VEOF] = 255; + tcsetattr(0, TCSAFLUSH, &new_settings); + memset(buf, 0x00, 255); + read(0, buf, 255); + printf("%s\n", buf); + tcsetattr(0, TCSAFLUSH, &old_settings); + exit(0); + return (0); +} diff --git a/backup/test/termcap_test/testtest 2.c b/backup/test/termcap_test/testtest 2.c new file mode 100644 index 0000000..ba5ea89 --- /dev/null +++ b/backup/test/termcap_test/testtest 2.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +# define BACKSPACE 127 +# define LEFT_ARROW 4479771 +# define RIGHT_ARROW 4414235 + +int nbr_length(int n) +{ + int i = 0; + + if (n <= 0) + i++; + while (n != 0) + { + n /= 10; + i++; + } + return (i); +} + +void get_cursor_position(int *col, int *rows) +{ + int a = 0; + int i = 1; + char buf[255]; + int ret; + int temp; + + write(0, "\033[6n", 4); //report cursor location + ret = read(0, buf, 254); + buf[ret] = '\0'; + while (buf[i]) + { + if (buf[i] >= '0' && buf[i] <= '9') + { + if (a == 0) + *rows = atoi(&buf[i]) - 1; + else + { + temp = atoi(&buf[i]); + *col = temp - 1; + } + a++; + i += nbr_length(temp) - 1; + } + i++; + } +} + +int putchar_tc(int tc) +{ + write(1, &tc, 1); + return (0); +} + +void move_cursor_left(int *col, int *row, char *cm) +{ + if (*col == 0) + return ; + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void move_cursor_right(int *col, int *row, char *cm) +{ + ++(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void delete_end(int *col, int *row, char *cm, char *ce) +{ + if (*col != 0) + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + tputs(ce, 1, putchar_tc); +} + +int main(void) +{ + /* change term settings */ + struct termios term; + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + /* init termcap */ + tgetent(NULL, "xterm"); + char *cm = tgetstr("cm", NULL); //cursor motion + char *ce = tgetstr("ce", NULL); //clear line from cursor + + int c = 0; + int row; + int col; + + while (1) + { + write(1, ">> ", 3); + while (read(0, &c, sizeof(c)) > 0) + { + if (c == '\n') + { + write(1, "\n", 1); + break; + } + get_cursor_position(&col, &row); + if (c == LEFT_ARROW) + move_cursor_left(&col, &row, cm); + else if (c == RIGHT_ARROW) + move_cursor_right(&col, &row, cm); + else if (c == BACKSPACE) + delete_end(&col, &row, cm, ce); + else + { + col++; + write(0, &c, 1); + } + c = 0; //flush buffer + } + } +} diff --git a/backup/test/termcap_test/testtest.c b/backup/test/termcap_test/testtest.c new file mode 100644 index 0000000..ba5ea89 --- /dev/null +++ b/backup/test/termcap_test/testtest.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +# define BACKSPACE 127 +# define LEFT_ARROW 4479771 +# define RIGHT_ARROW 4414235 + +int nbr_length(int n) +{ + int i = 0; + + if (n <= 0) + i++; + while (n != 0) + { + n /= 10; + i++; + } + return (i); +} + +void get_cursor_position(int *col, int *rows) +{ + int a = 0; + int i = 1; + char buf[255]; + int ret; + int temp; + + write(0, "\033[6n", 4); //report cursor location + ret = read(0, buf, 254); + buf[ret] = '\0'; + while (buf[i]) + { + if (buf[i] >= '0' && buf[i] <= '9') + { + if (a == 0) + *rows = atoi(&buf[i]) - 1; + else + { + temp = atoi(&buf[i]); + *col = temp - 1; + } + a++; + i += nbr_length(temp) - 1; + } + i++; + } +} + +int putchar_tc(int tc) +{ + write(1, &tc, 1); + return (0); +} + +void move_cursor_left(int *col, int *row, char *cm) +{ + if (*col == 0) + return ; + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void move_cursor_right(int *col, int *row, char *cm) +{ + ++(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void delete_end(int *col, int *row, char *cm, char *ce) +{ + if (*col != 0) + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + tputs(ce, 1, putchar_tc); +} + +int main(void) +{ + /* change term settings */ + struct termios term; + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + /* init termcap */ + tgetent(NULL, "xterm"); + char *cm = tgetstr("cm", NULL); //cursor motion + char *ce = tgetstr("ce", NULL); //clear line from cursor + + int c = 0; + int row; + int col; + + while (1) + { + write(1, ">> ", 3); + while (read(0, &c, sizeof(c)) > 0) + { + if (c == '\n') + { + write(1, "\n", 1); + break; + } + get_cursor_position(&col, &row); + if (c == LEFT_ARROW) + move_cursor_left(&col, &row, cm); + else if (c == RIGHT_ARROW) + move_cursor_right(&col, &row, cm); + else if (c == BACKSPACE) + delete_end(&col, &row, cm, ce); + else + { + col++; + write(0, &c, 1); + } + c = 0; //flush buffer + } + } +} diff --git a/incs/minishell.h b/incs/minishell.h index 07eda3d..d9b01ac 100644 --- a/incs/minishell.h +++ b/incs/minishell.h @@ -32,8 +32,11 @@ # define BUFFER_SIZE 1024 # define SIG_SIGINT 1 +<<<<<<< HEAD +======= # define SIG_SIGQUIT 2 # define SIG_EOF 3 +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 //# define FLAG_FD_OPEN 1 @@ -235,7 +238,11 @@ t_list *gen_line_list(char *lines); /* ** gen_tokens.c */ +<<<<<<< HEAD +t_tokens *gen_tokens(t_info *info, char *line); +======= t_tokens *gen_tokens(t_info *info, char *set_str); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 /* ** gen_env.c @@ -497,6 +504,12 @@ void append_history_list( */ int calc_min(int num1, int num2); int calc_max(int num1, int num2); +<<<<<<< HEAD +#endif + +void handle_sig_in_gnl(t_info info, long *inst_arr); + +======= void set_signal(); @@ -505,6 +518,7 @@ int handle_sig(); void handle_sig_in_proc(int pid); #endif +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 #if 0 diff --git a/srcs/backup/get_next_line 2.c b/srcs/backup/get_next_line 2.c new file mode 100644 index 0000000..2fb1218 --- /dev/null +++ b/srcs/backup/get_next_line 2.c @@ -0,0 +1,68 @@ +#include "../incs/minishell.h" + +static char handle_enter( + int backup_len, + char buf, + int read_size) +{ + int ret; + + ret = 0; + if((backup_len == 0) && buf == '\n' && read_size == 1) + { + //display_prompt(); + ret = 1; + } + return (ret); +} + +static char *handle_fflush(char *backup) +{ + char *ret; + + free(backup); + backup = NULL; + ret = ft_strjoin("", ""); + g_fsh_buf = 0; + return (ret); +} + +static void handle_eof(t_info *info, char *backup) +{ + if (ft_strlen(backup) == 0) + { + free(backup); + g_ret = 0; + g_ret = exit_shell(info); + exit(0); + } + ft_putstr_fd(" \b\b", STDERR_FILENO); +} + +int get_next_line(t_info *info, char **line) +{ + int read_size = 1; + char buf[2]; + char *backup; + + backup = ft_strjoin("", ""); + while (read_size >= 0 && buf[0] != '\n') + { + read_size = read(STDIN_FILENO, buf, 1); + buf[1] = '\0'; + if (g_fsh_buf) + backup = handle_fflush(backup); + if (read_size == 0) + handle_eof(info, backup); + else if (buf[0] != '\n' && read_size > 0) + { + char *tmp = backup; + backup = ft_strjoin(backup, buf); + free(tmp); + } + else if (handle_enter(ft_strlen(backup), buf[0], read_size)) + buf[0] = '\0'; + } + *line = backup; + return (read_size); +} diff --git a/srcs/backup/get_next_line.c b/srcs/backup/get_next_line.c new file mode 100644 index 0000000..2fb1218 --- /dev/null +++ b/srcs/backup/get_next_line.c @@ -0,0 +1,68 @@ +#include "../incs/minishell.h" + +static char handle_enter( + int backup_len, + char buf, + int read_size) +{ + int ret; + + ret = 0; + if((backup_len == 0) && buf == '\n' && read_size == 1) + { + //display_prompt(); + ret = 1; + } + return (ret); +} + +static char *handle_fflush(char *backup) +{ + char *ret; + + free(backup); + backup = NULL; + ret = ft_strjoin("", ""); + g_fsh_buf = 0; + return (ret); +} + +static void handle_eof(t_info *info, char *backup) +{ + if (ft_strlen(backup) == 0) + { + free(backup); + g_ret = 0; + g_ret = exit_shell(info); + exit(0); + } + ft_putstr_fd(" \b\b", STDERR_FILENO); +} + +int get_next_line(t_info *info, char **line) +{ + int read_size = 1; + char buf[2]; + char *backup; + + backup = ft_strjoin("", ""); + while (read_size >= 0 && buf[0] != '\n') + { + read_size = read(STDIN_FILENO, buf, 1); + buf[1] = '\0'; + if (g_fsh_buf) + backup = handle_fflush(backup); + if (read_size == 0) + handle_eof(info, backup); + else if (buf[0] != '\n' && read_size > 0) + { + char *tmp = backup; + backup = ft_strjoin(backup, buf); + free(tmp); + } + else if (handle_enter(ft_strlen(backup), buf[0], read_size)) + buf[0] = '\0'; + } + *line = backup; + return (read_size); +} diff --git a/srcs/backup/handle_sig_init 2.c b/srcs/backup/handle_sig_init 2.c new file mode 100644 index 0000000..4bc7d20 --- /dev/null +++ b/srcs/backup/handle_sig_init 2.c @@ -0,0 +1,27 @@ +#include "../incs/minishell.h" + +static void sigint_handler(int signo) +{ + //remove signal, and change line + ft_putstr_fd("\b\b \n", STDERR_FILENO); +// display_prompt(); + //change dollar_ret + g_ret = 130; + g_fsh_buf = 1; +} + +static void sigquit_handler(int signo) +{ + //remove signal(nothing to change on the screen) + ft_putstr_fd("\b\b \b\b", STDERR_FILENO); + //do nothing + //no change dollar_ret +} + +void handle_sig_init(t_info *info) +{ + if (signal(SIGINT, (void *)sigint_handler) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-C", 2); + if (signal(SIGQUIT, (void *)sigquit_handler) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-\\", 2); +} diff --git a/srcs/backup/handle_sig_init.c b/srcs/backup/handle_sig_init.c new file mode 100644 index 0000000..4bc7d20 --- /dev/null +++ b/srcs/backup/handle_sig_init.c @@ -0,0 +1,27 @@ +#include "../incs/minishell.h" + +static void sigint_handler(int signo) +{ + //remove signal, and change line + ft_putstr_fd("\b\b \n", STDERR_FILENO); +// display_prompt(); + //change dollar_ret + g_ret = 130; + g_fsh_buf = 1; +} + +static void sigquit_handler(int signo) +{ + //remove signal(nothing to change on the screen) + ft_putstr_fd("\b\b \b\b", STDERR_FILENO); + //do nothing + //no change dollar_ret +} + +void handle_sig_init(t_info *info) +{ + if (signal(SIGINT, (void *)sigint_handler) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-C", 2); + if (signal(SIGQUIT, (void *)sigquit_handler) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-\\", 2); +} diff --git a/srcs/backup/handle_sig_proc 2.c b/srcs/backup/handle_sig_proc 2.c new file mode 100644 index 0000000..b94f78e --- /dev/null +++ b/srcs/backup/handle_sig_proc 2.c @@ -0,0 +1,32 @@ +#include "../incs/minishell.h" + +static void sigint_handler_proc(int signo) +{ + //no need to remove signal + //stop process(minishell is still running) + //change dollar_ret; + g_ret = 130; + //display prompt + ft_putstr_fd("\n", STDERR_FILENO); +} + +static void sigquit_handler_proc(int signo) +{ + //no need to remove signal + //put messege + ft_putstr_fd("Quit: 3", STDERR_FILENO); + //stop process(minishell is still running) + //change dollar_ret + g_ret = 131; + //display prompt + ft_putstr_fd("\n", STDERR_FILENO); +} + +//when signal pressed in processing. +void handle_sig_proc(int pid) +{ + if (signal(SIGINT, (void *)sigint_handler_proc) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-C", STDERR_FILENO); + if (signal(SIGQUIT, (void *)sigquit_handler_proc) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-\\", STDERR_FILENO); +} diff --git a/srcs/backup/handle_sig_proc.c b/srcs/backup/handle_sig_proc.c new file mode 100644 index 0000000..b94f78e --- /dev/null +++ b/srcs/backup/handle_sig_proc.c @@ -0,0 +1,32 @@ +#include "../incs/minishell.h" + +static void sigint_handler_proc(int signo) +{ + //no need to remove signal + //stop process(minishell is still running) + //change dollar_ret; + g_ret = 130; + //display prompt + ft_putstr_fd("\n", STDERR_FILENO); +} + +static void sigquit_handler_proc(int signo) +{ + //no need to remove signal + //put messege + ft_putstr_fd("Quit: 3", STDERR_FILENO); + //stop process(minishell is still running) + //change dollar_ret + g_ret = 131; + //display prompt + ft_putstr_fd("\n", STDERR_FILENO); +} + +//when signal pressed in processing. +void handle_sig_proc(int pid) +{ + if (signal(SIGINT, (void *)sigint_handler_proc) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-C", STDERR_FILENO); + if (signal(SIGQUIT, (void *)sigquit_handler_proc) == SIG_ERR) + ft_putstr_fd("\n\e[1;31m can't catch cnrtl-\\", STDERR_FILENO); +} diff --git a/srcs/backup/init_global 2.c b/srcs/backup/init_global 2.c new file mode 100644 index 0000000..b30c42d --- /dev/null +++ b/srcs/backup/init_global 2.c @@ -0,0 +1,7 @@ +#include "../incs/minishell.h" + +void init_global() +{ +// g_ret = 0; +// g_fsh_buf = 0; +} diff --git a/srcs/backup/init_global.c b/srcs/backup/init_global.c new file mode 100644 index 0000000..b30c42d --- /dev/null +++ b/srcs/backup/init_global.c @@ -0,0 +1,7 @@ +#include "../incs/minishell.h" + +void init_global() +{ +// g_ret = 0; +// g_fsh_buf = 0; +} diff --git a/srcs/env/sort_env 2.c b/srcs/env/sort_env 2.c new file mode 100644 index 0000000..4749c0a --- /dev/null +++ b/srcs/env/sort_env 2.c @@ -0,0 +1,91 @@ +#include "../../incs/minishell.h" + +static int has_to_change(char *s1, char*s2) +{ + int s1_len; + int s2_len; + int max_len; + + s1_len = ft_strlen(s1); + s2_len = ft_strlen(s2); + max_len = (s1_len > s2_len) ? s1_len : s2_len; + return (ft_strncmp(s1, s2, max_len)); +} + +static int get_str_len( + char **str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +static char** sort_env( + char **env, + int env_len) +{ + int i; + int sorted_flag; + char *tmp; + + sorted_flag = 1; + while (--env_len && sorted_flag) + { + i = -1; + sorted_flag = 0; + while (++i < env_len - 1) + { + if (has_to_change(env[i], env[i + 1]) > 0) + { + tmp = env[i]; + env[i] = env[i + 1]; + env[i + 1] = tmp; + sorted_flag = 1; + } + } + } + return (env); +} + +static char *convert_list2str( + t_list *list) +{ + t_list *cur; + char *str; + char *tmp; + + cur = list; + str = ft_strdup(""); + while (cur) + { + tmp = ft_strjoin(str, cur->data); + free(str); + str = ft_strjoin(tmp, "\n"); + free(tmp); + cur = cur->next; + } + return (str); +} + +void print_sorted_env( + t_list *env_list) +{ + char *tmp; + char **str; + int i; + + tmp = convert_list2str(env_list); + str = ft_split(tmp, '\n'); + str = sort_env(str, get_str_len(str) + 1); + i = -1; + while (str[++i]) + { + printf("%s\n", str[i]); + free(str[i]); + } + free(tmp); + free(str); +} diff --git a/srcs/gen_tokens.c b/srcs/gen_tokens.c index d1c6c66..020ac58 100644 --- a/srcs/gen_tokens.c +++ b/srcs/gen_tokens.c @@ -1,6 +1,10 @@ #include "../incs/minishell.h" +<<<<<<< HEAD +t_tokens *gen_tokens(t_info *info, char *line) +======= t_tokens *gen_tokens(t_info *info, char *set_str) +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 { char *set_fd_res; char *set_fd_res_cpy; @@ -9,11 +13,19 @@ t_tokens *gen_tokens(t_info *info, char *set_str) if (!(ret = (t_tokens *)malloc(sizeof(t_tokens)))) return (NULL); +<<<<<<< HEAD + set_fd_res = set_fd(ret, line); + if (set_fd_res == NULL) + return (NULL); + set_fd_res_cpy = ft_strdup(set_fd_res); + handle_quote(line, &set_fd_res_cpy, ' '); +======= set_fd_res = set_fd(ret, set_str); if (set_fd_res == NULL) return (NULL); set_fd_res_cpy = ft_strdup(set_fd_res); handle_quote(set_str, &set_fd_res_cpy, ' '); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 if (!(tmp_tokens = pk_split(set_fd_res, set_fd_res_cpy, ' ', INF))) return (NULL); ret->tokens = tmp_tokens; diff --git a/srcs/get_next_line_tc.c b/srcs/get_next_line_tc.c index 8c6b232..4820020 100644 --- a/srcs/get_next_line_tc.c +++ b/srcs/get_next_line_tc.c @@ -72,6 +72,8 @@ static int handle_key_arrow( return (ret); } +<<<<<<< HEAD +======= static int handle_sig_in_gnl(long *arr, long *c, int *idx, int *buf_len) { if (g_signal == SIG_SIGINT) @@ -104,6 +106,7 @@ static int print_arr(long *arr) } #endif +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 static int set_inst_arr_in_loop( t_info *info, long *arr) @@ -115,9 +118,15 @@ static int set_inst_arr_in_loop( buf_len = 0; idx = -1; c = 0; +<<<<<<< HEAD + while ((read(STDIN_FILENO, &c, sizeof(c)) > 0) && (c != '\n')) + { + handle_sig_in_gnl(*info, arr); +======= while ((read(STDIN_FILENO, &c, sizeof(c))) >= 0 && (c != '\n')) { handle_sig_in_gnl(arr, &c, &idx, &buf_len); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 get_cursor_pos(&(info->tc.cursor.col), &(info->tc.cursor.row)); if (c <= 4500000) arr[++idx] = c; @@ -125,14 +134,21 @@ static int set_inst_arr_in_loop( ft_putchar_fd(c, STDOUT_FILENO); else if (is_key_arrow(c)) idx = handle_key_arrow(info, arr, c, info->prompt.size + buf_len); +<<<<<<< HEAD + else if (c == KEY_BACKSPACE && --buf_len) +======= else if (c == KEY_BACKSPACE && --buf_len >= 0) +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 ft_cursor_clr_line_end(info->tc, info->prompt.size); c = 0; } if (c == '\n') ft_putstr_fd("\n", STDOUT_FILENO); +<<<<<<< HEAD +======= if (g_signal) handle_sig_in_gnl(arr, &c, &idx, &buf_len); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 return (idx + 1); } @@ -143,14 +159,21 @@ char *get_next_line_tc(t_info *info) long inst_arr[BUFFER_SIZE]; int inst_arr_size; +<<<<<<< HEAD +======= ret = NULL; +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 tc = info->tc; get_cursor_pos(&tc.cursor.col, &tc.cursor.row); ft_cursor_mv_head(tc); write(STDOUT_FILENO, info->prompt.data, info->prompt.size); ft_memset(inst_arr, 0, BUFFER_SIZE); inst_arr_size = set_inst_arr_in_loop(info, inst_arr); +<<<<<<< HEAD + ret = get_str_by_inst_arr(inst_arr, inst_arr_size); +======= if (inst_arr[0] != 0) ret = get_str_by_inst_arr(inst_arr, inst_arr_size); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 return (ret); } diff --git a/srcs/handle_sig_in_gnl.c b/srcs/handle_sig_in_gnl.c index 7aa5e5d..932e183 100644 --- a/srcs/handle_sig_in_gnl.c +++ b/srcs/handle_sig_in_gnl.c @@ -5,11 +5,34 @@ extern int g_signal; static void handle_sigint_in_gnl(int signo) { g_signal = SIG_SIGINT; +<<<<<<< HEAD + write(STDOUT_FILENO, "\n", 1); +======= //prompt >> +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 } static void handle_sigquit_in_gnl(int signo) { +<<<<<<< HEAD + g_signal = SIG_SIGINT; +} + +void handle_sig_in_gnl(t_info info, long *inst_arr) +{ + signal(SIGINT, &handle_sigint_in_gnl); + //signal(SIGQUIT, SIG_IGN); + +#if 0 + if (g_signal == SIG_SIGINT) + { + ft_memset(inst_arr, 0, BUFFER_SIZE); + write(STDOUT_FILENO, "\n", 1); + } +#endif + + g_signal = OFF; +======= g_signal = SIG_SIGQUIT; } @@ -17,4 +40,5 @@ void handle_sig_in_gnl() { signal(SIGINT, &handle_sigint_in_gnl); signal(SIGQUIT, &handle_sigquit_in_gnl); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 } diff --git a/srcs/is_valid_fd.c b/srcs/is_valid_fd.c index 6b584a7..c9235c3 100644 --- a/srcs/is_valid_fd.c +++ b/srcs/is_valid_fd.c @@ -68,4 +68,8 @@ int is_valid_fd( free(file_name); *idx += inc_idx; return (ret); -} \ No newline at end of file +<<<<<<< HEAD +} +======= +} +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 diff --git a/srcs/main.c b/srcs/main.c index 2016cf8..8f62ae5 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -1,4 +1,4 @@ -#include "../incs/minishell.h" +#include "../incs/minishell.h" //memory free about tokens int g_signal; @@ -22,16 +22,25 @@ int run(t_info *info) { if (info->line_list == NULL) { +<<<<<<< HEAD + //handle_sig_init(info); + if ((lines = get_next_line_tc(info)) == NULL) + return -1; +======= handle_sig(); if ((lines = get_next_line_tc(info)) == NULL) continue ; +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 if (exact_strncmp(lines, "") != 0) { append_history_list(&(info->history), lines); info->history_ptr = info->history; } +<<<<<<< HEAD +======= else continue ; +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 if (!(info->line_list = gen_line_list(lines))) return -1; } @@ -46,9 +55,13 @@ int run(t_info *info) } change_head(info); } + +<<<<<<< HEAD return (0); } +======= +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 int main(int argc, char *argv[], char *env[]) { int err_num; @@ -57,6 +70,96 @@ int main(int argc, char *argv[], char *env[]) err_num = 0; init_minishell(&info, env); run(&info); +<<<<<<< HEAD + +#if 0 + t_prompt prompt; + prompt.data = ">> ";//shell💃we🕺dance===> + prompt.size = ft_strlen(prompt.data); + info.prompt = prompt; + //test(&info, prompt); + + char *line; + char *tmp; + while (1) + { + if ((line = get_next_line_tc(&info)) == NULL) + { + printf("EMPTY LINE======================================\n"); + break ; + } + if (exact_strncmp(line,"") != 0) + { + append_history_list(&(info.history), line); + info.history_ptr = info.history; + } + } +#endif +#if 0 + char *str = "echo abc >>text.txt args args2"; + char *str_cpy = "echo abc >>text.txt args args2"; + parse_redir(&info, str, str_cpy); + + + char *set_str = "echo abc \" >> << \"def>> edf"; + + gen_tokens(&info, set_str); +#endif + +#if 0 + int idx_inc = 0; + char *str = " test.txt >> test2.txt"; + int res = is_valid_fd(str, &idx_inc, TYPE_REIN); + printf("result of idx_inc: %d\n", idx_inc); + printf("rest string: %s\n", &str[idx_inc]); + printf("result of res: %d\n", res); +#endif + +#if 0 + char *str = " echo abc >> test.txt >> test2.txt defg hi"; + t_tokens *res = gen_tokens(&info, str); + //print_tokens(res); + +#endif + exit_shell(&info); + return (0); +} + +#if 0 // get_dollar_eval test + char *test = "$HOME abc"; + char *ret = get_dollar_eval(&test, info.env_list); + printf("part: %s\n", test); + printf("ret: %s\n", ret); +#endif + +#if 0 + char arg1[100] = "\"abc$HOME $HOMEdef\"\'abc\'efg"; + //char arg1[100] = "\"abc$HOME $HOMEdef\"\'ghi\'"; + //char arg1[100] = "\"abc def\"\'abc\'"; + char arg2[100] = "\"abc$HOMEdef\""; + char *args[2] = {arg1, arg2}; + sh_bti_echo(args, info.env_list); +#endif + +#if 0 + char arg1[100] = "\"-\""; + char arg2[100] = "\"abc$HOME $HOMEdef\"\'ghi\'"; + char arg3[100] = "\"abc def\"\'abc\'"; + char *args[2] = {NULL, NULL}; + sh_bti_cd(args, info.env_list); +#endif + +#if 0 + char *set_ex = "test=abcd====\"\"==="; + printf("result: %d\n", export_env(&info.env_list, set_ex)); + print_list(info.env_list); + + if ((err_num = run(&info))) + return (-1); + printf("err_num: %d\n", err_num); +#endif +======= exit_shell(&info); return (0); } +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 diff --git a/srcs/run_cmd.c b/srcs/run_cmd.c index f9b61c2..96afb04 100644 --- a/srcs/run_cmd.c +++ b/srcs/run_cmd.c @@ -24,7 +24,11 @@ static void open_redir_fd(t_tokens *curr) if (curr->type & TYPE_REOUT_D) dup2(curr->fd_out[curr->fd_out_idx], STDOUT_FILENO); if (curr->type & TYPE_REIN) +<<<<<<< HEAD + dup2(curr->fd_in[curr->fd_in_idx], STDIN_FILENO); +======= dup2(curr->fd_in[0], STDIN_FILENO); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 } static int close_fds(pid_t pid, t_tokens *curr, t_tokens *prev, int pipe_open) @@ -57,7 +61,10 @@ static int run_cmd_part( int ret; pid = fork(); +<<<<<<< HEAD +======= handle_sig_in_proc(pid); +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 if (pid < 0) return (exit_fatal()); if (pid == 0) diff --git a/srcs/set_fd.c b/srcs/set_fd.c index 0320dbc..48ff25e 100644 --- a/srcs/set_fd.c +++ b/srcs/set_fd.c @@ -9,8 +9,12 @@ static char *show_type_error(char *str) c[0] = str[0]; c[1] = '\0'; show_error(c); +<<<<<<< HEAD + if (str[1] && is_bracket(str[1])) { c[0] = str[1]; +======= if (str[1] && is_bracket(str[1])) { c[0] = str[1]; +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 c[1] = '\0'; show_error(c); } @@ -35,7 +39,11 @@ static int join_char_to_args(char **str, char c, int *idx) return (1); } +<<<<<<< HEAD +static char *set_tokens_type( +======= static char *set_tockens_type( +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 t_tokens *tokens, char *str, char *str_cpy, @@ -78,6 +86,16 @@ char *set_fd(t_tokens *tokens, char *set_str) tokens->type = 0; tokens->fd_in_idx = -1; tokens->fd_out_idx = -1; +<<<<<<< HEAD + ret = set_tokens_type(tokens, set_str, set_str_cpy, &is_valid_fd); + if (ret != NULL) + { + set_tokens_type(tokens, set_str, set_str_cpy, &open_valid_fd); + } + free(set_str_cpy); + return (ret); +} +======= ret = set_tockens_type(tokens, set_str, set_str_cpy, &is_valid_fd); if (ret != NULL) { @@ -85,4 +103,5 @@ char *set_fd(t_tokens *tokens, char *set_str) } free(set_str_cpy); return (ret); -} \ No newline at end of file +} +>>>>>>> df52ce4be677ec57d998a3f41ac1db1c54c05c33 diff --git a/test/pipe_test/a 2.out b/test/pipe_test/a 2.out new file mode 100755 index 0000000..a552570 Binary files /dev/null and b/test/pipe_test/a 2.out differ diff --git a/test/pipe_test/a.out b/test/pipe_test/a.out new file mode 100755 index 0000000..a552570 Binary files /dev/null and b/test/pipe_test/a.out differ diff --git a/test/pipe_test/main1 b/test/pipe_test/main1 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main1 2 b/test/pipe_test/main1 2 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main2 b/test/pipe_test/main2 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main2 2 b/test/pipe_test/main2 2 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main3 b/test/pipe_test/main3 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main3 2 b/test/pipe_test/main3 2 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main4 b/test/pipe_test/main4 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/main4 2 b/test/pipe_test/main4 2 new file mode 100644 index 0000000..e69de29 diff --git a/test/pipe_test/microshell 2.c b/test/pipe_test/microshell 2.c new file mode 100644 index 0000000..0344177 --- /dev/null +++ b/test/pipe_test/microshell 2.c @@ -0,0 +1,288 @@ +#include +#include +#include +#include + +#define SIDE_OUT 0 +#define SIDE_IN 1 + +#define STDIN 0 +#define STDOUT 1 +#define STDERR_FILENO 2 + +#define TYPE_END 0 +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +#ifdef TEST_SH +# define TEST 1 +#else +# define TEST 0 +#endif + +typedef struct s_list +{ + char **args; + int length; + int type; + int pipes[2]; + struct s_list *previous; + struct s_list *next; +} t_list; + +int ft_strlen(char const *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int show_error(char const *str) +{ + if (str) + write(STDERR_FILENO, str, ft_strlen(str)); + return (EXIT_FAILURE); +} + +int exit_fatal(void) +{ + show_error("error: fatal\n"); + exit(EXIT_FAILURE); + return (EXIT_FAILURE); +} + +void *exit_fatal_ptr(void) +{ + exit_fatal(); + exit(EXIT_FAILURE); + return (NULL); +} + +char *ft_strdup(char const *str) +{ + char *copy; + int i; + + if (!(copy = (char*)malloc(sizeof(*copy) * (ft_strlen(str) + 1)))) + return (exit_fatal_ptr()); + i = 0; + while (str[i]) + { + copy[i] = str[i]; + i++; + } + copy[i] = 0; + return (copy); +} + +// make new list and add arguments in new list +int list_push(t_list **list, char *arg) +{ + t_list *new; + int add_arg(); + + if (!(new = (t_list*)malloc(sizeof(*new)))) + return (exit_fatal()); + new->args = NULL; + new->length = 0; + new->type = TYPE_END; + new->previous = NULL; + new->next = NULL; + if (*list) + { + (*list)->next = new; + new->previous = *list; + } + *list = new; + return (add_arg(new, arg)); +} + +int list_rewind(t_list **list) +{ + while (*list && (*list)->previous) + *list = (*list)->previous; + return (EXIT_SUCCESS); +} + +int list_clear(t_list **cmds) +{ + t_list *tmp; + int i; + + list_rewind(cmds); + while (*cmds) + { + tmp = (*cmds)->next; + i = 0; + while (i < (*cmds)->length) + free((*cmds)->args[i++]); + free((*cmds)->args); + free(*cmds); + *cmds = tmp; + } + *cmds = NULL; + return (EXIT_SUCCESS); +} + +int add_arg(t_list *cmd, char *arg) +{ + char **tmp; + int i; + + i = 0; + tmp = NULL; + if (!(tmp = (char**)malloc(sizeof(*tmp) * (cmd->length + 2)))) + return (exit_fatal()); + while (i < cmd->length) + { + tmp[i] = cmd->args[i]; + i++; + } + if (cmd->length > 0) + free(cmd->args); + cmd->args = tmp; + cmd->args[i++] = ft_strdup(arg); + cmd->args[i] = 0; + cmd->length++; +#if 0 + printf("cmd: %s\n", cmd->args[0]); + for (int i = 0; cmd->args[i]; ++i) + printf("arg[%d]: %s\n", i, cmd->args[i]); +#endif + return (EXIT_SUCCESS); +} + +int parse_arg(t_list **cmds, char *arg) +{ + int is_break; + + is_break = (strcmp(";", arg) == 0); + if (is_break && !*cmds) + return (EXIT_SUCCESS); + else if (!is_break && (!*cmds || (*cmds)->type > TYPE_END)) + { + printf("before list_push function\n"); + return (list_push(cmds, arg)); + } + else if (strcmp("|", arg) == 0) + { + printf("pipe case\n"); + (*cmds)->type = TYPE_PIPE; + } + else if (is_break) + { + printf("break case\n"); + (*cmds)->type = TYPE_BREAK; + } + else + { + printf("add_arg function start\n"); + return (add_arg(*cmds, arg)); + } + return (EXIT_SUCCESS); +} + +int exec_cmd(t_list *cmd, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (cmd->type == TYPE_PIPE || (cmd->previous && cmd->previous->type == TYPE_PIPE)) + { + pipe_open = 1; + if (pipe(cmd->pipes)) + return (exit_fatal()); + } + pid = fork(); + if (pid < 0) + return (exit_fatal()); + else if (pid == 0) + { + if (cmd->type == TYPE_PIPE + && dup2(cmd->pipes[SIDE_IN], STDOUT) < 0) + return (exit_fatal()); + if (cmd->previous && cmd->previous->type == TYPE_PIPE + && dup2(cmd->previous->pipes[SIDE_OUT], STDIN) < 0) + return (exit_fatal()); + if ((ret = execve(cmd->args[0], cmd->args, env)) < 0) + { + show_error("error: cannot execute "); + show_error(cmd->args[0]); + show_error("\n"); + } + exit(ret); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(cmd->pipes[SIDE_IN]); + if (!cmd->next || cmd->type == TYPE_BREAK) + close(cmd->pipes[SIDE_OUT]); + } + if (cmd->previous && cmd->previous->type == TYPE_PIPE) + close(cmd->previous->pipes[SIDE_OUT]); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + } + return (ret); +} + +int exec_cmds(t_list **cmds, char **env) +{ + t_list *crt; + int ret; + + ret = EXIT_SUCCESS; + list_rewind(cmds); + while (*cmds) + { + crt = *cmds; + if (strcmp("cd", crt->args[0]) == 0) + { + ret = EXIT_SUCCESS; + if (crt->length < 2) + ret = show_error("error: cd: bad arguments\n"); + else if (chdir(crt->args[1])) + { + ret = show_error("error: cd: cannot change directory to "); + show_error(crt->args[1]); + show_error("\n"); + } + } + else + ret = exec_cmd(crt, env); + if (!(*cmds)->next) + break ; + *cmds = (*cmds)->next; + } + return (ret); +} + +int main(int argc, char **argv, char **env) +{ + t_list *cmds; + int i; + int ret; + + ret = EXIT_SUCCESS; + cmds = NULL; + i = 1; + printf("Program Start\n"); + while (i < argc) + parse_arg(&cmds, argv[i++]); + if (cmds) + ret = exec_cmds(&cmds, env); + list_clear(&cmds); + if (TEST) + while (1); + return (ret); +} diff --git a/test/pipe_test/microshell.c b/test/pipe_test/microshell.c new file mode 100644 index 0000000..0344177 --- /dev/null +++ b/test/pipe_test/microshell.c @@ -0,0 +1,288 @@ +#include +#include +#include +#include + +#define SIDE_OUT 0 +#define SIDE_IN 1 + +#define STDIN 0 +#define STDOUT 1 +#define STDERR_FILENO 2 + +#define TYPE_END 0 +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +#ifdef TEST_SH +# define TEST 1 +#else +# define TEST 0 +#endif + +typedef struct s_list +{ + char **args; + int length; + int type; + int pipes[2]; + struct s_list *previous; + struct s_list *next; +} t_list; + +int ft_strlen(char const *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int show_error(char const *str) +{ + if (str) + write(STDERR_FILENO, str, ft_strlen(str)); + return (EXIT_FAILURE); +} + +int exit_fatal(void) +{ + show_error("error: fatal\n"); + exit(EXIT_FAILURE); + return (EXIT_FAILURE); +} + +void *exit_fatal_ptr(void) +{ + exit_fatal(); + exit(EXIT_FAILURE); + return (NULL); +} + +char *ft_strdup(char const *str) +{ + char *copy; + int i; + + if (!(copy = (char*)malloc(sizeof(*copy) * (ft_strlen(str) + 1)))) + return (exit_fatal_ptr()); + i = 0; + while (str[i]) + { + copy[i] = str[i]; + i++; + } + copy[i] = 0; + return (copy); +} + +// make new list and add arguments in new list +int list_push(t_list **list, char *arg) +{ + t_list *new; + int add_arg(); + + if (!(new = (t_list*)malloc(sizeof(*new)))) + return (exit_fatal()); + new->args = NULL; + new->length = 0; + new->type = TYPE_END; + new->previous = NULL; + new->next = NULL; + if (*list) + { + (*list)->next = new; + new->previous = *list; + } + *list = new; + return (add_arg(new, arg)); +} + +int list_rewind(t_list **list) +{ + while (*list && (*list)->previous) + *list = (*list)->previous; + return (EXIT_SUCCESS); +} + +int list_clear(t_list **cmds) +{ + t_list *tmp; + int i; + + list_rewind(cmds); + while (*cmds) + { + tmp = (*cmds)->next; + i = 0; + while (i < (*cmds)->length) + free((*cmds)->args[i++]); + free((*cmds)->args); + free(*cmds); + *cmds = tmp; + } + *cmds = NULL; + return (EXIT_SUCCESS); +} + +int add_arg(t_list *cmd, char *arg) +{ + char **tmp; + int i; + + i = 0; + tmp = NULL; + if (!(tmp = (char**)malloc(sizeof(*tmp) * (cmd->length + 2)))) + return (exit_fatal()); + while (i < cmd->length) + { + tmp[i] = cmd->args[i]; + i++; + } + if (cmd->length > 0) + free(cmd->args); + cmd->args = tmp; + cmd->args[i++] = ft_strdup(arg); + cmd->args[i] = 0; + cmd->length++; +#if 0 + printf("cmd: %s\n", cmd->args[0]); + for (int i = 0; cmd->args[i]; ++i) + printf("arg[%d]: %s\n", i, cmd->args[i]); +#endif + return (EXIT_SUCCESS); +} + +int parse_arg(t_list **cmds, char *arg) +{ + int is_break; + + is_break = (strcmp(";", arg) == 0); + if (is_break && !*cmds) + return (EXIT_SUCCESS); + else if (!is_break && (!*cmds || (*cmds)->type > TYPE_END)) + { + printf("before list_push function\n"); + return (list_push(cmds, arg)); + } + else if (strcmp("|", arg) == 0) + { + printf("pipe case\n"); + (*cmds)->type = TYPE_PIPE; + } + else if (is_break) + { + printf("break case\n"); + (*cmds)->type = TYPE_BREAK; + } + else + { + printf("add_arg function start\n"); + return (add_arg(*cmds, arg)); + } + return (EXIT_SUCCESS); +} + +int exec_cmd(t_list *cmd, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (cmd->type == TYPE_PIPE || (cmd->previous && cmd->previous->type == TYPE_PIPE)) + { + pipe_open = 1; + if (pipe(cmd->pipes)) + return (exit_fatal()); + } + pid = fork(); + if (pid < 0) + return (exit_fatal()); + else if (pid == 0) + { + if (cmd->type == TYPE_PIPE + && dup2(cmd->pipes[SIDE_IN], STDOUT) < 0) + return (exit_fatal()); + if (cmd->previous && cmd->previous->type == TYPE_PIPE + && dup2(cmd->previous->pipes[SIDE_OUT], STDIN) < 0) + return (exit_fatal()); + if ((ret = execve(cmd->args[0], cmd->args, env)) < 0) + { + show_error("error: cannot execute "); + show_error(cmd->args[0]); + show_error("\n"); + } + exit(ret); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(cmd->pipes[SIDE_IN]); + if (!cmd->next || cmd->type == TYPE_BREAK) + close(cmd->pipes[SIDE_OUT]); + } + if (cmd->previous && cmd->previous->type == TYPE_PIPE) + close(cmd->previous->pipes[SIDE_OUT]); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + } + return (ret); +} + +int exec_cmds(t_list **cmds, char **env) +{ + t_list *crt; + int ret; + + ret = EXIT_SUCCESS; + list_rewind(cmds); + while (*cmds) + { + crt = *cmds; + if (strcmp("cd", crt->args[0]) == 0) + { + ret = EXIT_SUCCESS; + if (crt->length < 2) + ret = show_error("error: cd: bad arguments\n"); + else if (chdir(crt->args[1])) + { + ret = show_error("error: cd: cannot change directory to "); + show_error(crt->args[1]); + show_error("\n"); + } + } + else + ret = exec_cmd(crt, env); + if (!(*cmds)->next) + break ; + *cmds = (*cmds)->next; + } + return (ret); +} + +int main(int argc, char **argv, char **env) +{ + t_list *cmds; + int i; + int ret; + + ret = EXIT_SUCCESS; + cmds = NULL; + i = 1; + printf("Program Start\n"); + while (i < argc) + parse_arg(&cmds, argv[i++]); + if (cmds) + ret = exec_cmds(&cmds, env); + list_clear(&cmds); + if (TEST) + while (1); + return (ret); +} diff --git a/test/pipe_test/pipe_basic 2.c b/test/pipe_test/pipe_basic 2.c new file mode 100644 index 0000000..55b2414 --- /dev/null +++ b/test/pipe_test/pipe_basic 2.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + printf("DEBUG=========================\n"); + dup2(fds[1], STDOUT_FILENO); + write(STDOUT_FILENO, DATA, DATA_LEN); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + return (0); +} diff --git a/test/pipe_test/pipe_basic.c b/test/pipe_test/pipe_basic.c new file mode 100644 index 0000000..55b2414 --- /dev/null +++ b/test/pipe_test/pipe_basic.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + printf("DEBUG=========================\n"); + dup2(fds[1], STDOUT_FILENO); + write(STDOUT_FILENO, DATA, DATA_LEN); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + return (0); +} diff --git a/test/pipe_test/pipe_ex 2.c b/test/pipe_test/pipe_ex 2.c new file mode 100644 index 0000000..5ee5d86 --- /dev/null +++ b/test/pipe_test/pipe_ex 2.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 3 +#define TYPE_REOUT 4 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + + if (tokens->type == TYPE_BREAK) + dup2(tokens->fd, STDIN_FILENO); + + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || tokens->type == TYPE_BREAK) + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + close(tokens->prev->fds[0]); + if (tokens->type == TYPE_BREAK) + close(tokens->fd); + printf("parent process finished==============\n"); + } + return (ret); +} +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + set2.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} +#endif + +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + printf("first run\n"); + run(&set1, env); + return (0); +} +#endif +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_RDONLY); + + run(&set1, env); + return (0); +} diff --git a/test/pipe_test/pipe_ex.c b/test/pipe_test/pipe_ex.c new file mode 100644 index 0000000..5ee5d86 --- /dev/null +++ b/test/pipe_test/pipe_ex.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 3 +#define TYPE_REOUT 4 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + + if (tokens->type == TYPE_BREAK) + dup2(tokens->fd, STDIN_FILENO); + + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || tokens->type == TYPE_BREAK) + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + close(tokens->prev->fds[0]); + if (tokens->type == TYPE_BREAK) + close(tokens->fd); + printf("parent process finished==============\n"); + } + return (ret); +} +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + set2.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} +#endif + +#if 0 +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + printf("first run\n"); + run(&set1, env); + return (0); +} +#endif +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK; + set1.fd = open("test.txt", O_RDONLY); + + run(&set1, env); + return (0); +} diff --git a/test/pipe_test/pipe_redir_ex 2.c b/test/pipe_test/pipe_redir_ex 2.c new file mode 100644 index 0000000..41fb788 --- /dev/null +++ b/test/pipe_test/pipe_redir_ex 2.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 4 +#define TYPE_REOUT 8 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd[2]; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if ((tokens->type & TYPE_PIPE) || (tokens->prev && (tokens->prev->type & TYPE_PIPE))) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + if (tokens->type & TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[0], STDIN_FILENO); + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[1], STDIN_FILENO); + if (tokens->type & TYPE_REOUT) + dup2(tokens->fd[0], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || (tokens->type & TYPE_BREAK)) + close(tokens->fds[0]); + } + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + close(tokens->prev->fds[0]); +#if 1 + if (tokens->type & TYPE_REIN) + close(tokens->fd[0]); + if (tokens->type & TYPE_REIN) + close(tokens->fd[1]); + if (tokens->type & TYPE_REOUT) + close(tokens->fd[0]); +#endif + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK | TYPE_REIN; + set1.fd[0] = open("test.txt", O_RDONLY); + set1.fd[1] = open("test2.txt", O_RDONLY); + +#if 0 + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "a"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK | TYPE_REOUT; + set2.fd = open("test2.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + run(&set1, env); + run(&set2, env); +#endif + run(&set1, env); + + return (0); +} diff --git a/test/pipe_test/pipe_redir_ex.c b/test/pipe_test/pipe_redir_ex.c new file mode 100644 index 0000000..41fb788 --- /dev/null +++ b/test/pipe_test/pipe_redir_ex.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 +#define TYPE_REIN 4 +#define TYPE_REOUT 8 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + int fd[2]; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if ((tokens->type & TYPE_PIPE) || (tokens->prev && (tokens->prev->type & TYPE_PIPE))) + { + pipe_open = 1; + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + if (tokens->type & TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[0], STDIN_FILENO); + if (tokens->type & TYPE_REIN) + dup2(tokens->fd[1], STDIN_FILENO); + if (tokens->type & TYPE_REOUT) + dup2(tokens->fd[0], STDOUT_FILENO); +//////////////////////////////////////////////////////////////////////////////// + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + } + else + { + waitpid(pid, &status, 0); + if (pipe_open) + { + close(tokens->fds[1]); + if (!tokens->next || (tokens->type & TYPE_BREAK)) + close(tokens->fds[0]); + } + if (tokens->prev && (tokens->prev->type & TYPE_PIPE)) + close(tokens->prev->fds[0]); +#if 1 + if (tokens->type & TYPE_REIN) + close(tokens->fd[0]); + if (tokens->type & TYPE_REIN) + close(tokens->fd[1]); + if (tokens->type & TYPE_REOUT) + close(tokens->fd[0]); +#endif + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/usr/bin/sort"; + set1.args[1] = NULL; + set1.type = TYPE_BREAK | TYPE_REIN; + set1.fd[0] = open("test.txt", O_RDONLY); + set1.fd[1] = open("test2.txt", O_RDONLY); + +#if 0 + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "a"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK | TYPE_REOUT; + set2.fd = open("test2.txt", O_CREAT | O_APPEND | O_WRONLY, 0755); + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + run(&set1, env); + run(&set2, env); +#endif + run(&set1, env); + + return (0); +} diff --git a/test/pipe_test/redir_ex 2.c b/test/pipe_test/redir_ex 2.c new file mode 100644 index 0000000..abab671 --- /dev/null +++ b/test/pipe_test/redir_ex 2.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +#if 0 +void add_args(char **args, char **buf) { + int i; + i = 1; + while (buf[i - 1]) + { + args[i] = buf[i - 1]; + ++i; + } + args[i] = NULL; +} +#endif + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + // both set1 and set2 use pipe function to connect fds[0], fds[1] + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + // fds[1] of set1 has result of ls -al + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + // fds[0] of set1 has input of ls -al for grep + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + // close fds[1] of set1 + // close fds[1] of set2 + close(tokens->fds[1]); + // fds[0] of set1 is still open + if (!tokens->next || tokens->type == TYPE_BREAK) + // fds[0] of set2 is closed + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + // fds[0] of set1 is closed + close(tokens->prev->fds[0]); + printf("parent process finished==============\n"); + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} + +#if 0 +int run1(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[0], STDOUT_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[1]); + } + return (0); +} + +int run2(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[1], STDIN_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[0]); + } + return (0); +} +#endif diff --git a/test/pipe_test/redir_ex.c b/test/pipe_test/redir_ex.c new file mode 100644 index 0000000..abab671 --- /dev/null +++ b/test/pipe_test/redir_ex.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include + +#define TYPE_PIPE 1 +#define TYPE_BREAK 2 + +typedef struct s_set +{ + char *args[30]; + int fds[2]; + int type; + struct s_set *next; + struct s_set *prev; +} t_tokens; + +#if 0 +void add_args(char **args, char **buf) { + int i; + i = 1; + while (buf[i - 1]) + { + args[i] = buf[i - 1]; + ++i; + } + args[i] = NULL; +} +#endif + +int run(t_tokens *tokens, char **env) +{ + pid_t pid; + int ret; + int status; + int pipe_open; + + ret = EXIT_FAILURE; + pipe_open = 0; + if (tokens->type == TYPE_PIPE || (tokens->prev && tokens->prev->type == TYPE_PIPE)) + { + pipe_open = 1; + // both set1 and set2 use pipe function to connect fds[0], fds[1] + pipe(tokens->fds); + } + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + // fds[1] of set1 has result of ls -al + if (tokens->type == TYPE_PIPE) + dup2(tokens->fds[1], STDOUT_FILENO); + // fds[0] of set1 has input of ls -al for grep + if (tokens->prev && (tokens->prev->type == TYPE_PIPE)) + dup2(tokens->prev->fds[0], STDIN_FILENO); + if ((ret = execve(tokens->args[0], tokens->args, env) < 0)) + exit(0); + printf("child process finished===============\n"); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + if (pipe_open) + { + // close fds[1] of set1 + // close fds[1] of set2 + close(tokens->fds[1]); + // fds[0] of set1 is still open + if (!tokens->next || tokens->type == TYPE_BREAK) + // fds[0] of set2 is closed + close(tokens->fds[0]); + } + if (tokens->prev && tokens->prev->type == TYPE_PIPE) + // fds[0] of set1 is closed + close(tokens->prev->fds[0]); + printf("parent process finished==============\n"); + } + return (ret); +} + +int main(int argc, char *argv[], char *env[]) { + t_tokens set1; + set1.args[0] = "/bin/ls"; + set1.args[1] = "-al"; + set1.args[2] = NULL; + set1.type = TYPE_PIPE; + + t_tokens set2; + set2.args[0] = "/usr/bin/grep"; + set2.args[1] = "main"; + set2.args[2] = NULL; + set2.type = TYPE_BREAK; + + set1.prev = NULL; + set1.next = &set2; + set2.prev = &set1; + set2.next = NULL; + printf("first run\n"); + run(&set1, env); + printf("second run\n"); + run(&set2, env); + return (0); +} + +#if 0 +int run1(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[0], STDOUT_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[1]); + } + return (0); +} + +int run2(t_tokens *tokens, char **env) +{ + pipe(tokens->fds); + pid_t pid; + int status; + + pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) + { + printf("child process========================\n"); + print_tokens(tokens); + dup2(tokens->fds[1], STDIN_FILENO); + if (execve(tokens->args[0], tokens->args, env) < 0) + exit(0); + } + else + { + printf("parent process=======================\n"); + waitpid(pid, &status, 0); + close(tokens->fds[0]); + } + return (0); +} +#endif diff --git a/test/pipe_test/test 2.c b/test/pipe_test/test 2.c new file mode 100644 index 0000000..80873d9 --- /dev/null +++ b/test/pipe_test/test 2.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + pid_t pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) { // child process + close(fds[0]); + write(fds[1], DATA, DATA_LEN); + } + else { // parents process + waitpid(pid, &status, 0); + close(fds[1]); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + close(fds[0]); + } + return (0); +} diff --git a/test/pipe_test/test 2.txt b/test/pipe_test/test 2.txt new file mode 100644 index 0000000..30908be --- /dev/null +++ b/test/pipe_test/test 2.txt @@ -0,0 +1,4 @@ +apple +banana +mango +pineapple diff --git a/test/pipe_test/test.c b/test/pipe_test/test.c new file mode 100644 index 0000000..80873d9 --- /dev/null +++ b/test/pipe_test/test.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +#define DATA "Hello World!\n" +#define DATA_LEN 13 + +int main() { + int fds[2]; + char buf[1024]; + int status; + + pipe(fds); + pid_t pid = fork(); + if (pid < 0) + return (-1); + else if (pid == 0) { // child process + close(fds[0]); + write(fds[1], DATA, DATA_LEN); + } + else { // parents process + waitpid(pid, &status, 0); + close(fds[1]); + int read_size = read(fds[0], buf, DATA_LEN); + for (int i = 0; i < read_size; ++i) + write(STDOUT_FILENO, &buf[i], 1); + close(fds[0]); + } + return (0); +} diff --git a/test/pipe_test/test.txt b/test/pipe_test/test.txt new file mode 100644 index 0000000..30908be --- /dev/null +++ b/test/pipe_test/test.txt @@ -0,0 +1,4 @@ +apple +banana +mango +pineapple diff --git a/test/pipe_test/test2 2.txt b/test/pipe_test/test2 2.txt new file mode 100644 index 0000000..89eb6b9 --- /dev/null +++ b/test/pipe_test/test2 2.txt @@ -0,0 +1,3 @@ +orange +grape +tomato diff --git a/test/pipe_test/test2.txt b/test/pipe_test/test2.txt new file mode 100644 index 0000000..89eb6b9 --- /dev/null +++ b/test/pipe_test/test2.txt @@ -0,0 +1,3 @@ +orange +grape +tomato diff --git a/test/termcap_test/a 2.out b/test/termcap_test/a 2.out new file mode 100755 index 0000000..132a4f9 Binary files /dev/null and b/test/termcap_test/a 2.out differ diff --git a/test/termcap_test/a.out b/test/termcap_test/a.out new file mode 100755 index 0000000..132a4f9 Binary files /dev/null and b/test/termcap_test/a.out differ diff --git a/test/termcap_test/cursor 2.c b/test/termcap_test/cursor 2.c new file mode 100644 index 0000000..fd6fdf0 --- /dev/null +++ b/test/termcap_test/cursor 2.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +#include + +#define LEFT_ARROW 4479771 +#define RIGHT_ARROW 4414235 + +int ft_putchar_tc(int tc) +{ + write(STDOUT_FILENO, &tc, 1); + return (0); +} + +int ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} + +int ft_strlen(char *str) +{ + int ret; + + ret = 0; + while (str[ret]) + ++ret; + return (ret); +} + +void get_cursor_pos(int *row, int *col) +{ + int cnt; + int idx; + char buf[255]; + int read_size; + + write(STDIN_FILENO, "\033[6n", ft_strlen("\033[6n")); + read_size = read(STDIN_FILENO, buf, 255); + buf[read_size] = '\0'; + idx = -1; + while (!ft_isdigit(buf[idx])) + ++idx; + *row = 0; + while (ft_isdigit(buf[idx])) + *row = *row * 10 + buf[idx++] - '0'; + ++idx; + *col = 0; + while (ft_isdigit(buf[idx])) + *col = *col * 10 + buf[idx++] - '0'; +} + +void move_cursor_left() +{ + write(STDIN_FILENO, "\033[D", 4); +} + +void move_cursor_right() +{ + write(STDIN_FILENO, "\033[C", 4); +} + +int main() +{ + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif +#if 0 + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#endif +#if 1 + int col; + int row; + char *prompt; + + long c; + prompt = ">> "; + while (1) + { + write(1, prompt, ft_strlen(prompt)); + c = 0; + while (read(0, &c, sizeof(c)) > 0) + { + //printf("keycode: %ld\n", c); + if (c == '\n') + { + printf("DEBUG=========================\n"); + break ; + } + get_cursor_pos(&row, &col); + if (col > 3 && c == LEFT_ARROW) + write(STDIN_FILENO, "\033[D", ft_strlen("\033[D")); + else if (c == RIGHT_ARROW) + write(STDIN_FILENO, "\033[C", ft_strlen("\033[C")); + else + write(1, &c, 1); + } + } +#endif +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/test/termcap_test/cursor.c b/test/termcap_test/cursor.c new file mode 100644 index 0000000..fd6fdf0 --- /dev/null +++ b/test/termcap_test/cursor.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +#include + +#define LEFT_ARROW 4479771 +#define RIGHT_ARROW 4414235 + +int ft_putchar_tc(int tc) +{ + write(STDOUT_FILENO, &tc, 1); + return (0); +} + +int ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} + +int ft_strlen(char *str) +{ + int ret; + + ret = 0; + while (str[ret]) + ++ret; + return (ret); +} + +void get_cursor_pos(int *row, int *col) +{ + int cnt; + int idx; + char buf[255]; + int read_size; + + write(STDIN_FILENO, "\033[6n", ft_strlen("\033[6n")); + read_size = read(STDIN_FILENO, buf, 255); + buf[read_size] = '\0'; + idx = -1; + while (!ft_isdigit(buf[idx])) + ++idx; + *row = 0; + while (ft_isdigit(buf[idx])) + *row = *row * 10 + buf[idx++] - '0'; + ++idx; + *col = 0; + while (ft_isdigit(buf[idx])) + *col = *col * 10 + buf[idx++] - '0'; +} + +void move_cursor_left() +{ + write(STDIN_FILENO, "\033[D", 4); +} + +void move_cursor_right() +{ + write(STDIN_FILENO, "\033[C", 4); +} + +int main() +{ + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif +#if 0 + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#endif +#if 1 + int col; + int row; + char *prompt; + + long c; + prompt = ">> "; + while (1) + { + write(1, prompt, ft_strlen(prompt)); + c = 0; + while (read(0, &c, sizeof(c)) > 0) + { + //printf("keycode: %ld\n", c); + if (c == '\n') + { + printf("DEBUG=========================\n"); + break ; + } + get_cursor_pos(&row, &col); + if (col > 3 && c == LEFT_ARROW) + write(STDIN_FILENO, "\033[D", ft_strlen("\033[D")); + else if (c == RIGHT_ARROW) + write(STDIN_FILENO, "\033[C", ft_strlen("\033[C")); + else + write(1, &c, 1); + } + } +#endif +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/test/termcap_test/ft_putchar_tc 2.c b/test/termcap_test/ft_putchar_tc 2.c new file mode 100644 index 0000000..22164b7 --- /dev/null +++ b/test/termcap_test/ft_putchar_tc 2.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + char c; + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/test/termcap_test/ft_putchar_tc.c b/test/termcap_test/ft_putchar_tc.c new file mode 100644 index 0000000..22164b7 --- /dev/null +++ b/test/termcap_test/ft_putchar_tc.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + char c; + long d; + struct termios term; +#if 1 + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); +#endif + while (read(STDIN_FILENO, &c, sizeof(c)) > 0) { + printf("keycode: %c\n", c); + } +#if 0 + while (read(STDIN_FILENO, &d, sizeof(d)) > 0) { + printf("keycode: %ld\n", d); + } +#endif + return (0); +} diff --git a/test/termcap_test/test 2.c b/test/termcap_test/test 2.c new file mode 100644 index 0000000..03e93f0 --- /dev/null +++ b/test/termcap_test/test 2.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +struct termios old_settings; + +int main() +{ + struct termios new_settings; + + int vdisable; + char buf[255]; +#if 0 + if ((vdisable = fpathconf(0, _PC_VDISABLE)) < 0) { + perror("error "); + exit(0); + } + printf("vdisable: %d\n", vdisable); +#endif + + tcgetattr(0, &new_settings); + old_settings = new_settings; + new_settings.c_cc[VEOF] = 255; + tcsetattr(0, TCSAFLUSH, &new_settings); + memset(buf, 0x00, 255); + read(0, buf, 255); + printf("%s\n", buf); + tcsetattr(0, TCSAFLUSH, &old_settings); + exit(0); + return (0); +} diff --git a/test/termcap_test/test.c b/test/termcap_test/test.c new file mode 100644 index 0000000..03e93f0 --- /dev/null +++ b/test/termcap_test/test.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +struct termios old_settings; + +int main() +{ + struct termios new_settings; + + int vdisable; + char buf[255]; +#if 0 + if ((vdisable = fpathconf(0, _PC_VDISABLE)) < 0) { + perror("error "); + exit(0); + } + printf("vdisable: %d\n", vdisable); +#endif + + tcgetattr(0, &new_settings); + old_settings = new_settings; + new_settings.c_cc[VEOF] = 255; + tcsetattr(0, TCSAFLUSH, &new_settings); + memset(buf, 0x00, 255); + read(0, buf, 255); + printf("%s\n", buf); + tcsetattr(0, TCSAFLUSH, &old_settings); + exit(0); + return (0); +} diff --git a/test/termcap_test/testtest 2.c b/test/termcap_test/testtest 2.c new file mode 100644 index 0000000..ba5ea89 --- /dev/null +++ b/test/termcap_test/testtest 2.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +# define BACKSPACE 127 +# define LEFT_ARROW 4479771 +# define RIGHT_ARROW 4414235 + +int nbr_length(int n) +{ + int i = 0; + + if (n <= 0) + i++; + while (n != 0) + { + n /= 10; + i++; + } + return (i); +} + +void get_cursor_position(int *col, int *rows) +{ + int a = 0; + int i = 1; + char buf[255]; + int ret; + int temp; + + write(0, "\033[6n", 4); //report cursor location + ret = read(0, buf, 254); + buf[ret] = '\0'; + while (buf[i]) + { + if (buf[i] >= '0' && buf[i] <= '9') + { + if (a == 0) + *rows = atoi(&buf[i]) - 1; + else + { + temp = atoi(&buf[i]); + *col = temp - 1; + } + a++; + i += nbr_length(temp) - 1; + } + i++; + } +} + +int putchar_tc(int tc) +{ + write(1, &tc, 1); + return (0); +} + +void move_cursor_left(int *col, int *row, char *cm) +{ + if (*col == 0) + return ; + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void move_cursor_right(int *col, int *row, char *cm) +{ + ++(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void delete_end(int *col, int *row, char *cm, char *ce) +{ + if (*col != 0) + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + tputs(ce, 1, putchar_tc); +} + +int main(void) +{ + /* change term settings */ + struct termios term; + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + /* init termcap */ + tgetent(NULL, "xterm"); + char *cm = tgetstr("cm", NULL); //cursor motion + char *ce = tgetstr("ce", NULL); //clear line from cursor + + int c = 0; + int row; + int col; + + while (1) + { + write(1, ">> ", 3); + while (read(0, &c, sizeof(c)) > 0) + { + if (c == '\n') + { + write(1, "\n", 1); + break; + } + get_cursor_position(&col, &row); + if (c == LEFT_ARROW) + move_cursor_left(&col, &row, cm); + else if (c == RIGHT_ARROW) + move_cursor_right(&col, &row, cm); + else if (c == BACKSPACE) + delete_end(&col, &row, cm, ce); + else + { + col++; + write(0, &c, 1); + } + c = 0; //flush buffer + } + } +} diff --git a/test/termcap_test/testtest.c b/test/termcap_test/testtest.c new file mode 100644 index 0000000..ba5ea89 --- /dev/null +++ b/test/termcap_test/testtest.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +# define BACKSPACE 127 +# define LEFT_ARROW 4479771 +# define RIGHT_ARROW 4414235 + +int nbr_length(int n) +{ + int i = 0; + + if (n <= 0) + i++; + while (n != 0) + { + n /= 10; + i++; + } + return (i); +} + +void get_cursor_position(int *col, int *rows) +{ + int a = 0; + int i = 1; + char buf[255]; + int ret; + int temp; + + write(0, "\033[6n", 4); //report cursor location + ret = read(0, buf, 254); + buf[ret] = '\0'; + while (buf[i]) + { + if (buf[i] >= '0' && buf[i] <= '9') + { + if (a == 0) + *rows = atoi(&buf[i]) - 1; + else + { + temp = atoi(&buf[i]); + *col = temp - 1; + } + a++; + i += nbr_length(temp) - 1; + } + i++; + } +} + +int putchar_tc(int tc) +{ + write(1, &tc, 1); + return (0); +} + +void move_cursor_left(int *col, int *row, char *cm) +{ + if (*col == 0) + return ; + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void move_cursor_right(int *col, int *row, char *cm) +{ + ++(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + +} + +void delete_end(int *col, int *row, char *cm, char *ce) +{ + if (*col != 0) + --(*col); + tputs(tgoto(cm, *col, *row), 1, putchar_tc); + tputs(ce, 1, putchar_tc); +} + +int main(void) +{ + /* change term settings */ + struct termios term; + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~ICANON; + term.c_lflag &= ~ECHO; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + /* init termcap */ + tgetent(NULL, "xterm"); + char *cm = tgetstr("cm", NULL); //cursor motion + char *ce = tgetstr("ce", NULL); //clear line from cursor + + int c = 0; + int row; + int col; + + while (1) + { + write(1, ">> ", 3); + while (read(0, &c, sizeof(c)) > 0) + { + if (c == '\n') + { + write(1, "\n", 1); + break; + } + get_cursor_position(&col, &row); + if (c == LEFT_ARROW) + move_cursor_left(&col, &row, cm); + else if (c == RIGHT_ARROW) + move_cursor_right(&col, &row, cm); + else if (c == BACKSPACE) + delete_end(&col, &row, cm, ce); + else + { + col++; + write(0, &c, 1); + } + c = 0; //flush buffer + } + } +} diff --git a/test1.txt b/test1.txt new file mode 100644 index 0000000..ed1374a --- /dev/null +++ b/test1.txt @@ -0,0 +1,4 @@ +d +c +b +a diff --git a/test2.txt b/test2.txt new file mode 100644 index 0000000..5f29e3a --- /dev/null +++ b/test2.txt @@ -0,0 +1,3 @@ +apple +banana +grape