3

我认为使用 GNU Readline 库作为命令行提示符很好,我希望我正在处理的 shell 具有该功能。现在 readline 对我有用(我的环境是 CLion、CMake、Ubuntu、BSD、C、flex-lexer 和柠檬解析器)但我还需要 flex 和 yacc 同时工作以扫描和解析输入,但代码似乎“不相容”——真的吗?

    params[0] = NULL;
   printf("> ");

    i=1;
    do {
        lexCode = yylex(scanner);

        /*  snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));
         Display prompt and read input (NB: input must be freed after use)...*/



        text = strdup(yyget_text(scanner));
        /*
        input = readline(text);

        if (!input)
            break;

        add_history(input);

        free(input);*/
        printf("lexcode %i Text %s\n", lexCode, text);
        if (lexCode == 4) {
            params[i++] = mystring;
            if (strcmp(text, "\'\0")) {
                params[i++] = mystring;
            }
        } else
        if (lexCode != EOL) {
                params[i++] = text;
                printf("B%s\n", text);
        }
        Parse(shellParser, lexCode, text);
        if (lexCode == EOL) {
            dump_argv("Before exec_arguments", i, params);
            exec_arguments(i, params);
            corpse_collector();
            Parse(shellParser, 0, NULL);
            i=1;
        }
    } while (lexCode > 0);
    if (-1 == lexCode) {
        fprintf(stderr, "The scanner encountered an error.\n");
    }

上面的代码具有解析和扫描功能,并注释掉了如果我同时想要两者都不起作用的 readline 功能。我可以让它工作吗?

4

2 回答 2

5

flex+readline 示例

跟随一个快速弯曲的“外壳”,它可以ls而且date

%{
  #include <stdlib.h>
  #include <readline/readline.h>
  #include <readline/history.h>
  #define YY_INPUT(buf,result,max_size) result = mygetinput(buf, max_size);

  static int mygetinput(char *buf, int size) {
    char *line;
    if (feof(yyin))  return YY_NULL;
    line = readline("> ");
    if(!line)        return YY_NULL;
    if(strlen(line) > size-2){
       fprintf(stderr,"input line too long\n"); return YY_NULL; }
    sprintf(buf,"%s\n",line);
    add_history(line);
    free(line);
    return strlen(buf);
  }   
%}

%option noyywrap    
%%
ls.*         system(yytext);
date.*       system(yytext);
.+           fprintf(stderr, "Error: unknown comand\n");
[ \t\n]+     {}
%%

构建它:

flex mysh.fl
cc -o mysh lex.yy.c -lreadline -lfl
于 2016-05-23T16:03:35.907 回答
4

阅读GNU readline的文档。阅读tty 揭秘页面。您readline只希望在您的标准输入是tty时使用,所以使用isatty(3)isatty(STDIN_FILENO)检测它。

shell using 的基本行为readline 就是使用

char *readline (const char *prompt);

功能。因此,您希望调整解析器以从缓冲区读取,而不是从stdin. 这是通常的做法。完成后,不要忘记测试(针对失败)您readlinefree 结果缓冲区的调用。

然后,您想添加一些完成。那是困难的部分。看看其他 shell ( zsh, bash, fish...) 正在做什么,至少是为了获得灵感。请注意,默认文件名完成可能就足够了,至少在启动时是这样。

顺便说一句,我不会同时使用柠檬野牛来解析相同的输入。实际上,对于一个 shell(因为它的语法很简单),我将只使用一些手写的递归下降解析器

于 2016-04-19T04:59:30.020 回答