rpm-razor.c
author Kristian H?gsberg <krh@redhat.com>
Wed Jun 04 15:04:45 2008 -0400 (2008-06-04)
changeset 217 bf5d87af3e1e
parent 216 b97c130460a5
child 218 9aed412ed2b8
permissions -rw-r--r--
Implement the rpm --package option.
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; either version 2 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along
    16  * with this program; if not, write to the Free Software Foundation, Inc.,
    17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  */
    19 
    20 #include <stdlib.h>
    21 #include <string.h>
    22 #include <stdio.h>
    23 #include "razor.h"
    24 
    25 enum option_type {
    26 	OPTION_LAST,
    27 	OPTION_GROUP,
    28 	OPTION_BOOL,
    29 	OPTION_STRING
    30 };
    31 
    32 struct option {
    33 	enum option_type type;
    34 	const char *name;
    35 	char short_name;
    36 	const char *arg_name;
    37 	const char *description;
    38 	void *data;
    39 };
    40 
    41 static int option_all, option_whatrequires, option_whatprovides;
    42 static int option_package;
    43 
    44 static const struct option query_options[] = {
    45 	{ OPTION_BOOL, "configfiles", 'c', NULL, "list all configuration files", NULL },
    46 	{ OPTION_BOOL, "docfiles", 'd', NULL, "list all documentation files", NULL },
    47 	{ OPTION_BOOL, "dump", 0, NULL, "dump basic file information", NULL },
    48 	{ OPTION_BOOL, "list", 0, NULL, "list files in package", NULL },
    49 	{ OPTION_STRING, "queryformat", 0, "QUERYFORMAT", "use the following query format", NULL },
    50 	{ OPTION_BOOL, "state", 's', NULL, "display the states of the listed files", NULL },
    51 	{ OPTION_BOOL, "all", 'a', NULL, "query/verify all packages", &option_all },
    52 	{ OPTION_BOOL, "file", 'f', NULL, "query/verify package(s) owning file", NULL },
    53 	{ OPTION_BOOL, "group", 'g', NULL, "query/verify package(s) in group", NULL },
    54 	{ OPTION_BOOL, "package", 'p', NULL, "query/verify a package file", &option_package },
    55 	{ OPTION_BOOL, "ftswalk", 'W', NULL, "query/verify package(s) from TOP file tree walk", NULL },
    56 	{ OPTION_BOOL, "pkgid", 0, NULL, "query/verify package(s) with package identifier", NULL },
    57 	{ OPTION_BOOL, "hdrid", 0, NULL, "query/verify package(s) with header identifier", NULL },
    58 	{ OPTION_BOOL, "fileid", 0, NULL, "query/verify package(s) with file identifier", NULL },
    59 	{ OPTION_BOOL, "specfile", 0, NULL, "query a spec file", NULL, },
    60 	{ OPTION_BOOL, "triggeredby", 0, NULL, "query the package(s) triggered by the package", NULL },
    61 	{ OPTION_BOOL, "whatrequires", 0, NULL, "query/verify the package(s) which require a dependency", &option_whatrequires },
    62 	{ OPTION_BOOL, "whatprovides", 0, NULL, "query/verify the package(s) which provide a dependency", &option_whatprovides },
    63 	{ OPTION_BOOL, "nomanifest", 0, NULL, "do not process non-package files as manifests", NULL },
    64 	{ }
    65 };
    66 
    67 static const struct option verify_options[] = {
    68 	{ OPTION_BOOL, "nomd5", 0, NULL, "don't verify MD5 digest of files", NULL },
    69 	{ OPTION_BOOL, "nofiles", 0, NULL, "don't verify files in package", NULL },
    70 	{ OPTION_BOOL, "nodeps", 0, NULL, "don't verify package dependencies", NULL },
    71 	{ OPTION_BOOL, "noscript", 0, NULL, "don't execute verify script(s)", NULL, },
    72 	{ OPTION_BOOL, "all", 'a', NULL, "query/verify all packages", NULL },
    73 	{ OPTION_BOOL, "file", 'f', NULL, "query/verify package(s) owning file", NULL },
    74 	{ OPTION_BOOL, "group", 'g', NULL, "query/verify package(s) in group", NULL },
    75 	{ OPTION_BOOL, "package", 'p', NULL, "query/verify a package file", NULL },
    76 	{ OPTION_BOOL, "ftswalk", 'W', NULL, "query/verify package(s) from TOP file tree walk", NULL },
    77 	{ OPTION_BOOL, "pkgid", 0, NULL, "query/verify package(s) with package identifier", NULL },
    78 	{ OPTION_BOOL, "hdrid", 0, NULL, "query/verify package(s) with header identifier", NULL },
    79 	{ OPTION_BOOL, "fileid", 0, NULL, "query/verify package(s) with file identifier", NULL },
    80 	{ OPTION_BOOL, "specfile", 0, NULL, "query a spec file", NULL },
    81 	{ OPTION_BOOL, "triggeredby", 0, NULL, "query the package(s) triggered by the package", NULL },
    82 	{ OPTION_BOOL, "whatrequires", 0, NULL, "query/verify the package(s) which require a dependency", &option_whatrequires },
    83 	{ OPTION_BOOL, "whatprovides", 0, NULL, "query/verify the package(s) which provide a dependency", &option_whatprovides },
    84 	{ OPTION_BOOL, "nomanifest", 0, NULL, "do not process non-package files as manifests", NULL },
    85 	{ }
    86 };
    87 
    88 static const struct option ftw_options[] = {
    89 	{ OPTION_BOOL, "comfollow", 0, NULL, "FTS_COMFOLLOW: follow command line symlinks", NULL },
    90 	{ OPTION_BOOL, "logical", 0, NULL, "FTS_LOGICAL: logical walk", NULL },
    91 	{ OPTION_BOOL, "nochdir", 0, NULL, "FTS_NOCHDIR: don't change directories", NULL },
    92 	{ OPTION_BOOL, "nostat", 0, NULL, "FTS_NOSTAT: don't get stat info", NULL },
    93 	{ OPTION_BOOL, "physical", 0, NULL, "FTS_PHYSICAL: physical walk", NULL },
    94 	{ OPTION_BOOL, "seedot", 0, NULL, "FTS_SEEDOT: return dot and dot-dot", NULL },
    95 	{ OPTION_BOOL, "xdev", 0, NULL, "FTS_XDEV: don't cross devices", NULL },
    96 	{ OPTION_BOOL, "whiteout", 0, NULL, "FTS_WHITEOUT: return whiteout information", NULL },
    97 	{ }
    98 };
    99 
   100 static const struct option signature_options[] = {
   101 	{ OPTION_BOOL, "addsign", 0, NULL, "sign package(s) (identical to --resign)", NULL, },
   102 	{ OPTION_BOOL, "checksig", 'K', NULL, "verify package signature(s)", NULL, },
   103 	{ OPTION_BOOL, "delsign", 0, NULL, "delete package signatures", NULL, },
   104 	{ OPTION_BOOL, "import", 0, NULL, "import an armored public key", NULL, },
   105 	{ OPTION_BOOL, "resign", 0, NULL, "sign package(s) (identical to --addsign)", NULL, },
   106 	{ OPTION_BOOL, "nodigest", 0, NULL, "don't verify package digest(s)", NULL, },
   107 	{ OPTION_BOOL, "nosignature", 0, NULL, "don't verify package signature(s)", NULL },
   108 	{ }
   109 };
   110 
   111 static const struct option database_options[] = {
   112 	{ OPTION_BOOL, "initdb", 0, NULL, "initialize database", NULL },
   113 	{ OPTION_BOOL, "rebuilddb", 0, NULL, "rebuild database inverted lists from installed package headers", NULL },
   114 	{ }
   115 };
   116 
   117 static int option_erase, option_install, option_upgrade;
   118 
   119 static const struct option install_options[] = {
   120 	{ OPTION_BOOL, "aid", 0, NULL, "add suggested packages to transaction", NULL, },
   121 	{ OPTION_BOOL, "allfiles", 0, NULL, "install all files, even configurations which might otherwise be skipped", NULL, },
   122 	{ OPTION_BOOL, "allmatches", 0, NULL, "remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)", NULL, },
   123 	{ OPTION_BOOL, "badreloc", 0, NULL, "relocate files in non-relocatable package", NULL },
   124 	{ OPTION_BOOL, "erase", 'e', "<package>", "erase (uninstall) package", &option_erase },
   125 	{ OPTION_BOOL, "excludedocs", 0, NULL, "do not install documentation", NULL, },
   126 	{ OPTION_BOOL, "excludepath", 0, "<path>", "skip files with leading component <path> ", NULL, },
   127 	{ OPTION_BOOL, "fileconflicts", 0, NULL, "detect file conflicts between packages", NULL, },
   128 	{ OPTION_BOOL, "force", 0, NULL, "short hand for --replacepkgs --replacefiles", NULL },
   129 	{ OPTION_BOOL, "freshen", 'F', "<packagefile>+", "upgrade package(s) if already installed", NULL },
   130 	{ OPTION_BOOL, "hash", 'h', NULL, "print hash marks as package installs (good with -v)", NULL },
   131 	{ OPTION_BOOL, "ignorearch", 0, NULL, "don't verify package architecture", NULL, },
   132 	{ OPTION_BOOL, "ignoreos", 0, NULL, "don't verify package operating system", NULL, },
   133 	{ OPTION_BOOL, "ignoresize", 0, NULL, "don't check disk space before installing", NULL },
   134 	{ OPTION_BOOL, "install", 'i', NULL, "install package(s)", &option_install },
   135 	{ OPTION_BOOL, "justdb", 0, NULL, "update the database, but do not modify the filesystem", NULL, },
   136 	{ OPTION_BOOL, "nodeps", 0, NULL, "do not verify package dependencies", NULL, },
   137 	{ OPTION_BOOL, "nomd5", 0, NULL, "don't verify MD5 digest of files", NULL, },
   138 	{ OPTION_BOOL, "nocontexts", 0, NULL, "don't install file security contexts", NULL, },
   139 	{ OPTION_BOOL, "noorder", 0, NULL, "do not reorder package installation to satisfy dependencies", NULL, },
   140 	{ OPTION_BOOL, "nosuggest", 0, NULL, "do not suggest missing dependency resolution(s)", NULL, },
   141 	{ OPTION_BOOL, "noscripts", 0, NULL, "do not execute package scriptlet(s)", NULL, },
   142 	{ OPTION_BOOL, "notriggers", 0, NULL, "do not execute any scriptlet(s) triggered by this package", NULL, },
   143 	{ OPTION_BOOL, "oldpackage", 0, NULL, "upgrade to an old version of the package (--force on upgrades does this automatically)", NULL },
   144 	{ OPTION_BOOL, "percent", 0, NULL, "print percentages as package installs", NULL, },
   145 	{ OPTION_STRING, "prefix", 0, "<dir>", "relocate the package to <dir>, if relocatable", NULL, },
   146 	{ OPTION_STRING, "relocate", 0, "<old>=<new>", "relocate files from path <old> to <new>", NULL, },
   147 	{ OPTION_BOOL, "repackage", 0, NULL, "save erased package files by repackaging", NULL, },
   148 	{ OPTION_BOOL, "replacefiles", 0, NULL, "ignore file conflicts between packages", NULL, },
   149 	{ OPTION_BOOL, "replacepkgs", 0, NULL, "reinstall if the package is already present", NULL, },
   150 	{ OPTION_BOOL, "test", 0, NULL, "don't install, but tell if it would work or not", NULL },
   151 	{ OPTION_BOOL, "upgrade", 'U', "<packagefile>+", "upgrade package(s)", &option_upgrade },
   152 	{ }
   153 };
   154 
   155 static int option_version;
   156 
   157 static const struct option common_options[] = {
   158 	{ OPTION_STRING, "define", 'D', "MACRO EXPR", "define MACRO with value EXPR", NULL, },
   159 	{ OPTION_STRING, "eval", 'E', "EXPR", "print macro expansion of EXPR", NULL },
   160 	{ OPTION_STRING, "macros", 0, "<FILE:...>", "read <FILE:...> instead of default file(s)", NULL },
   161 	{ OPTION_BOOL, "nodigest", 0, NULL, "don't verify package digest(s)", NULL, },
   162 	{ OPTION_BOOL, "nosignature", 0, NULL, "don't verify package signature(s)", NULL, },
   163 	{ OPTION_STRING, "rcfile", 0, "<FILE:...>", "read <FILE:...> instead of default file(s)", NULL },
   164 	{ OPTION_STRING, "root", 'r', "ROOT", "use ROOT as top level directory (default: \"/\")", NULL },
   165 	{ OPTION_BOOL, "querytags", 0, NULL, "display known query tags", NULL, },
   166 	{ OPTION_BOOL, "showrc", 0, NULL, "display final rpmrc and macro configuration", NULL, },
   167 	{ OPTION_BOOL, "quiet", 0, NULL, "provide less detailed output", NULL },
   168 	{ OPTION_BOOL, "verbose", 'v', NULL, "provide more detailed output", NULL },
   169 	{ OPTION_BOOL, "version", 0, NULL, "print the version of rpm being used", &option_version },
   170 	{ }
   171 };
   172 
   173 static int option_conflicts, option_obsoletes, option_requires, option_provides;
   174 
   175 static const struct option alias_options[] = {
   176 	{ OPTION_BOOL, "scripts", 0, NULL, "list install/erase scriptlets from package(s)", NULL, },
   177 	{ OPTION_BOOL, "setperms", 0, NULL, "set permissions of files in a package", NULL, },
   178 	{ OPTION_BOOL, "setugids", 0, NULL, "set user/group ownership of files in a package", NULL, },
   179 	{ OPTION_BOOL, "conflicts", 0, NULL, "list capabilities this package conflicts with", &option_conflicts, },
   180 	{ OPTION_BOOL, "obsoletes", 0, NULL, "list other packages removed by installing this package", &option_obsoletes, },
   181 	{ OPTION_BOOL, "provides", 0, NULL, "list capabilities that this package provides", &option_provides, },
   182 	{ OPTION_BOOL, "requires", 0, NULL, "list capabilities required by package(s)", &option_requires, },
   183 	{ OPTION_BOOL, "info", 0, NULL, "list descriptive information from package(s)", NULL, },
   184 	{ OPTION_BOOL, "changelog", 0, NULL, "list change logs for this package", NULL, },
   185 	{ OPTION_BOOL, "xml", 0, NULL, "list metadata in xml", NULL, },
   186 	{ OPTION_BOOL, "triggers", 0, NULL, "list trigger scriptlets from package(s)", NULL, },
   187 	{ OPTION_BOOL, "last", 0, NULL, "list package(s) by install time, most recent first", NULL, },
   188 	{ OPTION_BOOL, "dupes", 0, NULL, "list duplicated packages", NULL, },
   189 	{ OPTION_BOOL, "filesbypkg", 0, NULL, "list all files from each package", NULL, },
   190 	{ OPTION_BOOL, "fileclass", 0, NULL, "list file names with classes", NULL, },
   191 	{ OPTION_BOOL, "filecolor", 0, NULL, "list file names with colors", NULL, },
   192 	{ OPTION_BOOL, "filecontext", 0, NULL, "list file names with security context from header", NULL, },
   193 	{ OPTION_BOOL, "fscontext", 0, NULL, "list file names with security context from file system", NULL, },
   194 	{ OPTION_BOOL, "recontext", 0, NULL, "list file names with security context from policy RE", NULL, },
   195 	{ OPTION_BOOL, "fileprovide", 0, NULL, "list file names with provides", NULL, },
   196 	{ OPTION_BOOL, "filerequire", 0, NULL, "list file names with requires", NULL, },
   197 	{ OPTION_BOOL, "redhatprovides", 0, NULL, "find package name that contains a provided capability (needs rpmdb-redhat package installed)", NULL, },
   198 	{ OPTION_BOOL, "redhatrequires", 0, NULL, "find package name that contains a required capability (needs rpmdb-redhat package installed)", NULL, },
   199 	{ OPTION_STRING, "buildpolicy", 0, "<policy>", "set buildroot <policy> (e.g. compress man pages)", NULL, },
   200 	{ OPTION_BOOL, "with", 0, "<option>", "enable configure <option> for build", NULL, },
   201 	{ OPTION_BOOL, "without", 0, "<option>", "disable configure <option> for build", NULL },
   202 	{ }
   203 };
   204 
   205 static int option_help, option_usage;
   206 
   207 static const struct option help_options[] = {
   208 	{ OPTION_BOOL, "help", '?', NULL, "Show this help message", &option_help },
   209 	{ OPTION_BOOL, "usage", 0, NULL, "Display brief usage message", &option_usage},
   210 	{ }
   211 };
   212 
   213 static int option_query, option_verify;
   214 
   215 static const struct option rpm_options[] = {
   216 	{ OPTION_BOOL, "query", 'q', NULL, "Query rpm database", &option_query },
   217 	{ OPTION_BOOL, "verify", 'V', NULL, "Verify rpm database", &option_verify },
   218 	{ OPTION_GROUP, NULL, 0, NULL, "Query options (with -q or --query):", &query_options },
   219 	{ OPTION_GROUP, NULL, 0, NULL, "Verify options (with -V or --verify):", &verify_options },
   220 	{ OPTION_GROUP, NULL, 0, NULL, "File tree walk options (with --ftswalk):", &ftw_options },
   221 	{ OPTION_GROUP, NULL, 0, NULL, "Signature options:", &signature_options },
   222 	{ OPTION_GROUP, NULL, 0, NULL, "Database options:", &database_options },
   223 	{ OPTION_GROUP, NULL, 0, NULL, "Install/Upgrade/Erase options:", &install_options },
   224 	{ OPTION_GROUP, NULL, 0, NULL, "Common options for all rpm modes and executables:", &common_options },
   225 	{ OPTION_GROUP, NULL, 0, NULL, "Options implemented via popt alias/exec:", &alias_options },
   226 	{ OPTION_GROUP, NULL, 0, NULL, "Help options", &help_options },
   227 	{ }
   228 };
   229 
   230 static const char system_repo_filename[] = "system.repo";
   231 static const char *repo_filename = system_repo_filename;
   232 
   233 static struct razor_property *
   234 add_property_packages(struct razor_set *set, 
   235 		      struct razor_package_query *query,
   236 		      const char *ref_name,
   237 		      const char *ref_version,
   238 		      enum razor_property_type ref_type)
   239 {
   240 	struct razor_property *property;
   241 	struct razor_property_iterator *pi;
   242 	struct razor_package_iterator *pkgi;
   243 	const char *name, *version;
   244 	enum razor_property_type type;
   245 	enum razor_version_relation relation;
   246 
   247 	pi = razor_property_iterator_create(set, NULL);
   248 	while (razor_property_iterator_next(pi, &property, &name,
   249 					    &relation, &version, &type)) {
   250 		if (strcmp(ref_name, name) != 0)
   251 			continue;
   252 		if (ref_version && relation == RAZOR_VERSION_EQUAL &&
   253 		    strcmp(ref_version, version) != 0)
   254 			continue;
   255 		if (ref_type != type)
   256 			continue;
   257 
   258 		pkgi = razor_package_iterator_create_for_property(set,
   259 								  property);
   260 		razor_package_query_add_iterator(query, pkgi);
   261 		razor_package_iterator_destroy(pkgi);
   262 	}
   263 	razor_property_iterator_destroy(pi);
   264 
   265 	return property;
   266 }
   267 
   268 static int
   269 strcmpp(const void *p1, const void *p2)
   270 {
   271 	return strcmp(*(char * const *) p1, *(char * const *) p2);
   272 }
   273 
   274 static void
   275 add_command_line_packages(struct razor_set *set,
   276 			  struct razor_package_query *query,
   277 			  int argc, const char **argv)
   278 {
   279 	struct razor_package *package;
   280 	struct razor_package_iterator *pi;
   281 	const char *name, *version, *arch;
   282 	int i, cmp;
   283 
   284 	qsort(argv, argc, sizeof(*argv), strcmpp);
   285 	i = 0;
   286 
   287 	pi = razor_package_iterator_create(set);
   288 
   289 	while (razor_package_iterator_next(pi, &package,
   290 					   &name, &version, &arch)) {
   291 		while (cmp = strcmp(argv[i], name), cmp < 0 && i < argc) {
   292 			printf("package %s is not installed\n", argv[i]);
   293 			i++;
   294 		}
   295 
   296 		if (cmp == 0) {
   297 			razor_package_query_add_package(query, package);
   298 			i++;
   299 		}
   300 	}
   301 
   302 	razor_package_iterator_destroy(pi);
   303 }
   304 
   305 static void
   306 print_package_properties(struct razor_set *set,
   307 			 struct razor_package *package,
   308 			 enum razor_property_type ref_type)
   309 {
   310 	struct razor_property *property;
   311 	struct razor_property_iterator *pi;
   312 	const char *name, *version;
   313 	enum razor_property_type type;
   314 	enum razor_version_relation relation;
   315 
   316 	pi = razor_property_iterator_create(set, package);
   317 	while (razor_property_iterator_next(pi, &property,
   318 					    &name, &relation, &version,
   319 					    &type)) {
   320 		if (type != ref_type)
   321 			continue;
   322 		if (version[0] == '\0')
   323 			printf("%s\n", name);
   324 		else
   325 			printf("%s %s %s\n", name,
   326 			       razor_version_relations[relation], version);
   327 	}
   328 	razor_property_iterator_destroy(pi);
   329 }
   330 
   331 static struct razor_set *
   332 create_set_from_command_line(int argc, const char *argv[])
   333 {
   334 	struct razor_importer *importer;
   335 	struct razor_rpm *rpm;
   336 	int i;
   337 
   338 	importer = razor_importer_new();
   339 
   340 	for (i = 0; i < argc; i++) {
   341 		rpm = razor_rpm_open(argv[i]);
   342 		if (rpm == NULL)
   343 			continue;
   344 		if (razor_importer_add_rpm(importer, rpm))
   345 			printf("couldn't import %s\n", argv[i]);
   346 
   347 		razor_rpm_close(rpm);
   348 	}
   349 
   350 	return razor_importer_finish(importer);
   351 }
   352 
   353 static void
   354 command_query(int argc, const char *argv[])
   355 {
   356 	struct razor_set *set;
   357 	struct razor_package_iterator *pi;
   358 	struct razor_package *package;
   359 	struct razor_package_query *query;
   360 	const char *name, *version, *arch;
   361 
   362 	if (option_package) {
   363 		set = create_set_from_command_line(argc, argv);
   364 		argc = 0;
   365 		option_all = 1;
   366 	} else {
   367 		set = razor_set_open(repo_filename);
   368 	}
   369 
   370 	if (option_all + option_whatprovides + option_whatrequires > 1) {
   371 		printf("only one type of query/verify "
   372 		       "may be performed at a time\n");
   373 		exit(1);
   374 	}
   375 
   376 	query = razor_package_query_create(set);
   377 	if (option_all) {
   378 		pi = razor_package_iterator_create(set);
   379 		razor_package_query_add_iterator(query, pi);
   380 		razor_package_iterator_destroy(pi);
   381 	} else if (option_whatrequires) {
   382 		add_property_packages(set, query,
   383 				      argv[0], NULL, RAZOR_PROPERTY_REQUIRES);
   384 	} else if (option_whatprovides) {
   385 		add_property_packages(set, query,
   386 				      argv[0], NULL, RAZOR_PROPERTY_PROVIDES);
   387 	} else if (argc > 0) {
   388 		add_command_line_packages(set, query, argc, argv);
   389 	} else {
   390 		printf("no arguments given for query\n");
   391 		exit(1);
   392 	}
   393 
   394 	pi = razor_package_query_finish(query);
   395 
   396 	while (razor_package_iterator_next(pi, &package,
   397 					   &name, &version, &arch)) {
   398 		if (option_conflicts)
   399 			print_package_properties(set, package,
   400 						 RAZOR_PROPERTY_CONFLICTS);
   401 		if (option_obsoletes)
   402 			print_package_properties(set, package,
   403 						 RAZOR_PROPERTY_OBSOLETES);
   404 		if (option_requires)
   405 			print_package_properties(set, package,
   406 						 RAZOR_PROPERTY_REQUIRES);
   407 		if (option_provides)
   408 			print_package_properties(set, package,
   409 						 RAZOR_PROPERTY_PROVIDES);
   410 		if (!option_conflicts && !option_obsoletes &&
   411 		    !option_requires && !option_provides)
   412 			printf("%s-%s.%s\n", name, version, arch);
   413 	}
   414 
   415 	razor_package_iterator_destroy(pi);
   416 
   417 	razor_set_destroy(set);
   418 
   419 	return;
   420 }
   421 
   422 static void
   423 command_verify(int argc, const char *argv[])
   424 {
   425 	if (argc == 0) {
   426 		printf("no arguments given for verify\n");
   427 		exit(1);
   428 	}
   429 
   430 	printf("command verify - not implemented\n");
   431 }
   432 
   433 static void
   434 command_erase(int argc, const char *argv[])
   435 {
   436 	if (argc == 0) {
   437 		printf("no packages given for erase\n");
   438 		exit(1);
   439 	}
   440 
   441 	printf("command erase - not implemented\n");
   442 }
   443 
   444 static void
   445 command_install(int argc, const char *argv[])
   446 {
   447 	if (argc == 0) {
   448 		printf("no packages given for install\n");
   449 		exit(1);
   450 	}
   451 
   452 	printf("command install - not implemented\n");
   453 }
   454 
   455 static void
   456 command_update(int argc, const char *argv[])
   457 {
   458 	if (argc == 0) {
   459 		printf("no packages given for update\n");
   460 		exit(1);
   461 	}
   462 
   463 	printf("command update - not implemented\n");
   464 }
   465 
   466 static const struct option *
   467 find_option(const struct option *options, const char *s)
   468 {
   469 	const struct option *o;
   470 	int i;
   471 
   472 	for (i = 0; options[i].type != OPTION_LAST; i++) {
   473 		switch (options[i].type) {
   474 		case OPTION_GROUP:
   475 			o = find_option(options[i].data, s);
   476 			if (o != NULL)
   477 				return o;
   478 			break;
   479 
   480 		case OPTION_BOOL:
   481 		case OPTION_STRING:
   482 			if (s[0] == '-' &&
   483 			    s[1] == options[i].short_name && s[2] == '\0')
   484 				return &options[i];
   485 			if (s[0] == '-' && s[1] == '-' &&
   486 			    strcmp(options[i].name, s + 2) == 0)
   487 				return &options[i];
   488 			break;
   489 
   490 		case OPTION_LAST:
   491 			break;
   492 		}
   493 	}
   494 
   495 	return NULL;
   496 }
   497 
   498 static int
   499 parse_options(const struct option *options, int argc, const char **argv)
   500 {
   501 	const struct option *o;
   502 	int i, j;
   503 
   504 	/* FIXME: Bundling... rpm -Uvh must work :) */
   505 
   506 	for (i = 1, j = 0; i < argc; i++) {
   507 		if (argv[i][0] != '-') {
   508 			argv[j++] = argv[i];
   509 			continue;
   510 		}
   511 		o = find_option(options, argv[i]);
   512 		if (o == NULL) {
   513 			printf("unknown option: \"%s\"\n", argv[i]);
   514 			continue;
   515 		}
   516 		if (o->data == NULL) {
   517 			printf("option \"%s\" not supported\n", argv[i]);
   518 			continue;
   519 		}
   520 		switch (o->type) {
   521 		case OPTION_BOOL:
   522 			*(int *) o->data = 1;
   523 			break;
   524 
   525 		case OPTION_STRING:
   526 			*(const char **) o->data =
   527 				argv[i] + strlen(o->name) + 3;
   528 			break;
   529 
   530 		case OPTION_LAST:
   531 		case OPTION_GROUP:
   532 			/* Shouldn't happen. */
   533 			break;
   534 		}
   535 	}
   536 
   537 	return j;
   538 }
   539 
   540 static void
   541 print_options_help(const struct option *options)
   542 {
   543 	int i;
   544 
   545 	for (i = 0; options[i].type != OPTION_LAST; i++) {
   546 		switch (options[i].type) {
   547 		case OPTION_GROUP:
   548 			printf("%s\n", options[i].description);
   549 			print_options_help(options[i].data);
   550 			printf("\n");
   551 			break;
   552 
   553 		case OPTION_BOOL:
   554 		case OPTION_STRING:
   555 			printf("  ");
   556 			if (options[i].short_name)
   557 				printf("-%c", options[i].short_name);
   558 			if (options[i].short_name && options[i].name)
   559 				printf(", ");
   560 			if (options[i].name)
   561 				printf("--%s", options[i].name);
   562 			if (options[i].arg_name)
   563 				printf("=%s", options[i].arg_name);
   564 			if (options[i].description)
   565 				printf("\t\t%s", options[i].description);
   566 			printf("\n");
   567 			break;
   568 
   569 		case OPTION_LAST:
   570 			break;
   571 		}
   572 	}
   573 }
   574 
   575 static void
   576 print_options_usage(const struct option *options)
   577 {
   578 	int i;
   579 
   580 	for (i = 0; options[i].type != OPTION_LAST; i++) {
   581 		switch (options[i].type) {
   582 		case OPTION_GROUP:
   583 			print_options_usage(options[i].data);
   584 			break;
   585 
   586 		case OPTION_BOOL:
   587 			printf("[");
   588 			if (options[i].short_name)
   589 				printf("-%c", options[i].short_name);
   590 			if (options[i].short_name && options[i].name)
   591 				printf("|");
   592 			if (options[i].name)
   593 				printf("--%s", options[i].name);
   594 			printf("] ");
   595 			break;
   596 
   597 		case OPTION_STRING:
   598 			printf("[");
   599 			if (options[i].short_name)
   600 				printf("-%c", options[i].short_name);
   601 			if (options[i].short_name && options[i].name)
   602 				printf("|");
   603 			if (options[i].name)
   604 				printf("--%s", options[i].name);
   605 			if (options[i].arg_name)
   606 				printf("=%s", options[i].arg_name);
   607 			printf("] ");
   608 			break;
   609 
   610 
   611 			break;
   612 
   613 		case OPTION_LAST:
   614 			break;
   615 		}
   616 	}
   617 }
   618 
   619 int
   620 main(int argc, const char *argv[])
   621 {
   622 	argc = parse_options(rpm_options, argc, argv);
   623 
   624 	if (option_version) {
   625 		printf("razor rpm version hoopla.\n");
   626 		exit(0);
   627 	}
   628 
   629 	if (option_help) {
   630 		printf("Usage: rpm [OPTION...]\n");
   631 		print_options_help(rpm_options);
   632 		exit(0);
   633 	}
   634 
   635 	if (option_usage) {
   636 		printf("Usage: rpm [OPTION...]\n");
   637 		print_options_usage(rpm_options);
   638 		printf("\n");
   639 		exit(0);
   640 	}
   641 
   642 	if (option_query + option_verify +
   643 	    option_erase + option_install + option_upgrade > 1) {
   644 		printf("specify only one of query, verify, erase, install "
   645 		       "or upgrade\n");
   646 		exit(1);
   647 	}
   648 	    
   649 	if (option_query) {
   650 		command_query(argc, argv);
   651 	} else if (option_verify) {
   652 		command_verify(argc, argv);
   653 	} else if (option_erase) {
   654 		command_erase(argc, argv);
   655 	} else if (option_install) {
   656 		command_install(argc, argv);
   657 	} else if (option_upgrade) {
   658 		command_update(argc, argv);
   659 	} else {
   660 		print_options_usage(rpm_options);
   661 		printf("\n");
   662 		exit(0);
   663 	}
   664 
   665 	return 0;
   666 }