在自己的外壳C++中实现历史记录

Implementing history in own shell C++

本文关键字:实现 历史 记录 C++ 自己的 外壳      更新时间:2023-10-16

我正在自己的shell中实现历史命令,C++。我正在非规范模式下编写它。我已经实现了向上箭头键和向下箭头键以及退格键。我不知道如何开始历史。我应该使用C++库中的内置函数吗?

----编辑

夏亚 *布夫;

rl_bind_key('t',rl_abort);//disable auto-complete
while((buf = readline("n >> "))!=NULL)
{
    if (strcmp(buf,"quit")==0)
        break;
    printf("[%s]n",buf);
    if (buf[0]!=0)
        add_history(buf);
}

我没有使用过NonCanonicalMode但这是我如何在我的一个项目中实现 readline 的历史记录的。

也许它会对你有用:

#include <string>
#include <memory>
#include <iostream>
#include <algorithm>
#include <readline/readline.h>
#include <readline/history.h>
// clean up user input by deleting spaces from each end
inline std::string& trim(std::string& s, const char* t = " t")
{
    s.erase(s.find_last_not_of(t) + 1);
    s.erase(0, s.find_first_not_of(t));
    return s;
}
// smart pointer to clean up memory
// allocated by readline
struct malloc_deleter
{
    template <class T>
    void operator()(T* p) { std::free(p); }
};
typedef std::unique_ptr<char, malloc_deleter> cstring_uptr;
int main()
{
    // this directory needs to exist beforehand
    const std::string config_dir = "/home/wibble/.prog";
    using_history();
    read_history((config_dir + "/.history").c_str());
    std::string shell_prompt = "> ";
    cstring_uptr input;
    std::string line, prev;
    input.reset(readline(shell_prompt.c_str()));
    // copy input into a std::string
    while(input && trim(line = input.get()) != "exit")
    {
        if(!line.empty())
        {
            // only add line to history if it is different
            // from previous line
            if(line != prev)
            {
                add_history(line.c_str());
                write_history((config_dir + "/.history").c_str());
                prev = line;
            }
            // process the input
            std::reverse(line.begin(), line.end());
            // give relevant output
            std::cout << "reply: " << line << 'n';
        }
        input.reset(readline(shell_prompt.c_str()));
    }
}

我不喜欢我需要在两个地方调用readline(),但我无法弄清楚如何重写循环以避免它。也许我错过了一些简单的东西?

它使用带有自定义删除器的智能指针std::unique_ptr来清理readline使用 malloc() 分配的缓冲区。