diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-12 20:43:30 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-12 20:43:30 +0200 |
commit | ff116f01c3680d0e5d8c32dd9978ed05a37f800c (patch) | |
tree | 73765e5ac7ea4ec8af461f44f6d8288fdad6fd82 | |
parent | Lots of stuff to do (diff) | |
download | 42-minishell-ff116f01c3680d0e5d8c32dd9978ed05a37f800c.tar.gz 42-minishell-ff116f01c3680d0e5d8c32dd9978ed05a37f800c.tar.bz2 42-minishell-ff116f01c3680d0e5d8c32dd9978ed05a37f800c.tar.xz 42-minishell-ff116f01c3680d0e5d8c32dd9978ed05a37f800c.tar.zst 42-minishell-ff116f01c3680d0e5d8c32dd9978ed05a37f800c.zip |
FeelsCleanMan
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | TODO.org | 5 | ||||
-rw-r--r-- | src/b_exit.c | 2 | ||||
-rw-r--r-- | src/b_export.c | 2 | ||||
-rw-r--r-- | src/b_export_mute.c | 2 | ||||
-rw-r--r-- | src/b_type.c | 2 | ||||
-rw-r--r-- | src/e_builtins.c | 14 | ||||
-rw-r--r-- | src/e_builtins.h | 2 | ||||
-rw-r--r-- | src/e_externs.c | 18 | ||||
-rw-r--r-- | src/e_externs.h | 2 | ||||
-rw-r--r-- | src/e_externs_next.c | 2 | ||||
-rw-r--r-- | src/e_externs_pipes.c | 16 | ||||
-rw-r--r-- | src/e_line.c (renamed from src/e_lcom.c) | 17 | ||||
-rw-r--r-- | src/e_line.h (renamed from src/e_lcom.h) | 4 | ||||
-rw-r--r-- | src/e_pipes.c | 2 | ||||
-rw-r--r-- | src/e_pipes.h | 2 | ||||
-rw-r--r-- | src/f_alloc.c | 8 | ||||
-rw-r--r-- | src/f_alloc.h | 2 | ||||
-rw-r--r-- | src/f_redir.c | 4 | ||||
-rw-r--r-- | src/m_comm.c | 8 | ||||
-rw-r--r-- | src/m_loop.c | 12 | ||||
-rw-r--r-- | src/m_redirs.c | 4 | ||||
-rw-r--r-- | src/m_redirs.h | 2 | ||||
-rw-r--r-- | src/p_lcom.c | 50 | ||||
-rw-r--r-- | src/p_lcom.h | 2 | ||||
-rw-r--r-- | src/p_lcom_next.c | 2 | ||||
-rw-r--r-- | src/p_lcom_next.h | 2 | ||||
-rw-r--r-- | src/p_line.c | 4 | ||||
-rw-r--r-- | src/s_com.c | 130 | ||||
-rw-r--r-- | src/s_com.h (renamed from src/s_lcom_next.h) | 9 | ||||
-rw-r--r-- | src/s_lcom.c | 149 | ||||
-rw-r--r-- | src/s_lcom_next.c | 38 | ||||
-rw-r--r-- | src/s_line.c | 92 | ||||
-rw-r--r-- | src/s_line.h (renamed from src/s_lcom.h) | 10 | ||||
-rw-r--r-- | src/s_lpipes.c | 19 | ||||
-rw-r--r-- | src/s_lpipes.h | 2 | ||||
-rw-r--r-- | src/s_struct.h | 21 | ||||
-rw-r--r-- | src/u_vars.c | 2 |
38 files changed, 355 insertions, 315 deletions
@@ -29,7 +29,7 @@ SRCS_NAME += e_builtins SRCS_NAME += e_externs SRCS_NAME += e_externs_next SRCS_NAME += e_externs_pipes -SRCS_NAME += e_lcom +SRCS_NAME += e_line SRCS_NAME += e_pipes SRCS_NAME += f_alloc SRCS_NAME += f_chdir @@ -46,9 +46,9 @@ SRCS_NAME += m_minishell SRCS_NAME += m_prompt SRCS_NAME += m_redirs SRCS_NAME += s_destroy +SRCS_NAME += s_com SRCS_NAME += s_init -SRCS_NAME += s_lcom -SRCS_NAME += s_lcom_next +SRCS_NAME += s_line SRCS_NAME += s_lvars SRCS_NAME += s_lpipes SRCS_NAME += p_line @@ -2,11 +2,12 @@ ** DONE PWD=qwe; pwd <== recheck ** DONE VISUAL=qweqwe crontab -e <=== deal with it ** DONE cd qweqwe <=== stderr -** TODO [#A] lpipes leaks <=== this is a big one (split lpipes | lcom | com) +** DONE [#A] lpipes leaks <=== this is a big one (split lpipes | lcom | com) +** TODO [#A] lpipes leaks on builtins ** TODO [#C] Handle memory * Stuff to add -** TODO [#A] builtins to pipes +** DONE [#A] builtins to pipes ** TODO [#A] Proper ~ subst ** TODO [#B] && || ** TODO [#B] & diff --git a/src/b_exit.c b/src/b_exit.c index 8e912a6..19e55d9 100644 --- a/src/b_exit.c +++ b/src/b_exit.c @@ -16,7 +16,7 @@ #include <unistd.h> #include "f_fail.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_destroy.h" #include "s_struct.h" #include "u_utils.h" diff --git a/src/b_export.c b/src/b_export.c index 62dc64b..4267b4d 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -19,7 +19,7 @@ #include "d_enum.h" #include "f_fail.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_lvars.h" #include "s_struct.h" #include "u_utils.h" diff --git a/src/b_export_mute.c b/src/b_export_mute.c index eed7f60..4969320 100644 --- a/src/b_export_mute.c +++ b/src/b_export_mute.c @@ -19,7 +19,7 @@ #include "d_enum.h" #include "f_fail.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_lvars.h" #include "s_struct.h" #include "u_utils.h" diff --git a/src/b_type.c b/src/b_type.c index 16791c9..0d80869 100644 --- a/src/b_type.c +++ b/src/b_type.c @@ -17,7 +17,7 @@ #include <unistd.h> #include "f_fail.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" #include "e_externs_next.h" #include "u_utils.h" diff --git a/src/e_builtins.c b/src/e_builtins.c index 7b074af..c00114d 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -20,11 +20,11 @@ #include "b_export_mute.h" #include "m_redirs.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" static void - e_export_env_fork(const t_lcom *ptr, + e_export_env_fork(const t_com *ptr, t_msh *msh) { char **re_ptr; @@ -38,7 +38,7 @@ static void } static void - e_builtin_child(const t_lcom *ptr, + e_builtin_child(const t_com *ptr, uint8_t bu_id, t_msh *msh) { @@ -48,14 +48,14 @@ static void e_export_env_fork(ptr, msh); dup_redirs(ptr, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); - lcom_clear(&msh->curr); + s_line_clear(&msh->curr); s_destroy(msh); exit(ret); } static void e_builtin_parent(pid_t pid, - const t_lcom *ptr, + const t_com *ptr, uint8_t bu_id, t_msh *msh) { @@ -73,14 +73,14 @@ static void b_export_mute(ptr->argv + 1, msh); else if (bu_id == FT_ID_EXIT && ret == 0) { - lcom_clear(&msh->curr); + s_line_clear(&msh->curr); s_destroy(msh); exit(ret); } } void - e_builtin(const t_lcom *ptr, + e_builtin(const t_com *ptr, uint8_t bu_id, t_msh *msh) { diff --git a/src/e_builtins.h b/src/e_builtins.h index 25026cc..f3492c9 100644 --- a/src/e_builtins.h +++ b/src/e_builtins.h @@ -17,6 +17,6 @@ #include "s_struct.h" -void e_builtin(const t_lcom *ptr, uint8_t bu_id, t_msh *msh); +void e_builtin(const t_com *ptr, uint8_t bu_id, t_msh *msh); #endif diff --git a/src/e_externs.c b/src/e_externs.c index 0e3c2bb..554b3d1 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -21,12 +21,12 @@ #include "f_fail.h" #include "m_redirs.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" static void e_extern_child(const char *fullpath, - t_lcom *ptr, + t_com *ptr, t_msh *msh) { dup_redirs(ptr, msh); @@ -35,7 +35,7 @@ static void } static void - e_export_env_fork(t_lcom *ptr, + e_export_env_fork(t_com *ptr, t_msh *msh) { char **re_ptr; @@ -50,7 +50,7 @@ static void static void exec_path(const char fullpath[], - t_lcom *ptr, + t_com *ptr, t_msh *msh) { pid_t pid; @@ -75,24 +75,24 @@ static void } void - e_extern(t_lcom *ptr, + e_extern(t_com *ptr, t_msh *msh) { char **envpath; char *fullpath; - if (ft_ischarset("./", ptr->com[0])) + if (ft_ischarset("./", ptr->bin[0])) { - exec_path(ptr->com, ptr, msh); + exec_path(ptr->bin, ptr, msh); return ; } else if ((envpath = get_env_path(msh)) != NULL) { - fullpath = search_in_path(ptr->com, envpath, msh); + fullpath = search_in_path(ptr->bin, envpath, msh); ft_delwords(envpath); if (fullpath == NULL) { - f_fail_command_not_found(ptr->com); + f_fail_command_not_found(ptr->bin); } else { diff --git a/src/e_externs.h b/src/e_externs.h index 878bb83..3e666f9 100644 --- a/src/e_externs.h +++ b/src/e_externs.h @@ -15,6 +15,6 @@ #include "s_struct.h" -void e_extern(t_lcom *ptr, t_msh *msh); +void e_extern(t_com *ptr, t_msh *msh); #endif diff --git a/src/e_externs_next.c b/src/e_externs_next.c index 5d7deaf..c1e248f 100644 --- a/src/e_externs_next.c +++ b/src/e_externs_next.c @@ -17,7 +17,7 @@ #include "f_fail.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" #include "u_utils.h" diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index 13842a7..6c78029 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -23,7 +23,7 @@ #include "f_fail.h" #include "m_redirs.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_lpipes.h" #include "s_struct.h" @@ -44,7 +44,7 @@ static uint8_t static void e_extern_child(const char *fullpath, - t_lcom *ptr, + t_com *ptr, t_msh *msh) { uint8_t bu_id; @@ -53,7 +53,7 @@ static void dup_redirs(ptr, msh); if (ft_strncmp(fullpath, "builtin", 8) == 0) { - bu_id = get_builtin_id(ptr->com, msh); + bu_id = get_builtin_id(ptr->bin, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); exit(ret); } @@ -114,7 +114,7 @@ static void close(fd[j][FT_READ_END]); j++; } - e_extern_child(fullpath[i], head->one, msh); + e_extern_child(fullpath[i], head->com, msh); } head = head->next; i++; @@ -151,21 +151,21 @@ void i = 0; while (rptr != NULL) { - if (ft_ischarset("/.", rptr->one->com[0])) + if (ft_ischarset("/.", rptr->com->bin[0])) { - if ((fullpath[i] = ft_strdup(rptr->one->com)) == NULL) + if ((fullpath[i] = ft_strdup(rptr->com->bin)) == NULL) f_alloc_and_destroy_msh(msh); } else if ((envpath = get_env_path(msh)) != NULL) { - if ((bu_id = get_builtin_id(rptr->one->com, msh)) + if ((bu_id = get_builtin_id(rptr->com->bin, msh)) < FT_BUILTINS_COUNT) { if ((fullpath[i] = ft_strdup("builtin")) == NULL) f_alloc_and_destroy_msh(msh); } else - fullpath[i] = search_in_path(rptr->one->com, envpath, msh); + fullpath[i] = search_in_path(rptr->com->bin, envpath, msh); ft_delwords(envpath); } i++; diff --git a/src/e_lcom.c b/src/e_line.c index 30b55d2..4489926 100644 --- a/src/e_lcom.c +++ b/src/e_line.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* e_lcom.c :+: :+: :+: */ +/* e_line.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -21,13 +21,13 @@ #include "s_struct.h" static uint8_t - get_builtin_id(const char com[], + get_builtin_id(const char bin[], t_msh *msh) { uint8_t i; i = 0; - while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i], + while (msh->bu_ref[i] && ft_strncmp(bin, msh->bu_ref[i], ft_strlen(msh->bu_ref[i]) + 1) != 0) { i++; @@ -36,9 +36,9 @@ static uint8_t } void - e_lcom(t_msh *msh) + e_line(t_msh *msh) { - t_lcom *ptr; + t_line *ptr; uint8_t bu_id; ptr = msh->curr; @@ -50,10 +50,11 @@ void } else if (ptr->com) { - if ((bu_id = get_builtin_id(ptr->com, msh)) < FT_BUILTINS_COUNT) - e_builtin(ptr, bu_id, msh); + if ((bu_id = get_builtin_id(ptr->com->bin, msh)) + < FT_BUILTINS_COUNT) + e_builtin(ptr->com, bu_id, msh); else - e_extern(ptr, msh); + e_extern(ptr->com, msh); } ptr = ptr->next; } diff --git a/src/e_lcom.h b/src/e_line.h index ab0368f..02b7714 100644 --- a/src/e_lcom.h +++ b/src/e_line.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* e_lcom.h :+: :+: :+: */ +/* e_line.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -16,6 +16,6 @@ #include <stdint.h> #include "s_struct.h" -void e_lcom(t_msh *msh); +void e_line(t_msh *msh); #endif diff --git a/src/e_pipes.c b/src/e_pipes.c index 223a467..30fced2 100644 --- a/src/e_pipes.c +++ b/src/e_pipes.c @@ -35,7 +35,7 @@ /* } */ void - e_pipes(t_lcom *ptr, + e_pipes(t_line *ptr, t_msh *msh) { /* uint8_t bu_id; */ diff --git a/src/e_pipes.h b/src/e_pipes.h index d4de639..5bc073a 100644 --- a/src/e_pipes.h +++ b/src/e_pipes.h @@ -15,6 +15,6 @@ #include "s_struct.h" -void e_pipes(t_lcom *ptr, t_msh *msh); +void e_pipes(t_line *ptr, t_msh *msh); #endif diff --git a/src/f_alloc.c b/src/f_alloc.c index 62b7008..641bc9f 100644 --- a/src/f_alloc.c +++ b/src/f_alloc.c @@ -17,7 +17,7 @@ #include <errno.h> #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" void @@ -25,7 +25,7 @@ void { char tmp[255]; - lcom_clear(&msh->curr); + s_line_clear(&msh->curr); ft_strlcpy(tmp, msh->shname, ft_strlen(msh->shname) + 1); s_destroy(msh); ft_dprintf(STDERR_FILENO, "%s: %s\n", tmp, strerror(errno)); @@ -33,8 +33,8 @@ void } void - f_alloc_and_clear_lcom(t_msh *msh) + f_alloc_and_clear_line(t_msh *msh) { - lcom_clear(&msh->curr); + s_line_clear(&msh->curr); ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); } diff --git a/src/f_alloc.h b/src/f_alloc.h index d309802..73f7a91 100644 --- a/src/f_alloc.h +++ b/src/f_alloc.h @@ -16,6 +16,6 @@ #include "s_struct.h" void f_alloc_and_destroy_msh(t_msh *msh); -void f_alloc_and_clear_lcom(t_msh *msh); +void f_alloc_and_clear_line(t_msh *msh); #endif diff --git a/src/f_redir.c b/src/f_redir.c index 3561272..71e5613 100644 --- a/src/f_redir.c +++ b/src/f_redir.c @@ -16,7 +16,7 @@ #include <unistd.h> #include <errno.h> -#include "s_lcom.h" +#include "s_line.h" #include "s_destroy.h" void @@ -25,7 +25,7 @@ void { ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->shname, path, strerror(errno)); - lcom_clear(&msh->curr); + s_line_clear(&msh->curr); s_destroy(msh); exit(1); } diff --git a/src/m_comm.c b/src/m_comm.c index 66e0ac2..30f609d 100644 --- a/src/m_comm.c +++ b/src/m_comm.c @@ -14,10 +14,10 @@ #include <stdint.h> #include "d_enum.h" -#include "e_lcom.h" +#include "e_line.h" #include "m_prompt.h" #include "p_line.h" -#include "s_lcom.h" +#include "s_line.h" uint8_t m_comm(const char line[], @@ -26,8 +26,8 @@ uint8_t if (line[0] != '\0') { p_line((char*)line, msh); - e_lcom(msh); - lcom_clear(&msh->curr); + e_line(msh); + s_line_clear(&msh->curr); } else { diff --git a/src/m_loop.c b/src/m_loop.c index 143c25d..4d942b2 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -15,10 +15,10 @@ #include <stdlib.h> #include <unistd.h> -#include "e_lcom.h" +#include "e_line.h" #include "m_prompt.h" #include "p_line.h" -#include "s_lcom.h" +#include "s_line.h" static char @@ -77,8 +77,8 @@ void p_line(fin, msh); ft_memdel((void*)&prog); ft_memdel((void*)&fin); - e_lcom(msh); - lcom_clear(&msh->curr); + e_line(msh); + s_line_clear(&msh->curr); } uint8_t @@ -102,8 +102,8 @@ uint8_t { p_line(line, msh); ft_memdel((void*)&line); - e_lcom(msh); - lcom_clear(&msh->curr); + e_line(msh); + s_line_clear(&msh->curr); } else { diff --git a/src/m_redirs.c b/src/m_redirs.c index 2414dcd..91014bb 100644 --- a/src/m_redirs.c +++ b/src/m_redirs.c @@ -16,11 +16,11 @@ #include "f_fail.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" void - dup_redirs(const t_lcom *ptr, + dup_redirs(const t_com *ptr, t_msh *msh) { int32_t fd; diff --git a/src/m_redirs.h b/src/m_redirs.h index 6d1b96a..fc33758 100644 --- a/src/m_redirs.h +++ b/src/m_redirs.h @@ -15,6 +15,6 @@ #include "s_struct.h" -void dup_redirs(const t_lcom *ptr, t_msh *msh); +void dup_redirs(const t_com *ptr, t_msh *msh); #endif diff --git a/src/p_lcom.c b/src/p_lcom.c index 2dbd505..339ab17 100644 --- a/src/p_lcom.c +++ b/src/p_lcom.c @@ -19,23 +19,23 @@ #include "d_define.h" #include "f_fail.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_lpipes.h" #include "s_struct.h" static void rdr_err_check(char *ptr, - t_lcom **link) + t_com **com) { - if ((*link)->redir == -1 && ft_ischarset("><", *(ptr + 1))) + if ((*com)->redir == -1 && ft_ischarset("><", *(ptr + 1))) { /* TODO: syntax err */ } - else if ((*link)->redir == 1 && ft_ischarset("<", *(ptr + 1))) + else if ((*com)->redir == 1 && ft_ischarset("<", *(ptr + 1))) { /* TODO: syntax err */ } - else if ((*link)->redir == 2 && ft_ischarset("<>", *(ptr + 1))) + else if ((*com)->redir == 2 && ft_ischarset("<>", *(ptr + 1))) { /* TODO: syntax err */ } @@ -43,17 +43,17 @@ static void static int8_t get_rdrpath(char *ptr, - t_lcom **link) + t_com **com) { char *p_rdrpath; - ptr += ((*link)->redir == 2) ? (2) : (1); - if (!((*link)->rdrpath = + ptr += ((*com)->redir == 2) ? (2) : (1); + if (!((*com)->rdrpath = (char*)malloc((ft_strlen(ptr) + 1) * sizeof(char)))) { return (-1); } - p_rdrpath = (*link)->rdrpath; + p_rdrpath = (*com)->rdrpath; while (*ptr) { if (*ptr != ' ') @@ -69,24 +69,24 @@ static int8_t static void get_rdrfd(const char *ptr, - t_lcom **link) + t_com **com) { while (ft_isdigit(*ptr)) { ptr--; } if (*ptr != ' ') - (*link)->rdrfd = STDOUT_FILENO; + (*com)->rdrfd = STDOUT_FILENO; else { ptr += 1; - (*link)->rdrfd = ft_atoi(ptr); + (*com)->rdrfd = ft_atoi(ptr); } } int8_t get_redir(const char word[], - t_lcom **link) + t_com **com) { /* TODO: norme */ char *ptr; @@ -96,12 +96,12 @@ int8_t { if (*ptr == '<') { - (*link)->redir = -1; + (*com)->redir = -1; break ; } if (*ptr == '>') { - (*link)->redir = (*(ptr + 1) == '>') ? (2) : (1); + (*com)->redir = (*(ptr + 1) == '>') ? (2) : (1); break ; } ptr++; @@ -109,14 +109,14 @@ int8_t /* hint: bash only handles the last one */ /* TODO: handle "msh ~> cat < Makefile >qwe" | gl hf */ } - if ((*link)->redir > 0) + if ((*com)->redir > 0) { if (ft_isdigit(*(ptr - 1))) - get_rdrfd(ptr - 1, link); + get_rdrfd(ptr - 1, com); else - (*link)->rdrfd = STDOUT_FILENO; - rdr_err_check(ptr, link); - if (get_rdrpath(ptr, link) != 0) + (*com)->rdrfd = STDOUT_FILENO; + rdr_err_check(ptr, com); + if (get_rdrpath(ptr, com) != 0) return (-1); } return (0); @@ -129,7 +129,7 @@ int8_t { /* TODO: norme */ uint64_t i; - t_lcom *link; + t_line *link; char **words; t_bool next; @@ -141,15 +141,15 @@ int8_t next = FALSE; if (ft_strchr(words[i], '|')) { - if (!(link = lcom_new(NULL, msh))) + if ((link = s_line_new(NULL, msh)) == NULL) return (-1); - if (!(split_pipes(words[i], link, msh))) + if ((split_pipes(words[i], link, msh)) == NULL) return (-1); next = TRUE; } - if (next == FALSE && !(link = lcom_new(words[i], msh))) + if (next == FALSE && (link = s_line_new(words[i], msh)) == NULL) return (-1); - lcom_add_back(&msh->curr, link); + s_line_add_back(&msh->curr, link); i++; } ft_delwords(words); diff --git a/src/p_lcom.h b/src/p_lcom.h index 393d7e5..d84269e 100644 --- a/src/p_lcom.h +++ b/src/p_lcom.h @@ -17,7 +17,7 @@ #include "s_struct.h" -int8_t get_redir(const char word[], t_lcom **link); +int8_t get_redir(const char word[], t_com **com); int8_t p_lcom(const char line[], const uint64_t count, t_msh *msh); #endif diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 9a39dd0..cdceef5 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* p_lcom_next.c :+: :+: :+: */ +/* p_line_next.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h index 2d0ae98..f9ae416 100644 --- a/src/p_lcom_next.h +++ b/src/p_lcom_next.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* p_lcom_next.h :+: :+: :+: */ +/* p_line_next.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ diff --git a/src/p_line.c b/src/p_line.c index a9a918c..23a3896 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -15,14 +15,14 @@ #include "d_enum.h" #include "f_fail.h" -#include "p_lcom.h" #include "p_line.h" +#include "p_lcom.h" #include "s_destroy.h" #include "s_struct.h" void p_line(char line[], - t_msh *msh) + t_msh *msh) { char *ptr; uint64_t count; diff --git a/src/s_com.c b/src/s_com.c new file mode 100644 index 0000000..576b407 --- /dev/null +++ b/src/s_com.c @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_com.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> + +#include "f_fail.h" +#include "p_lcom.h" +#include "p_lcom_next.h" +#include "s_struct.h" + +static int8_t + fill_com(char *words[], + t_com **com) +{ + /* TODO: norme */ + uint64_t i; + uint64_t j; + + i = 0; + if (words[0]) + { + if (((*com)->bin = (char*)malloc((ft_strlen(words[0]) + 1) * + sizeof(char))) == NULL) + return (-1); + ft_strlcpy((*com)->bin, words[0], ft_strlen(words[0]) + 1); + } + else + return (0); + while(words[i]) + { + /* TODO: cut fd number "msh ~> echo a 2>file" */ + /* ^ */ + if (ft_ischarset("<>", words[i][0])) + break ; + i++; + } + if (!((*com)->argv = (char**)malloc((i + 1) * sizeof(char*)))) + return (-1); + j = 0; + while (i > 0 && j < i) + { + if (!((*com)->argv[j] = ft_strdup(words[j]))) + return (-1); + j++; + } + (*com)->argv[j] = 0; + return (0); +} + +static void + com_cpy_env_fork(t_com **com, + t_msh *msh) +{ + size_t i; + size_t j; + + i = 0; + while (msh->env_fork_tmp[i][0] != '\0') + i++; + if (((*com)->env_fork = (char**)malloc((i + 1) * sizeof(char*))) == NULL) + f_alloc_and_destroy_msh(msh); + j = 0; + while (j < i) + { + if (((*com)->env_fork[j] = ft_strdup(msh->env_fork_tmp[j])) == NULL) + f_alloc_and_destroy_msh(msh); + j++; + } + (*com)->env_fork[j] = NULL; +} + +void + s_com_destroy(t_com **com) +{ + t_com *ptr; + + ptr = *com; + ft_memdel((void*)&ptr->bin); + if (ptr->argv != NULL) + ft_delwords(ptr->argv); + if (ptr->redir != 0) + ft_memdel((void*)&ptr->rdrpath); + if (ptr->env_fork != NULL) + ft_delwords(ptr->env_fork); + ft_memdel((void*)&ptr); + +} + +t_com + *s_com_new(char word[], + t_msh *msh) +{ + t_com *com; + char **words; + + if ((com = (t_com*)malloc(sizeof(t_com))) == NULL) + return (NULL); + com->redir = 0; + com->bin = NULL; + com->argv = NULL; + com->rdrfd = 0; + com->rdrpath = NULL; + com->env_fork = NULL; + if (get_redir(word, &com) != 0) + return (NULL); + if (!(words = p_subst_args(word, com->redir))) + return (NULL); + if (!(words = p_subst_vars(words, msh))) + return (NULL); + words = p_check_args_equals(words, msh); + if (msh->env_fork_tmp[0][0] != '\0') + com_cpy_env_fork(&com, msh); + if (fill_com(words, &com) < 0) + { + ft_delwords(words); + return (NULL); + } + ft_delwords(words); + return (com); +} diff --git a/src/s_lcom_next.h b/src/s_com.h index 96ae01c..aad61e2 100644 --- a/src/s_lcom_next.h +++ b/src/s_com.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* s_lcom_next.h :+: :+: :+: */ +/* s_com.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,11 +10,12 @@ /* */ /* ************************************************************************** */ -#ifndef S_LCOM_NEXT_H -#define S_LCOM_NEXT_H +#ifndef S_COM_H +#define S_COM_H #include "s_struct.h" -void lcom_cpy_env_fork(t_lcom **link, t_msh *msh); +void s_com_destroy(t_com **com); +t_com *s_com_new(const char word[], t_msh *msh); #endif diff --git a/src/s_lcom.c b/src/s_lcom.c deleted file mode 100644 index 536a8ff..0000000 --- a/src/s_lcom.c +++ /dev/null @@ -1,149 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* s_lcom.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include <libft.h> -#include <stdlib.h> -#include <stdint.h> - -#include "p_lcom.h" -#include "p_lcom_next.h" -#include "s_struct.h" -#include "s_lcom_next.h" - -static int8_t - fill_lcom(char *words[], - t_lcom **lcom) -{ - /* TODO: norme */ - uint64_t i; - uint64_t j; - - i = 0; - if (words[0]) - { - if (!((*lcom)->com = (char*)malloc((ft_strlen(words[0]) + 1) * - sizeof(char)))) - return (-1); - ft_strlcpy((*lcom)->com, words[0], ft_strlen(words[0]) + 1); - } - else - return (0); - while(words[i]) - { - /* TODO: cut fd number "msh ~> echo a 2>file" */ - /* ^ */ - if (ft_ischarset("<>", words[i][0])) - break ; - i++; - } - if (!((*lcom)->argv = (char**)malloc((i + 1) * sizeof(char*)))) - return (-1); - j = 0; - while (i > 0 && j < i) - { - if (!((*lcom)->argv[j] = ft_strdup(words[j]))) - return (-1); - j++; - } - (*lcom)->argv[j] = 0; - return (0); -} - -t_lcom - *lcom_last(t_lcom *lcom) -{ - while (lcom->next != NULL) - lcom = lcom->next; - return (lcom); -} - -void - lcom_add_back(t_lcom **alcom, - t_lcom *new) -{ - t_lcom *tmp; - - if (!*alcom) - *alcom = new; - else - { - tmp = lcom_last(*alcom); - tmp->next = new; - } -} - -void - lcom_clear(t_lcom **lcom) -{ - t_lcom *tmp; - t_lcom *renext; - - if (!lcom) - return ; - tmp = *lcom; - while (tmp) - { - renext = tmp->next; - ft_memdel((void*)&tmp->com); - if (tmp->argv) - ft_delwords(tmp->argv); - if (tmp->redir != 0) - ft_memdel((void*)&tmp->rdrpath); - if (tmp->env_fork != NULL) - ft_delwords(tmp->env_fork); - ft_memdel((void*)&tmp); - tmp = renext; - } - *lcom = NULL; -} - -t_lcom - *lcom_new(const char word[], - t_msh *msh) -{ - /* TODO: norme */ - t_lcom *link; - char **words; - - if (!(link = (t_lcom*)malloc(sizeof(t_lcom)))) - return (NULL); - link->redir = 0; - link->com = NULL; - link->argv = NULL; - link->rdrfd = 0; - link->rdrpath = NULL; - link->pipes = NULL; - link->env_fork = NULL; - if (!word) - { - link->next = NULL; - return (link); - } - link->pipes = NULL; - if (get_redir(word, &link) != 0) - return (NULL); - if (!(words = p_subst_args(word, link->redir))) - return (NULL); - if (!(words = p_subst_vars(words, msh))) - return (NULL); - words = p_check_args_equals(words, msh); - if (msh->env_fork_tmp[0][0] != '\0') - lcom_cpy_env_fork(&link, msh); - if (fill_lcom(words, &link) < 0) - { - ft_delwords(words); - return (NULL); - } - link->next = NULL; - ft_delwords(words); - return (link); -} diff --git a/src/s_lcom_next.c b/src/s_lcom_next.c deleted file mode 100644 index b7e4482..0000000 --- a/src/s_lcom_next.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* s_lcom_next.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include <libft.h> -#include <stdlib.h> - -#include "f_fail.h" - -void - lcom_cpy_env_fork(t_lcom **link, - t_msh *msh) -{ - size_t i; - size_t j; - - i = 0; - while (msh->env_fork_tmp[i][0] != '\0') - i++; - if (((*link)->env_fork = (char**)malloc((i + 1) * sizeof(char*))) == NULL) - f_alloc_and_destroy_msh(msh); - j = 0; - while (j < i) - { - if (((*link)->env_fork[j] = ft_strdup(msh->env_fork_tmp[j])) == NULL) - f_alloc_and_destroy_msh(msh); - j++; - } - (*link)->env_fork[j] = NULL; -} diff --git a/src/s_line.c b/src/s_line.c new file mode 100644 index 0000000..5fe9a96 --- /dev/null +++ b/src/s_line.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdint.h> + +#include "p_line.h" +#include "s_com.h" +#include "s_struct.h" + +t_line + *s_line_last(t_line *line) +{ + while (line->next != NULL) + line = line->next; + return (line); +} + +void + s_line_add_back(t_line **aline, + t_line *new) +{ + t_line *tmp; + + if (!*aline) + *aline = new; + else + { + tmp = s_line_last(*aline); + tmp->next = new; + } +} + +void + s_line_clear(t_line **line) +{ + t_line *tmp; + t_line *renext; + + if (!line) + return ; + tmp = *line; + while (tmp != NULL) + { + renext = tmp->next; + if (tmp->com != NULL) + { + s_com_destroy(&tmp->com); + } + /* ft_memdel((void*)&tmp->com); */ + /* if (tmp->argv) */ + /* ft_delwords(tmp->argv); */ + /* if (tmp->redir != 0) */ + /* ft_memdel((void*)&tmp->rdrpath); */ + /* if (tmp->env_fork != NULL) */ + /* ft_delwords(tmp->env_fork); */ + ft_memdel((void*)&tmp); + tmp = renext; + } + *line = NULL; +} + +t_line + *s_line_new(const char word[], + t_msh *msh) +{ + t_line *link; + + if ((link = (t_line*)malloc(sizeof(t_line))) == NULL) + return (NULL); + link->com = NULL; + link->pipes = NULL; + link->next = NULL; + if (word == NULL) + { + link->next = NULL; + return (link); + } + link->com = s_com_new(word, msh); + /* TODO: check link->com = NULL */ + return (link); +} diff --git a/src/s_lcom.h b/src/s_line.h index 10fcf7c..cd2211b 100644 --- a/src/s_lcom.h +++ b/src/s_line.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* s_lcom.h :+: :+: :+: */ +/* s_line.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -15,9 +15,9 @@ #include "s_struct.h" -void lcom_add_back(t_lcom **alcom, t_lcom *new); -void lcom_clear(t_lcom **lcom); -t_lcom *lcom_new(const char word[], t_msh *msh); -t_lcom *lcom_last(t_lcom *lcom); +void s_line_add_back(t_line **aline, t_line *new); +void s_line_clear(t_line **line); +t_line *s_line_new(const char word[], t_msh *msh); +t_line *s_line_last(t_line *line); #endif diff --git a/src/s_lpipes.c b/src/s_lpipes.c index cb78df3..9d10849 100644 --- a/src/s_lpipes.c +++ b/src/s_lpipes.c @@ -14,7 +14,8 @@ #include <stdlib.h> #include <stdint.h> -#include "s_lcom.h" +#include "s_com.h" +#include "s_line.h" #include "s_lpipes.h" #include "s_struct.h" @@ -53,7 +54,7 @@ void while (tmp) { renext = tmp->next; - lcom_clear(&tmp->one); + s_com_destroy(&tmp->com); ft_memdel((void*)&tmp); tmp = renext; } @@ -62,14 +63,14 @@ void struct s_lpipes *lpipes_new(const char pipedword[], - t_msh *msh) + t_msh *msh) { struct s_lpipes *link; if (!(link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) return (NULL); - link->one = NULL; - if (!(link->one = lcom_new(pipedword, msh))) + link->com = NULL; + if ((link->com = s_com_new(pipedword, msh)) == NULL) { return (NULL); } @@ -79,8 +80,8 @@ struct s_lpipes struct s_lpipes *split_pipes(const char word[], - t_lcom *lcom, - t_msh *msh) + t_line *line, + t_msh *msh) { struct s_lpipes *lpipes; char **words; @@ -89,15 +90,13 @@ struct s_lpipes if (!(words = ft_split(word, '|'))) return (NULL); i = 0; - if (!(lpipes = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) - return (NULL); while (words[i]) { if (!(lpipes = lpipes_new(words[i], msh))) { return (NULL); } - lpipes_add_back(&lcom->pipes, lpipes); + lpipes_add_back(&line->pipes, lpipes); i++; } ft_delwords(words); diff --git a/src/s_lpipes.h b/src/s_lpipes.h index 1fc6eec..7b90d2c 100644 --- a/src/s_lpipes.h +++ b/src/s_lpipes.h @@ -20,6 +20,6 @@ void lpipes_add_back(struct s_lpipes **alpipes, struct s_lpipes *new); void lpipes_clear(struct s_lpipes **lpipes); struct s_lpipes *lpipes_new(const char pipedword[], t_msh *msh); -struct s_lpipes *split_pipes(const char word[], t_lcom *lcom, t_msh *msh); +struct s_lpipes *split_pipes(const char word[], t_line *line, t_msh *msh); #endif diff --git a/src/s_struct.h b/src/s_struct.h index a730b78..e03bbf2 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -26,8 +26,6 @@ ** 0: means no redirection */ -typedef struct s_lpipes *t_lpipes; - typedef struct s_lvars { char *name; @@ -35,24 +33,29 @@ typedef struct s_lvars struct s_lvars *next; } t_lvars; -typedef struct s_lcom +typedef struct s_com { - char *com; + char *bin; char **argv; int8_t redir; int32_t rdrfd; char *rdrpath; char **env_fork; - struct s_lpipes *pipes; - struct s_lcom *next; -} t_lcom; +} t_com; struct s_lpipes { - struct s_lcom *one; + struct s_com *com; struct s_lpipes *next; }; +typedef struct s_line +{ + struct s_com *com; + struct s_lpipes *pipes; + struct s_line *next; +} t_line; + typedef struct s_msh { char **envp; @@ -63,7 +66,7 @@ typedef struct s_msh char *cwd; char env_fork_tmp[128][1024]; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); - struct s_lcom *curr; + struct s_line *curr; struct s_lvars *vars; } t_msh; diff --git a/src/u_vars.c b/src/u_vars.c index ebdba25..9594036 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -17,7 +17,7 @@ #include "d_define.h" #include "f_fail.h" #include "s_destroy.h" -#include "s_lcom.h" +#include "s_line.h" #include "s_struct.h" #include "u_utils.h" |