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.
92 lines
2.2 KiB
92 lines
2.2 KiB
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* minishell.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
|
|
/* Updated: 2022/05/06 12:03:13 by narnaud ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
# include "minishell.h"
|
|
|
|
void termios(int ctl)
|
|
{
|
|
struct termios termios_p;
|
|
int tty;
|
|
|
|
tty = ttyslot();
|
|
if (tcgetattr(tty, &termios_p) < 0)
|
|
exit ((perror("stdin"), EXIT_FAILURE));
|
|
if (ctl)
|
|
{
|
|
termios_p.c_lflag |= ECHOCTL;
|
|
signal(SIGINT, nothing);
|
|
signal(SIGQUIT, nothing);
|
|
}
|
|
else
|
|
{
|
|
termios_p.c_lflag &= ~(ECHOCTL);
|
|
signal(SIGINT, sigs_handler);
|
|
signal(SIGQUIT, sigs_handler);
|
|
}
|
|
termios_p.c_cc[VQUIT] = ctl * 28;
|
|
tcsetattr(tty, TCSANOW, &termios_p);
|
|
}
|
|
|
|
void free_cmd(t_command *cmd)
|
|
{
|
|
int i;
|
|
|
|
if (!cmd)
|
|
return ;
|
|
i = 0;
|
|
while (cmd->argv[i])
|
|
{
|
|
free(cmd->argv[i]);
|
|
i++;
|
|
}
|
|
free(cmd->argv);
|
|
free_cmd(cmd->next);
|
|
free(cmd);
|
|
}
|
|
|
|
void prompt(t_datas *datas)
|
|
{
|
|
char *line;
|
|
t_command *cmd;
|
|
|
|
termios(0);
|
|
line = readline("$ ");
|
|
termios(1);
|
|
if (line == NULL)
|
|
halt(datas, EXIT_FAILURE);
|
|
if (is_empty(line))
|
|
return ;
|
|
cmd = parser(datas, lexer(datas, line), NULL);
|
|
if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0]))
|
|
builtin_call(datas, cmd);
|
|
else
|
|
piper(datas, cmd);
|
|
free_cmd(cmd);
|
|
add_history(line);
|
|
}
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
{
|
|
t_datas datas;
|
|
int i;
|
|
|
|
i = 0;
|
|
datas.envp = ft_calloc(ENVP_LIMIT, sizeof(char *));
|
|
while (*envp)
|
|
datas.envp[i++] = ft_strdup(*(envp++));
|
|
datas.exit_code = 0;
|
|
if (argc < 2)
|
|
(void)argv;
|
|
while (1)
|
|
prompt(&datas);
|
|
halt(&datas, EXIT_SUCCESS);
|
|
}
|
|
|