加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在bash终端中实现CTRL-R(反向搜索)?

发布时间:2020-12-15 21:39:27 所属栏目:安全 来源:网络整理
导读:反向搜索示例: (reverse-i-search)`grep': git log | grep master 用于查找建议的算法是什么? 它的搜索空间来自哪里? 非常感谢指向其源代码的指针. 解决方法 反向搜索是 GNU Readline Library的一部分.Readline库有助于阅读线和编辑设施.整个源代码可以在
反向搜索示例:

(reverse-i-search)`grep': git log | grep master

用于查找建议的算法是什么?
它的搜索空间来自哪里?

非常感谢指向其源代码的指针.

解决方法

反向搜索是 GNU Readline Library的一部分.Readline库有助于阅读线和编辑设施.整个源代码可以在 here找到.

搜索空间的来源

以下源代码片段显示了如何确定历史记录的源文件:

/* Return the string that should be used in the place of this
   filename.  This only matters when you dont specify the
   filename to read_history (),or write_history (). */
static char *
history_filename (filename)
     const char *filename;
{
  char *return_val;
  const char *home;
  int home_len;

  return_val = filename ? savestring (filename) : (char *)NULL;

  if (return_val)
    return (return_val);

  home = sh_get_env_value ("HOME");
#if defined (_WIN32)
  if (home == 0)
    home = sh_get_env_value ("APPDATA");
#endif

  if (home == 0)
    return (NULL);
  else
    home_len = strlen (home);

  return_val = (char *)xmalloc (2 + home_len + 8); /* strlen(".history") == 8 */
  strcpy (return_val,home);
  return_val[home_len] = '/';
#if defined (__MSDOS__)
  strcpy (return_val + home_len + 1,"_history");
#else
  strcpy (return_val + home_len + 1,".history");
#endif

  return (return_val);
}

> savedtring()在savestring.c中定义,如果已定义,则只复制字符串文件名.
> sh_get_env_value()函数使用getenv()函数(由< stdlib.h>提供)实现,用于获取环境值(参见man page getenv(3)).
>如图所示,.bash_history或.history(这是函数返回NULL时使用的文件)将用作在Linux系统上实现搜索的源.

资料来源:histfile.c

如何存储历史记录

>可搜索的历史记录存储在HIST_ENTRY(历史列表)数组中.来自.bash_history的数据将添加到此数组中.资料来源:history.c
>在当前会话中输入的命令记录保存在_rl_saved_line_for_history中.
>将这两个组合成_rl_search_cxt实例成员数组(cxt-> line []),使用该数组执行搜索.

算法

使用_rl_isearch_dispatch()_rl_search_getchar()功能执行实际搜索.

简短的摘要 :
该算法逐字逐句地读取输入决定它应该做什么.如果没有中断,它会将字符添加到搜索字符串中,在数组中搜索它.如果找不到该字符串,它将移动到下一个元素,跳过再次找到的相同字符串,并且字符串的长度比搜索字符串的当前长度短. (读取默认值:在交换机中获取_rl_isearch_dispatch()中的确切详细信息)

如果没有找到字符串,则响铃.否则,它会显示字符串,但实际上并未在历史列表中移动,直到用户接受该位置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读