summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile4
-rw-r--r--TODO.org21
-rw-r--r--src/b_alias.c1
-rw-r--r--src/c_init.c2
-rw-r--r--src/c_input.c7
-rw-r--r--src/c_input.h5
-rw-r--r--src/c_keys.c4
-rw-r--r--src/f_alloc.c12
-rw-r--r--src/f_alloc.h1
-rw-r--r--src/m_funptr.c2
-rw-r--r--src/m_funptr.h2
-rw-r--r--src/m_loop.c19
-rw-r--r--src/m_minishell.c2
-rw-r--r--src/s_init.c20
-rw-r--r--src/s_init_next.c30
-rw-r--r--src/s_init_next.h2
-rw-r--r--src/u_vars_next.c26
-rw-r--r--src/u_vars_next.h2
19 files changed, 117 insertions, 47 deletions
diff --git a/.gitignore b/.gitignore
index 948a82a..d608b22 100644
--- a/.gitignore
+++ b/.gitignore
@@ -158,3 +158,5 @@ compile_flags.txt
/mini
.ccls*
tags
+TAGS
+*.supp
diff --git a/Makefile b/Makefile
index 29adfde..8fc99b5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-default: all
+default: debug
#==============================================================================#
#--------------------------------- SHELL --------------------------------------#
#==============================================================================#
@@ -148,7 +148,7 @@ MKDIR = mkdir -p
#==============================================================================#
LFTRULE = all
#------------------------------------------------------------------------------#
-${OBJS_DIR}%.o: ${SRCS_DIR}%.c ${INCS}
+${OBJS_DIR}%.o: ${SRCS_DIR}%.c ${INCS}
${CC} -c ${CFLAGS} ${CDEFS} -I${LFT_INCS_DIR} -o $@ $<
#------------------------------------------------------------------------------#
${OBJS_DIR}:
diff --git a/TODO.org b/TODO.org
index ab47b5a..6f2018b 100644
--- a/TODO.org
+++ b/TODO.org
@@ -33,7 +33,7 @@
CLOSED: [2020-10-22 Thu 21:47]
** DONE 'echo qweqwe 1>file' file has 'qweqwe 1' in it
CLOSED: [2020-10-27 Tue 19:35]
-** CANCELED alias ls='clear; ls | grep qwe' <--- aliases not substed in the right place
+** CANCELLED alias ls='clear; ls | grep qwe' <--- aliases not substed in the right place
CLOSED: [2020-10-19 Mon 19:56]
** DONE forked write(2) stuff on cd
** DONE Multiline && ||
@@ -58,16 +58,19 @@
** DONE macOS compliancy
** DONE No fixed int
CLOSED: [2020-10-28 Wed 01:40]
+** TODO export qwe="'123'"; echo $qwe <--- show squotes like bash
* Stuff to add
** DONE builtins to pipes
** DONE Proper ~ subst
-** TODO SIGNAL
+** DONE SIGNAL
+ CLOSED: [2020-12-07 Mon 15:31]
** DONE && ||
** DONE .minishellrc
** DONE .minishell_history
** DONE history builtin
-** TODO $_
+** DONE $_
+ CLOSED: [2020-12-11 Fri 18:37]
** TODO Parse error before p_line for scripts
** DONE Norm [2/2]
CLOSED: [2020-10-03 Sat 14:46]
@@ -81,15 +84,19 @@
CLOSED: [2020-09-28 Mon 19:30]
+ [X] base
+ [X] <<- to skip tabs
-** TODO if else [0/4]
+** CANCELLED if else [0/4]
+ CLOSED: [2020-12-07 Mon 15:32]
+ [ ] if - fi
+ [ ] else
+ [ ] else
+ [ ] elif
+ [ ] while - do - done
+ [ ] for
-** TODO Termcaps
+ CLOSED: [2020-12-07 Mon 15:31]
+** DONE Termcaps
+ CLOSED: [2020-12-07 Mon 15:31]
** TODO -i option (alias and history in scripts, etc)
-** TODO Simple arithmetics $((a + 1))
-** CANCELED &
+** CANCELLED Simple arithmetics $((a + 1))
+ CLOSED: [2020-12-11 Fri 16:01]
+** CANCELLED &
CLOSED: [2020-10-21 Wed 16:41]
diff --git a/src/b_alias.c b/src/b_alias.c
index c94a6db..c0eed23 100644
--- a/src/b_alias.c
+++ b/src/b_alias.c
@@ -25,6 +25,7 @@
#include "s_struct.h"
#include "u_alias.h"
#include "u_utils.h"
+#include "u_vars.h"
static unsigned char b_print_arg_next(char *ptr,
const char arg[],
diff --git a/src/c_init.c b/src/c_init.c
index b2fd3d3..cea3cce 100644
--- a/src/c_init.c
+++ b/src/c_init.c
@@ -156,7 +156,7 @@ short
t_caps tcaps;
char nread[4];
- c_init_line(psx, &tcaps);
+ c_init_line(psx, &tcaps, msh);
tputs(tgetstr("cr", NULL), 1, ft_putchar);
m_prompt_psx(psx, msh);
ft_bzero(nread, 4);
diff --git a/src/c_input.c b/src/c_input.c
index 141538d..7848f93 100644
--- a/src/c_input.c
+++ b/src/c_input.c
@@ -23,17 +23,18 @@
#include "c_utils.h"
#include "m_prompt.h"
#include "m_loop.h"
+#include "u_vars.h"
short
- c_init_line(char psx, t_caps *tcaps)
+ c_init_line(char psx, t_caps *tcaps, t_msh *msh)
{
- char *term;
+ char term[4096];
signal(SIGINT, c_signal_int);
signal(SIGQUIT, c_signal_ign);
if (tcaps)
{
- term = getenv("TERM");
+ u_get_var_value(term, "$TERM", 4096, msh);
if (!tgetent(NULL, term))
return (-1);
c_set_term_raw(1);
diff --git a/src/c_input.h b/src/c_input.h
index f2ccba8..cd582eb 100644
--- a/src/c_input.h
+++ b/src/c_input.h
@@ -13,10 +13,13 @@
#ifndef C_INPUT_H
# define C_INPUT_H
+#include "c_init.h"
+#include "s_struct.h"
+
short c_back_slash(char **line, unsigned int plen, t_caps *tcaps);
unsigned int c_get_line_num(char *line, unsigned int cpos, unsigned int plen,
t_caps *tcaps);
-short c_init_line(char psx, t_caps *tcaps);
+short c_init_line(char psx, t_caps *tcaps, t_msh *msh);
unsigned short c_get_win_size(struct winsize *ws);
#endif
diff --git a/src/c_keys.c b/src/c_keys.c
index 93bb04b..3687a84 100644
--- a/src/c_keys.c
+++ b/src/c_keys.c
@@ -48,8 +48,8 @@ short
short
c_end_key(unsigned short size, unsigned int plen, t_caps *tcaps)
{
- unsigned short i;
- unsigned short j;
+ unsigned short i;
+ unsigned short j;
i = tcaps->cpos;
j = tcaps->lpos;
diff --git a/src/f_alloc.c b/src/f_alloc.c
index dc3f589..5f42f09 100644
--- a/src/f_alloc.c
+++ b/src/f_alloc.c
@@ -10,16 +10,24 @@
/* */
/* ************************************************************************** */
+#include <errno.h>
#include <libft.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#include <errno.h>
+#include <unistd.h>
#include "s_destroy.h"
#include "s_line.h"
#include "s_struct.h"
+void f_alloc(t_msh *msh)
+{
+ char tmp[255];
+
+ ft_strlcpy(tmp, msh->argv[0], 255);
+ ft_dprintf(STDERR_FILENO, "%s: %s\n", tmp, strerror(errno));
+}
+
void f_alloc_and_destroy_msh(t_msh *msh)
{
char tmp[255];
diff --git a/src/f_alloc.h b/src/f_alloc.h
index a520eeb..6a72088 100644
--- a/src/f_alloc.h
+++ b/src/f_alloc.h
@@ -15,6 +15,7 @@
# include "s_struct.h"
+void f_alloc(t_msh *msh);
void f_alloc_and_destroy_msh(t_msh *msh);
void f_alloc_and_clear_line(t_msh *msh);
diff --git a/src/m_funptr.c b/src/m_funptr.c
index 2ae09f6..bd2e777 100644
--- a/src/m_funptr.c
+++ b/src/m_funptr.c
@@ -19,7 +19,7 @@
#include "m_funptr.h"
#include "s_struct.h"
-void init_buptr(t_msh *msh)
+void m_init_buptr(t_msh *msh)
{
msh->bu_ptr[B_ID_ECHO] = b_echo;
msh->bu_ptr[B_ID_CD] = b_cd;
diff --git a/src/m_funptr.h b/src/m_funptr.h
index f5375b9..a1d5f69 100644
--- a/src/m_funptr.h
+++ b/src/m_funptr.h
@@ -15,6 +15,6 @@
# include "s_struct.h"
-void init_buptr(t_msh *msh);
+void m_init_buptr(t_msh *msh);
#endif
diff --git a/src/m_loop.c b/src/m_loop.c
index 6ec2a3b..1641d53 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -37,12 +37,15 @@
#include "u_utils.h"
#include "u_parse.h"
#include "u_vars.h"
+#include "u_vars_next.h"
static void m_parse_and_run_line(char line[],
unsigned char previf,
t_msh *msh)
{
t_line_block *ptr;
+ size_t i;
+ t_bool pipe;
p_line(line, msh);
ptr = msh->curr;
@@ -51,14 +54,24 @@ static void m_parse_and_run_line(char line[],
if ((previf == 0) || (previf == 1 && msh->ret == 0)
|| (previf == 2 && msh->ret != 0))
{
- if ((p_find_good_pipe(ptr->lblock) == TRUE
- && (s_split_pipes(ptr->lblock, msh)) == NULL)
- || (msh->com = s_com_new(ptr->lblock, msh)) == NULL)
+ pipe = p_find_good_pipe(ptr->lblock);
+ if ((pipe == TRUE && s_split_pipes(ptr->lblock, msh) == NULL)
+ || (msh->com = s_com_new(ptr->lblock, msh)) == NULL)
{
previf = ptr->nextif;
ptr = ptr->next;
continue ;
}
+ if (msh->com != NULL && pipe == FALSE)
+ {
+ i = 0;
+ while (msh->com->argv != NULL && msh->com->argv[i] != NULL)
+ i++;
+ if (msh->com->argv != NULL && msh->com->argv[0] != NULL)
+ u_subst_var_value("$_", msh->com->argv[i - 1], msh);
+ else
+ u_subst_var_value("$_", msh->com->bin, msh);
+ }
e_line_block(msh);
s_com_destroy(&msh->com);
s_lpipes_clear(&msh->pipes);
diff --git a/src/m_minishell.c b/src/m_minishell.c
index 987cfc0..afec0de 100644
--- a/src/m_minishell.c
+++ b/src/m_minishell.c
@@ -29,7 +29,7 @@ int main(int argc, char *const argv[], char *const envp[])
if ((msh = s_init_msh(argc, argv, envp)) == NULL)
{
- ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno));
+ ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno));
return (M_RET_ALLOC);
}
ret = m_argv(argc, argv, msh);
diff --git a/src/s_init.c b/src/s_init.c
index a572c32..1e86727 100644
--- a/src/s_init.c
+++ b/src/s_init.c
@@ -11,13 +11,13 @@
/* ************************************************************************** */
#include <libft.h>
-#include <stdlib.h>
-#include <unistd.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif
+#include <stdlib.h>
+#include <unistd.h>
#include "b_export_next.h"
#include "d_define.h"
@@ -108,21 +108,31 @@ t_msh *s_init_msh(int argc, char *const argv[], char *const envp[])
if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL)
return (NULL);
msh->envp = NULL;
+ (void)envp;
if ((msh->envp = s_dupenv(envp)) == NULL)
+ {
+ ft_memdel((void*)&msh);
return (NULL);
+ }
msh->argc = argc - 1;
msh->argv = (char**)argv;
msh->ret = 0;
- init_buptr(msh);
+ m_init_buptr(msh);
s_null_some(msh);
s_set_cwd(cwd, msh);
- if ((msh->cwd = ft_strdup(cwd)) == NULL)
+ if (cwd[0] != C_NUL && (msh->cwd = ft_strdup(cwd)) == NULL)
{
+ ft_delwords(msh->envp);
ft_memdel((void*)&msh);
return (NULL);
}
msh->env_fork_tmp[0][0] = '\0';
- s_inc_shlvl(msh);
+ if (s_inc_shlvl(msh) == 1)
+ {
+ ft_memdel((void*)&msh->cwd);
+ ft_memdel((void*)&msh);
+ return (NULL);
+ }
s_init_sqb_ref_one(msh);
s_init_sqb_ref_two(msh);
s_init_sqb_ref_thr(msh);
diff --git a/src/s_init_next.c b/src/s_init_next.c
index 9a98e9a..6ee17d4 100644
--- a/src/s_init_next.c
+++ b/src/s_init_next.c
@@ -20,15 +20,17 @@
#endif
#include <paths.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include "b_export_next.h"
#include "f_fail.h"
+#include "s_destroy.h"
#include "s_struct.h"
#include "u_vars.h"
#include "u_vars_next.h"
-void s_inc_shlvl(t_msh *msh)
+char s_inc_shlvl(t_msh *msh)
{
char fmt[8];
char tmp[255];
@@ -49,8 +51,12 @@ void s_inc_shlvl(t_msh *msh)
shlvl = (shlvl >= 999) ? 0 : shlvl;
shlvl = (shlvl < 0) ? 0 : shlvl + 1;
ft_itoa_s(str, shlvl);
- u_subst_var_value("$SHLVL", str, msh);
+ if (u_subst_var_value("$SHLVL", str, msh) == 1)
+ {
+ return (1);
+ }
}
+ return (0);
}
char **s_dupenv_del(char **nenvp, unsigned long i)
@@ -116,18 +122,26 @@ void s_set_cwd(char cwd[], t_msh *msh)
u_get_var_value(cwd, "$PWD", PATH_MAX, msh);
if (cwd[0] == C_NUL)
{
- getcwd(cwd, PATH_MAX);
- ft_sprintf(fmt, "%s=%s", "PWD", cwd);
- b_export_with_equals(fmt, msh);
+ if (getcwd(cwd, PATH_MAX) != NULL)
+ {
+ ft_sprintf(fmt, "%s=%s", "PWD", cwd);
+ b_export_with_equals(fmt, msh);
+ }
+ else
+ ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno));
return ;
}
if ((dir = opendir(cwd)) != NULL)
closedir(dir);
else if (errno == ENOENT)
{
- getcwd(cwd, PATH_MAX);
- ft_sprintf(fmt, "%s=%s", "PWD", cwd);
- b_export_with_equals(fmt, msh);
+ if (getcwd(cwd, PATH_MAX) != NULL)
+ {
+ ft_sprintf(fmt, "%s=%s", "PWD", cwd);
+ b_export_with_equals(fmt, msh);
+ }
+ else
+ ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno));
return ;
}
}
diff --git a/src/s_init_next.h b/src/s_init_next.h
index dbb6cd4..650e795 100644
--- a/src/s_init_next.h
+++ b/src/s_init_next.h
@@ -15,7 +15,7 @@
# include "s_struct.h"
-void s_inc_shlvl(t_msh *msh);
+char s_inc_shlvl(t_msh *msh);
char **s_dupenv_del(char **nenvp, unsigned long i);
char **s_dupenv(char *const envp[]);
void s_set_cwd(char cwd[], t_msh *msh);
diff --git a/src/u_vars_next.c b/src/u_vars_next.c
index 22288d2..11027cb 100644
--- a/src/u_vars_next.c
+++ b/src/u_vars_next.c
@@ -65,26 +65,29 @@ static t_bool u_get_custom_var_existance(const char varname[], t_msh *msh)
}
/*
-** void
+** SYNOPSIS
+** char
** u_subst_var_value(const char varname[], const char newval[], t_msh *msh);
**
** DESCRIPTION
-** The subst_var_value() changes the value of msh->envp
+** The u_subst_var_value() function changes the value of msh->envp
** variable varname[] with newval[]. If varname[] wasn't found
** in msh->envp, varname[] is searched in msh->vars. Otherwise
** varname[] is added to msh->vars.
+**
+** RETURN VALUES
+** The u_subst_var_value() function returns 0 upon success
+** and 1 in case of malloc(3) failure. When returning 1, the
+** entire environment is destroyed.
*/
-/*
-** TODO: Handle malloc fail
-*/
-
-void u_subst_var_value(const char varname[],
+char u_subst_var_value(const char varname[],
const char newval[],
t_msh *msh)
{
- char new_line_fmt[ARG_MAX];
+ size_t i;
long env_line;
+ char new_line_fmt[ARG_MAX];
if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1)
{
@@ -93,6 +96,12 @@ void u_subst_var_value(const char varname[],
if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) *
sizeof(char))) == NULL)
{
+ i = env_line;
+ while (msh->envp[++i] != NULL)
+ ft_memdel((void*)&msh->envp[i]);
+ ft_delwords(msh->envp);
+ msh->envp = NULL;
+ return (1);
}
(void)ft_strlcpy(msh->envp[env_line],
new_line_fmt, ft_strlen(new_line_fmt) + 1);
@@ -105,4 +114,5 @@ void u_subst_var_value(const char varname[],
{
lvars_add_front(&msh->vars, lvars_new(varname + 1, newval));
}
+ return (0);
}
diff --git a/src/u_vars_next.h b/src/u_vars_next.h
index cd802c7..2defb7b 100644
--- a/src/u_vars_next.h
+++ b/src/u_vars_next.h
@@ -15,7 +15,7 @@
# include "s_struct.h"
-void u_subst_var_value(const char varname[],
+char u_subst_var_value(const char varname[],
const char newval[],
t_msh *msh);