s4-ldb: make ldb tools line buffered
[ira/wip.git] / source4 / lib / ldb / tools / cmdline.c
index aff730c1780f3556fe0202b8e1ff19718833e799..f0cd0a3a60c3b93bac0934dc24abfd4ceca652e4 100644 (file)
@@ -22,6 +22,7 @@
 */
 
 #include "ldb_includes.h"
+#include "ldb.h"
 #include "tools/cmdline.h"
 
 #if (_SAMBA_BUILD_ >= 4)
 #include "param/param.h"
 #endif
 
+static struct ldb_cmdline options; /* needs to be static for older compilers */
+
+static struct poptOption popt_options[] = {
+       POPT_AUTOHELP
+       { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
+       { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
+       { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
+       { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
+       { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
+       { "trace",     0,   POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
+       { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
+       { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
+       { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
+       { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
+       { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
+       { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
+       { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
+       { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
+       { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
+       { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
+       { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
+       { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
+       { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
+#if (_SAMBA_BUILD_ >= 4)
+       POPT_COMMON_SAMBA
+       POPT_COMMON_CREDENTIALS
+       POPT_COMMON_CONNECTION
+       POPT_COMMON_VERSION
+#endif
+       { NULL }
+};
 
+void ldb_cmdline_help(const char *cmdname, FILE *f)
+{
+       poptContext pc;
+       pc = poptGetContext(cmdname, 0, NULL, popt_options, 
+                           POPT_CONTEXT_KEEP_FIRST);
+       poptPrintHelp(pc, f, 0);
+}
 
 /**
   process command line options
@@ -43,7 +82,6 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
                                        int argc, const char **argv,
                                        void (*usage)(void))
 {
-       static struct ldb_cmdline options; /* needs to be static for older compilers */
        struct ldb_cmdline *ret=NULL;
        poptContext pc;
 #if (_SAMBA_BUILD_ >= 4)
@@ -53,35 +91,6 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
        int opt;
        int flags = 0;
 
-       struct poptOption popt_options[] = {
-               POPT_AUTOHELP
-               { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
-               { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
-               { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
-               { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
-               { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
-               { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
-               { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
-               { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
-               { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
-               { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
-               { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
-               { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
-               { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
-               { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
-               { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
-               { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
-               { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
-#if (_SAMBA_BUILD_ >= 4)
-               POPT_COMMON_SAMBA
-               POPT_COMMON_CREDENTIALS
-               POPT_COMMON_VERSION
-#endif
-               { NULL }
-       };
-
-       ldb_global_init();
-
 #if (_SAMBA_BUILD_ >= 4)
        r = ldb_register_samba_handlers(ldb);
        if (r != 0) {
@@ -90,9 +99,12 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
 
 #endif
 
+       /* make the ldb utilities line buffered */
+       setlinebuf(stdout);
+
        ret = talloc_zero(ldb, struct ldb_cmdline);
        if (ret == NULL) {
-               ldb_oom(ldb);
+               fprintf(stderr, "Out of memory!\n");
                goto failed;
        }
 
@@ -140,7 +152,7 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
                        options.options = talloc_realloc(ret, options.options, 
                                                         const char *, num_options+3);
                        if (options.options == NULL) {
-                               ldb_oom(ldb);
+                               fprintf(stderr, "Out of memory!\n");
                                goto failed;
                        }
                        options.options[num_options] = poptGetOptArg(pc);
@@ -157,7 +169,7 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
 
                        options.controls = talloc_array(ret, char *, cc + 1);
                        if (options.controls == NULL) {
-                               ldb_oom(ldb);
+                               fprintf(stderr, "Out of memory!\n");
                                goto failed;
                        }
                        for (p = cs, cc = 0; p != NULL; cc++) {
@@ -208,17 +220,25 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
                flags |= LDB_FLG_NOSYNC;
        }
 
+       if (options.show_binary) {
+               flags |= LDB_FLG_SHOW_BINARY;
+       }
+
+       if (options.tracing) {
+               flags |= LDB_FLG_ENABLE_TRACING;
+       }
+
 #if (_SAMBA_BUILD_ >= 4)
        /* Must be after we have processed command line options */
-       gensec_init(global_loadparm); 
+       gensec_init(cmdline_lp_ctx); 
        
-       if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb, global_loadparm))) {
+       if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
                goto failed;
        }
        if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
                goto failed;
        }
-       if (ldb_set_opaque(ldb, "loadparm", global_loadparm)) {
+       if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
                goto failed;
        }