- fix: popt exec doesn't add '--', --target et al no longer need '='.
[tools/librpm-tizen.git] / rpmqv.c
1 #include "system.h"
2
3 #define _AUTOHELP
4
5 #if defined(IAM_RPM) || defined(__LCLINT__)
6 #define IAM_RPMBT
7 #define IAM_RPMDB
8 #define IAM_RPMEIU
9 #define IAM_RPMQV
10 #define IAM_RPMK
11 #endif
12
13 #include <rpmbuild.h>
14 #include <rpmurl.h>
15
16 #ifdef  IAM_RPMBT
17 #include "build.h"
18 #define GETOPT_REBUILD          1003
19 #define GETOPT_RECOMPILE        1004
20 #endif
21
22 #ifdef  IAM_RPMDB
23 #define GETOPT_REBUILDDB        1013
24 #define GETOPT_VERIFYDB         1023
25 static int initdb = 0;
26 #endif
27
28 #ifdef  IAM_RPMEIU
29 #define GETOPT_INSTALL          1014
30 #define GETOPT_RELOCATE         1016
31 #define GETOPT_EXCLUDEPATH      1019
32 static int incldocs = 0;
33 /*@null@*/ static const char * prefix = NULL;
34 #endif  /* IAM_RPMEIU */
35
36 #ifdef  IAM_RPMK
37 #define GETOPT_ADDSIGN          1005
38 #define GETOPT_RESIGN           1006
39 static int noGpg = 0;
40 static int noPgp = 0;
41 #endif  /* IAM_RPMK */
42
43 #if defined(IAM_RPMBT) || defined(IAM_RPMK)
44 #include "signature.h"
45 #endif
46
47 #include "debug.h"
48
49 #define GETOPT_DBPATH           1010
50 #define GETOPT_SHOWRC           1018
51 #define GETOPT_DEFINEMACRO      1020
52 #define GETOPT_EVALMACRO        1021
53 #ifdef  NOTYET
54 #define GETOPT_RCFILE           1022
55 #endif
56
57 enum modes {
58
59     MODE_QUERY          = (1 <<  0),
60     MODE_VERIFY         = (1 <<  3),
61     MODE_QUERYTAGS      = (1 <<  9),
62 #define MODES_QV (MODE_QUERY | MODE_VERIFY)
63
64     MODE_INSTALL        = (1 <<  1),
65     MODE_UNINSTALL      = (1 <<  2),
66 #define MODES_IE (MODE_INSTALL | MODE_UNINSTALL)
67
68     MODE_BUILD          = (1 <<  4),
69     MODE_REBUILD        = (1 <<  5),
70     MODE_RECOMPILE      = (1 <<  8),
71     MODE_TARBUILD       = (1 << 11),
72 #define MODES_BT (MODE_BUILD | MODE_TARBUILD | MODE_REBUILD | MODE_RECOMPILE)
73
74     MODE_CHECKSIG       = (1 <<  6),
75     MODE_RESIGN         = (1 <<  7),
76 #define MODES_K  (MODE_CHECKSIG | MODES_RESIGN)
77
78     MODE_INITDB         = (1 << 10),
79     MODE_REBUILDDB      = (1 << 12),
80     MODE_VERIFYDB       = (1 << 13),
81 #define MODES_DB (MODE_INITDB | MODE_REBUILDDB | MODE_VERIFYDB)
82
83     MODE_UNKNOWN        = 0
84 };
85
86 #define MODES_FOR_DBPATH        (MODES_BT | MODES_IE | MODES_QV | MODES_DB)
87 #define MODES_FOR_NODEPS        (MODES_BT | MODES_IE | MODE_VERIFY)
88 #define MODES_FOR_TEST          (MODES_BT | MODES_IE)
89 #define MODES_FOR_ROOT          (MODES_BT | MODES_IE | MODES_QV | MODES_DB)
90
91 extern int _ftp_debug;
92 extern int noLibio;
93 extern int _rpmio_debug;
94 extern int _url_debug;
95 extern int _noDirTokens;
96
97 extern const char * rpmNAME;
98 extern const char * rpmEVR;
99 extern int rpmFLAGS;
100
101 extern struct MacroContext_s rpmCLIMacroContext;
102
103 /* options for all executables */
104
105 static int help = 0;
106 static int noUsageMsg = 0;
107 /*@null@*/ static const char * pipeOutput = NULL;
108 static int quiet = 0;
109 /*@null@*/ static const char * rcfile = NULL;
110 /*@null@*/ static char * rootdir = "/";
111 static int showrc = 0;
112 static int showVersion = 0;
113
114 #if defined(IAM_RPMBT) || defined(IAM_RPMK)
115 static int signIt = 0;
116 #endif  /* IAM_RPMBT || IAM_RPMK */
117
118 #if defined(IAM_RPMQV) || defined(IAM_RPMK)
119 static int noMd5 = 0;
120 #endif
121
122 #if defined(IAM_RPMEIU)
123 static int noDeps = 0;
124 #endif
125
126 #if defined(IAM_RPMEIU)
127 static int force = 0;
128 #endif
129
130 static struct poptOption rpmAllPoptTable[] = {
131  { "version", '\0', 0, &showVersion, 0,
132         N_("print the version of rpm being used"),
133         NULL },
134  { "quiet", '\0', 0, &quiet, 0,
135         N_("provide less detailed output"), NULL},
136  { "verbose", 'v', 0, 0, 'v',
137         N_("provide more detailed output"), NULL},
138  { "define", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, GETOPT_DEFINEMACRO,
139         N_("define macro <name> with value <body>"),
140         N_("'<name> <body>'") },
141  { "eval", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, 0, GETOPT_EVALMACRO,
142         N_("print macro expansion of <expr>+"),
143         N_("<expr>+") },
144  { "pipe", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, &pipeOutput, 0,
145         N_("send stdout to <cmd>"),
146         N_("<cmd>") },
147  { "root", 'r', POPT_ARG_STRING, &rootdir, 0,
148         N_("use <dir> as the top level directory"),
149         N_("<dir>") },
150  { "macros", '\0', POPT_ARG_STRING, &macrofiles, 0,
151         N_("read <file:...> instead of default macro file(s)"),
152         N_("<file:...>") },
153 #if !defined(GETOPT_RCFILE)
154  { "rcfile", '\0', POPT_ARG_STRING, &rcfile, 0,
155         N_("read <file:...> instead of default rpmrc file(s)"),
156         N_("<file:...>") },
157 #else
158  { "rcfile", '\0', 0, 0, GETOPT_RCFILE, 
159         N_("read <file:...> instead of default rpmrc file(s)"),
160         N_("<file:...>") },
161 #endif
162  { "showrc", '\0', 0, &showrc, GETOPT_SHOWRC,
163         N_("display final rpmrc and macro configuration"),
164         NULL },
165
166 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
167  { "nolibio", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &noLibio, 1,
168         N_("disable use of libio(3) API"), NULL},
169 #endif
170  { "ftpdebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_ftp_debug, -1,
171         N_("debug protocol data stream"), NULL},
172  { "rpmiodebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmio_debug, -1,
173         N_("debug rpmio I/O"), NULL},
174  { "urldebug", '\0', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_url_debug, -1,
175         N_("debug URL cache handling"), NULL},
176
177    POPT_TABLEEND
178 };
179
180 #ifdef  IAM_RPMDB
181 static struct poptOption rpmDatabasePoptTable[] = {
182  { "initdb", '\0', 0, &initdb, 0,
183         N_("initialize database"), NULL},
184  { "rebuilddb", '\0', 0, 0, GETOPT_REBUILDDB,
185         N_("rebuild database inverted lists from installed package headers"),
186         NULL},
187  { "verifydb", '\0', 0, 0, GETOPT_VERIFYDB,
188         N_("verify database files"),
189         NULL},
190  { "nodirtokens", '\0', POPT_ARG_VAL, &_noDirTokens, 1,
191         N_("generate headers compatible with (legacy) rpm[23] packaging"),
192         NULL},
193  { "dirtokens", '\0', POPT_ARG_VAL, &_noDirTokens, 0,
194         N_("generate headers compatible with rpm4 packaging"), NULL},
195
196    POPT_TABLEEND
197 };
198 #endif  /* IAM_RPMDB */
199
200 #ifdef  IAM_RPMK
201 static struct poptOption rpmSignPoptTable[] = {
202  { "addsign", '\0', 0, 0, GETOPT_ADDSIGN,
203         N_("add a signature to a package"), NULL },
204  { "resign", '\0', 0, 0, GETOPT_RESIGN,
205         N_("sign a package (discard current signature)"), NULL },
206  { "sign", '\0', 0, &signIt, 0,
207         N_("generate PGP/GPG signature"), NULL },
208  { "checksig", 'K', 0, 0, 'K',
209         N_("verify package signature"), NULL },
210  { "nogpg", '\0', 0, &noGpg, 0,
211         N_("skip any PGP signatures"), NULL },
212  { "nopgp", '\0', 0, &noPgp, 0,
213         N_("skip any GPG signatures"), NULL },
214  { "nomd5", '\0', 0, &noMd5, 0,
215         N_("do not verify file md5 checksums"), NULL },
216
217    POPT_TABLEEND
218 };
219 #endif  /* IAM_RPMK */
220
221 #ifdef  IAM_RPMEIU
222 static rpmtransFlags transFlags = RPMTRANS_FLAG_NONE;
223 static rpmprobFilterFlags probFilter = RPMPROB_FILTER_NONE;
224 static rpmInstallInterfaceFlags installInterfaceFlags = INSTALL_NONE;
225 static rpmEraseInterfaceFlags eraseInterfaceFlags = UNINSTALL_NONE;
226
227 #define _POPT_SET_BIT   (POPT_ARG_VAL|POPT_ARGFLAG_OR)
228
229 static struct poptOption rpmInstallPoptTable[] = {
230  { "allfiles", '\0', _POPT_SET_BIT, &transFlags, RPMTRANS_FLAG_ALLFILES,
231   N_("install all files, even configurations which might otherwise be skipped"),
232         NULL},
233  { "allmatches", '\0', _POPT_SET_BIT, &eraseInterfaceFlags, UNINSTALL_ALLMATCHES,
234         N_("remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)"),
235         NULL},
236
237  { "apply", '\0', _POPT_SET_BIT, &transFlags,
238         (_noTransScripts|_noTransTriggers|
239                 RPMTRANS_FLAG_APPLYONLY|RPMTRANS_FLAG_PKGCOMMIT),
240         N_("do not execute package scriptlet(s)"), NULL },
241
242  { "badreloc", '\0', _POPT_SET_BIT, &probFilter, RPMPROB_FILTER_FORCERELOCATE,
243         N_("relocate files in non-relocateable package"), NULL},
244  { "dirstash", '\0', _POPT_SET_BIT, &transFlags, RPMTRANS_FLAG_DIRSTASH,
245         N_("save erased package files by renaming into sub-directory"), NULL},
246  { "erase", 'e', 0, 0, 'e',
247         N_("erase (uninstall) package"), N_("<package>+") },
248  { "excludedocs", '\0', _POPT_SET_BIT, &transFlags, RPMTRANS_FLAG_NODOCS,
249         N_("do not install documentation"), NULL},
250  { "excludepath", '\0', POPT_ARG_STRING, 0, GETOPT_EXCLUDEPATH,
251         N_("skip files with leading component <path> "),
252         N_("<path>") },
253  { "force", '\0', 0, &force, 0,
254         N_("short hand for --replacepkgs --replacefiles"), NULL},
255  { "freshen", 'F', _POPT_SET_BIT, &installInterfaceFlags,
256         (INSTALL_UPGRADE|INSTALL_FRESHEN),
257         N_("upgrade package(s) if already installed"),
258         N_("<packagefile>+") },
259  { "hash", 'h', _POPT_SET_BIT, &installInterfaceFlags, INSTALL_HASH,
260         N_("print hash marks as package installs (good with -v)"), NULL},
261  { "ignorearch", '\0', _POPT_SET_BIT, &probFilter, RPMPROB_FILTER_IGNOREARCH,
262         N_("don't verify package architecture"), NULL},
263  { "ignoreos", '\0', _POPT_SET_BIT, &probFilter, RPMPROB_FILTER_IGNOREOS,
264         N_("don't verify package operating system"), NULL},
265  { "ignoresize", '\0', _POPT_SET_BIT, &probFilter,
266         (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES),
267         N_("don't check disk space before installing"), NULL},
268  { "includedocs", '\0', 0, &incldocs, 0,
269         N_("install documentation"), NULL},
270  { "install", '\0', 0, 0, GETOPT_INSTALL,
271         N_("install package"), N_("<packagefile>+") },
272  { "justdb", '\0', _POPT_SET_BIT, &transFlags, RPMTRANS_FLAG_JUSTDB,
273         N_("update the database, but do not modify the filesystem"), NULL},
274  { "nodeps", '\0', 0, &noDeps, 0,
275         N_("do not verify package dependencies"), NULL },
276  { "noorder", '\0', _POPT_SET_BIT, &installInterfaceFlags, INSTALL_NOORDER,
277         N_("do not reorder package installation to satisfy dependencies"),
278         NULL},
279
280  { "noscripts", '\0', _POPT_SET_BIT, &transFlags,
281         (_noTransScripts|_noTransTriggers),
282         N_("do not execute package scriptlet(s)"), NULL },
283  { "nopre", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
284         RPMTRANS_FLAG_NOPRE,
285         N_("do not execute %%pre scriptlet (if any)"), NULL },
286  { "nopost", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
287         RPMTRANS_FLAG_NOPOST,
288         N_("do not execute %%post scriptlet (if any)"), NULL },
289  { "nopreun", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
290         RPMTRANS_FLAG_NOPREUN,
291         N_("do not execute %%preun scriptlet (if any)"), NULL },
292  { "nopostun", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
293         RPMTRANS_FLAG_NOPOSTUN,
294         N_("do not execute %%postun scriptlet (if any)"), NULL },
295
296  { "notriggers", '\0', _POPT_SET_BIT, &transFlags,
297         _noTransTriggers,
298         N_("do not execute any scriptlet(s) triggered by this package"), NULL},
299  { "notriggerprein", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
300         RPMTRANS_FLAG_NOTRIGGERPREIN,
301         N_("do not execute any %%triggerprein scriptlet(s)"), NULL},
302  { "notriggerin", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
303         RPMTRANS_FLAG_NOTRIGGERIN,
304         N_("do not execute any %%triggerin scriptlet(s)"), NULL},
305  { "notriggerun", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
306         RPMTRANS_FLAG_NOTRIGGERUN,
307         N_("do not execute any %%triggerun scriptlet(s)"), NULL},
308  { "notriggerpostun", '\0', _POPT_SET_BIT|POPT_ARGFLAG_DOC_HIDDEN, &transFlags,
309         RPMTRANS_FLAG_NOTRIGGERPOSTUN,
310         N_("do not execute any %%triggerpostun scriptlet(s)"), NULL},
311
312  { "oldpackage", '\0', _POPT_SET_BIT, &probFilter, RPMPROB_FILTER_OLDPACKAGE,
313         N_("upgrade to an old version of the package (--force on upgrades does this automatically)"),
314         NULL},
315  { "percent", '\0', _POPT_SET_BIT, &installInterfaceFlags, INSTALL_PERCENT,
316         N_("print percentages as package installs"), NULL},
317  { "prefix", '\0', POPT_ARG_STRING, &prefix, 0,
318         N_("relocate the package to <dir>, if relocatable"),
319         N_("<dir>") },
320  { "relocate", '\0', POPT_ARG_STRING, 0, GETOPT_RELOCATE,
321         N_("relocate files from path <old> to <new>"),
322         N_("<old>=<new>") },
323  { "repackage", '\0', _POPT_SET_BIT, &transFlags, RPMTRANS_FLAG_REPACKAGE,
324         N_("save erased package files by repackaging"), NULL},
325  { "replacefiles", '\0', _POPT_SET_BIT, &probFilter,
326         (RPMPROB_FILTER_REPLACEOLDFILES | RPMPROB_FILTER_REPLACENEWFILES),
327         N_("install even if the package replaces installed files"), NULL},
328  { "replacepkgs", '\0', _POPT_SET_BIT, &probFilter, RPMPROB_FILTER_REPLACEPKG,
329         N_("reinstall if the package is already present"), NULL},
330  { "test", '\0', _POPT_SET_BIT, &transFlags, RPMTRANS_FLAG_TEST,
331         N_("don't install, but tell if it would work or not"), NULL},
332  { "upgrade", 'U', _POPT_SET_BIT, &installInterfaceFlags, INSTALL_UPGRADE,
333         N_("upgrade package(s)"),
334         N_("<packagefile>+") },
335
336    POPT_TABLEEND
337 };
338 #endif  /* IAM_RPMEIU */
339
340 /* the structure describing the options we take and the defaults */
341 static struct poptOption optionsTable[] = {
342 #if !defined(_AUTOHELP)
343  { "help", '\0', 0, &help, 0,                   NULL, NULL},
344 #endif
345
346  /* XXX colliding options */
347 #if defined(IAM_RPMQV) || defined(IAM_RPMEIU) || defined(IAM_RPMBT)
348  {  NULL, 'i', POPT_ARGFLAG_DOC_HIDDEN, 0, 'i',                 NULL, NULL},
349 #endif
350
351 #ifdef  IAM_RPMQV
352  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmQueryPoptTable, 0,
353         N_("Query options (with -q or --query):"),
354         NULL },
355  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmVerifyPoptTable, 0,
356         N_("Verify options (with -V or --verify):"),
357         NULL },
358 #endif  /* IAM_RPMQV */
359
360 #ifdef  IAM_RPMK
361  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmSignPoptTable, 0,
362         N_("Signature options:"),
363         NULL },
364 #endif  /* IAM_RPMK */
365
366 #ifdef  IAM_RPMDB
367  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmDatabasePoptTable, 0,
368         N_("Database options:"),
369         NULL },
370 #endif  /* IAM_RPMDB */
371
372 #ifdef  IAM_RPMBT
373  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmBuildPoptTable, 0,
374         N_("Build options with [ <specfile> | <tarball> | <source package> ]:"),
375         NULL },
376 #endif  /* IAM_RPMBT */
377
378 #ifdef  IAM_RPMEIU
379  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmInstallPoptTable, 0,
380         N_("Install/Upgrade/Erase options:"),
381         NULL },
382 #endif  /* IAM_RPMEIU */
383
384  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmAllPoptTable, 0,
385         N_("Common options for all rpm modes:"),
386         NULL },
387
388    POPT_AUTOHELP
389    POPT_TABLEEND
390 };
391
392 #ifdef __MINT__
393 /* MiNT cannot dynamically increase the stack.  */
394 long _stksize = 64 * 1024L;
395 #endif
396
397 /*@exits@*/ static void argerror(const char * desc)
398 {
399     fprintf(stderr, _("rpm: %s\n"), desc);
400     exit(EXIT_FAILURE);
401 }
402
403 static void printVersion(void) {
404     fprintf(stdout, _("RPM version %s\n"), rpmEVR);
405 }
406
407 static void printBanner(void) {
408     (void) puts(_("Copyright (C) 1998-2000 - Red Hat, Inc."));
409     (void) puts(_("This program may be freely redistributed under the terms of the GNU GPL"));
410 }
411
412 static void printUsage(void) {
413     FILE * fp;
414     printVersion();
415     printBanner();
416     (void) puts("");
417
418     fp = stdout;
419
420     fprintf(fp, _("Usage: %s {--help}\n"), __progname);
421     fprintf(fp,  ("       %s {--version}\n"), __progname);
422
423 #ifdef  IAM_RPMEIU
424 #ifdef  DYING
425 --dbpath        all
426 --ftpproxy etc  all
427 --force         alias for --replacepkgs --replacefiles
428 --includedocs   handle as option in table
429                 --erase forbids many options
430 #endif
431 #endif  /* IAM_RPMEIU */
432
433 #ifdef  IAM_RPMQV
434 #ifdef  DYING   /* XXX popt glue needing --help doco. */
435 --dbpath        all
436 --ftpproxy etc  all
437 -i,--info       Q
438 -R,--requires   Q
439 -P,--provides   Q
440 --scripts       Q
441 --triggeredby   Q
442 --changelog     Q
443 --triggers      Q
444 --querytags     !V
445 --setperms      V
446 --setugids      V
447 #endif  /* DYING */
448 #endif  /* IAM_RPMQV */
449
450 }
451
452 #ifdef  DYING
453 static void printHelpLine(char * prefix, char * help) {
454     int indentLength = strlen(prefix) + 3;
455     int lineLength = 79 - indentLength;
456     int helpLength = strlen(help);
457     char * ch;
458     char format[10];
459
460     fprintf(stdout, "%s - ", prefix);
461
462     while (helpLength > lineLength) {
463         ch = help + lineLength - 1;
464         while (ch > help && !isspace(*ch)) ch--;
465         if (ch == help) break;          /* give up */
466         while (ch > (help + 1) && isspace(*ch)) ch--;
467         ch++;
468
469         sprintf(format, "%%.%ds\n%%%ds", (int) (ch - help), indentLength);
470         fprintf(stdout, format, help, " ");
471         help = ch;
472         while (isspace(*help) && *help) help++;
473         helpLength = strlen(help);
474     }
475
476     if (helpLength) puts(help);
477 }
478
479 static void printHelp(void) {
480     printVersion();
481     printBanner();
482     puts("");
483
484     puts(         _("Usage:"));
485     printHelpLine(  "   --help                 ", 
486                   _("print this message"));
487     printHelpLine(  "   --version              ",
488                   _("print the version of rpm being used"));
489
490     puts("");
491     puts(         _("  All modes support the following options:"));
492     printHelpLine(_("   --define '<name> <body>'"),
493                   _("define macro <name> with value <body>"));
494     printHelpLine(_("   --eval '<expr>+'       "),
495                   _("print the expansion of macro <expr> to stdout"));
496     printHelpLine(_("   --pipe <cmd>           "),
497                   _("send stdout to <cmd>"));
498     printHelpLine(_("   --rcfile <file:...>    "),
499                   _("use <file:...> instead of default list of macro files"));
500     printHelpLine(  "   --showrc               ",
501                   _("display final rpmrc and macro configuration"));
502 #if defined(IAM_RPMBT) || defined(IAM_RPMDB) || defined(IAM_RPMEIU) || defined(IAM_RPMQV)
503     printHelpLine(_("   --dbpath <dir>         "),
504                   _("use <dir> as the directory for the database"));
505     printHelpLine(_("   --root <dir>           "),
506                   _("use <dir> as the top level directory"));
507 #endif  /* IAM_RPMBT || IAM_RPMDB || IAM_RPMEIU || IAM_RPMQV */
508     printHelpLine(  "   -v                     ",
509                   _("be a little more verbose"));
510     printHelpLine(  "   -vv                    ",
511                   _("be incredibly verbose (for debugging)"));
512
513 #if defined(IAM_RPMEIU) || defined(IAM_RPMQV)
514     puts("");
515     puts(         _("  Install, upgrade and query (with -p) modes allow URL's to be used in place"));
516     puts(         _("  of file names as well as the following options:"));
517     printHelpLine(_("     --ftpproxy <host>    "),
518                   _("hostname or IP of ftp proxy"));
519     printHelpLine(_("     --ftpport <port>     "),
520                   _("port number of ftp server (or proxy)"));
521     printHelpLine(_("     --httpproxy <host>   "),
522                   _("hostname or IP of http proxy"));
523     printHelpLine(_("     --httpport <port>    "),
524                   _("port number of http server (or proxy)"));
525 #endif  /* IAM_RPMEIU || IAM_RPMQV */
526
527 #ifdef IAM_RPMQV
528     puts("");
529     puts(         _("  Package specification options:"));
530     printHelpLine(  "     -a, --all            ",
531                   _("query/verify all packages"));
532     printHelpLine(_("     -f <file>+           "),
533                   _("query/verify package owning <file>"));
534     printHelpLine(_("     -p <packagefile>+    "),
535                   _("query/verify (uninstalled) package <packagefile>"));
536     printHelpLine(_("     --triggeredby <pkg>  "),
537                   _("query/verify packages triggered by <pkg>"));
538     printHelpLine(_("     --whatprovides <cap> "),
539                   _("query/verify packages which provide <cap> capability"));
540     printHelpLine(_("     --whatrequires <cap> "),
541                   _("query/verify packages which require <cap> capability"));
542     puts("");
543     printHelpLine(  "  -q, --query             ",
544                   _("query mode"));
545     printHelpLine(_("     --queryformat <qfmt> "),
546                   _("use <qfmt> as the header format (implies --info)"));
547     puts("");
548     puts(         _("    Information selection options:"));
549     printHelpLine(  "       -i, --info         ",
550                   _("display package information"));
551     printHelpLine(  "       --changelog        ",
552                   _("display the package's change log"));
553     printHelpLine(  "       -l                 ",
554                   _("display package file list"));
555     printHelpLine(  "       -s                 ",
556                   _("show file states (implies -l)"));
557     printHelpLine(  "       -d                 ",
558                   _("list only documentation files (implies -l)"));
559     printHelpLine(  "       -c                 ",
560                   _("list only configuration files (implies -l)"));
561     printHelpLine(  "       --dump             ",
562                   _("show all verifiable information for each file (must be used with -l, -c, or -d)"));
563     printHelpLine(  "       --provides         ",
564                   _("list capabilities provided by package"));
565     printHelpLine(  "       -R, --requires     ",
566                   _("list capabilities required by package"));
567     printHelpLine(  "       --scripts          ",
568                   _("print the various [un]install scriptlets"));
569     printHelpLine(  "       --triggers         ",
570                   _("show the trigger scriptlets contained in the package"));
571     puts("");
572     printHelpLine(  "   -V, -y, --verify       ",
573                   _("verify a package installation using the same same package specification options as -q"));
574     printHelpLine(  "     --nodeps             ",
575                   _("do not verify package dependencies"));
576     printHelpLine(  "     --nofiles            ",
577                   _("do not verify file attributes"));
578     printHelpLine(  "     --nomd5              ",
579                   _("do not verify file md5 checksums"));
580     printHelpLine(  "     --noscripts          ",
581                   _("do not execute scripts (if any)"));
582     puts("");
583     printHelpLine(  "   --querytags            ",
584                   _("list the tags that can be used in a query format"));
585     printHelpLine(  "   --setperms             ",
586                   _("set the file permissions to those in the package database"
587                     " using the same package specification options as -q"));
588     printHelpLine(  "   --setugids             ",
589                   _("set the file owner and group to those in the package "
590                     "database using the same package specification options as "
591                     "-q"));
592 #endif  /* IAM_RPMQV */
593
594 #ifdef  IAM_RPMEIU
595     puts("");
596     puts(         _("   --install <packagefile>"));
597     printHelpLine(_("   -i <packagefile>       "),
598                   _("install package"));
599     printHelpLine(  "     --allfiles           ",
600                   _("install all files, even configurations which might "
601                     "otherwise be skipped"));
602     printHelpLine(  "     --badreloc           ",
603                   _("relocate files in non-relocateable package"));
604     printHelpLine(  "     --excludedocs        ",
605                   _("do not install documentation"));
606     printHelpLine(_("     --excludepath <path> "),
607                   _("skip files with leading component <path> "));
608     printHelpLine(  "     --force              ",
609                   _("short hand for --replacepkgs --replacefiles"));
610     printHelpLine(  "     -h, --hash           ",
611                   _("print hash marks as package installs (good with -v)"));
612     printHelpLine(  "     --ignorearch         ",
613                   _("don't verify package architecture"));
614     printHelpLine(  "     --ignoresize         ",
615                   _("don't check disk space before installing"));
616     printHelpLine(  "     --ignoreos           ",
617                   _("don't verify package operating system"));
618     printHelpLine(  "     --includedocs        ",
619                   _("install documentation"));
620     printHelpLine(  "     --justdb             ",
621                   _("update the database, but do not modify the filesystem"));
622     printHelpLine(  "     --nodeps             ",
623                   _("do not verify package dependencies"));
624     printHelpLine(  "     --noorder            ",
625                   _("do not reorder package installation to satisfy dependencies"));
626     printHelpLine(  "     --noscripts          ",
627                   _("don't execute any installation scriptlets"));
628     printHelpLine(  "     --notriggers         ",
629                   _("don't execute any scriptlets triggered by this package"));
630     printHelpLine(  "     --percent            ",
631                   _("print percentages as package installs"));
632     printHelpLine(_("     --prefix <dir>       "),
633                   _("relocate the package to <dir>, if relocatable"));
634     printHelpLine(_("     --relocate <oldpath>=<newpath>"),
635                   _("relocate files from <oldpath> to <newpath>"));
636     printHelpLine(  "     --replacefiles       ",
637                   _("install even if the package replaces installed files"));
638     printHelpLine(  "     --replacepkgs        ",
639                   _("reinstall if the package is already present"));
640     printHelpLine(  "     --test               ",
641                   _("don't install, but tell if it would work or not"));
642     puts("");
643     puts(         _("   --upgrade <packagefile>"));
644     printHelpLine(_("   -U <packagefile>       "),
645                   _("upgrade package (same options as --install, plus)"));
646     printHelpLine(  "     --oldpackage         ",
647                   _("upgrade to an old version of the package (--force on upgrades does this automatically)"));
648     puts("");
649     puts(         _("   --erase <package>"));
650     printHelpLine(_("   -e <package>           "),
651                   _("erase (uninstall) package"));
652     printHelpLine(  "     --allmatches         ",
653                   _("remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)"));
654     printHelpLine(  "     --justdb             ",
655                   _("update the database, but do not modify the filesystem"));
656     printHelpLine(  "     --nodeps             ",
657                   _("do not verify package dependencies"));
658     printHelpLine(  "     --noorder            ",
659                   _("do not reorder package installation to satisfy dependencies"));
660     printHelpLine(  "     --noscripts          ",
661                   _("do not execute any package specific scripts"));
662     printHelpLine(  "     --notriggers         ",
663                   _("don't execute any scripts triggered by this package"));
664 #endif  /* IAM_RPMEIU */
665
666 #ifdef IAM_RPMK
667     puts("");
668     printHelpLine(_("   --resign <pkg>+        "),
669                   _("sign a package (discard current signature)"));
670     printHelpLine(_("   --addsign <pkg>+       "),
671                   _("add a signature to a package"));
672
673     puts(         _("   --checksig <pkg>+"));
674     printHelpLine(_("   -K <pkg>+             "),
675                   _("verify package signature"));
676     printHelpLine(  "     --nopgp              ",
677                   _("skip any PGP signatures"));
678     printHelpLine(  "     --nogpg              ",
679                   _("skip any GPG signatures"));
680     printHelpLine(  "     --nomd5              ",
681                   _("skip any MD5 signatures"));
682 #endif  /* IAM_RPMK */
683
684 #ifdef  IAM_RPMDB
685     puts("");
686     printHelpLine(  "   --initdb               ",
687                   _("initalize database (unnecessary, legacy use)"));
688     printHelpLine(  "   --rebuilddb            ",
689                   _("rebuild database indices from existing database headers"));
690 #endif
691
692 }
693 #endif
694
695 int main(int argc, const char ** argv)
696 {
697     enum modes bigMode = MODE_UNKNOWN;
698
699 #ifdef  IAM_RPMQV
700     QVA_t qva = &rpmQVArgs;
701 #endif
702
703 #ifdef  IAM_RPMBT
704     BTA_t ba = &rpmBTArgs;
705 #endif
706
707 #ifdef  IAM_RPMEIU
708 /*@only@*/ rpmRelocation * relocations = NULL;
709     int numRelocations = 0;
710 #endif
711
712 #if defined(IAM_RPMK)
713     rpmResignFlags addSign = RESIGN_NEW_SIGNATURE;
714     rpmCheckSigFlags checksigFlags = CHECKSIG_NONE;
715 #endif
716
717 #if defined(IAM_RPMBT) || defined(IAM_RPMK)
718     char * passPhrase = "";
719 #endif
720
721     int arg;
722     int gotDbpath = 0;
723
724     const char * optArg;
725     pid_t pipeChild = 0;
726     poptContext optCon;
727     int ec = 0;
728     int status;
729     int p[2];
730         
731 #if HAVE_MCHECK_H && HAVE_MTRACE
732     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
733 #endif
734     setprogname(argv[0]);       /* Retrofit glibc __progname */
735
736     /* XXX glibc churn sanity */
737     if (__progname == NULL) {
738         if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++;
739         else __progname = argv[0];
740     }
741
742     /* Set the major mode based on argv[0] */
743     /*@-nullpass@*/
744 #ifdef  IAM_RPMBT
745     if (!strcmp(__progname, "rpmb"))    bigMode = MODE_BUILD;
746     if (!strcmp(__progname, "rpmt"))    bigMode = MODE_TARBUILD;
747     if (!strcmp(__progname, "rpmbuild"))        bigMode = MODE_BUILD;
748 #endif
749 #ifdef  IAM_RPMQV
750     if (!strcmp(__progname, "rpmq"))    bigMode = MODE_QUERY;
751     if (!strcmp(__progname, "rpmv"))    bigMode = MODE_VERIFY;
752     if (!strcmp(__progname, "rpmquery"))        bigMode = MODE_QUERY;
753     if (!strcmp(__progname, "rpmverify"))       bigMode = MODE_VERIFY;
754 #endif
755 #ifdef  RPMEIU
756     if (!strcmp(__progname, "rpme"))    bigMode = MODE_UNINSTALL;
757     if (!strcmp(__progname, "rpmi"))    bigMode = MODE_INSTALL;
758     if (!strcmp(__progname, "rpmu"))    bigMode = MODE_INSTALL;
759 #endif
760     /*@=nullpass@*/
761
762     /* set the defaults for the various command line options */
763     _ftp_debug = 0;
764
765 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
766     noLibio = 0;
767 #else
768     noLibio = 1;
769 #endif
770     _rpmio_debug = 0;
771     _url_debug = 0;
772
773     /* XXX Eliminate query linkage loop */
774     specedit = 0;
775     parseSpecVec = parseSpec;
776     freeSpecVec = freeSpec;
777
778     /* set up the correct locale */
779     (void) setlocale(LC_ALL, "" );
780
781 #ifdef  __LCLINT__
782 #define LOCALEDIR       "/usr/share/locale"
783 #endif
784     bindtextdomain(PACKAGE, LOCALEDIR);
785     textdomain(PACKAGE);
786
787     rpmSetVerbosity(RPMMESS_NORMAL);    /* XXX silly use by showrc */
788
789     /* Make a first pass through the arguments, looking for --rcfile */
790     /* We need to handle that before dealing with the rest of the arguments. */
791     /*@-nullpass -temptrans@*/
792     optCon = poptGetContext(__progname, argc, argv, optionsTable, 0);
793     /*@=nullpass =temptrans@*/
794     (void) poptReadConfigFile(optCon, LIBRPMALIAS_FILENAME);
795     (void) poptReadDefaultConfig(optCon, 1);
796     poptSetExecPath(optCon, RPMCONFIGDIR, 1);
797
798     /* reading rcfile early makes it easy to override */
799     /* XXX only --rcfile (and --showrc) need this pre-parse */
800
801     while ((arg = poptGetNextOpt(optCon)) > 0) {
802         switch(arg) {
803         case 'v':
804             rpmIncreaseVerbosity();     /* XXX silly use by showrc */
805             break;
806         default:
807             break;
808       }
809     }
810
811     if (rpmReadConfigFiles(rcfile, NULL))  
812         exit(EXIT_FAILURE);
813
814     if (showrc) {
815         (void) rpmShowRC(stdout);
816         exit(EXIT_SUCCESS);
817     }
818
819     rpmSetVerbosity(RPMMESS_NORMAL);    /* XXX silly use by showrc */
820
821     poptResetContext(optCon);
822
823 #ifdef  IAM_RPMQV
824     if (qva->qva_queryFormat) free((void *)qva->qva_queryFormat);
825     memset(qva, 0, sizeof(*qva));
826     qva->qva_source = RPMQV_PACKAGE;
827     qva->qva_mode = ' ';
828     qva->qva_char = ' ';
829 #endif
830
831 #ifdef  IAM_RPMBT
832     if (ba->buildRootOverride) free((void *)ba->buildRootOverride);
833     if (ba->targets) free(ba->targets);
834     memset(ba, 0, sizeof(*ba));
835     ba->buildMode = ' ';
836     ba->buildChar = ' ';
837 #endif
838
839 #ifdef  IAM_RPMEIU
840     transFlags = RPMTRANS_FLAG_NONE;
841     probFilter = RPMPROB_FILTER_NONE;
842     installInterfaceFlags = INSTALL_NONE;
843     eraseInterfaceFlags = UNINSTALL_NONE;
844 #endif
845
846     while ((arg = poptGetNextOpt(optCon)) > 0) {
847         optArg = poptGetOptArg(optCon);
848
849         switch (arg) {
850             
851           case 'v':
852             rpmIncreaseVerbosity();
853             break;
854
855 #if defined(IAM_RPMQV) || defined(IAM_RPMEIU) || defined(IAM_RPMBT)
856           case 'i':
857 #ifdef  IAM_RPMQV
858             if (bigMode == MODE_QUERY) {
859                 /*@-nullassign@*/
860                 const char * infoCommand[] = { "--info", NULL };
861                 /*@=nullassign@*/
862                 (void) poptStuffArgs(optCon, infoCommand);
863             }
864 #endif
865 #ifdef  IAM_RPMEIU
866             if (bigMode == MODE_INSTALL)
867                 /*@-ifempty@*/ ;
868             if (bigMode == MODE_UNKNOWN) {
869                 /*@-nullassign@*/
870                 const char * installCommand[] = { "--install", NULL };
871                 /*@=nullassign@*/
872                 (void) poptStuffArgs(optCon, installCommand);
873             }
874 #endif
875             break;
876 #endif  /* IAM_RPMQV || IAM_RPMEIU || IAM_RPMBT */
877
878 #ifdef  IAM_RPMEIU
879         
880           case 'e':
881             if (bigMode != MODE_UNKNOWN && bigMode != MODE_UNINSTALL)
882                 argerror(_("only one major mode may be specified"));
883             bigMode = MODE_UNINSTALL;
884             break;
885         
886           case GETOPT_INSTALL:
887             if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
888                 argerror(_("only one major mode may be specified"));
889             bigMode = MODE_INSTALL;
890             break;
891
892 #ifdef  DYING   /* XXX handled by popt */
893           case 'U':
894             if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
895                 argerror(_("only one major mode may be specified"));
896             bigMode = MODE_INSTALL;
897             break;
898
899           case 'F':
900             if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
901                 argerror(_("only one major mode may be specified"));
902             bigMode = MODE_INSTALL;
903             break;
904 #endif
905
906           case GETOPT_EXCLUDEPATH:
907             if (optArg == NULL || *optArg != '/') 
908                 argerror(_("exclude paths must begin with a /"));
909
910             relocations = xrealloc(relocations, 
911                                   sizeof(*relocations) * (numRelocations + 1));
912             /*@-observertrans -dependenttrans@*/        /* FIX: W2DO? */
913             relocations[numRelocations].oldPath = optArg;
914             relocations[numRelocations++].newPath = NULL;
915             /*@=observertrans =dependenttrans@*/
916             break;
917
918           case GETOPT_RELOCATE:
919           { char * newPath = NULL;
920             if (optArg == NULL || *optArg != '/') 
921                 argerror(_("relocations must begin with a /"));
922             if (!(newPath = strchr(optArg, '=')))
923                 argerror(_("relocations must contain a ="));
924             *newPath++ = '\0';
925             if (*newPath != '/') 
926                 argerror(_("relocations must have a / following the ="));
927             relocations = xrealloc(relocations, 
928                                   sizeof(*relocations) * (numRelocations + 1));
929             /*@-observertrans -kepttrans@*/     /* FIX: W2DO? */
930             relocations[numRelocations].oldPath = optArg;
931             relocations[numRelocations++].newPath = newPath;
932             /*@=observertrans =kepttrans@*/
933           } break;
934 #endif  /* IAM_RPMEIU */
935
936 #ifdef  IAM_RPMDB
937           case GETOPT_REBUILDDB:
938             if (bigMode != MODE_UNKNOWN && bigMode != MODE_REBUILDDB)
939                 argerror(_("only one major mode may be specified"));
940             bigMode = MODE_REBUILDDB;
941             break;
942           case GETOPT_VERIFYDB:
943             if (bigMode != MODE_UNKNOWN && bigMode != MODE_VERIFYDB)
944                 argerror(_("only one major mode may be specified"));
945             bigMode = MODE_VERIFYDB;
946             break;
947 #endif
948
949 #ifdef  IAM_RPMK
950           case 'K':
951             if (bigMode != MODE_UNKNOWN && bigMode != MODE_CHECKSIG)
952                 argerror(_("only one major mode may be specified"));
953             bigMode = MODE_CHECKSIG;
954             break;
955
956           case GETOPT_RESIGN:
957             if (bigMode != MODE_UNKNOWN && bigMode != MODE_RESIGN)
958                 argerror(_("only one major mode may be specified"));
959             bigMode = MODE_RESIGN;
960             addSign = RESIGN_NEW_SIGNATURE;
961             signIt = 1;
962             break;
963
964           case GETOPT_ADDSIGN:
965             if (bigMode != MODE_UNKNOWN && bigMode != MODE_RESIGN)
966                 argerror(_("only one major mode may be specified"));
967             bigMode = MODE_RESIGN;
968             addSign = RESIGN_ADD_SIGNATURE;
969             signIt = 1;
970             break;
971 #endif  /* IAM_RPMK */
972
973           case GETOPT_DEFINEMACRO:
974             if (optArg) {
975                 (void) rpmDefineMacro(NULL, optArg, RMIL_CMDLINE);
976                 (void) rpmDefineMacro(&rpmCLIMacroContext, optArg,RMIL_CMDLINE);
977             }
978             noUsageMsg = 1;
979             break;
980
981           case GETOPT_EVALMACRO:
982             if (optArg) {
983                 const char *val = rpmExpand(optArg, NULL);
984                 fprintf(stdout, "%s\n", val);
985                 val = _free(val);
986             }
987             noUsageMsg = 1;
988             break;
989
990 #if defined(GETOPT_RCFILE)
991           case GETOPT_RCFILE:
992             fprintf(stderr, _("The --rcfile option has been eliminated.\n"));
993             fprintf(stderr, _("Use \"--macros <file:...>\" instead.\n"));
994             exit(EXIT_FAILURE);
995             /*@notreached@*/ break;
996 #endif
997
998           default:
999             fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
1000             exit(EXIT_FAILURE);
1001         }
1002     }
1003
1004     if (quiet)
1005         rpmSetVerbosity(RPMMESS_QUIET);
1006
1007     if (showVersion) printVersion();
1008
1009     if (arg < -1) {
1010         fprintf(stderr, "%s: %s\n", 
1011                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
1012                 poptStrerror(arg));
1013         exit(EXIT_FAILURE);
1014     }
1015
1016 #ifdef  IAM_RPMBT
1017     switch (ba->buildMode) {
1018     case 'b':   bigMode = MODE_BUILD;           break;
1019     case 't':   bigMode = MODE_TARBUILD;        break;
1020     case 'B':   bigMode = MODE_REBUILD;         break;
1021     case 'C':   bigMode = MODE_RECOMPILE;       break;
1022     }
1023
1024     if ((ba->buildAmount & RPMBUILD_RMSOURCE) && bigMode == MODE_UNKNOWN)
1025         bigMode = MODE_BUILD;
1026
1027     if ((ba->buildAmount & RPMBUILD_RMSPEC) && bigMode == MODE_UNKNOWN)
1028         bigMode = MODE_BUILD;
1029
1030     if (ba->buildRootOverride && bigMode != MODE_BUILD &&
1031         bigMode != MODE_REBUILD && bigMode != MODE_TARBUILD) {
1032         argerror("--buildroot may only be used during package builds");
1033     }
1034 #endif  /* IAM_RPMBT */
1035     
1036 #ifdef  IAM_RPMDB
1037     if (initdb) {
1038         if (bigMode != MODE_UNKNOWN) 
1039             argerror(_("only one major mode may be specified"));
1040         else
1041             bigMode = MODE_INITDB;
1042     }
1043 #endif  /* IAM_RPMDB */
1044
1045 #ifdef  IAM_RPMQV
1046     switch (qva->qva_mode) {
1047     case 'q':   bigMode = MODE_QUERY;           break;
1048     case 'V':   bigMode = MODE_VERIFY;          break;
1049     case 'Q':   bigMode = MODE_QUERYTAGS;       break;
1050     }
1051
1052     if (qva->qva_sourceCount) {
1053         if (qva->qva_sourceCount > 2)
1054             argerror(_("one type of query/verify may be performed at a "
1055                         "time"));
1056     }
1057     if (qva->qva_flags && (bigMode & ~MODES_QV)) 
1058         argerror(_("unexpected query flags"));
1059
1060     if (qva->qva_queryFormat && (bigMode & ~MODES_QV)) 
1061         argerror(_("unexpected query format"));
1062
1063     if (qva->qva_source != RPMQV_PACKAGE && (bigMode & ~MODES_QV)) 
1064         argerror(_("unexpected query source"));
1065 #endif
1066
1067     if (gotDbpath && (bigMode & ~MODES_FOR_DBPATH))
1068         argerror(_("--dbpath given for operation that does not use a "
1069                         "database"));
1070
1071 #if defined(IAM_RPMEIU)
1072
1073     if (!( bigMode == MODE_INSTALL ) && force)
1074         argerror(_("only installation, upgrading, rmsource and rmspec may be forced"));
1075 #endif  /* IAM_RPMEIU */
1076
1077 #ifdef  IAM_RPMEIU
1078     if (bigMode != MODE_INSTALL && (probFilter & RPMPROB_FILTER_FORCERELOCATE))
1079         argerror(_("files may only be relocated during package installation"));
1080
1081     if (relocations && prefix)
1082         argerror(_("only one of --prefix or --relocate may be used"));
1083
1084     if (bigMode != MODE_INSTALL && relocations)
1085         argerror(_("--relocate and --excludepath may only be used when installing new packages"));
1086
1087     if (bigMode != MODE_INSTALL && prefix)
1088         argerror(_("--prefix may only be used when installing new packages"));
1089
1090     if (prefix && prefix[0] != '/') 
1091         argerror(_("arguments to --prefix must begin with a /"));
1092
1093     if (bigMode != MODE_INSTALL && (installInterfaceFlags & INSTALL_HASH))
1094         argerror(_("--hash (-h) may only be specified during package "
1095                         "installation"));
1096
1097     if (bigMode != MODE_INSTALL && (installInterfaceFlags & INSTALL_PERCENT))
1098         argerror(_("--percent may only be specified during package "
1099                         "installation"));
1100
1101     if (bigMode != MODE_INSTALL &&
1102  (probFilter & (RPMPROB_FILTER_REPLACEOLDFILES|RPMPROB_FILTER_REPLACENEWFILES)))
1103         argerror(_("--replacefiles may only be specified during package "
1104                         "installation"));
1105
1106     if (bigMode != MODE_INSTALL && (probFilter & RPMPROB_FILTER_REPLACEPKG))
1107         argerror(_("--replacepkgs may only be specified during package "
1108                         "installation"));
1109
1110     if (bigMode != MODE_INSTALL && (transFlags & RPMTRANS_FLAG_NODOCS))
1111         argerror(_("--excludedocs may only be specified during package "
1112                    "installation"));
1113
1114     if (bigMode != MODE_INSTALL && incldocs)
1115         argerror(_("--includedocs may only be specified during package "
1116                    "installation"));
1117
1118     if (incldocs && (transFlags & RPMTRANS_FLAG_NODOCS))
1119         argerror(_("only one of --excludedocs and --includedocs may be "
1120                  "specified"));
1121   
1122     if (bigMode != MODE_INSTALL && (probFilter & RPMPROB_FILTER_IGNOREARCH))
1123         argerror(_("--ignorearch may only be specified during package "
1124                    "installation"));
1125
1126     if (bigMode != MODE_INSTALL && (probFilter & RPMPROB_FILTER_IGNOREOS))
1127         argerror(_("--ignoreos may only be specified during package "
1128                    "installation"));
1129
1130     if (bigMode != MODE_INSTALL &&
1131         (probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES)))
1132         argerror(_("--ignoresize may only be specified during package "
1133                    "installation"));
1134
1135     if ((eraseInterfaceFlags & UNINSTALL_ALLMATCHES) && bigMode != MODE_UNINSTALL)
1136         argerror(_("--allmatches may only be specified during package "
1137                    "erasure"));
1138
1139     if ((transFlags & RPMTRANS_FLAG_ALLFILES) && bigMode != MODE_INSTALL)
1140         argerror(_("--allfiles may only be specified during package "
1141                    "installation"));
1142
1143     if ((transFlags & RPMTRANS_FLAG_JUSTDB) &&
1144         bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL)
1145         argerror(_("--justdb may only be specified during package "
1146                    "installation and erasure"));
1147 #endif  /* IAM_RPMEIU */
1148
1149 #if defined(IAM_RPMEIU)
1150     if (bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL &&
1151         (transFlags & (RPMTRANS_FLAG_NOSCRIPTS | _noTransScripts | _noTransTriggers)))
1152         argerror(_("script disabling options may only be specified during "
1153                    "package installation and erasure"));
1154
1155     if (bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL &&
1156         (transFlags & (RPMTRANS_FLAG_NOTRIGGERS | _noTransTriggers)))
1157         argerror(_("trigger disabling options may only be specified during "
1158                    "package installation and erasure"));
1159
1160     if (noDeps & (bigMode & ~MODES_FOR_NODEPS))
1161         argerror(_("--nodeps may only be specified during package "
1162                    "building, rebuilding, recompilation, installation,"
1163                    "erasure, and verification"));
1164
1165     if ((transFlags & RPMTRANS_FLAG_TEST) && (bigMode & ~MODES_FOR_TEST))
1166         argerror(_("--test may only be specified during package installation, "
1167                  "erasure, and building"));
1168 #endif  /* IAM_RPMEIU */
1169
1170     if (rootdir && rootdir[1] && (bigMode & ~MODES_FOR_ROOT))
1171         argerror(_("--root (-r) may only be specified during "
1172                  "installation, erasure, querying, and "
1173                  "database rebuilds"));
1174
1175     if (rootdir) {
1176         switch (urlIsURL(rootdir)) {
1177         default:
1178             if (bigMode & MODES_FOR_ROOT)
1179                 break;
1180             /*@fallthrough@*/
1181         case URL_IS_UNKNOWN:
1182             if (rootdir[0] != '/')
1183                 argerror(_("arguments to --root (-r) must begin with a /"));
1184             break;
1185         }
1186     }
1187
1188 #ifdef  IAM_RPMK
1189     if (noPgp && bigMode != MODE_CHECKSIG)
1190         argerror(_("--nopgp may only be used during signature checking"));
1191
1192     if (noGpg && bigMode != MODE_CHECKSIG)
1193         argerror(_("--nogpg may only be used during signature checking"));
1194 #endif
1195
1196 #if defined(IAM_RPMK) || defined(IAM_RPMQV)
1197     if (noMd5 && bigMode != MODE_CHECKSIG && bigMode != MODE_VERIFY)
1198         argerror(_("--nomd5 may only be used during signature checking and "
1199                    "package verification"));
1200 #endif
1201
1202 #if defined(IAM_RPMBT) || defined(IAM_RPMK)
1203 #if defined(IAM_RPMBT)
1204     signIt = ba->sign;
1205 #endif
1206     if (signIt) {
1207         if (bigMode == MODE_REBUILD || bigMode == MODE_BUILD ||
1208             bigMode == MODE_RESIGN || bigMode == MODE_TARBUILD) {
1209             const char ** av;
1210             struct stat sb;
1211             int errors = 0;
1212
1213             if ((av = poptGetArgs(optCon)) == NULL) {
1214                 fprintf(stderr, _("no files to sign\n"));
1215                 errors++;
1216             } else
1217             while (*av) {
1218                 if (stat(*av, &sb)) {
1219                     fprintf(stderr, _("cannot access file %s\n"), *av);
1220                     errors++;
1221                 }
1222                 av++;
1223             }
1224
1225             if (errors) {
1226                 ec = errors;
1227                 goto exit;
1228             }
1229
1230             if (poptPeekArg(optCon)) {
1231                 int sigTag;
1232                 switch (sigTag = rpmLookupSignatureType(RPMLOOKUPSIG_QUERY)) {
1233                   case 0:
1234                     break;
1235                   case RPMSIGTAG_PGP:
1236                     if ((sigTag == RPMSIGTAG_PGP || sigTag == RPMSIGTAG_PGP5) &&
1237                         !rpmDetectPGPVersion(NULL)) {
1238                         fprintf(stderr, _("pgp not found: "));
1239                         ec = EXIT_FAILURE;
1240                         goto exit;
1241                     }   /*@fallthrough@*/
1242                   case RPMSIGTAG_GPG:
1243                     passPhrase = rpmGetPassPhrase(_("Enter pass phrase: "), sigTag);
1244                     if (passPhrase == NULL) {
1245                         fprintf(stderr, _("Pass phrase check failed\n"));
1246                         ec = EXIT_FAILURE;
1247                         goto exit;
1248                     }
1249                     fprintf(stderr, _("Pass phrase is good.\n"));
1250                     passPhrase = xstrdup(passPhrase);
1251                     break;
1252                   default:
1253                     fprintf(stderr,
1254                             _("Invalid %%_signature spec in macro file.\n"));
1255                     ec = EXIT_FAILURE;
1256                     goto exit;
1257                     /*@notreached@*/ break;
1258                 }
1259             }
1260         } else {
1261             argerror(_("--sign may only be used during package building"));
1262         }
1263     } else {
1264         /* Make rpmLookupSignatureType() return 0 ("none") from now on */
1265         (void) rpmLookupSignatureType(RPMLOOKUPSIG_DISABLE);
1266     }
1267 #endif  /* IAM_RPMBT || IAM_RPMK */
1268
1269     if (pipeOutput) {
1270         (void) pipe(p);
1271
1272         if (!(pipeChild = fork())) {
1273             (void) close(p[1]);
1274             (void) dup2(p[0], STDIN_FILENO);
1275             (void) close(p[0]);
1276             (void) execl("/bin/sh", "/bin/sh", "-c", pipeOutput, NULL);
1277             fprintf(stderr, _("exec failed\n"));
1278         }
1279
1280         (void) close(p[0]);
1281         (void) dup2(p[1], STDOUT_FILENO);
1282         (void) close(p[1]);
1283     }
1284         
1285     switch (bigMode) {
1286 #ifdef  IAM_RPMDB
1287       case MODE_INITDB:
1288         (void) rpmdbInit(rootdir, 0644);
1289         break;
1290
1291       case MODE_REBUILDDB:
1292         ec = rpmdbRebuild(rootdir);
1293         break;
1294       case MODE_VERIFYDB:
1295         ec = rpmdbVerify(rootdir);
1296         break;
1297 #if !defined(__LCLINT__)
1298       case MODE_QUERY:
1299       case MODE_VERIFY:
1300       case MODE_QUERYTAGS:
1301       case MODE_INSTALL:
1302       case MODE_UNINSTALL:
1303       case MODE_BUILD:
1304       case MODE_REBUILD:
1305       case MODE_RECOMPILE:
1306       case MODE_TARBUILD:
1307       case MODE_CHECKSIG:
1308       case MODE_RESIGN:
1309         if (!showVersion && !help && !noUsageMsg) printUsage();
1310         break;
1311 #endif
1312 #endif  /* IAM_RPMDB */
1313
1314 #ifdef  IAM_RPMBT
1315       case MODE_REBUILD:
1316       case MODE_RECOMPILE:
1317       { const char * pkg;
1318         while (!rpmIsVerbose())
1319             rpmIncreaseVerbosity();
1320
1321         if (!poptPeekArg(optCon))
1322             argerror(_("no packages files given for rebuild"));
1323
1324         ba->buildAmount = RPMBUILD_PREP | RPMBUILD_BUILD | RPMBUILD_INSTALL;
1325         if (bigMode == MODE_REBUILD) {
1326             ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
1327             ba->buildAmount |= RPMBUILD_RMSOURCE;
1328             ba->buildAmount |= RPMBUILD_RMSPEC;
1329             ba->buildAmount |= RPMBUILD_CLEAN;
1330             ba->buildAmount |= RPMBUILD_RMBUILD;
1331         }
1332
1333         while ((pkg = poptGetArg(optCon))) {
1334             const char * specFile = NULL;
1335             char * cookie = NULL;
1336
1337             ec = rpmInstallSource("", pkg, &specFile, &cookie);
1338             if (ec)
1339                 break;
1340
1341             ba->rootdir = rootdir;
1342             ec = build(specFile, ba, passPhrase, cookie, rcfile);
1343             free(cookie);
1344             cookie = NULL;
1345             free((void *)specFile);
1346             specFile = NULL;
1347
1348             if (ec)
1349                 break;
1350         }
1351       } break;
1352
1353       case MODE_BUILD:
1354       case MODE_TARBUILD:
1355       { const char * pkg;
1356         while (!rpmIsVerbose())
1357             rpmIncreaseVerbosity();
1358        
1359         switch (ba->buildChar) {
1360           case 'a':
1361             ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
1362             /*@fallthrough@*/
1363           case 'b':
1364             ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
1365             ba->buildAmount |= RPMBUILD_CLEAN;
1366             /*@fallthrough@*/
1367           case 'i':
1368             ba->buildAmount |= RPMBUILD_INSTALL;
1369             if ((ba->buildChar == 'i') && ba->shortCircuit)
1370                 break;
1371             /*@fallthrough@*/
1372           case 'c':
1373             ba->buildAmount |= RPMBUILD_BUILD;
1374             if ((ba->buildChar == 'c') && ba->shortCircuit)
1375                 break;
1376             /*@fallthrough@*/
1377           case 'p':
1378             ba->buildAmount |= RPMBUILD_PREP;
1379             break;
1380             
1381           case 'l':
1382             ba->buildAmount |= RPMBUILD_FILECHECK;
1383             break;
1384           case 's':
1385             ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
1386             break;
1387         }
1388
1389         if (!poptPeekArg(optCon)) {
1390             if (bigMode == MODE_BUILD)
1391                 argerror(_("no spec files given for build"));
1392             else
1393                 argerror(_("no tar files given for build"));
1394         }
1395
1396         while ((pkg = poptGetArg(optCon))) {
1397             ba->rootdir = rootdir;
1398             ec = build(pkg, ba, passPhrase, NULL, rcfile);
1399             if (ec)
1400                 break;
1401             rpmFreeMacros(NULL);
1402             (void) rpmReadConfigFiles(rcfile, NULL);
1403         }
1404       } break;
1405
1406 #if !defined(__LCLINT__)
1407       case MODE_QUERY:
1408       case MODE_VERIFY:
1409       case MODE_QUERYTAGS:
1410       case MODE_INSTALL:
1411       case MODE_UNINSTALL:
1412       case MODE_CHECKSIG:
1413       case MODE_RESIGN:
1414       case MODE_INITDB:
1415       case MODE_REBUILDDB:
1416       case MODE_VERIFYDB:
1417         if (!showVersion && !help && !noUsageMsg) printUsage();
1418         break;
1419 #endif
1420 #endif  /* IAM_RPMBT */
1421
1422 #ifdef  IAM_RPMEIU
1423       case MODE_UNINSTALL:
1424         if (!poptPeekArg(optCon))
1425             argerror(_("no packages given for uninstall"));
1426
1427         if (noDeps) eraseInterfaceFlags |= UNINSTALL_NODEPS;
1428
1429         ec = rpmErase(rootdir, (const char **)poptGetArgs(optCon), 
1430                          transFlags, eraseInterfaceFlags);
1431         break;
1432
1433       case MODE_INSTALL:
1434         if (force) {
1435             probFilter |= RPMPROB_FILTER_REPLACEPKG | 
1436                           RPMPROB_FILTER_REPLACEOLDFILES |
1437                           RPMPROB_FILTER_REPLACENEWFILES |
1438                           RPMPROB_FILTER_OLDPACKAGE;
1439         }
1440
1441         /* RPMTRANS_FLAG_BUILD_PROBS */
1442         /* RPMTRANS_FLAG_KEEPOBSOLETE */
1443
1444         if (!incldocs) {
1445             if (transFlags & RPMTRANS_FLAG_NODOCS)
1446                 ;
1447             else if (rpmExpandNumeric("%{_excludedocs}"))
1448                 transFlags |= RPMTRANS_FLAG_NODOCS;
1449         }
1450
1451         if (noDeps) installInterfaceFlags |= INSTALL_NODEPS;
1452
1453         if (!poptPeekArg(optCon))
1454             argerror(_("no packages given for install"));
1455
1456         /* we've already ensured !(!prefix && !relocations) */
1457         if (prefix) {
1458             relocations = alloca(2 * sizeof(*relocations));
1459             relocations[0].oldPath = NULL;   /* special case magic */
1460             relocations[0].newPath = prefix;
1461             relocations[1].oldPath = relocations[1].newPath = NULL;
1462         } else if (relocations) {
1463             relocations = xrealloc(relocations, 
1464                                   sizeof(*relocations) * (numRelocations + 1));
1465             relocations[numRelocations].oldPath = NULL;
1466             relocations[numRelocations].newPath = NULL;
1467         }
1468
1469         ec += rpmInstall(rootdir, (const char **)poptGetArgs(optCon), 
1470                         transFlags, installInterfaceFlags, probFilter,
1471                         relocations);
1472         break;
1473 #if !defined(__LCLINT__)
1474       case MODE_QUERY:
1475       case MODE_VERIFY:
1476       case MODE_QUERYTAGS:
1477       case MODE_BUILD:
1478       case MODE_REBUILD:
1479       case MODE_RECOMPILE:
1480       case MODE_TARBUILD:
1481       case MODE_CHECKSIG:
1482       case MODE_RESIGN:
1483       case MODE_INITDB:
1484       case MODE_REBUILDDB:
1485       case MODE_VERIFYDB:
1486         if (!showVersion && !help && !noUsageMsg) printUsage();
1487         break;
1488 #endif
1489 #endif  /* IAM_RPMEIU */
1490
1491 #ifdef  IAM_RPMQV
1492       case MODE_QUERY:
1493       { const char * pkg;
1494
1495         qva->qva_prefix = rootdir;
1496         if (qva->qva_source == RPMQV_ALL) {
1497             if (poptPeekArg(optCon))
1498                 argerror(_("extra arguments given for query of all packages"));
1499
1500             ec = rpmQuery(qva, RPMQV_ALL, NULL);
1501         } else {
1502             if (!poptPeekArg(optCon))
1503                 argerror(_("no arguments given for query"));
1504             while ((pkg = poptGetArg(optCon)))
1505                 ec += rpmQuery(qva, qva->qva_source, pkg);
1506         }
1507       } break;
1508
1509       case MODE_VERIFY:
1510       { const char * pkg;
1511         int verifyFlags;
1512
1513         verifyFlags = (VERIFY_FILES|VERIFY_DEPS|VERIFY_SCRIPT|VERIFY_MD5);
1514         verifyFlags &= ~qva->qva_flags;
1515
1516         qva->qva_prefix = rootdir;
1517         qva->qva_flags = verifyFlags;
1518         if (qva->qva_source == RPMQV_ALL) {
1519             if (poptPeekArg(optCon))
1520                 argerror(_("extra arguments given for verify of all packages"));
1521             ec = rpmVerify(qva, RPMQV_ALL, NULL);
1522         } else {
1523             if (!poptPeekArg(optCon))
1524                 argerror(_("no arguments given for verify"));
1525             while ((pkg = poptGetArg(optCon)))
1526                 ec += rpmVerify(qva, qva->qva_source, pkg);
1527         }
1528       } break;
1529
1530       case MODE_QUERYTAGS:
1531         if (argc != 2)
1532             argerror(_("unexpected arguments to --querytags "));
1533
1534         rpmDisplayQueryTags(stdout);
1535         break;
1536
1537 #if !defined(__LCLINT__)
1538       case MODE_INSTALL:
1539       case MODE_UNINSTALL:
1540       case MODE_BUILD:
1541       case MODE_REBUILD:
1542       case MODE_RECOMPILE:
1543       case MODE_TARBUILD:
1544       case MODE_CHECKSIG:
1545       case MODE_RESIGN:
1546       case MODE_INITDB:
1547       case MODE_REBUILDDB:
1548       case MODE_VERIFYDB:
1549         if (!showVersion && !help && !noUsageMsg) printUsage();
1550         break;
1551 #endif
1552 #endif  /* IAM_RPMQV */
1553
1554 #ifdef IAM_RPMK
1555       case MODE_CHECKSIG:
1556         if (!poptPeekArg(optCon))
1557             argerror(_("no packages given for signature check"));
1558         if (!noPgp) checksigFlags |= CHECKSIG_PGP;
1559         if (!noGpg) checksigFlags |= CHECKSIG_GPG;
1560         if (!noMd5) checksigFlags |= CHECKSIG_MD5;
1561         ec = rpmCheckSig(checksigFlags, (const char **)poptGetArgs(optCon));
1562         /* XXX don't overflow single byte exit status */
1563         if (ec > 255) ec = 255;
1564         break;
1565
1566       case MODE_RESIGN:
1567         if (!poptPeekArg(optCon))
1568             argerror(_("no packages given for signing"));
1569         ec = rpmReSign(addSign, passPhrase, (const char **)poptGetArgs(optCon));
1570         /* XXX don't overflow single byte exit status */
1571         if (ec > 255) ec = 255;
1572         break;
1573 #if !defined(__LCLINT__)
1574       case MODE_QUERY:
1575       case MODE_VERIFY:
1576       case MODE_QUERYTAGS:
1577       case MODE_INSTALL:
1578       case MODE_UNINSTALL:
1579       case MODE_BUILD:
1580       case MODE_REBUILD:
1581       case MODE_RECOMPILE:
1582       case MODE_TARBUILD:
1583       case MODE_INITDB:
1584       case MODE_REBUILDDB:
1585       case MODE_VERIFYDB:
1586         if (!showVersion && !help && !noUsageMsg) printUsage();
1587         break;
1588 #endif
1589 #endif  /* IAM_RPMK */
1590         
1591       case MODE_UNKNOWN:
1592         if (!showVersion && !help && !noUsageMsg) printUsage();
1593         break;
1594
1595     }
1596
1597 #if defined(IAM_RPMBT) || defined(IAM_RPMK)
1598 exit:
1599 #endif  /* IAM_RPMBT || IAM_RPMK */
1600     optCon = poptFreeContext(optCon);
1601     rpmFreeMacros(NULL);
1602     rpmFreeMacros(&rpmCLIMacroContext);
1603     rpmFreeRpmrc();
1604
1605     if (pipeChild) {
1606         (void) fclose(stdout);
1607         (void) waitpid(pipeChild, &status, 0);
1608     }
1609
1610     /* keeps memory leak checkers quiet */
1611     freeNames();
1612     freeFilesystems();
1613     urlFreeCache();
1614
1615 #ifdef  IAM_RPMQV
1616     qva->qva_queryFormat = _free(qva->qva_queryFormat);
1617 #endif
1618
1619 #ifdef  IAM_RPMBT
1620     ba->buildRootOverride = _free(ba->buildRootOverride);
1621     ba->targets = _free(ba->targets);
1622 #endif
1623
1624 #if HAVE_MCHECK_H && HAVE_MTRACE
1625     muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
1626 #endif
1627     /*@-globstate@*/
1628     return ec;
1629     /*@=globstate@*/
1630 }