null+****@clear*****
null+****@clear*****
2010年 6月 23日 (水) 14:41:38 JST
Yutaro Shimamura 2010-06-23 05:41:38 +0000 (Wed, 23 Jun 2010) New Revision: dfb4236d994a36c4f98f192db797469369a61014 Log: groonga.c using libedit. Modified files: configure.ac src/groonga.c Modified: configure.ac (+16 -1) =================================================================== --- configure.ac 2010-06-23 03:05:15 +0000 (0f12fd2) +++ configure.ac 2010-06-23 05:41:38 +0000 (3f30e00) @@ -446,6 +446,21 @@ fi AC_SUBST(RUBY) AM_CONDITIONAL([WITH_RUBY], [test "$ac_cv_ruby_available" = "yes"]) +# libedit +AC_ARG_WITH(libedit, + [AS_HELP_STRING([--with-libedit], + [use libedit for console. [default=yes]])], + [with_libedit="$withval"], + [with_libedit="yes"]) +AC_CHECK_LIB(edit,el_init, + [with_libedit="yes"], + [with_libedit="no"]) +if test "x$with_libedit" = "xyes"; then + AC_DEFINE(WITH_LIBEDIT, [1], [with libedit]) + EDIT_LIBS="-ledit" +fi + + # zlib AC_ARG_WITH(zlib, [AS_HELP_STRING([--with-zlib], @@ -555,7 +570,7 @@ AC_DEFINE_UNQUOTED(GRN_MODULE_SUFFIX, ["$suffix"], "module suffix") # flags for compile groonga CFLAGS="$CFLAGS $OPT_CFLAGS " CFLAGS="$CFLAGS -DMODULES_DIR=\\\"\"\$(modulesdir)\"\\\"" -LIBS="$LIBS $ZLIB_LIBS $LZO_LIBS $PTHREAD_LIBS $M_LIBS $NSL_LIBS $SOCKET_LIBS $WINDOWS_LIBS" +LIBS="$LIBS $ZLIB_LIBS $LZO_LIBS $PTHREAD_LIBS $M_LIBS $NSL_LIBS $SOCKET_LIBS $WINDOWS_LIBS $EDIT_LIBS" AC_DEFINE_UNQUOTED(CONFIGURE_OPTIONS, "$ac_configure_args", "specified configure options") # flags for groonga-cfg Modified: src/groonga.c (+58 -10) =================================================================== --- src/groonga.c 2010-06-23 03:05:15 +0000 (d59ef84) +++ src/groonga.c 2010-06-23 05:41:38 +0000 (5ea51d1) @@ -59,6 +59,20 @@ static int (*do_server)(char *path); static uint32_t default_max_nfthreads = DEFAULT_MAX_NFTHREADS; static const char *pidfile_path; +#ifdef WITH_LIBEDIT +#include <histedit.h> +static EditLine *el; +static History *elh; +static HistEvent elhv; + +inline static const char * +disp_prompt(EditLine *e __attribute__((unused))) +{ + return "> "; +} + +#endif + static void usage(FILE *output) { @@ -140,14 +154,35 @@ show_version(void) #endif } -inline static void -prompt(void) +#define BUFSIZE 0x1000000 + +inline static int +prompt(char *buf) { - if (!batchmode) { fputs("> ", stderr); } + int len; + if (!batchmode) { +#ifdef WITH_LIBEDIT + const char *es; + es = el_gets(el, &len); + if (len > 0 && BUFSIZE > len) { + history(elh, &elhv, H_ENTER, es); + strncpy(buf,es,len); + } else { + buf = ""; + len = 0; + } +#else + fprintf(stderr, "> "); + fgets(buf, BUFSIZE, stdin); + len = strlen(buf); +#endif + } else { + fgets(buf, BUFSIZE, stdin); + len = strlen(buf); + } + return len; } -#define BUFSIZE 0x1000000 - static int do_alone(int argc, char **argv) { @@ -166,9 +201,9 @@ do_alone(int argc, char **argv) rc = grn_bulk_reserve(ctx, &text, BUFSIZE); if (!rc) { char *buf = GRN_TEXT_VALUE(&text); - while ((prompt(), fgets(buf, BUFSIZE, stdin))) { - uint32_t size = strlen(buf) - 1; - buf[size] = '\0'; + int len; + while ((len = prompt(buf))) { + uint32_t size = len - 1; grn_ctx_send(ctx, buf, size, 0); if (ctx->stat == GRN_CTX_QUIT) { break; } } @@ -236,9 +271,10 @@ g_client(int argc, char **argv) rc = grn_bulk_reserve(ctx, &text, BUFSIZE); if (!rc) { char *buf = GRN_TEXT_VALUE(&text); + int len; if (batchmode) { BATCHMODE(ctx); } - while ((prompt(), fgets(buf, BUFSIZE, stdin))) { - uint32_t size = strlen(buf) - 1; + while ((len = prompt(buf))) { + uint32_t size = len - 1; grn_ctx_send(ctx, buf, size, 0); rc = ctx->rc; if (rc) { break; } @@ -1714,6 +1750,14 @@ main(int argc, char **argv) max_nfthreads = default_max_nfthreads; } batchmode = !isatty(0); +#ifdef WITH_LIBEDIT + el = el_init(argv[0],stdin,stdout,stderr); + el_set(el, EL_PROMPT, &disp_prompt); + el_set(el, EL_EDITOR, "emacs"); + elh = history_init(); + history(elh, &elhv, H_SETSIZE, 200); + el_set(el, EL_HIST, history, elh); +#endif if (grn_init()) { return -1; } grn_set_default_encoding(enc); if (loglevel) { SET_LOGLEVEL(atoi(loglevel)); } @@ -1765,6 +1809,10 @@ main(int argc, char **argv) usage(stderr); r = -1; break; } +#ifdef WITH_LIBEDIT + history_end(elh); + el_end(el); +#endif grn_fin(); return r; }