libreadline:什么时候调用函数“_rl_parse_colors()”?

libreadline: When is the function "_rl_parse_colors()" called?

我正在 GNU repository 开发 Bash 5.0。我想调试解析颜色的函数,所以我去了颜色文件:

  1. bash-5.0/lib/readline/colors.c - link
  2. bash-5.0/lib/readline/parse-colors.c - link

我在函数中添加了这样的打印 _rl_parse_colors:

printf ("[*] INSIDE _rl_parse_colors");  

我几乎在任何函数中都这样做了,但在我编译它之后,我 运行 它没有打印我的调试打印:

root@ubuntu:~/Desktop/bash-5.0# ./bash -c 'echo -e "3[31mHello\e[0m World"'
Hello World

我也尝试访问 bash 然后 运行 它:

root@ubuntu:~/Desktop/bash-5.0# ./bash
root@ubuntu:~/Desktop/bash-5.0# echo $BASH_VERSION                                                         
5.0.0(8)-release
root@ubuntu:~/Desktop/bash-5.0# ./bash -c 'echo -e "3[31mHello\e[0m World"'
Hello World

它用红色打印“你好”,但不是我的打印件。
跟我用的echo有关系吗?我看不出它会如何影响,因为 bash 包装了命令的 STDOUT,因此它应该用红色绘制字符串。

找到代码(我在bash-5.1-testing分支)--

在文件 lib/readline/parse-colors.c 中:

300 void _rl_parse_colors(void)
301 {
302 #if defined (COLOR_SUPPORT)
303   const char *p;                /* Pointer to character being parsed */
304   char *buf;                    /* color_buf buffer pointer */
305   int state;                    /* State of parser */
306   int ind_no;                   /* Indicator number */
307   char label[3];                /* Indicator label */
308   COLOR_EXT_TYPE *ext;          /* Extension we are working on */
309
310   p = sh_get_env_value ("LS_COLORS");
311   if (p == 0 || *p == '[=10=]')
312     {
313       _rl_color_ext_list = NULL;
314       return;
315     }
...

文件 lib/readline/readline.c 中只有一个 _rl_parse_colors() 的调用:

1215 /* Initialize the entire state of the world. */
1216 static void
1217 readline_initialize_everything (void)
1218 {
....
1287
1288 #if defined (COLOR_SUPPORT)
1289   if (_rl_colored_stats || _rl_colored_completion_prefix)
1290     _rl_parse_colors ();
1291 #endif
1292

变量名称 _rl_colored_stats_rl_colored_completion_prefix 听起来适用于这些 readline 设置:

[STEP 101] $ bind -v | grep color
set colored-completion-prefix off
set colored-stats off
[STEP 102] $

我刚刚验证了这些步骤会进入 _rl_parse_colors():

  1. ~/.inputrc中打开colored-completion-prefixcolored-stats
  2. [可选] 设置(并导出)LS_COLORS
  3. 使用您编译的 bash (./bash)
  4. 开始一个 interactive shell

根据bash手册(man bash):

colored-completion-prefix (Off)

If set to On, when listing completions, readline displays the common prefix of the set of possible completions using a different color. The color definitions are taken from the value of the LS_COLORS environment variable.

colored-stats (Off)

If set to On, readline displays possible completions using different colors to indicate their file type. The color definitions are taken from the value of the LS_COLORS environment variable.