Lump much of the common cli-init + finish tasks into cliutils helpers
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 20 Aug 2010 10:16:48 +0000 (13:16 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 20 Aug 2010 10:50:48 +0000 (13:50 +0300)
cliutils.c
cliutils.h
rpmbuild.c
rpmqv.c

index b1ffd24..f022803 100644 (file)
@@ -1,6 +1,12 @@
 #include "system.h"
+#if HAVE_MCHECK_H
+#include <mcheck.h>
+#endif
 #include <rpm/rpmlog.h>
 #include <rpm/rpmlib.h>
+#include <rpm/rpmfileutil.h>
+#include <rpm/rpmmacro.h>
+#include <rpm/rpmcli.h>
 #include "cliutils.h"
 #include "debug.h"
 
@@ -34,3 +40,73 @@ void printUsage(poptContext con, FILE * fp, int flags)
        poptPrintUsage(con, fp, flags);
 }
 
+poptContext initCli(const char *ctx, struct poptOption *optionsTable,
+                   int argc, char *argv[])
+{
+    const char *optArg;
+    int arg;
+    poptContext optCon;
+
+#if HAVE_MCHECK_H && HAVE_MTRACE
+    mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
+#endif
+    
+#if defined(ENABLE_NLS)
+    /* set up the correct locale */
+    (void) setlocale(LC_ALL, "" );
+
+    bindtextdomain(PACKAGE, LOCALEDIR);
+    textdomain(PACKAGE);
+#endif
+
+    rpmSetVerbosity(RPMLOG_NOTICE);    /* XXX silly use by showrc */
+
+    /* Make a first pass through the arguments, looking for --rcfile */
+    /* We need to handle that before dealing with the rest of the arguments. */
+    /* XXX popt argv definition should be fixed instead of casting... */
+    optCon = poptGetContext(ctx, argc, (const char **)argv, optionsTable, 0);
+    {
+       char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
+       (void) poptReadConfigFile(optCon, poptfile);
+       free(poptfile);
+    }
+    (void) poptReadDefaultConfig(optCon, 1);
+    poptSetExecPath(optCon, rpmConfigDir(), 1);
+
+    while ((arg = poptGetNextOpt(optCon)) > 0) {
+       optArg = poptGetOptArg(optCon);
+
+       switch (arg) {
+       default:
+           fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
+           exit(EXIT_FAILURE);
+       }
+    }
+
+    if (arg < -1) {
+       fprintf(stderr, "%s: %s\n", 
+               poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
+               poptStrerror(arg));
+       exit(EXIT_FAILURE);
+    }
+
+    rpmcliConfigured();
+
+    return optCon;
+}
+
+int finishCli(poptContext optCon, int rc)
+{
+    poptFreeContext(optCon);
+    rpmFreeMacros(NULL);
+    rpmFreeMacros(rpmCLIMacroContext);
+    rpmFreeRpmrc();
+    rpmlogClose();
+
+#if HAVE_MCHECK_H && HAVE_MTRACE
+    muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
+#endif
+
+    /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
+    return (rc > 254) ? 254 : rc;
+}
index 15979dc..830b4c4 100644 (file)
@@ -10,4 +10,11 @@ void argerror(const char * desc);
 
 void printUsage(poptContext con, FILE * fp, int flags);
 
+/* Initialize cli-environment, returning parsed popt context caller */
+poptContext initCli(const char *ctx, struct poptOption *optionsTable,
+                   int argc, char *argv[]);
+
+/* Free up common resources, return "normalized" exit code */
+int finishCli(poptContext optCon, int rc);
+
 #endif /* _CLIUTIL_H */
index af30393..3b0b8ce 100644 (file)
@@ -7,9 +7,6 @@ const char *__progname;
 #include <libgen.h>
 #include <ctype.h>
 #include <sys/wait.h>
-#if HAVE_MCHECK_H
-#include <mcheck.h>
-#endif
 
 #include <rpm/rpmcli.h>
 #include <rpm/rpmlib.h>                        /* RPMSIGTAG, rpmReadPackageFile .. */
