poptContext poptInit(int argc, const char ** argv, const struct poptOption * options, const char * configPaths) { poptContext con = NULL; const char * argv0; if (argv == NULL || argv[0] == NULL || options == NULL) return con; if ((argv0 = strrchr(argv[0], '/')) != NULL) argv0++; else argv0 = argv[0]; con = poptGetContext(argv0, argc, (const char **)argv, options, 0); if (con != NULL&& poptReadConfigFiles(con, configPaths)) con = poptFini(con); return con; }
/*@-globstate@*/ poptContext rpmcliInit(int argc, char *const argv[], struct poptOption * optionsTable) /*@globals rpmpoptfiles @*/ /*@modifies rpmpoptfiles @*/ { poptContext optCon; int rc; int xx; int i; #if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE) /*@-noeffect@*/ mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ /*@=noeffect@*/ #endif /*@-globs -mods@*/ setprogname(argv[0]); /* Retrofit glibc __progname */ /* XXX glibc churn sanity */ if (__progname == NULL) { if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++; else __progname = argv[0]; } /*@=globs =mods@*/ /* Insure that stdin/stdout/stderr are open, lest stderr end up in rpmdb. */ { static const char _devnull[] = "/dev/null"; #if defined(STDIN_FILENO) (void) checkfd(_devnull, STDIN_FILENO, O_RDONLY); #endif #if defined(STDOUT_FILENO) (void) checkfd(_devnull, STDOUT_FILENO, O_WRONLY); #endif #if defined(STDERR_FILENO) (void) checkfd(_devnull, STDERR_FILENO, O_WRONLY); #endif } #if defined(RPM_VENDOR_WINDRIVER) (void) setRuntimeRelocPaths(); #endif #if defined(ENABLE_NLS) && !defined(__LCLINT__) (void) setlocale(LC_ALL, "" ); (void) bindtextdomain(PACKAGE, __localedir); (void) textdomain(PACKAGE); #endif rpmSetVerbosity(RPMLOG_NOTICE); if (optionsTable == NULL) { /* Read rpm configuration (if not already read). */ rpmcliConfigured(); return NULL; } /* read all RPM POPT configuration files */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--rpmpopt") == 0 && i+1 < argc) { rpmpoptfiles = argv[i+1]; break; } else if (strncmp(argv[i], "--rpmpopt=", 10) == 0) { rpmpoptfiles = argv[i]+10; break; } } /* XXX strip off the "lt-" prefix so that rpmpopt aliases "work". */ { static const char lt_[] = "lt-"; const char * s = __progname; if (!strncmp(s, lt_, sizeof(lt_)-1)) s += sizeof(lt_)-1; /*@-nullpass -temptrans@*/ optCon = poptGetContext(s, argc, (const char **)argv, optionsTable, 0); /*@=nullpass =temptrans@*/ } #if defined(RPM_VENDOR_OPENPKG) /* stick-with-rpm-file-sanity-checking */ || \ !defined(POPT_ERROR_BADCONFIG) /* XXX POPT 1.15 retrofit */ { char * path_buf = xstrdup(rpmpoptfiles); char *path; char *path_next; for (path = path_buf; path != NULL && *path != '\0'; path = path_next) { const char **av; int ac; /* locate start of next path element */ path_next = strchr(path, ':'); if (path_next != NULL && *path_next == ':') *path_next++ = '\0'; else path_next = path + strlen(path); /* glob-expand the path element */ ac = 0; av = NULL; if ((xx = rpmGlob(path, &ac, &av)) != 0) continue; /* work-off each resulting file from the path element */ for (i = 0; i < ac; i++) { const char *fn = av[i]; if (fn[0] == '@' /* attention */) { fn++; if (!rpmSecuritySaneFile(fn)) { rpmlog(RPMLOG_WARNING, "existing POPT configuration file \"%s\" considered INSECURE -- not loaded\n", fn); /*@innercontinue@*/ continue; } } (void) poptReadConfigFile(optCon, fn); av[i] = _free(av[i]); } av = _free(av); } path_buf = _free(path_buf); } #else /* XXX FIXME: better error message is needed. */ if ((xx = poptReadConfigFiles(optCon, rpmpoptfiles)) != 0) rpmlog(RPMLOG_WARNING, "existing POPT configuration file \"%s\" considered INSECURE -- not loaded\n", rpmpoptfiles); #endif #if defined(RPM_VENDOR_WINDRIVER) { const char * poptAliasFn = rpmGetPath(__usrlibrpm, "/rpmpopt", NULL); (void) poptReadConfigFile(optCon, poptAliasFn); poptAliasFn = _free(poptAliasFn); } #endif /* read standard POPT configuration files */ /* XXX FIXME: the 2nd arg useEnv flag is UNUSED. */ (void) poptReadDefaultConfig(optCon, 1); #if defined(RPM_VENDOR_WINDRIVER) { const char * poptExecPath = rpmGetPath(__usrlibrpm, NULL); poptSetExecPath(optCon, poptExecPath, 1); poptExecPath = _free(poptExecPath); } #else poptSetExecPath(optCon, USRLIBRPM, 1); #endif /* Process all options, whine if unknown. */ while ((rc = poptGetNextOpt(optCon)) > 0) { const char * optArg = poptGetOptArg(optCon); /*@-dependenttrans -observertrans@*/ /* Avoid popt memory leaks. */ optArg = _free(optArg); /*@=dependenttrans =observertrans @*/ switch (rc) { default: /*@-nullpass@*/ fprintf(stderr, _("%s: option table misconfigured (%d)\n"), __progname, rc); /*@=nullpass@*/ exit(EXIT_FAILURE); /*@notreached@*/ /*@switchbreak@*/ break; } } if (rc < -1) { /*@-nullpass@*/ fprintf(stderr, "%s: %s: %s\n", __progname, poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(rc)); /*@=nullpass@*/ exit(EXIT_FAILURE); } /* Read rpm configuration (if not already read). */ rpmcliConfigured(); if (_debug) { rpmIncreaseVerbosity(); rpmIncreaseVerbosity(); } /* Initialize header stat collection. */ /*@-mods@*/ _hdr_stats = _rpmts_stats; /*@=mods@*/ return optCon; }