summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPanu Matilainen <pmatilai@redhat.com>2010-08-20 13:16:48 +0300
committerPanu Matilainen <pmatilai@redhat.com>2010-08-20 13:50:48 +0300
commit03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221 (patch)
treec796c19f884bd3f660e192e7e4745c835a77eb93
parentb217529967f972225dc9b7d2e37860885fde1ee1 (diff)
downloadlibrpm-tizen-03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221.tar.gz
librpm-tizen-03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221.tar.bz2
librpm-tizen-03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221.zip
Lump much of the common cli-init + finish tasks into cliutils helpers
-rw-r--r--cliutils.c76
-rw-r--r--cliutils.h7
-rw-r--r--rpmbuild.c67
-rw-r--r--rpmqv.c76
4 files changed, 89 insertions, 137 deletions
diff --git a/cliutils.c b/cliutils.c
index b1ffd2465..f022803dd 100644
--- a/cliutils.c
+++ b/cliutils.c
@@ -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;
+}
diff --git a/cliutils.h b/cliutils.h
index 15979dc66..830b4c4f5 100644
--- a/cliutils.h
+++ b/cliutils.h
@@ -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 */
diff --git a/rpmbuild.c b/rpmbuild.c
index af3039358..3b0b8cecd 100644
--- a/rpmbuild.c
+++ b/rpmbuild.c
@@ -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 dd6b86380..4897eece0 100644
--- 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);
}