diff --git a/cliutils.c b/cliutils.c index 23c632372..cbbc7fef2 100644 --- a/cliutils.c +++ b/cliutils.c @@ -44,77 +44,6 @@ 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; -} - int initPipe(void) { int p[2]; diff --git a/cliutils.h b/cliutils.h index ea4539899..875b9507c 100644 --- a/cliutils.h +++ b/cliutils.h @@ -5,18 +5,14 @@ #include #include +/* "normalized" exit: avoid overflowing and xargs special value 255 */ +#define RETVAL(rc) (((rc) > 254) ? 254 : (rc)) + RPM_GNUC_NORETURN 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); - int initPipe(void); void finishPipe(void); diff --git a/lib/poptALL.c b/lib/poptALL.c index 796675608..1ad95ea76 100644 --- a/lib/poptALL.c +++ b/lib/poptALL.c @@ -230,7 +230,11 @@ struct poptOption rpmcliAllPoptTable[] = { poptContext rpmcliFini(poptContext optCon) { - optCon = poptFreeContext(optCon); + poptFreeContext(optCon); + rpmFreeMacros(NULL); + rpmFreeMacros(rpmCLIMacroContext); + rpmFreeRpmrc(); + rpmlogClose(); #if HAVE_MCHECK_H && HAVE_MTRACE muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ diff --git a/rpmbuild.c b/rpmbuild.c index b7cfbbd08..da04289ff 100644 --- a/rpmbuild.c +++ b/rpmbuild.c @@ -386,18 +386,8 @@ int main(int argc, char *argv[]) char * passPhrase = ""; const char *pkg = NULL; - poptContext optCon; int ec = 0; - - setprogname(argv[0]); /* Retrofit glibc __progname */ - - /* XXX glibc churn sanity */ - if (__progname == NULL) { - if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++; - else __progname = argv[0]; - } - - optCon = initCli("rpmbuild", optionsTable, argc, argv); + poptContext optCon = rpmcliInit(argc, argv, optionsTable); if (argc <= 1 || poptPeekArg(optCon) == NULL) { printUsage(optCon, stderr, 0); @@ -534,5 +524,7 @@ exit: ba->buildRootOverride = _free(ba->buildRootOverride); ba->targets = _free(ba->targets); - return finishCli(optCon, ec); + rpmcliFini(optCon); + + return RETVAL(ec); } diff --git a/rpmqv.c b/rpmqv.c index e9e23ee3e..8d4bdf0f0 100644 --- a/rpmqv.c +++ b/rpmqv.c @@ -123,15 +123,7 @@ int main(int argc, char *argv[]) int i; #endif - setprogname(argv[0]); /* Retrofit glibc __progname */ - - /* XXX glibc churn sanity */ - if (__progname == NULL) { - if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++; - else __progname = argv[0]; - } - - optCon = initCli("rpm", optionsTable, argc, argv); + optCon = rpmcliInit(argc, argv, optionsTable); /* Set the major mode based on argv[0] */ #ifdef IAM_RPMQV @@ -547,5 +539,7 @@ exit: ia->relocations = _free(ia->relocations); #endif - return finishCli(optCon, ec); + rpmcliFini(optCon); + + return RETVAL(ec); }