[Groonga-commit] groonga/groonga [master] Returns error when invalid command name is assinged.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 7月 12日 (月) 21:20:28 JST


Daijiro MORI	2010-07-12 12:20:28 +0000 (Mon, 12 Jul 2010)

  New Revision: 8c90299113a09f4dfdcbf38798897ed163022d0a

  Log:
    Returns error when invalid command name is assinged.

  Modified files:
    lib/ctx.c
    lib/proc.c
    src/groonga.c

  Modified: lib/ctx.c (+39 -33)
===================================================================
--- lib/ctx.c    2010-07-12 10:33:16 +0000 (99a233c)
+++ lib/ctx.c    2010-07-12 12:20:28 +0000 (bb18e9d)
@@ -907,46 +907,52 @@ grn_ctx_qe_exec(grn_ctx *ctx, const char *str, uint32_t str_len)
   const char *p = str, *e = str + str_len, *v;
   GRN_TEXT_INIT(&buf, 0);
   p = grn_text_unesc_tok(ctx, &buf, p, e, &tok_type);
-  if ((expr = grn_ctx_get(ctx, GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf)))) {
-    while (p < e) {
-      GRN_BULK_REWIND(&buf);
-      p = grn_text_unesc_tok(ctx, &buf, p, e, &tok_type);
-      v = GRN_TEXT_VALUE(&buf);
-      switch (tok_type) {
-      case GRN_TOK_VOID :
-        p = e;
-        break;
-      case GRN_TOK_SYMBOL :
-        if (GRN_TEXT_LEN(&buf) > 2 && v[0] == '-' && v[1] == '-') {
-          int l = GRN_TEXT_LEN(&buf) - 2;
-          v += 2;
-          if (l == OUTPUT_TYPE_LEN && !memcmp(v, OUTPUT_TYPE, OUTPUT_TYPE_LEN)) {
-            GRN_BULK_REWIND(&buf);
-            p = grn_text_unesc_tok(ctx, &buf, p, e, &tok_type);
-            v = GRN_TEXT_VALUE(&buf);
-            get_content_mime_type(ctx, v, GRN_BULK_CURR(&buf));
-          } else if ((val = grn_expr_get_or_add_var(ctx, expr, v, l))) {
-            grn_obj_reinit(ctx, val, GRN_DB_TEXT, 0);
-            p = grn_text_unesc_tok(ctx, val, p, e, &tok_type);
-          } else {
-            p = e;
-          }
-          break;
-        }
-        // fallthru
-      case GRN_TOK_STRING :
-      case GRN_TOK_QUOTE :
-        if ((val = grn_expr_get_var_by_offset(ctx, expr, offset++))) {
+  expr = grn_ctx_get(ctx, GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf));
+  while (p < e) {
+    GRN_BULK_REWIND(&buf);
+    p = grn_text_unesc_tok(ctx, &buf, p, e, &tok_type);
+    v = GRN_TEXT_VALUE(&buf);
+    switch (tok_type) {
+    case GRN_TOK_VOID :
+      p = e;
+      break;
+    case GRN_TOK_SYMBOL :
+      if (GRN_TEXT_LEN(&buf) > 2 && v[0] == '-' && v[1] == '-') {
+        int l = GRN_TEXT_LEN(&buf) - 2;
+        v += 2;
+        if (l == OUTPUT_TYPE_LEN && !memcmp(v, OUTPUT_TYPE, OUTPUT_TYPE_LEN)) {
+          GRN_BULK_REWIND(&buf);
+          p = grn_text_unesc_tok(ctx, &buf, p, e, &tok_type);
+          v = GRN_TEXT_VALUE(&buf);
+          get_content_mime_type(ctx, v, GRN_BULK_CURR(&buf));
+        } else if (expr && (val = grn_expr_get_or_add_var(ctx, expr, v, l))) {
           grn_obj_reinit(ctx, val, GRN_DB_TEXT, 0);
-          GRN_TEXT_PUT(ctx, val, GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf));
+          p = grn_text_unesc_tok(ctx, val, p, e, &tok_type);
         } else {
           p = e;
         }
         break;
       }
+      // fallthru
+    case GRN_TOK_STRING :
+    case GRN_TOK_QUOTE :
+      if (expr && (val = grn_expr_get_var_by_offset(ctx, expr, offset++))) {
+        grn_obj_reinit(ctx, val, GRN_DB_TEXT, 0);
+        GRN_TEXT_PUT(ctx, val, GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf));
+      } else {
+        p = e;
+      }
+      break;
     }
-    ctx->impl->curr_expr = expr;
+  }
+  ctx->impl->curr_expr = expr;
+  if (expr) {
     grn_expr_exec(ctx, expr, 0);
+  } else {
+    GRN_BULK_REWIND(&buf);
+    grn_text_unesc_tok(ctx, &buf, str, str + str_len, &tok_type);
+    ERR(GRN_INVALID_ARGUMENT, "invalid command name: %.*s",
+        GRN_TEXT_LEN(&buf), GRN_TEXT_VALUE(&buf));
   }
   GRN_OBJ_FIN(ctx, &buf);
   return expr;
@@ -1017,7 +1023,7 @@ 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 (!ERRP(ctx, GRN_CRIT)) {
         if (!(flags & GRN_CTX_QUIET) && ctx->impl->output) {
-          ctx->impl->output(ctx, 0, ctx->impl->data.ptr);
+          ctx->impl->output(ctx, GRN_CTX_TAIL, ctx->impl->data.ptr);
         }
       }
       if (expr) { grn_expr_clear_vars(ctx, expr); }

  Modified: lib/proc.c (+2 -0)
===================================================================
--- lib/proc.c    2010-07-12 10:33:16 +0000 (c639f79)
+++ lib/proc.c    2010-07-12 12:20:28 +0000 (f240317)
@@ -286,6 +286,8 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
     }
     if (taintable) { grn_db_touch(ctx, DB_OBJ(table_)->db); }
     grn_obj_unlink(ctx, table_);
