summaryrefslogtreecommitdiffstats
path: root/src/s_com.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/s_com.c')
-rw-r--r--src/s_com.c130
1 files changed, 130 insertions, 0 deletions
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);
+}