Lump much of the common cli-init + finish tasks into 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 <rpm/rpmlog.h>
6 #include <rpm/rpmlib.h>
7 #include <rpm/rpmfileutil.h>
8 #include <rpm/rpmmacro.h>
9 #include <rpm/rpmcli.h>
10 #include "cliutils.h"
11 #include "debug.h"
12
13 RPM_GNUC_NORETURN
14 void argerror(const char * desc)
15 {
16     fprintf(stderr, _("%s: %s\n"), __progname, desc);
17     exit(EXIT_FAILURE);
18 }
19
20 static void printVersion(FILE * fp)
21 {
22     fprintf(fp, _("RPM version %s\n"), rpmEVR);
23 }
24
25 static void printBanner(FILE * fp)
26 {
27     fprintf(fp, _("Copyright (C) 1998-2002 - Red Hat, Inc.\n"));
28     fprintf(fp, _("This program may be freely redistributed under the terms of the GNU GPL\n"));
29 }
30
31 void printUsage(poptContext con, FILE * fp, int flags)
32 {
33     printVersion(fp);
34     printBanner(fp);
35     fprintf(fp, "\n");
36
37     if (rpmIsVerbose())
38         poptPrintHelp(con, fp, flags);
39     else
40         poptPrintUsage(con, fp, flags);
41 }
42
43 poptContext initCli(const char *ctx, struct poptOption *optionsTable,
44                     int argc, char *argv[])
45 {
46     const char *optArg;
47     int arg;
48     poptContext optCon;
49
50 #if HAVE_MCHECK_H && HAVE_MTRACE
51     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
52 #endif
53     
54 #if defined(ENABLE_NLS)
55     /* set up the correct locale */
56     (void) setlocale(LC_ALL, "" );
57
58     bindtextdomain(PACKAGE, LOCALEDIR);
59     textdomain(PACKAGE);
60 #endif
61
62     rpmSetVerbosity(RPMLOG_NOTICE);     /* XXX silly use by showrc */
63
64     /* Make a first pass through the arguments, looking for --rcfile */
65     /* We need to handle that before dealing with the rest of the arguments. */
66     /* XXX popt argv definition should be fixed instead of casting... */
67     optCon = poptGetContext(ctx, argc, (const char **)argv, optionsTable, 0);
68     {
69         char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
70         (void) poptReadConfigFile(optCon, poptfile);
71         free(poptfile);
72     }
73     (void) poptReadDefaultConfig(optCon, 1);
74     poptSetExecPath(optCon, rpmConfigDir(), 1);
75
76     while ((arg = poptGetNextOpt(optCon)) > 0) {
77         optArg = poptGetOptArg(optCon);
78
79         switch (arg) {
80         default:
81             fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
82             exit(EXIT_FAILURE);
83         }
84     }
85
86     if (arg < -1) {
87         fprintf(stderr, "%s: %s\n", 
88                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
89                 poptStrerror(arg));
90         exit(EXIT_FAILURE);
91     }
92
93     rpmcliConfigured();
94
95     return optCon;
96 }
97
98 int finishCli(poptContext optCon, int rc)
99 {
100     poptFreeContext(optCon);
101     rpmFreeMacros(NULL);
102     rpmFreeMacros(rpmCLIMacroContext);
103     rpmFreeRpmrc();
104     rpmlogClose();
105
106 #if HAVE_MCHECK_H && HAVE_MTRACE
107     muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
108 #endif
109
110     /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
111     return (rc > 254) ? 254 : rc;
112 }