+  } else {
+    ERR(GRN_INVALID_ARGUMENT, "invalid table name: %.*s", table_len, table);
   }
   /* GRN_LOG(ctx, GRN_LOG_NONE, "%d", ctx->seqno); */
   return ctx->rc;

  Modified: src/groonga.c (+9 -8)
===================================================================
--- src/groonga.c    2010-07-12 10:33:16 +0000 (596386d)
+++ src/groonga.c    2010-07-12 12:20:28 +0000 (dcf73ab)
@@ -321,9 +321,9 @@ print_return_code(grn_ctx *ctx, grn_rc rc, grn_obj *head, grn_obj *body, grn_obj
 static void
 s_output(grn_ctx *ctx, int flags, void *arg)
 {
-  if (ctx && ctx->impl) {
+  if (ctx && ctx->impl && (flags & GRN_CTX_TAIL)) {
     grn_obj *buf = ctx->impl->outbuf;
-    if (GRN_TEXT_LEN(buf)) {
+    if (GRN_TEXT_LEN(buf) || ctx->rc) {
       FILE * stream = (FILE *) arg;
       grn_obj head, foot;
       GRN_TEXT_INIT(&head, 0);
@@ -394,7 +394,7 @@ do_alone(int argc, char **argv)
 } while (0)
 
 static int
-g_output(grn_ctx *ctx)
+c_output(grn_ctx *ctx)
 {
   int flags;
   char *str;
@@ -407,7 +407,7 @@ g_output(grn_ctx *ctx)
       return -1;
     }
     */
-    if (str_len) {
+    if (str_len || ctx->rc) {
       grn_obj head, body, foot;
       GRN_TEXT_INIT(&head, 0);
       GRN_TEXT_INIT(&body, GRN_OBJ_DO_SHALLOW_COPY);
@@ -449,7 +449,7 @@ g_client(int argc, char **argv)
           grn_ctx_send(ctx, buf, size, 0);
           rc = ctx->rc;
           if (rc) { break; }
-          if (g_output(ctx)) { goto exit; }
+          if (c_output(ctx)) { goto exit; }
           if (ctx->stat == GRN_CTX_QUIT) { break; }
         }
       } else {
@@ -458,7 +458,7 @@ g_client(int argc, char **argv)
       grn_obj_unlink(ctx, &text);
     } else {
       rc = grn_ctx_sendv(ctx, argc, argv, 0);
-      if (g_output(ctx)) { goto exit; }
+      if (c_output(ctx)) { goto exit; }
     }
   } else {
     fprintf(stderr, "grn_ctx_connect failed (%s:%d)\n", hostname, port);
@@ -489,6 +489,7 @@ h_output(grn_ctx *ctx, int flags, void *arg)
   grn_obj *body = &hc->body;
   const char *mime_type = ctx->impl->mime_type;
   grn_obj head, foot, *outbuf = ctx->impl->outbuf;
+  if (!(flags & GRN_CTX_TAIL)) { return; }
   GRN_TEXT_INIT(&head, 0);
   GRN_TEXT_INIT(&foot, 0);
   if (!expr_rc) {
@@ -1412,7 +1413,7 @@ dispatcher(grn_ctx *ctx, grn_edge *edge)
 }
 
 static void
-output(grn_ctx *ctx, int flags, void *arg)
+g_output(grn_ctx *ctx, int flags, void *arg)
 {
   grn_edge *edge = arg;
   grn_com *com = edge->com;
@@ -1454,7 +1455,7 @@ g_handler(grn_ctx *ctx, grn_obj *msg)
       GRN_COM_QUEUE_INIT(&edge->recv_new);
       GRN_COM_QUEUE_INIT(&edge->send_old);
       grn_ctx_use(&edge->ctx, (grn_obj *)com->ev->opaque);
-      grn_ctx_recv_handler_set(&edge->ctx, output, edge);
+      grn_ctx_recv_handler_set(&edge->ctx, g_output, edge);
       com->opaque = edge;
       grn_obj_close(&edge->ctx, edge->ctx.impl->outbuf);
       edge->ctx.impl->outbuf = grn_msg_open(&edge->ctx, com, &edge->send_old);




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