简体   繁体   English

在C中解析类似shell的命令

[英]Parsing commands shell-like in C

I want to parse user input commands in my C (just C) program. 我想在我的C(只是C)程序中解析用户输入命令。 Sample commands: 示例命令:

add node ID

add arc ID from ID to ID

print

exit

and so on. 等等。 Then I want to do some validation with IDs and forward them to specified functions. 然后我想用ID做一些验证并将它们转发到指定的函数。 Functions and validations are of course ready. 功能和验证当然是准备好的。 It's all about parsing and matching functions... 这都是关于解析和匹配函数的...

I've made it with many if s and strtok s, but I'm sure it's not the best way... Any ideas (libs)? 我和许多做它if S和strtok S,但我敢肯定,这不是最好的办法...任何想法(库)?

I think what you want is something like this: 我想你想要的是这样的:

while (1) 
{
    char *line = malloc(128); // we need to be able to increase the pointer
    char *origLine = line;
    fgets(line, 128, stdin);

    char command[20];
    sscanf(line, "%20s ", command);

    line = strchr(line, ' ');

    printf("The Command is: %s\n", command);

    unsigned argumentsCount = 0;
    char **arguments = malloc(sizeof(char *));

    while (1)
    {
        char arg[20];
        if (line && (sscanf(++line, "%20s", arg) == 1))
        {
            arguments[argumentsCount] = malloc(sizeof(char) * 20);
            strncpy(arguments[argumentsCount], arg, 20);

            argumentsCount++;

            arguments = realloc(arguments, sizeof(char *) * argumentsCount + 1);
            line = strchr(line, ' ');
        }
        else {
            break;
        }
    }

    for (int i = 0; i < argumentsCount; i++) {
        printf("Argument %i is: %s\n", i, arguments[i]);
    }

    for (int i = 0; i < argumentsCount; i++) {
        free(arguments[i]);
    }

    free(arguments);
    free(origLine);
}  

You can do what you wish with 'command' and 'arguments' just before you free it all. 在你释放它之前,你可以用'命令'和'参数'做你想做的事。

It depends on how complicated your command language is. 这取决于您的命令语言有多复杂。 It might be worth going to the trouble of womping up a simple recursive descent parser if you have more than a couple of commands, or if each command can take multiple forms, such as your add command. 如果你有多个命令,或者如果每个命令可以采用多种形式,例如你的add命令,那么编写一个简单的递归下降解析器 可能是值得的。

I've done a couple of RDPs by hand for some projects in the past. 我过去曾为一些项目手工完成了几个RDP。 It's a bit of work, but it allows you to handle some fairly complex commands that wouldn't be straightforward to parse otherwise. 这有点工作,但是它允许你处理一些相当复杂的命令,否则这些命令不会直接解析。 You could also use a parser generator like lex/yacc or flex/bison, although that may be overkill for what you are doing. 您也可以使用像lex / yacc或flex / bison这样的解析器生成器,尽管这可能对您正在做的事情有些过分。

Otherwise, it's basically what you've described; 否则,它基本上就是你所描述的; strok and a bunch of nested if statements. strok和一堆嵌套的if语句。

I just wanted to add something to Richard Ross's reply: Check the returned value from malloc and realloc. 我只想在Richard Ross的回复中添加一些内容:检查malloc和realloc的返回值。 It may lead to hard-to-find crashes in your program. 这可能会导致程序中难以发现的崩溃。

所有命令行参数都将存储到char数组* argv中。您可以使用argv [0],argv [1] ... argv [n]访问这些值。希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM