Skip to content

Commit

Permalink
feat: Add E_WILDCARD to expander_op bit enum
Browse files Browse the repository at this point in the history
Don't do wildcard expansion in heredoc content or end.

Known issues:
- #295 Word splitting and filename expansion are not performed when assigning values to env variables
- #296 In --posix mode, words after redirection operators do not undergo filename expansion if shell is not interactive
  • Loading branch information
itislu committed Mar 31, 2024
1 parent fcb3c12 commit cc300a9
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 12 deletions.
7 changes: 4 additions & 3 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ typedef enum e_is_open_pair_op

typedef enum e_expander_op
{
E_EXPAND = 0b001,
E_SPLIT_WORDS = 0b010,
E_RM_QUOTES = 0b100
E_EXPAND = 0b0001,
E_SPLIT_WORDS = 0b0010,
E_WILDCARD = 0b0100,
E_RM_QUOTES = 0b1000
} t_expander_op;

typedef enum e_expander_task_type
Expand Down
3 changes: 2 additions & 1 deletion include/expander.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ bool handle_quote_removal(t_list **task_list);
bool remove_quote(t_list *task_list);

/* wildcard_expansion.c */
bool handle_wildcard_expansion(t_list **lst, t_list **task_list);
bool handle_wildcard_expansion(t_list **lst, t_list **task_list,
t_expander_op op_mask);

/* wildcard_expansion_utils.c */
bool is_wildcard(char *word, t_list *task_list);
Expand Down
2 changes: 1 addition & 1 deletion source/backend/executor/executor_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int set_expanded_cmd_name(

expanded_list = NULL;
ret = expand_list(shell, simple_cmd_list, &expanded_list, \
E_EXPAND | E_SPLIT_WORDS | E_RM_QUOTES);
E_EXPAND | E_SPLIT_WORDS | E_WILDCARD | E_RM_QUOTES);
*cmd_name = ft_lstpop_front_content(&expanded_list);
ft_lstclear(&expanded_list, free);
return (ret);
Expand Down
3 changes: 2 additions & 1 deletion source/backend/redirection/io_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ int expand_filename(t_shell *shell, char **filename)
int ret;

expanded_list = NULL;
ret = expander(*filename, &expanded_list, shell, E_EXPAND | E_RM_QUOTES);
ret = expander(*filename,
&expanded_list, shell, E_EXPAND | E_WILDCARD | E_RM_QUOTES);
if (ret == MALLOC_ERROR || ret == BAD_SUBSTITUTION)
return (ft_lstclear(&expanded_list, free), ret);
if (ft_lstsize_non_null(expanded_list) != 1)
Expand Down
2 changes: 1 addition & 1 deletion source/debug/print_expanded_cmd_table_content.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bool expand_and_print(char *str, t_shell *shell)

expanded_list = NULL;
ret = expander(str, &expanded_list, shell,
E_EXPAND | E_SPLIT_WORDS | E_RM_QUOTES);
E_EXPAND | E_SPLIT_WORDS | E_WILDCARD | E_RM_QUOTES);
if (ret == MALLOC_ERROR)
return (printf("malloc failed in expander"), false);
if (ret == BAD_SUBSTITUTION)
Expand Down
2 changes: 1 addition & 1 deletion source/frontend/expander/expander.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool handle_expansion(t_list **lst, t_shell *shell, t_expander_op op_mask)
!handle_word_splitting(lst, op_mask, &task_list) || \
!set_wildcard_task_list(&task_list, *lst, op_mask) || \
!handle_quote_removal(&task_list) || \
!handle_wildcard_expansion(lst, &task_list))
!handle_wildcard_expansion(lst, &task_list, op_mask))
return (ft_lstclear(&task_list, (void *)free_expander_task), false);
ft_lstclear(&task_list, (void *)free_expander_task);
return (true);
Expand Down
5 changes: 3 additions & 2 deletions source/frontend/expander/wildcard_expansion.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ static bool iter_word_list(t_list **lst, t_list *file_list, t_list **task_list)
return (true);
}

bool handle_wildcard_expansion(t_list **lst, t_list **task_list)
bool handle_wildcard_expansion(
t_list **lst, t_list **task_list, t_expander_op op_mask)
{
t_list *file_list;

if (!any_wildcard(*lst, *task_list))
if (!(op_mask & E_WILDCARD) || !any_wildcard(*lst, *task_list))
return (true);
file_list = NULL;
if (!set_file_list(&file_list))
Expand Down
3 changes: 2 additions & 1 deletion source/frontend/expander/wildcard_task_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ bool set_wildcard_task_list(
t_list *new_task_list;
bool ret;

(void)op_mask;
if (!(op_mask & E_WILDCARD))
return (true);
ret = true;
new_task_list = NULL;
while (lst && ret)
Expand Down
2 changes: 1 addition & 1 deletion source/utils/final_cmd_table_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int setup_simple_cmd(t_shell *shell, t_list *simple_cmd_list)

expanded_list = NULL;
ret = expand_list(shell, simple_cmd_list, &expanded_list,
E_EXPAND | E_SPLIT_WORDS | E_RM_QUOTES);
E_EXPAND | E_SPLIT_WORDS | E_WILDCARD | E_RM_QUOTES);
if (ret != SUCCESS)
return (ft_lstclear(&expanded_list, free), ret);
shell->final_cmd_table->simple_cmd = \
Expand Down

0 comments on commit cc300a9

Please sign in to comment.