diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2010-08-20 13:16:48 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2010-08-20 13:50:48 +0300 |
commit | 03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221 (patch) | |
tree | c796c19f884bd3f660e192e7e4745c835a77eb93 /cliutils.c | |
parent | b217529967f972225dc9b7d2e37860885fde1ee1 (diff) | |
download | rpm-03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221.tar.gz rpm-03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221.tar.bz2 rpm-03a8511e3ba8b2c6cf3dc67fadaeca4aa9fb5221.zip |
Lump much of the common cli-init + finish tasks into cliutils helpers
Diffstat (limited to 'cliutils.c')
-rw-r--r-- | cliutils.c | 76 |
1 files changed, 76 insertions, 0 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; +} |