c - readline() 内部缓冲区

标签 c linux gnu libreadline

使用GNU Readline :

函数readline()显示提示并读取用户的输入。

我可以修改其内部缓冲区吗?以及如何实现这一目标?

#include <readline/readline.h>
#include <readline/history.h>

int main()
{
    char* input;
        // Display prompt and read input 
        input = readline("please enter your name: ");

        // Check for EOF.
        if (!input)
            break;

        // Add input to history.
        add_history(input);

        // Do stuff...

        // Free input.
        free(input);
    }
}

最佳答案

是的,可以修改readline的编辑缓冲区,例如通过使用函数rl_insert_text()。为了使其有用,我认为您需要使用 readline 稍微复杂的“回调接口(interface)”,而不是你的例子。

Readline 附带了非常好的和完整的 documentation ,因此我只给出一个最小的示例程序来帮助您入门:

/* compile with gcc -o test <this program>.c -lreadline */

#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>

void line_handler(char *line) { /* This function (callback) gets called by readline
                                   whenever rl_callback_read_char sees an ENTER */ 
  printf("You changed this into: '%s'\n", line);
  exit(0);
}

int main() {
  rl_callback_handler_install("Enter a line: ", &line_handler);
  rl_insert_text("Heheheh...");    /* insert some text into readline's edit buffer... */
  rl_redisplay ();                 /* Make sure we see it ... */

  while (1) {
    rl_callback_read_char();       /* read and process one character from stdin */
  }
}    

关于c - readline() 内部缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659931/

相关文章:

C编程内存子分配器的循环列表

c - s1、&s1、&s1.a 之间的区别(结构)

c++ - unix上高效的IP地址c/c++库

linux - 如何将上下文后的 grep 设置为 "until the next blank line"?

llvm - 使用 LLVM 自定义标准 C 库(以支持 llvm 后端优化)

bash - Makefiles中 '@'命令中 "@set -e"的含义

c - 如何找到下一个种子,使随机数序列在 C 中保持不变?

linux - 如何在 shell 脚本中进行比较?

linux - Beagleboard 上的 Angstrom Boot 挂起

c++ - 如何在 C 宏中连接变量字符串和文字字符串?