解析命令仅适用于一个单词的命令

时间:2018-10-22 01:48:17

标签: c parsing token

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
HIST_ENTRY;
void printPrompt()
{
    printf("SB> ");
}
char *readCommandLine()
{
    char* myComm = (char*)malloc(1024);
    scanf("%s", myComm);
    return myComm;
}
char *parseCmd(char *myComm, char **tokens, size_t *index)
{
    char *tok;
    const char *delim = " ";
    *index = 0;
        if (tok != NULL)
        {
            tokens[*index] = tok;
            (*index)++;
        }
        else
        {
            tokens[*index] = "\0";
            printf("%s\n", tok);
            return;
        }
    while(tok != NULL)
    {
        tok = strtok(NULL, delim);
        tokens[*index] = tok;
        (*index)++;
    }
    tokens[*index] = NULL;
    return myComm;
}
int isInternalCommand(char *c)
{
    int value = 0;
    if (strcmp(c, "exit") == 0)
    {
        value = 1;
        printf("%s\n", "yes");
    }
    return value;
}
void execInternalCommand(char *c)
{
    int val = 0;
    char a[4];
    if (strcmp(c,"exit") == 0)
    {
        val = val + 1;
    }
    switch(val)
    {
    case 1:
    _exit(0);
    break;
    default:
    break;
    }
}
void executeCommand (char * c, char * const arguments)
{
    execvp(c, arguments);
}
int main()
{
    char *myComm;
    char *parsed;
    size_t num_args = 100;
    char **tokens = malloc(sizeof(char *) * (num_args+1));
    size_t *idx = (size_t *) malloc(sizeof(size_t));
    pid_t pid, cpid;
    int *status;
    while(1)
    {
        printPrompt();
        myComm = readCommandLine();
        parsed = parseCmd(myComm, tokens, idx);
        char *const *args = tokens;
        if (isInternalCommand(parsed))
        {
            execInternalCommand(parsed);
        }
        else
        {
            pid = fork();
            if (pid == 0)
            {
                executeCommand(parsed, args);
                printf("%s\n", "executing...");
            }
            else if (pid > 0)
            {
                waitpid(cpid, &status, 0);
                printf("%s\n", "waiting...");
            }
            else
            {
            } 

    }
    free(idx);
    }
    return 0;
}

因此,每当我尝试解析命令时,它仅适用于一个单词的命令。 例如,每当我尝试mkdir ./something时,它将显示带有“缺少操作数”的mkdir,这意味着它将不会创建目录。另外,每当我键入man时,例如man exec,它就会告诉诸如“您要查看哪个手册页?”之类的信息。我该如何解决?看起来这是一个解析问题。

1 个答案:

答案 0 :(得分:1)

scanf("%s", myComm);

从stdin读取单个空格分隔的单词。因此,您的命令只能是单个单词。如果您想阅读一行,则应该使用fgets,或者更好的是,使用getline,它将为您调用malloc:

char *readCommandLine()
{
    char* myComm = 0;
    size_t buffer_length = 0;
    size_t length = getline(&myComm, &buffer_length, stdin);
    if (myComm[length] == '\n')
        myComm[length--] = 0;  /* strip off trailing newline, if any */
    return myComm;
}