如何实施&>和&>>在shell程序中?

时间:2014-03-23 00:30:01

标签: c shell unix

我正在实现自己的shell,并且设法让I / O重定向与管道一起工作。但是,我无法理解我应该如何重定向stderr,以便我可以在我的代码中包含>&>>&功能。

此外,逻辑是否遵循实施|&

这是我的代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>

void execute(char **, int, char **);
void handle_signal(int);
int parse(char *, char **, char **, int *);
void chop(char *);

#define INPUT_STRING_SIZE 80

#define NORMAL              00
#define OUTPUT_REDIRECTION  11
#define INPUT_REDIRECTION   22
#define PIPELINE            33
#define BACKGROUND          44
#define OUTPUT_APP  55

typedef void (*sighandler_t)(int);

int main(int argc, char *argv[])
{
    int i, mode = NORMAL, cmdArgc;
    size_t len = INPUT_STRING_SIZE;
    char *cpt, *inputString, *cmdArgv[INPUT_STRING_SIZE], *supplement = NULL;
    inputString = (char*)malloc(sizeof(char)*INPUT_STRING_SIZE);

    char curDir[100];

    while(1)
    {
        mode = NORMAL;
        getcwd(curDir, 100);
        printf("%s@%s->", getlogin(),curDir);
        getline( &inputString, &len, stdin);
        if(strcmp(inputString, "exit\n") == 0)
            exit(0);
        cmdArgc = parse(inputString, cmdArgv, &supplement, &mode);
        if(strcmp(*cmdArgv, "cd") == 0)
        {
            chdir(cmdArgv[1]);
        }
        else 
            execute(cmdArgv, mode, &supplement);
    }
    return 0;
}

int parse(char *inputString, char *cmdArgv[], char **supplementPtr, int *modePtr)
{
    int cmdArgc = 0, terminate = 0;
    char *srcPtr = inputString;
    //printf("parse fun%sends", inputString);
    while(*srcPtr != '\0' && terminate == 0)
    {
        *cmdArgv = srcPtr;
        cmdArgc++;
        //printf("parse fun2%sends", *cmdArgv);
        while(*srcPtr != ' ' && *srcPtr != '\t' && *srcPtr != '\0' && *srcPtr != '\n' && terminate == 0)
        {
            switch(*srcPtr)
            {
                case '&':
                    *modePtr = BACKGROUND;
                    break;
                case '>':
                    *modePtr = OUTPUT_REDIRECTION;
                    *cmdArgv = '\0';
                    srcPtr++;
                    if(*srcPtr == '>')
                    {
                        *modePtr = OUTPUT_APP;
                        srcPtr++;
                    }
                    while(*srcPtr == ' ' || *srcPtr == '\t')
                        srcPtr++;
                    *supplementPtr = srcPtr;
                    chop(*supplementPtr);
                    terminate = 1;
                    break;
                case '<':
                    *modePtr = INPUT_REDIRECTION;
                    *cmdArgv = '\0';
                    srcPtr++;
                    while(*srcPtr == ' ' || *srcPtr == '\t')
                        srcPtr++;
                    *supplementPtr = srcPtr;
                    chop(*supplementPtr);
                    terminate = 1;
                    break;
                case '|':
                    *modePtr = PIPELINE;
                    *cmdArgv = '\0';
                    srcPtr++;
                    while(*srcPtr == ' ' || *srcPtr == '\t')
                        srcPtr++;
                    *supplementPtr = srcPtr;
                    //chop(*supplementPtr);
                    terminate = 1;
                    break;
            }
            srcPtr++;
        }
        while((*srcPtr == ' ' || *srcPtr == '\t' || *srcPtr == '\n') && terminate == 0)
        {
            *srcPtr = '\0';
            srcPtr++;
        }
        cmdArgv++;
    }
    /*srcPtr++;
    *srcPtr = '\0';
    destPtr--;*/
    *cmdArgv = '\0';
    return cmdArgc;
}

void chop(char *srcPtr)
{
    while(*srcPtr != ' ' && *srcPtr != '\t' && *srcPtr != '\n')
    {
        srcPtr++;
    }
    *srcPtr = '\0';
}

