From 48e96b2f3b96247c0d8beffe404c4c714705979a Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 7 Sep 2009 20:52:31 +0000 Subject: [PATCH] Add readline support to eval. --- CMakeLists.txt | 34 +++++++++++++++++++++++++++------- src/main.cpp | 31 ++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22fdb19..851e983 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,22 +2,42 @@ cmake_minimum_required(VERSION 2.6) project(chaiscript) -SET (CMAKE_BUILD_TYPE gdb) -SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb") -SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb") +FIND_LIBRARY(READLINE_LIBRARY NAMES readline PATH /usr/lib /usr/local/lib /opt/local/lib) -#SET (CMAKE_BUILD_TYPE rel) -#SET (CMAKE_C_FLAGS_REL " -Wall -O3") -#SET (CMAKE_CXX_FLAGS_REL " -Wall -O3") +MESSAGE(STATUS "Detecting readline support") +if (READLINE_LIBRARY) + MESSAGE(STATUS "Found: ${READLINE_LIBRARY}") + + SET (READLINE_LIB readline) + SET (CMAKE_BUILD_TYPE gdb) + SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb -DREADLINE_AVAILABLE") + SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb -DREADLINE_AVAILABLE") + + #SET (CMAKE_BUILD_TYPE rel) + #SET (CMAKE_C_FLAGS_REL " -Wall -O3 -DREADLINE_AVAILABLE") + #SET (CMAKE_CXX_FLAGS_REL " -Wall -O3 -DREADLINE_AVAILABLE") +else(READLINE_LIBRARY) + MESSAGE(STATUS "Not Found") + + SET (READLINE_LIB ) + SET (CMAKE_BUILD_TYPE gdb) + SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb") + SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb") + + #SET (CMAKE_BUILD_TYPE rel) + #SET (CMAKE_C_FLAGS_REL " -Wall -O3") + #SET (CMAKE_CXX_FLAGS_REL " -Wall -O3") +endif(READLINE_LIBRARY) include_directories(include) find_package(Boost 1.36.0 COMPONENTS thread) + if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) add_executable(chaiscript_eval src/main.cpp) #add_executable(dispatchkit_test contrib/test/dispatchkit_test.cpp) - target_link_libraries(chaiscript_eval dl ${Boost_LIBRARIES}) + target_link_libraries(chaiscript_eval dl ${Boost_LIBRARIES} ${READLINE_LIB}) add_library(test MODULE src/test_module.cpp) target_link_libraries(test ${Boost_LIBRARIES}) diff --git a/src/main.cpp b/src/main.cpp index 8daba2c..178c2a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,11 @@ #include +#ifdef READLINE_AVAILABLE +#include +#include +#endif + #include void print_help() { @@ -17,16 +22,33 @@ void print_help() { std::cout << " dump_object(x) - dumps information about the given symbol" << std::endl; } +std::string get_next_command() { +#ifdef READLINE_AVAILABLE + char *input_raw; + input_raw = readline("eval> "); + add_history(input_raw); + return std::string(input_raw); +#else + std::string retval; + std::cout << "eval> "; + std::getline(std::cin, retval); + return retval; +#endif +} + int main(int argc, char *argv[]) { std::string input; chaiscript::ChaiScript chai; chai.add(chaiscript::bootstrap::list_type >("List")); - if (argc < 2) { - std::cout << "eval> "; - std::getline(std::cin, input); + //std::cout << "eval> "; + //std::getline(std::cin, input); +#ifdef READLINE_AVAILABLE + using_history(); +#endif + input = get_next_command(); while (input != "quit") { chaiscript::Boxed_Value val; @@ -54,8 +76,7 @@ int main(int argc, char *argv[]) { } } - std::cout << "eval> "; - std::getline(std::cin, input); + input = get_next_command(); } } else {