Skip to content

Commit

Permalink
fixed bug in lexer that would cause it to create bad nodes for spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Jul 17, 2024
1 parent 3f62f47 commit 615cee0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
4 changes: 3 additions & 1 deletion include/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 16:53:36 by jteissie #+# #+# */
/* Updated: 2024/07/17 14:25:18 by jteissie ### ########.fr */
/* Updated: 2024/07/17 18:55:34 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,10 +23,12 @@
# define EXIT_FAILURE 1
# define CHILD 0
# define PARENT 1
# define BUILT_IN 2

void trash(char **array);
void handle_error(char *message, int code);
void execute_cmd(char *cmd, char **env, t_parser *data);
int count_commands(t_parser *data);

int process_files(t_lex_parser *table);
int get_redirections(t_lex_parser *roaming, char *redirection[]);
Expand Down
34 changes: 16 additions & 18 deletions src/execution/execute_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:19:59 by jteissie #+# #+# */
/* Updated: 2024/07/17 14:28:01 by jteissie ### ########.fr */
/* Updated: 2024/07/17 18:56:55 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,43 +35,41 @@ void wait_for_children(int index)
}
}

int count_commands(t_parser *data)
int filter_cmd(t_lex_parser *roaming, char **envp, int std_fds[], t_parser *data)
{
t_lex_parser *roaming;
t_cmd_table *cmd_table;
int cmd_count;

roaming = data->node;
cmd_count = 0;
while (roaming)
cmd_count = count_commands(data);
cmd_table = roaming->table;
if (cmd_count == 1 && is_builtin(cmd_table->cmd, 0) == TRUE)
{
if (roaming->type == TK_PARS_CMD)
{
cmd_count++;
}
roaming = roaming->next;
execute_builtin(cmd_table->cmd, envp, data, PARENT);
return (BUILT_IN);
}
return (cmd_count);
else if (process_command(roaming, envp, data, std_fds) == PANIC)
return (PANIC);
return (SUCCESS);
}

int execute_commands(t_parser *data, char **envp, int std_fds[])
{
int cmd_count;
int index;
int status;
t_lex_parser *roaming;
t_cmd_table *cmd_table;

cmd_count = count_commands(data);
index = cmd_count;
roaming = data->node;
status = SUCCESS;
while (roaming && index)
{
if (roaming->type == TK_PARS_CMD)
{
cmd_table = roaming->table;
if (cmd_count == 1 && is_builtin(cmd_table->cmd, 0) == TRUE)
execute_builtin(cmd_table->cmd, envp, data, PARENT);
else if (process_command(roaming, envp, data, std_fds) == PANIC)
return (PANIC);
status = filter_cmd(roaming, envp, std_fds, data);
if (status == BUILT_IN || status == PANIC)
break ;
index--;
}
roaming = roaming->next;
Expand Down
20 changes: 19 additions & 1 deletion src/execution/execution_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 18:14:25 by jteissie #+# #+# */
/* Updated: 2024/07/17 14:24:46 by jteissie ### ########.fr */
/* Updated: 2024/07/17 18:55:04 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,6 +34,24 @@ void trash(char **array)
free(array);
}

int count_commands(t_parser *data)
{
t_lex_parser *roaming;
int cmd_count;

roaming = data->node;
cmd_count = 0;
while (roaming)
{
if (roaming->type == TK_PARS_CMD)
{
cmd_count++;
}
roaming = roaming->next;
}
return (cmd_count);
}

void check_pipes(t_lex_parser *table, int pipe_status[])
{
t_lex_parser *roaming;
Expand Down
6 changes: 3 additions & 3 deletions src/lexer/lex_executables.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* lex_executables.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <bthomas@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 18:08:45 by bthomas #+# #+# */
/* Updated: 2024/07/16 12:39:03 by bthomas ### ########.fr */
/* Updated: 2024/07/17 19:17:47 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -42,7 +42,7 @@ char *get_exec_path(char *input, size_t start_idx)

exec_path = NULL;
cmd = get_substr(input, start_idx);
if (!cmd)
if (!cmd || !cmd[0])
return (NULL);
path = getenv("PATH");
if (!path)
Expand Down
15 changes: 12 additions & 3 deletions src/lexer/lex_retrieve_tk1.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* lex_retrieve_tk1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <bthomas@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 08:04:48 by bento #+# #+# */
/* Updated: 2024/07/11 13:02:09 by bthomas ### ########.fr */
/* Updated: 2024/07/17 19:27:53 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,7 +38,16 @@ t_token *get_string_tk(t_data *data, char *input, size_t start_idx)

t_token *get_word_tk(t_data *data, char *input, size_t start_idx)
{
return (get_token(data, get_substr(input, start_idx), NULL, TK_WORD));
char *word;

word = get_substr(input, start_idx);
if (!word || !word[0])
{
if (word)
free(word);
return (NULL);
}
return (get_token(data, word, NULL, TK_WORD));
}

t_token *get_flag_tk(t_data *data, char *input, size_t start_idx)
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/validate_tokens.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* validate_tokens.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <bthomas@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 12:26:40 by bthomas #+# #+# */
/* Updated: 2024/07/16 11:55:33 by bthomas ### ########.fr */
/* Updated: 2024/07/17 19:24:34 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down

0 comments on commit 615cee0

Please sign in to comment.