@@ -389,9 +386,6 @@ int main(int argc, char *argv[])
     BTA_t ba = &rpmBTArgs;
     char * passPhrase = "";
 
-    int arg;
-    const char *optArg;
-    const char *poptCtx = "rpmbuild";
     const char *pkg = NULL;
     pid_t pipeChild = 0;
     poptContext optCon;
@@ -399,9 +393,6 @@ int main(int argc, char *argv[])
     int status;
     int p[2];
        
-#if HAVE_MCHECK_H && HAVE_MTRACE
-    mtrace();  /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
-#endif
     setprogname(argv[0]);      /* Retrofit glibc __progname */
 
     /* XXX glibc churn sanity */
@@ -410,52 +401,13 @@ int main(int argc, char *argv[])
        else __progname = argv[0];
     }
 
-#if defined(ENABLE_NLS)
-    /* set up the correct locale */
-    (void) setlocale(LC_ALL, "" );
-
-    bindtextdomain(PACKAGE, LOCALEDIR);
-    textdomain(PACKAGE);
-#endif
-
-    rpmSetVerbosity(RPMLOG_NOTICE);    /* XXX silly use by showrc */
-
-    /* Make a first pass through the arguments, looking for --rcfile */
-    /* We need to handle that before dealing with the rest of the arguments. */
-    /* XXX popt argv definition should be fixed instead of casting... */
-    optCon = poptGetContext(poptCtx, argc, (const char **)argv, optionsTable, 0);
-    {
-       char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
-       (void) poptReadConfigFile(optCon, poptfile);
-       free(poptfile);
-    }
-    (void) poptReadDefaultConfig(optCon, 1);
-    poptSetExecPath(optCon, rpmConfigDir(), 1);
-
-    while ((arg = poptGetNextOpt(optCon)) > 0) {
-       optArg = poptGetOptArg(optCon);
-
-       switch (arg) {
-       default:
-           fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
-           exit(EXIT_FAILURE);
-       }
-    }
-
-    if (arg < -1) {
-       fprintf(stderr, "%s: %s\n", 
-               poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
-               poptStrerror(arg));
-       exit(EXIT_FAILURE);
-    }
+    optCon = initCli("rpmbuild", optionsTable, argc, argv);
 
     if (argc <= 1 || poptPeekArg(optCon) == NULL) {
        printUsage(optCon, stderr, 0);
        exit(EXIT_FAILURE);
     }
 
-    rpmcliConfigured();
-
     switch (ba->buildMode) {
     case 'b':  bigMode = MODE_BUILD;           break;
     case 't':  bigMode = MODE_TARBUILD;        break;
@@ -599,29 +551,14 @@ exit:
 
     ts = rpmtsFree(ts);
 
-    optCon = poptFreeContext(optCon);
-    rpmFreeMacros(NULL);
-    rpmFreeMacros(rpmCLIMacroContext);
-    rpmFreeRpmrc();
-
     if (pipeChild) {
        (void) fclose(stdout);
        (void) waitpid(pipeChild, &status, 0);
     }
 
-    /* keeps memory leak checkers quiet */
-    rpmlogClose();
-
     freeNames();
     ba->buildRootOverride = _free(ba->buildRootOverride);
     ba->targets = _free(ba->targets);
 
-#if HAVE_MCHECK_H && HAVE_MTRACE
-    muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
-#endif
-
-    /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
-    if (ec > 254) ec = 254;
-
-    return ec;
+    return finishCli(optCon, ec);
 }
diff --git a/rpmqv.c b/rpmqv.c
index dd6b863..4897eec 100644 (file)
--- a/rpmqv.c
+++ b/rpmqv.c
@@ -4,15 +4,10 @@ const char *__progname;
 #define        _AUTOHELP
 
 #include <sys/wait.h>
-#if HAVE_MCHECK_H
-#include <mcheck.h>
-#endif
-
 #include <rpm/rpmcli.h>
 #include <rpm/rpmlib.h>                        /* RPMSIGTAG, rpmReadPackageFile .. */
 #include <rpm/rpmbuild.h>
 #include <rpm/rpmlog.h>
