Browse Source

fixing everything

master
Michael Ea 3 years ago
parent
commit
8aa16c2d57
  1. 20
      built-in.c
  2. 16
      caller.c
  3. 18
      lexer.c
  4. 6
      lexer_2.c
  5. 10
      minishell.c
  6. 6
      minishell.h
  7. 10
      parser.c

20
built-in.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* built-in.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */
/* Updated: 2022/05/16 11:55:41 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:38:18 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,16 +41,16 @@ int ft_echo(t_command *cmd)
fd_out = 1;
no_nl = 0;
i = 1;
if (cmd->argv[1])
while (cmd->argv[i])
{
if (ft_strncmp(cmd->argv[1], "-n", 3) == 0 && i++)
no_nl = 1;
while (cmd->argv[i])
if (ft_strncmp(cmd->argv[i], "-n", 2) == 0 && i++)
{
ft_putstr_fd(cmd->argv[i], fd_out);
if (cmd->argv[++i])
ft_putstr_fd(" ", fd_out);
no_nl = 1;
continue ;
}
ft_putstr_fd(cmd->argv[i], fd_out);
if (cmd->argv[++i])
ft_putstr_fd(" ", fd_out);
}
if (!no_nl)
ft_putstr_fd("\n", fd_out);
@ -85,7 +85,7 @@ int ft_cd(t_command *cmd)
return (file_error(cmd->argv[0], cmd->argv[1],
"Not a directory\n"));
chdir(cmd->argv[1]);
return (1);
return (0);
}
else
chdir(getenv("HOME"));

16
caller.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* caller.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/17 09:58:36 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:45:59 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -107,18 +107,20 @@ int caller(t_datas *datas, t_command *cmd)
{
int status;
create_pipe(cmd);
cmd->pid = call(datas, cmd);
if (cmd->ope != PIPE && is_builtin(cmd->argv[0]))
datas->exit_code = call_builtin(datas, cmd);
else
cmd->pid = (create_pipe(cmd), call(datas, cmd));
if (cmd->fd[0])
close(cmd->fd[0]);
if (cmd->fd[1])
close(cmd->fd[1]);
if (cmd->ope == PIPE)
caller(datas, cmd->next);
waitpid(cmd->pid, &status, 0);
if (cmd->ope != PIPE)
else
{
datas->exit_code = handle_status(datas, status);
if (!is_builtin(cmd->argv[0]) && waitpid(cmd->pid, &status, 0))
datas->exit_code = handle_status(datas, status);
while (cmd->next && !((cmd->ope == AND && !datas->exit_code) \
|| (cmd->ope == OR && datas->exit_code)))
cmd = cmd->next;

18
lexer.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* lexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/16 12:53:40 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:58:09 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,7 +24,7 @@ void tmp_dealer(t_datas *datas, t_lexer *lex, char *line, char *tmp)
if (check_state(lex, &line))
continue ;
if (lex->state != S_QUOTE_ST && lex->deep == 0 && *line == '$'
&& (line[1] != '\0' && !isspace(line[1])))
&& (line[1] != '\0' && !ft_isspace(line[1])))
{
tmp_i = replace_var(datas, &line, tmp, tmp_i);
lex->empty = 1;
@ -35,6 +35,8 @@ void tmp_dealer(t_datas *datas, t_lexer *lex, char *line, char *tmp)
&& (*line == '"' || *line == '\'')))
tmp[tmp_i++] = *(line++);
}
if (!*tmp && (*(line - 1) == '"' || *(line - 1) == '\''))
lex->empty = 1;
}
t_token *lexer(t_datas *datas, char *line)
@ -52,7 +54,10 @@ t_token *lexer(t_datas *datas, char *line)
else
create_token(lex, tmp);
free(tmp);
ret = lex->tokens;
if (lex->state != ROOT_ST)
ret = NULL;
else
ret = lex->tokens;
free(lex);
return (ret);
}
@ -126,16 +131,15 @@ static int check_register(t_lexer *lex, char **line, char *tmp)
return (create_token(lex, tmp));
if (check_ope(lex, line, tmp))
return (1);
if (**line == '(' || spaces)
if ((**line == '(' && ++lex->deep) || spaces)
{
if (**line == '(')
lex->deep++;
if (*tmp || lex->empty)
{
if (lex->type == WORD && lex->wc)
return (create_wc(lex, tmp));
return (create_token(lex, tmp));
}
return (1);
}
return (0);
}

6
lexer_2.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* lexer_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 07:53:17 by narnaud #+# #+# */
/* Updated: 2022/05/17 08:36:11 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:41:41 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -81,7 +81,7 @@ char *get_var_value(t_datas *datas, char *line, int name_len)
if (value)
free(env);
else
value = strdup("");
value = ft_strdup("");
return (value);
}

10
minishell.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/16 08:16:48 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:33:20 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -64,10 +64,10 @@ void prompt(t_datas *datas, char *line)
if (is_empty(line))
return ;
cmd = parser(datas, lexer(datas, line), NULL);
if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0]))
call_builtin(datas, cmd);
else
if (cmd)
caller(datas, cmd);
else
ft_putstr_fd("Minishell: syntax error\n", 2);
free_cmd(cmd);
add_history(line);
}

6
minishell.h

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/17 09:11:13 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:55:14 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,6 +50,8 @@ typedef struct s_datas
int silent;
} t_datas;
void free_cmd(t_command *cmd);
// ---------------------------------- Utils.c
int is_empty(char *line);
int handle_status(t_datas *datas, int status);

10
parser.c

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */
/* Updated: 2022/05/16 08:17:31 by narnaud ### ########.fr */
/* Updated: 2022/05/17 15:52:40 by mea ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,9 +18,11 @@ static t_token *parse_cmd(t_datas *datas, t_token *tok, t_command *cmd);
t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
{
t_command *cmd;
int syntax_err;
syntax_err = 0;
if (!tok)
return (NULL);
syntax_err = 1;
cmd = ft_calloc(1, sizeof(*cmd));
if (prev)
cmd->prev = prev;
@ -36,6 +38,8 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
free(tok->value);
free(tok);
}
if (free_cmd(cmd), syntax_err)
return (NULL);
return (cmd);
}

Loading…
Cancel
Save