在自己的shell C ++中实现历史

时间:2015-04-12 01:27:02

标签: c++ shell unix

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

----修改

char * buf;

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);
}

1 个答案:

答案 0 :(得分:0)

我没有使用过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()分配的缓冲区。