这实际上是作为对更具体问题的答案的一部分(如何将评估的PS1输出变为变量),并且我认为该过程应该得到自己的问题和答案,这更加概括于修改bash源代码的过程.
以下是添加内部命令的步骤,该命令evalps1旨在为您提供与主提示字符串相同的输出.它基于版本4.2代码库.
步骤1.
首先,进行更改,support/mkversion.sh以免您将其与"真实"混淆bash,以便FSF可以为保修目的拒绝所有知识:-)只需更改一行(我添加了-pax位):
echo "#define DISTVERSION \"${float_dist}-pax\""
Run Code Online (Sandbox Code Playgroud)
该脚本是创建一个version.h和影响从获得的版本信息的所有不同的方式bash:--version中,$BASH_VERSION变量等等.
第2步:
更改builtins/Makefile.in以添加新的源文件.这需要许多步骤.
(a)$(srcdir)/evalps1.def加到最后DEFSRC.此def文件包含有关内部命令以及实现代码的信息.
(b)添加evalps1.o到最后OFILES.这只会导致您的代码被链接bash.
(c)添加所需的依赖项:
evalps1.o: evalps1.def $(topdir)/bashtypes.h $(topdir)/config.h \
$(topdir)/bashintl.h $(topdir)/shell.h common.h
Run Code Online (Sandbox Code Playgroud)
第3步:
添加builtins/evalps1.def文件本身,这是运行evalps1命令时执行的代码:
This file is evalps1.def, from which is created evalps1.c.
It implements the builtin "evalps1" in Bash.
Copyright (C) 1987-2009 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES evalps1.c
$BUILTIN evalps1
$FUNCTION evalps1_builtin
$SHORT_DOC evalps1
Outputs the fully interpreted PS1 prompt.
Outputs the PS1 prompt, fully evaluated, for whatever nefarious purposes
you require.
$END
Run Code Online (Sandbox Code Playgroud)
其中很大一部分是GPL许可证(因为我修改了它exit.def),如上所示,最后使用一个非常简单的函数来获取和解码PS1,使用与shell本身使用相同的函数:
#include <config.h>
#include "../bashtypes.h"
#include <stdio.h>
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
int
evalps1_builtin (list)
WORD_LIST *list;
{
char *ps1 = get_string_value ("PS1");
if (ps1 != 0)
{
ps1 = decode_prompt_string (ps1);
if (ps1 != 0)
{
printf ("%s", ps1);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,代码本身非常简单,虽然它使用了我认为不必要的复杂缩进/支撑规则和K&R风格的预原型函数声明 - 我保持它们与当前代码保持一致以保持一致性.
代码只是调用函数来获取PS1环境变量,然后调用另一个解码提示字符串的函数.最后,它将解码后的字符串输出到标准输出.
当然,您可以使用自己的代码替换该代码(以及将命令和文件名更改为evalps1其他代码),具体取决于您的特定需求.
第4步:
只需在顶级目录中构建东西:
./configure
make
Run Code Online (Sandbox Code Playgroud)
bash显示在该顶级目录中的可执行文件可以重命名为paxsh(例如),但我怀疑它将变得像它的祖先一样普遍:-)
运行它,你可以看到它在行动中,从一个真正的bashshell开始:
pax> mv bash paxsh
pax> ./paxsh --version
GNU bash, version 4.2-pax.0(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
pax> ./paxsh
pax> echo $BASH_VERSION
4.2-pax.0(1)-release
pax> echo "[$PS1]"
[pax> ]
pax> echo "[$(evalps1)]"
[pax> ]
pax> PS1="\h: "
paxbox01: echo "[$PS1]"
[\h: ]
paxbox01: echo "[$(evalps1)]"
[paxbox01: ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
846 次 |
| 最近记录: |