multitudes / 42-minishell

This project is about creating a simple shell
MIT License
0 stars 0 forks source link

Suggestions for the expand_tokenlist function #181

Closed multitudes closed 4 months ago

multitudes commented 4 months ago

Since you will be working on this function again:

oid expand_tokenlist(t_data *data, t_list *tokenlist)
{

    // flags to ensure correct expansions are done depending on other tokens in the list eg VAR=~"string"$EXP:~
    while (tokenlist)
    {
        if (!get_curr_token(tokenlist))
            print_error_status("minishell: system error: missing token\n", 1);
        debug("Token to check for expansion - token type: %d, lexeme: %s", get_token_type(tokenlist), get_token_lexeme(tokenlist));
        if (get_token_type(tokenlist) == S_QUOTED_STRING)
            extract_string(get_curr_token(tokenlist));
        else if (get_token_type(tokenlist) == QUOTED_STRING)
            expand_string(data, get_curr_token(tokenlist));
        else if (get_token_type(tokenlist) == VAR_EXPANSION || get_token_type(tokenlist) == DOLLAR)
            expand_dollar(data->env_arr, get_curr_token(tokenlist));
        if (get_token_type(tokenlist) == TILDE \
                || get_token_type(tokenlist) == PATHNAME) // specify condition
            expand_path(data->env_arr, get_curr_token(tokenlist));
        else if (get_token_type(tokenlist)  == DOLLAR_QUESTION)
            expand_exit_status(data, get_curr_token(tokenlist));
        else if (get_token_type(tokenlist) == GLOBBING)
            expand_globbing(tokenlist);
        else
            debug("Token type not expanded");
        // Merge tokens
        tokenlist = tokenlist->next;
    }

Some suggestions:

PS after looking at the function recognizing if PATHNAME, I did a lot of checks trying to get a mostly correct check for paths, but if I put tilde in here then this -w-+}[]=w{~or^%ld- becomes a pathname which makes me cringe a bit...

multitudes commented 4 months ago

THis would be the function in scanner:

bool    str_is_pathname(const char *str)
{
    if (((ft_strchr(str, '/') && (!ft_strchr(str, '~'))) || peek(str, ".", \
    false) || peek(str, "./", FUZZY) || peek(str, "../", FUZZY) || peek(str, \
    "~/", FUZZY) || peek(str, "~+/", FUZZY) || peek(str, "~+", EXACT) || \
    peek(str, "~-/", FUZZY) || peek(str, "~-", EXACT)) && !ft_strchr(str, '*'))
    {
        while (*str)
        {
            if (!char_is_in_pathname(*str))
                return (false);
            str++;
        }
        return (true);
    }
    return (false);
}

Mayeb if you have sggestions to improve this? here I try to get the pathname type whjih includes all of the valid tilde cases (I tried)