readline 命令补全

40 篇文章 0 订阅

    readline是linux下常用的CLI交互式开源库,readline可以实现命令编辑,自动命令补全,历史命令搜索等人性化的交互方式。

系统实现了rl_filename_completion_function和rl_username_completion_function自动补全,实现自定义命令的自动补全需要实现rl_attemped_completion_function函数。

 

工作原理:

1.通过rl_complete()调用rl_completion_matches()来产生补全字符。

2.rl_completion_matches() 使用程序提供的generator函数产生补全字符。

3.generator函数会在rl_completion_matches()中不断调用,每次返回一个字符串。

Generator( const char *text,int state ) 第一个参数为当前输入字符,state为调用次数。第一次调用返回补全字符串,第二次必须返回0,终止匹配过程,否则会陷入死循环(不知API为何如此设计)。

 

具体补全参见:$(READLINE)/examples/fileman.c

调用代码:

initialize_readline()
rl_attempted_completion_function = fileman_completion;
fileman_comletion() 
matches = rl_completion_matches (text, command_generator);

 

// 命令补全
char *command_generator ( const char *text, int state )
{
	static int list_index, len;
	char *name;

	// 第一次查找   
	if (!state)
	{
	  list_index = 0;	// 注:该list_index一定要设置为static,下次再调用时无法找到name而结束匹配过程
	  len = strlen (text);
	}

	// 遍历命令列表
	while (name = commands[list_index].name)
	{
	  list_index++;

	  // 如部分匹配,则返回该name
	  if (strncmp (name, text, len) == 0)
	  {
	  	return dupstr(name);
	  }
	}

	// 无名称匹配,找到NULL
	return ((char *)NULL);
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值