master_ctl: support using readline optional.

This commit is contained in:
zhengshuxin 2017-11-07 13:25:54 +08:00
parent 056cb8d060
commit e7800ce7c9
2 changed files with 27 additions and 3 deletions

View File

@ -9,7 +9,7 @@ CFLAGS = -c -g -W -Wall -Wcast-qual -Wcast-align \
########################################################### ###########################################################
#Check system: #Check system:
# Linux, SunOS, Solaris, BSD variants, AIX, HP-UX # Linux, SunOS, Solaris, BSD variants, AIX, HP-UX
SYSLIB = -lreadline -lcurses -lpthread -lz SYSLIB = -lpthread -lz
CHECKSYSRES = @echo "Unknow system type!";exit 1 CHECKSYSRES = @echo "Unknow system type!";exit 1
OSNAME = $(shell uname -s) OSNAME = $(shell uname -s)
OSTYPE = $(shell uname -m) OSTYPE = $(shell uname -m)
@ -20,20 +20,28 @@ endif
ifeq ($(findstring on, $(HAS_READLINE)), on) ifeq ($(findstring on, $(HAS_READLINE)), on)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring ON, $(HAS_READLINE)), ON) else ifeq ($(findstring ON, $(HAS_READLINE)), ON)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring yes, $(HAS_READLINE)), yes) else ifeq ($(findstring yes, $(HAS_READLINE)), yes)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring YES, $(HAS_READLINE)), YES) else ifeq ($(findstring YES, $(HAS_READLINE)), YES)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring on, $(has_readline)), on) else ifeq ($(findstring on, $(has_readline)), on)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring ON, $(has_readline)), ON) else ifeq ($(findstring ON, $(has_readline)), ON)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring yes, $(has_readline)), yes) else ifeq ($(findstring yes, $(has_readline)), yes)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
else ifeq ($(findstring YES, $(has_readline)), YES) else ifeq ($(findstring YES, $(has_readline)), YES)
CFLAGS += -DHAS_READLINE CFLAGS += -DHAS_READLINE
SYSLIB += -lreadline -lcurses
endif endif
# For FreeBSD # For FreeBSD
@ -148,7 +156,7 @@ all: RM $(OBJ)
@echo "" @echo ""
RM: RM:
rm -f $(PROG) rm -f $(PROG)
clean: clean cl:
rm -f $(PROG) rm -f $(PROG)
rm -f $(OBJ) rm -f $(OBJ)
rebuild rb: clean all rebuild rb: clean all

View File

@ -1,6 +1,10 @@
#include "stdafx.h" #include "stdafx.h"
#ifdef HAS_READLINE
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#endif
#include "http_request.h" #include "http_request.h"
static bool __verbose = false; static bool __verbose = false;
@ -508,7 +512,9 @@ static bool do_quit(const std::vector<acl::string>&, const char*, const char*)
static bool do_clear(const std::vector<acl::string>&, const char*, const char*) static bool do_clear(const std::vector<acl::string>&, const char*, const char*)
{ {
#if !defined(__APPLE__) #if !defined(__APPLE__)
#ifdef HAS_READLINE
rl_clear_screen(0, 0); rl_clear_screen(0, 0);
#endif
#endif #endif
printf("\r\n"); printf("\r\n");
return true; return true;
@ -516,17 +522,27 @@ static bool do_clear(const std::vector<acl::string>&, const char*, const char*)
static void getline(acl::string& out) static void getline(acl::string& out)
{ {
#ifdef HAS_READLINE
const char* prompt = "\033[1;34;40mmaster_ctl>\033[0m "; const char* prompt = "\033[1;34;40mmaster_ctl>\033[0m ";
char* ptr = readline(prompt); char* ptr = readline(prompt);
if (ptr == NULL) #else
printf("master_ctl> ");
fflush(stdout);
char buf[1024];
char* ptr = fgets(buf, (int) sizeof(buf), stdin);
#endif
if (ptr == NULL || *ptr == 0)
{ {
printf("Bye!\r\n"); printf("Bye!\r\n");
exit(0); exit(0);
} }
out = ptr; out = ptr;
out.trim_right_line(); out.trim_right_line();
#ifdef HAS_READLINE
if (!out.empty() && !out.equal("y", false) && !out.equal("n", false)) if (!out.empty() && !out.equal("y", false) && !out.equal("n", false))
add_history(out.c_str()); add_history(out.c_str());
#endif
} }
static struct { static struct {