void execute(char **cmdArgv, int mode, char **supplementPtr)
{
    pid_t pid, pid2;
    FILE *fp;
    int mode2 = NORMAL, cmdArgc, status1, status2;
    char *cmdArgv2[INPUT_STRING_SIZE], *supplement2 = NULL;
    int myPipe[2];
    if(mode == PIPELINE)
    {
        if(pipe(myPipe))                    //create pipe
        {
            fprintf(stderr, "Pipe failed!");
            exit(-1);
        }
        parse(*supplementPtr, cmdArgv2, &supplement2, &mode2);
    }
    pid = fork();
    if( pid < 0)
    {
        printf("Error occured");
        exit(-1);
    }
    else if(pid == 0)
    {
        switch(mode)
        {
            case OUTPUT_REDIRECTION:
                fp = fopen(*supplementPtr, "w+");
                dup2(fileno(fp), 1);
                break;
            case OUTPUT_APP:
                fp = fopen(*supplementPtr, "a");
                dup2(fileno(fp), 1);
                break;
            case INPUT_REDIRECTION:
                fp = fopen(*supplementPtr, "r");
                dup2(fileno(fp), 0);
                break;
            case PIPELINE:
                close(myPipe[0]);       //close input of pipe
                dup2(myPipe[1], fileno(stdout));
                close(myPipe[1]);
                break;
        }
        execvp(*cmdArgv, cmdArgv);
    }
    else
    {
        if(mode == BACKGROUND)
            ;
        else if(mode == PIPELINE)
        {
            waitpid(pid, &status1, 0);      //wait for process 1 to finish
            pid2 = fork();
            if(pid2 < 0)
            {
                printf("error in forking");
                exit(-1);
            }
            else if(pid2 == 0)
            {
                close(myPipe[1]);       //close output to pipe
                dup2(myPipe[0], fileno(stdin));
                close(myPipe[0]);
                execvp(*cmdArgv2, cmdArgv2);
            }
            else
            {
                ;//wait(NULL);
                //waitpid(pid, &status1, 0);
                //waitpid(pid2, &status2, 0);
                close(myPipe[0]);
                close(myPipe[1]);
            }
        }
        else
            waitpid(pid, &status1, 0);
            //wait(NULL);
    }
}

任何帮助将不胜感激!

好的,根据Jonathan Leffler的建议,我修改了我的代码以包含重定向。现在,我想看看是否可以通过尝试让用户输入“&gt;&amp;”来改变它。而不是“&amp;&gt;”并仍然实现“&gt;&amp;”确实。 “&amp;&gt;&gt;”也是如此和“&amp; |”。

但是,当我尝试发出命令时,请告诉echo hello&gt;&amp; a.txt,我得到一个名为“&amp;”的文件那个文件现在包含字符串hello!我不确定这里出了什么问题。 @JonathanLeffler - 请你看看我可能做错了什么?这是更新的代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>


void execute(char **, int, char **);
void handle_signal(int);
int parse(char *, char **, char **, int *);
void chop(char *);

#define INPUT_STRING_SIZE 80

#define NORMAL              00
#define OUTPUT_REDIRECTION  11
#define INPUT_REDIRECTION   22
#define PIPELINE            33
#define BACKGROUND          44
#define OUTPUT_APP  55
#define OUTPUT_REDIRECTION_WITH_ERROR 66
#define OUTPUT_REDIRECTION_WITH_APPEND_ERROR 77
#define PIPELINE_WITH_ERROR 88


typedef void (*sighandler_t)(int);



int main(int argc, char *argv[])
{
    int i, mode = NORMAL, cmdArgc;
    size_t len = INPUT_STRING_SIZE;
    char *cpt, *inputString, *cmdArgv[INPUT_STRING_SIZE], *supplement = NULL;
    inputString = (char*)malloc(sizeof(char)*INPUT_STRING_SIZE);

    char curDir[100];

    while(1)
    {
        mode = NORMAL;
        getcwd(curDir, 100);
        printf("%s@%s->", getlogin(),curDir);
        getline( &inputString, &len, stdin);
        if(strcmp(inputString, "exit\n") == 0)
            exit(0);
        cmdArgc = parse(inputString, cmdArgv, &supplement, &mode);
        if(strcmp(*cmdArgv, "cd") == 0)
        {
            chdir(cmdArgv[1]);
        }
        else 
            execute(cmdArgv, mode, &supplement);
    }
    return 0;
}

