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];
#include <popt.h>
#include <rpm/rpmutil.h>
+/* "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);
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. */
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);
ba->buildRootOverride = _free(ba->buildRootOverride);
ba->targets = _free(ba->targets);
- return finishCli(optCon, ec);
+ rpmcliFini(optCon);
+
+ return RETVAL(ec);
}
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
ia->relocations = _free(ia->relocations);
#endif
- return finishCli(optCon, ec);
+ rpmcliFini(optCon);
+
+ return RETVAL(ec);
}