GNU Readline是一个软件库,可为具有命令行界面(例如Bash)的交互式程序提供行编辑器和历史记录功能。是GNU計劃的一部分,目前由Chet Ramey维护。
用户可以移动文本光标,搜索命令历史,控制kill ring(一个更灵活的剪贴板),并在文本终端上使用Tab键自动完成。作为一个跨平臺的库,readline允许不同系统上的应用程序展现相同的行编辑行为。
GPL许可协议
GNU Readline是一个著名的自由软件库,它是通过GNU通用公共许可协议(GPL)进行授权的。而其他的自由软件库通常是通过GNU宽公用许可协议(LGPL)进行授权,例如GNU C库,GNU Gettext和FLTK。但其它软件在分发时,如果链接到像GNU Readline这种GPL协议的库,必须将与之相连的整个软件以GPL协议进行授权,以符合GPL第5章的规定。自由软件基金会为GNU Readline选用了这个授权协议,以鼓励其它软件选用GPL协议。
示例代码
下面的代码是C语言的,必须通过向编译器传递-lreadline标志来链接到readline库:
#include
#include
#include
#include
int main()
{
// Configure readline to auto-complete paths when the tab key is hit.
rl_bind_key('\t', rl_complete);
while (1) {
// Display prompt and read input
char* input = readline("prompt> ");
// Check for EOF.
if (!input)
break;
// Add input to readline history.
add_history(input);
// Do stuff...
// Free buffer that was allocated by readline
free(input);
}
return 0;
}
參考資料
外部链接
*[https://web.archive.org/web/20181012053849/http://tiswww.case.edu/php/chet/readline/rltop.html GNU readline主页]
*[https://twobithistory.org/2019/08/22/readline.html 关于GNU Readline你不知道的事情]
评论 (0)