int parse(char *inputString, char *cmdArgv[], char **supplementPtr, int *modePtr)
{
    int cmdArgc = 0, terminate = 0;
    char *srcPtr = inputString;
    //printf("parse fun%sends", inputString);
    while(*srcPtr != '\0' && terminate == 0)
    {
        *cmdArgv = srcPtr;
        cmdArgc++;
        //printf("parse fun2%sends", *cmdArgv);
        while(*srcPtr != ' ' && *srcPtr != '\t' && *srcPtr != '\0' && *srcPtr != '\n' && terminate == 0)
        {
            switch(*srcPtr)
            {
            /*  case '&':
                    *modePtr = BACKGROUND;
                    break; */
                case '>':
                    *modePtr = OUTPUT_REDIRECTION;
                    *cmdArgv = '\0';
                    srcPtr++;
                    if(*srcPtr == '>')
                    {
                        *modePtr = OUTPUT_APP;
                        srcPtr++;
                    }
                    else if(*srcPtr == '>&')
                    {
                        *modePtr = OUTPUT_REDIRECTION_WITH_ERROR;
                        srcPtr++;
                    }
                    else if(*srcPtr == '>>&')
                    {
                        *modePtr = OUTPUT_REDIRECTION_WITH_APPEND_ERROR;
                        srcPtr++;
                    }
                    while(*srcPtr == ' ' || *srcPtr == '\t')
                        srcPtr++;
                    *supplementPtr = srcPtr;
                    chop(*supplementPtr);
                    terminate = 1;
                    break;
                case '<':
                    *modePtr = INPUT_REDIRECTION;
                    *cmdArgv = '\0';
                    srcPtr++;
                    while(*srcPtr == ' ' || *srcPtr == '\t')
                        srcPtr++;
                    *supplementPtr = srcPtr;
                    chop(*supplementPtr);
                    terminate = 1;
                    break;
                case '|':
                    *modePtr = PIPELINE;
                    *cmdArgv = '\0';
                    srcPtr++;
                    if(*srcPtr == '|')
                    {
                        *modePtr = PIPELINE;
                        srcPtr++;
                    }
                    else if(*srcPtr == '|&')
                    {
                        *modePtr = PIPELINE_WITH_ERROR;
                        srcPtr++;
                    }
                    while(*srcPtr == ' ' || *srcPtr == '\t')
                        srcPtr++;
                    *supplementPtr = srcPtr;
                    //chop(*supplementPtr);
                    terminate = 1;
                    break;
            }
            srcPtr++;
        }
        while((*srcPtr == ' ' || *srcPtr == '\t' || *srcPtr == '\n') && terminate == 0)
        {
            *srcPtr = '\0';
            srcPtr++;
        }
        cmdArgv++;
    }
    /*srcPtr++;
    *srcPtr = '\0';
    destPtr--;*/
    *cmdArgv = '\0';
    return cmdArgc;
}

void chop(char *srcPtr)
{
    while(*srcPtr != ' ' && *srcPtr != '\t' && *srcPtr != '\n')
    {
        srcPtr++;
    }
    *srcPtr = '\0';
}

