Move --pipe handling to cliutils helpers
[platform/upstream/rpm.git] / cliutils.c
1 #include "system.h"
2 #if HAVE_MCHECK_H
3 #include <mcheck.h>
4 #endif
5 #include <sys/wait.h>
6
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>
12 #include "cliutils.h"
13 #include "debug.h"
14
15 static pid_t pipeChild = 0;
16
17 RPM_GNUC_NORETURN
18 void argerror(const char * desc)
19 {
20     fprintf(stderr, _("%s: %s\n"), __progname, desc);
21     exit(EXIT_FAILURE);
22 }
23
24 static void printVersion(FILE * fp)
25 {
26     fprintf(fp, _("RPM version %s\n"), rpmEVR);
27 }
28
29 static void printBanner(FILE * fp)
30 {
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"));
33 }
34
35 void printUsage(poptContext con, FILE * fp, int flags)
36 {
37     printVersion(fp);
38     printBanner(fp);
39     fprintf(fp, "\n");
40
41     if (rpmIsVerbose())
42         poptPrintHelp(con, fp, flags);
43     else
44         poptPrintUsage(con, fp, flags);
45 }
46
47 poptContext initCli(const char *ctx, struct poptOption *optionsTable,
48                     int argc, char *argv[])
49 {
50     const char *optArg;
51     int arg;
52     poptContext optCon;
53
54 #if HAVE_MCHECK_H && HAVE_MTRACE
55     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
56 #endif
57     
58 #if defined(ENABLE_NLS)
59     /* set up the correct locale */
60     (void) setlocale(LC_ALL, "" );
61
62     bindtextdomain(PACKAGE, LOCALEDIR);
63     textdomain(PACKAGE);
64 #endif
65
66     rpmSetVerbosity(RPMLOG_NOTICE);     /* XXX silly use by showrc */
67
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);
72     {
73         char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
74         (void) poptReadConfigFile(optCon, poptfile);
75         free(poptfile);
76     }
77     (void) poptReadDefaultConfig(optCon, 1);
78     poptSetExecPath(optCon, rpmConfigDir(), 1);
79
80     while ((arg = poptGetNextOpt(optCon)) > 0) {
81         optArg = poptGetOptArg(optCon);
82
83         switch (arg) {
84         default:
85             fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
86             exit(EXIT_FAILURE);
87         }
88     }
89
90     if (arg < -1) {
91         fprintf(stderr, "%s: %s\n", 
92                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
93                 poptStrerror(arg));
94         exit(EXIT_FAILURE);
95     }
96
97     rpmcliConfigured();
98
99     return optCon;
100 }
101
102 int finishCli(poptContext optCon, int rc)
103 {
104     poptFreeContext(optCon);
105     rpmFreeMacros(NULL);
106     rpmFreeMacros(rpmCLIMacroContext);
107     rpmFreeRpmrc();
108     rpmlogClose();
109
110 #if HAVE_MCHECK_H && HAVE_MTRACE
111     muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
112 #endif
113
114     /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
115     return (rc > 254) ? 254 : rc;
116 }
117
118 int initPipe(void)
119 {
120     int p[2];
121
122     if (pipe(p) < 0) {
123         fprintf(stderr, _("creating a pipe for --pipe failed: %m\n"));
124         return -1;
125     }
126
127     if (!(pipeChild = fork())) {
128         (void) signal(SIGPIPE, SIG_DFL);
129         (void) close(p[1]);
130         (void) dup2(p[0], STDIN_FILENO);
131         (void) close(p[0]);
132         (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
133         fprintf(stderr, _("exec failed\n"));
134         exit(EXIT_FAILURE);
135     }
136
137     (void) close(p[0]);
138     (void) dup2(p[1], STDOUT_FILENO);
139     (void) close(p[1]);
140     return 0;
141 }
142
143 void finishPipe(void)
144 {
145     int status;
146     if (pipeChild) {
147         (void) fclose(stdout);
148         (void) waitpid(pipeChild, &status, 0);
149     }
150 }