Accumulate compress/uncompress times.
[platform/upstream/rpm.git] / lib / poptALL.c
1 /** \ingroup rpmcli
2  * \file lib/poptALL.c
3  *  Popt tables for all rpm modes.
4  */
5
6 #include "system.h"
7
8 #include <rpmcli.h>
9
10 #include "debug.h"
11
12 #define POPT_SHOWVERSION        -999
13 #define POPT_SHOWRC             -998
14 #define POPT_QUERYTAGS          -997
15 #define POPT_PREDEFINE          -996
16 #ifdef  NOTYET
17 #define POPT_RCFILE             -995
18 #endif
19
20 /*@unchecked@*/
21 static int _debug = 0;
22
23 /*@-exportheadervar@*/
24 /*@unchecked@*/
25 extern int _rpmds_nopromote;
26
27 /*@unchecked@*/
28 extern int _fps_debug;
29
30 /*@unchecked@*/
31 extern int _fsm_debug;
32
33 /*@unchecked@*/
34 extern int _fsm_threads;
35
36 /*@unchecked@*/
37 extern int _hdr_debug;
38
39 /*@unchecked@*/
40 extern int _psm_debug;
41
42 /*@unchecked@*/
43 extern int _psm_threads;
44
45 /*@unchecked@*/
46 extern int _rpmal_debug;
47
48 /*@unchecked@*/
49 extern int _rpmdb_debug;
50
51 /*@unchecked@*/
52 extern int _rpmds_debug;
53
54 /* XXX avoid -lrpmbuild linkage. */
55 /*@unchecked@*/
56        int _rpmfc_debug;
57
58 /*@unchecked@*/
59 extern int _rpmfi_debug;
60
61 /*@unchecked@*/
62 extern int _rpmps_debug;
63
64 /*@unchecked@*/
65 extern int _rpmsq_debug;
66
67 /*@unchecked@*/
68 extern int _rpmte_debug;
69
70 /*@unchecked@*/
71 extern int _rpmts_debug;
72
73 /*@unchecked@*/
74 extern int _rpmts_stats;
75
76 /*@unchecked@*/
77 extern int noLibio;
78 /*@=exportheadervar@*/
79
80 /*@unchecked@*/
81 const char * rpmcliPipeOutput = NULL;
82
83 /*@unchecked@*/
84 const char * rpmcliRcfile = NULL;
85
86 /*@unchecked@*/
87 const char * rpmcliRootDir = "/";
88
89 /*@unchecked@*/
90 rpmQueryFlags rpmcliQueryFlags;
91
92 /*@-exportheadervar@*/
93 /*@unchecked@*/
94 extern int _ftp_debug;
95 /*@unchecked@*/
96 extern int noLibio;
97 /*@unchecked@*/
98 extern int _rpmio_debug;
99 /*@=exportheadervar@*/
100
101 /*@unchecked@*/
102 static int rpmcliInitialized = -1;
103
104 /**
105  * Display rpm version.
106  */
107 static void printVersion(FILE * fp)
108         /*@globals rpmEVR, fileSystem @*/
109         /*@modifies *fp, fileSystem @*/
110 {
111     fprintf(fp, _("RPM version %s\n"), rpmEVR);
112 }
113
114 /**
115  * Make sure that config files have been read.
116  * @warning Options like --rcfile and --verbose must precede callers option.
117  */
118 /*@mayexit@*/
119 void rpmcliConfigured(void)
120         /*@globals rpmcliInitialized, rpmCLIMacroContext, rpmGlobalMacroContext,
121                 fileSystem, internalState @*/
122         /*@modifies rpmcliInitialized, rpmCLIMacroContext, rpmGlobalMacroContext,
123                 fileSystem, internalState @*/
124 {
125
126     if (rpmcliInitialized < 0)
127         rpmcliInitialized = rpmReadConfigFiles(rpmcliRcfile, NULL);
128     if (rpmcliInitialized)
129         exit(EXIT_FAILURE);
130 }
131
132 /**
133  */
134 /*@-bounds@*/
135 static void rpmcliAllArgCallback( /*@unused@*/ poptContext con,
136                 /*@unused@*/ enum poptCallbackReason reason,
137                 const struct poptOption * opt, const char * arg,
138                 /*@unused@*/ const void * data)
139         /*@globals rpmcliQueryFlags, rpmCLIMacroContext, rpmGlobalMacroContext,
140                 fileSystem, internalState @*/
141         /*@modifies rpmcliQueryFlags, rpmCLIMacroContext, rpmGlobalMacroContext,
142                 fileSystem, internalState @*/
143 {
144
145     /* XXX avoid accidental collisions with POPT_BIT_SET for flags */
146     /*@-branchstate@*/
147     if (opt->arg == NULL)
148     switch (opt->val) {
149     case 'q':
150         rpmSetVerbosity(RPMMESS_QUIET);
151         break;
152     case 'v':
153         rpmIncreaseVerbosity();
154         break;
155     case POPT_PREDEFINE:
156         (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
157         break;
158     case 'D':
159         /* XXX Predefine macro if not initialized yet. */
160         if (rpmcliInitialized < 0)
161             (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
162         rpmcliConfigured();
163 /*@-type@*/
164         (void) rpmDefineMacro(NULL, arg, RMIL_CMDLINE);
165         (void) rpmDefineMacro(rpmCLIMacroContext, arg, RMIL_CMDLINE);
166 /*@=type@*/
167         break;
168     case 'E':
169         rpmcliConfigured();
170         {   const char *val = rpmExpand(arg, NULL);
171             fprintf(stdout, "%s\n", val);
172             val = _free(val);
173         }
174         break;
175     case POPT_SHOWVERSION:
176         printVersion(stdout);
177         exit(EXIT_SUCCESS);
178         /*@notreached@*/ break;
179     case POPT_SHOWRC:
180         rpmcliConfigured();
181         (void) rpmShowRC(stdout);
182         exit(EXIT_SUCCESS);
183         /*@notreached@*/ break;
184     case POPT_QUERYTAGS:
185         rpmDisplayQueryTags(stdout);
186         exit(EXIT_SUCCESS);
187         /*@notreached@*/ break;
188 #if defined(POPT_RCFILE)
189     case POPT_RCFILE:           /* XXX FIXME: noop for now */
190         break;
191 #endif
192     case RPMCLI_POPT_NODIGEST:
193         rpmcliQueryFlags |= VERIFY_DIGEST;
194         break;
195
196     case RPMCLI_POPT_NOSIGNATURE:
197         rpmcliQueryFlags |= VERIFY_SIGNATURE;
198         break;
199
200     case RPMCLI_POPT_NOHDRCHK:
201         rpmcliQueryFlags |= VERIFY_HDRCHK;
202         break;
203     }
204     /*@=branchstate@*/
205 }
206
207 /*@-bitwisesigned -compmempass @*/
208 /*@unchecked@*/
209 struct poptOption rpmcliAllPoptTable[] = {
210 /*@-type@*/ /* FIX: cast? */
211  { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
212         rpmcliAllArgCallback, 0, NULL, NULL },
213 /*@=type@*/
214
215  { "debug", 'd', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_debug, -1,
216         NULL, NULL },
217
218  { "predefine", 'D', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_PREDEFINE,
219         N_("predefine MACRO with value EXPR"),
220         N_("'MACRO EXPR'") },
221  { "define", 'D', POPT_ARG_STRING, 0, 'D',
222         N_("define MACRO with value EXPR"),
223         N_("'MACRO EXPR'") },
224  { "eval", 'E', POPT_ARG_STRING, 0, 'E',
225         N_("print macro expansion of EXPR"),
226         N_("'EXPR'") },
227  { "macros", '\0', POPT_ARG_STRING, &macrofiles, 0,
228         N_("read <FILE:...> instead of default file(s)"),
229         N_("<FILE:...>") },
230
231  { "nodigest", '\0', 0, 0, RPMCLI_POPT_NODIGEST,
232         N_("don't verify package digest(s)"), NULL },
233  { "nohdrchk", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, RPMCLI_POPT_NOHDRCHK,
234         N_("don't verify database header(s) when retrieved"), NULL },
235 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
236  { "nolibio", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &noLibio, 1,
237         N_("disable use of libio(3) API"), NULL},
238 #endif
239  { "nosignature", '\0', 0, 0, RPMCLI_POPT_NOSIGNATURE,
240         N_("don't verify package signature(s)"), NULL },
241
242  { "pipe", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, &rpmcliPipeOutput, 0,
243         N_("send stdout to CMD"),
244         N_("CMD") },
245 #if !defined(POPT_RCFILE)
246  { "rcfile", '\0', POPT_ARG_STRING, &rpmcliRcfile, 0,
247         N_("read <FILE:...> instead of default file(s)"),
248         N_("<FILE:...>") },
249 #else
250  { "rcfile", '\0', 0, NULL, POPT_RCFILE,        
251         N_("read <FILE:...> instead of default file(s)"),
252         N_("<FILE:...>") },
253 #endif
254  { "root", 'r', POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT, &rpmcliRootDir, 0,
255         N_("use ROOT as top level directory"),
256         N_("ROOT") },
257
258  { "querytags", '\0', 0, 0, POPT_QUERYTAGS,
259         N_("display known query tags"), NULL },
260  { "showrc", '\0', 0, NULL, POPT_SHOWRC,
261         N_("display final rpmrc and macro configuration"), NULL },
262  { "quiet", '\0', 0, NULL, 'q',
263         N_("provide less detailed output"), NULL},
264  { "verbose", 'v', 0, NULL, 'v',
265         N_("provide more detailed output"), NULL},
266  { "version", '\0', 0, NULL, POPT_SHOWVERSION,
267         N_("print the version of rpm being used"), NULL },
268
269 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
270  { "nolibio", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &noLibio, 1,
271        N_("disable use of libio(3) API"), NULL},
272 #endif
273
274  { "promoteepoch", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmds_nopromote, 0,
275         NULL, NULL},
276
277  { "fpsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fps_debug, -1,
278         NULL, NULL},
279  { "fsmdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fsm_debug, -1,
280         N_("debug payload file state machine"), NULL},
281  { "fsmthreads", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_fsm_threads, -1,
282         N_("use threads for file state machine"), NULL},
283  { "ftpdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_ftp_debug, -1,
284         N_("debug protocol data stream"), NULL},
285  { "hdrdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_hdr_debug, -1,
286         NULL, NULL},
287 #ifdef  DYING
288  { "poptdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_popt_debug, -1,
289         N_("debug option/argument processing"), NULL},
290 #endif
291  { "psmdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_psm_debug, -1,
292         N_("debug package state machine"), NULL},
293  { "psmthreads", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_psm_threads, -1,
294         N_("use threads for package state machine"), NULL},
295  { "rpmaldebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmal_debug, -1,
296         NULL, NULL},
297  { "rpmdbdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmdb_debug, -1,
298         NULL, NULL},
299  { "rpmdsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmds_debug, -1,
300         NULL, NULL},
301  { "rpmfcdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmfc_debug, -1,
302         NULL, NULL},
303  { "rpmfidebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmfi_debug, -1,
304         NULL, NULL},
305  { "rpmiodebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmio_debug, -1,
306         N_("debug rpmio I/O"), NULL},
307  { "rpmpsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmps_debug, -1,
308         NULL, NULL},
309  { "rpmsqdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmsq_debug, -1,
310         NULL, NULL},
311  { "rpmtedebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmte_debug, -1,
312         NULL, NULL},
313  { "rpmtsdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmts_debug, -1,
314         NULL, NULL},
315  { "stats", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmts_stats, -1,
316         NULL, NULL},
317  { "urldebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_url_debug, -1,
318         N_("debug URL cache handling"), NULL},
319
320    POPT_TABLEEND
321 };
322 /*@=bitwisesigned =compmempass @*/
323
324 poptContext
325 rpmcliFini(poptContext optCon)
326 {
327     optCon = poptFreeContext(optCon);
328
329 #if HAVE_MCHECK_H && HAVE_MTRACE
330     /*@-noeffect@*/
331     muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
332     /*@=noeffect@*/
333 #endif
334
335     return NULL;
336 }
337
338 /*@-globstate@*/
339 poptContext
340 rpmcliInit(int argc, char *const argv[], struct poptOption * optionsTable)
341 {
342     const char * optArg;
343     poptContext optCon;
344     int rc;
345
346 #if HAVE_MCHECK_H && HAVE_MTRACE
347     /*@-noeffect@*/
348     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
349     /*@=noeffect@*/
350 #endif
351 /*@-globs -mods@*/
352     setprogname(argv[0]);       /* Retrofit glibc __progname */
353
354     /* XXX glibc churn sanity */
355     if (__progname == NULL) {
356         if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++;
357         else __progname = argv[0];
358     }
359 /*@=globs =mods@*/
360
361 #if defined(ENABLE_NLS) && !defined(__LCLINT__)
362     (void) setlocale(LC_ALL, "" );
363     (void) bindtextdomain(PACKAGE, LOCALEDIR);
364     (void) textdomain(PACKAGE);
365 #endif
366
367     rpmSetVerbosity(RPMMESS_NORMAL);
368
369     if (optionsTable == NULL) {
370         /* Read rpm configuration (if not already read). */
371         rpmcliConfigured();
372         return NULL;
373     }
374
375 /*@-nullpass -temptrans@*/
376     optCon = poptGetContext(__progname, argc, (const char **)argv, optionsTable, 0);
377 /*@=nullpass =temptrans@*/
378     (void) poptReadConfigFile(optCon, LIBRPMALIAS_FILENAME);
379     (void) poptReadDefaultConfig(optCon, 1);
380     poptSetExecPath(optCon, RPMCONFIGDIR, 1);
381
382     /* Process all options, whine if unknown. */
383     while ((rc = poptGetNextOpt(optCon)) > 0) {
384         optArg = poptGetOptArg(optCon);
385         switch (rc) {
386         default:
387 /*@-nullpass@*/
388             fprintf(stderr, _("%s: option table misconfigured (%d)\n"),
389                 __progname, rc);
390 /*@=nullpass@*/
391             exit(EXIT_FAILURE);
392
393             /*@notreached@*/ /*@switchbreak@*/ break;
394         }
395     }
396
397     if (rc < -1) {
398 /*@-nullpass@*/
399         fprintf(stderr, "%s: %s: %s\n", __progname,
400                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
401                 poptStrerror(rc));
402 /*@=nullpass@*/
403         exit(EXIT_FAILURE);
404     }
405
406     /* Read rpm configuration (if not already read). */
407     rpmcliConfigured();
408
409     if (_debug) {
410         rpmIncreaseVerbosity();
411         rpmIncreaseVerbosity();
412     }
413
414     return optCon;
415 }
416 /*@=globstate@*/