srcs files moved
This commit is contained in:
45
srcs/misc/error_wrappers.c
Normal file
45
srcs/misc/error_wrappers.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error_wrappers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/05 16:26:48 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int shell_error(char *s1, char *s2, char *s3, int ret_val)
|
||||
{
|
||||
char *prefix;
|
||||
|
||||
prefix = "minishell: ";
|
||||
write(STDERR_FILENO, prefix, ft_strlen(prefix));
|
||||
if (s1)
|
||||
write(STDERR_FILENO, s1, ft_strlen(s1));
|
||||
if (s2)
|
||||
write(STDERR_FILENO, s2, ft_strlen(s2));
|
||||
if (s3)
|
||||
write(STDERR_FILENO, s3, ft_strlen(s3));
|
||||
write(STDERR_FILENO, "\n", 1);
|
||||
return (ret_val);
|
||||
}
|
||||
|
||||
int shell_perror(char *s1, char *s2, char *s3, int ret_val)
|
||||
{
|
||||
char *prefix;
|
||||
|
||||
prefix = "minishell: ";
|
||||
write(STDERR_FILENO, prefix, ft_strlen(prefix));
|
||||
if (s1)
|
||||
write(STDERR_FILENO, s1, ft_strlen(s1));
|
||||
if (s2)
|
||||
write(STDERR_FILENO, s2, ft_strlen(s2));
|
||||
if (s3)
|
||||
write(STDERR_FILENO, s3, ft_strlen(s3));
|
||||
perror(NULL);
|
||||
return (ret_val);
|
||||
}
|
||||
83
srcs/misc/free.c
Normal file
83
srcs/misc/free.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/02 00:42:33 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int exit_free(t_all *c, int exit_status)
|
||||
{
|
||||
free(c->prompt_base);
|
||||
free(c->prompt);
|
||||
ft_lstclear((t_list **)&c->token_list, free);
|
||||
ft_free_2d_arr(environ);
|
||||
ft_free_2d_arr(c->path);
|
||||
free_pipeline(&c->pipeline);
|
||||
//if (c->termios_changed)
|
||||
// tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
|
||||
rl_clear_history();
|
||||
close_stdio();
|
||||
exit(exit_status);
|
||||
}
|
||||
|
||||
void free_pipeline(t_cmd **pipeline_ptr[])
|
||||
{
|
||||
int i;
|
||||
t_cmd **pipeline;
|
||||
|
||||
pipeline = *pipeline_ptr;
|
||||
if (!pipeline)
|
||||
return ;
|
||||
close_pipeline_fd(pipeline);
|
||||
i = 0;
|
||||
while (pipeline[i])
|
||||
{
|
||||
ft_free_2d_arr(pipeline[i]->argv);
|
||||
free(pipeline[i]->path);
|
||||
i++;
|
||||
}
|
||||
ft_free_2d_arr(pipeline);
|
||||
*pipeline_ptr = NULL;
|
||||
}
|
||||
|
||||
void close_pipeline_fd(t_cmd *pipeline[])
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!pipeline)
|
||||
return ;
|
||||
i = 0;
|
||||
while (pipeline[i])
|
||||
{
|
||||
if (pipeline[i]->fd_in != STDIN_FILENO && pipeline[i]->fd_in > 0)
|
||||
{
|
||||
if (close(pipeline[i]->fd_in) == -1)
|
||||
perror("close()");
|
||||
pipeline[i]->fd_in = 0;
|
||||
}
|
||||
if (pipeline[i]->fd_out != STDOUT_FILENO && pipeline[i]->fd_out > 0)
|
||||
{
|
||||
if (close(pipeline[i]->fd_out) == -1)
|
||||
perror("close()");
|
||||
pipeline[i]->fd_out = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void close_stdio(void)
|
||||
{
|
||||
if (close(STDIN_FILENO) == -1)
|
||||
perror("close()");
|
||||
if (close(STDOUT_FILENO) == -1)
|
||||
perror("close()");
|
||||
if (close(STDERR_FILENO) == -1)
|
||||
perror("close()");
|
||||
}
|
||||
115
srcs/misc/init.c
Normal file
115
srcs/misc/init.c
Normal file
@@ -0,0 +1,115 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/08 21:54:36 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static char *init_prompt_base(void);
|
||||
static int init_readline_hook(void);
|
||||
static int shlvl_init(void);
|
||||
|
||||
int init(t_all *c)
|
||||
{
|
||||
ft_bzero(c, sizeof (*c));
|
||||
rl_outstream = stderr;
|
||||
rl_startup_hook = init_readline_hook;
|
||||
readline(NULL);
|
||||
rl_startup_hook = NULL;
|
||||
environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
|
||||
if (!environ)
|
||||
return (ft_reti_perror(0, "ft_dup_2d_arr(environ)"));
|
||||
c->path = retrieve_path();
|
||||
if (!shlvl_init())
|
||||
return (ft_reti_perror(0, "shlvl_init()"));
|
||||
c->prompt_base = init_prompt_base();
|
||||
if (!c->prompt_base)
|
||||
return (ft_reti_perror(0, "init_prompt_base()"));
|
||||
c->prompt = init_prompt(c->prompt_base);
|
||||
if (!c->prompt)
|
||||
return (ft_reti_perror(0, "init_prompt()"));
|
||||
set_signals_behaviour();
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int shlvl_init(void)
|
||||
{
|
||||
char *tmp;
|
||||
int ret;
|
||||
|
||||
tmp = getenv("SHLVL");
|
||||
if (tmp && ft_isdigit_str(tmp))
|
||||
{
|
||||
tmp = ft_itoa(ft_atoi(tmp) + 1);
|
||||
if (!tmp)
|
||||
return (0);
|
||||
tmp = ft_strjoinfree_s2("SHLVL=", tmp);
|
||||
if (!tmp)
|
||||
return (0);
|
||||
ret = export_var(tmp);
|
||||
free(tmp);
|
||||
}
|
||||
else
|
||||
ret = export_var("SHLVL=1");
|
||||
if (ret == -1)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static char *init_prompt_base(void)
|
||||
{
|
||||
char *prompt_base;
|
||||
char *tmp;
|
||||
|
||||
tmp = getenv("USER");
|
||||
if (!tmp)
|
||||
tmp = getenv("LOGNAME");
|
||||
if (!tmp)
|
||||
tmp = U_DEFAULT_USER;
|
||||
prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp);
|
||||
if (!prompt_base)
|
||||
return (NULL);
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
|
||||
if (!prompt_base)
|
||||
return (NULL);
|
||||
tmp = getenv("NAME");
|
||||
if (!tmp)
|
||||
tmp = U_DEFAULT_NAME;
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
|
||||
if (!prompt_base)
|
||||
return (NULL);
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
|
||||
if (!prompt_base)
|
||||
return (NULL);
|
||||
return (prompt_base);
|
||||
}
|
||||
|
||||
char *init_prompt(char *prompt_base)
|
||||
{
|
||||
char *prompt;
|
||||
|
||||
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
|
||||
if (!prompt)
|
||||
return (NULL);
|
||||
prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END);
|
||||
if (!prompt)
|
||||
return (NULL);
|
||||
return (prompt);
|
||||
}
|
||||
|
||||
/*
|
||||
** set rl_startup_hook with this, for init COLUMNS and LINES variables
|
||||
** and prevent leak/double_free with **environ
|
||||
*/
|
||||
static int init_readline_hook(void)
|
||||
{
|
||||
rl_done = 1;
|
||||
return (0);
|
||||
}
|
||||
37
srcs/misc/last_exit_status.c
Normal file
37
srcs/misc/last_exit_status.c
Normal file
@@ -0,0 +1,37 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* last_exit_status.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/26 19:02:27 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/26 20:48:11 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int set_last_exit_status(int new_value)
|
||||
{
|
||||
static int last_exit_status = 0;
|
||||
|
||||
if (new_value >= 0)
|
||||
last_exit_status = new_value;
|
||||
return (last_exit_status);
|
||||
}
|
||||
|
||||
int get_last_exit_status(void)
|
||||
{
|
||||
return (set_last_exit_status(-1));
|
||||
}
|
||||
|
||||
/* void ALT_set_last_exit_status(int new_value, int *set_last_exit_status_ptr)
|
||||
{
|
||||
static int *last_exit_status_ptr = NULL;
|
||||
|
||||
if (set_last_exit_status_ptr)
|
||||
last_exit_status_ptr = set_last_exit_status_ptr;
|
||||
else if (new_value >= 0)
|
||||
*last_exit_status_ptr = new_value;
|
||||
} */
|
||||
27
srcs/misc/retrieve_path.c
Normal file
27
srcs/misc/retrieve_path.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* retrieve_path.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/08 06:27:23 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char **retrieve_path(void)
|
||||
{
|
||||
char *path;
|
||||
char **path_split;
|
||||
|
||||
path = getenv("PATH");
|
||||
if (!path)
|
||||
return (ft_retp_print(NULL, "minishell: Warning, $PATH not set\n", 2));
|
||||
path_split = ft_split(path, ':');
|
||||
if (!path_split)
|
||||
return (ft_retp_perror(NULL, "retrieve_path()"));
|
||||
return (path_split);
|
||||
}
|
||||
38
srcs/misc/signals.c
Normal file
38
srcs/misc/signals.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* signals.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/26 21:45:22 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void sigint_handler_interactive(int signum)
|
||||
{
|
||||
write(1, "\n", 1);
|
||||
rl_on_new_line();
|
||||
rl_replace_line("", 1);
|
||||
rl_redisplay();
|
||||
set_last_exit_status(EXIT_SIGNAL + signum);
|
||||
}
|
||||
|
||||
void sigint_handler_heredoc(int signum)
|
||||
{
|
||||
(void)signum;
|
||||
rl_done = 1;
|
||||
g_switch_heredoc_sigint = 1;
|
||||
}
|
||||
|
||||
void set_signals_behaviour(void)
|
||||
{
|
||||
struct sigaction signal_behaviour;
|
||||
|
||||
ft_bzero(&signal_behaviour, sizeof signal_behaviour);
|
||||
signal_behaviour.sa_handler = SIG_IGN;
|
||||
sigaction(SIGQUIT, &signal_behaviour, NULL);
|
||||
}
|
||||
76
srcs/misc/terminal.c
Normal file
76
srcs/misc/terminal.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* terminal.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/27 00:10:04 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/30 14:17:16 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
#define CTRL_C 03
|
||||
#define CTRL_D 04
|
||||
#define CTRL_BACKSLASH 034
|
||||
|
||||
int set_terminal_attributes(struct termios *ori_termios,
|
||||
struct termios *interactive_termios,
|
||||
int *termios_changed)
|
||||
{
|
||||
tcgetattr(STDIN_FILENO, ori_termios);
|
||||
*interactive_termios = *ori_termios;
|
||||
|
||||
interactive_termios->c_cc[VINTR] = CTRL_D;
|
||||
interactive_termios->c_cc[VEOF] = CTRL_C;
|
||||
|
||||
//interactive_termios->c_cc[VQUIT] = CTRL_C;
|
||||
//interactive_termios->c_cc[VEOF] = CTRL_BACKSLASH;
|
||||
|
||||
//interactive_termios->c_cc[VEOL] = CTRL_C;
|
||||
|
||||
*termios_changed = 1;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, interactive_termios);
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
//printf("STDIN_FILENO = %s\n ", ttyname(STDIN_FILENO));
|
||||
//printf("STDOUT_FILENO = %s\n ", ttyname(STDOUT_FILENO));
|
||||
//printf("STDERR_FILENO = %s\n ", ttyname(STDERR_FILENO));
|
||||
//ft_putendl_fd(ttyname(STDIN_FILENO), 1);
|
||||
//ft_putendl_fd(ttyname(STDOUT_FILENO), 1);
|
||||
//ft_putendl_fd(ttyname(STDERR_FILENO), 1);
|
||||
// ft_printf("BEFORE\n");
|
||||
// ft_printf("i_io.c_cc[VEOF] = %i\ni_termios.c_cc[VINTR] = %i\n", (*interactive_termios)->c_cc[VEOF], (*interactive_termios)->c_cc[VINTR]);
|
||||
// ft_printf("o_io.c_cc[VEOF] = %i\no_termios.c_cc[VINTR] = %i\n", (*ori_termios)->c_cc[VEOF], (*ori_termios)->c_cc[VINTR]);
|
||||
// ft_printf("AFTER\n");
|
||||
// ft_printf("i_io.c_cc[VEOF] = %i\ni_termios.c_cc[VINTR] = %i\n", (*interactive_termios)->c_cc[VEOF], (*interactive_termios)->c_cc[VINTR]);
|
||||
// ft_printf("o_io.c_cc[VEOF] = %i\no_termios.c_cc[VINTR] = %i\n", (*ori_termios)->c_cc[VEOF], (*ori_termios)->c_cc[VINTR]);
|
||||
|
||||
void wip_test()
|
||||
{
|
||||
char term_desc[2048];
|
||||
char *term_type;
|
||||
int term_width;
|
||||
int term_height;
|
||||
int ret;
|
||||
|
||||
term_type = getenv("TERM");
|
||||
if (term_type == 0)
|
||||
ft_putstr_fd("Specify a terminal type with `setenv TERM <yourtype>'.\n", 2);
|
||||
ret = tgetent(term_desc, term_type);
|
||||
if (ret < 0)
|
||||
ft_putstr_fd("Could not access the termcap data base.\n", 2);
|
||||
if (ret == 0)
|
||||
ft_putstr_fd("Terminal type `%s' is not defined.\n", 2);
|
||||
term_height = tgetnum ("li");
|
||||
term_width = tgetnum ("co");
|
||||
/* Extract information that termcap functions use. */
|
||||
/* temp = tgetstr ("pc", BUFFADDR);
|
||||
PC = temp ? *temp : 0;
|
||||
BC = tgetstr ("le", BUFFADDR);
|
||||
UP = tgetstr ("up", BUFFADDR); */
|
||||
}
|
||||
Reference in New Issue
Block a user