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