Annotate function args.
[tools/librpm-tizen.git] / rpm.c
1 #include "system.h"
2
3 #include <rpmcli.h>
4 #include <rpmbuild.h>
5
6 #include "build.h"
7 #include "signature.h"
8 #include "debug.h"
9
10 #define GETOPT_ADDSIGN          1005
11 #define GETOPT_RESIGN           1006
12 #define GETOPT_DBPATH           1010
13 #define GETOPT_REBUILDDB        1013
14 #define GETOPT_INSTALL          1014
15 #define GETOPT_RELOCATE         1016
16 #define GETOPT_SHOWRC           1018
17 #define GETOPT_EXCLUDEPATH      1019
18 #define GETOPT_DEFINEMACRO      1020
19 #define GETOPT_EVALMACRO        1021
20 #define GETOPT_RCFILE           1022
21 #define GETOPT_VERIFYDB         1023
22
23 enum modes {
24     MODE_UNKNOWN        = 0,
25     MODE_QUERY          = (1 <<  0),
26     MODE_INSTALL        = (1 <<  1),
27     MODE_ERASE          = (1 <<  2),
28     MODE_VERIFY         = (1 <<  3),
29     MODE_BUILD          = (1 <<  4),
30     MODE_REBUILD        = (1 <<  5),
31     MODE_CHECKSIG       = (1 <<  6),
32     MODE_RESIGN         = (1 <<  7),
33     MODE_RECOMPILE      = (1 <<  8),
34     MODE_QUERYTAGS      = (1 <<  9),
35     MODE_INITDB         = (1 << 10),
36     MODE_TARBUILD       = (1 << 11),
37     MODE_REBUILDDB      = (1 << 12),
38     MODE_VERIFYDB       = (1 << 13)
39 };
40
41 #define MODES_QV (MODE_QUERY | MODE_VERIFY)
42 #define MODES_BT (MODE_BUILD | MODE_TARBUILD | MODE_REBUILD | MODE_RECOMPILE)
43 #define MODES_IE (MODE_INSTALL | MODE_ERASE)
44 #define MODES_DB (MODE_INITDB | MODE_REBUILDDB | MODE_VERIFYDB)
45 #define MODES_K  (MODE_CHECKSIG | MODES_RESIGN)
46
47 #define MODES_FOR_DBPATH        (MODES_BT | MODES_IE | MODES_QV | MODES_DB)
48 #define MODES_FOR_NODEPS        (MODES_BT | MODES_IE | MODE_VERIFY)
49 #define MODES_FOR_TEST          (MODES_BT | MODES_IE)
50 #define MODES_FOR_ROOT          (MODES_BT | MODES_IE | MODES_QV | MODES_DB)
51
52 /* the flags for the various options */
53 static int allFiles;
54 static int allMatches;
55 static int applyOnly;
56 static int badReloc;
57 static int dirStash;
58 static int excldocs;
59 static int force;
60 extern int _fsm_debug;
61 extern int _ftp_debug;
62 static int showHash;
63 static int help;
64 static int ignoreArch;
65 static int ignoreOs;
66 static int ignoreSize;
67 static int incldocs;
68 static int initdb;
69 static int justdb;
70 static int noDeps;
71 static int noGpg;
72 extern int noLibio;
73 static int noMd5;
74 static int noOrder;
75 static int noPgp;
76
77 static int noScripts;
78 static int noPre;
79 static int noPost;
80 static int noPreun;
81 static int noPostun;
82
83 static int noTriggers;
84 static int noTPrein;
85 static int noTIn;
86 static int noTUn;
87 static int noTPostun;
88
89 static int noUsageMsg;
90 static int oldPackage;
91 static char * pipeOutput;
92 static char * prefix;
93 static int quiet;
94 static char * rcfile;
95
96 static int rePackage;
97 static int pkgCommit;
98 static int pkgUndo;
99 static int tsCommit;
100 static int tsUndo;
101
102 static int replaceFiles;
103 static int replacePackages;
104 static char * rootdir;
105 extern int _rpmio_debug;
106 static int showPercents;
107 static int showrc;
108 static int signIt;
109 static int test;
110 extern int _url_debug;
111 extern int _noDirTokens;
112 extern int _useDbiMajor;
113
114 static int showVersion;
115 extern const char * rpmNAME;
116 extern const char * rpmEVR;
117 extern int rpmFLAGS;
118
119 extern struct MacroContext_s rpmCLIMacroContext;
120
121 extern struct rpmBuildArguments_s       rpmBTArgs;
122
123 /* the structure describing the options we take and the defaults */
124 static struct poptOption optionsTable[] = {
125  { "addsign", '\0', 0, 0, GETOPT_ADDSIGN,       NULL, NULL},
126  { "allfiles", '\0', 0, &allFiles, 0,           NULL, NULL},
127  { "allmatches", '\0', 0, &allMatches, 0,       NULL, NULL},
128  { "apply", '\0', 0, &applyOnly, 0,             NULL, NULL},
129  { "badreloc", '\0', 0, &badReloc, 0,           NULL, NULL},
130  { "checksig", 'K', 0, 0, 'K',                  NULL, NULL},
131  { "define", '\0', POPT_ARG_STRING, 0, GETOPT_DEFINEMACRO,NULL, NULL},
132  { "dirstash", '\0', POPT_ARG_VAL, &dirStash, 1,        NULL, NULL},
133  { "dirtokens", '\0', POPT_ARG_VAL, &_noDirTokens, 0,   NULL, NULL},
134  { "erase", 'e', 0, 0, 'e',                     NULL, NULL},
135  { "eval", '\0', POPT_ARG_STRING, 0, GETOPT_EVALMACRO, NULL, NULL},
136  { "excludedocs", '\0', 0, &excldocs, 0,        NULL, NULL},
137  { "excludepath", '\0', POPT_ARG_STRING, 0, GETOPT_EXCLUDEPATH, NULL, NULL},
138  { "force", '\0', 0, &force, 0,                 NULL, NULL},
139  { "freshen", 'F', 0, 0, 'F',                   NULL, NULL},
140  { "fsmdebug", '\0', POPT_ARG_VAL, &_fsm_debug, -1,             NULL, NULL},
141  { "ftpdebug", '\0', POPT_ARG_VAL, &_ftp_debug, -1,             NULL, NULL},
142  { "hash", 'h', 0, &showHash, 0,                NULL, NULL},
143  { "help", '\0', 0, &help, 0,                   NULL, NULL},
144  {  NULL, 'i', 0, 0, 'i',                       NULL, NULL},
145  { "ignorearch", '\0', 0, &ignoreArch, 0,       NULL, NULL},
146  { "ignoreos", '\0', 0, &ignoreOs, 0,           NULL, NULL},
147  { "ignoresize", '\0', 0, &ignoreSize, 0,       NULL, NULL},
148  { "includedocs", '\0', 0, &incldocs, 0,        NULL, NULL},
149  { "initdb", '\0', 0, &initdb, 0,               NULL, NULL},
150 /* info and install both using 'i' is dumb */
151  { "install", '\0', 0, 0, GETOPT_INSTALL,       NULL, NULL},
152  { "justdb", '\0', 0, &justdb, 0,               NULL, NULL},
153  { "macros", '\0', POPT_ARG_STRING, &macrofiles, 0,     NULL, NULL},
154  { "nodeps", '\0', 0, &noDeps, 0,               NULL, NULL},
155  { "nodirtokens", '\0', POPT_ARG_VAL, &_noDirTokens, 1, NULL, NULL},
156  { "nogpg", '\0', 0, &noGpg, 0,                 NULL, NULL},
157 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
158  { "nolibio", '\0', POPT_ARG_VAL, &noLibio, 1,          NULL, NULL},
159 #endif
160  { "nomd5", '\0', 0, &noMd5, 0,                 NULL, NULL},
161  { "noorder", '\0', 0, &noOrder, 0,             NULL, NULL},
162  { "nopgp", '\0', 0, &noPgp, 0,                 NULL, NULL},
163
164  { "noscripts", '\0', 0, &noScripts, 0,         NULL, NULL},
165  { "nopre", '\0', 0, &noPre, 0,                 NULL, NULL},
166  { "nopost", '\0', 0, &noPost, 0,               NULL, NULL},
167  { "nopreun", '\0', 0, &noPreun, 0,             NULL, NULL},
168  { "nopostun", '\0', 0, &noPostun, 0,           NULL, NULL},
169
170  { "notriggers", '\0', 0, &noTriggers, 0,       NULL, NULL},
171  { "notriggerprein", '\0', 0, &noTPrein, 0,     NULL, NULL},
172  { "notriggerin", '\0', 0, &noTIn, 0,           NULL, NULL},
173  { "notriggerun", '\0', 0, &noTUn, 0,           NULL, NULL},
174  { "notriggerpostun", '\0', 0, &noTPostun, 0,   NULL, NULL},
175
176  { "oldpackage", '\0', 0, &oldPackage, 0,       NULL, NULL},
177  { "percent", '\0', 0, &showPercents, 0,        NULL, NULL},
178  { "pipe", '\0', POPT_ARG_STRING, &pipeOutput, 0,       NULL, NULL},
179  { "pkgcommit", '\0', POPT_ARG_VAL, &pkgCommit, 1,      NULL, NULL},
180  { "pkgundo", '\0', POPT_ARG_VAL, &pkgUndo, 1,  NULL, NULL},
181  { "prefix", '\0', POPT_ARG_STRING, &prefix, 0, NULL, NULL},
182  { "quiet", '\0', 0, &quiet, 0,                 NULL, NULL},
183 #ifndef DYING
184  { "rcfile", '\0', POPT_ARG_STRING, &rcfile, 0, NULL, NULL},
185 #else
186  { "rcfile", '\0', 0, 0, GETOPT_RCFILE,         NULL, NULL},
187 #endif
188  { "rebuilddb", '\0', 0, 0, GETOPT_REBUILDDB,   NULL, NULL},
189  { "verifydb", '\0', 0, 0, GETOPT_VERIFYDB,     NULL, NULL},
190  { "relocate", '\0', POPT_ARG_STRING, 0, GETOPT_RELOCATE,       NULL, NULL},
191  { "repackage", '\0', POPT_ARG_VAL, &rePackage, 1,      NULL, NULL},
192  { "replacefiles", '\0', 0, &replaceFiles, 0,   NULL, NULL},
193  { "replacepkgs", '\0', 0, &replacePackages, 0, NULL, NULL},
194  { "resign", '\0', 0, 0, GETOPT_RESIGN,         NULL, NULL},
195  { "root", 'r', POPT_ARG_STRING, &rootdir, 0,   NULL, NULL},
196  { "rpmiodebug", '\0', POPT_ARG_VAL, &_rpmio_debug, -1,         NULL, NULL},
197  { "showrc", '\0', 0, &showrc, GETOPT_SHOWRC,   NULL, NULL},
198  { "sign", '\0', 0, &signIt, 0,                 NULL, NULL},
199  { "test", '\0', 0, &test, 0,                   NULL, NULL},
200  { "commit", '\0', POPT_ARG_VAL, &tsCommit, 1,  NULL, NULL},
201  { "undo", '\0', POPT_ARG_VAL, &tsUndo, 1,      NULL, NULL},
202  { "upgrade", 'U', 0, 0, 'U',                   NULL, NULL},
203  { "urldebug", '\0', POPT_ARG_VAL, &_url_debug, -1,             NULL, NULL},
204  { "uninstall", 'u', 0, 0, 'u',                 NULL, NULL},
205  { "verbose", 'v', 0, 0, 'v',                   NULL, NULL},
206  { "version", '\0', 0, &showVersion, 0,         NULL, NULL},
207
208  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, 
209                 rpmQVSourcePoptTable, 0,        (void *) &rpmQVArgs, NULL },
210  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, 
211                 rpmQueryPoptTable, 0,           (void *) &rpmQVArgs, NULL },
212  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, 
213                 rpmVerifyPoptTable, 0,          (void *) &rpmQVArgs, NULL },
214
215  { NULL, '\0', POPT_ARG_INCLUDE_TABLE,
216                 rpmBuildPoptTable, 0,           (void *) &rpmBTArgs, NULL },
217
218  { 0, 0, 0, 0, 0,       NULL, NULL }
219 };
220
221 #ifdef __MINT__
222 /* MiNT cannot dynamically increase the stack.  */
223 long _stksize = 64 * 1024L;
224 #endif
225
226 static void argerror(const char * desc) {
227     fprintf(stderr, _("rpm: %s\n"), desc);
228     exit(EXIT_FAILURE);
229 }
230
231 static void printHelp(void);
232 static void printVersion(void);
233 static void printBanner(void);
234 static void printUsage(void);
235 static void printHelpLine(char * prefix, char * help);
236
237 static void printVersion(void) {
238     fprintf(stdout, _("RPM version %s\n"), rpmEVR);
239 }
240
241 static void printBanner(void) {
242     puts(_("Copyright (C) 1998-2000 - Red Hat, Inc."));
243     puts(_("This program may be freely redistributed under the terms of the GNU GPL"));
244 }
245
246 static void printUsage(void) {
247     printVersion();
248     printBanner();
249     puts("");
250
251     puts(_("Usage: rpm {--help}"));
252     puts(_("       rpm {--version}"));
253     puts(_("       rpm {--initdb}   [--dbpath <dir>]"));
254     puts(_("       rpm {--install -i} [-v] [--hash -h] [--percent] [--force] [--test]"));
255     puts(_("                        [--replacepkgs] [--replacefiles] [--root <dir>]"));
256     puts(_("                        [--excludedocs] [--includedocs] [--noscripts]"));
257     puts(_("                        [--rcfile <file>] [--ignorearch] [--dbpath <dir>]"));
258     puts(_("                        [--prefix <dir>] [--ignoreos] [--nodeps] [--allfiles]"));
259     puts(_("                        [--ftpproxy <host>] [--ftpport <port>]"));
260     puts(_("                        [--httpproxy <host>] [--httpport <port>]"));
261     puts(_("                        [--justdb] [--noorder] [--relocate oldpath=newpath]"));
262     puts(_("                        [--badreloc] [--notriggers] [--excludepath <path>]"));
263     puts(_("                        [--ignoresize] file1.rpm ... fileN.rpm"));
264     puts(_("       rpm {--upgrade -U} [-v] [--hash -h] [--percent] [--force] [--test]"));
265     puts(_("                        [--oldpackage] [--root <dir>] [--noscripts]"));
266     puts(_("                        [--excludedocs] [--includedocs] [--rcfile <file>]"));
267     puts(_("                        [--ignorearch]  [--dbpath <dir>] [--prefix <dir>] "));
268     puts(_("                        [--ftpproxy <host>] [--ftpport <port>]"));
269     puts(_("                        [--httpproxy <host>] [--httpport <port>] "));
270     puts(_("                        [--ignoreos] [--nodeps] [--allfiles] [--justdb]"));
271     puts(_("                        [--noorder] [--relocate oldpath=newpath]"));
272     puts(_("                        [--badreloc] [--excludepath <path>] [--ignoresize]"));
273     puts(_("                        file1.rpm ... fileN.rpm"));
274     puts(_("       rpm {--query -q} [-afpg] [-i] [-l] [-s] [-d] [-c] [-v] [-R]"));
275     puts(_("                        [--scripts] [--root <dir>] [--rcfile <file>]"));
276     puts(_("                        [--whatprovides] [--whatrequires] [--requires]"));
277     puts(_("                        [--triggeredby]"));
278     puts(_("                        [--ftpproxy <host>] [--ftpport <port>]"));
279     puts(_("                        [--httpproxy <host>] [--httpport <port>]"));
280     puts(_("                        [--provides] [--triggers] [--dump]"));
281     puts(_("                        [--changelog] [--dbpath <dir>] [targets]"));
282     puts(_("       rpm {--verify -V -y} [-afpg] [--root <dir>] [--rcfile <file>]"));
283     puts(_("                        [--dbpath <dir>] [--nodeps] [--nofiles] [--noscripts]"));
284     puts(_("                        [--nomd5] [targets]"));
285     puts(_("       rpm {--setperms} [-afpg] [target]"));
286     puts(_("       rpm {--setugids} [-afpg] [target]"));
287     puts(_("       rpm {--freshen -F} file1.rpm ... fileN.rpm"));
288     puts(_("       rpm {--erase -e} [--root <dir>] [--noscripts] [--rcfile <file>]"));
289     puts(_("                        [--dbpath <dir>] [--nodeps] [--allmatches]"));
290     puts(_("                        [--justdb] [--notriggers] package1 ... packageN"));
291     puts(_("       rpm {--resign} [--rcfile <file>] package1 package2 ... packageN"));
292     puts(_("       rpm {--addsign} [--rcfile <file>] package1 package2 ... packageN"));
293     puts(_("       rpm {--checksig -K} [--nopgp] [--nogpg] [--nomd5] [--rcfile <file>]"));
294     puts(_("                           package1 ... packageN"));
295     puts(_("       rpm {--rebuilddb} [--rcfile <file>] [--dbpath <dir>]"));
296     puts(_("       rpm {--verifydb} [--rcfile <file>] [--dbpath <dir>]"));
297     puts(_("       rpm {--querytags}"));
298 }
299
300 static void printHelpLine(char * prefix, char * help) {
301     int indentLength = strlen(prefix) + 3;
302     int lineLength = 79 - indentLength;
303     int helpLength = strlen(help);
304     char * ch;
305     char format[64];
306
307     fprintf(stdout, "%s - ", prefix);
308
309     while (helpLength > lineLength) {
310         ch = help + lineLength - 1;
311         while (ch > help && !isspace(*ch)) ch--;
312         if (ch == help) break;          /* give up */
313         while (ch > (help + 1) && isspace(*ch)) ch--;
314         ch++;
315
316         sprintf(format, "%%.%ds\n%%%ds", (int) (ch - help), indentLength);
317         fprintf(stdout, format, help, " ");
318         help = ch;
319         while (isspace(*help) && *help) help++;
320         helpLength = strlen(help);
321     }
322
323     if (helpLength) puts(help);
324 }
325
326 static void printHelp(void) {
327     printVersion();
328     printBanner();
329     puts("");
330
331     puts(         _("Usage:"));
332     printHelpLine(  "   --help                 ", 
333                   _("print this message"));
334     printHelpLine(  "   --version              ",
335                   _("print the version of rpm being used"));
336
337     puts("");
338     puts(         _("   All modes support the following arguments:"));
339     printHelpLine(_("    --define '<name> <body>'"),
340                   _("define macro <name> with value <body>"));
341     printHelpLine(_("    --eval '<name>+'      "),
342                   _("print the expansion of macro <name> to stdout"));
343     printHelpLine(_("    --pipe <cmd>          "),
344                   _("send stdout to <cmd>"));
345     printHelpLine(_("    --rcfile <file>       "),
346                   _("use <file> instead of /etc/rpmrc and $HOME/.rpmrc"));
347     printHelpLine(  "    --showrc              ",
348                   _("display final rpmrc and macro configuration"));
349     printHelpLine(  "     -v                   ",
350                   _("be a little more verbose"));
351     printHelpLine(  "     -vv                  ",
352                   _("be incredibly verbose (for debugging)"));
353
354     puts("");
355     puts(         _("   Install, upgrade and query (with -p) allow URL's to be used in place"));
356     puts(         _("   of file names as well as the following options:"));
357     printHelpLine(_("      --ftpproxy <host>   "),
358                   _("hostname or IP of ftp proxy"));
359     printHelpLine(_("      --ftpport <port>    "),
360                   _("port number of ftp server (or proxy)"));
361     printHelpLine(_("      --httpproxy <host>  "),
362                   _("hostname or IP of http proxy"));
363     printHelpLine(_("      --httpport <port>   "),
364                   _("port number of http server (or proxy)"));
365
366     puts("");
367     printHelpLine(  "   -q, --query            ",
368                   _("query mode"));
369     printHelpLine(_("      --dbpath <dir>      "),
370                   _("use <dir> as the directory for the database"));
371     printHelpLine(_("      --queryformat <qfmt>"),
372                   _("use <qfmt> as the header format (implies --info)"));
373     printHelpLine(_("      --root <dir>        "),
374                   _("use <dir> as the top level directory"));
375     puts(         _("      Package specification options:"));
376     printHelpLine(  "        -a, --all         ",
377                   _("query all packages"));
378     printHelpLine(_("        -f <file>+        "),
379                   _("query package owning <file>"));
380     printHelpLine(_("        -p <packagefile>+ "),
381                   _("query (uninstalled) package <packagefile>"));
382     printHelpLine(_("        --triggeredby <pkg>"),
383                   _("query packages triggered by <pkg>"));
384     printHelpLine(_("        --whatprovides <cap>"),
385                   _("query packages which provide <cap> capability"));
386     printHelpLine(_("        --whatrequires <cap>"),
387                   _("query packages which require <cap> capability"));
388     puts(         _("      Information selection options:"));
389     printHelpLine(  "        -i, --info        ",
390                   _("display package information"));
391     printHelpLine(  "        --changelog       ",
392                   _("display the package's change log"));
393     printHelpLine(  "        -l                ",
394                   _("display package file list"));
395     printHelpLine(  "        -s                ",
396                   _("show file states (implies -l)"));
397     printHelpLine(  "        -d                ",
398                   _("list only documentation files (implies -l)"));
399     printHelpLine(  "        -c                ",
400                   _("list only configuration files (implies -l)"));
401     printHelpLine(  "        --dump            ",
402                   _("show all verifiable information for each file (must be used with -l, -c, or -d)"));
403     printHelpLine(  "        --provides        ",
404                   _("list capabilities package provides"));
405     printHelpLine(  "        -R, --requires    ",
406                   _("list package dependencies"));
407     printHelpLine(  "        --scripts         ",
408                   _("print the various [un]install scripts"));
409     printHelpLine(  "        --triggers        ",
410                   _("show the trigger scripts contained in the package"));
411
412     puts("");
413     printHelpLine(  "    -V, -y, --verify      ",
414                   _("verify a package installation using the same same package specification options as -q"));
415     printHelpLine(_("      --dbpath <dir>      "),
416                   _("use <dir> as the directory for the database"));
417     printHelpLine(_("      --root <dir>        "),
418                   _("use <dir> as the top level directory"));
419     printHelpLine(  "      --nodeps            ",
420                   _("do not verify package dependencies"));
421     printHelpLine(  "      --nomd5             ",
422                   _("do not verify file md5 checksums"));
423     printHelpLine(  "      --nofiles           ",
424                   _("do not verify file attributes"));
425     printHelpLine(  "    --querytags           ",
426                   _("list the tags that can be used in a query format"));
427
428     puts("");
429     puts(         _("    --install <packagefile>"));
430     printHelpLine(_("    -i <packagefile>      "),
431                   _("install package"));
432     printHelpLine(_("      --excludepath <path>"),
433                   _("skip files in path <path>"));
434     printHelpLine(_("      --relocate <oldpath>=<newpath>"),
435                   _("relocate files from <oldpath> to <newpath>"));
436     printHelpLine(  "      --badreloc          ",
437                   _("relocate files in non-relocatable package"));
438     printHelpLine(_("      --prefix <dir>      "),
439                   _("relocate the package to <dir>, if relocatable"));
440     printHelpLine(_("      --dbpath <dir>      "),
441                   _("use <dir> as the directory for the database"));
442     printHelpLine(  "      --excludedocs       ",
443                   _("do not install documentation"));
444     printHelpLine(  "      --force             ",
445                   _("short hand for --replacepkgs --replacefiles"));
446     printHelpLine(  "      -h, --hash          ",
447                   _("print hash marks as package installs (good with -v)"));
448     printHelpLine(  "      --allfiles          ",
449                   _("install all files, even configurations which might "
450                     "otherwise be skipped"));
451     printHelpLine(  "      --ignorearch        ",
452                   _("don't verify package architecture"));
453     printHelpLine(  "      --ignoresize        ",
454                   _("don't check disk space before installing"));
455     printHelpLine(  "      --ignoreos          ",
456                   _("don't verify package operating system"));
457     printHelpLine(  "      --includedocs       ",
458                   _("install documentation"));
459     printHelpLine(  "      --justdb            ",
460                   _("update the database, but do not modify the filesystem"));
461     printHelpLine(  "      --nodeps            ",
462                   _("do not verify package dependencies"));
463     printHelpLine(  "      --noorder           ",
464                   _("do not reorder package installation to satisfy dependencies"));
465     printHelpLine(  "      --noscripts         ",
466                   _("don't execute any installation scripts"));
467     printHelpLine(  "      --notriggers        ",
468                   _("don't execute any scripts triggered by this package"));
469     printHelpLine(  "      --percent           ",
470                   _("print percentages as package installs"));
471     printHelpLine(  "      --replacefiles      ",
472                   _("install even if the package replaces installed files"));
473     printHelpLine(  "      --replacepkgs       ",
474                   _("reinstall if the package is already present"));
475     printHelpLine(_("      --root <dir>        "),
476                   _("use <dir> as the top level directory"));
477     printHelpLine(  "      --test              ",
478                   _("don't install, but tell if it would work or not"));
479
480     puts("");
481     puts(         _("    --upgrade <packagefile>"));
482     printHelpLine(_("    -U <packagefile>      "),
483                   _("upgrade package (same options as --install, plus)"));
484     printHelpLine(  "      --oldpackage        ",
485                   _("upgrade to an old version of the package (--force on upgrades does this automatically)"));
486     puts("");
487     puts(         _("    --erase <package>"));
488     printHelpLine(_("    -e <package>          "),
489                   _("erase (uninstall) package"));
490     printHelpLine(  "      --allmatches        ",
491                   _("remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)"));
492     printHelpLine(_("      --dbpath <dir>      "),
493                   _("use <dir> as the directory for the database"));
494     printHelpLine(  "      --justdb            ",
495                   _("update the database, but do not modify the filesystem"));
496     printHelpLine(  "      --nodeps            ",
497                   _("do not verify package dependencies"));
498     printHelpLine(  "      --noorder           ",
499                   _("do not reorder package installation to satisfy dependencies"));
500     printHelpLine(  "      --noscripts         ",
501                   _("do not execute any package specific scripts"));
502     printHelpLine(  "      --notriggers        ",
503                   _("don't execute any scripts triggered by this package"));
504     printHelpLine(_("      --root <dir>        "),
505                   _("use <dir> as the top level directory"));
506     puts("");
507     puts(         _("    -b<stage> <spec>      "));
508     printHelpLine(_("    -t<stage> <tarball>   "),
509                   _("build package, where <stage> is one of:"));
510     printHelpLine(  "          p               ",
511                   _("prep (unpack sources and apply patches)"));
512     printHelpLine(  "          l               ",
513                   _("list check (do some cursory checks on %files)"));
514     printHelpLine(  "          c               ",
515                   _("compile (prep and compile)"));
516     printHelpLine(  "          i               ",
517                   _("install (prep, compile, install)"));
518     printHelpLine(  "          b               ",
519                   _("binary package (prep, compile, install, package)"));
520     printHelpLine(  "          a               ",
521                   _("bin/src package (prep, compile, install, package)"));
522     printHelpLine(  "      --short-circuit     ",
523                   _("skip straight to specified stage (only for c,i)"));
524     printHelpLine(  "      --clean             ",
525                   _("remove build tree when done"));
526     printHelpLine(  "      --rmsource          ",
527                   _("remove sources when done"));
528     printHelpLine(  "      --rmspec            ",
529                   _("remove spec file when done"));
530     printHelpLine(  "      --sign              ",
531                   _("generate PGP/GPG signature"));
532     printHelpLine(_("      --buildroot <dir>   "),
533                   _("use <dir> as the build root"));
534     printHelpLine(_("      --target=<platform>+"),
535                   _("build the packages for the build targets platform1...platformN."));
536     printHelpLine(  "      --nobuild           ",
537                   _("do not execute any stages"));
538     puts("");
539     printHelpLine(_("    --rebuild <src_pkg>   "),
540                   _("install source package, build binary package and remove spec file, sources, patches, and icons."));
541     printHelpLine(_("    --recompile <src_pkg> "),
542                   _("like --rebuild, but don't build any package"));
543
544     puts("");
545     printHelpLine(_("    --resign <pkg>+       "),
546                   _("sign a package (discard current signature)"));
547     printHelpLine(_("    --addsign <pkg>+      "),
548                   _("add a signature to a package"));
549     puts(         _("    --checksig <pkg>+"));
550     printHelpLine(_("    -K <pkg>+             "),
551                   _("verify package signature"));
552     printHelpLine(  "      --nopgp             ",
553                   _("skip any PGP signatures"));
554     printHelpLine(  "      --nogpg             ",
555                   _("skip any GPG signatures"));
556     printHelpLine(  "      --nomd5             ",
557                   _("skip any MD5 signatures"));
558
559     puts("");
560     printHelpLine(  "    --initdb              ",
561                   _("make sure a valid database exists"));
562     printHelpLine(  "    --rebuilddb           ",
563                   _("rebuild database from existing database"));
564     printHelpLine(  "    --verifydb            ",
565                   _("verify database files"));
566     printHelpLine(_("      --dbpath <dir>      "),
567                   _("use <dir> as the directory for the database"));
568     printHelpLine(  "      --root <dir>        ",
569                   _("use <dir> as the top level directory"));
570
571     puts("");
572     printHelpLine(  "    --setperms            ",
573                   _("set the file permissions to those in the package database"
574                     " using the same package specification options as -q"));
575     printHelpLine(  "    --setugids            ",
576                   _("set the file owner and group to those in the package "
577                     "database using the same package specification options as "
578                     "-q"));
579 }
580
581 int main(int argc, const char ** argv)
582 {
583     enum modes bigMode = MODE_UNKNOWN;
584     QVA_t qva = &rpmQVArgs;
585     int arg;
586     rpmtsFlags transFlags = RPMTRANS_FLAG_NONE;
587     rpmInstallInterfaceFlags installInterfaceFlags = INSTALL_NONE;
588     rpmEraseInterfaceFlags eraseInterfaceFlags = UNINSTALL_NONE;
589     int verifyFlags;
590     int checksigFlags = 0;
591     rpmSignFlags addSign = RESIGN_NEW_SIGNATURE;
592     char * passPhrase = "";
593     const char * optArg;
594     pid_t pipeChild = 0;
595     const char * pkg;
596     char * errString = NULL;
597     poptContext optCon;
598     int ec = 0;
599     int status;
600     int p[2];
601     rpmRelocation * relocations = NULL;
602     int numRelocations = 0;
603     int sigTag;
604     int upgrade = 0;
605     int freshen = 0;
606     int probFilter = 0;
607         
608 #if HAVE_MCHECK_H && HAVE_MTRACE
609     mtrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
610 #endif
611     setprogname(argv[0]);       /* Retrofit glibc __progname */
612
613     /* set the defaults for the various command line options */
614     allFiles = 0;
615     allMatches = 0;
616     applyOnly = 0;
617     badReloc = 0;
618     excldocs = 0;
619     force = 0;
620     _ftp_debug = 0;
621     showHash = 0;
622     help = 0;
623     ignoreArch = 0;
624     ignoreOs = 0;
625     ignoreSize = 0;
626     incldocs = 0;
627     initdb = 0;
628     justdb = 0;
629     noDeps = 0;
630     noGpg = 0;
631 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
632     noLibio = 0;
633 #else
634     noLibio = 1;
635 #endif
636     noMd5 = 0;
637     noOrder = 0;
638     noPgp = 0;
639
640     noScripts = 0;
641     noPre = 0;
642     noPost = 0;
643     noPreun = 0;
644     noPostun = 0;
645
646     noTriggers = 0;
647     noTPrein = 0;
648     noTIn = 0;
649     noTUn = 0;
650     noTPostun = 0;
651
652     noUsageMsg = 0;
653     oldPackage = 0;
654     showPercents = 0;
655     pipeOutput = NULL;
656     prefix = NULL;
657     quiet = 0;
658     _rpmio_debug = 0;
659     replaceFiles = 0;
660     replacePackages = 0;
661     rootdir = "/";
662     showrc = 0;
663     signIt = 0;
664     showVersion = 0;
665     specedit = 0;
666     test = 0;
667     _url_debug = 0;
668
669     /* XXX Eliminate query linkage loop */
670     parseSpecVec = parseSpec;
671     freeSpecVec = freeSpec;
672
673     /* set up the correct locale */
674     setlocale(LC_ALL, "" );
675
676 #ifdef  __LCLINT__
677 #define LOCALEDIR       "/usr/share/locale"
678 #endif
679     bindtextdomain(PACKAGE, LOCALEDIR);
680     textdomain(PACKAGE);
681
682     rpmSetVerbosity(RPMMESS_NORMAL);    /* XXX silly use by showrc */
683
684     /* Make a first pass through the arguments, looking for --rcfile */
685     /* We need to handle that before dealing with the rest of the arguments. */
686     optCon = poptGetContext("rpm", argc, argv, optionsTable, 0);
687     poptReadConfigFile(optCon, LIBRPMALIAS_FILENAME);
688     poptReadDefaultConfig(optCon, 1);
689     poptSetExecPath(optCon, RPMCONFIGDIR, 1);
690
691     /* reading rcfile early makes it easy to override */
692     /* XXX only --rcfile (and --showrc) need this pre-parse */
693
694     while ((arg = poptGetNextOpt(optCon)) > 0) {
695         switch(arg) {
696         case 'v':
697             rpmIncreaseVerbosity();     /* XXX silly use by showrc */
698             break;
699         default:
700             break;
701       }
702     }
703
704     if (rpmReadConfigFiles(rcfile, NULL))  
705         exit(EXIT_FAILURE);
706
707     if (showrc) {
708         rpmShowRC(stdout);
709         exit(EXIT_SUCCESS);
710     }
711
712     rpmSetVerbosity(RPMMESS_NORMAL);    /* XXX silly use by showrc */
713
714     poptResetContext(optCon);
715
716     if (qva->qva_queryFormat) free((void *)qva->qva_queryFormat);
717     memset(qva, 0, sizeof(*qva));
718     qva->qva_source = RPMQV_PACKAGE;
719     qva->qva_mode = ' ';
720     qva->qva_char = ' ';
721
722     while ((arg = poptGetNextOpt(optCon)) > 0) {
723         optArg = poptGetOptArg(optCon);
724
725         switch (arg) {
726           case 'K':
727             if (bigMode != MODE_UNKNOWN && bigMode != MODE_CHECKSIG)
728                 argerror(_("only one major mode may be specified"));
729             bigMode = MODE_CHECKSIG;
730             break;
731             
732           case 'u':
733             if (bigMode != MODE_UNKNOWN && bigMode != MODE_ERASE)
734                 argerror(_("only one major mode may be specified"));
735             bigMode = MODE_ERASE;
736             rpmMessage(RPMMESS_ERROR, _("-u and --uninstall are deprecated and no"
737                     " longer work.\n"));
738             rpmMessage(RPMMESS_ERROR, _("Use -e or --erase instead.\n"));
739             exit(EXIT_FAILURE);
740         
741           case 'e':
742             if (bigMode != MODE_UNKNOWN && bigMode != MODE_ERASE)
743                 argerror(_("only one major mode may be specified"));
744             bigMode = MODE_ERASE;
745             break;
746         
747           case 'v':
748             rpmIncreaseVerbosity();
749             break;
750
751           case 'i':
752             if (bigMode == MODE_QUERY) {
753                 const char * infoCommand[] = { "--info", NULL };
754                 poptStuffArgs(optCon, infoCommand);
755             } else if (bigMode == MODE_INSTALL)
756                 /*@-ifempty@*/ ;
757             else if (bigMode == MODE_UNKNOWN) {
758                 const char * installCommand[] = { "--install", NULL };
759                 poptStuffArgs(optCon, installCommand);
760             }
761             break;
762
763           case GETOPT_INSTALL:
764             if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
765                 argerror(_("only one major mode may be specified"));
766             bigMode = MODE_INSTALL;
767             break;
768
769           case 'U':
770             if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
771                 argerror(_("only one major mode may be specified"));
772             bigMode = MODE_INSTALL;
773             upgrade = 1;
774             break;
775
776           case 'F':
777             if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
778                 argerror(_("only one major mode may be specified"));
779             bigMode = MODE_INSTALL;
780             upgrade = 1;  /* Freshen implies upgrade */
781             freshen = 1;
782             break;
783
784           case GETOPT_RESIGN:
785             if (bigMode != MODE_UNKNOWN && bigMode != MODE_RESIGN)
786                 argerror(_("only one major mode may be specified"));
787             bigMode = MODE_RESIGN;
788             addSign = RESIGN_NEW_SIGNATURE;
789             signIt = 1;
790             break;
791
792           case GETOPT_ADDSIGN:
793             if (bigMode != MODE_UNKNOWN && bigMode != MODE_RESIGN)
794                 argerror(_("only one major mode may be specified"));
795             bigMode = MODE_RESIGN;
796             addSign = RESIGN_ADD_SIGNATURE;
797             signIt = 1;
798             break;
799
800           case GETOPT_DEFINEMACRO:
801             rpmDefineMacro(NULL, optArg, RMIL_CMDLINE);
802             rpmDefineMacro(&rpmCLIMacroContext, optArg, RMIL_CMDLINE);
803             noUsageMsg = 1;
804             break;
805
806           case GETOPT_EVALMACRO:
807           { const char *val = rpmExpand(optArg, NULL);
808             fprintf(stdout, "%s\n", val);
809             free((void *)val);
810             noUsageMsg = 1;
811           } break;
812
813           case GETOPT_REBUILDDB:
814             if (bigMode != MODE_UNKNOWN && bigMode != MODE_REBUILDDB)
815                 argerror(_("only one major mode may be specified"));
816             bigMode = MODE_REBUILDDB;
817             break;
818
819           case GETOPT_VERIFYDB:
820             if (bigMode != MODE_UNKNOWN && bigMode != MODE_VERIFYDB)
821                 argerror(_("only one major mode may be specified"));
822             bigMode = MODE_VERIFYDB;
823             break;
824
825           case GETOPT_RELOCATE:
826             if (*optArg != '/') 
827                 argerror(_("relocations must begin with a /"));
828             if (!(errString = strchr(optArg, '=')))
829                 argerror(_("relocations must contain a ="));
830             *errString++ = '\0';
831             if (*errString != '/') 
832                 argerror(_("relocations must have a / following the ="));
833             relocations = xrealloc(relocations, 
834                                   sizeof(*relocations) * (numRelocations + 1));
835             relocations[numRelocations].oldPath = optArg;
836             relocations[numRelocations++].newPath = errString;
837             break;
838
839           case GETOPT_EXCLUDEPATH:
840             if (*optArg != '/') 
841                 argerror(_("exclude paths must begin with a /"));
842
843             relocations = xrealloc(relocations, 
844                                   sizeof(*relocations) * (numRelocations + 1));
845             relocations[numRelocations].oldPath = optArg;
846             relocations[numRelocations++].newPath = NULL;
847             break;
848
849           case GETOPT_RCFILE:
850             fprintf(stderr, _("The --rcfile option has been eliminated.\n"));
851             fprintf(stderr, _("Use --macros with a colon separated list of macro files to read.\n"));
852             exit(EXIT_FAILURE);
853             /*@notreached@*/ break;
854
855           default:
856             fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
857             exit(EXIT_FAILURE);
858         }
859     }
860
861     if (quiet)
862         rpmSetVerbosity(RPMMESS_QUIET);
863
864     if (showVersion) printVersion();
865     if (help) printHelp();
866
867     if (arg < -1) {
868         fprintf(stderr, "%s: %s\n", 
869                 poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
870                 poptStrerror(arg));
871         exit(EXIT_FAILURE);
872     }
873
874     if (bigMode == MODE_UNKNOWN && qva->qva_mode != ' ') {
875         switch (qva->qva_mode) {
876         case 'q':       bigMode = MODE_QUERY;           break;
877         case 'V':       bigMode = MODE_VERIFY;          break;
878         case 'Q':       bigMode = MODE_QUERYTAGS;       break;
879         }
880     }
881
882     if (initdb) {
883         if (bigMode != MODE_UNKNOWN) 
884             argerror(_("only one major mode may be specified"));
885         else
886             bigMode = MODE_INITDB;
887     }
888
889     if (qva->qva_sourceCount) {
890         if (qva->qva_sourceCount > 1)
891             argerror(_("one type of query/verify may be performed at a "
892                         "time"));
893     }
894
895     if (qva->qva_flags && (bigMode & ~MODES_QV)) 
896         argerror(_("unexpected query flags"));
897
898     if (qva->qva_queryFormat && (bigMode & ~MODES_QV)) 
899         argerror(_("unexpected query format"));
900
901     if (qva->qva_source != RPMQV_PACKAGE && (bigMode & ~MODES_QV)) 
902         argerror(_("unexpected query source"));
903
904     if (!(bigMode == MODE_INSTALL) && force)
905         argerror(_("only installation, upgrading, rmsource and rmspec may be forced"));
906
907     if (bigMode != MODE_INSTALL && badReloc)
908         argerror(_("files may only be relocated during package installation"));
909
910     if (relocations && prefix)
911         argerror(_("only one of --prefix or --relocate may be used"));
912
913     if (bigMode != MODE_INSTALL && relocations)
914         argerror(_("--relocate and --excludepath may only be used when installing new packages"));
915
916     if (bigMode != MODE_INSTALL && prefix)
917         argerror(_("--prefix may only be used when installing new packages"));
918
919     if (prefix && prefix[0] != '/') 
920         argerror(_("arguments to --prefix must begin with a /"));
921
922     if (bigMode != MODE_INSTALL && showHash)
923         argerror(_("--hash (-h) may only be specified during package "
924                         "installation"));
925
926     if (bigMode != MODE_INSTALL && showPercents)
927         argerror(_("--percent may only be specified during package "
928                         "installation"));
929
930     if (bigMode != MODE_INSTALL && replaceFiles)
931         argerror(_("--replacefiles may only be specified during package "
932                         "installation"));
933
934     if (bigMode != MODE_INSTALL && replacePackages)
935         argerror(_("--replacepkgs may only be specified during package "
936                         "installation"));
937
938     if (bigMode != MODE_INSTALL && excldocs)
939         argerror(_("--excludedocs may only be specified during package "
940                    "installation"));
941
942     if (bigMode != MODE_INSTALL && incldocs)
943         argerror(_("--includedocs may only be specified during package "
944                    "installation"));
945
946     if (excldocs && incldocs)
947         argerror(_("only one of --excludedocs and --includedocs may be "
948                  "specified"));
949   
950     if (bigMode != MODE_INSTALL && ignoreArch)
951         argerror(_("--ignorearch may only be specified during package "
952                    "installation"));
953
954     if (bigMode != MODE_INSTALL && ignoreOs)
955         argerror(_("--ignoreos may only be specified during package "
956                    "installation"));
957
958     if (bigMode != MODE_INSTALL && ignoreSize)
959         argerror(_("--ignoresize may only be specified during package "
960                    "installation"));
961
962     if (allMatches && bigMode != MODE_ERASE)
963         argerror(_("--allmatches may only be specified during package "
964                    "erasure"));
965
966     if (allFiles && bigMode != MODE_INSTALL)
967         argerror(_("--allfiles may only be specified during package "
968                    "installation"));
969
970     if (justdb && bigMode != MODE_INSTALL && bigMode != MODE_ERASE)
971         argerror(_("--justdb may only be specified during package "
972                    "installation and erasure"));
973
974     if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE && 
975         bigMode != MODE_VERIFY &&
976         (noScripts | noPre | noPost | noPreun | noPostun |
977          noTriggers | noTPrein | noTIn | noTUn | noTPostun))
978         argerror(_("script disabling options may only be specified during package "
979                    "installation, erasure, and verification"));
980
981     if (bigMode != MODE_INSTALL && applyOnly)
982         argerror(_("--apply may only be specified during package "
983                    "installation"));
984
985     if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE &&
986         (noTriggers | noTPrein | noTIn | noTUn | noTPostun))
987         argerror(_("trigger disabling options may only be specified during package "
988                    "installation and erasure"));
989
990     if (noDeps & (bigMode & ~MODES_FOR_NODEPS))
991         argerror(_("--nodeps may only be specified during package "
992                    "building, rebuilding, recompilation, installation,"
993                    "erasure, and verification"));
994
995     if (test && (bigMode & ~MODES_FOR_TEST))
996         argerror(_("--test may only be specified during package installation, "
997                  "erasure, and building"));
998
999     if (rootdir[1] && (bigMode & ~MODES_FOR_ROOT))
1000         argerror(_("--root (-r) may only be specified during "
1001                  "installation, erasure, querying, and "
1002                  "database rebuilds"));
1003
1004     if (rootdir) {
1005         switch (urlIsURL(rootdir)) {
1006         default:
1007             if (bigMode & MODES_FOR_ROOT)
1008                 break;
1009             /*@fallthrough@*/
1010         case URL_IS_UNKNOWN:
1011             if (rootdir[0] != '/')
1012                 argerror(_("arguments to --root (-r) must begin with a /"));
1013             break;
1014         }
1015     }
1016
1017     if (oldPackage && !upgrade)
1018         argerror(_("--oldpackage may only be used during upgrades"));
1019
1020     if (noPgp && bigMode != MODE_CHECKSIG)
1021         argerror(_("--nopgp may only be used during signature checking"));
1022
1023     if (noGpg && bigMode != MODE_CHECKSIG)
1024         argerror(_("--nogpg may only be used during signature checking"));
1025
1026     if (noMd5 && bigMode != MODE_CHECKSIG && bigMode != MODE_VERIFY)
1027         argerror(_("--nomd5 may only be used during signature checking and "
1028                    "package verification"));
1029
1030     if (signIt) {
1031         if (bigMode == MODE_REBUILD || bigMode == MODE_BUILD ||
1032             bigMode == MODE_RESIGN || bigMode == MODE_TARBUILD) {
1033             const char ** argv;
1034             struct stat sb;
1035             int errors = 0;
1036
1037             if ((argv = poptGetArgs(optCon)) == NULL) {
1038                 fprintf(stderr, _("no files to sign\n"));
1039                 errors++;
1040             } else
1041             while (*argv) {
1042                 if (stat(*argv, &sb)) {
1043                     fprintf(stderr, _("cannot access file %s\n"), *argv);
1044                     errors++;
1045                 }
1046                 argv++;
1047             }
1048
1049             if (errors) return errors;
1050
1051             if (poptPeekArg(optCon)) {
1052                 switch (sigTag = rpmLookupSignatureType(RPMLOOKUPSIG_QUERY)) {
1053                   case 0:
1054                     break;
1055                   case RPMSIGTAG_PGP:
1056                     if ((sigTag == RPMSIGTAG_PGP || sigTag == RPMSIGTAG_PGP5) &&
1057                         !rpmDetectPGPVersion(NULL)) {
1058                         fprintf(stderr, _("pgp not found: "));
1059                         exit(EXIT_FAILURE);
1060                     }   /*@fallthrough@*/
1061                   case RPMSIGTAG_GPG:
1062                     passPhrase = rpmGetPassPhrase(_("Enter pass phrase: "), sigTag);
1063                     if (passPhrase == NULL) {
1064                         fprintf(stderr, _("Pass phrase check failed\n"));
1065                         exit(EXIT_FAILURE);
1066                     }
1067                     fprintf(stderr, _("Pass phrase is good.\n"));
1068                     passPhrase = xstrdup(passPhrase);
1069                     break;
1070                   default:
1071                     fprintf(stderr,
1072                             _("Invalid %%_signature spec in macro file.\n"));
1073                     exit(EXIT_FAILURE);
1074                     /*@notreached@*/ break;
1075                 }
1076             }
1077         } else {
1078             argerror(_("--sign may only be used during package building"));
1079         }
1080     } else {
1081         /* Make rpmLookupSignatureType() return 0 ("none") from now on */
1082         rpmLookupSignatureType(RPMLOOKUPSIG_DISABLE);
1083     }
1084
1085     if (pipeOutput) {
1086         pipe(p);
1087
1088         if (!(pipeChild = fork())) {
1089             close(p[1]);
1090             dup2(p[0], STDIN_FILENO);
1091             close(p[0]);
1092             execl("/bin/sh", "/bin/sh", "-c", pipeOutput, NULL);
1093             fprintf(stderr, _("exec failed\n"));
1094         }
1095
1096         close(p[0]);
1097         dup2(p[1], STDOUT_FILENO);
1098         close(p[1]);
1099     }
1100         
1101     switch (bigMode) {
1102       case MODE_UNKNOWN:
1103         if (!showVersion && !help && !noUsageMsg) printUsage();
1104         break;
1105
1106       case MODE_REBUILDDB:
1107         ec = rpmdbRebuild(rootdir);
1108         break;
1109
1110       case MODE_VERIFYDB:
1111         ec = rpmdbVerify(rootdir);
1112         break;
1113
1114       case MODE_QUERYTAGS:
1115         if (argc != 2)
1116             argerror(_("unexpected arguments to --querytags "));
1117
1118         rpmDisplayQueryTags(stdout);
1119         break;
1120
1121       case MODE_INITDB:
1122         rpmdbInit(rootdir, 0644);
1123         break;
1124
1125       case MODE_CHECKSIG:
1126         if (!poptPeekArg(optCon))
1127             argerror(_("no packages given for signature check"));
1128         if (!noPgp) checksigFlags |= CHECKSIG_PGP;
1129         if (!noGpg) checksigFlags |= CHECKSIG_GPG;
1130         if (!noMd5) checksigFlags |= CHECKSIG_MD5;
1131         ec = rpmCheckSig(checksigFlags, (const char **)poptGetArgs(optCon));
1132         /* XXX don't overflow single byte exit status */
1133         if (ec > 255) ec = 255;
1134         break;
1135
1136       case MODE_RESIGN:
1137         if (!poptPeekArg(optCon))
1138             argerror(_("no packages given for signing"));
1139         ec = rpmReSign(addSign, passPhrase, (const char **)poptGetArgs(optCon));
1140         /* XXX don't overflow single byte exit status */
1141         if (ec > 255) ec = 255;
1142         break;
1143         
1144       case MODE_REBUILD:
1145       case MODE_RECOMPILE:
1146         break;
1147
1148       case MODE_BUILD:
1149       case MODE_TARBUILD:
1150         break;
1151
1152       case MODE_ERASE:
1153         if (!poptPeekArg(optCon))
1154             argerror(_("no packages given for uninstall"));
1155
1156         if (noScripts) transFlags |= (_noTransScripts | _noTransTriggers);
1157         if (noPre) transFlags |= RPMTRANS_FLAG_NOPRE;
1158         if (noPost) transFlags |= RPMTRANS_FLAG_NOPOST;
1159         if (noPreun) transFlags |= RPMTRANS_FLAG_NOPREUN;
1160         if (noPostun) transFlags |= RPMTRANS_FLAG_NOPOSTUN;
1161
1162         if (noTriggers) transFlags |= _noTransTriggers;
1163         if (noTPrein) transFlags |= RPMTRANS_FLAG_NOTRIGGERPREIN;
1164         if (noTIn) transFlags |= RPMTRANS_FLAG_NOTRIGGERIN;
1165         if (noTUn) transFlags |= RPMTRANS_FLAG_NOTRIGGERUN;
1166         if (noTPostun) transFlags |= RPMTRANS_FLAG_NOTRIGGERPOSTUN;
1167
1168         if (test) transFlags |= RPMTRANS_FLAG_TEST;
1169         if (justdb) transFlags |= RPMTRANS_FLAG_JUSTDB;
1170         if (dirStash) transFlags |= RPMTRANS_FLAG_DIRSTASH;
1171         if (rePackage) transFlags |= RPMTRANS_FLAG_REPACKAGE;
1172         if (pkgCommit) transFlags |= RPMTRANS_FLAG_PKGCOMMIT;
1173         if (pkgUndo) transFlags |= RPMTRANS_FLAG_PKGUNDO;
1174         if (tsCommit) transFlags |= RPMTRANS_FLAG_COMMIT;
1175         if (tsUndo) transFlags |= RPMTRANS_FLAG_UNDO;
1176
1177         if (noDeps) eraseInterfaceFlags |= UNINSTALL_NODEPS;
1178         if (allMatches) eraseInterfaceFlags |= UNINSTALL_ALLMATCHES;
1179
1180         ec = rpmErase(rootdir, (const char **)poptGetArgs(optCon), 
1181                          transFlags, eraseInterfaceFlags);
1182         break;
1183
1184       case MODE_INSTALL:
1185         if (force) {
1186             probFilter |= RPMPROB_FILTER_REPLACEPKG | 
1187                           RPMPROB_FILTER_REPLACEOLDFILES |
1188                           RPMPROB_FILTER_REPLACENEWFILES |
1189                           RPMPROB_FILTER_OLDPACKAGE;
1190         }
1191         if (replaceFiles) probFilter |= RPMPROB_FILTER_REPLACEOLDFILES |
1192                                         RPMPROB_FILTER_REPLACENEWFILES;
1193         if (badReloc) probFilter |= RPMPROB_FILTER_FORCERELOCATE;
1194         if (replacePackages) probFilter |= RPMPROB_FILTER_REPLACEPKG;
1195         if (oldPackage) probFilter |= RPMPROB_FILTER_OLDPACKAGE;
1196         if (ignoreArch) probFilter |= RPMPROB_FILTER_IGNOREARCH; 
1197         if (ignoreOs) probFilter |= RPMPROB_FILTER_IGNOREOS;
1198         if (ignoreSize) probFilter |= RPMPROB_FILTER_DISKSPACE;
1199
1200         if (applyOnly)
1201             transFlags = (_noTransScripts | _noTransTriggers |
1202                 RPMTRANS_FLAG_APPLYONLY | RPMTRANS_FLAG_PKGCOMMIT);
1203
1204         if (test) transFlags |= RPMTRANS_FLAG_TEST;
1205         /* RPMTRANS_FLAG_BUILD_PROBS */
1206
1207         if (noScripts) transFlags |= (_noTransScripts | _noTransTriggers);
1208         if (noPre) transFlags |= RPMTRANS_FLAG_NOPRE;
1209         if (noPost) transFlags |= RPMTRANS_FLAG_NOPOST;
1210         if (noPreun) transFlags |= RPMTRANS_FLAG_NOPREUN;
1211         if (noPostun) transFlags |= RPMTRANS_FLAG_NOPOSTUN;
1212
1213         if (noTriggers) transFlags |= RPMTRANS_FLAG_NOTRIGGERS;
1214         if (noTPrein) transFlags |= RPMTRANS_FLAG_NOTRIGGERPREIN;
1215         if (noTIn) transFlags |= RPMTRANS_FLAG_NOTRIGGERIN;
1216         if (noTUn) transFlags |= RPMTRANS_FLAG_NOTRIGGERUN;
1217         if (noTPostun) transFlags |= RPMTRANS_FLAG_NOTRIGGERPOSTUN;
1218
1219         if (justdb) transFlags |= RPMTRANS_FLAG_JUSTDB;
1220         if (!incldocs) {
1221             if (excldocs)
1222                 transFlags |= RPMTRANS_FLAG_NODOCS;
1223             else if (rpmExpandNumeric("%{_excludedocs}"))
1224                 transFlags |= RPMTRANS_FLAG_NODOCS;
1225         }
1226         if (allFiles) transFlags |= RPMTRANS_FLAG_ALLFILES;
1227         if (dirStash) transFlags |= RPMTRANS_FLAG_DIRSTASH;
1228         if (rePackage) transFlags |= RPMTRANS_FLAG_REPACKAGE;
1229         if (pkgCommit) transFlags |= RPMTRANS_FLAG_PKGCOMMIT;
1230         if (pkgUndo) transFlags |= RPMTRANS_FLAG_PKGUNDO;
1231         if (tsCommit) transFlags |= RPMTRANS_FLAG_COMMIT;
1232         if (tsUndo) transFlags |= RPMTRANS_FLAG_UNDO;
1233         /* RPMTRANS_FLAG_KEEPOBSOLETE */
1234
1235         if (showPercents) installInterfaceFlags |= INSTALL_PERCENT;
1236         if (showHash) installInterfaceFlags |= INSTALL_HASH;
1237         if (noDeps) installInterfaceFlags |= INSTALL_NODEPS;
1238         if (noOrder) installInterfaceFlags |= INSTALL_NOORDER;
1239         if (upgrade) installInterfaceFlags |= INSTALL_UPGRADE;
1240         if (freshen) installInterfaceFlags |= (INSTALL_UPGRADE|INSTALL_FRESHEN);
1241
1242         if (!poptPeekArg(optCon))
1243             argerror(_("no packages given for install"));
1244
1245         /* we've already ensured !(!prefix && !relocations) */
1246         if (prefix) {
1247             relocations = alloca(2 * sizeof(*relocations));
1248             relocations[0].oldPath = NULL;   /* special case magic */
1249             relocations[0].newPath = prefix;
1250             relocations[1].oldPath = relocations[1].newPath = NULL;
1251         } else if (relocations) {
1252             relocations = xrealloc(relocations, 
1253                                   sizeof(*relocations) * (numRelocations + 1));
1254             relocations[numRelocations].oldPath = NULL;
1255             relocations[numRelocations].newPath = NULL;
1256         }
1257
1258         ec += rpmInstall(rootdir, (const char **)poptGetArgs(optCon), 
1259                         transFlags, installInterfaceFlags, probFilter,
1260                         relocations);
1261         break;
1262
1263       case MODE_QUERY:
1264         qva->qva_prefix = rootdir;
1265         if (qva->qva_source == RPMQV_ALL) {
1266             if (poptPeekArg(optCon))
1267                 argerror(_("extra arguments given for query of all packages"));
1268
1269             ec = rpmQuery(qva, RPMQV_ALL, NULL);
1270         } else {
1271             if (!poptPeekArg(optCon))
1272                 argerror(_("no arguments given for query"));
1273             while ((pkg = poptGetArg(optCon)))
1274                 ec += rpmQuery(qva, qva->qva_source, pkg);
1275         }
1276         break;
1277
1278       case MODE_VERIFY:
1279         verifyFlags = VERIFY_ALL;
1280         verifyFlags &= ~qva->qva_flags;
1281         if (noDeps)     verifyFlags &= ~VERIFY_DEPS;
1282         if (noScripts)  verifyFlags &= ~VERIFY_SCRIPT;
1283         if (noMd5)      verifyFlags &= ~VERIFY_MD5;
1284
1285         qva->qva_prefix = rootdir;
1286         qva->qva_flags = verifyFlags;
1287         if (qva->qva_source == RPMQV_ALL) {
1288             if (poptPeekArg(optCon))
1289                 argerror(_("extra arguments given for verify of all packages"));
1290             ec = rpmVerify(qva, RPMQV_ALL, NULL);
1291         } else {
1292             if (!poptPeekArg(optCon))
1293                 argerror(_("no arguments given for verify"));
1294             while ((pkg = poptGetArg(optCon)))
1295                 ec += rpmVerify(qva, qva->qva_source, pkg);
1296         }
1297         break;
1298     }
1299
1300     optCon = poptFreeContext(optCon);
1301     rpmFreeMacros(NULL);
1302     rpmFreeMacros(&rpmCLIMacroContext);
1303     rpmFreeRpmrc();
1304
1305     if (pipeChild) {
1306         fclose(stdout);
1307         (void)waitpid(pipeChild, &status, 0);
1308     }
1309
1310     /* keeps memory leak checkers quiet */
1311     freeNames();
1312     freeFilesystems();
1313     urlFreeCache();
1314     if (qva->qva_queryFormat) free((void *)qva->qva_queryFormat);
1315
1316 #if HAVE_MCHECK_H && HAVE_MTRACE
1317     muntrace();   /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
1318 #endif
1319     return ec;
1320 }