You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.3 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
3 years ago
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/24 08:23:21 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
3 years ago
#include "minishell.h"
3 years ago
void termios(int ctl)
{
3 years ago
struct termios termios_p;
int tty;
tty = ttyslot();
tcgetattr(tty, &termios_p);
if (ctl)
{
3 years ago
termios_p.c_lflag |= ECHOCTL;
signal(SIGINT, nothing);
signal(SIGQUIT, nothing);
}
else
{
3 years ago
termios_p.c_lflag &= ~(ECHOCTL);
signal(SIGINT, sigs_handler);
signal(SIGQUIT, sigs_handler);
}
3 years ago
tcsetattr(tty, TCSANOW, &termios_p);
}
3 years ago
void free_cmd(t_command *cmd)
3 years ago
{
3 years ago
int i;
3 years ago
3 years ago
if (!cmd)
return ;
3 years ago
i = 0;
3 years ago
while (cmd->argv[i])
3 years ago
{
3 years ago
free(cmd->argv[i]);
i++;
3 years ago
}
3 years ago
free(cmd->argv);
free_cmd(cmd->next);
free(cmd);
3 years ago
}
void prompt(t_datas *datas, char *line)
3 years ago
{
3 years ago
t_command *cmd;
3 years ago
termios(0);
if (!line)
line = readline("$ ");
3 years ago
termios(1);
if (line == NULL)
halt(datas, EXIT_SUCCESS, datas->silent);
3 years ago
if (is_empty(line))
return ;
cmd = parser(datas, lexer(datas, line), NULL);
if (cmd)
caller(datas, cmd);
else
ft_putstr_fd("Minishell: syntax error\n", 2);
3 years ago
free_cmd(cmd);
add_history(line);
}
int main(int argc, char **argv, char **envp)
{
3 years ago
static t_datas datas;
static int i = 0;
datas.silent = (argc > 2 && !ft_strncmp(argv[1], "-c", 3));
3 years ago
datas.envp = ft_calloc(ENVP_MAX_SIZE, sizeof(char *));
while (*envp)
datas.envp[i++] = ft_strdup(*(envp++));
if (!*get_var_value(&datas, "MINISH", 7))
3 years ago
datas.envp[i] = ft_strjoin_with("MINISH=", "/minishell",
getcwd(NULL, 0));
datas.exit_code = 0;
if (datas.silent)
prompt(&datas, argv[2]);
else
while (1)
prompt(&datas, NULL);
halt(&datas, EXIT_SUCCESS, datas.silent);
3 years ago
}