void execute(char **cmdArgv, int mode, char **supplementPtr)
{
    pid_t pid, pid2;
    FILE *fp;
    int mode2 = NORMAL, cmdArgc, status1, status2;
    char *cmdArgv2[INPUT_STRING_SIZE], *supplement2 = NULL;
    int myPipe[2];
    if(mode == PIPELINE)
    {
        if(pipe(myPipe))                    //create pipe
        {
            fprintf(stderr, "Pipe failed!");
            exit(-1);
        }
        parse(*supplementPtr, cmdArgv2, &supplement2, &mode2);
    }
    pid = fork();
    if( pid < 0)
    {
        printf("Error occured");
        exit(-1);
    }
    else if(pid == 0)
    {
        switch(mode)
        {
            case OUTPUT_REDIRECTION:
                fp = fopen(*supplementPtr, "w+");
                dup2(fileno(fp), 1);
                break;
            case OUTPUT_REDIRECTION_WITH_ERROR:
                fp = fopen(*supplementPtr, "w+");
                dup2(fileno(fp), 1);
                dup2(2, 1);
                break;
            case OUTPUT_REDIRECTION_WITH_APPEND_ERROR:
                fp = fopen(*supplementPtr, "a");
                dup2(fileno(fp), 1);
                dup2(2, 1);
                break;
            case OUTPUT_APP:
                fp = fopen(*supplementPtr, "a");
                dup2(fileno(fp), 1);
                break;
            case INPUT_REDIRECTION:
                fp = fopen(*supplementPtr, "r");
                dup2(fileno(fp), 0);
                break;
            case PIPELINE:
                close(myPipe[0]);       //close input of pipe
                dup2(myPipe[1], fileno(stdout));
                close(myPipe[1]);
                break;
            case PIPELINE_WITH_ERROR:
                close(myPipe[0]);
                dup2(myPipe[1], 1);
                dup2(2, 1);
                close(myPipe[1]);
                break;
        }
        execvp(*cmdArgv, cmdArgv);
    }
    else
    {
        if(mode == BACKGROUND)
            ;
        else if(mode == PIPELINE)
        {
            waitpid(pid, &status1, 0);      //wait for process 1 to finish
            pid2 = fork();
            if(pid2 < 0)
            {
                printf("error in forking");
                exit(-1);
            }
            else if(pid2 == 0)
            {
                close(myPipe[1]);       //close output to pipe
                dup2(myPipe[0], fileno(stdin));
                close(myPipe[0]);
                execvp(*cmdArgv2, cmdArgv2);
            }
            else
            {
                ;//wait(NULL);
                //waitpid(pid, &status1, 0);
                //waitpid(pid2, &status2, 0);
                close(myPipe[0]);
                close(myPipe[1]);
            }
        }
        else
            waitpid(pid, &status1, 0);
            //wait(NULL);
    }
}

1 个答案:

答案 0 :(得分:5)

通过所有I / O重定向,操作是对文件描述符的直接操作。关键功能是dup2()

>&2表示法将标准输出(文件描述符,fd,1)重定向到标准错误(fd = 2):

dup2(2, 1);

这使得现有的打开文件描述符2和(不一定是打开的)文件描述符1引用相同的描述符。 (我在这个答案的第一版中得到了相反的论点;也在下一次调用dup2()时,但最后两个是正确的。)

&>表示法将标准错误重定向到与标准输出相同的位置:

dup2(1, 2);

使用竖线(|&),您首先执行:

pipe(pair);
…fork()…
…in the writer…
dup2(pair[1], 1);
dup2(1, 2);
close(pair[0]);
close(pair[1]);

使用>>表示法,以附加模式打开文件,然后使用dup2()


  

可以使用dup()代替dup2()来完成吗?如果是这样,它将如何完成?

是的,就像用Peano算术做数学一样。这很难,为什么要这么麻烦?

不同之处在于dup()将描述符复制到最低可用描述符。因此,假设标准输入(0)和标准输出(1)打开,您可以模拟:

dup2(1, 2);

使用:

close(2);
dup(1);

只要您只处理描述符,例如5,这是可管理的。但是,dup2()更容易使用;即使存在小于目标描述符的闭合描述符,它也会复制到指定的描述符。


修复更新后的代码

  

请您查看我上面提到的更新问题并提出我可能做错的建议吗?

在给出这样的请求时,我做的第一件事是在我通常的编译器警告标志集下编译它(从问题中逐字复制),在这种情况下,是警告(由{{1}转换为错误}选项)既丰富又严肃。如果你没有编译并看到这样的警告,那么你的生活就会比你需要的更难!

-Werror

