Skip to content
Snippets Groups Projects
Commit 014bf168 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tools: use GNU Readline

* live command edition
* history browsing
parent 64d52f74
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,9 @@ set (CMAKE_CXX_FLAGS "-std=c++11 -Wno-return-type -Wall -Wextra -Wnon-virtual-dt ...@@ -20,6 +20,9 @@ set (CMAKE_CXX_FLAGS "-std=c++11 -Wno-return-type -Wall -Wextra -Wnon-virtual-dt
find_package (GnuTLS 3.1 REQUIRED) find_package (GnuTLS 3.1 REQUIRED)
find_package (Msgpack 1.1 REQUIRED) find_package (Msgpack 1.1 REQUIRED)
if (OPENDHT_TOOLS)
find_package (Readline 6 REQUIRED)
endif ()
list (APPEND opendht_SOURCES list (APPEND opendht_SOURCES
src/infohash.cpp src/infohash.cpp
......
# - Try to find readline, a library for easy editing of command lines.
# Variables used by this module:
# READLINE_ROOT_DIR - Readline root directory
# Variables defined by this module:
# READLINE_FOUND - system has Readline
# READLINE_INCLUDE_DIR - the Readline include directory (cached)
# READLINE_INCLUDE_DIRS - the Readline include directories
# (identical to READLINE_INCLUDE_DIR)
# READLINE_LIBRARY - the Readline library (cached)
# READLINE_LIBRARIES - the Readline library plus the libraries it
# depends on
# Copyright (C) 2009
# ASTRON (Netherlands Institute for Radio Astronomy)
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This program is free software; you can redistribute it and/or modify
# 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# $Id: FindReadline.cmake 15228 2010-03-16 09:27:26Z loose $
if(NOT READLINE_FOUND)
find_path(READLINE_INCLUDE_DIR readline/readline.h
HINTS ${READLINE_ROOT_DIR} PATH_SUFFIXES include)
find_library(READLINE_LIBRARY readline
HINTS ${READLINE_ROOT_DIR} PATH_SUFFIXES lib)
find_library(NCURSES_LIBRARY ncurses) # readline depends on libncurses
mark_as_advanced(READLINE_INCLUDE_DIR READLINE_LIBRARY NCURSES_LIBRARY)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Readline DEFAULT_MSG
READLINE_LIBRARY NCURSES_LIBRARY READLINE_INCLUDE_DIR)
set(READLINE_INCLUDE_DIRS ${READLINE_INCLUDE_DIR})
set(READLINE_LIBRARIES ${READLINE_LIBRARY} ${NCURSES_LIBRARY})
endif(NOT READLINE_FOUND)
...@@ -3,9 +3,9 @@ add_executable (dhtnode dhtnode.cpp tools_common.h) ...@@ -3,9 +3,9 @@ add_executable (dhtnode dhtnode.cpp tools_common.h)
add_executable (dhtscanner dhtscanner.cpp tools_common.h) add_executable (dhtscanner dhtscanner.cpp tools_common.h)
add_executable (dhtchat dhtchat.cpp tools_common.h) add_executable (dhtchat dhtchat.cpp tools_common.h)
target_link_libraries (dhtnode LINK_PUBLIC opendht gnutls) target_link_libraries (dhtnode LINK_PUBLIC opendht gnutls readline)
target_link_libraries (dhtscanner LINK_PUBLIC opendht gnutls) target_link_libraries (dhtscanner LINK_PUBLIC opendht gnutls readline)
target_link_libraries (dhtchat LINK_PUBLIC opendht gnutls) target_link_libraries (dhtchat LINK_PUBLIC opendht gnutls readline)
if (NOT DEFINED CMAKE_INSTALL_BINDIR) if (NOT DEFINED CMAKE_INSTALL_BINDIR)
set(CMAKE_INSTALL_BINDIR bin) set(CMAKE_INSTALL_BINDIR bin)
......
...@@ -72,18 +72,20 @@ main(int argc, char **argv) ...@@ -72,18 +72,20 @@ main(int argc, char **argv)
InfoHash room; InfoHash room;
const InfoHash myid = dht.getId(); const InfoHash myid = dht.getId();
// using the GNU History API
using_history();
while (true) while (true)
{ {
std::cout << (connected ? ">> " : "> "); // using the GNU Readline API
std::string line; std::string line = readLine(connected ? PROMPT : "> ");
std::getline(std::cin, line); if (!line.empty() && line[0] == '\0')
static constexpr dht::InfoHash INVALID_ID {};
if (std::cin.eof())
break; break;
if (line.empty()) if (line.empty())
continue; continue;
static constexpr dht::InfoHash INVALID_ID {};
std::istringstream iss(line); std::istringstream iss(line);
std::string op, idstr; std::string op, idstr;
iss >> op; iss >> op;
......
...@@ -114,16 +114,21 @@ main(int argc, char **argv) ...@@ -114,16 +114,21 @@ main(int argc, char **argv)
print_node_info(dht, params); print_node_info(dht, params);
std::cout << " (type 'h' or 'help' for a list of possible commands)" << std::endl << std::endl; std::cout << " (type 'h' or 'help' for a list of possible commands)" << std::endl << std::endl;
// using the GNU History API
using_history();
while (true) while (true)
{ {
std::cout << ">> "; // using the GNU Readline API
std::string line; std::string line = readLine();
std::getline(std::cin, line); if (!line.empty() && line[0] == '\0')
break;
std::istringstream iss(line); std::istringstream iss(line);
std::string op, idstr, value; std::string op, idstr, value;
iss >> op >> idstr; iss >> op >> idstr;
if (std::cin.eof() || op == "x" || op == "q" || op == "exit" || op == "quit") { if (op == "x" || op == "q" || op == "exit" || op == "quit") {
break; break;
} else if (op == "h" || op == "help") { } else if (op == "h" || op == "help") {
print_help(); print_help();
......
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
#include <opendht.h> #include <opendht.h>
#include <getopt.h> #include <getopt.h>
#include <readline/readline.h>
#include <readline/history.h>
/** /**
* Terminal colors for logging * Terminal colors for logging
...@@ -202,3 +204,15 @@ parseArgs(int argc, char **argv) { ...@@ -202,3 +204,15 @@ parseArgs(int argc, char **argv) {
} }
return params; return params;
} }
static const constexpr char* PROMPT = ">> ";
std::string
readLine(const char* prefix = PROMPT)
{
const char* line_read = readline(prefix);
if (line_read && *line_read)
add_history(line_read);
return line_read ? std::string(line_read) : std::string("\0", 1);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment