[Groonga-commit] groonga/groonga [master] support comment line in grn commands. #723

Back to archive index

null+****@clear***** null+****@clear*****
2010年 11月 24日 (水) 10:18:03 JST


Kouhei Sutou	2010-11-24 01:18:03 +0000 (Wed, 24 Nov 2010)

  New Revision: 64499052a20f189afed253a96639a7c37458d073

  Log:
    support comment line in grn commands. #723

  Added files:
    test/unit/command/test-comment.rb
  Modified files:
    lib/ctx.c
    src/groonga.c
    test/unit/command/Makefile.am

  Modified: lib/ctx.c (+26 -4)
===================================================================
--- lib/ctx.c    2010-12-08 02:46:06 +0000 (4db67ca)
+++ lib/ctx.c    2010-11-24 01:18:03 +0000 (61dcc43)
@@ -1142,6 +1142,26 @@ grn_ctx_sendv(grn_ctx *ctx, int argc, char **argv, int flags)
   return ctx->rc;
 }
 
+static int
+comment_command_p(const char *command, unsigned int length)
+{
+  const char *p, *e;
+
+  e = command + length;
+  for (p = command; p < e; p++) {
+    switch (*p) {
+    case '#' :
+      return GRN_TRUE;
+    case ' ' :
+    case '\t' :
+      break;
+    default :
+      return GRN_FALSE;
+    }
+  }
+  return GRN_FALSE;
+}
+
 unsigned int
 grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
 {
@@ -1167,7 +1187,8 @@ grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
       }
       goto exit;
     } else {
-      grn_obj *expr;
+      grn_obj *expr = NULL;
+      if (comment_command_p(str, str_len)) { goto output; };
       if (ctx->impl->qe_next) {
         grn_obj *val;
         expr = ctx->impl->qe_next;
@@ -1189,15 +1210,16 @@ grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
         }
       }
       if (ctx->stat == GRN_CTX_QUITTING) { ctx->stat = GRN_CTX_QUIT; }
+      if (!ctx->impl->qe_next) {
+        LAP("<", "rc=%d", ctx->rc);
+      }
+    output :
       if (!ERRP(ctx, GRN_CRIT)) {
         if (!(flags & GRN_CTX_QUIET) && ctx->impl->output) {
           ctx->impl->output(ctx, GRN_CTX_TAIL, ctx->impl->data.ptr);
         }
       }
       if (expr) { grn_expr_clear_vars(ctx, expr); }
-      if (!ctx->impl->qe_next) {
-        LAP("<", "rc=%d", ctx->rc);
-      }
       goto exit;
     }
   }

  Modified: src/groonga.c (+0 -22)
===================================================================
--- src/groonga.c    2010-12-08 02:46:06 +0000 (5f48971)
+++ src/groonga.c    2010-11-24 01:18:03 +0000 (82edfe8)
@@ -629,26 +629,6 @@ s_output(grn_ctx *ctx, int flags, void *arg)
 }
 
 static int
-comment_p(const char *line, int length)
-{
-  const char *p, *e;
-
-  e = line + length;
-  for (p = line; p < e; p++) {
-    switch (*p) {
-    case '#' :
-      return GRN_TRUE;
-    case ' ' :
-    case '\t' :
-      break;
-    default :
-      return GRN_FALSE;
-    }
-  }
-  return GRN_FALSE;
-}
-
-static int
 do_alone(int argc, char **argv)
 {
   int rc = -1;
@@ -669,7 +649,6 @@ do_alone(int argc, char **argv)
         int  len;
         while ((len = prompt(ctx, buf))) {
           uint32_t size = len - 1;
-          if (comment_p(buf, len)) { continue; }
           grn_ctx_send(ctx, buf, size, 0);
           if (ctx->stat == GRN_CTX_QUIT) { break; }
         }
@@ -741,7 +720,6 @@ g_client(int argc, char **argv)
         int   len;
         while ((len = prompt(ctx, buf))) {
           uint32_t size = len - 1;
-          if (comment_p(buf, len)) { continue; }
           grn_ctx_send(ctx, buf, size, 0);
           rc = ctx->rc;
           if (rc) { break; }

  Modified: test/unit/command/Makefile.am (+2 -1)
===================================================================
--- test/unit/command/Makefile.am    2010-12-08 02:46:06 +0000 (020194b)
+++ test/unit/command/Makefile.am    2010-11-24 01:18:03 +0000 (874333d)
@@ -1,4 +1,5 @@
 EXTRA_DIST =					\
 	test-config-file.rb			\
 	test-option.rb				\
-	test-option-bom.rb
+	test-option-bom.rb			\
+	test-comment.rb

  Added: test/unit/command/test-comment.rb (+45 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/command/test-comment.rb    2010-11-24 01:18:03 +0000 (e70ea96)
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2010  Kouhei Sutou <kou****@clear*****>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+class CommentTest < Test::Unit::TestCase
+  include GroongaTestUtils
+
+  def setup
+    setup_database_path
+    @input_file = File.join(@tmp_dir, "commands")
+  end
+
+  def teardown
+    teardown_database_path
+  end
+
+  def test_comment
+    open(@input_file, "w") do |file|
+      file.puts("# defrag")
+    end
+    assert_equal("",
+                 run_groonga("--file", @input_file, "-n", @database_path))
+  end
+
+  def test_comment_with_preceding_spaces
+    open(@input_file, "w") do |file|
+      file.puts("   # defrag")
+    end
+    assert_equal("",
+                 run_groonga("--file", @input_file, "-n", @database_path))
+  end
+end




Groonga-commit メーリングリストの案内
Back to archive index