首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Heredoc和tmpfile

Heredoc和tmpfile
EN

Stack Overflow用户
提问于 2021-12-05 21:58:30
回答 1查看 640关注 0票数 0

我正在编写我的minishell版本,并试图在C中实现本文档(<<)。我决定使用tmpfile --首先我将数据从stdin写入tmpfile,直到到达分隔符为止,然后用dup2将程序的stdin更改为tmpfile的fd,然后尝试使用execve执行cat命令。

我试图简化程序,并包括以下所有相关功能:

代码语言:javascript
复制
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);
}

我想执行cat << eof命令。一切都很好,但是我面临着handle_dobinp函数中重复输出(在测试期间)的问题。另外,在while循环中,main中还有一个带有空输入的迭代(即程序执行空命令)。

只有一个进程在运行,所以我不确定这一行为的原因是什么?

更新:I根据Edwin Buck注释更新了程序的输出。

代码语言:javascript
复制
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$ 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-05 22:11:21

提高你的伐木能力。我想您的输出是正确的,但是看起来

代码语言:javascript
复制
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$ 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70238913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档