Browse Source

-new: handle 0

master
narnaud 3 years ago
parent
commit
5e9ccbfa34
  1. 39
      lexer.c
  2. 37
      minishell.c
  3. 4
      minishell.h

39
lexer.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/05 08:43:52 by narnaud ### ########.fr */
/* Updated: 2022/05/05 11:47:15 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -85,25 +85,42 @@ int check_register(t_lexer *lex, char **line, char *tmp)
return (create_token(lex, tmp));
}
int replace_var(char **line, char *tmp, int tmp_i)
char *ft_strreplace(char *dest, char *src)
{
int len;
int len;
len = ft_strlen(src);
while (*src)
*(dest++) = *(src++);
return (dest);
}
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
int i;
char *var_name;
char *value;
// TODO : free split and var_name
i = 1;
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++;
var_name = ft_substr(*line, 1, i - 1);
value = getenv(var_name);
len = ft_strlen(value);
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
}
else
{
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++;
var_name = ft_substr(*line, 1, i - 1);
value = getenv(var_name);
name_len = ft_strlen(var_name) + 1;
}
i = 0;
while (i < len)
while (value[i])
tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0;
*line += ft_max(ft_strlen(var_name) + 1, i);
*line += ft_max(name_len, i);
return (tmp_i);
}

37
minishell.c

@ -6,12 +6,28 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/05 11:10:24 by narnaud ### ########.fr */
/* Updated: 2022/05/05 11:49:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
# include "minishell.h"
void handle_status(t_datas *datas, int status)
{
if (WIFSIGNALED(status))
{
datas->exit_code = WTERMSIG(status);
if (datas->exit_code == 3)
printf("Quit: 3\n");
if (datas->exit_code == 9)
printf("Kill: 15\n");
if (datas->exit_code == 15)
printf("Terminated: 15\n");
}
else
datas->exit_code = WEXITSTATUS(status);
}
int caller(t_datas *datas, t_command *cmd)
{
pid_t pid;
@ -40,18 +56,9 @@ int caller(t_datas *datas, t_command *cmd)
if (cmd->fd[1])
close(cmd->fd[1]);
if (cmd->next)
return (caller(datas, cmd->next));
caller(datas, cmd->next);
waitpid(pid, &status, 0);
if (WIFSIGNALED(status))
{
datas->exit_code = WTERMSIG(status);
if (datas->exit_code == 3)
printf("Quit: 3\n");
if (datas->exit_code == 9)
printf("Kill: 15\n");
if (datas->exit_code == 15)
printf("Terminated: 15\n");
}
handle_status(datas, status);
if (cmd->fd[0])
close(cmd->fd[0]);
return (1);
@ -86,7 +93,7 @@ t_command *parser(t_token *tok, t_command *prev)
return(cmd);
}
t_token *lexer(char *line)
t_token *lexer(t_datas *datas, char *line)
{
t_lexer *lex;
char *tmp;
@ -101,7 +108,7 @@ t_token *lexer(char *line)
if (check_state(lex, &line))
continue;
if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(&line, tmp, tmp_i);
tmp_i = replace_var(datas, &line, tmp, tmp_i);
if (check_register(lex, &line, tmp))
{
tmp_i = 0;
@ -134,7 +141,7 @@ int main(int argc, char **argv, char **envp)
halt(EXIT_FAILURE);
if (is_empty(line))
continue ;
caller(&datas, parser(lexer(line), NULL));
caller(&datas, parser(lexer(&datas, line), NULL));
add_history(line);
}
clear_history();

4
minishell.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/05 09:38:23 by narnaud ### ########.fr */
/* Updated: 2022/05/05 11:35:09 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -111,7 +111,7 @@ typedef struct s_lex
int create_token(t_lexer *lex, char str[]);
int check_register(t_lexer *lex, char **line, char *tmp);
int replace_var(char **line, char *tmp, int tmp_i);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i);
int check_state(t_lexer *lex, char **line);
#endif

Loading…
Cancel
Save