-#include <rpm/rpmfileutil.h>
 #include <rpm/rpmdb.h>
 #include <rpm/rpmps.h>
 #include <rpm/rpmts.h>
@@ -123,9 +118,6 @@ int main(int argc, char *argv[])
     char * passPhrase = "";
 #endif
 
-    int arg;
-
-    const char *optArg, *poptCtx;
     pid_t pipeChild = 0;
     poptContext optCon;
     int ec = 0;
@@ -134,10 +126,7 @@ int main(int argc, char *argv[])
 #ifdef IAM_RPMEIU
     int i;
 #endif
-       
-#if HAVE_MCHECK_H && HAVE_MTRACE
-    mtrace();  /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
-#endif
+
     setprogname(argv[0]);      /* Retrofit glibc __progname */
 
     /* XXX glibc churn sanity */
@@ -146,6 +135,8 @@ int main(int argc, char *argv[])
        else __progname = argv[0];
     }
 
+    optCon = initCli("rpm", optionsTable, argc, argv);
+
     /* Set the major mode based on argv[0] */
 #ifdef IAM_RPMQV
     if (rstreq(__progname, "rpmquery"))        bigMode = MODE_QUERY;
@@ -170,50 +161,6 @@ int main(int argc, char *argv[])
     }
 #endif
 
-#if defined(ENABLE_NLS)
-    /* set up the correct locale */
-    (void) setlocale(LC_ALL, "" );
-
-    bindtextdomain(PACKAGE, LOCALEDIR);
-    textdomain(PACKAGE);
-#endif
-
-    rpmSetVerbosity(RPMLOG_NOTICE);    /* XXX silly use by showrc */
-
-    /* Only build has it's own set of aliases, everything else uses rpm */
-    poptCtx = "rpm";
-
-    /* Make a first pass through the arguments, looking for --rcfile */
-    /* We need to handle that before dealing with the rest of the arguments. */
-    /* XXX popt argv definition should be fixed instead of casting... */
-    optCon = poptGetContext(poptCtx, argc, (const char **)argv, optionsTable, 0);
-    {
-       char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
-       (void) poptReadConfigFile(optCon, poptfile);
-       free(poptfile);
-    }
-    (void) poptReadDefaultConfig(optCon, 1);
-    poptSetExecPath(optCon, rpmConfigDir(), 1);
-
-    while ((arg = poptGetNextOpt(optCon)) > 0) {
-       optArg = poptGetOptArg(optCon);
-
-       switch (arg) {
-       default:
-           fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
-           exit(EXIT_FAILURE);
-       }
-    }
-
-    if (arg < -1) {
-       fprintf(stderr, "%s: %s\n", 
-               poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
-               poptStrerror(arg));
-       exit(EXIT_FAILURE);
-    }
-
-    rpmcliConfigured();
-
 #ifdef IAM_RPMDB
   if (bigMode == MODE_UNKNOWN || (bigMode & MODES_DB)) {
     if (da->init) {
@@ -610,19 +557,11 @@ exit:
 
     ts = rpmtsFree(ts);
 
-    optCon = poptFreeContext(optCon);
-    rpmFreeMacros(NULL);
-       rpmFreeMacros(rpmCLIMacroContext);
-    rpmFreeRpmrc();
-
     if (pipeChild) {
        (void) fclose(stdout);
        (void) waitpid(pipeChild, &status, 0);
     }
 
-    /* keeps memory leak checkers quiet */
-    rpmlogClose();
-
 #ifdef IAM_RPMQV
     qva->qva_queryFormat = _free(qva->qva_queryFormat);
 #endif
@@ -634,12 +573,5 @@ exit:
     ia->relocations = _free(ia->relocations);
 #endif
 
-#if HAVE_MCHECK_H && HAVE_MTRACE
-    muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
-#endif
-
-    /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
-    if (ec > 254) ec = 254;
-
-    return ec;
+    return finishCli(optCon, ec);
 }