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