diff --git a/42sh/.gitignore b/42sh/.gitignore index 2c239b7d..a0b5f8cc 100644 --- a/42sh/.gitignore +++ b/42sh/.gitignore @@ -3,6 +3,7 @@ minishell 42sh out debug +.42sh_history *.dSYM .script.sh.swp *.swp diff --git a/42sh/Makefile b/42sh/Makefile index 8dcf5189..e240d66b 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -1,90 +1,251 @@ -NAME = minishell -CC = gcc -TAGFILE = .tags +#* ************************************************************************** *# +#* *# +#* ::: :::::::: *# +#* Makefile :+: :+: :+: *# +#* +:+ +:+ +:+ *# +#* By: wescande +#+ +:+ +#+ *# +#* +#+#+#+#+#+ +#+ *# +#* Created: 2016/08/29 21:32:58 by wescande #+# #+# *# +#* Updated: 2016/09/27 20:14:55 by wescande ### ########.fr *# +#* *# +#* ************************************************************************** *# -D_SRC = src -F_SRC := $(shell find $(D_SRC) -type f -regex ".*\.c$$") +NAME = 42sh -D_OBJ = obj -F_OBJ = $(notdir $(F_SRC:.c=.o)) -DF_OBJ := $(addprefix $(D_OBJ)/, $(F_OBJ)) +CC = gcc +FLAGS = -Wall -Wextra -Werror +D_FLAGS = -g -D_INC = includes libft/includes -F_INC := $(shell find $(D_INC) -type f -regex ".*\.h$$") -O_INC = $(addprefix -I, $(D_INC)) +DELTA = $$(echo "$$(tput cols)-47"|bc) -D_SER = libft/ -O_SER = $(addprefix -L, $(D_SER)) +LIBFT_DIR = libft/ +LIBFT_LIB = $(LIBFT_DIR)libft.a +LIBFT_INC = $(LIBFT_DIR)includes/ -D_LIB = ft ncurses -O_LIB = $(addprefix -l, $(D_LIB)) +LIBS = -ltermcap -W_FLAGS = -Wall -Wextra -Werror -D_FLAGS = -g +SRC_DIR = src/ +INC_DIR = includes/ +OBJ_DIR = objs/ -MKDIR = mkdir -p -RM = /bin/rm -rf +SRC_BASE = \ +builtin/bt_read_get.c\ +builtin/bt_read_parse.c\ +builtin/bt_read_term.c\ +builtin/builtin_cd.c\ +builtin/builtin_echo.c\ +builtin/builtin_env.c\ +builtin/builtin_exit.c\ +builtin/builtin_read.c\ +builtin/builtin_setenv.c\ +builtin/builtin_unsetenv.c\ +builtin/is_builtin.c\ +exec/ast_free.c\ +exec/exec_ampersand.c\ +exec/exec_and_if.c\ +exec/exec_command.c\ +exec/exec_dgreat.c\ +exec/exec_great.c\ +exec/exec_less.c\ +exec/exec_or_if.c\ +exec/exec_pipe.c\ +exec/exec_semi.c\ +exec/ft_exec.c\ +exec/ft_findexec.c\ +exec/launch_process.c\ +exec/process_redirect.c\ +exec/process_reset.c\ +exec/process_setexec.c\ +exec/process_setgroup.c\ +exec/process_setsig.c\ +exec/set_exitstatus.c\ +glob/dir_glob.c\ +glob/expand_brace.c\ +glob/expand_esc.c\ +glob/ft_strsplit_esc.c\ +glob/ft_strsplit_spe.c\ +glob/glob.c\ +glob/is_char_esc.c\ +glob/lib_perso/ft_ld_back.c\ +glob/lib_perso/ft_ld_clear.c\ +glob/lib_perso/ft_ld_del.c\ +glob/lib_perso/ft_ld_front.c\ +glob/lib_perso/ft_ld_new.c\ +glob/lib_perso/ft_ld_order.c\ +glob/lib_perso/ft_ld_pushback.c\ +glob/lib_perso/ft_ld_pushfront.c\ +glob/lib_perso/ft_ld_reverse.c\ +glob/lib_perso/ft_ld_size.c\ +glob/lib_perso/ft_ld_swap.c\ +glob/lib_perso/ft_ld_to_tab.c\ +glob/lib_perso/ft_strjoinf.c\ +glob/lib_perso/ft_tabdel.c\ +glob/lib_perso/ft_tablen.c\ +glob/match_pattern.c\ +job-control/builtin_bg.c\ +job-control/builtin_fg.c\ +job-control/builtin_jobs.c\ +job-control/do_job_notification.c\ +job-control/job_addprocess.c\ +job-control/job_cmp_id.c\ +job-control/job_format.c\ +job-control/job_format_head.c\ +job-control/job_free.c\ +job-control/job_getprocess.c\ +job-control/job_getrank.c\ +job-control/job_is_completed.c\ +job-control/job_is_stopped.c\ +job-control/job_kill_all.c\ +job-control/job_notify_change.c\ +job-control/job_notify_new.c\ +job-control/job_remove.c\ +job-control/job_run.c\ +job-control/job_update_id.c\ +job-control/job_update_status.c\ +job-control/job_wait.c\ +job-control/mark_job_as_running.c\ +job-control/process_cmp_pid.c\ +job-control/process_format.c\ +job-control/process_free.c\ +job-control/process_mark_status.c\ +job-control/put_job_in_background.c\ +job-control/put_job_in_foreground.c\ +job-control/sigchld_handler.c\ +job-control/sigint_handler.c\ +job-control/sigtstp_handler.c\ +job-control/sigttin_handler.c\ +job-control/sigttou_handler.c\ +lexer/command_getoutput.c\ +lexer/ft_lexer.c\ +lexer/ft_post_tokenize.c\ +lexer/ft_tokenize.c\ +lexer/get_lexer_state.c\ +lexer/lexer_backslash.c\ +lexer/lexer_default.c\ +lexer/lexer_delim.c\ +lexer/lexer_dquote.c\ +lexer/lexer_great.c\ +lexer/lexer_greatand.c\ +lexer/lexer_less.c\ +lexer/lexer_lessand.c\ +lexer/lexer_number.c\ +lexer/lexer_quote.c\ +lexer/lexer_sep.c\ +lexer/lexer_special.c\ +lexer/lexer_var.c\ +lexer/lexer_word.c\ +lexer/reduce_bquotes.c\ +lexer/reduce_parens.c\ +lexer/token_append.c\ +lexer/token_cmp_type.c\ +lexer/token_expand_var.c\ +lexer/token_free.c\ +lexer/token_init.c\ +lexer/token_print.c\ +line-editing/builtin_history.c\ +line-editing/check_backslash.c\ +line-editing/control_c_and_d.c\ +line-editing/copy_cut_paste.c\ +line-editing/ft_split_whitespaces.c\ +line-editing/get_touch.c\ +line-editing/get_touch_toolz.c\ +line-editing/get_touch_toolz_2.c\ +line-editing/heredoc.c\ +line-editing/history.c\ +line-editing/history_parsing.c\ +line-editing/history_parsing_toolz.c\ +line-editing/history_parsing_toolz_2.c\ +line-editing/list_toolz.c\ +line-editing/move_to_line.c\ +line-editing/print_and_del.c\ +line-editing/prompt.c\ +line-editing/quotes_gest.c\ +line-editing/reader.c\ +line-editing/readline.c\ +line-editing/surch_in_history.c\ +line-editing/tool_line.c\ +line-editing/tool_line_2.c\ +line-editing/toolz.c\ +line-editing/toolz2.c\ +line-editing/toolz_parseur.c\ +line-editing/toolz_termcaps.c\ +main/data_exit.c\ +main/data_init.c\ +main/data_singleton.c\ +main/ft_cleanup.c\ +main/ft_putast.c\ +main/ft_putast2.c\ +main/lib_expansion.c\ +main/main.c\ +main/shell_exit.c\ +main/shell_get_avdata.c\ +main/shell_get_opts.c\ +main/shell_init.c\ +main/sig_handler.c\ +parser/ft_parse.c\ +parser/parse_dgreat.c\ +parser/parse_dless.c\ +parser/parse_great.c\ +parser/parse_greatand.c\ +parser/parse_less.c\ +parser/parse_lessand.c\ +parser/parse_separator.c\ +parser/parse_subshell.c\ +parser/parse_word.c -.PHONY: all clean fclean re +SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) +OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) +NB = $(words $(SRC_BASE)) +INDEX = 0 -all: $(NAME) +all : + @make -j $(NAME) -$(NAME): libft/libft.a $(DF_OBJ) - $(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) +$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) + @$(CC) $(FLAGS) $(D_FLAGS) \ + -I $(INC_DIR) \ + -I $(LIBFT_INC) \ + $(LIBS) \ + $(LIBFT_LIB) $(OBJS) \ + -o $(NAME) + @echo "\r\033[48;5;15;38;5;25m✅ MAKE $(NAME)\033[0m\033[K" -$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +$(LIBFT_LIB): + @make -j -C $(LIBFT_DIR) -$(D_OBJ)/%.o: $(D_SRC)/builtin/%.c includes/minishell.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +$(OBJ_DIR) : + @mkdir -p $(OBJ_DIR) + @mkdir -p $(dir $(OBJS)) -$(D_OBJ)/%.o: $(D_SRC)/minishell-exec/%.c includes/minishell.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) + @$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB))))) + @$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB))))) + @$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196)))) + @$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB))))) + @printf "\r\033[38;5;11m⌛ MAKE %10.10s : %2d%% \033[48;5;%dm%*s\033[0m%*s\033[48;5;255m \033[0m \033[38;5;11m %*s\033[0m\033[K" $(NAME) $(PERCENT) $(COLOR) $(DONE) "" $(TO_DO) "" $(DELTA) "$@" + @$(CC) $(FLAGS) -MMD -c $< -o $@\ + -I $(INC_DIR)\ + -I $(LIBFT_INC) + @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) -$(D_OBJ)/%.o: $(D_SRC)/line-editing/%.c includes/ft_readline.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +clean: cleanlib + @rm -rf $(OBJ_DIR) + @echo "\r\033[38;5;202m✖ clean $(NAME).\033[0m\033[K" -$(D_OBJ)/%.o: $(D_SRC)/lexer/%.c includes/lexer.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +cleanlib: + @make -C $(LIBFT_DIR) clean -$(D_OBJ)/%.o: $(D_SRC)/parser/%.c includes/parser.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +fclean: clean fcleanlib + @rm -f $(NAME) + @echo "\r\033[38;5;196m❌ fclean $(NAME).\033[0m\033[K" -$(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +fcleanlib: cleanlib + @make -C $(LIBFT_DIR) fclean -$(D_OBJ)/%.o: $(D_SRC)/job-control/%.c includes/job_control.h - @$(MKDIR) $(D_OBJ) - @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) - @echo "Compiling "$<"..." +re: fclean all -libft/libft.a: libft/src/*/*.c - @echo "libft/libft.a" - @$(MAKE) -C libft 2>/dev/null +relib: fcleanlib $(LIBFT_LIB) -libft: - @echo "libft" - @$(MAKE) -C libft 2>/dev/null +.PHONY : fclean clean re relib cleanlib fcleanlib -clean: - $(RM) $(D_OBJ) - -fclean: clean - $(RM) $(NAME) - @$(MAKE) fclean -C libft/ 2>/dev/null - -re: fclean all +-include $(OBJS:.o=.d) diff --git a/42sh/includes/btree.h b/42sh/includes/btree.h new file mode 100644 index 00000000..5839155c --- /dev/null +++ b/42sh/includes/btree.h @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/16 11:13:15 by jhalford #+# #+# */ +/* Updated: 2016/12/05 11:53:30 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BTREE_H +# define BTREE_H + +# include "libft.h" + +struct s_btree +{ + void *item; + size_t content_size; + struct s_btree *left; + struct s_btree *right; +}; + +struct s_printdata +{ + int is_left; + int offset; + int depth; + int left; + int right; +}; + +typedef struct s_btree t_btree; +typedef struct s_printdata t_printdata; + +t_btree *btree_create_node(void const *item, size_t content_size); +void btree_insert_data( + t_btree **root, + void *item, + size_t content_size, + int (*cmpf)(void *, void *)); +void *btree_search_item(t_btree *root, + void *data_ref, int (*cmpf)(void *, void *)); +int btree_level_count(t_btree *root); +void btree_apply_prefix(t_btree *root, void (*applyf)(void *)); +void btree_apply_infix(t_btree *root, void (*applyf)(void *)); +void btree_apply_suffix(t_btree *root, void (*applyf)(void *)); +void btree_print(int fd, t_btree *tree, char *(*printer)(void *)); +void btree_del(t_btree **root, void (*del)(void *, size_t)); +void btree_delone(t_btree **root, void (*del)(void *, size_t)); + +#endif diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index b1c2c834..265c5f9c 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/01/25 15:49:26 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:44:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # include "types.h" # include "libft.h" +# include "builtin_read.h" t_execf *is_builtin(t_process *p); int builtin_env(const char *path, char *const argv[], char *const envp[]); @@ -26,5 +27,6 @@ int builtin_unsetenv(const char *path, char *const argv[], char *const envp[]); int builtin_jobs(const char *path, char *const av[], char *const envp[]); int builtin_fg(const char *path, char *const av[], char *const envp[]); int builtin_bg(const char *path, char *const av[], char *const envp[]); +int builtin_history(const char *path, char *const av[], char *const envp[]); #endif diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h new file mode 100644 index 00000000..177f9176 --- /dev/null +++ b/42sh/includes/builtin_read.h @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_read.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ +/* Updated: 2017/01/27 18:56:58 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTIN_READ_H +# define BUILTIN_READ_H + +# include "types.h" +# include "libft.h" +# include "builtin.h" +# include "minishell.h" + +# define READ_OPT_LA (1 << 0) +# define READ_OPT_LD (1 << 1) +# define READ_OPT_LE (1 << 2) +# define READ_OPT_LI (1 << 3) +# define READ_OPT_LN (1 << 4) +# define READ_OPT_UN (1 << 5) +# define READ_OPT_LP (1 << 6) +# define READ_OPT_LR (1 << 7) +# define READ_OPT_LS (1 << 8) +# define READ_OPT_LT (1 << 9) +# define READ_OPT_LU (1 << 10) + +typedef struct s_read t_read; +typedef struct s_readopt t_readopt; + +struct s_read +{ + t_flag opts; + char delim; + int nchars; + char *prompt; + int timeout; + int fd; + char **names; + char *input; +}; + +struct s_readopt +{ + char letter; + t_flag flag; + int (*get)(t_read *data, char *arg); +}; + +extern t_readopt g_readtab[]; + +int builtin_read(const char *path, char *const av[], char *const envp[]); + +int bt_read_init(t_read *data, char **av); + +struct termios bt_read_term(int init); +int bt_read_terminit(t_read *data); +int bt_read_exit(t_read *data); + +int bt_read_getdelim(t_read *data, char *arg); +int bt_read_getnchars(t_read *data, char *arg); +int bt_read_getprompt(t_read *data, char *arg); +int bt_read_gettimeout(t_read *data, char *arg); +int bt_read_getfd(t_read *data, char *arg); + +#endif diff --git a/42sh/includes/color.h b/42sh/includes/color.h new file mode 100644 index 00000000..96eea18e --- /dev/null +++ b/42sh/includes/color.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 13:36:48 by jhalford #+# #+# */ +/* Updated: 2016/12/09 22:09:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLOR_H +# define COLOR_H +# include "libft.h" + +struct s_color +{ + char fg[7]; + char bg[7]; +}; + +typedef struct s_color t_color; + +# define FG_DEFAULT "\x1b[0m" +# define ON_BOLD "\x1b[1m" + +# define ON_UNDERLINED "\x1b[4m" +# define ON_INVERTED "\x1b[7m" + +# define FG_BLACK "\x1b[30m" +# define FG_RED "\x1b[31m" +# define FG_GREEN "\x1b[32m" +# define FG_YELLOW "\x1b[33m" +# define FG_BLUE "\x1b[34m" +# define FG_MAGENTA "\x1b[35m" +# define FG_CYAN "\x1b[36m" + +# define BG_BLACK "\x1b[40m" +# define BG_RED "\x1b[41m" +# define BG_GREEN "\x1b[42m" +# define BG_YELLOW "\x1b[43m" +# define BG_BLUE "\x1b[44m" +# define BG_MAGENTA "\x1b[45m" +# define BG_CYAN "\x1b[46m" +# define BG_DEFAULT "\x1b[49m" + +# define FBG_DEFAULT "\x1b[49m\x1b[20m" + +void ft_color_reset(void); +void ft_color_set(t_color color); + +void ft_color_mk(t_color *color, char fg[7], char bg[7]); +void ft_color_mkif(t_color *color, int cond, char fg[7], char bg[7]); + +#endif diff --git a/42sh/includes/dlst.h b/42sh/includes/dlst.h new file mode 100644 index 00000000..42d73729 --- /dev/null +++ b/42sh/includes/dlst.h @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dlst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:21:04 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:21:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DLST_H +# define DLST_H + +struct s_dlist +{ + void *content; + size_t content_size; + struct s_dlist *next; + struct s_dlist *prev; +}; + +typedef struct s_dlist t_dlist; + +void ft_dlstadd_after(t_dlist **alst, t_dlist *new); +void ft_dlstadd_before(t_dlist **alst, t_dlist *new); +void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t)); +void ft_dlstdelone(t_dlist **alst, void (*del)(void *, size_t)); +int ft_dlstsize(t_dlist *list); +t_dlist *ft_dlstnew(void const *content, size_t content_size); +t_dlist *ft_dlstlast(t_dlist *list); +char *ft_dlsttostr(t_dlist *list); + +#endif diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index e9e37fe4..9e753070 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -33,6 +33,7 @@ # define IS_PIPESTART(a) (a & PROCESS_PIPESTART) # define IS_PIPEEND(a) (a & PROCESS_PIPEEND) +# define IS_PIPESINGLE(a) (a & (PROCESS_PIPESTART | PROCESS_PIPEEND)) # define SCRIPT_LOOP (1 << 0) @@ -48,6 +49,7 @@ struct s_process pid_t pid; int fdin; int fdout; + int toclose; int status; t_flag attributes; t_flag script; @@ -91,8 +93,9 @@ int exec_list(t_btree **ast); int launch_process(t_process *p); int process_setexec(t_type type, t_process *p); -int process_setgroup(t_process *p); +int process_setgroup(t_process *p, pid_t pid); int process_redirect(t_process *p); +void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); @@ -101,7 +104,7 @@ void fd_reset(void); char *ft_findexec(char *path, char *file); -void set_exitstatus(int status); +void set_exitstatus(int status, int override); void ast_free(void *data, size_t content_size); diff --git a/42sh/includes/ft_printf.h b/42sh/includes/ft_printf.h new file mode 100644 index 00000000..6db739f4 --- /dev/null +++ b/42sh/includes/ft_printf.h @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftprintf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:22:54 by jhalford #+# #+# */ +/* Updated: 2016/12/15 15:29:22 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H +# include "libft.h" +# include +# define ALL_FLAGS "#0- +" +# define ALL_CONVERSIONS "sSpdDioOuUxXcCb" + +typedef struct s_fmt t_fmt; +typedef struct s_conv t_conv; +typedef char *(t_converter)(t_fmt *fmt, va_list ap); +typedef void (t_pad_func)(char *str, t_fmt *fmt); + +struct s_conv +{ + char id; + char allowed_flags[6]; + char base[20]; + t_converter *converter; + t_pad_func *sharp_func; +}; + +struct s_fmt +{ + char flags[6]; + int width; + int precision; + char modifier[3]; + char conversion; + int valid; + t_conv conv; +}; + +int ft_vdprintf(int fd, const char *format, va_list ap); +int ft_fmtcalc(char **final, char **str, va_list ap); + +extern t_conv g_convs[]; + +t_fmt *ft_fmt_init(void); +void ft_fmt_print(t_fmt *fmt); +void ft_printf_color(int fd, char **final, char **str); + +t_fmt *ft_printf_parse(char **format, va_list ap); +void ft_printf_parse_flags(t_fmt *fmt, char **format); +void ft_printf_parse_width(t_fmt *fmt, char **format, va_list ap); +void ft_printf_parse_precision(t_fmt *fmt, char **format, va_list ap); +void ft_printf_parse_modifiers(t_fmt *fmt, char **format); + +char *ft_transform(t_fmt *fmt, va_list ap); + +void ft_fmt_error_conv(char conv); +void ft_fmt_error_mod_conv(char *mod, char conv); +void ft_fmt_error_flag_conv(char flag, char conv); +void ft_fmt_error_flag_flag(char flag1, char flag2); + +void ft_fmt_simplify(t_fmt *fmt); +int ft_fmt_validate_conv(t_fmt *fmt); +void ft_fmt_validate_flags(t_fmt *fmt); +void ft_fmt_validate_mod(t_fmt *fmt); + +char *ft_signed_conversion(t_fmt *fmt, va_list ap); +char *ft_unsigned_conversion(t_fmt *fmt, va_list ap); +char *ft_str_conversion(t_fmt *fmt, va_list ap); +char *ft_char_conversion(t_fmt *fmt, va_list ap); + +void ft_pad_sharp_o(char *str, t_fmt *fmt); +void ft_pad_sharp_xb(char *str, t_fmt *fmt); +void ft_pad_left(char *str, t_fmt *fmt); +void ft_pad_right(char *str, t_fmt *fmt); +#endif diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index acc96bba..323f7eb0 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -3,212 +3,145 @@ /* ::: :::::::: */ /* ft_readline.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ +/* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 18:02:25 by sbenning #+# #+# */ -/* Updated: 2017/01/21 23:42:07 by ariard ### ########.fr */ +/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ +/* Updated: 2017/02/03 14:43:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_READLINE_H # define FT_READLINE_H -# include "libft.h" -# include "ft_curs.h" +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include -/* - * Input-Key Mapping !!! LINUX Or MACOSX !!! -*/ +# define FLECHE_HAUT 4283163 +# define FLECHE_BAS 4348699 +# define FLECHE_GAUCHE 4479771 +# define FLECHE_DROITE 4414235 +# define TOUCHE_DELETE 2117294875 +# define TOUCHE_CTRL_C 3 +# define TOUCHE_CTRL_D 4 +# define TOUCHE_CTRL_R 18 +# define TOUCHE_SUPPR 127 +# define TOUCHE_HOME 4741915 +# define TOUCHE_END 4610843 +# define TOUCHE_TAB 9 +# define TOUCHE_OPT_RIGHT 1130044187 +# define TOUCHE_OPT_LEFT 1146821403 +# define TOUCHE_OPT_UP 1096489755 +# define TOUCHE_OPT_DOWN 1113266971 +# define TOUCHE_OPT_V 10127586 +# define TOUCHE_OPT_C 42947 +# define TOUCHE_OPT_X 8948194 +# define TOUCHE_F5 892427035 +# define TOUCHE_F6 925981467 -//# define LINUX -# define MACOSX -# include "ft_input.h" +# define HIST 1 -/* - * Default offset for dynamic allocation -*/ +# define ERROR_CNTL_R 1 -# define RL_OFFSET 1024 - -/* - * Readline possible prompt -*/ - -# define RL_PROMPT_DEFAULT "$> " -# define RL_PROMPT_ESC "> " -# define RL_PROMPT_QUOTE "quote> " -# define RL_PROMPT_DQUOTE "double quote> " - -/* - * Bitset manipulation : X is the bitset and Y is the bit to manipulate - * IS : Is Y set in X - * NOT : Is Y not set in X - * SET : Set Y in X - * UNSET : Unset Y from X -*/ - -# define RL_IS(X, Y) (X & Y) -# define RL_NOT(X, Y) (!RL_IS(X, Y)) -# define RL_SET(X, Y) (X |= Y) -# define RL_UNSET(X, Y) (X &= ~Y) - -/* - * Possible bit for t_line.bitset - * - * FINISH : bit to terminate and flush the readline - * STACK : bit to stack the current line and begin another one - * INSERT : bit to toogle insert-mode/replace-mode - * SELECT : bit to toogle extend-video-area-mode/clear-video-area-mode - * ESC : bit to escape quoting and new line - * QUOTE : bit to toogle quote-terminated-mode/quote-not-terminated-mode - * DQUOTE : bit to toogle dquote-terminated-mode/dquote-not-terminated-mode - * QUOTING : Regroup ESC, QUOTE and DQUOTE -*/ - -# define RL_FINISH (1 << 0) -# define RL_STACK (1 << 1) -# define RL_INSERT (1 << 2) -# define RL_SELECT (1 << 3) -# define RL_ESC (1 << 4) -# define RL_QUOTE (1 << 5) -# define RL_DQUOTE (1 << 6) -# define RL_QUOTING (RL_ESC | RL_QUOTE | RL_DQUOTE) - -typedef struct s_data t_data; -typedef struct s_line t_line; -typedef struct s_input t_input; -typedef int (*t_input_function)(t_line *, long int); - -/* - * Interactive line data: - * - * bitset : global/local state and settings for readline - * prompt : current prompt - * input : destination buffer - * pos : cursor position in the destination buffer - * size : allocated size of the destination buffer - * used : actual used size in the destination buffer - * select : start position of the video (aka selected) area in the destination buffer - * clipboard : duplication of the copied/cuted part of the destination buffer - * stack : list of stacked line. (lines are stacked when a quoted (or escaped) new line appear) -*/ - -struct s_line +typedef struct s_list_history +{ + char *str; + struct s_list_history *prev; + struct s_list_history *next; +} t_list_history; + +typedef struct s_line { - int bitset; - char *prompt; char *input; - char *clipboard; - int pos; - int size; - int used; - int select; - t_curs curs; - t_list *stack; - t_dlist *history; -}; + char *copy_tmp; + size_t pos; + int prompt_size; + int list_size; + char opt; + t_list_history *list_end; + t_list_history *list_cur; + t_list_history *list_beg; +} t_line; -/* - * Input data: - * code : input identifier - * function : input handler -*/ - -struct s_input +typedef struct s_key { - long int code; - t_input_function function; -}; + int value; + void (*f)(void); +} t_key; -# include "minishell.h" +extern t_key g_keys[]; -/* - * Readline setup/cleanup/teardown -*/ -int rl_setup(t_line *line); -int rl_cleanup(t_line *line); -void rl_teardown(t_line *line); -int rl_set_termios(int input_mode); +void ft_putnc(char c, int n); +int ft_nbr_len(int nbr); +void ft_puttermcaps(char *str); +void ft_get_beggin_with_curs(char *str, size_t *pos); +void ft_check_end_of_line(char *str, size_t pos); +int ft_size_term(void); +void ft_add_in_history_file(char *str); +void ft_get_beggin(char *str, size_t *pos); +void ft_get_next_str(char *str, size_t *pos); +void ft_putall_current_str(char *str, size_t *pos); +void ft_current_str(char *str, size_t pos); +int ft_strlen_next(char *str, size_t pos); +void ft_push_back_history(t_list_history **head, t_list_history *new); +void ft_prompt(void); +char *ft_remove_imput(char *str, size_t pos); +char *ft_realloc_imput(char *str, int a, size_t pos); +char *ft_strdupi(char const *s); +char *ft_strndup(char const *s, int n); +t_list_history *ft_create_history_list(char *str); +char *ft_strdupi_w(char const *s); +void ft_realloc_str_history(char **str, size_t pos, int nb_his, int len); +void ft_realloc_str_history_2(char **str, size_t pos, char *s); +long long ft_pow(int nbr, int power); +void ft_realloc_str_history_3(char **str, size_t pos, char *s); +void ft_check_backslash(char **str); +char *ft_strget_history(char *str); +int ft_nb_last_line(char *str, size_t pos); +int ft_put(int nb); -/* - * Dynamic allocated buffer manipulation -*/ +char *ft_read_stdin(void); +void ft_end(void); +void ft_home(void); +void ft_move_right(void); +void ft_move_left(void); +void ft_up(void); +void ft_down(void); +void ft_buff_f6(void); +void ft_control_d(void); +void ft_control_c(void); +void ft_del(void); +void ft_suppr(void); +void ft_print(int ret); +void ft_surch_in_history(void); +void ft_printall(void); +void ft_history_down(void); +void ft_history_up(void); +void ft_found_next_word(void); +void ft_found_prev_word(void); +void ft_c(void); +void ft_x(void); +void ft_v(void); +void ft_history_parsing(void); +void ft_read_it(int input, size_t *pos, char **str); +int ft_readline(void); -int input_maj(t_line *line, char *str, int size); -int input_move(t_line *line, char *str, int size); -void input_remove(t_line *line, int size); +void ft_check_heredoc(char **str); +void ft_check_quotes(char **s); -/* - * Readline internal function -*/ - -void rl_set_prompt(t_line *line); -void rl_put_prompt(t_line *line); -int rl_previous_word(t_line *line); -int rl_next_word(t_line *line); -int rl_clipboard_new(t_line *line); -int rl_clipboard_new_cut(t_line *line); -void rl_toogle_bitset(t_line *line, int set); -int rl_finish(t_line *line); -int rl_stack_line(t_line *line); -int rl_merge_line(t_line *line); - -/* - * Realine display functions: - * - * reset_display : Redraw the interactive buffer and replace the cursor - * based on t_line.input, t_line.pos and t_line.select . - * reset_display_ante : Redraw before the cursor position. - * reset_display_post : Redraw after the cursor position. -*/ - -void rl_reset_display(t_line *line); -void rl_reset_display_ante(t_line *line); -void rl_reset_display_post(t_line *line); - -/* - * input handler functions - * - * An handler use curs_ to manipulate the cursor position - * and recalc t_line.pos based on the cursor movement. - * - * An handler can also modify the content of the destination buffer. - * If it do so, it must reset all display that appear - * after the first index of that modification. - * If t_line.pos is that index, rl_reset_display_post can simply do the work. -*/ - -int rl_default_function(t_line *line, long int input); /* Not handled input */ -int rl_esc_function(t_line *line, long int input); /* Backslash */ -int rl_quote_function(t_line *line, long int input); /* Simple quote */ -int rl_dquote_function(t_line *line, long int input); /* Double quote */ -int rl_nl_function(t_line *line, long int input); /* New line */ -int rl_comp_function(t_line *line, long int input); /* Tabulation */ -int rl_left_function(t_line *line, long int input); /* Left move */ -int rl_right_function(t_line *line, long int input); /* Right move */ -int rl_wleft_function(t_line *line, long int input); /* Word left move */ -int rl_wright_function(t_line *line, long int input); /* Word right move */ -int rl_home_function(t_line *line, long int input); /* Home move */ -int rl_end_function(t_line *line, long int input); /* End move */ -int rl_pageup_function(t_line *line, long int input); /* Pageup move */ -int rl_pagedown_function(t_line *line, long int input); /* Pagedown move*/ -int rl_select_left_function(t_line *line, long int input); /* Left extend video area */ -int rl_select_right_function(t_line *line, long int input); /* Right extend video area */ -int rl_select_wleft_function(t_line *line, long int input); /* Word left extend video area */ -int rl_select_wright_function(t_line *line, long int input); /* Word right extend video area */ -int rl_select_home_function(t_line *line, long int input); /* Home extend video area */ -int rl_select_end_function(t_line *line, long int input); /* End extend video area */ -int rl_select_pageup_function(t_line *line, long int input); /* Pageup extend video area */ -int rl_select_pagedown_function(t_line *line, long int input); /* Pagedown extend video area */ -int rl_retarr_function(t_line *line, long int input); /* Remove before the cursor */ -int rl_suppr_function(t_line *line, long int input); /* Remove after the cursor */ -int rl_clear_function(t_line *line, long int input); /* Clear screen */ -int rl_copy_function(t_line *line, long int input); /* Copy selected area or current cursor line */ -int rl_cut_function(t_line *line, long int input); /* Cut selected area or current cursor line */ -int rl_paste_function(t_line *line, long int input); /* Paste copied/cuted area */ -int rl_insert_function(t_line *line, long int input); /* Toogle insert-mode/replacement-mode */ - -int ft_readline(); #endif diff --git a/42sh/includes/ft_xattr.h b/42sh/includes/ft_xattr.h new file mode 100644 index 00000000..9afadc18 --- /dev/null +++ b/42sh/includes/ft_xattr.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftxattr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:24:05 by jhalford #+# #+# */ +/* Updated: 2016/11/07 13:24:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_XATTR_H +# define FT_XATTR_H +# define FT_XATTR_SIZE 10000 +# include +# include + +int ft_xattr_print(char *path); +int ft_xattr_count(char *path); +#endif diff --git a/42sh/src/line-editing/curs_clear.c b/42sh/includes/get_next_line.h similarity index 52% rename from 42sh/src/line-editing/curs_clear.c rename to 42sh/includes/get_next_line.h index a76809ab..b63d0ac2 100644 --- a/42sh/src/line-editing/curs_clear.c +++ b/42sh/includes/get_next_line.h @@ -1,34 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* curs_clear.c :+: :+: :+: */ +/* get_next_line.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 17:28:50 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:22:46 by ariard ### ########.fr */ +/* Created: 2016/11/05 12:21:36 by jhalford #+# #+# */ +/* Updated: 2016/12/15 13:07:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_curs.h" +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H +# define BUFF_SIZE 32 -/* - * Clear screen and keep track of cursoe's coordonate -*/ +# include "libft.h" +# include +# include -void curs_clear(t_curs *curs) +typedef struct s_save t_save; + +struct s_save { - ft_putstr(tgetstr("cl", NULL)); - curs->li = 0; - curs->co = 0; -} + int fd; + char *str; +}; -/* - * Clear screen after cursor position without moving cursor -*/ +int get_next_line(int const fd, char **line); -void curs_clear_post(t_curs *curs) -{ - ft_putstr(tgetstr("cd", NULL)); - (void)curs; -} +#endif diff --git a/42sh/includes/glob.h b/42sh/includes/glob.h new file mode 100644 index 00000000..96cf40f2 --- /dev/null +++ b/42sh/includes/glob.h @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */ +/* Updated: 2017/02/01 19:50:07 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GLOB_H +# define GLOB_H + +# include "minishell.h" + +# define CH(x) ((char **)(x)->content) +# define UCH(x) ((unsigned char **)(x)->content) + +typedef struct s_ld +{ + void *content; + struct s_ld *next; + struct s_ld *prev; +} t_ld; + +typedef struct s_glob +{ + const char *pat; + const unsigned char *esc; + t_ld *match; + t_ld *m_pat; +} t_glob; + +typedef struct s_expand +{ + t_ld **wk; + char *str; + unsigned char *esc; + char **split; + unsigned char **m_esc; + char *s1; +} t_expand; + +/* +** interface of glob. +*/ +char **glob(const char *str, const unsigned char *esc); + +/* +** return TRUE if path file is a directory. +*/ + +int is_directory(const char *path); +/* +** return TRUE if char at str_pos in ini_str is escape. +*/ +int is_char_esc(const unsigned char *esc, + const char *ini_str, const char *str_pos); + +/* +** Internal function. +*/ +unsigned char *ft_sub_esc(const unsigned char *esc, int start, int len); +unsigned char *calc_expand_esc(const unsigned char *esc, + int nb_start, int nb_middle, int *nb_end); +void modify_esc_split(unsigned char *esc_dest, + unsigned char *esc_src, int start, int len); +void expand_brace(t_glob *tglob); +int match_pattern(t_glob *tglob, char *str, char *full_word); +int dir_research(t_glob *tglob, char *p, const char *pat, int rec); +char **ft_strsplit_spe(const char *str, + const unsigned char *esc, char c); +unsigned char **ft_strsplit_esc(const char *str, + const unsigned char *esc, char c); +/* +** LIST D: +*/ +void ft_ld_new(t_ld **alst, void *content); +t_ld *ft_ld_front(t_ld *ld); +void ft_ld_pushfront(t_ld **alst, void *content); +void ft_ld_pushback(t_ld **alst, void *content); +size_t ft_ld_size(t_ld *ld); +void ft_ld_del(t_ld **ld, void (*del)()); +void ft_ld_clear(t_ld **ld, void (*del)()); +void ft_ld_reverse(t_ld **lst); +t_ld *ft_ld_back(t_ld *ld); +t_ld *ft_ld_swap(t_ld *l_cur); +char **ft_ld_to_tab(t_ld *ld); +t_ld *ft_ld_order(t_ld *ld, int (*f)(), void (*del)()); + +/* +** str: +*/ + +char *ft_strjoinf(char *str, char *str2, int mode); +void ft_tabdel(char ***mytab); +int ft_tablen(char **mytab); + +#endif diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 6cd51a51..418de6eb 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:55:09 by jhalford #+# #+# */ -/* Updated: 2017/01/19 21:51:10 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:43:34 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ struct s_jobc # include "exec.h" -t_process *job_getprocess(pid_t pid); +t_list *job_getprocess(pid_t pid); int job_addprocess(t_process *p); void job_update_id(void); void job_update_rank(void); @@ -53,8 +53,8 @@ void job_update_rank(void); int do_job_notification(void); void job_notify_new(t_job *job); void job_notify_change(int id); -void job_format(t_job *j, int rank[2], int opts); -void job_format_head(t_job *j, int rank[2]); +void job_format(t_job *j, int opts); +void job_format_head(t_job *j); void job_update_status(void); void mark_job_as_running (t_job *j); @@ -63,6 +63,7 @@ int job_is_stopped(int id); int job_is_completed(int id); void job_new(char **av, pid_t pid); +void job_run(t_job *job, int foreground); int job_wait(int id); void job_remove(int id); void job_free(void *content, size_t content_size); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index f7bd6830..58fbdd49 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/01/31 23:52:50 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:43:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,9 +85,10 @@ enum e_lexstate struct s_token { - t_type type; - char *data; - int size; + t_type type; + char *data; + unsigned char *esc; + int size; }; typedef struct s_data t_data; @@ -96,10 +97,12 @@ typedef enum e_lexstate t_lexstate; extern int (*g_lexer[])(t_list **alst, char *str); -t_token *token_init(); +int ft_lexer(t_list **alst, char **str); int ft_tokenize(t_list **alst, char *str, t_lexstate state); -int ft_post_tokenize(t_list **alst, char *str); -int token_append(t_token *token, char c); +int ft_post_tokenize(t_list **alst, char **str); + +t_token *token_init(); +int token_append(t_token *token, char c, short int esc); void token_free(void *data, size_t size); int token_cmp_type(t_token *token, t_type *ref); void token_print(t_list *lst); diff --git a/42sh/includes/libft.h b/42sh/includes/libft.h new file mode 100644 index 00000000..ecbe6bb5 --- /dev/null +++ b/42sh/includes/libft.h @@ -0,0 +1,162 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ +/* Updated: 2017/01/19 16:13:05 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# define STDIN 0 +# define STDOUT 1 +# define STDERR 2 +# define STDBUG 3 + +# define DG_MSG0 "{inv}{yel}%21s {bol}{blu}%-3d{eoc}" +# define DG_MSG1 ft_path_notdir(__FILE__), __LINE__ +# define DG(f, ...) ft_dprintf(STDBUG, DG_MSG0 f "{eoc}\n", DG_MSG1, ##__VA_ARGS__) + +# include "get_next_line.h" +# include "ft_xattr.h" +# include "mytime.h" +# include "lst.h" +# include "dlst.h" +# include "btree.h" +# include "color.h" + +# include +# include +# include +# include +# include +# include +# include + +# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n') +# define FT_ABS(x) (((x) < 0) ? -(x) : (x)) +# define FT_NEG(x) (((x) < 0) ? 1 : 0) +# define FT_POS(x) (((x) > 0) ? 1 : 0) +# define FT_MIN(a, b) ((a) < (b) ? (a) : (b)) +# define FT_MAX(a, b) ((a) > (b) ? (a) : (b)) +# define FT_DIST(a, b) (FT_ABS((a) - (b))) + +typedef struct s_stof t_stof; +typedef struct s_stos t_stos; + +struct s_stos +{ + char *key; + char *val; +}; + +struct s_stof +{ + char *name; + int (*f)(); +}; + +void ft_debug(void); + +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memccpy(void *dst, const void *src, int c, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +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 *s); +char *ft_strdup(const char *s1); +char *ft_strcpy(char *dst, const char *src); +char *ft_strncpy(char *dst, const char *src, size_t len); +char *ft_strcat(char *s1, const char *s2); +char *ft_strncat(char *s1, const char *s2, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strstr(const char *big, const char *little); +char *ft_strnstr(const char *big, const char *little, size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *str); +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_memalloc(size_t size); +void ft_memdel(void **ap); +char *ft_strnew(size_t size); +void ft_strdel(char **as); +void ft_strclr(char *s); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s); +char **ft_strsplit(char const *s, char c); +char *ft_itoa(int n); +int ft_putchar(int c); +void ft_putstr(char const *s); +void ft_putendl(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char const *s, int fd); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putaddr(void *a); + +char *ft_strrev(char *str); +char **ft_strsplit(char const *s, char c); +char *ft_str3join(char const *s1, char const *s2, char const *s3); +char *ft_strcut(char *str, char *cut); +char **ft_split_whitespaces(char const *str); +char *ft_convert_base( + char *str, char *base_from, char *base_to, char *flags); +char *ft_strcatf(char *s1, const char *s2); +char *ft_strinsert(char *str, char c, int n); +int ft_strappend(char **dst, char *src); +char *ft_strbetween(char *start, char *end); +char *ft_strreplace(char **str, char *start, char *end, char *new); + +char *ft_itoa_base(int nbr, char *base, char *flags); +char *ft_lltoa_base(long long nbr, char *base, char *flags); +char *ft_ulltoa_base(unsigned long long nbr, char *base); +char *ft_uitoa_base(unsigned int nbr, char *base); +size_t ft_ilen(int n); +size_t ft_ilen_base(int n, int base); +size_t ft_uilen(unsigned int n); +size_t ft_lllen(long long n); +size_t ft_lllen_base(long long n, int base); +int ft_addrcmp(void *a, void *b); + +char **ft_sstradd(char **list, char *new); +void ft_sstrsort(char **list, int (*cmp)()); +void ft_sstrprint(char **list, char sep); +void ft_sstrprint_fd(int fd, char **list, char sep); +char **ft_sstrdup(char **list); +void ft_sstrdel(char **sstr, int index); +void ft_sstrfree(char **sstr); +char *ft_sstrcat(char **sstr, char sep); + +char *ft_path_notdir(char *path); + +int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); + +char *ft_getenv(char **env, char *key); + +void *ft_realloc(void *data, int size); +#endif diff --git a/42sh/includes/lst.h b/42sh/includes/lst.h new file mode 100644 index 00000000..ee4926df --- /dev/null +++ b/42sh/includes/lst.h @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lst.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */ +/* Updated: 2016/12/12 14:59:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LST_H +# define LST_H + +# include "libft.h" + +struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +}; + +typedef struct s_list t_list; + +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); + +t_list *ft_lstnew_range(int a, int b); +void ft_lsteadd(t_list **alst, t_list *new); +void ft_lstnadd(t_list **alst, t_list *new, int n); +void ft_lstsort(t_list **begin_list, int (*cmp)()); +void ft_lst_print(t_list *list, void (*printer)()); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lst_sorted_merge( + t_list **begin_list1, + t_list *begin_list2, + int (*cmp)()); +void ft_lst_sorted_insert( + t_list **begin_list, + t_list *insert, + int (*cmp)()); +void ft_lst_delif( + t_list **alist, + void *data_ref, + int (*cmp)(), + void (*del)(void *, size_t)); +void ft_lst_delsub( + t_list **alst, + t_list *sub, int (*cmp)(), + void (*del)(void *, size_t)); +void ft_lst_cfree(void *ptr, size_t size); +void ft_lst_bfree(void *ptr, size_t size); +t_list *ft_lst_filter( + t_list *lst, + void const *data_ref, + t_list *(*f)(t_list *elem, void const *)); +t_list *ft_lst_removeif( + t_list **alst, + void *data_ref, + int (*cmp)()); +t_list *ft_lst_find( + t_list *begin_list, + void *data_ref, + int (*cmp)()); +t_list *ft_lstpop(t_list **lst); +void ft_lst_merge(t_list **begin_list1, t_list *begin_list2); +void ft_lst_reverse(t_list **begin_list); + +int ft_diff(void *a, void *b); +t_list *ft_id(t_list *a); +t_list *ft_lst_at(t_list *list, unsigned int nbr); + +#endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 944c850c..630b5b70 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* minishell.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/01/31 23:57:46 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:42:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ # include "exec.h" # include "builtin.h" # include "job_control.h" +# include "glob.h" # include # include diff --git a/42sh/includes/mytime.h b/42sh/includes/mytime.h new file mode 100644 index 00000000..f9678127 --- /dev/null +++ b/42sh/includes/mytime.h @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mytime.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/25 11:43:12 by jhalford #+# #+# */ +/* Updated: 2017/02/03 14:42:38 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MYTIME_H +# define MYTIME_H +# include "libft.h" + +struct s_mytime +{ + char *year; + char *month; + char *day; + char *hour; + char *min; + char *sec; +}; + +typedef struct s_mytime t_mytime; + +int ft_time_isrecent(time_t event); + +t_mytime *ft_mytime_get(time_t epoch); +void ft_mytime_free(t_mytime **time); + +#endif diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 72c2046d..5145897a 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/01/31 19:31:01 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:42:21 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ # include "minishell.h" typedef struct s_parser t_parser; +typedef struct s_ld t_ld; typedef struct s_astnode t_astnode; typedef struct s_redir t_redir; typedef union u_astdata t_astdata; @@ -45,6 +46,7 @@ struct s_redir union u_astdata { t_redir redir; + t_ld *token; char **sstr; char *str; }; diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c new file mode 100644 index 00000000..a7364b71 --- /dev/null +++ b/42sh/src/builtin/bt_read_get.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */ +/* Updated: 2017/01/22 16:24:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +int bt_read_getdelim(t_read *data, char *arg) +{ + data->delim = *arg; + return (0); +} + +int bt_read_getnchars(t_read *data, char *arg) +{ + data->nchars = ft_atoi(arg); + return (0); +} + +int bt_read_getprompt(t_read *data, char *arg) +{ + data->prompt = arg; + return (0); +} + +int bt_read_gettimeout(t_read *data, char *arg) +{ + data->timeout = ft_atoi(arg); + return (0); +} + +int bt_read_getfd(t_read *data, char *arg) +{ + data->fd = ft_atoi(arg); + return (0); +} diff --git a/42sh/src/builtin/bt_read_parse.c b/42sh/src/builtin/bt_read_parse.c new file mode 100644 index 00000000..b23672f1 --- /dev/null +++ b/42sh/src/builtin/bt_read_parse.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/27 18:25:22 by jhalford #+# #+# */ +/* Updated: 2017/01/27 19:40:14 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +static t_readopt *bt_read_getopt(char letter) +{ + int i; + + i = 0; + while (g_readtab[i].letter) + { + if (g_readtab[i].letter == letter) + return (&g_readtab[i]); + i++; + } + return (NULL); +} + +int bt_read_parse(t_read *data, char **av) +{ + int i; + int j; + int k; + t_readopt *opt; + + i = 1; + k = 0; + while (av[i]) + { + j = 0; + if (av[i][j++] == '-') + { + if (av[i][j] == '-' && av[i][j + 1] == 0) + { + i++; + break ; + } + while (av[i][j]) + { + if (!(opt = bt_read_getopt(av[i][j]))) + { + ft_dprintf(2, "{red}%s: bad option: %c{eoc}\n", SHELL_NAME, av[i][j]); + return (2); + } + data->opts |= opt->flag; + if (opt->get) + { + (*opt->get)(data, av[++i]); + break ; + } + j++; + } + } + else + break ; + i++; + } + data->names = av[i] ? av + i : NULL; + DG("read_opts: %b", data->opts); + DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i", + data->delim, data->nchars, data->prompt, data->timeout, data->fd); + return (0); +} + +int bt_read_init(t_read *data, char **av) +{ + data->opts = 0; + data->delim = '\n'; + data->nchars = -1; + data->prompt = NULL; + data->fd = 0; + data->input = NULL; + if ((bt_read_parse(data, av))) + return (1); + if (data->names) + DG("%s,%s", data->names[0], data->names[1]); + bt_read_terminit(data); + return (0); +} diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c new file mode 100644 index 00000000..853929c4 --- /dev/null +++ b/42sh/src/builtin/bt_read_term.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_term.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ +/* Updated: 2017/01/27 19:00:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin.h" + +struct termios bt_read_term(int init) +{ + static struct termios term; + + if (init) + tcgetattr(0, &term); + return (term); +} + +int bt_read_terminit(t_read *data) +{ + struct termios term; + + (void)data; + term = bt_read_term(1); + term.c_lflag &= ~(ECHO | ICANON); + if (data->opts & READ_OPT_LT) + { + term.c_cc[VTIME] = data->timeout * 10; + term.c_cc[VMIN] = 0; + } + else + { + term.c_cc[VTIME] = 0; + term.c_cc[VMIN] = 1; + } + if (tcsetattr(0, TCSANOW, &term) < 0) + return (-1); + return (0); +} + +int bt_read_exit(t_read *data) +{ + struct termios term; + + ft_strdel(&data->input); + term = bt_read_term(0); + if (tcsetattr(0, TCSANOW, &term) < 0) + return (-1); + return (0); +} diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 80148cb8..220886c9 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:25:06 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:04:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c new file mode 100644 index 00000000..586f3627 --- /dev/null +++ b/42sh/src/builtin/builtin_read.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ +/* Updated: 2017/01/27 19:40:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +t_readopt g_readtab[] = +{ + /* {'a', READ_OPT_LA, NULL}, */ + {'d', READ_OPT_LD, bt_read_getdelim}, + /* {'e', READ_OPT_LE, NULL}, */ + /* {'i', READ_OPT_LI, NULL}, */ + {'n', READ_OPT_LN, bt_read_getnchars}, + /* {'N', READ_OPT_UN, bt_read_getnchars}, */ + {'p', READ_OPT_LP, bt_read_getprompt}, + {'r', READ_OPT_LR, NULL}, + {'s', READ_OPT_LS, NULL}, + {'t', READ_OPT_LT, bt_read_gettimeout}, + {'u', READ_OPT_LU, bt_read_getfd}, + {0, 0, 0}, +}; + +int bt_read_loop(t_read *data) +{ + int i; + int esc; + char buf[2]; + + i = 0; + esc = 0; + if (data->prompt) + ft_printf(data->prompt); + while (42) + { + if (read(data->fd, buf, 1) <= 0) + return (1); + buf[1] = 0; + if (!esc && *buf == data->delim) + break ; + esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\'); + ft_strappend(&data->input, buf); + if (!(data->opts & READ_OPT_LS)) + ft_putchar(*buf); + if (*buf == '\n' && !(data->opts & READ_OPT_LR)) + ft_putstr("> "); + if ((data->opts & READ_OPT_LN) && ++i >= data->nchars) + break ; + } + ft_putchar('\n'); + return (0); +} + +int bt_read_assign(t_read *data) +{ + char *input; + char **names; + char *IFS; + char *start; + + input = data->input; + names = data->names ? data->names : (char*[]){"REPLY", NULL}; + IFS = ft_getenv(data_singleton()->env, "IFS"); + start = input; + while (*start && *names) + { + if (!(names[1]) || !IFS) + { + builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); + break ; + } + while (*input && !ft_strchr(IFS, *input)) + input++; + while (input && ft_strchr(IFS, *input)) + *(input++) = 0; + builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); + start = input; + names++; + } + return (0); +} + +int builtin_read(const char *path, char *const av[], char *const envp[]) +{ + t_read data; + int ret; + + (void)path; + (void)envp; + ret = 0; + if (bt_read_init(&data, (char **)av)) + ret = 2; + else if (bt_read_loop(&data)) + ret = 1; + else if (bt_read_assign(&data)) + ret = 1; + bt_read_exit(&data); + return (ret); +} diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 98f92890..3c566d6b 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/01/09 15:53:07 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 19:00:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 7dbdda0c..eba6e445 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -3,16 +3,17 @@ /* ::: :::::::: */ /* is_builtin.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:58:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 14:58:02 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_stof g_builtin[] = { +t_stof g_builtin[] = +{ {"echo", &builtin_echo}, {"cd", &builtin_cd}, {"setenv", &builtin_setenv}, @@ -22,6 +23,8 @@ t_stof g_builtin[] = { {"jobs", &builtin_jobs}, {"fg", &builtin_fg}, {"bg", &builtin_bg}, + {"history", &builtin_history}, + {"read", &builtin_read}, {NULL, NULL}, }; diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index 29a7f0e1..7f7d9591 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/01/31 22:35:31 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:41:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,11 @@ void ast_free(void *data, size_t content_size) (void)content_size; node = data; if (node->type == TK_COMMAND) + ft_ld_clear(&node->data.token, &ft_tabdel); + else if (node->type == TK_SUBSHELL) { - if (node->data.sstr) - ft_sstrfree(node->data.sstr); + ft_sstrfree(node->data.sstr); + node->data.sstr = NULL; } else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT) { diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 80176ee5..6697b4a5 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,13 +6,42 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/02/02 17:25:52 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:40:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_command(t_btree **ast) +static char **token_to_argv(t_astnode *node) +{ + char **my_tab; + int index; + char **expand; + char **content; + t_ld *ld; + + if (node->type == TK_COMMAND) + { + ld = node->data.token; + my_tab = NULL; + while (ld) + { + content = ld->content; + expand = glob(content[0], (unsigned char *)content[1]); + index = -1; + while (expand[++index]) + my_tab = ft_sstradd(my_tab, expand[index]); + ft_tabdel(&expand); + ld = ld->next; + } + return (my_tab); + } + else if (node->type == TK_SUBSHELL) + return (ft_sstrdup(node->data.sstr)); + return (NULL); +} + +int exec_command(t_btree **ast) { t_astnode *node; t_process *p; @@ -21,7 +50,7 @@ int exec_command(t_btree **ast) node = (*ast)->item; p = &data_singleton()->exec.process; job = &data_singleton()->exec.job; - p->av = ft_sstrdup(node->data.sstr); + p->av = token_to_argv(node); process_setexec(node->type, p); if (!(launch_process(p))) { @@ -36,8 +65,7 @@ int exec_command(t_btree **ast) } p->av = NULL; p->pid = 0; - p->attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; - -// btree_delone(ast, &ast_free); + p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); + btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index d11fc8f1..d876dd9e 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -25,11 +25,13 @@ int exec_pipe(t_btree **ast) DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); p->fdout = fds[PIPE_WRITE]; start = IS_PIPESTART(p->attributes); + p->toclose = fds[PIPE_READ]; p->attributes &= ~PROCESS_PIPEEND; ft_exec(&(*ast)->left); p->attributes &= ~PROCESS_PIPESTART; + p->toclose = STDIN; close(fds[PIPE_WRITE]); p->fdout = STDOUT; p->fdin = fds[PIPE_READ]; diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 8251c16e..117bf317 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/01/31 20:00:23 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:40:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,7 @@ int ft_exec(t_btree **ast) while (g_execmap[i].type) { if (item->type & g_execmap[i].type) + { /* return ((*g_execmap[i].f)(ast)); */ (*g_execmap[i].f)(ast); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 5fd4472d..1952d096 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/01/30 19:36:21 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:39:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,40 +21,38 @@ int launch_process(t_process *p) if (p->attributes & PROCESS_UNKNOWN) { ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); - set_exitstatus(127); - return (1); + set_exitstatus(127, 1); } - else if (p->attributes & PROCESS_BUILTIN && p->fdout == STDOUT) - set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env)); + else if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(p->attributes)) + set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); else { p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_RUNNING; - DG("process is to be forked, %i->[]->%i, attr=%b", p->fdin, p->fdout, p->attributes); if (p->attributes & (PROCESS_BINARY | PROCESS_SCRIPT) && access(p->path, X_OK) == -1) { ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); - return (-1); + set_exitstatus(126, 1); + return (1); } pid = fork(); if (pid == 0) { - process_setgroup(p); - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGTSTP, SIG_DFL); - signal(SIGTTIN, sigttin_handler); - signal(SIGTTOU, sigttou_handler); - signal(SIGCHLD, SIG_DFL); + process_setgroup(p, 0); + process_setsig(); process_redirect(p); (*p->execf)(p->path, p->av, data_singleton()->env); - exit(42); + exit(43); } else if (pid > 0) + { p->pid = pid; + process_setgroup(p, pid); + return (0); + } else if (pid == -1) - perror("fork"); + ft_dprintf(2, "{red}internal fork error{eoc}\n"); } - return (0); + return (1); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index fed5209d..77830bb0 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -14,15 +14,15 @@ int process_redirect(t_process *p) { + if (p->toclose != STDIN) + close(p->toclose); if (p->fdin != STDIN) { - /* DG("redirect STDIN to %i", p->fdin); */ dup2(p->fdin, STDIN); close(p->fdin); } if (p->fdout != STDOUT) { - /* DG("redirect STDOUT to %i", p->fdout); */ dup2(p->fdout, STDOUT); close(p->fdout); } diff --git a/42sh/src/exec/process_setgroup.c b/42sh/src/exec/process_setgroup.c index 4cf1e1c4..186ed6d9 100644 --- a/42sh/src/exec/process_setgroup.c +++ b/42sh/src/exec/process_setgroup.c @@ -13,23 +13,18 @@ #include "job_control.h" #include "exec.h" -int process_setgroup(t_process *p) +int process_setgroup(t_process *p, pid_t pid) { - t_job *job; - int pid; + t_job *j; (void)p; - job = &data_singleton()->exec.job; - pid = getpid(); - if (job->pgid == 0) - job->pgid = pid; - if (setpgid(pid, job->pgid)) - DG("setpgid(%i, %i) failed", pid, job->pgid); - if (JOB_IS_FG(job->attributes)) - { - signal(SIGTTOU, SIG_IGN); - tcsetpgrp(STDIN, job->pgid); - signal(SIGTTOU, SIG_DFL); - } + if (!SHELL_HAS_JOBC(data_singleton()->opts)) + return (0); + j = &data_singleton()->exec.job; + if (!j->pgid) + j->pgid = pid ? pid : getpid(); + setpgid(pid, j->pgid); + if (pid == 0 && JOB_IS_FG(j->attributes)) + tcsetpgrp(STDIN, j->pgid); return (0); } diff --git a/42sh/src/exec/process_setsig.c b/42sh/src/exec/process_setsig.c new file mode 100644 index 00000000..b2a68b09 --- /dev/null +++ b/42sh/src/exec/process_setsig.c @@ -0,0 +1,11 @@ +#include "exec.h" + +void process_setsig(void) +{ + /* signal(SIGINT, SIG_DFL); */ + signal(SIGQUIT, SIG_DFL); + signal(SIGTSTP, SIG_DFL); + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + signal(SIGCHLD, SIG_DFL); +} diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index 0a82731d..ed05ea7b 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -12,11 +12,27 @@ #include "minishell.h" -void set_exitstatus(int status) +void set_exitstatus(int status, int override) { char *astatus; + int exitval; - astatus = ft_itoa(status); - builtin_setenv("setenv", (char*[3]){"?", astatus}, data_singleton()->env); + if (override) + exitval = status; + else + { + if (WIFEXITED(status)) + exitval = WEXITSTATUS(status); + else if (WIFSIGNALED(status)) + exitval = 128 + WTERMSIG(status); + else + { + DG("fail: process was not exited nor signaled."); + return ; + } + } + astatus = ft_itoa(exitval); + builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0}, + data_singleton()->env); ft_strdel(&astatus); } diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c new file mode 100644 index 00000000..3ed4a660 --- /dev/null +++ b/42sh/src/glob/dir_glob.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dir_glob.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/30 12:07:16 by wescande #+# #+# */ +/* Updated: 2017/02/01 19:49:44 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +int is_directory(const char *path) +{ + struct stat path_stat; + + stat(path, &path_stat); + return (S_ISDIR(path_stat.st_mode)); +} + +static int dir_list_content(t_glob *gl, char **str, const char *pat, + int recursive) +{ + int ret; + char *path_tmp; + + ret = 0; + if (ft_strcmp(str[1], ".") && ft_strcmp(str[1], "..")) + { + if (*str[0] == '/' && !*(str[0] + 1)) + path_tmp = ft_strjoin(str[0], str[1]); + else + path_tmp = ft_strjoinf(ft_strjoin(str[0], "/"), str[1], 1); + if (recursive) + dir_research(gl, path_tmp, pat, recursive); + gl->pat = pat; + if (match_pattern(gl, str[1], path_tmp) && ++ret) + ft_ld_pushfront(&gl->match, ft_strdup(path_tmp + 2 * + (path_tmp[0] == '.' && path_tmp[1] == '/'))); + ft_strdel(&path_tmp); + } + return (ret); +} + +int dir_research(t_glob *gl, char *p, + const char *pat, int recursive) +{ + DIR *dir; + struct dirent *in; + int ret; + + ret = 0; + if ((ft_strlen(p) <= 1 || p[ft_strlen(p) - 1] != '.') && is_directory(p)) + { + dir = opendir(p); + while ((in = readdir(dir))) + ret += dir_list_content(gl, + (char *[2]){p, in->d_name}, pat, recursive); + closedir(dir); + } + return (ret); +} diff --git a/42sh/src/glob/expand_brace.c b/42sh/src/glob/expand_brace.c new file mode 100644 index 00000000..6aa16d58 --- /dev/null +++ b/42sh/src/glob/expand_brace.c @@ -0,0 +1,139 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_brace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/12 19:00:29 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:20:38 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +/* +** expand_brace return expansion of a string. +** pattern searched are {ab, cd}. +** return is t_ld which first param is ab and second is cd +** input parameters are : +** -char *pat -> pattern string to be looking for expand +*/ + +static char **gen_tab(const char *pat, + const unsigned char *esc, int dup) +{ + char **my_tab; + + if (!(my_tab = (char **)malloc(sizeof(char *) * 3))) + return (NULL); + if (dup) + { + my_tab[0] = ft_strdup(pat); + my_tab[1] = ft_strdup((const char *)esc); + } + else + { + my_tab[0] = (char *)pat; + my_tab[1] = (char *)esc; + } + my_tab[2] = NULL; + return (my_tab); +} + +static void iter_on_each(t_expand *me) +{ + int i; + char **my_new; + char *first; + unsigned char *second; + t_ld *wk_tmp; + + i = -1; + wk_tmp = *me->wk; + while (me->split[++i]) + { + first = ft_strjoinf(ft_strjoin(me->s1, me->split[i]), me->str + 1, 1); + second = calc_expand_esc(me->esc, + ft_strlen(me->s1), + ft_strlen(me->split[i]), + (int[2]){me->str - CH(*me->wk)[0], ft_strlen(me->str + 1)}); + modify_esc_split(second, me->m_esc[i], + ft_strlen(me->s1), ft_strlen(me->split[i])); + my_new = gen_tab(first, second, 0); + ft_ld_pushfront(&wk_tmp, my_new); + } + me->wk = &wk_tmp; +} + +static void init_expand(t_expand *me, char *start) +{ + unsigned char *esc; + + me->s1 = ft_strsub(start, 1, me->str - start - 1); + esc = ft_sub_esc(me->esc, start - CH(*me->wk)[0] + 1, me->str - start); + me->split = ft_strsplit_spe(me->s1, esc, ','); + me->m_esc = ft_strsplit_esc(me->s1, esc, ','); + ft_strdel(&me->s1); + ft_strdel((char **)&esc); + me->s1 = ft_strsub(CH(*me->wk)[0], 0, start - CH(*me->wk)[0]); + iter_on_each(me); + ft_strdel(&me->s1); + ft_tabdel(&me->split); + ft_tabdel((char ***)&me->m_esc); +} + +static int search_brace(t_expand *me) +{ + char *start; + int nb; + + start = NULL; + nb = 0; + while (*me->str) + { + start = *me->str == '{' + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str) + && nb == 0 ? me->str : start; + nb += *me->str == '{' + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str); + nb -= *me->str == '}' + && !is_char_esc(me->esc, CH(*me->wk)[0], me->str); + if (!nb && start) + { + init_expand(me, start); + return (1); + } + ++me->str; + } + return (0); +} + +void expand_brace(t_glob *gl) +{ + t_ld *tmp; + int do_it; + t_expand me; + + ft_ld_pushfront(&gl->m_pat, gen_tab("", (const unsigned char *)"", 1)); + ft_ld_pushfront(&gl->m_pat, gen_tab(gl->pat, gl->esc, 1)); + me = (t_expand){NULL, NULL, NULL, NULL, NULL, NULL}; + do_it = 1; + while (do_it) + { + do_it = 0; + while (gl->m_pat->next && !do_it) + { + me.wk = &gl->m_pat; + me.esc = UCH(gl->m_pat)[1]; + me.str = CH(gl->m_pat)[0]; + if ((tmp = gl->m_pat) && search_brace(&me)) + { + ft_ld_del(&tmp, &ft_tabdel); + do_it = 1; + } + gl->m_pat = gl->m_pat->next; + } + gl->m_pat = ft_ld_front(gl->m_pat); + } +} diff --git a/42sh/src/glob/expand_esc.c b/42sh/src/glob/expand_esc.c new file mode 100644 index 00000000..9ad603e4 --- /dev/null +++ b/42sh/src/glob/expand_esc.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 23:18:56 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:19:45 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +unsigned char *calc_expand_esc(const unsigned char *esc, + int nb_start, int nb_middle, int *nb_end) +{ + unsigned char *new_esc; + int index; + int pos; + + if (!(new_esc = (unsigned char *)ft_strnew(((nb_start + nb_middle + + nb_end[1]) / 8) + 1))) + return (NULL); + index = -1; + while (++index < nb_start) + new_esc[index / 8] |= + ((esc[index / 8] >> (7 - index % 8)) & 1) << (7 - index % 8); + pos = -1; + while (++pos < nb_middle) + { + new_esc[index / 8] |= 0 << (7 - index % 8); + ++index; + } + pos = nb_end[0]; + while (++pos <= nb_end[0] + nb_end[1]) + { + new_esc[index / 8] |= + ((esc[pos / 8] >> (7 - pos % 8)) & 1) << (7 - index % 8); + ++index; + } + return (new_esc); +} + +void modify_esc_split(unsigned char *esc_dest, + unsigned char *esc_src, int start, int len) +{ + int index; + int wk; + + index = -1; + while (++index < len) + { + wk = index + start - 1; + esc_dest[wk / 8] |= + ((esc_src[index / 8] >> (7 - index % 8)) & 1) << (7 - wk % 8); + } +} + +unsigned char *ft_sub_esc(const unsigned char *esc, + int start, int len) +{ + unsigned char *new_esc; + int index; + + if (!(new_esc = (unsigned char *)ft_strnew(((len) / 8) + 1))) + return (NULL); + index = -1; + while (++index < len) + { + new_esc[index / 8] |= + ((esc[start / 8] >> (7 - start % 8)) & 1) << (7 - index % 8); + ++start; + } + return (new_esc); +} diff --git a/42sh/src/glob/ft_strsplit_esc.c b/42sh/src/glob/ft_strsplit_esc.c new file mode 100644 index 00000000..f37c7b14 --- /dev/null +++ b/42sh/src/glob/ft_strsplit_esc.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit_esc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 22:18:46 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:15:17 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +static int ft_strlen_c(const char *str, const char *fix, + const unsigned char *esc, char c) +{ + int len; + int nb; + + len = 0; + nb = 0; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + ++len; + } + return (len); +} + +static int nb_c(const char *str, const unsigned char *esc, char c) +{ + int len; + int nb; + const char *fix; + + len = 0; + nb = 0; + fix = str; + while (*str) + { + while (*str && *str == c) + ++str; + if (*str) + ++len; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + } + } + return (len); +} + +unsigned char **ft_strsplit_esc(const char *str, + const unsigned char *esc, char c) +{ + unsigned char **s1; + int i; + const char *fix; + int len; + int pos; + + if ((pos = -1) && !str) + return (NULL); + if (!(s1 = (unsigned char **)malloc(sizeof(*s1) * (nb_c(str, esc, c) + 1)))) + return (NULL); + i = 0; + fix = str; + while (*str && *str == c && ++pos > -1) + ++str; + while (*str && (len = ft_strlen_c(str, fix, esc, c))) + { + s1[i] = ft_sub_esc(esc, pos, len); + str += len; + pos += len; + ++i; + while (*str && *str == c && ++pos) + ++str; + } + s1[i] = 0; + return (s1); +} diff --git a/42sh/src/glob/ft_strsplit_spe.c b/42sh/src/glob/ft_strsplit_spe.c new file mode 100644 index 00000000..a76d475e --- /dev/null +++ b/42sh/src/glob/ft_strsplit_spe.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit_spe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 20:49:30 by wescande #+# #+# */ +/* Updated: 2017/01/31 23:15:41 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +static int ft_strlen_c(const char *str, const char *fix, + const unsigned char *esc, char c) +{ + int len; + int nb; + + len = 0; + nb = 0; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + ++len; + } + return (len); +} + +static int ft_nbstr_c(const char *str, const unsigned char *esc, char c) +{ + int len; + int nb; + const char *fix; + + len = 0; + nb = 0; + fix = str; + while (*str) + { + while (*str && *str == c) + ++str; + if (*str) + ++len; + while (*str && (*str != c || nb)) + { + nb += *str == '{' && !is_char_esc(esc, fix, str); + nb -= *str == '}' && !is_char_esc(esc, fix, str); + ++str; + } + } + return (len); +} + +char **ft_strsplit_spe(const char *str, + const unsigned char *esc, char c) +{ + char **s1; + int i; + const char *fix; + int len; + + if (!str) + return (NULL); + if (!(s1 = (char**)malloc(sizeof(*s1) * (ft_nbstr_c(str, esc, c) + 1)))) + return (NULL); + i = 0; + fix = str; + while (*str && *str == c) + ++str; + while (*str && (len = ft_strlen_c(str, fix, esc, c))) + { + if (!(s1[i] = (char*)malloc(sizeof(**s1) * (len + 1)))) + return (NULL); + ft_strncpy(s1[i], str, len); + str = str + len; + ++i; + while (*str && *str == c) + ++str; + } + s1[i] = 0; + return (s1); +} diff --git a/42sh/src/glob/glob.c b/42sh/src/glob/glob.c new file mode 100644 index 00000000..1ec41786 --- /dev/null +++ b/42sh/src/glob/glob.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ +/* Updated: 2017/02/01 19:46:22 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +/* +** glob return expansion of a string. +** pattern searched are *, ?, [a-z], [!a-z], [^a-z], {ab, ac}, ** +** input parameters are : +** -char *pat -> pattern string to be looking for match +** -char *esc -> escape binary sequence of *pat +** -char **env -> env var. could be NULL +** to just expanse in local directory and not in path dir +*/ + +static char **treat_glob(t_ld **match) +{ + char **gl; + + gl = NULL; + ft_ld_reverse(match); + gl = ft_ld_to_tab(*match); + ft_ld_clear(match, &ft_strdel); + return (gl); +} + +char **glob(const char *pat, const unsigned char *esc) +{ + t_glob gl; + int ret; + + gl = (t_glob){pat, esc, NULL, NULL}; + expand_brace(&gl); + while (gl.m_pat->next) + { + gl.pat = ((char **)gl.m_pat->content)[0]; + gl.esc = ((unsigned char **)gl.m_pat->content)[1]; + if (gl.pat[0] != '/') + ret = dir_research(&gl, ".", gl.pat, 0); + else + ret = dir_research(&gl, "/", gl.pat + 1, 0); + if (!ret) + ft_ld_pushfront(&gl.match, + ft_strdup(((char **)gl.m_pat->content)[0])); + gl.m_pat = gl.m_pat->next; + } + ft_ld_clear(&gl.m_pat, &ft_tabdel); + return (treat_glob(&gl.match)); +} diff --git a/42sh/src/line-editing/rl_clear_function.c b/42sh/src/glob/is_char_esc.c similarity index 51% rename from 42sh/src/line-editing/rl_clear_function.c rename to 42sh/src/glob/is_char_esc.c index 02305b2d..4ec6f5ae 100644 --- a/42sh/src/line-editing/rl_clear_function.c +++ b/42sh/src/glob/is_char_esc.c @@ -1,32 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* rl_clear_function.c :+: :+: :+: */ +/* is_char_esc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/09 13:34:29 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:36:01 by ariard ### ########.fr */ +/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */ +/* Updated: 2017/02/03 14:38:37 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_readline.h" +#include "glob.h" -int rl_clear_function(t_line *line, long int input) +int is_char_esc(const unsigned char *esc, + const char *ini_str, const char *str_pos) { - int pos; + int pos; - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - curs_clear(&line->curs); - rl_put_prompt(line); - pos = line->pos; - line->pos = 0; - if (line->used) - rl_reset_display_post(line); - if (pos) - curs_forward(&line->curs, pos); - line->pos = pos; + pos = str_pos - ini_str; + if ((esc[pos / 8] >> (7 - pos % 8)) & 1) + return (1); return (0); - (void)input; } diff --git a/42sh/src/glob/lib_perso/ft_ld_back.c b/42sh/src/glob/lib_perso/ft_ld_back.c new file mode 100644 index 00000000..d448f449 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_back.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/16 16:23:26 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:16:53 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_back(t_ld *ld) +{ + if (ld) + while (ld->next) + ld = ld->next; + return (ld); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_clear.c b/42sh/src/glob/lib_perso/ft_ld_clear.c new file mode 100644 index 00000000..962d3c11 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_clear.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/06 19:56:49 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:11 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_clear(t_ld **ld, void (*del)()) +{ + if (!ld || !*ld) + return ; + *ld = ft_ld_front(*ld); + while (*ld) + { + ft_ld_del(ld, del); + } +} diff --git a/42sh/src/line-editing/curs_goto.c b/42sh/src/glob/lib_perso/ft_ld_del.c similarity index 50% rename from 42sh/src/line-editing/curs_goto.c rename to 42sh/src/glob/lib_perso/ft_ld_del.c index 451be308..69852e67 100644 --- a/42sh/src/line-editing/curs_goto.c +++ b/42sh/src/glob/lib_perso/ft_ld_del.c @@ -1,31 +1,36 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* curs_goto.c :+: :+: :+: */ +/* ft_ld_del.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 17:10:22 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:22:56 by ariard ### ########.fr */ +/* Created: 2016/12/06 15:54:53 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:18 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_curs.h" +#include "glob.h" -/* - * Move the cursor to the given coordonate // scroll screen if line is unreachable -*/ - -void curs_goto(t_curs *curs, int li, int co) +void ft_ld_del(t_ld **ld, void (*del)()) { - if (co >= curs->win_co) - co = curs->win_co - 1; - if (li >= curs->win_li) + t_ld *next; + t_ld *prev; + + if (!ld || !*ld) + return ; + next = (*ld)->next; + prev = (*ld)->prev; + if ((*ld)->content && del) + del(&(*ld)->content); + free(*ld); + if (next) + next->prev = prev; + if (prev) { - ft_putstr(tgetstr("sf", NULL)); - li = curs->win_li - 1; + prev->next = next; + *ld = prev; } - ft_putstr(tgoto(tgetstr("cm", NULL), co, li)); - curs->li = li; - curs->co = co; + else + *ld = next; } diff --git a/42sh/src/glob/lib_perso/ft_ld_front.c b/42sh/src/glob/lib_perso/ft_ld_front.c new file mode 100644 index 00000000..1599f49e --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:26:11 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:27 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_front(t_ld *ld) +{ + if (ld) + while (ld->prev) + ld = ld->prev; + return (ld); +} diff --git a/42sh/src/line-editing/curs_single.c b/42sh/src/glob/lib_perso/ft_ld_new.c similarity index 58% rename from 42sh/src/line-editing/curs_single.c rename to 42sh/src/glob/lib_perso/ft_ld_new.c index 5d233d62..b292702b 100644 --- a/42sh/src/line-editing/curs_single.c +++ b/42sh/src/glob/lib_perso/ft_ld_new.c @@ -1,26 +1,26 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* curs_single.c :+: :+: :+: */ +/* ft_ld_new.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 17:04:53 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:23:28 by ariard ### ########.fr */ +/* Created: 2016/12/01 18:17:34 by wescande #+# #+# */ +/* Updated: 2017/02/03 14:38:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_curs.h" +#include "glob.h" -/* - * Save cursor reference or return a previous saved reference -*/ - -t_curs *curs_single(t_curs *curs, int set) +void ft_ld_new(t_ld **alst, void *content) { - static t_curs *single; + t_ld *new; - if (set) - single = curs; - return (single); + if ((new = (t_ld *)malloc(sizeof(t_ld)))) + { + new->content = content; + new->prev = NULL; + new->next = NULL; + *alst = new; + } } diff --git a/42sh/src/glob/lib_perso/ft_ld_order.c b/42sh/src/glob/lib_perso/ft_ld_order.c new file mode 100644 index 00000000..6f6b2a9c --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_order.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_order.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 19:20:45 by wescande #+# #+# */ +/* Updated: 2017/01/24 19:40:54 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_order(t_ld *ld, int (*f)(), void (*del)()) +{ + int swap; + + swap = 1; + ld = ft_ld_front(ld); + while (swap) + { + swap = 0; + while (ld && ld->next) + { + if (f(ld->content, ld->next->content) > 0 && (swap = 1)) + ld = ft_ld_swap(ld); + else if (!f(ld->content, ld->next->content)) + ft_ld_del(&ld, del); + ld = ld->next; + } + ld = ft_ld_front(ld); + } + return (ld); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_pushback.c b/42sh/src/glob/lib_perso/ft_ld_pushback.c new file mode 100644 index 00000000..25a1d019 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_pushback.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_pushback.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/16 15:59:38 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:40 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_pushback(t_ld **alst, void *content) +{ + t_ld *new; + t_ld *back; + + if (!alst || !*alst) + return (ft_ld_new(alst, content)); + if ((new = (t_ld *)malloc(sizeof(t_ld)))) + { + back = ft_ld_back(*alst); + new->prev = back; + new->next = NULL; + new->content = content; + back->next = new; + } +} diff --git a/42sh/src/glob/lib_perso/ft_ld_pushfront.c b/42sh/src/glob/lib_perso/ft_ld_pushfront.c new file mode 100644 index 00000000..5f830ee1 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_pushfront.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_pushfront.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:32:40 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:46 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_pushfront(t_ld **alst, void *content) +{ + t_ld *new; + t_ld *front; + + if (!alst || !*alst) + return (ft_ld_new(alst, content)); + if ((new = (t_ld *)malloc(sizeof(t_ld)))) + { + front = ft_ld_front(*alst); + new->next = front; + new->prev = NULL; + new->content = content; + front->prev = new; + *alst = new; + } +} diff --git a/42sh/src/glob/lib_perso/ft_ld_reverse.c b/42sh/src/glob/lib_perso/ft_ld_reverse.c new file mode 100644 index 00000000..62ffe670 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_reverse.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/12 14:16:04 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:17:53 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_ld_reverse(t_ld **lst) +{ + t_ld *l_prev; + t_ld *l_next; + t_ld *l_cur; + + l_cur = ft_ld_front(*lst); + l_prev = NULL; + while (l_cur) + { + l_next = l_cur->next; + l_cur->next = l_prev; + l_cur->prev = l_next; + l_prev = l_cur; + l_cur = l_next; + } + *lst = l_prev; +} diff --git a/42sh/src/glob/lib_perso/ft_ld_size.c b/42sh/src/glob/lib_perso/ft_ld_size.c new file mode 100644 index 00000000..1a2d8852 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_size.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 18:33:48 by wescande #+# #+# */ +/* Updated: 2017/02/03 14:38:07 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +size_t ft_ld_size(t_ld *ld) +{ + size_t size; + + size = 0; + ld = ft_ld_front(ld); + while (ld) + { + ++size; + ld = ld->next; + } + return (size); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_swap.c b/42sh/src/glob/lib_perso/ft_ld_swap.c new file mode 100644 index 00000000..6e6ff87b --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_swap.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/24 21:12:53 by wescande #+# #+# */ +/* Updated: 2017/01/05 14:18:06 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +t_ld *ft_ld_swap(t_ld *l_cur) +{ + t_ld *l_next; + t_ld *l_prev; + + l_next = l_cur; + if (l_cur && l_cur->next) + { + l_prev = l_cur->prev; + l_next = l_cur->next; + l_cur->next = l_next->next; + if (l_cur->next) + l_cur->next->prev = l_cur; + l_cur->prev = l_next; + l_next->next = l_cur; + l_next->prev = l_prev; + if (l_prev) + l_prev->next = l_next; + } + return (l_next); +} diff --git a/42sh/src/glob/lib_perso/ft_ld_to_tab.c b/42sh/src/glob/lib_perso/ft_ld_to_tab.c new file mode 100644 index 00000000..e6db262d --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_ld_to_tab.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ld_to_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/05 16:17:00 by wescande #+# #+# */ +/* Updated: 2017/01/05 16:22:12 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +char **ft_ld_to_tab(t_ld *ld) +{ + char **my_tab; + int nb_item; + int index; + + if (!ld) + return (NULL); + nb_item = ft_ld_size(ld); + if (!(my_tab = (char **)malloc(sizeof(char *) * (nb_item + 1)))) + return (NULL); + index = 0; + while (ld) + { + my_tab[index++] = ft_strdup((char *)ld->content); + ld = ld->next; + } + my_tab[index] = NULL; + return (my_tab); +} diff --git a/42sh/src/glob/lib_perso/ft_strjoinf.c b/42sh/src/glob/lib_perso/ft_strjoinf.c new file mode 100644 index 00000000..3bc72d56 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_strjoinf.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/05 13:33:24 by wescande #+# #+# */ +/* Updated: 2017/01/24 16:53:13 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +char *ft_strjoinf(char *s1, char *s2, int state) +{ + char *ans; + + ans = ft_strjoin((const char *)s1, (const char *)s2); + if (state == 1 || state == 3) + ft_strdel(&s1); + if (state == 2 || state == 3) + ft_strdel(&s2); + return (ans); +} diff --git a/42sh/src/glob/lib_perso/ft_tabdel.c b/42sh/src/glob/lib_perso/ft_tabdel.c new file mode 100644 index 00000000..d4f9cff0 --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_tabdel.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* glob.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ +/* Updated: 2017/02/02 14:22:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +void ft_tabdel(char ***mytab) +{ + char **erase; + int i; + + if (!mytab || !*mytab) + return ; + erase = *mytab; + i = 0; + while (erase[i]) + { + ft_strdel(&erase[i]); + ++i; + } + free(*mytab); + *mytab = NULL; +} diff --git a/42sh/src/glob/lib_perso/ft_tablen.c b/42sh/src/glob/lib_perso/ft_tablen.c new file mode 100644 index 00000000..3efbee0b --- /dev/null +++ b/42sh/src/glob/lib_perso/ft_tablen.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tablen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/28 16:54:07 by wescande #+# #+# */ +/* Updated: 2016/12/28 16:55:46 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tablen(char **mytab) +{ + int i; + + if (!mytab || !*mytab) + return (0); + i = 0; + while (mytab[i]) + { + ++i; + } + return (i); +} diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c new file mode 100644 index 00000000..b2688603 --- /dev/null +++ b/42sh/src/glob/match_pattern.c @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* match_pattern.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 17:30:23 by wescande #+# #+# */ +/* Updated: 2017/02/01 19:46:43 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "glob.h" + +static int match_bracket_char(char **cmp, const char *pat, char c, int neg) +{ + int dir; + char s; + + if ((*cmp + 2) < pat && (*cmp)[1] == '-' && (s = (*cmp)[0])) + { + dir = s > (*cmp)[2]; + while ((dir && s >= (*cmp)[2]) || (!dir && s <= (*cmp)[2])) + { + if (!neg && s == c) + return (1); + else if (neg && s == c) + return (0); + s += (dir * -2 + 1); + } + *cmp += 2; + } + else if (!neg && **cmp == c) + return (1); + else if (neg && **cmp == c) + return (0); + return (-1); +} + +static int match_bracket(t_glob *gl, char c) +{ + char *cmp; + int neg; + int ret; + + cmp = (char *)gl->pat + 1; + while (*gl->pat != ']' + || is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat)) + { + if (!*gl->pat) + return (0); + ++gl->pat; + } + neg = 0; + if ((*cmp == '^' || *cmp == '!') + && !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], cmp) + && ++neg) + ++cmp; + while (cmp < gl->pat) + { + ret = match_bracket_char(&cmp, gl->pat, c, neg); + if (ret != -1) + return (ret); + ++cmp; + } + return (neg); +} + +static int match_star(t_glob *gl, char *str, char *full_word) +{ + char *fix; + + if (gl->pat[1] == '*' && + !is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat + 1)) + dir_research(gl, full_word, gl->pat + 1, 1); + if (!*++gl->pat) + return (1); + fix = str + ft_strlen(str); + while (fix > str) + { + if (match_pattern(gl, fix, full_word)) + return (1); + --fix; + } + return (0); +} + +const char *manage_pat(const char *pat, char *str) +{ + if (pat[0] == '.' && pat[1] == '/' + && ((str[0] == '.' && str[1] != '/') || str[0] != '.')) + return (pat + 2); + return (pat); +} + +int match_pattern(t_glob *gl, char *str, char *full_word) +{ + gl->pat = manage_pat(gl->pat, str); + while (*gl->pat) + { + if (is_char_esc(gl->esc, ((char **)gl->m_pat->content)[0], gl->pat)) + { + if (*str != *gl->pat) + return (0); + } + else if (*gl->pat == '?') + str++; + else if (*gl->pat == '[') + { + if (!match_bracket(gl, *str)) + return (0); + } + else if (*gl->pat == '*') + return (match_star(gl, str, full_word)); + else if (*gl->pat == '/' && !*str && is_directory(full_word)) + dir_research(gl, full_word, gl->pat + 1, 0); + else if (*gl->pat != *str) + return (0); + ++str; + ++gl->pat; + } + return (*str ? 0 : 1); +} diff --git a/42sh/src/job-control/builtin_bg.c b/42sh/src/job-control/builtin_bg.c index d17ef2ed..a972a19a 100644 --- a/42sh/src/job-control/builtin_bg.c +++ b/42sh/src/job-control/builtin_bg.c @@ -15,14 +15,12 @@ int builtin_bg(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; - t_job *job; t_list *jlist; int rank[2]; int id; (void)path; (void)envp; - (void)av; if (!SHELL_HAS_JOBC(data_singleton()->opts)) { ft_dprintf(2, "{red}bg: %s{eoc}\n", SHELL_MSG_NOJOBC); @@ -31,13 +29,9 @@ int builtin_bg(const char *path, char *const av[], char *const envp[]) jobc = &data_singleton()->jobc; job_getrank(&rank); id = av[1] ? ft_atoi(av[1]) : rank[0]; - jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id); - if (jlist) + if ((jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id))) { - job = jlist->content; - mark_job_as_running(job); - job_format(job, rank, JOBS_OPTS_L); - put_job_in_background(job, 1); + job_run(jlist->content, 0); return (0); } else if (av[1]) diff --git a/42sh/src/job-control/builtin_fg.c b/42sh/src/job-control/builtin_fg.c index 5e6c53b4..48945eca 100644 --- a/42sh/src/job-control/builtin_fg.c +++ b/42sh/src/job-control/builtin_fg.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ -/* Updated: 2017/01/11 14:42:04 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:05:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,14 +15,12 @@ int builtin_fg(const char *path, char *const av[], char *const envp[]) { t_jobc *jobc; - t_job *job; t_list *jlist; int rank[2]; int id; (void)path; (void)envp; - (void)av; if (!SHELL_HAS_JOBC(data_singleton()->opts)) { ft_dprintf(2, "{red}fg: %s{eoc}\n", SHELL_MSG_NOJOBC); @@ -31,12 +29,9 @@ int builtin_fg(const char *path, char *const av[], char *const envp[]) jobc = &data_singleton()->jobc; job_getrank(&rank); id = av[1] ? ft_atoi(av[1]) : rank[0]; - jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id); - if (jlist) + if ((jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id))) { - job = jlist->content; - mark_job_as_running(job); - put_job_in_foreground(job, 1); + job_run(jlist->content, 1); return (0); } else if (av[1]) diff --git a/42sh/src/job-control/builtin_jobs.c b/42sh/src/job-control/builtin_jobs.c index 4009347e..9e3fe05d 100644 --- a/42sh/src/job-control/builtin_jobs.c +++ b/42sh/src/job-control/builtin_jobs.c @@ -6,43 +6,100 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/01/11 14:38:50 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:05:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" - -int builtin_jobs(const char *path, char *const av[], char *const envp[]) +static int bt_jobs_parse(char **av, int *i) { - t_jobc *jobc; - t_list *jlist; - t_list *tmplist; - int rank[2]; - int opts; + int opts; + int j; - (void)path; - (void)envp; - (void)av; - if (!SHELL_HAS_JOBC(data_singleton()->opts)) + opts = 0; + *i = 1; + while (av[*i]) { - ft_dprintf(2, "{red}jobs: %s{eoc}\n", SHELL_MSG_NOJOBC); - return (-1); + j = 0; + if (av[*i][j++] != '-') + break ; + while (av[*i][j]) + { + if (av[*i][j] == 'l') + opts |= JOBS_OPTS_L; + else + { + ft_dprintf(2, "{red}%s: bad option: -%c{eoc}\n", + SHELL_NAME, av[*i][j]); + return (-1); + } + j++; + } + (*i)++; } + return (opts); +} + +static void bt_jobs_all(int opts) +{ + t_jobc *jobc; + t_list *jlist; + t_list *tmplist; + jobc = &data_singleton()->jobc; jlist = jobc->first_job; - job_getrank(&rank); - opts = 0; - if (ft_strcmp(av[1], "-l") == 0) - opts |= JOBS_OPTS_L; tmplist = ft_lst_filter(jlist, NULL, NULL); ft_lstsort(&tmplist, job_cmp_id); jlist = tmplist; while (jlist) { - job_format(jlist->content, rank, opts); + job_format(jlist->content, opts); jlist = jlist->next; } ft_lstdel(&tmplist, ft_lst_cfree); +} + +static int bt_jobs_spec(char **av, int opts) +{ + t_jobc *jobc; + t_list *jlist; + t_list *lst; + int id; + + jobc = &data_singleton()->jobc; + jlist = jobc->first_job; + while (*av) + { + id = ft_atoi(*av); + if (!(lst = ft_lst_find(jlist, &id, job_cmp_id))) + { + ft_dprintf(2, "{red}jobs: %s: no such job{eoc}\n", *av); + return (1); + } + job_format(lst->content, opts); + av++; + } + return (0); +} + +int builtin_jobs(const char *path, char *const av[], char *const envp[]) +{ + int opts; + int i; + + (void)path; + (void)envp; + if (!SHELL_HAS_JOBC(data_singleton()->opts)) + { + ft_dprintf(2, "{red}jobs: %s{eoc}\n", SHELL_MSG_NOJOBC); + return (1); + } + if ((opts = bt_jobs_parse((char**)av, &i)) < 0) + return (1); + if (!av[i]) + bt_jobs_all(opts); + else if (bt_jobs_spec((char **)av + i, opts)) + return (1); return (0); } diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index c748bd3f..b47b83b0 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/01/11 17:35:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:06:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,18 +26,15 @@ int do_job_notification(void) while (jlist) { j = jlist->content; - DG("checking [%i]", j->id); - if (job_is_completed(j->id)) + if (job_is_completed(j->id) + || (job_is_stopped(j->id) && !(j->attributes & JOB_NOTIFIED))) { ret = 1; job_notify_change(j->id); - job_remove(j->id); - } - else if (job_is_stopped(j->id) && !(j->attributes & JOB_NOTIFIED)) - { - ret = 1; - job_notify_change(j->id); - j->attributes |= JOB_NOTIFIED; + if (job_is_completed(j->id)) + job_remove(j->id); + else + j->attributes |= JOB_NOTIFIED; } jlist = jlist->next; } diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 7bd9f659..171df690 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:54:51 by jhalford #+# #+# */ -/* Updated: 2017/01/11 14:40:34 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:07:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,15 +25,13 @@ int job_addprocess(t_process *p) job->id = jobc->current_id; job->pgid = p->pid; ft_lstadd(&jobc->first_job, ft_lstnew(job, sizeof(*job))); - /* DG("added new job [%i]", job->id); */ } job = jobc->first_job->content; if (p->pid > 0) { ft_lsteadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - /* DG("added pid=%i to [%i]", p->pid, job->id); */ } if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); - return(0); + return (0); } diff --git a/42sh/src/job-control/job_cmp_id.c b/42sh/src/job-control/job_cmp_id.c index cc9614d2..d3f6b162 100644 --- a/42sh/src/job-control/job_cmp_id.c +++ b/42sh/src/job-control/job_cmp_id.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:38:13 by jhalford #+# #+# */ -/* Updated: 2017/01/10 11:08:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:06:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,4 +16,3 @@ int job_cmp_id(t_job *job, int *id) { return (job->id - *id); } - diff --git a/42sh/src/job-control/job_format.c b/42sh/src/job-control/job_format.c index 1c8f3dd1..5c811a86 100644 --- a/42sh/src/job-control/job_format.c +++ b/42sh/src/job-control/job_format.c @@ -1,23 +1,23 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* job_format.c :+: :+: :+: */ +/* job_format.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/09 12:47:17 by jhalford #+# #+# */ -/* Updated: 2017/01/10 11:16:50 by jhalford ### ########.fr */ +/* Created: 2017/01/31 15:06:44 by jhalford #+# #+# */ +/* Updated: 2017/01/31 15:06:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void job_format(t_job *j, int rank[2], int opts) +void job_format(t_job *j, int opts) { t_list *plist; int firstp; - job_format_head(j, rank); + job_format_head(j); plist = j->first_process; firstp = 1; while (plist) diff --git a/42sh/src/job-control/job_format_head.c b/42sh/src/job-control/job_format_head.c index 0dfcaaf9..c7b8854f 100644 --- a/42sh/src/job-control/job_format_head.c +++ b/42sh/src/job-control/job_format_head.c @@ -6,16 +6,18 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:10:38 by jhalford #+# #+# */ -/* Updated: 2017/01/09 13:53:48 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 14:53:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void job_format_head(t_job *j, int rank[2]) +void job_format_head(t_job *j) { - char crank; + char crank; + int rank[2]; + job_getrank(&rank); if (j->id == rank[0]) crank = '+'; else if (j->id == rank[1]) diff --git a/42sh/src/job-control/job_getprocess.c b/42sh/src/job-control/job_getprocess.c index a4fa75ba..a5d421c3 100644 --- a/42sh/src/job-control/job_getprocess.c +++ b/42sh/src/job-control/job_getprocess.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_getprocess.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/31 15:07:30 by jhalford #+# #+# */ +/* Updated: 2017/01/31 15:07:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "job_control.h" -t_process *job_getprocess(pid_t pid) +t_list *job_getprocess(pid_t pid) { t_jobc *jobc; t_job *j; @@ -13,7 +25,7 @@ t_process *job_getprocess(pid_t pid) { j = jlist->content; if ((lst = ft_lst_find(j->first_process, &pid, &process_cmp_pid))) - return (lst->content); + return (lst); jlist = jlist->next; } return (NULL); diff --git a/42sh/src/job-control/job_getrank.c b/42sh/src/job-control/job_getrank.c index 8c7b6996..f07263e0 100644 --- a/42sh/src/job-control/job_getrank.c +++ b/42sh/src/job-control/job_getrank.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 12:38:31 by jhalford #+# #+# */ -/* Updated: 2017/01/10 10:30:20 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:04:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_is_completed.c b/42sh/src/job-control/job_is_completed.c index 1895291c..8c9a0f1c 100644 --- a/42sh/src/job-control/job_is_completed.c +++ b/42sh/src/job-control/job_is_completed.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:10:20 by jhalford #+# #+# */ -/* Updated: 2017/01/12 15:15:21 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 13:52:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,12 +25,8 @@ int job_is_completed(int id) while (lst) { p = lst->content; - DG("checking pid=%i", p->pid); if (!(p->attributes & PROCESS_COMPLETED)) - { - DG("process %i is not completed", p->pid); return (0); - } lst = lst->next; } return (1); diff --git a/42sh/src/job-control/job_notify_change.c b/42sh/src/job-control/job_notify_change.c index 73f0a594..fd6c7728 100644 --- a/42sh/src/job-control/job_notify_change.c +++ b/42sh/src/job-control/job_notify_change.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 15:04:03 by jhalford #+# #+# */ -/* Updated: 2017/01/10 11:16:17 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 14:46:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,8 @@ void job_notify_change(int id) { t_job *job; t_jobc *jobc; - int rank[2]; jobc = &data_singleton()->jobc; job = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; - job_getrank(&rank); - job_format(job, rank, JOBS_OPTS_L); + job_format(job, JOBS_OPTS_L); } diff --git a/42sh/src/job-control/job_remove.c b/42sh/src/job-control/job_remove.c index afcfa110..9c94db11 100644 --- a/42sh/src/job-control/job_remove.c +++ b/42sh/src/job-control/job_remove.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:51:08 by jhalford #+# #+# */ -/* Updated: 2017/01/12 15:16:05 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 13:44:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,18 @@ void job_remove(int id) { - t_jobc *jobc; + t_jobc *jobc; + t_job *j; + t_process *p; jobc = &data_singleton()->jobc; + j = ft_lst_find(jobc->first_job, &id, job_cmp_id)->content; if (job_is_completed(id)) { - DG("job_remove"); + p = ft_lstlast(j->first_process)->content; + set_exitstatus(p->status, 0); if (id < data_singleton()->jobc.current_id) data_singleton()->jobc.current_id = id; ft_lst_delif(&jobc->first_job, &id, job_cmp_id, job_free); } - else - DG("job_remove failed (not completed)"); } diff --git a/42sh/src/job-control/job_run.c b/42sh/src/job-control/job_run.c new file mode 100644 index 00000000..bd9d0f48 --- /dev/null +++ b/42sh/src/job-control/job_run.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* job_run.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 16:54:18 by jhalford #+# #+# */ +/* Updated: 2017/01/31 14:47:26 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "job_control.h" + +void job_run(t_job *job, int foreground) +{ + mark_job_as_running(job); + job_format(job, JOBS_OPTS_L); + if (foreground) + put_job_in_foreground(job, 1); + else + put_job_in_background(job, 1); +} diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 5426c5fc..e9f9a24e 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/01/10 12:33:09 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:07:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void job_update_status(void) int status; pid_t pid; - pid = waitpid (WAIT_ANY, &status, WUNTRACED | WNOHANG); + pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); while (!process_mark_status(pid, status)) - pid = waitpid (WAIT_ANY, &status, WUNTRACED | WNOHANG); + pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); } diff --git a/42sh/src/job-control/job_wait.c b/42sh/src/job-control/job_wait.c index 89268b18..62f5ebef 100644 --- a/42sh/src/job-control/job_wait.c +++ b/42sh/src/job-control/job_wait.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 11:49:05 by jhalford #+# #+# */ -/* Updated: 2017/01/12 15:14:38 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 13:44:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,19 +17,15 @@ int job_wait(int id) pid_t pid; int status; - DG("job wait"); - if (job_is_stopped(id) || job_is_completed(id)) + if (job_is_stopped(id)) return (0); - DG("waitpid now"); + job_update_status(); pid = waitpid(WAIT_ANY, &status, WUNTRACED); - DG("waitpid done"); while (!process_mark_status(pid, status) && !job_is_completed(id) && !job_is_stopped(id)) { - DG("waitpid now"); pid = waitpid(WAIT_ANY, &status, WUNTRACED); - DG("waitpid done"); } return (0); } diff --git a/42sh/src/job-control/mark_job_as_running.c b/42sh/src/job-control/mark_job_as_running.c index 0854b8d9..ea82a0e7 100644 --- a/42sh/src/job-control/mark_job_as_running.c +++ b/42sh/src/job-control/mark_job_as_running.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:40:40 by jhalford #+# #+# */ -/* Updated: 2017/01/10 10:52:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:08:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void mark_job_as_running (t_job *j) +void mark_job_as_running(t_job *j) { t_list *plist; t_process *p; diff --git a/42sh/src/job-control/process_cmp_pid.c b/42sh/src/job-control/process_cmp_pid.c index d0b3b09c..7683e116 100644 --- a/42sh/src/job-control/process_cmp_pid.c +++ b/42sh/src/job-control/process_cmp_pid.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 13:00:21 by jhalford #+# #+# */ -/* Updated: 2016/12/12 13:40:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:06:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/process_format.c b/42sh/src/job-control/process_format.c index 7391dba4..25fe10cb 100644 --- a/42sh/src/job-control/process_format.c +++ b/42sh/src/job-control/process_format.c @@ -6,22 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ -/* Updated: 2017/01/12 13:16:45 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:10:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -void process_format(t_list **plist, int firstp, int opts) +static void process_format_state(t_process *p) { - t_process *p; int state; - p = (*plist)->content; - if (!firstp) - ft_printf(" "); - if (opts & JOBS_OPTS_L) - ft_printf("%i ", p->pid); state = p->attributes & PROCESS_STATE_MASK; if (state == PROCESS_RUNNING) ft_putstr("running "); @@ -41,8 +35,41 @@ void process_format(t_list **plist, int firstp, int opts) ft_printf("exit %i ", p->status); } ft_putchar('\t'); - if (opts & JOBS_OPTS_L) +} + +static void process_format_com_long(t_list **plist) +{ + t_process *p; + + p = (*plist)->content; + if (p->attributes & PROCESS_SUBSHELL) { + ft_putstr("( "); + ft_putstr(p->av[2]); + ft_putstr(" )"); + } + else + ft_sstrprint(p->av, ' '); + if ((*plist)->next) + ft_putstr(" |"); + (*plist) = (*plist)->next; +} + +static void process_format_com_short(t_list **plist, t_flag state) +{ + t_process *p; + + while (*plist) + { + p = (*plist)->content; + if (!(p->attributes & state) || + (state == PROCESS_COMPLETED && p->status != 0)) + break ; + if (p->attributes & PROCESS_CONTINUED) + { + p->attributes &= ~PROCESS_STATE_MASK; + p->attributes &= ~PROCESS_RUNNING; + } if (p->attributes & PROCESS_SUBSHELL) { ft_putstr("( "); @@ -52,34 +79,26 @@ void process_format(t_list **plist, int firstp, int opts) else ft_sstrprint(p->av, ' '); if ((*plist)->next) - ft_putstr(" |"); + ft_putstr(" | "); (*plist) = (*plist)->next; } +} + +void process_format(t_list **plist, int firstp, int opts) +{ + t_process *p; + t_flag state; + + p = (*plist)->content; + state = p->attributes & PROCESS_STATE_MASK; + if (!firstp) + ft_printf(" "); + if (opts & JOBS_OPTS_L) + ft_printf("%i ", p->pid); + process_format_state(p); + if (opts & JOBS_OPTS_L) + process_format_com_long(plist); else - { - while (*plist) - { - p = (*plist)->content; - if (!(p->attributes & state) || - (state == PROCESS_COMPLETED && p->status != 0)) - break; - if (p->attributes & PROCESS_CONTINUED) - { - p->attributes &= ~PROCESS_STATE_MASK; - p->attributes &= ~PROCESS_RUNNING; - } - if (p->attributes & PROCESS_SUBSHELL) - { - ft_putstr("( "); - ft_putstr(p->av[2]); - ft_putstr(" )"); - } - else - ft_sstrprint(p->av, ' '); - if ((*plist)->next) - ft_putstr(" | "); - (*plist) = (*plist)->next; - } - } + process_format_com_short(plist, state); ft_putchar('\n'); } diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index ee4824e4..f0f77c0a 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/01/02 19:11:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:45:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/process_mark_status.c b/42sh/src/job-control/process_mark_status.c index 566e95e1..e071c53f 100644 --- a/42sh/src/job-control/process_mark_status.c +++ b/42sh/src/job-control/process_mark_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/01/12 15:14:28 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:10:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,32 +14,31 @@ int process_mark_status(pid_t pid, int status) { + t_list *plist; t_process *p; if (pid > 1) { - if ((p = job_getprocess(pid))) + if ((plist = job_getprocess(pid))) { + p = plist->content; p->status = status; if (WIFSTOPPED(status)) { - DG("marking: pid=%i, status=%i (stopped sig %i)", pid, status, WTERMSIG(status)); p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_SUSPENDED; } else { - DG("marking: pid=%i, status=%i (completed sig %i)", pid, status, WTERMSIG(status)); p->attributes &= ~PROCESS_STATE_MASK; p->attributes |= PROCESS_COMPLETED; if (WIFSIGNALED(status)) ft_printf("{mag}%d: Terminated by signal %d.\n{eoc}", - (int) pid, WTERMSIG(p->status)); + (int)pid, WTERMSIG(status)); } - return(0); + return (0); } ft_dprintf(2, "No child process %d.\n", pid); - return(-1); } - return(-1); + return (-1); } diff --git a/42sh/src/job-control/put_job_in_background.c b/42sh/src/job-control/put_job_in_background.c index a23dee5e..02357157 100644 --- a/42sh/src/job-control/put_job_in_background.c +++ b/42sh/src/job-control/put_job_in_background.c @@ -6,17 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */ -/* Updated: 2017/01/08 13:24:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:09:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int put_job_in_background(t_job *job, int cont) +int put_job_in_background(t_job *j, int cont) { - /* Send the job a continue signal, if necessary. */ if (cont) - if (kill (-job->pgid, SIGCONT) < 0) - perror ("kill (SIGCONT)"); + if (kill(-j->pgid, SIGCONT) < 0) + DG("kill(SIGCONT) failed"); return (0); } diff --git a/42sh/src/job-control/put_job_in_foreground.c b/42sh/src/job-control/put_job_in_foreground.c index 3b0f05d6..187ca02f 100644 --- a/42sh/src/job-control/put_job_in_foreground.c +++ b/42sh/src/job-control/put_job_in_foreground.c @@ -6,41 +6,32 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:58:36 by jhalford #+# #+# */ -/* Updated: 2017/01/12 13:16:48 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:10:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "job_control.h" -int put_job_in_foreground(t_job *job, int cont) +int put_job_in_foreground(t_job *j, int cont) { t_jobc *jobc; jobc = &data_singleton()->jobc; - /* Send the job a continue signal, if necessary. */ + tcsetpgrp(STDIN, j->pgid); + tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); + if (cont) { - /* Put the job into the foreground. */ - signal(SIGTTOU, SIG_IGN); - if (tcsetpgrp(STDIN, job->pgid) == -1) - DG("couldn't put process in control. errno=%i, pgid=%i", errno, job->pgid); - signal(SIGTTOU, sigttou_handler); - tcsetattr (STDIN, TCSANOW, &job->tmodes); - if (kill(-job->pgid, SIGCONT) < 0) - perror("kill (SIGCONT)"); + tcsetattr(STDIN, TCSADRAIN, &j->tmodes); + if (kill(-j->pgid, SIGCONT) < 0) + DG("kill(SIGCONT) failed"); } - /* Wait for it to report. */ - DG("gonna wait for job id=%i", job->id); - job_wait(job->id); - job_remove(job->id); + job_wait(j->id); + job_remove(j->id); - /* Put the shell back in the foreground. */ - signal(SIGTTOU, SIG_IGN); tcsetpgrp(STDIN, jobc->shell_pgid); - signal(SIGTTOU, sigttou_handler); - /* Restore the shell’s terminal modes. */ - tcgetattr(STDIN, &job->tmodes); + tcgetattr(STDIN, &j->tmodes); tcsetattr(STDIN, TCSADRAIN, &jobc->shell_tmodes); return (0); } diff --git a/42sh/src/job-control/sigchld_handler.c b/42sh/src/job-control/sigchld_handler.c index 11800df5..87f795c7 100644 --- a/42sh/src/job-control/sigchld_handler.c +++ b/42sh/src/job-control/sigchld_handler.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 17:37:56 by jhalford #+# #+# */ -/* Updated: 2017/01/10 17:59:28 by jhalford ### ########.fr */ +/* Updated: 2017/01/31 15:09:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,4 @@ void sigchld_handler(int signo) (void)signo; data = data_singleton(); - /* DG("got SIGCHLD"); */ - /* if (do_job_notification()) */ - /* ft_putstr(SHELL_PROMPT); */ - /* if (data_singleton()->mode != MODE_EXEC) */ - /* job_update_status(); */ } diff --git a/42sh/src/job-control/sigint_handler.c b/42sh/src/job-control/sigint_handler.c index 93e6c4cf..85c66843 100644 --- a/42sh/src/job-control/sigint_handler.c +++ b/42sh/src/job-control/sigint_handler.c @@ -15,5 +15,5 @@ void sigint_handler(int signo) { (void)signo; - DG("got SIGINT in process %i", getpid()); + DG("pid:%i got SIGINT", getpid()); } diff --git a/42sh/src/job-control/sigttin_handler.c b/42sh/src/job-control/sigttin_handler.c index b28e84be..2f5f9402 100644 --- a/42sh/src/job-control/sigttin_handler.c +++ b/42sh/src/job-control/sigttin_handler.c @@ -15,5 +15,5 @@ void sigttin_handler(int signo) { (void)signo; - DG("got SIGTTIN"); + DG("got SIGTTIN, pid=%i, pgid=%i", getpid(), getpgrp()); } diff --git a/42sh/src/job-control/sigttou_handler.c b/42sh/src/job-control/sigttou_handler.c index a999d1e1..e89a5641 100644 --- a/42sh/src/job-control/sigttou_handler.c +++ b/42sh/src/job-control/sigttou_handler.c @@ -15,5 +15,5 @@ void sigttou_handler(int signo) { (void)signo; - DG("got SIGTTOU"); + DG("got SIGTTOU, pid=%i, pgid=%i", getpid(), getpgid(getpid())); } diff --git a/42sh/src/lexer/command_getoutput.c b/42sh/src/lexer/command_getoutput.c index da7bdd2a..56becc79 100644 --- a/42sh/src/lexer/command_getoutput.c +++ b/42sh/src/lexer/command_getoutput.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/20 00:31:42 by ariard ### ########.fr */ +======= +/* Updated: 2017/02/02 15:16:25 by jhalford ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/ft_lexer.c b/42sh/src/lexer/ft_lexer.c new file mode 100644 index 00000000..a0abdd90 --- /dev/null +++ b/42sh/src/lexer/ft_lexer.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lexer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 15:30:59 by jhalford #+# #+# */ +/* Updated: 2017/02/02 15:42:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int ft_lexer(t_list **alst, char **command) +{ + int ret; + + ret = 0; + if (!*command) + ret = 1; + else if (ft_tokenize(alst, *command, DEFAULT)) + ret = 1; + else if (ft_post_tokenize(alst, command)) + ret = 1; + ft_strdel(command); + return (ret); +} diff --git a/42sh/src/lexer/ft_post_tokenize.c b/42sh/src/lexer/ft_post_tokenize.c index 881b03fe..e1a1856f 100644 --- a/42sh/src/lexer/ft_post_tokenize.c +++ b/42sh/src/lexer/ft_post_tokenize.c @@ -6,23 +6,44 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/02/02 17:50:45 by ariard ### ########.fr */ +======= +/* Updated: 2017/02/02 15:29:57 by jhalford ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ #include "lexer.h" -int ft_post_tokenize(t_list **alst, char *str) +int ft_post_tokenize(t_list **alst, char **str) { int ret; + t_flag tk; +<<<<<<< HEAD // get_reserverd_words(alst, str); while ((ret = reduce_parens(alst, str))) +======= + while ((ret = reduce_parens(alst, *str))) +>>>>>>> master if (ret == -1) + { + ft_dprintf(2, "{red}%s: parse error near '('{eoc}\n", SHELL_NAME); return (-1); - while ((ret = reduce_bquotes(alst, &str))) + } + tk = TK_PAREN_CLOSE; + if (ft_lst_find(*alst, &tk, token_cmp_type)) + { + ft_dprintf(2, "{red}%s: parse error near ')'{eoc}\n", SHELL_NAME); + return (-1); + } + while ((ret = reduce_bquotes(alst, str))) if (ret == -1) + { + ft_dprintf(2, "{red}%s: parse error near '`'{eoc}\n", SHELL_NAME); return (-1); - DG("new command from bquotes: '%s'", str); + } + DG("new command from bquotes: '%s'", *str); return (0); } diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 6e9aaea2..8e2bf6d4 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/02/02 17:50:28 by ariard ### ########.fr */ +======= +/* Updated: 2017/02/02 15:34:45 by jhalford ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index 996ce9b1..8c8d4b99 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/02/02 17:50:17 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:27:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_backslash.c b/42sh/src/lexer/lexer_backslash.c index 9c94550d..374f5090 100644 --- a/42sh/src/lexer/lexer_backslash.c +++ b/42sh/src/lexer/lexer_backslash.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:49 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/23 23:24:40 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/30 13:07:52 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -18,6 +22,6 @@ int lexer_backslash(t_list **alst, char *str) token = (*alst)->content; token->type = TK_WORD; - token_append(token, str[1]); + token_append(token, str[1], 1); return (ft_tokenize(alst, str + 2, WORD)); } diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 98303077..ae3371f3 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/26 18:16:02 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/27 15:53:10 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -30,6 +34,10 @@ int lexer_default(t_list **alst, char *str) else state = WORD; token = (*alst)->content; +<<<<<<< HEAD +======= + token_append(token, *str, 0); +>>>>>>> master token->type = TK_N_WORD; return (ft_tokenize(alst, str, state)); } diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index 74947d16..d655aca5 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/26 23:37:49 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/26 16:30:43 by jhalford ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_dquote.c b/42sh/src/lexer/lexer_dquote.c index 492c8924..929d48e2 100644 --- a/42sh/src/lexer/lexer_dquote.c +++ b/42sh/src/lexer/lexer_dquote.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/25 22:52:47 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/27 15:53:31 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -23,9 +27,9 @@ int lexer_dquote(t_list **alst, char *str) return (ft_tokenize(alst, str + 1, DEFAULT)); if (*str == '\\') { - token_append(token, *(str + 1)); + token_append(token, *(str + 1), 1); return (lexer_dquote(alst, str + 1)); } - token_append(token, *str); + token_append(token, *str, 1); return (lexer_dquote(alst, str)); } diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 18d65b20..69589914 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/24 19:06:00 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/27 15:54:53 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -17,17 +21,17 @@ int lexer_great(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token_append(token, str[0]); + token_append(token, str[0], 0); if (*(str + 1) == '&') { token->type = TK_GREATAND; - token_append(token, str[1]); + token_append(token, str[1], 0); return (lexer_greatand(alst, str + 2)); } else if (*(str + 1) == '>') { token->type = TK_DGREAT; - token_append(token, str[1]); + token_append(token, str[1], 0); return (ft_tokenize(&(*alst)->next, str + 2, DEFAULT)); } else diff --git a/42sh/src/lexer/lexer_greatand.c b/42sh/src/lexer/lexer_greatand.c index 6ed64cd7..75c78d4a 100644 --- a/42sh/src/lexer/lexer_greatand.c +++ b/42sh/src/lexer/lexer_greatand.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/25 22:52:39 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/27 15:55:04 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -20,12 +24,12 @@ int lexer_greatand(t_list **alst, char *str) token->type = TK_GREATAND; if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_greatand(alst, str + 1)); } else if (*str == '-') { - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_less.c b/42sh/src/lexer/lexer_less.c index dd4fee4d..910cb3af 100644 --- a/42sh/src/lexer/lexer_less.c +++ b/42sh/src/lexer/lexer_less.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/26 18:26:28 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/27 15:55:16 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -17,17 +21,17 @@ int lexer_less(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token_append(token, str[0]); + token_append(token, str[0], 0); if (*(str + 1) == '&') { token->type = TK_LESSAND; - token_append(token, str[1]); + token_append(token, str[1], 0); return (lexer_lessand(alst, str + 2)); } else if (*(str + 1) == '<') { token->type = TK_DLESS; - token_append(token, str[1]); + token_append(token, str[1], 0); return (ft_tokenize(&(*alst)->next, str + 2, DEFAULT)); } else diff --git a/42sh/src/lexer/lexer_lessand.c b/42sh/src/lexer/lexer_lessand.c index 089c78bd..fc6eef56 100644 --- a/42sh/src/lexer/lexer_lessand.c +++ b/42sh/src/lexer/lexer_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:51 by jhalford #+# #+# */ -/* Updated: 2016/12/03 11:58:52 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:27 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,12 +20,12 @@ int lexer_lessand(t_list **alst, char *str) token->type = TK_LESSAND; if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_lessand(alst, str + 1)); } else if (*str == '-') { - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index 31aebba2..5ed39325 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ +<<<<<<< HEAD /* Updated: 2017/01/25 23:05:08 by ariard ### ########.fr */ +======= +/* Updated: 2017/01/30 13:08:05 by wescande ### ########.fr */ +>>>>>>> master /* */ /* ************************************************************************** */ @@ -18,7 +22,6 @@ int lexer_number(t_list **alst, char *str) t_lexstate state; token = (*alst)->content; - DG("*str=%c", *str); if ((state = get_lexer_state(str))) return (ft_tokenize(alst, str, state)); if (*str == '>') @@ -27,7 +30,7 @@ int lexer_number(t_list **alst, char *str) return (ft_tokenize(alst, str, LESS)); else if (ft_isdigit(*str)) { - token_append(token, *str); + token_append(token, *str, 0); return (lexer_number(alst, str + 1)); } return (ft_tokenize(alst, str, DEFAULT)); diff --git a/42sh/src/lexer/lexer_quote.c b/42sh/src/lexer/lexer_quote.c index 401fd07f..4b99e6fe 100644 --- a/42sh/src/lexer/lexer_quote.c +++ b/42sh/src/lexer/lexer_quote.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:08 by jhalford #+# #+# */ -/* Updated: 2017/01/10 15:13:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:52:21 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,6 @@ int lexer_quote(t_list **alst, char *str) str++; if (*str == '\'') return (ft_tokenize(alst, str + 1, WORD)); - token_append(token, *str); + token_append(token, *str, 1); return (lexer_quote(alst, str)); } diff --git a/42sh/src/lexer/lexer_var.c b/42sh/src/lexer/lexer_var.c index 61011338..ab6e0b79 100644 --- a/42sh/src/lexer/lexer_var.c +++ b/42sh/src/lexer/lexer_var.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 14:54:57 by jhalford #+# #+# */ -/* Updated: 2017/01/10 16:36:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:55:54 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int lexer_var(t_list **alst, char *str) token->type = TK_N_WORD; str++; if (!ft_strchr(token->data, '$')) - token_append(token, '$'); + token_append(token, '$', 0); if (!*str) { token_expand_var(token); @@ -32,6 +32,6 @@ int lexer_var(t_list **alst, char *str) token_expand_var(token); return (ft_tokenize(alst, str, state)); } - token_append(token, *str); + token_append(token, *str, 0); return (lexer_var(alst, str)); } diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 6dca613a..ea9e7bb8 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/02/02 17:50:27 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:37:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,6 @@ int lexer_word(t_list **alst, char *str) return (ft_tokenize(&(*alst)->next, str, GREAT)); else if (*str == '<') return (ft_tokenize(&(*alst)->next, str, LESS)); - token_append(token, *str); + token_append(token, *str, 0); return (ft_tokenize(alst, str + 1, WORD)); } diff --git a/42sh/src/lexer/reduce_bquotes.c b/42sh/src/lexer/reduce_bquotes.c index fbe1022b..60915dcb 100644 --- a/42sh/src/lexer/reduce_bquotes.c +++ b/42sh/src/lexer/reduce_bquotes.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 16:46:27 by jhalford #+# #+# */ -/* Updated: 2017/02/02 17:40:29 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:36:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,12 @@ int reduce_bquotes(t_list **alst, char **str) char *bq_end; tk = TK_BQUOTE; + DG("check 0"); if ((start = ft_lst_find(*alst, &tk, token_cmp_type))) { + DG("check 1"); end = &start->next; - while (end) + while (end && *end) { token = (*end)->content; if (token->type == TK_BQUOTE) @@ -35,7 +37,10 @@ int reduce_bquotes(t_list **alst, char **str) end = &(*end)->next; } if (!*end) + { + DG("check 42"); return (-1); + } bq_start = ((t_token*)start->content)->data; bq_end = ((t_token*)(*end)->content)->data; ft_lstdel(end, token_free); diff --git a/42sh/src/lexer/reduce_parens.c b/42sh/src/lexer/reduce_parens.c index 175c1dd6..f3fcf66b 100644 --- a/42sh/src/lexer/reduce_parens.c +++ b/42sh/src/lexer/reduce_parens.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 16:13:31 by jhalford #+# #+# */ -/* Updated: 2017/01/20 18:26:53 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:36:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_append.c b/42sh/src/lexer/token_append.c index 2aabc4c5..c02d1488 100644 --- a/42sh/src/lexer/token_append.c +++ b/42sh/src/lexer/token_append.c @@ -6,19 +6,29 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */ -/* Updated: 2017/01/26 18:07:02 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:36:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" -int token_append(t_token *token, char c) +#define TOKEN_INCR 10 + +int token_append(t_token *token, char c, short int esc) { - if ((int)ft_strlen(token->data) >= token->size) + int len; + + len = ft_strlen(token->data); + if (len >= token->size) { - token->data = (char *)ft_realloc(token->data, token->size + 10); - token->size += 10; + token->size += 8; + token->data = (char *)ft_realloc(token->data, token->size + 1); + token->esc = (unsigned char *)ft_realloc((char *)token->esc, + token->size / 8 + 1); + token->esc[token->size / 8 - 1] = 0; } ft_strcat(token->data, (char[2]){c, '\0'}); + if (esc) + token->esc[len / 8] |= 1 << (7 - len % 8); return (0); } diff --git a/42sh/src/lexer/token_expand_var.c b/42sh/src/lexer/token_expand_var.c index b729611c..9743d062 100644 --- a/42sh/src/lexer/token_expand_var.c +++ b/42sh/src/lexer/token_expand_var.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 14:57:53 by jhalford #+# #+# */ -/* Updated: 2017/01/10 16:37:33 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 15:56:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,5 @@ void token_expand_var(t_token *token) *dollar = 0; if (val) while (*val) - token_append(token, *val++); + token_append(token, *val++, 1); } diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index 88287de8..60fe819d 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */ -/* Updated: 2017/01/31 18:41:48 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:35:52 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,9 @@ void token_free(void *data, size_t size) (void)size; token = data; if (!(token->type & TK_NON_FREEABLE)) + { ft_strdel(&token->data); + ft_memdel((void **)&token->esc); + } free(token); } diff --git a/42sh/src/lexer/token_init.c b/42sh/src/lexer/token_init.c index 1cae40d0..b0340755 100644 --- a/42sh/src/lexer/token_init.c +++ b/42sh/src/lexer/token_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:30:25 by jhalford #+# #+# */ -/* Updated: 2017/01/26 23:36:55 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:35:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,8 @@ t_token *token_init(void) token = (t_token *)malloc(sizeof(t_token)); //non protege token->type = 0; - token->size = 10; - token->data = ft_strnew(token->size); + token->size = 8; + token->data = ft_strnew(token->size + 1); + token->esc = (unsigned char *)ft_strnew(token->size / 8 + 1); return (token); } diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 285c77a7..148e33f9 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/01/24 01:25:27 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:35:21 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ void token_print(t_list *lst) t_token *token; int i; t_type type; + int index; while (lst) { @@ -26,6 +27,7 @@ void token_print(t_list *lst) while (type >> (i++ + 2)) ; DG("%02i '%s'", i, token->data); + index = -1; lst = lst->next; } } diff --git a/42sh/src/line-editing/builtin_history.c b/42sh/src/line-editing/builtin_history.c new file mode 100644 index 00000000..019af98e --- /dev/null +++ b/42sh/src/line-editing/builtin_history.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_history.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 14:54:53 by gwojda #+# #+# */ +/* Updated: 2017/01/26 14:58:41 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int builtin_history(const char *path, char *const av[], char *const envp[]) +{ + size_t len; + t_list_history *head; + + (void)path; + (void)av; + (void)envp; + head = data_singleton()->line.list_end; + len = 1; + if (!head) + return (0); + if (head && !head->str) + head = head->next; + while (head && head->str) + { + ft_putnc(' ', ft_nbr_len(data_singleton()->line.list_size) + - ft_nbr_len(len)); + ft_printf("%zu %s\n", len, head->str); + ++len; + head = head->next; + } + return (0); +} diff --git a/42sh/src/line-editing/check_backslash.c b/42sh/src/line-editing/check_backslash.c new file mode 100644 index 00000000..1ab0256e --- /dev/null +++ b/42sh/src/line-editing/check_backslash.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_backslash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:01:25 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_check_backslash(char **str) +{ + char *tmp1; + char *tmp2; + + if (!*str || !**str) + return ; + if ((*str)[ft_strlen(*str) - 1] == '\\') + { + ft_putstr("> "); + data_singleton()->line.prompt_size = 2; + tmp1 = *str; + tmp2 = ft_strjoin(tmp1, "\n"); + free(tmp1); + tmp1 = ft_read_stdin(); + *str = ft_strjoin(tmp2, tmp1); + free(tmp1); + free(tmp2); + ft_putchar('\n'); + } +} diff --git a/42sh/src/line-editing/control_c_and_d.c b/42sh/src/line-editing/control_c_and_d.c new file mode 100644 index 00000000..a194e7a0 --- /dev/null +++ b/42sh/src/line-editing/control_c_and_d.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* control_c_and_d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 15:17:28 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:43:15 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_buff_f6(void) +{ + int ret; + + ret = 0; + read(0, &ret, sizeof(int)); +} + +void ft_control_d(void) +{ + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!(*str) || (*str)[0] == '\0') + builtin_exit(NULL, (char*[]){"exit", NULL}, NULL); + else if (*pos < ft_strlen(*str)) + ft_del(); + else + ft_puttermcaps("bl"); +} + +void ft_control_c(void) +{ + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + ft_putchar('\n'); + ft_prompt(); + ft_strdel(str); + *pos = 0; +} diff --git a/42sh/src/line-editing/copy_cut_paste.c b/42sh/src/line-editing/copy_cut_paste.c new file mode 100644 index 00000000..587469bb --- /dev/null +++ b/42sh/src/line-editing/copy_cut_paste.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* copy_cut_paste.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:10:51 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static char *ft_strdupi_space(char const *s) +{ + int i; + char *str; + + i = 0; + while (s[i] && s[i] != '\n') + ++i; + if (s[i] == '\n') + ++i; + str = (char *)malloc(sizeof(char) * (i + 1)); + if (str) + { + str[i] = '\0'; + --i; + while (i >= 0) + { + str[i] = s[i]; + --i; + } + } + return (str); +} + +void ft_v(void) +{ + size_t tmp_pos; + int i; + char *tmp; + char **str; + size_t *pos; + + tmp = data_singleton()->line.copy_tmp; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + i = 0; + tmp_pos = *pos; + if (!*str || !tmp) + return ; + while (tmp[i]) + { + *str = ft_realloc_imput(*str, tmp[i], *pos + i); + ++i; + } + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_current_str(*str, *pos); + ft_get_next_str(*str, pos); + ft_putnc('\b', *pos - tmp_pos); + (*pos) = tmp_pos; +} + +void ft_x(void) +{ + int i; + char **tmp; + char **str; + size_t *pos; + + tmp = &data_singleton()->line.copy_tmp; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!*str) + return ; + if (*tmp) + ft_strdel(tmp); + *tmp = ft_strdupi_space(&(*str)[(*pos)]); + i = ft_strlen(*tmp); + while (i >= 0) + { + *str = ft_remove_imput(*str, *pos + i); + --i; + } + ft_puttermcaps("cd"); +} + +void ft_c(void) +{ + char *tmp; + char **str; + size_t *pos; + + tmp = data_singleton()->line.copy_tmp; + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (tmp) + ft_strdel(&tmp); + tmp = ft_strdupi_space((*str) + (*pos)); +} diff --git a/42sh/src/line-editing/ft_split_whitespaces.c b/42sh/src/line-editing/ft_split_whitespaces.c new file mode 100644 index 00000000..05506cfb --- /dev/null +++ b/42sh/src/line-editing/ft_split_whitespaces.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_whitespaces.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/02 17:52:52 by gwojda #+# #+# */ +/* Updated: 2017/01/25 15:49:21 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_strdupi_w(char const *s) +{ + int i; + char *str; + + i = 0; + while (s[i] && s[i] != ' ' && s[i] != '\t') + i++; + str = (char *)malloc(sizeof(char) * (i + 1)); + if (str) + { + str[i--] = '\0'; + while (i >= 0) + { + str[i] = s[i]; + i--; + } + } + return (str); +} + +static int ft_len_words(char const *s) +{ + int i; + int len; + + i = 0; + len = 0; + while (s[i]) + { + if ((i == 0 && s[i] != ' ' && s[i] != '\t') || ((s[i] != ' ' + && s[i] != '\t') && (s[i - 1] == ' ' || s[i - 1] == '\t'))) + len++; + i++; + } + return (len); +} + +char **ft_split_whitespaces(char const *s) +{ + int i; + char **str; + int j; + + str = NULL; + if (!(s)) + return (str); + str = (char **)malloc(sizeof(char *) * (ft_len_words(s) + 1)); + i = 0; + j = 0; + if (!(str)) + return (str); + while (s[i]) + { + if ((i == 0 && s[i] != ' ' && s[i] != '\t') || ((s[i] != ' ' + && s[i] != '\t') && (s[i - 1] == ' ' || s[i - 1] == '\t'))) + { + str[j] = ft_strdupi_w((s + i)); + j++; + } + i++; + } + str[j] = NULL; + return (str); +} diff --git a/42sh/src/line-editing/get_touch.c b/42sh/src/line-editing/get_touch.c new file mode 100644 index 00000000..2ecde44c --- /dev/null +++ b/42sh/src/line-editing/get_touch.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_touch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ +/* Updated: 2017/02/02 18:27:07 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_key g_key[] = +{ + {FLECHE_HAUT , &ft_history_up }, + {FLECHE_BAS , &ft_history_down }, + {FLECHE_GAUCHE , &ft_move_left }, + {FLECHE_DROITE , &ft_move_right }, + {TOUCHE_DELETE , &ft_del }, + {TOUCHE_CTRL_C , &ft_control_c }, + {TOUCHE_CTRL_D , &ft_control_d }, + {TOUCHE_CTRL_R , &ft_history_parsing }, + {TOUCHE_SUPPR , &ft_suppr }, + {TOUCHE_HOME , &ft_home }, + {TOUCHE_END , &ft_end }, + {TOUCHE_OPT_UP , &ft_up }, + {TOUCHE_OPT_DOWN , &ft_down }, + {TOUCHE_OPT_LEFT , &ft_found_prev_word }, + {TOUCHE_OPT_RIGHT , &ft_found_next_word }, + {TOUCHE_OPT_C , &ft_c }, + {TOUCHE_OPT_V , &ft_v }, + {TOUCHE_OPT_X , &ft_x }, + {TOUCHE_F5 , &ft_printall }, + {TOUCHE_F6 , &ft_buff_f6 }, + {0 , 0 }, +}; + +char *ft_read_stdin(void) +{ + int ret; + int j; + + if (data_singleton()->line.input) + { + ft_current_str(data_singleton()->line.input, data_singleton()->line.pos); + ft_get_next_str(data_singleton()->line.input, &data_singleton()->line.pos); + if (data_singleton()->line.input[data_singleton()->line.pos]) + ++(data_singleton()->line.pos); + } + while (42) + { + ret = 0; + j = 0; + read(0, &ret, sizeof(int)); + while (g_key[j].value && g_key[j].value != ret) + ++j; + if (g_key[j].value) + g_key[j].f(); + else if (ft_isprint(ret)) + ft_print(ret); + else if (ret == 10) + return (data_singleton()->line.input); + else if (ft_isascii(ret) == 0) + ft_read_it(ret, &data_singleton()->line.pos, + &data_singleton()->line.input); + } +} diff --git a/42sh/src/line-editing/get_touch_toolz.c b/42sh/src/line-editing/get_touch_toolz.c new file mode 100644 index 00000000..e5937fac --- /dev/null +++ b/42sh/src/line-editing/get_touch_toolz.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_touch_toolz.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/19 16:42:54 by gwojda #+# #+# */ +/* Updated: 2017/02/02 15:22:35 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_found_prev_word(void) +{ + int i; + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + i = 0; + if (!*pos) + return ; + if (str[*pos - 1] != '\n' && !(str[*pos] == '\n' || str[*pos] == ' ')) + { + ft_puttermcaps("le"); + --(*pos); + } + while ((int)*pos - i - 1 > 0 && str[*pos - i - 1] == ' ') + { + ft_puttermcaps("le"); + ++i; + } + (*pos) = (i && str[*pos - i] == '\n') ? *pos - i + 1 : *pos - i; + i = 0; + if (*pos >= 1 && str[*pos - 1] == '\n') + { + ft_puttermcaps("cd"); + if (*pos >= 2) + (*pos) -= 2; + ft_get_beggin(str, pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + ++(*pos); + } + else + { + if (*pos && !(str[*pos] == '\n' || str[*pos] == ' ')) + { + ft_puttermcaps("le"); + --(*pos); + } + while (*pos - i && (str[*pos - i] == '\n' || str[*pos - i] == ' ')) + { + ft_puttermcaps("le"); + ++i; + } + while (*pos - i && str[*pos - i] != '\n' && str[*pos - i] != ' ') + { + ft_puttermcaps("le"); + ++i; + } + if (str[*pos - i] == '\n' || str[*pos - i] == ' ') + { + ft_puttermcaps("nd"); + ++(*pos); + } + (*pos) -= i; + } +} + +void ft_found_next_word(void) +{ + int i; + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + i = 0; + while (str[i + *pos] && str[i + *pos] == ' ') + { + ft_putchar(str[i + *pos]); + ++i; + } + if (str[*pos] == '\n') + { + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(str, pos); + } + ft_puttermcaps("cd"); + ft_get_next_str(str, pos); + (*pos) += (*pos) ? 2 : 1; + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + if (!str[*pos]) + --(*pos); + ft_get_beggin_with_curs(str, pos); + } + else + { + while (str[i + *pos] && str[i + *pos] != '\n' && str[i + *pos] != ' ') + { + ft_putchar(str[i + *pos]); + ++i; + } + *pos += i; + } +} diff --git a/42sh/src/line-editing/get_touch_toolz_2.c b/42sh/src/line-editing/get_touch_toolz_2.c new file mode 100644 index 00000000..05805aa2 --- /dev/null +++ b/42sh/src/line-editing/get_touch_toolz_2.c @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_touch_toolz_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/19 16:43:58 by gwojda #+# #+# */ +/* Updated: 2017/02/02 17:54:27 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_put(int nb) +{ + write(1, &nb, 1); + return (1); +} + +void ft_end(void) +{ + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!str) + return ; + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(str, pos); + } + ft_puttermcaps("cd"); + while (str[*pos]) + ++(*pos); + ft_get_beggin(str, pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); +} + +void ft_home(void) +{ + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!str) + return ; + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(str, pos); + if (str[*pos + 1] == '\n') + ft_puttermcaps("nd"); + } + ft_puttermcaps("cd"); + *pos = 0; + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + ft_check_end_of_line(str, *pos); + if (!str[*pos]) + --(*pos); + ft_get_beggin_with_curs(str, pos); +} + +void ft_move_right(void) +{ + size_t tmp; + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (ft_strlen(str) <= *pos) + return ; + if (str[*pos] == '\n') + { + if (*pos) + { + tmp = *pos - 1; + ft_get_beggin_with_curs(str, &tmp); + } + ft_puttermcaps("cd"); + ++(*pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + if (!str[*pos]) + --(*pos); + ft_get_beggin_with_curs(str, pos); + } + else + { + ft_putchar(str[*pos]); + ++(*pos); + } +} + +void ft_move_left(void) +{ + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!*pos) + return ; + if (str[*pos - 1] == '\n') + { + if (*pos - 1 == 0) + { + ft_puttermcaps("cd"); + --(*pos); + return ; + } + ft_puttermcaps("cd"); + (*pos) -= 2; + ft_get_beggin(str, pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + ++(*pos); + } + else + { + ft_puttermcaps("le"); + --(*pos); + } +} diff --git a/42sh/src/line-editing/heredoc.c b/42sh/src/line-editing/heredoc.c new file mode 100644 index 00000000..7637dcbd --- /dev/null +++ b/42sh/src/line-editing/heredoc.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* heredoc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/24 15:52:34 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:09:42 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_get_next_word(char *str) +{ + int j; + int k; + + j = 0; + k = 0; + while (str[j] && str[j] == ' ') + ++j; + while (str[j + k] && str[j + k] != ' ') + ++k; + return (ft_strndup(str + j, k)); +} + +static void ft_check_heredoc_2(char **str, char *end) +{ + char boolean; + char *tmp; + char *tmp2; + + boolean = 0; + while (!boolean) + { + ft_putstr("heredoc> "); + data_singleton()->line.prompt_size = 9; + tmp = *str; + *str = ft_strjoin(*str, "\n"); + free(tmp); + tmp = *str; + tmp2 = ft_read_stdin(); + if (!ft_strcmp(end, tmp2)) + boolean = 1; + *str = ft_strjoin(tmp, tmp2); + free(tmp); + free(tmp2); + ft_putchar('\n'); + } +} + +void ft_check_heredoc(char **str) +{ + int i; + char *end; + + i = 0; + end = NULL; + if (!*str) + return ; + while ((*str)[i]) + { + if ((*str)[i] == '<' && !ft_strncmp(*str + i, "<<", 2)) + { + if ((*str)[i + 1] == '<' && (*str)[i + 2]) + end = ft_get_next_word(*str + i + 2); + else + return ; + } + ++i; + } + if (!end) + return ; + ft_check_heredoc_2(str, end); +} diff --git a/42sh/src/line-editing/history.c b/42sh/src/line-editing/history.c new file mode 100644 index 00000000..295af73e --- /dev/null +++ b/42sh/src/line-editing/history.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */ +/* Updated: 2017/02/02 18:04:16 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_history_down(void) +{ + t_list_history *head; + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + head = data_singleton()->line.list_cur; + if (!head || !*str) + return ; + if (*str) + { + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_puttermcaps("cd"); + *pos = 0; + ft_strdel(str); + } + if (head->next) + head = head->next; + if (!head->str) + *str = NULL; + else + *str = ft_strdup(head->str); + if (*str) + ft_current_str(*str, *pos); + if (*str) + *pos = ft_strlen_next(*str, *pos); + else + *pos = 0; + data_singleton()->line.list_cur = head; +} + +void ft_history_up(void) +{ + t_list_history *head; + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + head = data_singleton()->line.list_cur; + if (!head) + return ; + if (*str) + { + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } + ft_puttermcaps("cd"); + *pos = 0; + ft_strdel(str); + } + if (head->prev) + head = head->prev; + if (!head->str) + *str = NULL; + else + *str = ft_strdup(head->str); + if (*str) + ft_current_str(*str, *pos); + if (*str) + *pos = ft_strlen_next(*str, *pos); + else + *pos = 0; + data_singleton()->line.list_cur = head; +} diff --git a/42sh/src/line-editing/history_parsing.c b/42sh/src/line-editing/history_parsing.c new file mode 100644 index 00000000..bd3f67d4 --- /dev/null +++ b/42sh/src/line-editing/history_parsing.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:20:26 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int ft_history_parsing_4(char *str, int *i) +{ + if (!ft_strncmp("!!", str + *i, 2)) + { + ft_realloc_str_history(&(data_singleton()->line.input), *i, 0, 2); + ++(*i); + return (1); + } + else if (ft_isdigit(str[(*i) + 1])) + { + ft_realloc_str_history(&(data_singleton()->line.input), *i, + ft_atoi(str + (*i) + 1), ft_nbr_len(ft_atoi(str + *i + 1)) + 1); + (*i) += ft_nbr_len(ft_atoi(str + *i + 1)); + return (1); + } + return (0); +} + +static int ft_history_parsing_3(char *str, int *i) +{ + if (ft_history_parsing_4(str, i)) + return (1); + else if (str[*i + 1] == '-') + { + ft_realloc_str_history(&(data_singleton()->line.input), *i, + data_singleton()->line.list_size - ft_atoi(str + *i + 2), + ft_nbr_len(ft_atoi(str + *i + 2)) + 2); + (*i) += ft_nbr_len(ft_atoi(str + *i + 2) + 1); + } + else if (str[*i + 1] == '?') + ft_realloc_str_history_3(&(data_singleton()->line.input), *i, + ft_strdupi_w(str + *i + 2)); + else if (str[*i + 1] != ' ') + ft_realloc_str_history_2(&(data_singleton()->line.input), *i, + ft_strdupi_w(str + *i + 1)); + else + return (0); + return (1); +} + +static void ft_history_parsing_2(void) +{ + data_singleton()->line.opt = data_singleton()->line.opt | HIST; + ft_prompt(); + data_singleton()->line.input = ft_read_stdin(); + ft_putchar('\n'); + data_singleton()->line.opt = data_singleton()->line.opt | ~HIST; + ft_check_quotes(&data_singleton()->line.input); + ft_check_heredoc(&data_singleton()->line.input); + ft_history_parsing(); +} + +void ft_history_parsing(void) +{ + char *str; + int i; + char boolean; + + i = 0; + boolean = 0; + str = data_singleton()->line.input; + if (!str) + return ; + while (str[i]) + { + if (str[i] == '!') + { + boolean = 1; + if (!ft_history_parsing_3(str, &i)) + boolean = 0; + } + ++i; + } + if (boolean) + ft_history_parsing_2(); +} diff --git a/42sh/src/line-editing/history_parsing_toolz.c b/42sh/src/line-editing/history_parsing_toolz.c new file mode 100644 index 00000000..31a4b818 --- /dev/null +++ b/42sh/src/line-editing/history_parsing_toolz.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history_parsing_toolz.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 14:09:39 by gwojda #+# #+# */ +/* Updated: 2017/01/25 14:41:29 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static char *ft_nget_histo(size_t nb_his) +{ + t_list_history *list; + size_t i; + + i = 0; + list = data_singleton()->line.list_beg; + if (!list) + return (NULL); + if (!list->str) + list = list->prev; + while (i < nb_his && list->str) + { + list = list->prev; + ++i; + } + return (list->str); +} + +void ft_realloc_str_history(char **str, size_t pos, int nb_his, int len) +{ + char *new_str; + char *new_str2; + char *new_str3; + + if (!*str) + return ; + new_str = ft_strndup(*str, pos); + ft_printf("\n\npos = %d\n\n", len); + new_str3 = ft_nget_histo(nb_his); + if (new_str3) + { + new_str2 = ft_strjoin(new_str, new_str3); + free(new_str); + new_str3 = ft_strjoin(new_str2, (*str) + pos + len); + free(new_str2); + } + else + new_str3 = ft_strjoin(new_str, (*str) + pos + len); + free(*str); + *str = new_str3; +} + +static char *ft_strget_histo(char *str) +{ + t_list_history *list; + + list = data_singleton()->line.list_beg; + if (!list) + return (NULL); + if (!list->str) + list = list->prev; + while (list->str && ft_strncmp(list->str, str, ft_strlen(str))) + list = list->prev; + return (list->str); +} + +void ft_realloc_str_history_2(char **str, size_t pos, char *s) +{ + char *new_str; + char *new_str2; + char *new_str3; + + if (!*str) + return ; + new_str = ft_strndup(*str, pos); + new_str3 = ft_strget_histo(s); + if (new_str3) + { + new_str2 = ft_strjoin(new_str, new_str3); + free(new_str); + new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 1); + free(new_str2); + } + else + new_str3 = ft_strjoin(new_str, (*str) + pos + ft_strlen(s) + 1); + free(s); + free(*str); + *str = new_str3; +} diff --git a/42sh/src/line-editing/history_parsing_toolz_2.c b/42sh/src/line-editing/history_parsing_toolz_2.c new file mode 100644 index 00000000..23ce77ae --- /dev/null +++ b/42sh/src/line-editing/history_parsing_toolz_2.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* history_parsing_toolz_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 20:04:16 by gwojda #+# #+# */ +/* Updated: 2017/01/26 11:33:22 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_strget_history(char *str) +{ + t_list_history *list; + size_t i; + char *tmp; + + list = data_singleton()->line.list_beg; + if (!list) + return (NULL); + if (!list->str) + list = list->prev; + while (list->str) + { + tmp = list->str; + i = 0; + while (tmp[i]) + { + if (ft_strlen(tmp + i) >= ft_strlen(str) + && !ft_strncmp(tmp + i, str, ft_strlen(str))) + return (tmp); + ++i; + } + list = list->prev; + } + return (list->str); +} + +void ft_realloc_str_history_3(char **str, size_t pos, char *s) +{ + char *new_str; + char *new_str2; + char *new_str3; + + if (!*str) + return ; + new_str = ft_strndup(*str, pos); + new_str3 = ft_strget_history(s); + if (new_str3) + { + new_str2 = ft_strjoin(new_str, new_str3); + free(new_str); + new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 2); + free(new_str2); + } + else + new_str3 = ft_strjoin(new_str, (*str) + pos + ft_strlen(s) + 2); + free(s); + free(*str); + *str = new_str3; +} diff --git a/42sh/src/line-editing/list_toolz.c b/42sh/src/line-editing/list_toolz.c new file mode 100644 index 00000000..51bde359 --- /dev/null +++ b/42sh/src/line-editing/list_toolz.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_toolz.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/17 11:37:47 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:08:01 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_list_history *ft_create_history_list(char *str) +{ + t_list_history *list; + + list = (t_list_history *)malloc(sizeof(t_list_history)); + if (!list) + return (NULL); + if (!str) + list->str = NULL; + else + list->str = ft_strdup(str); + list->prev = NULL; + list->next = NULL; + return (list); +} + +void ft_push_back_history(t_list_history **head, t_list_history *new) +{ + ++(data_singleton()->line.list_size); + if (!*head) + { + *head = new; + (*head)->prev = ft_create_history_list(NULL); + (*head)->prev->next = (*head); + (*head)->next = ft_create_history_list(NULL); + (*head)->next->prev = (*head); + (*head) = (*head)->next; + (data_singleton())->line.list_end = new; + return ; + } + while ((*head)->next) + (*head) = (*head)->next; + new->prev = (*head)->prev; + (*head)->prev->next = new; + new->next = (*head); + (*head)->prev = new; +} diff --git a/42sh/src/line-editing/move_to_line.c b/42sh/src/line-editing/move_to_line.c new file mode 100644 index 00000000..c8161cfb --- /dev/null +++ b/42sh/src/line-editing/move_to_line.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move_to_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */ +/* Updated: 2017/02/02 15:07:08 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void ft_up_2(size_t *pos, char *str) +{ + int len; + int i; + + i = 0; + len = ft_size_term(); + if (str[*pos - i] == '\n') + { + --len; + ft_puttermcaps("le"); + ++i; + } + while (*pos - i && str[*pos - i] != '\n' && --len + 1) + { + ft_puttermcaps("le"); + ++i; + } + if (str[*pos - i] == '\n') + { + ft_puttermcaps("nd"); + ++(*pos); + } + (*pos) -= i; +} + +void ft_up(void) +{ + int i; + char *str; + size_t *pos; + + i = 0; + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!str) + return ; + if (str[*pos - 1] == '\n') + { + ft_puttermcaps("cd"); + (*pos) -= 2; + ft_get_beggin(str, pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + ++(*pos); + } + else + ft_up_2(pos, str); +} + +static void ft_down_2(size_t *pos, char *str) +{ + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(str, pos); + } + ft_puttermcaps("cd"); + ft_get_next_str(str, pos); + (*pos) += 2; + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + if (!(str[*pos])) + --(*pos); + ft_get_beggin_with_curs(str, pos); +} + +void ft_down(void) +{ + int i; + int len; + char *str; + size_t *pos; + + i = 0; + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + if (!str) + return ; + if (str[*pos] == '\n') + ft_down_2(pos, str); + else + { + len = ft_size_term(); + while (str[i + *pos] && str[i + *pos] != '\n' && --len + 1) + { + ft_putchar(str[i + *pos]); + ++i; + } + *pos += i; + } +} diff --git a/42sh/src/line-editing/print_and_del.c b/42sh/src/line-editing/print_and_del.c new file mode 100644 index 00000000..ac352ae1 --- /dev/null +++ b/42sh/src/line-editing/print_and_del.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_and_del.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/05 16:02:43 by gwojda #+# #+# */ +/* Updated: 2017/02/02 15:10:50 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_found_next_char(char *str, size_t i) +{ + while (str[i]) + { + if (str[i] == '\n') + return (1); + ++i; + } + return (0); +} + +void ft_print(int ret) +{ + int j; + char **str; + size_t *i; + + j = 0; + str = &data_singleton()->line.input; + i = &data_singleton()->line.pos; + *str = ft_realloc_imput(*str, ret, *i); + while (*((*str) + *i + j) && *((*str) + *i + j) != '\n') + { + ft_putchar(*((*str) + *i + j)); + ++j; + } + ft_check_end_of_line(*str, *i + j); + ft_putnc('\b', j - 1); + ++(*i); +} + +void ft_suppr_2(char **str, size_t *i, size_t tmp) +{ + ft_puttermcaps("cd"); + ft_current_str(*str, *i); + ft_get_next_str(*str, i); + if (*i && (*str)[*i] && ft_found_next_char(*str, *i)) + ++(*i); + ft_putnc('\b', *i - tmp); + (*i) = tmp; + if (ft_strlen(*str) == 0) + ft_strdel(str); +} + +void ft_suppr(void) +{ + size_t tmp; + char boolean; + char **str; + size_t *i; + + str = &data_singleton()->line.input; + i = &data_singleton()->line.pos; + boolean = 0; + if (*i <= 0) + return ; + if ((*str)[*i - 1] != '\n') + boolean = 1; + --(*i); + tmp = *i; + if (boolean) + { + ft_get_beggin_with_curs(*str, i); + *str = ft_remove_imput((*str), tmp); + } + else + { + *str = ft_remove_imput((*str), tmp); + ft_get_beggin(*str, i); + } + ft_suppr_2(str, i, tmp); +} + +void ft_del(void) +{ + size_t tmp; + char **str; + size_t *i; + + str = &data_singleton()->line.input; + i = &data_singleton()->line.pos; + tmp = *i; + *str = ft_remove_imput((*str), tmp); + if (!((*str) && (*i) < ft_strlen((*str)))) + return ; + if (*i) + { + --(*i); + ft_get_beggin_with_curs(*str, i); + } + ft_puttermcaps("cd"); + ft_current_str(*str, *i); + ft_get_next_str(*str, i); + if ((*str)[*i]) + ++(*i); + ft_putnc('\b', *i - tmp); + (*i) = tmp; +} diff --git a/42sh/src/line-editing/prompt.c b/42sh/src/line-editing/prompt.c new file mode 100644 index 00000000..94482a27 --- /dev/null +++ b/42sh/src/line-editing/prompt.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* prompt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ +/* Updated: 2017/01/25 16:31:24 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +/* +**ft_printf de la lib bug avec unicode +*/ + +static void ft_get_date(void) +{ + time_t t; + struct tm tm; + + t = time(NULL); + tm = *localtime(&t); + ft_putstr("\033[22;32m"); + printf("%.2d:%.2d:%.2d ", tm.tm_hour, tm.tm_min, tm.tm_sec); + fflush(NULL); + if (tm.tm_hour >= 8 && tm.tm_hour < 20) + printf("\033[22;33m%C ", L'☀'); + else + printf("\033[22;33m%C ", L'★'); + fflush(NULL); +} + +static int ft_currend_dir(void) +{ + int i; + int j; + char **env; + + i = 0; + env = data_singleton()->env; + while (ft_strncmp(env[i], "PWD=", 4)) + ++i; + j = ft_strlen(env[i]); + while (j && env[i][j] != '/') + --j; + ft_putstr(env[i] + j + 1); + return (ft_strlen(env[i] + j + 1)); +} + +void ft_prompt(void) +{ + int ret; + + ft_get_date(); + ft_putstr("\033[22;36m"); + ret = ft_currend_dir(); + ft_putstr("\033[22;33m"); + printf(" %C", L'›'); + fflush(NULL); + ft_putstr("\033[22;37m"); + data_singleton()->line.prompt_size = ret + 12; +} diff --git a/42sh/src/line-editing/quotes_gest.c b/42sh/src/line-editing/quotes_gest.c new file mode 100644 index 00000000..c4be15f9 --- /dev/null +++ b/42sh/src/line-editing/quotes_gest.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* quotes_gest.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/05 12:20:19 by gwojda #+# #+# */ +/* Updated: 2017/02/02 17:53:41 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_check_quotes_num(char *s) +{ + int i; + char simp; + char doub; + + i = 0; + simp = 0; + doub = 0; + while (s[i]) + { + if (s[i] == '\'' && ((i && s[i - 1] != '\\') || !i)) + { + simp = 1; + ++i; + while (s[i] && !(s[i] == '\'' && ((i && s[i - 1] != '\\') || !i))) + ++i; + if (s[i] == '\'' && ((i && s[i - 1] != '\\') || !i)) + simp = 0; + } + else if (s[i] == '"' && ((i && s[i - 1] != '\\') || !i)) + { + doub = 1; + ++i; + while (s[i] && !(s[i] == '"' && ((i && s[i - 1] != '\\') || !i))) + ++i; + if (s[i] == '"' && ((i && s[i - 1] != '\\') || !i)) + doub = 0; + } + if (s[i]) + ++i; + } + if (doub) + return (2); + else if (simp) + return (1); + return (0); +} + +void ft_check_quotes(char **s) +{ + int ret; + char *tmp; + char *tmp2; + int prompt_size_mem; + t_list_history *head; + + head = data_singleton()->line.list_beg; + if (!*s) + return ; + while ((ret = (ft_check_quotes_num(*s)))) + { + if (ret == 1) + ft_putstr("squote> "); + else if (ret == 2) + ft_putstr("dquote> "); + tmp = *s; + *s = ft_strjoin(*s, "\n"); + ft_strdel(&tmp); + tmp = *s; + prompt_size_mem = data_singleton()->line.prompt_size; + data_singleton()->line.input = NULL; + data_singleton()->line.pos = 0; + tmp2 = ft_read_stdin(); + data_singleton()->line.prompt_size = prompt_size_mem; + ft_putchar('\n'); + if (!tmp2) + continue ; + *s = ft_strjoin(tmp, tmp2); + data_singleton()->line.input = *s; + ft_strdel(&tmp); + ft_strdel(&tmp2); + } +} diff --git a/42sh/src/line-editing/reader.c b/42sh/src/line-editing/reader.c new file mode 100644 index 00000000..4a3e17d3 --- /dev/null +++ b/42sh/src/line-editing/reader.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* reader.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/19 16:52:57 by gwojda #+# #+# */ +/* Updated: 2017/01/25 19:07:39 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void ft_read_it_3(char **str, char t[5], size_t *pos, int *j) +{ + int i; + + i = 0; + while (i < 4 && t[i] == '\0') + ++i; + while (i < 4) + { + if (t[i] && ft_isprint(t[i])) + { + *str = ft_realloc_imput(*str, t[i], *pos); + ++(*pos); + ++(*j); + } + ++i; + } +} + +static void ft_read_it_2(int input, char t[5]) +{ + t[3] = (input / ft_pow(256, 3)) ? (input / ft_pow(256, 3)) : '\0'; + if (t[3]) + input = input % ft_pow(256, 3); + t[2] = (input / ft_pow(256, 2)) ? (input / ft_pow(256, 2)) : '\0'; + if (t[2]) + input = input % ft_pow(256, 2); + t[1] = (input / ft_pow(256, 1)) ? (input / ft_pow(256, 1)) : '\0'; + if (t[1]) + input = input % ft_pow(256, 1); + t[0] = (input / ft_pow(256, 0)) ? (input / ft_pow(256, 0)) : '\0'; + if (t[0]) + input = input % ft_pow(256, 0); + t[4] = '\0'; +} + +void ft_read_it(int input, size_t *pos, char **str) +{ + int j; + char t[5]; + + j = 0; + if (input == TOUCHE_DELETE || input < 0 || input == 892427035 || + input == 126 || input == 993090331 || input == 925981467 || + input == 21298 || input == 892427035 || input == 8270395 || + input == 942758683 || input == 993090331 || input == 18489 || input == 17977) + return ; + ft_read_it_2(input, t); + ft_read_it_3(str, t, pos, &j); + ft_putstr((*str) + (*pos) - j); + ft_putnc('\b', ft_strlen((*str)) - ((*pos))); +} diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c new file mode 100644 index 00000000..f48668b4 --- /dev/null +++ b/42sh/src/line-editing/readline.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* readline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ +/* Updated: 2017/02/02 18:03:23 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_init_line(void) +{ + data_singleton()->line.input = NULL; + data_singleton()->line.copy_tmp = NULL; + data_singleton()->line.pos = 0; + data_singleton()->line.prompt_size = 0; + data_singleton()->line.list_size = 0; + data_singleton()->line.list_end = NULL; + data_singleton()->line.list_beg = NULL; + data_singleton()->line.opt = 0; +} + +void ft_init_history(void) +{ + int fd; + char *str; + + fd = open(".42sh_history", O_RDONLY); + if (fd == -1) + return ; + while (get_next_line(fd, &str) > 0) + { + ft_push_back_history(&data_singleton()->line.list_beg, + ft_create_history_list(str)); + free(str); + } + free(str); + close(fd); +} + +struct termios *ft_save_stats_term(void) +{ + static struct termios *term_save = NULL; + + if (!term_save) + { + term_save = (struct termios *)malloc(sizeof(struct termios)); + tcgetattr(0, term_save); + } + return (term_save); +} + +struct termios *ft_stats_term_termcaps(void) +{ + static struct termios *term = NULL; + + if (!term) + { + ft_init_line(); + ft_init_history(); + term = (struct termios *)malloc(sizeof(struct termios)); + tcgetattr(0, term); + (*term).c_lflag &= ~(ECHO | ICANON | ISIG); + (*term).c_cc[VMIN] = 1; + (*term).c_cc[VTIME] = 0; + } + return (term); +} + +int ft_readline(void) +{ + ft_save_stats_term(); + if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1) + return (-1); + if (data_singleton()->line.input) + ft_strdel(&data_singleton()->line.input); + data_singleton()->line.list_cur = data_singleton()->line.list_beg; + data_singleton()->line.pos = 0; + ft_prompt(); + data_singleton()->line.input = ft_read_stdin(); + ft_putchar('\n'); + ft_check_quotes(&data_singleton()->line.input); + ft_check_heredoc(&data_singleton()->line.input); + ft_check_backslash(&data_singleton()->line.input); + ft_history_parsing(); + if (data_singleton()->line.input) + { + ft_push_back_history(&data_singleton()->line.list_beg, + ft_create_history_list(data_singleton()->line.input)); + ft_add_in_history_file(data_singleton()->line.input); + } + if (tcsetattr(0, TCSANOW, ft_save_stats_term()) == -1) + return (-1); + return (0); +} diff --git a/42sh/src/line-editing/rl_bitset.c b/42sh/src/line-editing/rl_bitset.c deleted file mode 100644 index 1c909e4a..00000000 --- a/42sh/src/line-editing/rl_bitset.c +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_bitset.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 10:31:34 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:35:46 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -static void rl_toogle_esc(t_line *line, int set) -{ - static int esc; - - if (!set) - esc = RL_IS(line->bitset, RL_ESC); - else if (set && esc) - { - RL_UNSET(line->bitset, RL_ESC); - esc = 0; - } -} - -static void rl_toogle_select(t_line *line, int set) -{ - static int select; - - if (!set) - select = RL_IS(line->bitset, RL_SELECT); - else if (set &&\ - ((select && RL_NOT(line->bitset, RL_SELECT)) ||\ - (!select && RL_IS(line->bitset, RL_SELECT)))) - rl_reset_display(line); -} - -void rl_toogle_bitset(t_line *line, int set) -{ - rl_toogle_esc(line, set); - rl_toogle_select(line, set); -} - -int rl_finish(t_line *line) -{ - int finish; - int stack; - - stack = RL_IS(line->bitset, RL_STACK); - finish = RL_IS(line->bitset, RL_FINISH); - if (!stack && !finish) - return (0); - rl_end_function(line, 0); - write(1, "\n", 1); - if (stack) - { - RL_UNSET(line->bitset, RL_STACK); - if (rl_stack_line(line) < 0) - return (-1); - } - if (finish) - return (1); - if (curs_coo_setup(&line->curs) < 0) - return (-1); - rl_set_prompt(line); - rl_put_prompt(line); - return (0); -} - diff --git a/42sh/src/line-editing/rl_clipboard_function.c b/42sh/src/line-editing/rl_clipboard_function.c deleted file mode 100644 index a5c93532..00000000 --- a/42sh/src/line-editing/rl_clipboard_function.c +++ /dev/null @@ -1,62 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_clipboard_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:32:11 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:36:10 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_copy_function(t_line *line, long int input) -{ - if (line->clipboard) - ft_memdel((void **)&line->clipboard); - if (rl_clipboard_new(line) < 0) - return (-1); - return (0); - (void)input; -} - -int rl_cut_function(t_line *line, long int input) -{ - if (line->clipboard) - ft_memdel((void **)&line->clipboard); - if (rl_clipboard_new_cut(line) < 0) - return (-1); - rl_reset_display_post(line); - return (0); - (void)input; -} - -int rl_paste_function(t_line *line, long int input) -{ - int len; - - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (!line->clipboard) - return (0); - len = ft_strlen(line->clipboard); - if (!len) - return (0); - if (RL_IS(line->bitset, RL_INSERT)) - { - if (input_move(line, line->clipboard, len) < 0) - return (-1); - } - else - { - if (input_maj(line, line->clipboard, len) < 0) - return (-1); - } - rl_reset_display_post(line); - curs_forward(&line->curs, len); - line->pos += len; - return (0); - (void)input; -} diff --git a/42sh/src/line-editing/rl_clipboard_utility.c b/42sh/src/line-editing/rl_clipboard_utility.c deleted file mode 100644 index 5a7a595c..00000000 --- a/42sh/src/line-editing/rl_clipboard_utility.c +++ /dev/null @@ -1,95 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_clipboard.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/09 14:04:33 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:36:27 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_clipboard_new_selected(t_line *line) -{ - int start; - int size; - - RL_UNSET(line->bitset, RL_SELECT); - start = (line->select > line->pos) ? line->pos : line->select; - size = (line->select > line->pos ? line->select - line->pos : line->pos - line->select); - if ((line->clipboard = ft_strsub(line->input, start, (size_t)size)) == NULL) - return (-1); - return (0); -} - -int rl_clipboard_new_selected_cut(t_line *line) -{ - int start; - int size; - - RL_UNSET(line->bitset, RL_SELECT); - start = (line->select > line->pos) ? line->pos : line->select; - size = (line->select > line->pos ? line->select - line->pos : line->pos - line->select); - if ((line->clipboard = ft_strsub(line->input, start, (size_t)size)) == NULL) - return (-1); - if (line->pos != start) - { - curs_backward(&line->curs, line->pos - start); - line->pos = line->pos - (line->pos - start); - } - input_remove(line, -size); - return (0); -} - -int rl_clipboard_new(t_line *line) -{ - int start; - int real_pos; - int size; - - if (RL_IS(line->bitset, RL_SELECT)) - return (rl_clipboard_new_selected(line)); - size = line->curs.win_co; - RL_UNSET(line->bitset, RL_SELECT); - real_pos = line->pos + ft_strlen(line->prompt); - start = (real_pos / line->curs.win_co) * line->curs.win_co; - if (start) - start -= ft_strlen(line->prompt); - else - size -= ft_strlen(line->prompt); - if ((line->clipboard = ft_strsub(line->input, start, size)) == NULL) - return (-1); - return (0); -} - -int rl_clipboard_new_cut(t_line *line) -{ - int start; - int real_pos; - int size; - - if (RL_IS(line->bitset, RL_SELECT)) - return (rl_clipboard_new_selected_cut(line)); - size = line->curs.win_co; - RL_UNSET(line->bitset, RL_SELECT); - real_pos = line->pos + ft_strlen(line->prompt); - start = (real_pos / line->curs.win_co) * line->curs.win_co; - if (start) - start -= ft_strlen(line->prompt); - else - size -= ft_strlen(line->prompt); - if ((line->clipboard = ft_strsub(line->input, start, size)) == NULL) - return (-1); - if (line->pos != start) - { - curs_backward(&line->curs, line->pos - start); - line->pos = line->pos - (line->pos - start); - } - if (size > line->used - start) - size = line->used - start; - input_remove(line, -size); - return (0); -} diff --git a/42sh/src/line-editing/rl_comp_function.c b/42sh/src/line-editing/rl_comp_function.c deleted file mode 100644 index b4d0927a..00000000 --- a/42sh/src/line-editing/rl_comp_function.c +++ /dev/null @@ -1,41 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_comp_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 10:49:03 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:36:49 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -static int ft_autocomp(t_data *data) -{ - return (0); - int li; - int co; - - li = data->line.curs.li; - co = data->line.curs.co; - ft_putstr("\nTeeeeeeest"); - data->line.input = ft_strjoin(data->line.input, " JOIN "); - curs_goto(&data->line.curs, li, co + 6); - return (0); -} - -int rl_comp_function(t_line *line, long int input) -{ - t_data *data; - - data = data_singleton(); - if (ft_autocomp(data) < 0) - return (-1); - line->size = ft_strlen(line->input); - line->used = line->size; - curs_reset(0); - return (0); - (void)input; -} diff --git a/42sh/src/line-editing/rl_default_function.c b/42sh/src/line-editing/rl_default_function.c deleted file mode 100644 index bb1bd721..00000000 --- a/42sh/src/line-editing/rl_default_function.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_default_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 18:30:38 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:37:02 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_default_function(t_line *line, long int input) -{ - if (!ft_isprint(input)) - return (0); - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (RL_IS(line->bitset, RL_INSERT)) - { - if (input_move(line, (char *)&input, sizeof(char)) < 0) - return (-1); - } - else - { - if (input_maj(line, (char *)&input, sizeof(char)) < 0) - return (-1); - } - rl_reset_display_post(line); - curs_forward(&line->curs, sizeof(char)); - line->pos += 1; -/* curs_write(&line->curs, (char *)&input, sizeof(char)); - line->pos += 1; - rl_reset_display_post(line); -*/ return (0); -} diff --git a/42sh/src/line-editing/rl_display.c b/42sh/src/line-editing/rl_display.c deleted file mode 100644 index 3524d155..00000000 --- a/42sh/src/line-editing/rl_display.c +++ /dev/null @@ -1,61 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_display.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 18:38:39 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:37:29 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -void rl_reset_display_post(t_line *line) -{ - size_t len; - - len = ft_strlen(line->input + line->pos); - curs_clear_post(&line->curs); - if (len == 0) - return ; - if (RL_NOT(line->bitset, RL_SELECT) || (RL_IS(line->bitset, RL_SELECT) && line->select < line->pos)) - { - curs_write_static(&line->curs, line->input + line->pos, len); - return; - } - if (line->select == line->used) - { - curs_video_write_static(&line->curs, line->input + line->pos, len); - return ; - } - curs_video_write(&line->curs, line->input + line->pos, line->select - line->pos); - curs_write(&line->curs, line->input + line->select, line->used - line->select); - curs_backward(&line->curs, line->used - line->pos); -} - -void rl_reset_display_ante(t_line *line) -{ - if (!line->pos) - return ; - curs_backward(&line->curs, line->pos); - if (RL_NOT(line->bitset, RL_SELECT) || (RL_IS(line->bitset, RL_SELECT) && line->select > line->pos)) - { - curs_write(&line->curs, line->input, line->pos); - return ; - } - if (line->select == 0) - { - curs_video_write(&line->curs, line->input, line->pos); - return ; - } - curs_write(&line->curs, line->input, line->select); - curs_video_write(&line->curs, line->input + line->select, line->pos - line->select); -} - -void rl_reset_display(t_line *line) -{ - rl_reset_display_ante(line); - rl_reset_display_post(line); -} diff --git a/42sh/src/line-editing/rl_dynamic_buffer.c b/42sh/src/line-editing/rl_dynamic_buffer.c deleted file mode 100644 index c52ea08c..00000000 --- a/42sh/src/line-editing/rl_dynamic_buffer.c +++ /dev/null @@ -1,99 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* input_buffer.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 18:42:31 by sbenning #+# #+# */ -/* Updated: 2017/01/22 21:24:09 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - - -int input_extend(t_line *line, int size) -{ - char *tmp; - - size = (RL_OFFSET > size ? RL_OFFSET : size) + line->size; - if ((tmp = ft_strnew(size)) == NULL) - return (-1); - if (line->input) - { - ft_memcpy(tmp, line->input, line->size); - free(line->input); - } - line->input = tmp; - line->size = size; - return (0); -} - -int rl_dynamic_buffer_extend(t_line *line, int size) -{ - return (input_extend(line, size)); -} - -int input_maj(t_line *line, char *str, int size) -{ - if (line->used + size >= line->size) - { - if (rl_dynamic_buffer_extend(line, size) < 0) - return (-1); - } - ft_memcpy(line->input + line->pos, str, size); - if (line->pos + size > line->used) - line->used = line->pos + size; - return (0); -} - -int input_move(t_line *line, char *str, int size) -{ - char *dup; - - if (line->used + size >= line->size) - { - if (rl_dynamic_buffer_extend(line, size) < 0) - return (-1); - } - if ((dup = ft_strdup(line->input + line->pos)) == NULL) - return (-1); - ft_memcpy(line->input + line->pos, str, size); - ft_memcpy(line->input + line->pos + size, dup, ft_strlen(dup)); - free(dup); - line->used += size; - return (0); -} - -void input_remove(t_line *line, int size) -{ - char *start; - - if (size < 0) - { - start = line->input + line->pos; - size *= -1; - } - else - start = (line->input + line->pos) - size; - ft_strcpy(start, start + size); - line->used -= size; - ft_bzero(line->input + line->used, line->size - line->used); -} - - -int rl_dynamic_buffer_replace(t_line *line, char *str, int size) -{ - return (input_maj(line, str, size)); -} - -int rl_dynamic_buffer_insert(t_line *line, char *str, int size) -{ - return (input_move(line, str, size)); -} - -void rl_dynamic_buffer_remove(t_line *line, int size) -{ - input_remove(line, size); -} diff --git a/42sh/src/line-editing/rl_long_move_function.c b/42sh/src/line-editing/rl_long_move_function.c deleted file mode 100644 index a54f6859..00000000 --- a/42sh/src/line-editing/rl_long_move_function.c +++ /dev/null @@ -1,65 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_long_move_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:55:48 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:34:02 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_home_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (!line->pos) - return (0); - curs_backward(&line->curs, line->pos); - line->pos = 0; - return (0); - (void)input; -} - -int rl_end_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (line->pos == line->used) - return (0); - curs_forward(&line->curs, line->used - line->pos); - line->pos = line->used; - return (0); - (void)input; -} - -int rl_select_home_function(t_line *line, long int input) -{ - int ret; - - if (!line->pos) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_home_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_post(line); - return (ret); -} - -int rl_select_end_function(t_line *line, long int input) -{ - int ret; - - if (line->pos == line->used) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_end_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_ante(line); - return (ret); -} diff --git a/42sh/src/line-editing/rl_merge_line.c b/42sh/src/line-editing/rl_merge_line.c deleted file mode 100644 index 91a3f8ff..00000000 --- a/42sh/src/line-editing/rl_merge_line.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_merge_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/09 01:25:03 by sbenning #+# #+# */ -/* Updated: 2017/01/22 21:56:30 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_merge_line(t_line *line) -{ - t_list *next; - char *chunk; - size_t size; - - while (line->stack) - { - next = line->stack->next; - chunk = (char *)line->stack->content; - size = line->stack->content_size - 1; - if (input_maj(line, chunk, size) < 0) - return (-1); - line->pos += size; - /* if (next) - { - if (input_maj(line, "\n", 1) < 0) - return (-1); - line->pos += 1; - } - */ free(chunk); - free(line->stack); - line->stack = next; - } - return (0); -} diff --git a/42sh/src/line-editing/rl_page_move_function.c b/42sh/src/line-editing/rl_page_move_function.c deleted file mode 100644 index 1995f6ca..00000000 --- a/42sh/src/line-editing/rl_page_move_function.c +++ /dev/null @@ -1,72 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_page_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:39:57 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:37:41 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_pagedown_function(t_line *line, long int input) -{ - int len; - - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (line->pos == line->used) - return (0); - len = line->used - line->pos; - len = (line->curs.win_co > len ? len : line->curs.win_co); - curs_forward(&line->curs, len); - line->pos += len; - return (0); - (void)input; -} - -int rl_pageup_function(t_line *line, long int input) -{ - int len; - - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (!line->pos) - return (0); - len = (line->curs.win_co > line->pos ? line->pos : line->curs.win_co); - curs_backward(&line->curs, len); - line->pos -= len; - return (0); - (void)input; -} - -int rl_select_pagedown_function(t_line *line, long int input) -{ - int ret; - - if (line->pos == line->used) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_pagedown_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_ante(line); - return (ret); -} - -int rl_select_pageup_function(t_line *line, long int input) -{ - int ret; - - if (!line->pos) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_pageup_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_post(line); - return (ret); -} diff --git a/42sh/src/line-editing/rl_prompt.c b/42sh/src/line-editing/rl_prompt.c deleted file mode 100644 index 30fb6240..00000000 --- a/42sh/src/line-editing/rl_prompt.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_prompt.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/09 01:45:09 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:51:50 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" -#include "job_control.h" - -void rl_set_prompt(t_line *line) -{ - if (RL_IS(line->bitset, RL_ESC)) - line->prompt = RL_PROMPT_ESC; - else if (RL_IS(line->bitset, RL_QUOTE)) - line->prompt = RL_PROMPT_QUOTE; - else if (RL_IS(line->bitset, RL_DQUOTE)) - line->prompt = RL_PROMPT_DQUOTE; - else - line->prompt = RL_PROMPT_DEFAULT; -} - -void rl_put_prompt(t_line *line) -{ - curs_write(&line->curs, line->prompt, ft_strlen(line->prompt)); -} diff --git a/42sh/src/line-editing/rl_quoting_function.c b/42sh/src/line-editing/rl_quoting_function.c deleted file mode 100644 index 06384194..00000000 --- a/42sh/src/line-editing/rl_quoting_function.c +++ /dev/null @@ -1,44 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_quoting_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:35:25 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:38:02 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_esc_function(t_line *line, long int input) -{ - if (RL_NOT(line->bitset, RL_ESC)) - RL_SET(line->bitset, RL_ESC); - return (rl_default_function(line, input)); -} - -int rl_quote_function(t_line *line, long int input) -{ - if (RL_NOT(line->bitset, RL_ESC)) - { - if (RL_IS(line->bitset, RL_QUOTE)) - RL_UNSET(line->bitset, RL_QUOTE); - else - RL_SET(line->bitset, RL_QUOTE); - } - return (rl_default_function(line, input)); -} - -int rl_dquote_function(t_line *line, long int input) -{ - if (RL_NOT(line->bitset, RL_ESC)) - { - if (RL_IS(line->bitset, RL_DQUOTE)) - RL_UNSET(line->bitset, RL_DQUOTE); - else - RL_SET(line->bitset, RL_DQUOTE); - } - return (rl_default_function(line, input)); -} diff --git a/42sh/src/line-editing/rl_remove_function.c b/42sh/src/line-editing/rl_remove_function.c deleted file mode 100644 index a4bdf334..00000000 --- a/42sh/src/line-editing/rl_remove_function.c +++ /dev/null @@ -1,39 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_remove_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:45:08 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:38:14 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_retarr_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (!line->pos) - return (0); - input_remove(line, 1); - curs_backward(&line->curs, 1); - line->pos -= 1; - rl_reset_display_post(line); - return (0); - (void)input; -} - -int rl_suppr_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (line->pos == line->used) - return (0); - input_remove(line, -1); - rl_reset_display_post(line); - return (0); - (void)input; -} diff --git a/42sh/src/line-editing/rl_setup.c b/42sh/src/line-editing/rl_setup.c deleted file mode 100644 index 4870c0a7..00000000 --- a/42sh/src/line-editing/rl_setup.c +++ /dev/null @@ -1,46 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_setup.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/08 18:07:47 by sbenning #+# #+# */ -/* Updated: 2017/01/22 22:46:09 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_setup(t_line *line) -{ - if (SHELL_HAS_JOBC(data_singleton()->opts)) - do_job_notification(); - data_singleton()->opts &= ~SHELL_MODE_MASK; - data_singleton()->opts |= SHELL_MODE_INPUT; - ft_bzero(line, sizeof(t_line)); - if (curs_setup(&line->curs) < 0) - return (-1); - RL_SET(line->bitset, RL_INSERT); - rl_set_prompt(line); - return (0); -} - -int rl_cleanup(t_line *line) -{ - data_singleton()->opts &= ~SHELL_MODE_MASK; - data_singleton()->opts |= SHELL_MODE_EXEC; - if (curs_cleanup(&line->curs) < 0) - return (-1); - if (rl_merge_line(line) < 0) - return (-1); - return (0); -} - -void rl_teardown(t_line *line) -{ - rl_cleanup(line); - if (line->input) - ft_memdel((void **)&line->input); - line->pos = 0; -} diff --git a/42sh/src/line-editing/rl_short_move_function.c b/42sh/src/line-editing/rl_short_move_function.c deleted file mode 100644 index 7dc5196b..00000000 --- a/42sh/src/line-editing/rl_short_move_function.c +++ /dev/null @@ -1,65 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_move_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:53:18 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:38:52 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_left_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (!line->pos) - return (0); - curs_backward(&line->curs, 1); - line->pos -= 1; - return (0); - (void)input; -} - -int rl_right_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - if (line->pos == line->used) - return (0); - curs_forward(&line->curs, 1); - line->pos += 1; - return (0); - (void)input; -} - -int rl_select_left_function(t_line *line, long int input) -{ - int ret; - - if (!line->pos) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_left_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_post(line); - return (ret); -} - -int rl_select_right_function(t_line *line, long int input) -{ - int ret; - - if (line->pos == line->used) - return (0); - if (!RL_IS(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_right_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_ante(line); - return (ret); -} diff --git a/42sh/src/line-editing/rl_stack_line.c b/42sh/src/line-editing/rl_stack_line.c deleted file mode 100644 index 0824fb11..00000000 --- a/42sh/src/line-editing/rl_stack_line.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_stack_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/09 01:06:54 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:39:22 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_stack_line(t_line *line) -{ - t_list *elem; - - elem = ft_lstnew(line->input, line->used + 1); - if (elem == NULL) - return (-1); - ft_lsteadd(&line->stack, elem); - ft_memdel((void **)&line->input); - line->pos = 0; - line->size = 0; - line->used = 0; - if (RL_NOT(line->bitset, RL_ESC)) - return (0); - elem = ft_lstnew("\n", 2); - if (elem == NULL) - return (-1); - ft_lsteadd(&line->stack, elem); - return (0); -} diff --git a/42sh/src/line-editing/rl_state_function.c b/42sh/src/line-editing/rl_state_function.c deleted file mode 100644 index c7deca62..00000000 --- a/42sh/src/line-editing/rl_state_function.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_state_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 13:00:59 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:39:31 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_insert_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_INSERT)) - RL_UNSET(line->bitset, RL_INSERT); - else - RL_SET(line->bitset, RL_INSERT); - return (0); - (void)input; -} - -int rl_nl_function(t_line *line, long int input) -{ - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - RL_SET(line->bitset, RL_STACK); - if (RL_NOT(line->bitset, RL_QUOTING)) - RL_SET(line->bitset, RL_FINISH); - return (0); - (void)input; -} diff --git a/42sh/src/line-editing/rl_word_move_function.c b/42sh/src/line-editing/rl_word_move_function.c deleted file mode 100644 index de976fa4..00000000 --- a/42sh/src/line-editing/rl_word_move_function.c +++ /dev/null @@ -1,67 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_word_function.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:47:31 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:39:44 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_wleft_function(t_line *line, long int input) -{ - size_t len; - - if (!line->pos) - return (0); - len = rl_previous_word(line); - curs_backward(&line->curs, len); - line->pos -= len; - return (0); - (void)input; -} - -int rl_wright_function(t_line *line, long int input) -{ - size_t len; - - if (line->pos == line->used) - return (0); - len = rl_next_word(line); - curs_forward(&line->curs, len); - line->pos += len; - return (0); - (void)input; -} - -int rl_select_wleft_function(t_line *line, long int input) -{ - int ret; - - if (!line->pos) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_wleft_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_post(line); - return (ret); -} - -int rl_select_wright_function(t_line *line, long int input) -{ - int ret; - - if (line->pos == line->used) - return (0); - if (RL_NOT(line->bitset, RL_SELECT)) - line->select = line->pos; - ret = rl_wright_function(line, input); - RL_SET(line->bitset, RL_SELECT); - rl_reset_display_ante(line); - return (ret); -} diff --git a/42sh/src/line-editing/rl_word_utility.c b/42sh/src/line-editing/rl_word_utility.c deleted file mode 100644 index 343a6ecf..00000000 --- a/42sh/src/line-editing/rl_word_utility.c +++ /dev/null @@ -1,51 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rl_word_utility.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sbenning +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/12 12:48:07 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:39:55 by ariard ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_readline.h" - -int rl_previous_word(t_line *line) -{ - int i; - int len; - - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - i = 0; - while (line->input[i] && i < line->pos) - { - if (!i && line->input[i] != ' ') - len = i; - if (i && line->input[i - 1] == ' ' && line->input[i] != ' ') - len = i; - i++; - } - return (line->pos - len); -} - -int rl_next_word(t_line *line) -{ - int i; - int flag; - - if (RL_IS(line->bitset, RL_SELECT)) - RL_UNSET(line->bitset, RL_SELECT); - i = line->pos; - flag = 0; - while (line->input[i] && (line->input[i] == ' ' || !flag)) - { - if (line->input[i] == ' ') - flag = 1; - i++; - } - return (i - line->pos); -} - diff --git a/42sh/src/line-editing/surch_in_history.c b/42sh/src/line-editing/surch_in_history.c new file mode 100644 index 00000000..424dc5d2 --- /dev/null +++ b/42sh/src/line-editing/surch_in_history.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* surch_in_history.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */ +/* Updated: 2017/02/02 15:16:42 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_clear_prompt(char *str, size_t *pos, size_t srch_pos) +{ + if (str) + ft_get_beggin_with_curs(str, pos); + else if (*pos) + { + ft_putnc('\b', *pos); + (*pos) = 0; + } + ft_putnc('\b', data_singleton()->line.prompt_size + srch_pos + 1); + ft_puttermcaps("cd"); +} + +void ft_surch_and_realloc(char **str, char **str_srch, int ret, size_t *srch_pos) +{ + *str_srch = ft_realloc_imput(*str_srch, ret, *srch_pos); + ++(*srch_pos); + *str = ft_strget_history(*str_srch); +} + +void ft_surch_in_history(void) +{ + char *str_srch; + int ret; + size_t srch_pos; + char **str; + size_t *pos; + + str = &data_singleton()->line.input; + pos = &data_singleton()->line.pos; + srch_pos = 0; + str_srch = NULL; + if (*str) + ft_strdel(str); + while (42) + { + ft_clear_prompt(*str, pos, srch_pos); + data_singleton()->line.prompt_size = 21; + if (str_srch) + ft_printf("\033[35m(reverse-i-search)`\033[32m%s\033[35m': \033[37m", str_srch); + else + ft_putstr("\033[35m(reverse-i-search)`': \033[37m"); + if (*str) + { + ft_current_str(*str, *pos); + ft_get_next_str(*str, pos); + } + ret = 0; + read(0, &ret, sizeof(int)); + if (ft_isprint(ret)) + ft_surch_and_realloc(str, &str_srch, ret, &srch_pos); + else if (ret == 127) + { + if (srch_pos) + { + --srch_pos; + str_srch = ft_remove_imput(str_srch, srch_pos); + ft_puttermcaps("le"); + } + } + else + break ; + } + ft_clear_prompt(*str, pos, srch_pos); + ft_prompt(); + if (*str) + { + *str = ft_strdup(*str); + ft_current_str(*str, *pos); + ft_get_next_str(*str, pos); + } + free(str_srch); +} diff --git a/42sh/src/line-editing/tool_line.c b/42sh/src/line-editing/tool_line.c new file mode 100644 index 00000000..0745579b --- /dev/null +++ b/42sh/src/line-editing/tool_line.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tool_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 18:20:23 by gwojda #+# #+# */ +/* Updated: 2017/02/01 15:27:31 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_get_beggin(char *str, size_t *pos) +{ + while ((*pos) && str[(*pos)] != '\n') + --(*pos); + if (*pos && str[(*pos)] == '\n') + ++(*pos); +} + +void ft_get_next_str(char *str, size_t *pos) +{ + while (str[(*pos)] && str[(*pos)] != '\n') + ++(*pos); + if (*pos && str[(*pos)] == '\n') + --(*pos); +} + +void ft_putall_current_str(char *str, size_t *pos) +{ + int len; + + len = 0; + ft_get_beggin(str, pos); + while (str[(*pos) + len] && str[(*pos) + len] != '\n') + ++len; + write(1, str + *pos, len); +} + +void ft_current_str(char *str, size_t pos) +{ + int len; + + len = 0; + while (str[pos + len] && str[pos + len] != '\n') + ++len; + write(1, str + pos, len); + ft_check_end_of_line(str, pos + len); +} + +int ft_strlen_next(char *str, size_t pos) +{ + int len; + + len = 0; + while (str[pos + len] && str[pos + len] != '\n') + ++len; + return (len); +} diff --git a/42sh/src/line-editing/tool_line_2.c b/42sh/src/line-editing/tool_line_2.c new file mode 100644 index 00000000..c5c9dd8e --- /dev/null +++ b/42sh/src/line-editing/tool_line_2.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tool_line_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 19:07:52 by gwojda #+# #+# */ +/* Updated: 2017/02/02 14:16:24 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void ft_clear_window() +{ + pid_t soon_pid; + char *tab_str[2] = {(char *){"/usr/bin/clear"}, NULL}; + + soon_pid = fork(); + if (soon_pid != 0) + wait(NULL); + else + execve("/usr/bin/clear", tab_str, data_singleton()->env); +} + +void ft_printall(void) +{ + size_t pos_tmp; + int ret; + char *str; + size_t *pos; + + str = data_singleton()->line.input; + pos = &data_singleton()->line.pos; + ret = 0; + if (read(0, &ret, sizeof(int)) == -1 || ret != 126 || !str) + return ; + ft_clear_window(); + ft_prompt(); + pos_tmp = *pos; + ft_putstr("\033[31m"); + ft_puttermcaps("cd"); + ft_puttermcaps("sc"); + ft_putstr(str); + while (42) + { + ret = 0; + read(0, &ret, sizeof(int)); + if (ret == TOUCHE_F6) + { + ret = 0; + if (read(0, &ret, sizeof(int)) && ret == 126) + break ; + } + } + ft_puttermcaps("rc"); + ft_puttermcaps("cd"); + ft_putstr("\033[37m"); + if (*pos) + --(*pos); + ft_get_beggin(str, pos); + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + if (*pos && !str[*pos]) + --(*pos); + if (*pos) + ft_putnc('\b', *pos - pos_tmp + 1); + *pos = pos_tmp; +} + +void ft_check_end_of_line(char *str, size_t pos) +{ + if (!str) + return ; + if (ft_nb_last_line(str, pos) == 0) + { + ft_putchar(' '); + ft_puttermcaps("le"); + } +} + +void ft_get_beggin_with_curs(char *str, size_t *pos) +{ + while ((*pos) && str[(*pos)] != '\n') + { + --(*pos); + ft_puttermcaps("le"); + } + if (!*pos && str[(*pos)] != '\n') + ft_puttermcaps("le"); + if (str[(*pos)] == '\n') + ++(*pos); +} diff --git a/42sh/src/line-editing/toolz.c b/42sh/src/line-editing/toolz.c new file mode 100644 index 00000000..1051442e --- /dev/null +++ b/42sh/src/line-editing/toolz.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toolz.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/16 17:06:30 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:22:47 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_putnc(char c, int n) +{ + int i; + + i = 0; + while (i < n) + { + write(1, &c, 1); + ++i; + } +} + +char *ft_strndup(char const *s, int n) +{ + int i; + char *str; + + i = 0; + str = (char *)malloc(sizeof(char) * (n + 1)); + if (str) + { + while (i < n) + { + str[i] = s[i]; + i++; + } + str[i] = '\0'; + } + return (str); +} + +char *ft_strdupi(char const *s) +{ + int i; + char *str; + + i = 0; + while (s[i] && s[i] != ';' && s[i] != '>' && s[i] != '<' + && ft_strncmp(s + i, "||", 2)) + i++; + str = (char *)malloc(sizeof(char) * (i + 1)); + if (str) + { + str[i--] = '\0'; + while (i >= 0) + { + str[i] = s[i]; + i--; + } + } + return (str); +} diff --git a/42sh/src/line-editing/toolz2.c b/42sh/src/line-editing/toolz2.c new file mode 100644 index 00000000..ce1c62f6 --- /dev/null +++ b/42sh/src/line-editing/toolz2.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toolz2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/07 11:00:28 by gwojda #+# #+# */ +/* Updated: 2017/02/02 16:04:55 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_add_in_history_file(char *str) +{ + int fd; + + fd = open(".42sh_history", O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); + if (fd == -1) + return ; + write(fd, str, ft_strlen(str)); + write(fd, "\n", 1); + close(fd); +} + +int ft_nbr_len(int nbr) +{ + if (nbr % 10 != nbr) + return (ft_nbr_len(nbr / 10) + 1); + else + return (1); +} + +void ft_puttermcaps(char *str) +{ + char *res; + char *env; + + env = getenv("TERM"); + if ((res = tgetstr(str, &env)) == NULL) + return ; + tputs(res, 0, ft_put); +} + +int ft_size_term(void) +{ + struct winsize w; + + ioctl(0, TIOCGWINSZ, &w); + return (w.ws_col); +} + +long long ft_pow(int nbr, int power) +{ + int i; + long long ret; + + i = 0; + ret = 1; + while (i < power) + { + ret *= nbr; + i++; + } + return (ret); +} diff --git a/42sh/src/line-editing/toolz_parseur.c b/42sh/src/line-editing/toolz_parseur.c new file mode 100644 index 00000000..a0781e12 --- /dev/null +++ b/42sh/src/line-editing/toolz_parseur.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toolz_parseur.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/16 16:14:46 by gwojda #+# #+# */ +/* Updated: 2017/02/02 14:29:34 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_realloc_imput(char *str, int a, size_t pos) +{ + int i; + char tmp[2]; + char *str_tmp; + char *new_str; + + i = 0; + tmp[0] = (char)a; + tmp[1] = '\0'; + if (!str) + { + new_str = ft_strdup(tmp); + return (new_str); + } + str_tmp = ft_strndup(str, pos); + new_str = ft_strjoin(str_tmp, tmp); + free(str_tmp); + str_tmp = ft_strjoin(new_str, str + pos); + free(new_str); + free(str); + return (str_tmp); +} + +char *ft_remove_imput(char *str, size_t pos) +{ + char *new_str; + char *new_str2; + + if (!str) + return (str); + new_str = ft_strndup(str, pos); + new_str2 = ft_strjoin(new_str, &str[pos + 1]); + free(str); + free(new_str); + return (new_str2); +} diff --git a/42sh/src/line-editing/toolz_termcaps.c b/42sh/src/line-editing/toolz_termcaps.c new file mode 100644 index 00000000..901b026e --- /dev/null +++ b/42sh/src/line-editing/toolz_termcaps.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toolz_termcaps.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/08 12:35:11 by gwojda #+# #+# */ +/* Updated: 2017/01/24 11:19:34 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_get_ind_prev(char *str, size_t pos) +{ + while (pos && str[pos] != '\n') + --pos; + return (pos); +} + +int ft_get_size_prev(char *str, size_t pos) +{ + size_t len; + + len = 0; + while (pos && str[pos] != '\n') + { + --pos; + ++len; + } + return (len); +} + +int ft_nb_last_line(char *str, size_t pos) +{ + int len; + + len = 1; + if (pos && str[pos] == '\n') + --pos; + len += data_singleton()->line.prompt_size; + len += ft_get_size_prev(str, pos); + return ((len) ? len % ft_size_term() : -1); +} diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index c69faea1..a865b92c 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* data_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/01/31 21:50:31 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:34:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,6 @@ int data_init(void) t_data *data; data = data_singleton(); - data->line.input = NULL; data->env = ft_sstrdup(environ); data->opts = SHELL_OPTS_JOBC; diff --git a/42sh/src/main/ft_cleanup.c b/42sh/src/main/ft_cleanup.c new file mode 100644 index 00000000..7cf6aa3b --- /dev/null +++ b/42sh/src/main/ft_cleanup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cleanup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 14:42:42 by jhalford #+# #+# */ +/* Updated: 2016/12/09 21:50:38 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +extern char **environ; +extern char PC; +extern char *UP; +extern char *BC; + +void ft_cleanup(void) +{ + struct termios term; + + DG("cleanup. char * UP at %p", UP); + DG("cleanup. char * BC at %p", BC); + if (tcgetattr(0, &term) == -1) + return ; + term.c_lflag |= ICANON | ISIG | ECHO; + if (tcsetattr(0, TCSANOW, &term) == -1) + return ; +} diff --git a/42sh/src/main/lib_expansion.c b/42sh/src/main/lib_expansion.c new file mode 100644 index 00000000..8905c284 --- /dev/null +++ b/42sh/src/main/lib_expansion.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_expansion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/03 13:37:41 by jhalford #+# #+# */ +/* Updated: 2016/12/03 13:37:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_expand_dollar(char **av, char **env) +{ + char *dollar; + + while (*av) + { + if ((dollar = ft_strchr(*av, '$'))) + { + *dollar = '\0'; + *av = ft_strjoin(*av, ft_getenv(env, dollar + 1)); + } + av++; + } +} diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 5a7ecd69..ed06bf53 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/01/31 21:25:39 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:31:46 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,23 +19,14 @@ int shell_single_command(char *command) token = NULL; ast = NULL; - if (!command) - return (0); + DG("{inv}{mag}got command '%s'", command); - if (ft_tokenize(&token, command, DEFAULT)) + if (ft_lexer(&token, &command) || !token) return (1); - if (!token) - return (0); - if (ft_post_tokenize(&token, command)) - return (1); - DG("after post_tokenize"); token_print(token); - return (0); -// if (ft_parse(&ast, &token)) -// return (1); + if (ft_parse(&ast, &token)) + return (1); btree_print(STDBUG, ast, &ft_putast); - /* /1* ft_dprintf(STDBUG, "\n--- INFIX BREAKDOWN ---\n"); *1/ */ - /* /1* btree_apply_infix(ast, &ft_putast2); *1/ */ if (ft_exec(&ast)) return (1); return (0); @@ -43,11 +34,12 @@ int shell_single_command(char *command) int main(int ac, char **av) { + setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF"); shell_init(ac, av); if (data_singleton()->opts & SHELL_OPTS_LC) { - shell_single_command(shell_get_avdata()); + shell_single_command(ft_strdup(shell_get_avdata())); return (0); } if (data_singleton()->opts & SHELL_MODE_SCRIPT) @@ -59,7 +51,7 @@ int main(int ac, char **av) { if (ft_readline()) return (1); - if (shell_single_command(data_singleton()->line.input)) + if (shell_single_command(ft_strdup(data_singleton()->line.input)) < 0) return (1); } return (0); diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index d2051cbb..1fecc226 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -28,9 +28,8 @@ void shell_init(int ac, char **av) signal(SIGINT, sigint_handler); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, sigtstp_handler); - /* signal(SIGTSTP, SIG_IGN); */ - signal(SIGTTIN, sigttin_handler); - signal(SIGTTOU, sigttou_handler); + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); signal(SIGCHLD, sigchld_handler); *shell_pgid = getpid(); if (setpgid(*shell_pgid, *shell_pgid)) diff --git a/42sh/src/main/sig_handler.c b/42sh/src/main/sig_handler.c new file mode 100644 index 00000000..d56b03ee --- /dev/null +++ b/42sh/src/main/sig_handler.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sig_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/01 12:43:22 by jhalford #+# #+# */ +/* Updated: 2016/12/03 13:31:33 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +pid_t g_pid; + +void sig_handler(int signo) +{ + (void)signo; + if (signo == SIGINT) + { + if (g_pid) + kill(g_pid, SIGINT); + if (kill(g_pid, 0) == 0) + ft_putendl(""); + } +} diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 58b124a4..e1d4ed8b 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/01/31 23:20:37 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:31:06 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ int ft_parse(t_list **list_ast, t_btree **ast, t_list **start) if (!*ast) { *ast = btree_create_node(&item, sizeof(item)); - ((t_astnode *)(*ast)->item)->data.sstr = NULL; + ((t_astnode *)(*ast)->item)->data.token = NULL; } if (!*list_ast) *list_ast = ft_lstnew(ast, sizeof(t_btree *)); diff --git a/42sh/src/parser/parse_subshell.c b/42sh/src/parser/parse_subshell.c index ee2a5027..8d19414e 100644 --- a/42sh/src/parser/parse_subshell.c +++ b/42sh/src/parser/parse_subshell.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 16:52:44 by jhalford #+# #+# */ -/* Updated: 2017/01/26 19:59:58 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:30:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_word.c b/42sh/src/parser/parse_word.c index f8f537d7..6f7126b3 100644 --- a/42sh/src/parser/parse_word.c +++ b/42sh/src/parser/parse_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/01/30 18:37:21 by ariard ### ########.fr */ +/* Updated: 2017/02/03 14:30:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,17 @@ int parse_word(t_list **list_ast, t_btree **ast, { t_astnode *node; t_token *token; + char **my_tab; + (void)start; + my_tab = NULL; token = (*lst)->content; node = (*ast)->item; node->type = TK_COMMAND; - node->data.sstr = ft_sstradd(node->data.sstr, token->data); - ft_parse(list_ast, ast, &(*lst)->next); + my_tab = ft_sstradd(my_tab, token->data); + my_tab = ft_sstradd(my_tab, (char *)token->esc); + ft_ld_pushback(&node->data.token, my_tab); + ft_parse(ast, &(*lst)->next); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); return (0); } diff --git a/42sh/update_makefile.sh b/42sh/update_makefile.sh new file mode 100755 index 00000000..a1b3228d --- /dev/null +++ b/42sh/update_makefile.sh @@ -0,0 +1,17 @@ +MYPATH=$(pwd) +CUR_MAKEFILE=$MYPATH/Makefile +if [ -e $CUR_MAKEFILE ] +then + echo "regenerate Makefile" + sed "`grep -n 'SRC_BASE =' $CUR_MAKEFILE | sed 's/:.*//'`, \$d" $CUR_MAKEFILE > NEWMAKEFILE + grep 'SRC_BASE =' $CUR_MAKEFILE >> NEWMAKEFILE + expr "$(find ./src | grep "\.c" | sed -e 's/src\///' -e 's/\.\///' -e 's/$/\\/')" : "\(.*\).$" >> NEWMAKEFILE + echo "" >> NEWMAKEFILE + grep 'SRCS =' $CUR_MAKEFILE >> NEWMAKEFILE + sed "1, `grep -n 'SRCS =' $CUR_MAKEFILE | sed 's/:.*//'`d" $CUR_MAKEFILE >> NEWMAKEFILE + mv $CUR_MAKEFILE ~/Documents/.OLDMakefile + mv NEWMAKEFILE $CUR_MAKEFILE + echo "Makefile done (copy still alive in ~/Documents/.OLDMakefile)" +else + echo "Makefile not found." +fi