7 #include <rpm/rpmlog.h>
8 #include <rpm/rpmlib.h>
9 #include <rpm/rpmfileutil.h>
10 #include <rpm/rpmmacro.h>
11 #include <rpm/rpmcli.h>
15 static pid_t pipeChild = 0;
18 void argerror(const char * desc)
20 fprintf(stderr, _("%s: %s\n"), __progname, desc);
24 static void printVersion(FILE * fp)
26 fprintf(fp, _("RPM version %s\n"), rpmEVR);
29 static void printBanner(FILE * fp)
31 fprintf(fp, _("Copyright (C) 1998-2002 - Red Hat, Inc.\n"));
32 fprintf(fp, _("This program may be freely redistributed under the terms of the GNU GPL\n"));
35 void printUsage(poptContext con, FILE * fp, int flags)
42 poptPrintHelp(con, fp, flags);
44 poptPrintUsage(con, fp, flags);
47 poptContext initCli(const char *ctx, struct poptOption *optionsTable,
48 int argc, char *argv[])
54 #if HAVE_MCHECK_H && HAVE_MTRACE
55 mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
58 #if defined(ENABLE_NLS)
59 /* set up the correct locale */
60 (void) setlocale(LC_ALL, "" );
62 bindtextdomain(PACKAGE, LOCALEDIR);
66 rpmSetVerbosity(RPMLOG_NOTICE); /* XXX silly use by showrc */
68 /* Make a first pass through the arguments, looking for --rcfile */
69 /* We need to handle that before dealing with the rest of the arguments. */
70 /* XXX popt argv definition should be fixed instead of casting... */
71 optCon = poptGetContext(ctx, argc, (const char **)argv, optionsTable, 0);
73 char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
74 (void) poptReadConfigFile(optCon, poptfile);
77 (void) poptReadDefaultConfig(optCon, 1);
78 poptSetExecPath(optCon, rpmConfigDir(), 1);
80 while ((arg = poptGetNextOpt(optCon)) > 0) {
81 optArg = poptGetOptArg(optCon);
85 fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
91 fprintf(stderr, "%s: %s\n",
92 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
102 int finishCli(poptContext optCon, int rc)
104 poptFreeContext(optCon);
106 rpmFreeMacros(rpmCLIMacroContext);
110 #if HAVE_MCHECK_H && HAVE_MTRACE
111 muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
114 /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
115 return (rc > 254) ? 254 : rc;
123 fprintf(stderr, _("creating a pipe for --pipe failed: %m\n"));
127 if (!(pipeChild = fork())) {
128 (void) signal(SIGPIPE, SIG_DFL);
130 (void) dup2(p[0], STDIN_FILENO);
132 (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
133 fprintf(stderr, _("exec failed\n"));
138 (void) dup2(p[1], STDOUT_FILENO);
143 void finishPipe(void)
147 (void) fclose(stdout);
148 (void) waitpid(pipeChild, &status, 0);