Home > other >  Heredoc with tmpfile
Heredoc with tmpfile

Time:12-06

I am writing my version of minishell and trying to implement heredoc (<<) in C. I decided to use tmpfile - first I write data from stdin to tmpfile until I reach a delimiter, then I change program's stdin to the fd of the tmpfile with dup2 and, then, try to execute cat command with execve.

I tried to simplify the program and include all relevant functions below:

int main(int argc, char **argv, char **env)
{
    t_shell     shell;
    ft_parse_envs_to_lst(&envs, env); // transform env to linked list
    shell.tmpfile = "path to tmpfile";
    while (1)
    {
        char *buf = readline("bash: ");
        add_history(buf);
        shell.std_in = dup(0);
        shell.std_out = dup(1);
        shell.f_in = dup(0);
        shell.f_out = dup(1);
        /* Token is represented by linked list. "cat << eof" translates into "cat" -> ">>" -> "eof",
        with token pointing to "cat" */
        t_token *token = parse_buffer(buf); // parse_buffer will return pointer to the first token
        ft_execute_token(shell, token, env);
        free(buf);
    }
}

void    ft_execute_token(t_shell *shell, t_token *token, t_envs_lst *env)
{
    process_next_cmd(shell, token, env);
    close(shell->f_in);
    close(shell->f_out);
    dup2(shell->std_in, STDIN_FILENO);
    dup2(shell->std_out, STDOUT_FILENO);
    close(shell->std_in);
    close(shell->std_out);
}

void    process_next_cmd(t_shell *shell, t_token *token, t_envs_lst *env)
{
    t_token *prev = ft_get_prev_token(token); // get prev separator (for example, <<) or NULL
    t_token *next = ft_get_next_token(token); // get next separator (for example, <<) or NULL
    if (prev && (prev->type == DOBINP)) // "<<" will be marked as DOBINP
        ft_handle_dobinp(shell, token);
    if (next)
        process_next_cmd(shell, next->next, env); // recursively go to the next command
    if (!prev) // won't run any command on the part after "<<"" but will run "cat" 
    {
        ft_execute_cmd(token, env); // just execve on child process (created with fork), whilst parent is waiting for child
        if (next && next->type == DOBINP) // delete tmpfile
        {
            char **argv = malloc(sizeof(char *) * 3);
            argv[0] = "/bin/rm";
            argv[1] = shell->tmpfile;
            argv[2] = NULL;
            pid_t pid = fork();
            if (pid == 0)
                execve("/bin/rm", argv, NULL);
        }
    }
}

void    handle_dobinp(t_shell *shell, t_token *token)
{
    int     rd;
    int     fd;
    int     buf_size;
    char    *buf;

    fd = open(shell->tmpfile, O_TRUNC | O_CREAT | O_WRONLY, 0777);
    buf_size = strlen(token->str);
    buf = malloc(buf_size   1);
    printf("program: Start\n");
    rd = read(STDIN_FILENO, buf, buf_size);
    while (rd > 0)
    {
        buf[rd] = '\0';
        printf("program: Looping (read %s)", buf);
        if (strncmp(buf, token->str, buf_size   1) == 0)
            break ;
        write(fd, buf, rd);
        rd = read(STDIN_FILENO, buf, buf_size);
    }
    free(buf);
    close(fd);
    shell->f_in = open(shell->tmpfile, O_RDONLY, 0777);
    dup2(shell->f_in, STDIN_FILENO);
    close(shell->f_in);
}

I want to execute cat << eof command. Everything works fine but I face with the problem of repeated output (during testing) in handle_dobinp function. Also one more iteration occurs in while cycle in main with empty input (i.e. program executed empty command).

There is only one process running, so I am not sure what is the cause of this behaviour?

Update: I updated program's output according to Edwin Buck comment.

bash$ cat << eof
program: Start
foo
program: Looping (read foo
)
bar
program: Looping (read bar)
program: Looping (read 
)
eof
program: Looping (read eof)
foo
bar
bash$ 
bash$ 

CodePudding user response:

Improve your logging. I imagine your output is correct, but looking like

bash$ cat << eof
program: Start
foo
program: Looping (read "foo")
program: Looping (read "\n")
bar
program: Looping (read "bar")
program: Looping (read "\n")
eof
program: Looping (read "eof")
program: foo
program: bar
bash$ 
bash$ 
  • Related