在您实际解析传递给shell的参数之前,可以使用$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror pipes-22585525.c -o pipes-22585525 pipes-22585525.c: In function ‘main’: pipes-22585525.c:45:9: error: implicit declaration of function ‘strcmp’ [-Werror=implicit-function-declaration] if(strcmp(inputString, "exit\n") == 0) ^ pipes-22585525.c:34:11: error: unused variable ‘cpt’ [-Werror=unused-variable] char *cpt, *inputString, *cmdArgv[INPUT_STRING_SIZE], *supplement = NULL; ^ pipes-22585525.c:32:27: error: variable ‘cmdArgc’ set but not used [-Werror=unused-but-set-variable] int i, mode = NORMAL, cmdArgc; ^ pipes-22585525.c:32:9: error: unused variable ‘i’ [-Werror=unused-variable] int i, mode = NORMAL, cmdArgc; ^ pipes-22585525.c:30:14: error: unused parameter ‘argc’ [-Werror=unused-parameter] int main(int argc, char *argv[]) ^ pipes-22585525.c:30:26: error: unused parameter ‘argv’ [-Werror=unused-parameter] int main(int argc, char *argv[]) ^ pipes-22585525.c: In function ‘parse’: pipes-22585525.c:84:40: error: multi-character character constant [-Werror=multichar] else if(*srcPtr == '>&') ^ pipes-22585525.c:84:21: error: comparison is always false due to limited range of data type [-Werror=type-limits] else if(*srcPtr == '>&') ^ pipes-22585525.c:89:40: error: multi-character character constant [-Werror=multichar] else if(*srcPtr == '>>&') ^ pipes-22585525.c:89:21: error: comparison is always false due to limited range of data type [-Werror=type-limits] else if(*srcPtr == '>>&') ^ pipes-22585525.c:119:40: error: multi-character character constant [-Werror=multichar] else if(*srcPtr == '|&') ^ pipes-22585525.c:119:21: error: comparison is always false due to limited range of data type [-Werror=type-limits] else if(*srcPtr == '|&') ^ pipes-22585525.c: In function ‘execute’: pipes-22585525.c:160:43: error: unused variable ‘status2’ [-Werror=unused-variable] int mode2 = NORMAL, cmdArgc, status1, status2; ^ pipes-22585525.c:160:25: error: unused variable ‘cmdArgc’ [-Werror=unused-variable] int mode2 = NORMAL, cmdArgc, status1, status2; ^ cc1: all warnings being treated as errors $ 来避免有关argcargv的警告。 int main(void)的警告通过加入strcmp()来解决。其他未使用的变量警告也应该固定。它们是普通问题,但应该修复,以便代码免费编译警告。

另一组警告的例子如下:

<string.h>

变量pipes-22585525.c: In function ‘parse’: pipes-22585525.c:84:40: error: multi-character character constant [-Werror=multichar] else if(*srcPtr == '>&') ^ pipes-22585525.c:84:21: error: comparison is always false due to limited range of data type [-Werror=type-limits] srcPtr;它一次只能容纳一个字符。允许使用多字符常量,但具有实现定义的值。您唯一可以确定的是,单个字符永远不能保存值char *,这是消息中的第二个告诉您的。第一条消息暗示你应该写:

'>&'

有一个警告。假设符号为 else if (strncmp(srcPtr, ">&", 2) == 0) ;在Bash中,还有一个符号"<<"。在测试"<<<"之前测试"<<<"是至关重要的,否则你永远不会看到更长的符号,因为较短的符号总是匹配。与重定向一样;你需要注意确保没有早期测试阻止检测后面的一个符号。

解决这些问题,您可能会解决问题。如果你仍然卡住,再次Ping。 (哦,而且我已经看到程序在第一次接受我严格的编译选项时会产生更多警告 - 这不是错误的代码。但是因为编译器可以告诉你你做错了什么,没有让编译器告诉你错误的原因,你会浪费你的时间(可以说是我的,但这对未来有帮助)。记住,它比你知道的更多关于C!)

我使用那些编译器选项或其上的小变体,基本上都是我的C代码。其中大部分也在C ++编译器下编译和运行,但这是我选择穿的发型,你不必这样做。