[Groonga-commit] groonga/groonga [master] output return code in stand alone mode.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 7月 12日 (月) 11:40:11 JST


Daijiro MORI	2010-07-12 02:40:11 +0000 (Mon, 12 Jul 2010)

  New Revision: 8aed54cd3c76b070dc8ba0c80e573f983872508e

  Log:
    output return code in stand alone mode.

  Modified files:
    lib/output.c
    src/groonga.c

  Modified: lib/output.c (+6 -6)
===================================================================
--- lib/output.c    2010-07-12 01:21:08 +0000 (645266f)
+++ lib/output.c    2010-07-12 02:40:11 +0000 (2197fd7)
@@ -48,9 +48,9 @@ put_delimiter(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type)
     break;
   case GRN_CONTENT_TSV:
     if (level < 2) { return; }
-    if (DEPTH == 1) {
+    if (DEPTH <= 2) {
       GRN_TEXT_PUTC(ctx, outbuf, ((level & 3) == 3) ? '\t' : '\n');
-    } else if (DEPTH > 1) {
+    } else {
       GRN_TEXT_PUTC(ctx, outbuf, '\t');
     }
   case GRN_CONTENT_MSGPACK :
@@ -77,7 +77,7 @@ grn_output_array_open(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_typ
     grn_vector_add_element(ctx, &ctx->impl->names, name, strlen(name), 0, GRN_DB_SHORT_TEXT);
     break;
   case GRN_CONTENT_TSV:
-    if (DEPTH > 1) { GRN_TEXT_PUTS(ctx, outbuf, "[\t"); }
+    if (DEPTH > 2) { GRN_TEXT_PUTS(ctx, outbuf, "[\t"); }
     break;
   case GRN_CONTENT_MSGPACK :
     // todo
@@ -96,7 +96,7 @@ grn_output_array_close(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_ty
     GRN_TEXT_PUTC(ctx, outbuf, ']');
     break;
   case GRN_CONTENT_TSV:
-    if (DEPTH > 2) {
+    if (DEPTH > 3) {
       if (CURR_LEVEL >= 2) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
       GRN_TEXT_PUTC(ctx, outbuf, ']');
     }
@@ -136,7 +136,7 @@ grn_output_map_open(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
     grn_vector_add_element(ctx, &ctx->impl->names, name, strlen(name), 0, GRN_DB_SHORT_TEXT);
     break;
   case GRN_CONTENT_TSV:
-    if (DEPTH > 1) { GRN_TEXT_PUTS(ctx, outbuf, "{\t"); }
+    if (DEPTH > 2) { GRN_TEXT_PUTS(ctx, outbuf, "{\t"); }
     break;
   case GRN_CONTENT_MSGPACK :
     // todo
@@ -155,7 +155,7 @@ grn_output_map_close(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type
     GRN_TEXT_PUTS(ctx, outbuf, "}");
     break;
   case GRN_CONTENT_TSV:
-    if (DEPTH > 2) {
+    if (DEPTH > 3) {
       if (CURR_LEVEL >= 2) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
       GRN_TEXT_PUTC(ctx, outbuf, '}');
     }

  Modified: src/groonga.c (+141 -117)
===================================================================
--- src/groonga.c    2010-07-12 01:21:08 +0000 (06136c6)
+++ src/groonga.c    2010-07-12 02:40:11 +0000 (3bca79a)
@@ -201,122 +201,6 @@ prompt(char *buf)
   return len;
 }
 
-static int
-do_alone(int argc, char **argv)
-{
-  int rc = -1;
-  char *path = NULL;
-  grn_obj *db;
-  grn_ctx ctx_, *ctx = &ctx_;
-  grn_ctx_init(ctx, (useql ? GRN_CTX_USE_QL : 0)|(batchmode ? GRN_CTX_BATCH_MODE : 0));
-  if (argc > 0 && argv) { path = *argv++; argc--; }
-  db = (newdb || !path) ? grn_db_create(ctx, path, NULL) : grn_db_open(ctx, path);
-  if (db) {
-    grn_ctx_recv_handler_set(ctx, grn_ctx_stream_out_func, stdout);
-    if (!argc) {
-      grn_obj text;
-      GRN_TEXT_INIT(&text, 0);
-      rc = grn_bulk_reserve(ctx, &text, BUFSIZE);
-      if (!rc) {
-        char *buf = GRN_TEXT_VALUE(&text);
-        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; }
-        }
-        rc = ctx->rc;
-      } else {
-        fprintf(stderr, "grn_bulk_reserve() failed (%d): %d\n", BUFSIZE, rc);
-      }
-      grn_obj_unlink(ctx, &text);
-    } else {
-      rc = grn_ctx_sendv(ctx, argc, argv, 0);
-    }
-    grn_obj_close(ctx, db);
-  } else {
-    fprintf(stderr, "db open failed (%s): %s\n", path, ctx->errbuf);
-  }
-  grn_ctx_fin(ctx);
-  return rc;
-}
-
-#define BATCHMODE(ctx) do {\
-  int flags;\
-  unsigned int str_len;\
-  char *str, *query = "(batchmode #t)";\
-  grn_ctx_send(ctx, query, strlen(query), 0);\
-  do {\
-    if (grn_ctx_recv(ctx, &str, &str_len, &flags)) {\
-      fprintf(stderr, "grn_ctx_recv failed\n");\
-    }\
-  } while ((flags & GRN_CTX_MORE));\
-} while (0)
-
-static int
-recvput(grn_ctx *ctx)
-{
-  int flags;
-  char *str;
-  unsigned int str_len;
-  do {
-    grn_ctx_recv(ctx, &str, &str_len, &flags);
-    if (ctx->rc) {
-      fprintf(stderr, "grn_ctx_recv failed\n");
-      return -1;
-    }
-    if (str_len) {
-      fwrite(str, 1, str_len, stdout);
-      putchar('\n');
-      fflush(stdout);
-    }
-  } while ((flags & GRN_CTX_MORE));
-  return 0;
-}
-
-static int
-g_client(int argc, char **argv)
-{
-  int rc = -1;
-  grn_ctx ctx_, *ctx = &ctx_;
-  char *hostname = DEFAULT_DEST;
-  if (argc > 0 && argv) { hostname = *argv++; argc--; }
-  grn_ctx_init(ctx, (batchmode ? GRN_CTX_BATCH_MODE : 0));
-  if (!grn_ctx_connect(ctx, hostname, port, 0)) {
-    if (!argc) {
-      grn_obj text;
-      GRN_TEXT_INIT(&text, 0);
-      rc = grn_bulk_reserve(ctx, &text, BUFSIZE);
-      if (!rc) {
-        char *buf = GRN_TEXT_VALUE(&text);
-        int   len;
-        if (batchmode) { BATCHMODE(ctx); }
-        while ((len = prompt(buf))) {
-          uint32_t size = len - 1;
-          grn_ctx_send(ctx, buf, size, 0);
-          rc = ctx->rc;
-          if (rc) { break; }
-          if (recvput(ctx)) { goto exit; }
-          if (ctx->stat == GRN_CTX_QUIT) { break; }
-        }
-      } else {
-        fprintf(stderr, "grn_bulk_reserve() failed (%d): %d\n", BUFSIZE, rc);
-      }
-      grn_obj_unlink(ctx, &text);
-    } else {
-      rc = grn_ctx_sendv(ctx, argc, argv, 0);
-      if (recvput(ctx)) { goto exit; }
-    }
-  } else {
-    fprintf(stderr, "grn_ctx_connect failed (%s:%d)\n", hostname, port);
-  }
-exit :
-  grn_ctx_fin(ctx);
-  return rc;
-}
-
-/* server */
-
 typedef enum {
   grn_http_request_type_none = 0,
   grn_http_request_type_get,
@@ -389,7 +273,8 @@ print_return_code(grn_ctx *ctx, grn_rc rc, grn_obj *head, grn_obj *body, grn_obj
         grn_text_itoa(ctx, head, ctx->errline);
       }
     }
-    GRN_TEXT_PUTS(ctx, head, "\nEND\n");
+    GRN_TEXT_PUTS(ctx, head, "\n");
+    GRN_TEXT_PUTS(ctx, foot, "\nEND");
     break;
   case GRN_CONTENT_XML:
     GRN_TEXT_PUTS(ctx, head, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<RESULT CODE=\"");
@@ -433,6 +318,145 @@ 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) {
+    grn_obj *buf = ctx->impl->outbuf;
+    if (GRN_TEXT_LEN(buf)) {
+      FILE * stream = (FILE *) arg;
+      grn_obj head, foot;
+      GRN_TEXT_INIT(&head, 0);
+      GRN_TEXT_INIT(&foot, 0);
+      print_return_code(ctx, ctx->rc, &head, buf, &foot);
+      fwrite(GRN_TEXT_VALUE(&head), 1, GRN_TEXT_LEN(&head), stream);
+      fwrite(GRN_TEXT_VALUE(buf), 1, GRN_TEXT_LEN(buf), stream);
+      fwrite(GRN_TEXT_VALUE(&foot), 1, GRN_TEXT_LEN(&foot), stream);
+      fputc('\n', stream);
+      fflush(stream);
+      GRN_BULK_REWIND(buf);
+      GRN_OBJ_FIN(ctx, &head);
+      GRN_OBJ_FIN(ctx, &foot);
+    }
+  }
+}
+
+static int
+do_alone(int argc, char **argv)
+{
+  int rc = -1;
+  char *path = NULL;
+  grn_obj *db;
+  grn_ctx ctx_, *ctx = &ctx_;
+  grn_ctx_init(ctx, (useql ? GRN_CTX_USE_QL : 0)|(batchmode ? GRN_CTX_BATCH_MODE : 0));
+  if (argc > 0 && argv) { path = *argv++; argc--; }
+  db = (newdb || !path) ? grn_db_create(ctx, path, NULL) : grn_db_open(ctx, path);
+  if (db) {
+    grn_ctx_recv_handler_set(ctx, s_output, stdout);
+    if (!argc) {
+      grn_obj text;
+      GRN_TEXT_INIT(&text, 0);
+      rc = grn_bulk_reserve(ctx, &text, BUFSIZE);
+      if (!rc) {
+        char *buf = GRN_TEXT_VALUE(&text);
+        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; }
+        }
+        rc = ctx->rc;
+      } else {
+        fprintf(stderr, "grn_bulk_reserve() failed (%d): %d\n", BUFSIZE, rc);
+      }
+      grn_obj_unlink(ctx, &text);
+    } else {
+      rc = grn_ctx_sendv(ctx, argc, argv, 0);
+    }
+    grn_obj_close(ctx, db);
+  } else {
+    fprintf(stderr, "db open failed (%s): %s\n", path, ctx->errbuf);
+  }
+  grn_ctx_fin(ctx);
+  return rc;
+}
+
+#define BATCHMODE(ctx) do {\
+  int flags;\
+  unsigned int str_len;\
+  char *str, *query = "(batchmode #t)";\
+  grn_ctx_send(ctx, query, strlen(query), 0);\
+  do {\
+    if (grn_ctx_recv(ctx, &str, &str_len, &flags)) {\
+      fprintf(stderr, "grn_ctx_recv failed\n");\
+    }\
+  } while ((flags & GRN_CTX_MORE));\
+} while (0)
+
+static int
+recvput(grn_ctx *ctx)
+{
+  int flags;
+  char *str;
+  unsigned int str_len;
+  do {
+    grn_ctx_recv(ctx, &str, &str_len, &flags);
+    if (ctx->rc) {
+      fprintf(stderr, "grn_ctx_recv failed\n");
+      return -1;
+    }
+    if (str_len) {
+      fwrite(str, 1, str_len, stdout);
+      putchar('\n');
+      fflush(stdout);
+    }
+  } while ((flags & GRN_CTX_MORE));
+  return 0;
+}
+
+static int
+g_client(int argc, char **argv)
+{
+  int rc = -1;
+  grn_ctx ctx_, *ctx = &ctx_;
+  char *hostname = DEFAULT_DEST;
+  if (argc > 0 && argv) { hostname = *argv++; argc--; }
+  grn_ctx_init(ctx, (batchmode ? GRN_CTX_BATCH_MODE : 0));
+  if (!grn_ctx_connect(ctx, hostname, port, 0)) {
+    if (!argc) {
+      grn_obj text;
+      GRN_TEXT_INIT(&text, 0);
+      rc = grn_bulk_reserve(ctx, &text, BUFSIZE);
+      if (!rc) {
+        char *buf = GRN_TEXT_VALUE(&text);
+        int   len;
+        if (batchmode) { BATCHMODE(ctx); }
+        while ((len = prompt(buf))) {
+          uint32_t size = len - 1;
+          grn_ctx_send(ctx, buf, size, 0);
+          rc = ctx->rc;
+          if (rc) { break; }
+          if (recvput(ctx)) { goto exit; }
+          if (ctx->stat == GRN_CTX_QUIT) { break; }
+        }
+      } else {
+        fprintf(stderr, "grn_bulk_reserve() failed (%d): %d\n", BUFSIZE, rc);
+      }
+      grn_obj_unlink(ctx, &text);
+    } else {
+      rc = grn_ctx_sendv(ctx, argc, argv, 0);
+      if (recvput(ctx)) { goto exit; }
+    }
+  } else {
+    fprintf(stderr, "grn_ctx_connect failed (%s:%d)\n", hostname, port);
+  }
+exit :
+  grn_ctx_fin(ctx);
+  return rc;
+}
+
+/* server */
+
 #define JSON_CALLBACK_PARAM "callback"
 
 typedef struct {




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