home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / rpm / rpm.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  45KB  |  1,320 lines

  1. #include "miscfn.h"
  2.  
  3. #if HAVE_ALLOCA_H
  4. # include <alloca.h>
  5. #endif
  6.  
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <locale.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/time.h>
  15. #include <sys/resource.h>
  16. #include <sys/wait.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19.  
  20. #include "build/build.h"
  21. #include "checksig.h"
  22. #include "install.h"
  23. #include "intl.h"
  24. #include "lib/messages.h"
  25. #include "lib/signature.h"
  26. #include "misc/popt.h"
  27. #include "miscfn.h"
  28. #include "query.h"
  29. #include "rpmlib.h"
  30. #include "verify.h"
  31.  
  32. #define GETOPT_QUERYFORMAT    1000
  33. #define GETOPT_WHATREQUIRES    1001
  34. #define GETOPT_WHATPROVIDES    1002
  35. #define GETOPT_REBUILD        1003
  36. #define GETOPT_RECOMPILE    1004
  37. #define GETOPT_ADDSIGN        1005
  38. #define GETOPT_RESIGN        1006
  39. #define GETOPT_BUILDROOT     1007
  40. #define GETOPT_QUERYBYNUMBER    1009
  41. #define GETOPT_DBPATH        1010
  42. #define GETOPT_TIMECHECK        1012
  43. #define GETOPT_REBUILDDB        1013
  44. #define GETOPT_INSTALL        1014
  45.  
  46. char * version = VERSION;
  47.  
  48. enum modes { MODE_QUERY, MODE_INSTALL, MODE_UNINSTALL, MODE_VERIFY,
  49.          MODE_BUILD, MODE_REBUILD, MODE_CHECKSIG, MODE_RESIGN,
  50.          MODE_RECOMPILE, MODE_QUERYTAGS, MODE_INITDB, MODE_TARBUILD,
  51.          MODE_REBUILDDB, MODE_UNKNOWN };
  52.  
  53. static void argerror(char * desc);
  54.  
  55. static void argerror(char * desc) {
  56.     fprintf(stderr, "rpm: %s\n", desc);
  57.     exit(1);
  58. }
  59.  
  60. static void printHelp(void);
  61. static void printVersion(void);
  62. static void printBanner(void);
  63. static void printUsage(void);
  64. static void printHelpLine(char * prefix, char * help);
  65. static int build(char *arg, int buildAmount, char *passPhrase,
  66.              char *buildRootOverride, int fromTarball);
  67.  
  68. static void printVersion(void) {
  69.     printf(_("RPM version %s\n"), version);
  70. }
  71.  
  72. static void printBanner(void) {
  73.     puts(_("Copyright (C) 1997 - Red Hat Software"));
  74.     puts(_("This may be freely redistributed under the terms of the GNU "
  75.        "Public License"));
  76. }
  77.  
  78. static void printUsage(void) {
  79.     printVersion();
  80.     printBanner();
  81.     puts("");
  82.  
  83.     puts(_("usage: rpm {--help}"));
  84.     puts(_("       rpm {--version}"));
  85.     puts(_("       rpm {--initdb}   [--dbpath <dir>]"));
  86.     puts(_("       rpm {--install -i} [-v] [--hash -h] [--percent] [--force] [--test]"));
  87.     puts(_("                        [--replacepkgs] [--replacefiles] [--root <dir>]"));
  88.     puts(_("                        [--excludedocs] [--includedocs] [--noscripts]"));
  89.     puts(_("                        [--rcfile <file>] [--ignorearch] [--dbpath <dir>]"));
  90.     puts(_("                        [--prefix <dir>] [--ignoreos] [--nodeps] [--allfiles]"));
  91.     puts(_("                        [--ftpproxy <host>] [--ftpport <port>] [--justdb]"));
  92.     puts(_("                        [--noorder] file1.rpm ... fileN.rpm"));
  93.     puts(_("       rpm {--upgrade -U} [-v] [--hash -h] [--percent] [--force] [--test]"));
  94.     puts(_("                        [--oldpackage] [--root <dir>] [--noscripts]"));
  95.     puts(_("                        [--excludedocs] [--includedocs] [--rcfile <file>]"));
  96.     puts(_("                        [--ignorearch]  [--dbpath <dir>] [--prefix <dir>] "));
  97.     puts(_("                        [--ftpproxy <host>] [--ftpport <port>]"));
  98.     puts(_("                        [--ignoreos] [--nodeps] [--allfiles] [--justdb]"));
  99.     puts(_("                        [--noorder] file1.rpm ... fileN.rpm"));
  100.     puts(_("       rpm {--query -q} [-afpg] [-i] [-l] [-s] [-d] [-c] [-v] [-R]"));
  101.     puts(_("                        [--scripts] [--root <dir>] [--rcfile <file>]"));
  102.     puts(_("                        [--whatprovides] [--whatrequires] [--requires]"));
  103.     puts(_("                        [--ftpuseport] [--ftpproxy <host>] [--ftpport <port>]"));
  104.     puts(_("                        [--provides] [--dump] [--dbpath <dir>] [--changelog]"));
  105.     puts(_("                        [targets]"));
  106.     puts(_("       rpm {--verify -V -y} [-afpg] [--root <dir>] [--rcfile <file>]"));
  107.     puts(_("                        [--dbpath <dir>] [--nodeps] [--nofiles] [--noscripts]"));
  108.     puts(_("                        [--nomd5] [targets]"));
  109.     puts(_("       rpm {--setperms} [-afpg] [target]"));
  110.     puts(_("       rpm {--setugids} [-afpg] [target]"));
  111.     puts(_("       rpm {--erase -e} [--root <dir>] [--noscripts] [--rcfile <file>]"));
  112.     puts(_("                        [--dbpath <dir>] [--nodeps] [--allmatches]"));
  113.     puts(_("                        [--justdb] package1 ... packageN"));
  114.     puts(_("       rpm {-b|t}[plciba] [-v] [--short-circuit] [--clean] [--rcfile  <file>]"));
  115.     puts(_("                        [--sign] [--test] [--timecheck <s>] [--buildos <os>]"));
  116.     puts(_("                        [--buildarch <arch>] specfile"));
  117.     puts(_("       rpm {--rebuild} [--rcfile <file>] [-v] source1.rpm ... sourceN.rpm"));
  118.     puts(_("       rpm {--recompile} [--rcfile <file>] [-v] source1.rpm ... sourceN.rpm"));
  119.     puts(_("       rpm {--resign} [--rcfile <file>] package1 package2 ... packageN"));
  120.     puts(_("       rpm {--addsign} [--rcfile <file>] package1 package2 ... packageN"));
  121.     puts(_("       rpm {--checksig -K} [--nopgp] [--nomd5] [--rcfile <file>]"));
  122.     puts(_("                           package1 ... packageN"));
  123.     puts(_("       rpm {--rebuilddb} [--rcfile <file>] [--dbpath <dir>]"));
  124.     puts(_("       rpm {--querytags}"));
  125. }
  126.  
  127. static void printHelpLine(char * prefix, char * help) {
  128.     int indentLength = strlen(prefix) + 3;
  129.     int lineLength = 79 - indentLength;
  130.     int helpLength = strlen(help);
  131.     char * ch;
  132.     char format[10];
  133.  
  134.     printf("%s - ", prefix);
  135.  
  136.     while (helpLength > lineLength) {
  137.     ch = help + lineLength - 1;
  138.     while (ch > help && !isspace(*ch)) ch--;
  139.     if (ch == help) break;        /* give up */
  140.     while (ch > (help + 1) && isspace(*ch)) ch--;
  141.     ch++;
  142.  
  143.     sprintf(format, "%%.%ds\n%%%ds", (int) (ch - help), indentLength);
  144.     printf(format, help, " ");
  145.     help = ch;
  146.     while (isspace(*help) && *help) help++;
  147.     helpLength = strlen(help);
  148.     }
  149.  
  150.     if (helpLength) puts(help);
  151. }
  152.  
  153. static void printHelp(void) {
  154.     printVersion();
  155.     printBanner();
  156.     puts("");
  157.  
  158.     puts(_("usage:"));
  159.     printHelpLine("   --help                 ", 
  160.           _("print this message"));
  161.     printHelpLine("   --version              ",
  162.           _("print the version of rpm being used"));
  163.     puts(_("   all modes support the following arguments:"));
  164.     printHelpLine("      --rcfile <file>     ",
  165.           _("use <file> instead of /etc/rpmrc and $HOME/.rpmrc"));
  166.     printHelpLine("     -v                   ",
  167.           _("be a little more verbose"));
  168.     printHelpLine("     -vv                  ",
  169.           _("be incredibly verbose (for debugging)"));
  170.     printHelpLine("   -q                     ",
  171.           _("query mode"));
  172.     printHelpLine("      --root <dir>        ",
  173.           _("use <dir> as the top level directory"));
  174.     printHelpLine("      --dbpath <dir>      ",
  175.           _("use <dir> as the directory for the database"));
  176.     printHelpLine("      --queryformat <s>   ",
  177.           _("use s as the header format (implies -i)"));
  178.     puts(_("   install, upgrade and query (with -p) allow ftp URL's to be used in place"));
  179.     puts(_("   of file names as well as the following options:\n"));
  180.     printHelpLine("      --ftpproxy <host>   ",
  181.           _("hostname or IP of ftp proxy"));
  182.     printHelpLine("      --ftpport <port>    ",
  183.           _("port number of ftp server (or proxy)"));
  184.     puts(       _("      Package specification options:"));
  185.     printHelpLine("        -a                ",
  186.           _("query all packages"));
  187.     printHelpLine("        -f <file>+        ",
  188.           _("query package owning <file>"));
  189.     printHelpLine("        -p <packagefile>+ ",
  190.           _("query (uninstalled) package <packagefile>"));
  191.     printHelpLine("        --whatprovides <i>",
  192.           _("query packages which provide <i> capability"));
  193.     printHelpLine("        --whatrequires <i>",
  194.           _("query packages which require <i> capability"));
  195.     puts(_("      Information selection options:"));
  196.     printHelpLine("        -i                ",
  197.           _("display package information"));
  198.     printHelpLine("        --changelog       ",
  199.           _("display the package's change log"));
  200.     printHelpLine("        -l                ",
  201.           _("display package file list"));
  202.     printHelpLine("        -s                ",
  203.           _("show file states (implies -l)"));
  204.     printHelpLine("        -d                ",
  205.           _("list only documentation files (implies -l)"));
  206.     printHelpLine("        -c                ",
  207.           _("list only configuration files (implies -l)"));
  208.     printHelpLine("        --dump            ",
  209.           _("show all verifiable information for each file (must be used with -l, -c, or -d)"));
  210.     printHelpLine("        --provides        ",
  211.           _("list capabilities package provides"));
  212.     puts(       _("        --requires"));
  213.     printHelpLine("        -R                ",
  214.           _("list package dependencies"));
  215.     printHelpLine("        --scripts         ",
  216.           _("print the various [un]install scripts"));
  217.     puts(         "");
  218.     puts(         "    -V");
  219.     puts(         "    -y");
  220.     printHelpLine("    --pipe <cmd>        ",
  221.           _("send stdout to <cmd>"));
  222.     printHelpLine("    --verify              ",
  223.           _("verify a package installation using the same same package specification options as -q"));
  224.     printHelpLine("      --dbpath <dir>      ",
  225.           _("use <dir> as the directory for the database"));
  226.     printHelpLine("      --root <dir>        ",
  227.           _("use <dir> as the top level directory"));
  228.     printHelpLine("      --nodeps            ",
  229.           _("do not verify package dependencies"));
  230.     printHelpLine("      --nomd5             ",
  231.           _("do not verify file md5 checksums"));
  232.     printHelpLine("      --nofiles           ",
  233.           _("do not verify file attributes"));
  234.     puts("");
  235.     printHelpLine("    --setperms            ",
  236.           _("set the file permissions to those in the package database"
  237.             " using the same package specification options as -q"));
  238.     printHelpLine("    --setugids            ",
  239.           _("set the file owner and group to those in the package "
  240.             "database using the same package specification options as "
  241.             "-q"));
  242.     puts("");
  243.     puts(         "    --install <packagefile>");
  244.     printHelpLine("    -i <packagefile>      ",
  245.           _("install package"));
  246.     printHelpLine("      --prefix <dir>      ",
  247.           _("relocate the package to <dir>, if relocatable"));
  248.     printHelpLine("      --dbpath <dir>      ",
  249.           _("use <dir> as the directory for the database"));
  250.     printHelpLine("      --excludedocs       ",
  251.           _("do not install documentation"));
  252.     printHelpLine("      --force             ",
  253.           _("short hand for --replacepkgs --replacefiles"));
  254.     puts(         "      -h");
  255.     printHelpLine("      --hash              ",
  256.           _("print hash marks as package installs (good with -v)"));
  257.     printHelpLine("      --allfiles          ",
  258.           _("install all files, even configurations which might "
  259.             "otherwise be skipped"));
  260.     printHelpLine("      --ignorearch        ",
  261.           _("don't verify package architecture"));
  262.     printHelpLine("      --ignoreos          ",
  263.           _("don't verify package operating system"));
  264.     printHelpLine("      --includedocs       ",
  265.           _("install documentation"));
  266.     printHelpLine("      --justdb            ",
  267.           _("update the database, but do not modify the filesystem"));
  268.     printHelpLine("      --nodeps            ",
  269.           _("do not verify package dependencies"));
  270.     printHelpLine("      --noorder           ",
  271.           _("do not reorder package installation to satisfy dependencies"));
  272.     printHelpLine("      --noscripts         ",
  273.           _("don't execute any installation scripts"));
  274.     printHelpLine("      --percent           ",
  275.           _("print percentages as package installs"));
  276.     printHelpLine("      --replacefiles      ",
  277.           _("install even if the package replaces installed files"));
  278.     printHelpLine("      --replacepkgs       ",
  279.           _("reinstall if the package is already present"));
  280.     printHelpLine("      --root <dir>        ",
  281.           _("use <dir> as the top level directory"));
  282.     printHelpLine("      --test              ",
  283.           _("don't install, but tell if it would work or not"));
  284.     puts("");
  285.     puts(         "    --upgrade <packagefile>");
  286.     printHelpLine("    -U <packagefile>      ",
  287.           _("upgrade package (same options as --install, plus)"));
  288.     printHelpLine("      --oldpackage        ",
  289.           _("upgrade to an old version of the package (--force on upgrades does this automatically)"));
  290.     puts("");
  291.     puts(         "    --erase <package>");
  292.     printHelpLine("    -e <package>          ",
  293.           _("erase (uninstall) package"));
  294.     printHelpLine("      --allmatches        ",
  295.           _("remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)"));
  296.     printHelpLine("      --dbpath <dir>      ",
  297.           _("use <dir> as the directory for the database"));
  298.     printHelpLine("      --justdb            ",
  299.           _("update the database, but do not modify the filesystem"));
  300.     printHelpLine("      --nodeps            ",
  301.           _("do not verify package dependencies"));
  302.     printHelpLine("      --noorder           ",
  303.           _("do not reorder package installation to satisfy dependencies"));
  304.     printHelpLine("      --noscripts         ",
  305.           _("do not execute any package specific scripts"));
  306.     printHelpLine("      --root <dir>        ",
  307.           _("use <dir> as the top level directory"));
  308.     puts("");
  309.     puts(_("    -b<stage> <spec>      "));
  310.     printHelpLine("    -t<stage> <tarball>      ",
  311.           _("build package, where <stage> is one of:"));
  312.     printHelpLine("          p               ",
  313.           _("prep (unpack sources and apply patches)"));
  314.     printHelpLine("          l               ",
  315.           _("list check (do some cursory checks on %files)"));
  316.     printHelpLine("          c               ",
  317.           _("compile (prep and compile)"));
  318.     printHelpLine("          i               ",
  319.           _("install (prep, compile, install)"));
  320.     printHelpLine("          b               ",
  321.           _("binary package (prep, compile, install, package)"));
  322.     printHelpLine("          a               ",
  323.           _("bin/src package (prep, compile, install, package)"));
  324.     printHelpLine("      --short-circuit     ",
  325.           _("skip straight to specified stage (only for c,i)"));
  326.     printHelpLine("      --clean             ",
  327.           _("remove build tree when done"));
  328.     printHelpLine("      --sign              ",
  329.           _("generate PGP signature"));
  330.     printHelpLine("      --buildroot <dir>     ",
  331.           _("use <dir> as the build root"));
  332.     printHelpLine("      --buildarch <arch>  ",
  333.           _("build the packages for architecture <arch>"));
  334.     printHelpLine("      --buildos <os>  ",
  335.           _("build the packages for ositecture <os>"));
  336.     printHelpLine("      --test              ",
  337.           _("do not execute any stages"));
  338.     printHelpLine("      --timecheck <s>     ",
  339.           _("set the time check to S seconds (0 disables it)"));
  340.     puts("");
  341.     printHelpLine("    --rebuild <src_pkg>   ",
  342.           _("install source package, build binary package and remove spec file, sources, patches, and icons."));
  343.     printHelpLine("    --recompile <src_pkg> ",
  344.           _("like --rebuild, but don't build any package"));
  345.     printHelpLine("    --resign <pkg>+       ",
  346.           _("sign a package (discard current signature)"));
  347.     printHelpLine("    --addsign <pkg>+      ",
  348.           _("add a signature to a package"));
  349.     puts(         "    -K");
  350.     printHelpLine("    --checksig <pkg>+     ",
  351.           _("verify package signature"));
  352.     printHelpLine("      --nopgp             ",
  353.           _("skip any PGP signatures"));
  354.     printHelpLine("      --nomd5             ",
  355.           _("skip any MD5 signatures"));
  356.     printHelpLine("    --querytags           ",
  357.           _("list the tags that can be used in a query format"));
  358.     printHelpLine("    --initdb              ",
  359.           _("make sure a valid database exists"));
  360.     printHelpLine("    --rebuilddb           ",
  361.           _("rebuild database from existing database"));
  362.     printHelpLine("      --dbpath <dir>      ",
  363.           _("use <dir> as the directory for the database"));
  364.     printHelpLine("      --root <dir>        ",
  365.           _("use <dir> as the top level directory"));
  366. }
  367.  
  368. static int build(char *arg, int buildAmount, char *passPhrase,
  369.              char *buildRootOverride, int fromTarball) {
  370.     FILE *f;
  371.     Spec *s, *specArray;
  372.     char * specfile;
  373.     int res = 0;
  374.     struct stat statbuf;
  375.     char * specDir;
  376.     char * tmpSpecFile;
  377.     char * cmd;
  378.     char buf[1024];
  379.     int flags;
  380.  
  381.     if (fromTarball) {
  382.     specDir = rpmGetVar(RPMVAR_SPECDIR);
  383.     tmpSpecFile = alloca(1024);
  384.     sprintf(tmpSpecFile, "%s/rpm-spec-file-%d", specDir, (int) getpid());
  385.  
  386.     cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
  387.     sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
  388.             tmpSpecFile);
  389.     if (!(f = popen(cmd, "r"))) {
  390.         fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
  391.             strerror(errno));
  392.         return 1;
  393.     }
  394.     if (!fgets(buf, sizeof(buf) - 1, f)) {
  395.         fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
  396.         unlink(tmpSpecFile);
  397.         return 1;
  398.     }
  399.     pclose(f);
  400.  
  401.     cmd = specfile = buf;
  402.     while (*cmd) {
  403.         if (*cmd == '/') specfile = cmd + 1;
  404.         cmd++;
  405.     }
  406.  
  407.     cmd = specfile;
  408.  
  409.     /* remove trailing \n */
  410.     specfile = cmd + strlen(cmd) - 1;
  411.     *specfile = '\0';
  412.  
  413.     specfile = alloca(strlen(specDir) + strlen(cmd) + 5);
  414.     sprintf(specfile, "%s/%s", specDir, cmd);
  415.     
  416.     if (rename(tmpSpecFile, specfile)) {
  417.         fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
  418.             tmpSpecFile, specfile, strerror(errno));
  419.         unlink(tmpSpecFile);
  420.         return 1;
  421.     }
  422.  
  423.     /* Make the directory which contains the tarball the source 
  424.        directory for this run */
  425.  
  426.     if (*arg != '/') {
  427.         /* XXX this is broken if PWD is near 1024 */
  428.         getcwd(buf, 1024);
  429.         strcat(buf, "/");
  430.         strcat(buf, arg);
  431.     } else 
  432.         strcpy(buf, arg);
  433.  
  434.     cmd = buf + strlen(buf) - 1;
  435.     while (*cmd != '/') cmd--;
  436.     *cmd = '\0';
  437.  
  438.     rpmSetVar(RPMVAR_SOURCEDIR, buf);
  439.     } else if (arg[0] == '/') {
  440.     specfile = arg;
  441.     } else {
  442.     /* XXX this is broken if PWD is near 1024 */
  443.     specfile = alloca(1024);
  444.     getcwd(specfile, 1024);
  445.     strcat(specfile, "/");
  446.     strcat(specfile, arg);
  447.     }
  448.  
  449.     stat(specfile, &statbuf);
  450.     if (! S_ISREG(statbuf.st_mode)) {
  451.     rpmError(RPMERR_BADSPEC, _("File is not a regular file: %s\n"),
  452.          specfile);
  453.     return 1;
  454.     }
  455.     
  456.     if (!(f = fopen(specfile, "r"))) {
  457.     fprintf(stderr, _("unable to open: %s\n"), specfile);
  458.     return 1;
  459.     }
  460.     
  461.     s = specArray = parseSpec(f, specfile, buildRootOverride);
  462.     fclose(f);
  463.     if (! specArray) {
  464.     /* Spec parse failed -- could be Exclude: Exclusive: */
  465.     res = 1;
  466.     if (rpmErrorCode() == RPMERR_BADARCH) {
  467.         fprintf(stderr, _("%s doesn't build on this architecture\n"), arg);
  468.     } else {
  469.         fprintf(stderr, _("Build failed.\n"));
  470.     }
  471.     } else {
  472.     while (*s && !res) {
  473.         if (verifySpec(*s)) {
  474.         fprintf(stderr, _("\n%cSpec file check failed!!\n"), 7);
  475.         fprintf(stderr,
  476.             _("Tell rpm-list@redhat.com if this is incorrect.\n\n"));
  477.         res = 1;
  478.         } else {
  479.         flags = buildAmount;
  480.         /* Don't build source package or remove sources */
  481.         /* unless this is the last package being built. */
  482.         if (*(s+1)) {
  483.             flags = flags & ~RPMBUILD_SOURCE;
  484.             flags = flags & ~RPMBUILD_RMSOURCE;
  485.         }
  486.         if (doBuild(*s, flags, passPhrase)) {
  487.             fprintf(stderr, _("Build failed.\n"));
  488.             res = 1;
  489.         }
  490.         }
  491.         s++;
  492.     }
  493.  
  494.     s = specArray;
  495.     while (*s) {
  496.         freeSpec(*s++);
  497.     }
  498.     free(specArray);
  499.     }
  500.  
  501.     if (fromTarball) unlink(specfile);
  502.  
  503.     return res;
  504. }
  505.  
  506. int main(int argc, char ** argv) {
  507.     enum modes bigMode = MODE_UNKNOWN;
  508.     enum querysources querySource = QUERY_PACKAGE;
  509.     enum verifysources verifySource = VERIFY_PACKAGE;
  510.     int arg, len;
  511.     int queryFor = 0, test = 0, version = 0, help = 0, force = 0;
  512.     int quiet = 0, replaceFiles = 0, replacePackages = 0, showPercents = 0;
  513.     int showHash = 0, installFlags = 0, uninstallFlags = 0, interfaceFlags = 0;
  514.     int buildAmount = 0, oldPackage = 0, clean = 0, signIt = 0;
  515.     int shortCircuit = 0, queryTags = 0, excldocs = 0;
  516.     int incldocs = 0, noScripts = 0, noDeps = 0, allMatches = 0, noOrder = 0;
  517.     int noPgp = 0, dump = 0, initdb = 0, ignoreArch = 0, showrc = 0;
  518.     int gotDbpath = 0, building = 0, ignoreOs = 0, noFiles = 0, verifyFlags;
  519.     int noMd5 = 0, allFiles = 0, justdb = 0;
  520.     int checksigFlags = 0;
  521.     char *tce;
  522.     int timeCheck = 0;
  523.     int addSign = NEW_SIGNATURE;
  524.     char * rcfile = NULL, * queryFormat = NULL, * prefix = NULL;
  525.     char buildChar = ' ';
  526.     char * rootdir = "/";
  527.     char * pipeOutput = NULL;
  528.     char * specFile;
  529.     char *passPhrase = "";
  530.     char *buildRootOverride = NULL;
  531.     char *arch = NULL;
  532.     char * ftpProxy = NULL, * ftpPort = NULL;
  533.     char *os = NULL;
  534.     char * optArg;
  535.     pid_t pipeChild = 0;
  536.     char * pkg;
  537.     char ** currarg;
  538.     char * errString;
  539.     poptContext optCon;
  540.     char * infoCommand[] = { "--info", NULL };
  541.     char * installCommand[] = { "--install", NULL };
  542.     int ec = 0;
  543.     int status;
  544.     int p[2];
  545.     struct poptOption optionsTable[] = {
  546.         { "addsign", '\0', 0, 0, GETOPT_ADDSIGN },
  547.         { "all", 'a', 0, 0, 'a' },
  548.         { "allfiles", '\0', 0, &allFiles, 0 },
  549.         { "allmatches", 'a', 0, &allMatches, 0 },
  550.         { "build", 'b', POPT_ARG_STRING, 0, 'b' },
  551.         { "buildarch", '\0', POPT_ARG_STRING, 0, 0 },
  552.         { "buildos", '\0', POPT_ARG_STRING, 0, 0 },
  553.         { "buildroot", '\0', POPT_ARG_STRING, 0, GETOPT_BUILDROOT },
  554.         { "checksig", 'K', 0, 0, 'K' },
  555.         { "clean", '\0', 0, &clean, 0 },
  556.         { "configfiles", 'c', 0, 0, 'c' },
  557.         { "dbpath", '\0', POPT_ARG_STRING, 0, GETOPT_DBPATH },
  558.         { "docfiles", 'd', 0, 0, 'd' },
  559.         { "dump", '\0', 0, &dump, 0 },
  560.         { "erase", 'e', 0, 0, 'e' },
  561.             { "excludedocs", '\0', 0, &excldocs, 0},
  562.         { "file", 'f', 0, 0, 'f' },
  563.         { "force", '\0', 0, &force, 0 },
  564.         { "ftpport", '\0', POPT_ARG_STRING, &ftpPort, 0},
  565.         { "ftpproxy", '\0', POPT_ARG_STRING, &ftpProxy, 0},
  566.         { "group", 'g', 0, 0, 'g' },
  567.         { "hash", 'h', 0, &showHash, 0 },
  568.         { "help", '\0', 0, &help, 0 },
  569.         {  NULL, 'i', 0, 0, 'i' },
  570.         { "ignorearch", '\0', 0, &ignoreArch, 0 },
  571.         { "ignoreos", '\0', 0, &ignoreOs, 0 },
  572.             { "includedocs", '\0', 0, &incldocs, 0},
  573.         { "initdb", '\0', 0, &initdb, 0 },
  574.     /* info and install both using 'i' is dumb */
  575.         { "install", '\0', 0, 0, GETOPT_INSTALL },
  576.         { "justdb", '\0', 0, &justdb, 0 },
  577.         { "list", 'l', 0, 0, 'l' },
  578.         { "nodeps", '\0', 0, &noDeps, 0 },
  579.         { "noorder", '\0', 0, &noOrder, 0 },
  580.         { "nofiles", '\0', 0, &noFiles, 0 },
  581.         { "nomd5", '\0', 0, &noMd5, 0 },
  582.         { "nopgp", '\0', 0, &noPgp, 0 },
  583.         { "noscripts", '\0', 0, &noScripts, 0 },
  584.         { "oldpackage", '\0', 0, &oldPackage, 0 },
  585.         { "package", 'p', 0, 0, 'p' },
  586.         { "percent", '\0', 0, &showPercents, 0 },
  587.         { "pipe", '\0', POPT_ARG_STRING, &pipeOutput, 0 },
  588.         { "prefix", '\0', POPT_ARG_STRING, &prefix, 0 },
  589.         { "qf", '\0', POPT_ARG_STRING, 0, GETOPT_QUERYFORMAT },
  590.         { "query", 'q', 0, 0, 'q' },
  591.         { "querybynumber", '\0', 0, 0, GETOPT_QUERYBYNUMBER },
  592.         { "queryformat", '\0', POPT_ARG_STRING, 0, GETOPT_QUERYFORMAT },
  593.         { "querytags", '\0', 0, &queryTags, 0 },
  594.         { "quiet", '\0', 0, &quiet, 0 },
  595.         { "rcfile", '\0', POPT_ARG_STRING, 0, 0 },
  596.         { "recompile", '\0', 0, 0, GETOPT_RECOMPILE },
  597.         { "rebuild", '\0', 0, 0, GETOPT_REBUILD },
  598.         { "rebuilddb", '\0', 0, 0, GETOPT_REBUILDDB },
  599.         { "replacefiles", '\0', 0, &replaceFiles, 0 },
  600.         { "replacepkgs", '\0', 0, &replacePackages, 0 },
  601.         { "resign", '\0', 0, 0, GETOPT_RESIGN },
  602.         { "root", 'r', POPT_ARG_STRING, &rootdir, 0 },
  603.         { "short-circuit", '\0', 0, &shortCircuit, 0 },
  604.         { "showrc", '\0', 0, 0, 0 },
  605.         { "sign", '\0', 0, &signIt, 0 },
  606.         { "state", 's', 0, 0, 's' },
  607.         { "tarball", 't', POPT_ARG_STRING, 0, 't' },
  608.         { "test", '\0', 0, &test, 0 },
  609.         { "timecheck", '\0', POPT_ARG_STRING, 0, GETOPT_TIMECHECK },
  610.         { "upgrade", 'U', 0, 0, 'U' },
  611.         { "uninstall", 'u', 0, 0, 'u' },
  612.         { "verbose", 'v', 0, 0, 'v' },
  613.         { "verify", 'V', 0, 0, 'V' },
  614.         {  NULL, 'y', 0, 0, 'V' },
  615.         { "version", '\0', 0, &version, 0 },
  616.         { "whatrequires", '\0', 0, 0, GETOPT_WHATREQUIRES },
  617.         { "whatprovides", '\0', 0, 0, GETOPT_WHATPROVIDES },
  618.         { 0, 0, 0, 0, 0 } 
  619.     } ;
  620.  
  621.     /* set up the correct locale */
  622.     setlocale(LC_ALL, "" );
  623.  
  624.     bindtextdomain(RPMNLSPACKAGE, RPMNLSDIR);
  625.     textdomain(RPMNLSPACKAGE);
  626.  
  627.     /* Make a first pass through the arguments, looking for --rcfile */
  628.     /* as well as --arch and --os.  We need to handle that before    */
  629.     /* dealing with the rest of the arguments.                       */
  630.     currarg = argv;
  631.     while (*currarg) {
  632.     if (!strcmp(*currarg, "--rcfile")) {
  633.         rcfile = *(++currarg);
  634.     } else if (!strcmp(*currarg, "--buildarch")) {
  635.         arch = *(++currarg);
  636.     } else if (!strcmp(*currarg, "--buildos")) {
  637.         os = *(++currarg);
  638.     } else if (!strcmp(*currarg, "--showrc")) {
  639.         showrc = 1;
  640.         building = 1;
  641.     } else if (!strncmp(*currarg, "-b", 2) ||
  642.            !strncmp(*currarg, "-t", 2) ||
  643.            !strcmp(*currarg, "--tarbuild") ||
  644.            !strcmp(*currarg, "--build") ||
  645.            !strcmp(*currarg, "--rebuild") ||
  646.            !strcmp(*currarg, "--recompile")) {
  647.         building = 1;
  648.     }
  649.  
  650.     if (*currarg) currarg++;
  651.     } 
  652.  
  653.     /* reading this early makes it easy to override */
  654.     if (rpmReadConfigFiles(rcfile, arch, os, building))  
  655.     exit(1);
  656.     if (showrc) {
  657.     rpmShowRC(stdout);
  658.     exit(0);
  659.     }
  660.  
  661.     optCon = poptGetContext("rpm", argc, argv, optionsTable, 0);
  662.     poptReadConfigFile(optCon, LIBRPMALIAS_FILENAME);
  663.     poptReadDefaultConfig(optCon, 1);
  664.  
  665.     while ((arg = poptGetNextOpt(optCon)) > 0) {
  666.     optArg = poptGetOptArg(optCon);
  667.  
  668.     switch (arg) {
  669.       case 'K':
  670.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_CHECKSIG)
  671.         argerror(_("only one major mode may be specified"));
  672.         bigMode = MODE_CHECKSIG;
  673.         break;
  674.         
  675.       case 'q':
  676.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_QUERY)
  677.         argerror(_("only one major mode may be specified"));
  678.         bigMode = MODE_QUERY;
  679.         break;
  680.  
  681.       case 'V':
  682.       case 'y':
  683.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_VERIFY)
  684.         argerror(_("only one major mode may be specified"));
  685.         bigMode = MODE_VERIFY;
  686.         break;
  687.  
  688.       case 'u':
  689.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_UNINSTALL)
  690.         argerror(_("only one major mode may be specified"));
  691.         bigMode = MODE_UNINSTALL;
  692.         rpmMessage(RPMMESS_ERROR, _("-u and --uninstall are deprecated and no"
  693.             " longer work.\n"));
  694.         rpmMessage(RPMMESS_ERROR, _("Use -e or --erase instead.\n"));
  695.         exit(1);
  696.     
  697.       case 'e':
  698.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_UNINSTALL)
  699.         argerror(_("only one major mode may be specified"));
  700.         bigMode = MODE_UNINSTALL;
  701.         break;
  702.     
  703.       case 'b':
  704.       case 't':
  705.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_BUILD)
  706.         argerror(_("only one major mode may be specified"));
  707.  
  708.         if (arg == 'b') {
  709.         bigMode = MODE_BUILD;
  710.         errString = _("--build (-b) requires one of a,b,i,c,p,l as "
  711.                 "its sole argument");
  712.         } else {
  713.         bigMode = MODE_TARBUILD;
  714.         errString = _("--tarbuild (-t) requires one of a,b,i,c,p,l as "
  715.                   "its sole argument");
  716.         }
  717.  
  718.         if (strlen(optArg) > 1) 
  719.         argerror(errString);
  720.  
  721.         buildChar = optArg[0];
  722.         switch (buildChar) {
  723.           case 'a':
  724.           case 'b':
  725.           case 'i':
  726.           case 'c':
  727.           case 'p':
  728.           case 'l':
  729.         break;
  730.           default:
  731.         argerror(errString);
  732.         }
  733.  
  734.         break;
  735.     
  736.       case 'v':
  737.         rpmIncreaseVerbosity();
  738.         break;
  739.  
  740.       case 'i':
  741.         if (bigMode == MODE_QUERY)
  742.         poptStuffArgs(optCon, infoCommand);
  743.         else if (bigMode == MODE_INSTALL)
  744.         /* ignore it */ ;
  745.         else if (bigMode == MODE_UNKNOWN)
  746.         poptStuffArgs(optCon, installCommand);
  747.         break;
  748.  
  749.       case GETOPT_INSTALL:
  750.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
  751.         argerror(_("only one major mode may be specified"));
  752.         bigMode = MODE_INSTALL;
  753.         break;
  754.  
  755.       case 'U':
  756.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_INSTALL)
  757.         argerror(_("only one major mode may be specified"));
  758.         bigMode = MODE_INSTALL;
  759.         installFlags |= RPMINSTALL_UPGRADE;
  760.         break;
  761.  
  762.       case 's':
  763.         queryFor |= QUERY_FOR_LIST | QUERY_FOR_STATE;
  764.         break;
  765.  
  766.       case 'l':
  767.         queryFor |= QUERY_FOR_LIST;
  768.         break;
  769.  
  770.       case 'd':
  771.         queryFor |= QUERY_FOR_DOCS | QUERY_FOR_LIST;
  772.         break;
  773.  
  774.       case 'c':
  775.         queryFor |= QUERY_FOR_CONFIG | QUERY_FOR_LIST;
  776.         break;
  777.  
  778.       case 'p':
  779.         if (querySource != QUERY_PACKAGE && querySource != QUERY_RPM)
  780.         argerror(_("one type of query/verify may be performed at a " "time"));
  781.         querySource = QUERY_RPM;
  782.         verifySource = VERIFY_RPM;
  783.         break;
  784.  
  785.       case 'g':
  786.         if (querySource != QUERY_PACKAGE && querySource != QUERY_GROUP)
  787.         argerror(_("one type of query/verify may be performed at a "
  788.                 "time"));
  789.         querySource = QUERY_GROUP;
  790.         verifySource = VERIFY_GRP;
  791.         break;
  792.  
  793.       case 'f':
  794.         if (querySource != QUERY_PACKAGE && querySource != QUERY_PATH)
  795.         argerror(_("one type of query/verify may be performed at a "
  796.                 "time"));
  797.         querySource = QUERY_PATH;
  798.         verifySource = VERIFY_PATH;
  799.         break;
  800.  
  801.       case 'a':
  802.         if (querySource != QUERY_PACKAGE && querySource != QUERY_ALL)
  803.         argerror(_("one type of query/verify may be performed at a "
  804.                 "time"));
  805.         querySource = QUERY_ALL;
  806.         verifySource = VERIFY_EVERY;
  807.         break;
  808.  
  809.       case GETOPT_QUERYFORMAT:
  810.         if (queryFormat) {
  811.         len = strlen(queryFormat) + strlen(optArg) + 1;
  812.         queryFormat = realloc(queryFormat, len);
  813.         strcat(queryFormat, optArg);
  814.         } else {
  815.         queryFormat = malloc(strlen(optArg) + 1);
  816.         strcpy(queryFormat, optArg);
  817.         }
  818.         break;
  819.  
  820.       case GETOPT_WHATREQUIRES:
  821.         if (querySource != QUERY_PACKAGE && 
  822.         querySource != QUERY_WHATREQUIRES)
  823.         argerror(_("one type of query/verify may be performed at a "
  824.                 "time"));
  825.         querySource = QUERY_WHATREQUIRES;
  826.         break;
  827.  
  828.       case GETOPT_WHATPROVIDES:
  829.         if (querySource != QUERY_PACKAGE && 
  830.         querySource != QUERY_WHATPROVIDES)
  831.         argerror(_("one type of query/verify may be performed at a "
  832.                 "time"));
  833.         querySource = QUERY_WHATPROVIDES;
  834.         break;
  835.  
  836.       case GETOPT_REBUILD:
  837.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_REBUILD)
  838.         argerror(_("only one major mode may be specified"));
  839.         bigMode = MODE_REBUILD;
  840.         break;
  841.  
  842.       case GETOPT_RECOMPILE:
  843.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_RECOMPILE)
  844.         argerror(_("only one major mode may be specified"));
  845.         bigMode = MODE_RECOMPILE;
  846.         break;
  847.  
  848.       case GETOPT_BUILDROOT:
  849.         if (bigMode != MODE_UNKNOWN &&
  850.         bigMode != MODE_BUILD && bigMode != MODE_REBUILD)
  851.         argerror(_("only one major mode may be specified"));
  852.         buildRootOverride = optArg;
  853.         break;
  854.  
  855.       case GETOPT_RESIGN:
  856.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_RESIGN)
  857.         argerror(_("only one major mode may be specified"));
  858.         bigMode = MODE_RESIGN;
  859.         addSign = NEW_SIGNATURE;
  860.         signIt = 1;
  861.         break;
  862.  
  863.       case GETOPT_ADDSIGN:
  864.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_RESIGN)
  865.         argerror(_("only one major mode may be specified"));
  866.         bigMode = MODE_RESIGN;
  867.         addSign = ADD_SIGNATURE;
  868.         signIt = 1;
  869.         break;
  870.  
  871.       case GETOPT_DBPATH:
  872.             if (optArg[0] != '/')
  873.                 argerror(_("arguments to --dbpath must begin with a /"));
  874.         rpmSetVar(RPMVAR_DBPATH, optArg);
  875.         gotDbpath = 1;
  876.         break;
  877.  
  878.       case GETOPT_QUERYBYNUMBER:
  879.         if (querySource != QUERY_PACKAGE && querySource != QUERY_RPM)
  880.         argerror(_("one type of query may be performed at a " 
  881.                 "time"));
  882.         querySource = QUERY_DBOFFSET;
  883.         verifySource = VERIFY_RPM;
  884.         break;
  885.  
  886.       case GETOPT_TIMECHECK:
  887.         tce = NULL;
  888.         timeCheck = strtoul(optArg, &tce, 10);
  889.         if ((*tce) || (tce == optArg) || (timeCheck == ULONG_MAX)) {
  890.         argerror("Argument to --timecheck must be integer");
  891.         }
  892.         rpmSetVar(RPMVAR_TIMECHECK, optArg);
  893.         break;
  894.  
  895.       case GETOPT_REBUILDDB:
  896.         if (bigMode != MODE_UNKNOWN && bigMode != MODE_REBUILDDB)
  897.         argerror(_("only one major mode may be specified"));
  898.         bigMode = MODE_REBUILDDB;
  899.         break;
  900.  
  901.       default:
  902.         fprintf(stderr, "Internal error in argument processing :-(\n");
  903.         exit(1);
  904.     }
  905.     }
  906.  
  907.     if (quiet)
  908.     rpmSetVerbosity(RPMMESS_QUIET);
  909.  
  910.     if (version) printVersion();
  911.     if (help) printHelp();
  912.  
  913.     if (arg < -1) {
  914.     fprintf(stderr, "%s: %s\n", 
  915.         poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
  916.         poptStrerror(arg));
  917.     exit(1);
  918.     }
  919.  
  920.     if (initdb)
  921.     if (bigMode != MODE_UNKNOWN) 
  922.         argerror(_("only one major mode may be specified"));
  923.     else
  924.         bigMode = MODE_INITDB;
  925.  
  926.     if (queryTags)
  927.     if (bigMode != MODE_UNKNOWN) 
  928.         argerror(_("only one major mode may be specified"));
  929.     else
  930.         bigMode = MODE_QUERYTAGS;
  931.  
  932.     if (buildRootOverride && bigMode != MODE_BUILD &&
  933.     bigMode != MODE_REBUILD) {
  934.     argerror("--buildroot may only be used during package builds");
  935.     }
  936.  
  937.     if (bigMode != MODE_QUERY && bigMode != MODE_INSTALL && 
  938.     bigMode != MODE_UNINSTALL && bigMode != MODE_VERIFY &&
  939.     bigMode != MODE_INITDB && bigMode != MODE_REBUILDDB && gotDbpath)
  940.     argerror(_("--dbpath given for operation that does not use a "
  941.             "database"));
  942.  
  943.     if (timeCheck && bigMode != MODE_BUILD && bigMode != MODE_REBUILD &&
  944.     bigMode != MODE_RECOMPILE) 
  945.     argerror(_("--timecheck may only be used during package builds"));
  946.     
  947.     if (bigMode != MODE_QUERY && queryFor) 
  948.     argerror(_("unexpected query specifiers"));
  949.  
  950.     if (bigMode != MODE_QUERY && queryFormat) 
  951.     argerror(_("unexpected query specifiers"));
  952.  
  953.     if (bigMode != MODE_QUERY && bigMode != MODE_VERIFY &&
  954.     querySource != QUERY_PACKAGE) 
  955.     argerror(_("unexpected query source"));
  956.  
  957.     if (bigMode != MODE_INSTALL && force)
  958.     argerror(_("only installation and upgrading may be forced"));
  959.  
  960.     if (bigMode != MODE_INSTALL && prefix)
  961.     argerror(_("--prefix may only be used when installing new packages"));
  962.  
  963.     if (prefix && prefix[0] != '/') 
  964.     argerror(_("arguments to --prefix must begin with a /"));
  965.  
  966.     if (bigMode != MODE_INSTALL && showHash)
  967.     argerror(_("--hash (-h) may only be specified during package "
  968.             "installation"));
  969.  
  970.     if (bigMode != MODE_INSTALL && showPercents)
  971.     argerror(_("--percent may only be specified during package "
  972.             "installation"));
  973.  
  974.     if (bigMode != MODE_INSTALL && replaceFiles)
  975.     argerror(_("--replacefiles may only be specified during package "
  976.             "installation"));
  977.  
  978.     if (bigMode != MODE_INSTALL && replacePackages)
  979.     argerror(_("--replacepkgs may only be specified during package "
  980.             "installation"));
  981.  
  982.     if (bigMode != MODE_INSTALL && excldocs)
  983.     argerror(_("--excludedocs may only be specified during package "
  984.            "installation"));
  985.  
  986.     if (bigMode != MODE_INSTALL && incldocs)
  987.     argerror(_("--includedocs may only be specified during package "
  988.            "installation"));
  989.  
  990.     if (excldocs && incldocs)
  991.     argerror(_("only one of --excludedocs and --includedocs may be "
  992.          "specified"));
  993.   
  994.     if (bigMode != MODE_INSTALL && ignoreArch)
  995.     argerror(_("--ignorearch may only be specified during package "
  996.            "installation"));
  997.  
  998.     if (bigMode != MODE_INSTALL && ignoreOs)
  999.     argerror(_("--ignoreos may only be specified during package "
  1000.            "installation"));
  1001.  
  1002.     if (allMatches && bigMode != MODE_UNINSTALL)
  1003.     argerror(_("--allmatches may only be specified during package "
  1004.            "erasure"));
  1005.  
  1006.     if (allFiles && bigMode != MODE_INSTALL)
  1007.     argerror(_("--allfiles may only be specified during package "
  1008.            "installation"));
  1009.  
  1010.     if (justdb && bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL)
  1011.     argerror(_("--justdb may only be specified during package "
  1012.            "installation and erasure"));
  1013.  
  1014.     if (bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL && 
  1015.     bigMode != MODE_VERIFY && noScripts)
  1016.     argerror(_("--noscripts may only be specified during package "
  1017.            "installation, erasure, and verification"));
  1018.  
  1019.     if (bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL && 
  1020.     bigMode != MODE_VERIFY && noDeps)
  1021.     argerror(_("--nodeps may only be specified during package "
  1022.            "installation, erasure, and verification"));
  1023.  
  1024.     if (bigMode != MODE_VERIFY && noFiles)
  1025.     argerror(_("--nofiles may only be specified during package "
  1026.            "verification"));
  1027.  
  1028.     if (bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL &&
  1029.     bigMode != MODE_BUILD && test)
  1030.     argerror(_("--test may only be specified during package installation, "
  1031.          "erasure, and building"));
  1032.  
  1033.     if (bigMode != MODE_INSTALL && bigMode != MODE_UNINSTALL && 
  1034.     bigMode != MODE_QUERY   && bigMode != MODE_VERIFY    &&             bigMode != MODE_REBUILDDB && bigMode != MODE_INITDB && rootdir[1])
  1035.     argerror(_("--root (-r) may only be specified during "
  1036.          "installation, erasure, querying, and "
  1037.          "database rebuilds"));
  1038.  
  1039.     if (rootdir && rootdir[0] != '/')
  1040.     argerror(_("arguments to --root (-r) must begin with a /"));
  1041.  
  1042.     if (bigMode != MODE_BUILD && bigMode != MODE_TARBUILD && clean) 
  1043.     argerror(_("--clean may only be used during package building"));
  1044.  
  1045.     if (bigMode != MODE_BUILD && bigMode != MODE_TARBUILD && shortCircuit) 
  1046.     argerror(_("--short-circuit may only be used during package building"));
  1047.  
  1048.     if (shortCircuit && (buildChar != 'c') && (buildChar != 'i')) {
  1049.     argerror(_("--short-circuit may only be used with -bc, -bi, -tc "
  1050.             "or -ti"));
  1051.     }
  1052.  
  1053.     if (oldPackage && !(installFlags & RPMINSTALL_UPGRADE))
  1054.     argerror(_("--oldpackage may only be used during upgrades"));
  1055.  
  1056.     if (bigMode != MODE_QUERY && dump) 
  1057.     argerror(_("--dump may only be used during queries"));
  1058.  
  1059.     if (bigMode == MODE_QUERY && dump && !(queryFor & QUERY_FOR_LIST))
  1060.     argerror(_("--dump of queries must be used with -l, -c, or -d"));
  1061.  
  1062.     if ((ftpProxy || ftpPort) && !(bigMode == MODE_INSTALL ||
  1063.     ((bigMode == MODE_QUERY && querySource == QUERY_RPM)) ||
  1064.     ((bigMode == MODE_VERIFY && querySource == VERIFY_RPM))))
  1065.     argerror(_("ftp options can only be used during package queries, "
  1066.          "installs, and upgrades"));
  1067.  
  1068.     if (oldPackage || (force && (installFlags & RPMINSTALL_UPGRADE)))
  1069.     installFlags |= RPMINSTALL_UPGRADETOOLD;
  1070.  
  1071.     if (noPgp && bigMode != MODE_CHECKSIG)
  1072.     argerror(_("--nopgp may only be used during signature checking"));
  1073.  
  1074.     if (noMd5 && bigMode != MODE_CHECKSIG && bigMode != MODE_VERIFY)
  1075.     argerror(_("--nopgp may only be used during signature checking and "
  1076.            "package verification"));
  1077.  
  1078.     if (ftpProxy) rpmSetVar(RPMVAR_FTPPROXY, ftpProxy);
  1079.     if (ftpPort) rpmSetVar(RPMVAR_FTPPORT, ftpPort);
  1080.  
  1081.     if (signIt) {
  1082.         if (bigMode == MODE_REBUILD || bigMode == MODE_BUILD ||
  1083.         bigMode == MODE_RESIGN) {
  1084.             if (poptPeekArg(optCon)) {
  1085.         switch (rpmLookupSignatureType()) {
  1086.           case RPMSIGTAG_PGP:
  1087.             if (!(passPhrase = rpmGetPassPhrase("Enter pass phrase: "))) {
  1088.             fprintf(stderr, _("Pass phrase check failed\n"));
  1089.             exit(1);
  1090.             } else {
  1091.             fprintf(stderr, _("Pass phrase is good.\n"));
  1092.             passPhrase = strdup(passPhrase);
  1093.             }
  1094.             break;
  1095.           case 0:
  1096.             break;
  1097.           default:
  1098.             fprintf(stderr, "Invalid signature spec in rc file\n");
  1099.             exit(1);
  1100.         }
  1101.         }
  1102.     } else {
  1103.         argerror(_("--sign may only be used during package building"));
  1104.     }
  1105.     } else {
  1106.         /* Override any rpmrc setting */
  1107.         rpmSetVar(RPMVAR_SIGTYPE, "none");
  1108.     }
  1109.  
  1110.     if (pipeOutput) {
  1111.     pipe(p);
  1112.  
  1113.     if (!(pipeChild = fork())) {
  1114.         close(p[1]);
  1115.         dup2(p[0], 0);
  1116.         close(p[0]);
  1117.         execl("/bin/sh", "/bin/sh", "-c", pipeOutput, NULL);
  1118.         fprintf(stderr, "exec failed\n");
  1119.     }
  1120.  
  1121.     close(p[0]);
  1122.     dup2(p[1], 1);
  1123.     close(p[1]);
  1124.     }
  1125.     
  1126.     switch (bigMode) {
  1127.       case MODE_UNKNOWN:
  1128.     if (!version && !help) printUsage();
  1129.     break;
  1130.  
  1131.       case MODE_REBUILDDB:
  1132.     ec = rpmdbRebuild(rootdir);
  1133.     break;
  1134.  
  1135.       case MODE_QUERYTAGS:
  1136.     if (argc != 2)
  1137.         argerror(_("unexpected arguments to --querytags "));
  1138.  
  1139.     queryPrintTags();
  1140.     break;
  1141.  
  1142.       case MODE_INITDB:
  1143.     rpmdbInit(rootdir, 0644);
  1144.     break;
  1145.  
  1146.       case MODE_CHECKSIG:
  1147.     if (!poptPeekArg(optCon))
  1148.         argerror(_("no packages given for signature check"));
  1149.     if (!noPgp) checksigFlags |= CHECKSIG_PGP;
  1150.     if (!noMd5) checksigFlags |= CHECKSIG_MD5;
  1151.     exit(doCheckSig(checksigFlags, poptGetArgs(optCon)));
  1152.  
  1153.       case MODE_RESIGN:
  1154.     if (!poptPeekArg(optCon))
  1155.         argerror(_("no packages given for signing"));
  1156.     exit(doReSign(addSign, passPhrase, poptGetArgs(optCon)));
  1157.     
  1158.       case MODE_REBUILD:
  1159.       case MODE_RECOMPILE:
  1160.         if (rpmGetVerbosity() == RPMMESS_NORMAL)
  1161.         rpmSetVerbosity(RPMMESS_VERBOSE);
  1162.  
  1163.     if (!poptPeekArg(optCon))
  1164.         argerror(_("no packages files given for rebuild"));
  1165.  
  1166.     buildAmount = RPMBUILD_PREP | RPMBUILD_BUILD | RPMBUILD_INSTALL |
  1167.         RPMBUILD_SWEEP | RPMBUILD_RMSOURCE;
  1168.     if (bigMode == MODE_REBUILD) {
  1169.         buildAmount |= RPMBUILD_BINARY;
  1170.     }
  1171.  
  1172.     while ((pkg = poptGetArg(optCon))) {
  1173.         if (doSourceInstall("/", pkg, &specFile))
  1174.         exit(1);
  1175.  
  1176.         if (build(specFile, buildAmount, passPhrase, buildRootOverride,
  1177.             0)) {
  1178.         exit(1);
  1179.         }
  1180.     }
  1181.     break;
  1182.  
  1183.       case MODE_BUILD:
  1184.       case MODE_TARBUILD:
  1185.         if (rpmGetVerbosity() == RPMMESS_NORMAL)
  1186.         rpmSetVerbosity(RPMMESS_VERBOSE);
  1187.        
  1188.     switch (buildChar) {
  1189.       /* these fallthroughs are intentional */
  1190.       case 'a':
  1191.         buildAmount |= RPMBUILD_SOURCE;
  1192.       case 'b':
  1193.         buildAmount |= RPMBUILD_BINARY;
  1194.       case 'i':
  1195.         buildAmount |= RPMBUILD_INSTALL;
  1196.         if ((buildChar == 'i') && shortCircuit)
  1197.         break;
  1198.       case 'c':
  1199.         buildAmount |= RPMBUILD_BUILD;
  1200.         if ((buildChar == 'c') && shortCircuit)
  1201.         break;
  1202.       case 'p':
  1203.         buildAmount |= RPMBUILD_PREP;
  1204.         break;
  1205.         
  1206.       case 'l':
  1207.         buildAmount |= RPMBUILD_LIST;
  1208.         break;
  1209.     }
  1210.  
  1211.     if (clean)
  1212.         buildAmount |= RPMBUILD_SWEEP;
  1213.  
  1214.     if (test)
  1215.         buildAmount |= RPMBUILD_TEST;
  1216.  
  1217.     if (!poptPeekArg(optCon))
  1218.         if (bigMode == MODE_BUILD)
  1219.         argerror(_("no spec files given for build"));
  1220.         else
  1221.         argerror(_("no tar files given for build"));
  1222.  
  1223.     while ((pkg = poptGetArg(optCon)))
  1224.         if (build(pkg, buildAmount, passPhrase, buildRootOverride,
  1225.             bigMode == MODE_TARBUILD)) {
  1226.         exit(1);
  1227.         }
  1228.     break;
  1229.  
  1230.       case MODE_UNINSTALL:
  1231.     if (!poptPeekArg(optCon))
  1232.         argerror(_("no packages given for uninstall"));
  1233.  
  1234.     if (noScripts) uninstallFlags |= RPMUNINSTALL_NOSCRIPTS;
  1235.     if (test) uninstallFlags |= RPMUNINSTALL_TEST;
  1236.     if (justdb) uninstallFlags |= RPMUNINSTALL_JUSTDB;
  1237.     if (noDeps) interfaceFlags |= UNINSTALL_NODEPS;
  1238.     if (allMatches) interfaceFlags |= UNINSTALL_ALLMATCHES;
  1239.  
  1240.     ec = doUninstall(rootdir, poptGetArgs(optCon), uninstallFlags, 
  1241.         interfaceFlags);
  1242.     break;
  1243.  
  1244.       case MODE_INSTALL:
  1245.     if (force)
  1246.         installFlags |= (RPMINSTALL_REPLACEPKG | RPMINSTALL_REPLACEFILES);
  1247.     if (replaceFiles) installFlags |= RPMINSTALL_REPLACEFILES;
  1248.     if (replacePackages) installFlags |= RPMINSTALL_REPLACEPKG;
  1249.     if (test) installFlags |= RPMINSTALL_TEST;
  1250.     if (noScripts) installFlags |= RPMINSTALL_NOSCRIPTS;
  1251.     if (ignoreArch) installFlags |= RPMINSTALL_NOARCH;
  1252.     if (ignoreOs) installFlags |= RPMINSTALL_NOOS;
  1253.     if (allFiles) installFlags |= RPMINSTALL_ALLFILES;
  1254.     if (justdb) installFlags |= RPMINSTALL_JUSTDB;
  1255.  
  1256.     if (showPercents) interfaceFlags |= INSTALL_PERCENT;
  1257.     if (showHash) interfaceFlags |= INSTALL_HASH;
  1258.     if (noDeps) interfaceFlags |= INSTALL_NODEPS;
  1259.     if (noOrder) interfaceFlags |= INSTALL_NOORDER;
  1260.  
  1261.     if (!incldocs) {
  1262.         if (excldocs)
  1263.         installFlags |= RPMINSTALL_NODOCS;
  1264.         else if (rpmGetBooleanVar(RPMVAR_EXCLUDEDOCS))
  1265.         installFlags |= RPMINSTALL_NODOCS;
  1266.     }
  1267.  
  1268.     if (!poptPeekArg(optCon))
  1269.         argerror(_("no packages given for install"));
  1270.  
  1271.     ec += doInstall(rootdir, poptGetArgs(optCon), prefix, installFlags, 
  1272.             interfaceFlags);
  1273.     break;
  1274.  
  1275.       case MODE_QUERY:
  1276.     if (dump) queryFor |= QUERY_FOR_DUMPFILES;
  1277.  
  1278.     if (querySource == QUERY_ALL) {
  1279.         if (poptPeekArg(optCon))
  1280.         argerror(_("extra arguments given for query of all packages"));
  1281.  
  1282.         ec = doQuery(rootdir, QUERY_ALL, queryFor, NULL, queryFormat);
  1283.     } else {
  1284.         if (!poptPeekArg(optCon))
  1285.         argerror(_("no arguments given for query"));
  1286.         while ((pkg = poptGetArg(optCon)))
  1287.         ec = doQuery(rootdir, querySource, queryFor, pkg, queryFormat);
  1288.     }
  1289.     break;
  1290.  
  1291.       case MODE_VERIFY:
  1292.     verifyFlags = 0;
  1293.     if (!noFiles) verifyFlags |= VERIFY_FILES;
  1294.     if (!noDeps) verifyFlags |= VERIFY_DEPS;
  1295.     if (!noScripts) verifyFlags |= VERIFY_SCRIPT;
  1296.     if (!noMd5) verifyFlags |= VERIFY_MD5;
  1297.  
  1298.     if (verifySource == VERIFY_EVERY) {
  1299.         doVerify(rootdir, VERIFY_EVERY, NULL, verifyFlags);
  1300.     } else {
  1301.         if (!poptPeekArg(optCon))
  1302.         argerror(_("no arguments given for verify"));
  1303.         doVerify(rootdir, verifySource, poptGetArgs(optCon), verifyFlags);
  1304.     }
  1305.     break;
  1306.     }
  1307.  
  1308.     poptFreeContext(optCon);
  1309.  
  1310.     if (pipeChild) {
  1311.     fclose(stdout);
  1312.     waitpid(pipeChild, &status, 0);
  1313.     }
  1314.  
  1315.     /* keeps memory leak checkers quiet */
  1316.     if (queryFormat) free(queryFormat);
  1317.  
  1318.     return ec;
  1319. }
  1320.