rpm-razor.c
author Kristian H?gsberg <krh@redhat.com>
Tue Jun 03 15:21:22 2008 -0400 (2008-06-03)
changeset 215 85381bba83df
child 216 b97c130460a5
permissions -rw-r--r--
Start rpm commandline compatible wrapper.
krh@215
     1
/*
krh@215
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
krh@215
     3
 * Copyright (C) 2008  Red Hat, Inc
krh@215
     4
 *
krh@215
     5
 * This program is free software; you can redistribute it and/or modify
krh@215
     6
 * it under the terms of the GNU General Public License as published by
krh@215
     7
 * the Free Software Foundation; either version 2 of the License, or
krh@215
     8
 * (at your option) any later version.
krh@215
     9
 *
krh@215
    10
 * This program is distributed in the hope that it will be useful,
krh@215
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
krh@215
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
krh@215
    13
 * GNU General Public License for more details.
krh@215
    14
 *
krh@215
    15
 * You should have received a copy of the GNU General Public License along
krh@215
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
krh@215
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
krh@215
    18
 */
krh@215
    19
krh@215
    20
#include <stdlib.h>
krh@215
    21
#include <string.h>
krh@215
    22
#include <stdio.h>
krh@215
    23
#include "razor.h"
krh@215
    24
krh@215
    25
enum option_type {
krh@215
    26
	OPTION_LAST,
krh@215
    27
	OPTION_GROUP,
krh@215
    28
	OPTION_BOOL,
krh@215
    29
	OPTION_STRING
krh@215
    30
};
krh@215
    31
krh@215
    32
struct option {
krh@215
    33
	enum option_type type;
krh@215
    34
	const char *name;
krh@215
    35
	char short_name;
krh@215
    36
	const char *arg_name;
krh@215
    37
	const char *description;
krh@215
    38
	void *data;
krh@215
    39
};
krh@215
    40
krh@215
    41
static int option_all;
krh@215
    42
krh@215
    43
static const struct option query_options[] = {
krh@215
    44
	{ OPTION_BOOL, "configfiles", 'c', NULL, "list all configuration files", NULL },
krh@215
    45
	{ OPTION_BOOL, "docfiles", 'd', NULL, "list all documentation files", NULL },
krh@215
    46
	{ OPTION_BOOL, "dump", 0, NULL, "dump basic file information", NULL },
krh@215
    47
	{ OPTION_BOOL, "list", 0, NULL, "list files in package", NULL },
krh@215
    48
	{ OPTION_STRING, "queryformat", 0, "QUERYFORMAT", "use the following query format", NULL },
krh@215
    49
	{ OPTION_BOOL, "state", 's', NULL, "display the states of the listed files", NULL },
krh@215
    50
	{ OPTION_BOOL, "all", 'a', NULL, "query/verify all packages", &option_all },
krh@215
    51
	{ OPTION_BOOL, "file", 'f', NULL, "query/verify package(s) owning file", NULL },
krh@215
    52
	{ OPTION_BOOL, "group", 'g', NULL, "query/verify package(s) in group", NULL },
krh@215
    53
	{ OPTION_BOOL, "package", 'p', NULL, "query/verify a package file", NULL },
krh@215
    54
	{ OPTION_BOOL, "ftswalk", 'W', NULL, "query/verify package(s) from TOP file tree walk", NULL },
krh@215
    55
	{ OPTION_BOOL, "pkgid", 0, NULL, "query/verify package(s) with package identifier", NULL },
krh@215
    56
	{ OPTION_BOOL, "hdrid", 0, NULL, "query/verify package(s) with header identifier", NULL },
krh@215
    57
	{ OPTION_BOOL, "fileid", 0, NULL, "query/verify package(s) with file identifier", NULL },
krh@215
    58
	{ OPTION_BOOL, "specfile", 0, NULL, "query a spec file", NULL, },
krh@215
    59
	{ OPTION_BOOL, "triggeredby", 0, NULL, "query the package(s) triggered by the package", NULL },
krh@215
    60
	{ OPTION_BOOL, "whatrequires", 0, NULL, "query/verify the package(s) which require a dependency", NULL },
krh@215
    61
	{ OPTION_BOOL, "whatprovides", 0, NULL, "query/verify the package(s) which provide a dependency", NULL },
krh@215
    62
	{ OPTION_BOOL, "nomanifest", 0, NULL, "do not process non-package files as manifests", NULL },
krh@215
    63
	{ }
krh@215
    64
};
krh@215
    65
krh@215
    66
static const struct option verify_options[] = {
krh@215
    67
	{ OPTION_BOOL, "nomd5", 0, NULL, "don't verify MD5 digest of files", NULL },
krh@215
    68
	{ OPTION_BOOL, "nofiles", 0, NULL, "don't verify files in package", NULL },
krh@215
    69
	{ OPTION_BOOL, "nodeps", 0, NULL, "don't verify package dependencies", NULL },
krh@215
    70
	{ OPTION_BOOL, "noscript", 0, NULL, "don't execute verify script(s)", NULL, },
krh@215
    71
	{ OPTION_BOOL, "all", 'a', NULL, "query/verify all packages", NULL },
krh@215
    72
	{ OPTION_BOOL, "file", 'f', NULL, "query/verify package(s) owning file", NULL },
krh@215
    73
	{ OPTION_BOOL, "group", 'g', NULL, "query/verify package(s) in group", NULL },
krh@215
    74
	{ OPTION_BOOL, "package", 'p', NULL, "query/verify a package file", NULL },
krh@215
    75
	{ OPTION_BOOL, "ftswalk", 'W', NULL, "query/verify package(s) from TOP file tree walk", NULL },
krh@215
    76
	{ OPTION_BOOL, "pkgid", 0, NULL, "query/verify package(s) with package identifier", NULL },
krh@215
    77
	{ OPTION_BOOL, "hdrid", 0, NULL, "query/verify package(s) with header identifier", NULL },
krh@215
    78
	{ OPTION_BOOL, "fileid", 0, NULL, "query/verify package(s) with file identifier", NULL },
krh@215
    79
	{ OPTION_BOOL, "specfile", 0, NULL, "query a spec file", NULL },
krh@215
    80
	{ OPTION_BOOL, "triggeredby", 0, NULL, "query the package(s) triggered by the package", NULL },
krh@215
    81
	{ OPTION_BOOL, "whatrequires", 0, NULL, "query/verify the package(s) which require a dependency", NULL },
krh@215
    82
	{ OPTION_BOOL, "whatprovides", 0, NULL, "query/verify the package(s) which provide a dependency", NULL },
krh@215
    83
	{ OPTION_BOOL, "nomanifest", 0, NULL, "do not process non-package files as manifests", NULL },
krh@215
    84
	{ }
krh@215
    85
};
krh@215
    86
krh@215
    87
static const struct option ftw_options[] = {
krh@215
    88
	{ OPTION_BOOL, "comfollow", 0, NULL, "FTS_COMFOLLOW: follow command line symlinks", NULL },
krh@215
    89
	{ OPTION_BOOL, "logical", 0, NULL, "FTS_LOGICAL: logical walk", NULL },
krh@215
    90
	{ OPTION_BOOL, "nochdir", 0, NULL, "FTS_NOCHDIR: don't change directories", NULL },
krh@215
    91
	{ OPTION_BOOL, "nostat", 0, NULL, "FTS_NOSTAT: don't get stat info", NULL },
krh@215
    92
	{ OPTION_BOOL, "physical", 0, NULL, "FTS_PHYSICAL: physical walk", NULL },
krh@215
    93
	{ OPTION_BOOL, "seedot", 0, NULL, "FTS_SEEDOT: return dot and dot-dot", NULL },
krh@215
    94
	{ OPTION_BOOL, "xdev", 0, NULL, "FTS_XDEV: don't cross devices", NULL },
krh@215
    95
	{ OPTION_BOOL, "whiteout", 0, NULL, "FTS_WHITEOUT: return whiteout information", NULL },
krh@215
    96
	{ }
krh@215
    97
};
krh@215
    98
krh@215
    99
static const struct option signature_options[] = {
krh@215
   100
	{ OPTION_BOOL, "addsign", 0, NULL, "sign package(s) (identical to --resign)", NULL, },
krh@215
   101
	{ OPTION_BOOL, "checksig", 'K', NULL, "verify package signature(s)", NULL, },
krh@215
   102
	{ OPTION_BOOL, "delsign", 0, NULL, "delete package signatures", NULL, },
krh@215
   103
	{ OPTION_BOOL, "import", 0, NULL, "import an armored public key", NULL, },
krh@215
   104
	{ OPTION_BOOL, "resign", 0, NULL, "sign package(s) (identical to --addsign)", NULL, },
krh@215
   105
	{ OPTION_BOOL, "nodigest", 0, NULL, "don't verify package digest(s)", NULL, },
krh@215
   106
	{ OPTION_BOOL, "nosignature", 0, NULL, "don't verify package signature(s)", NULL },
krh@215
   107
	{ }
krh@215
   108
};
krh@215
   109
krh@215
   110
static const struct option database_options[] = {
krh@215
   111
	{ OPTION_BOOL, "initdb", 0, NULL, "initialize database", NULL },
krh@215
   112
	{ OPTION_BOOL, "rebuilddb", 0, NULL, "rebuild database inverted lists from installed package headers", NULL },
krh@215
   113
	{ }
krh@215
   114
};
krh@215
   115
krh@215
   116
static int option_erase, option_install, option_upgrade;
krh@215
   117
krh@215
   118
static const struct option install_options[] = {
krh@215
   119
	{ OPTION_BOOL, "aid", 0, NULL, "add suggested packages to transaction", NULL, },
krh@215
   120
	{ OPTION_BOOL, "allfiles", 0, NULL, "install all files, even configurations which might otherwise be skipped", NULL, },
krh@215
   121
	{ OPTION_BOOL, "allmatches", 0, NULL, "remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)", NULL, },
krh@215
   122
	{ OPTION_BOOL, "badreloc", 0, NULL, "relocate files in non-relocatable package", NULL },
krh@215
   123
	{ OPTION_BOOL, "erase", 'e', "<package>", "erase (uninstall) package", &option_erase },
krh@215
   124
	{ OPTION_BOOL, "excludedocs", 0, NULL, "do not install documentation", NULL, },
krh@215
   125
	{ OPTION_BOOL, "excludepath", 0, "<path>", "skip files with leading component <path> ", NULL, },
krh@215
   126
	{ OPTION_BOOL, "fileconflicts", 0, NULL, "detect file conflicts between packages", NULL, },
krh@215
   127
	{ OPTION_BOOL, "force", 0, NULL, "short hand for --replacepkgs --replacefiles", NULL },
krh@215
   128
	{ OPTION_BOOL, "freshen", 'F', "<packagefile>+", "upgrade package(s) if already installed", NULL },
krh@215
   129
	{ OPTION_BOOL, "hash", 'h', NULL, "print hash marks as package installs (good with -v)", NULL },
krh@215
   130
	{ OPTION_BOOL, "ignorearch", 0, NULL, "don't verify package architecture", NULL, },
krh@215
   131
	{ OPTION_BOOL, "ignoreos", 0, NULL, "don't verify package operating system", NULL, },
krh@215
   132
	{ OPTION_BOOL, "ignoresize", 0, NULL, "don't check disk space before installing", NULL },
krh@215
   133
	{ OPTION_BOOL, "install", 'i', NULL, "install package(s)", &option_install },
krh@215
   134
	{ OPTION_BOOL, "justdb", 0, NULL, "update the database, but do not modify the filesystem", NULL, },
krh@215
   135
	{ OPTION_BOOL, "nodeps", 0, NULL, "do not verify package dependencies", NULL, },
krh@215
   136
	{ OPTION_BOOL, "nomd5", 0, NULL, "don't verify MD5 digest of files", NULL, },
krh@215
   137
	{ OPTION_BOOL, "nocontexts", 0, NULL, "don't install file security contexts", NULL, },
krh@215
   138
	{ OPTION_BOOL, "noorder", 0, NULL, "do not reorder package installation to satisfy dependencies", NULL, },
krh@215
   139
	{ OPTION_BOOL, "nosuggest", 0, NULL, "do not suggest missing dependency resolution(s)", NULL, },
krh@215
   140
	{ OPTION_BOOL, "noscripts", 0, NULL, "do not execute package scriptlet(s)", NULL, },
krh@215
   141
	{ OPTION_BOOL, "notriggers", 0, NULL, "do not execute any scriptlet(s) triggered by this package", NULL, },
krh@215
   142
	{ OPTION_BOOL, "oldpackage", 0, NULL, "upgrade to an old version of the package (--force on upgrades does this automatically)", NULL },
krh@215
   143
	{ OPTION_BOOL, "percent", 0, NULL, "print percentages as package installs", NULL, },
krh@215
   144
	{ OPTION_STRING, "prefix", 0, "<dir>", "relocate the package to <dir>, if relocatable", NULL, },
krh@215
   145
	{ OPTION_STRING, "relocate", 0, "<old>=<new>", "relocate files from path <old> to <new>", NULL, },
krh@215
   146
	{ OPTION_BOOL, "repackage", 0, NULL, "save erased package files by repackaging", NULL, },
krh@215
   147
	{ OPTION_BOOL, "replacefiles", 0, NULL, "ignore file conflicts between packages", NULL, },
krh@215
   148
	{ OPTION_BOOL, "replacepkgs", 0, NULL, "reinstall if the package is already present", NULL, },
krh@215
   149
	{ OPTION_BOOL, "test", 0, NULL, "don't install, but tell if it would work or not", NULL },
krh@215
   150
	{ OPTION_BOOL, "upgrade", 'U', "<packagefile>+", "upgrade package(s)", &option_upgrade },
krh@215
   151
	{ }
krh@215
   152
};
krh@215
   153
krh@215
   154
static int option_version;
krh@215
   155
krh@215
   156
static const struct option common_options[] = {
krh@215
   157
	{ OPTION_STRING, "define", 'D', "MACRO EXPR", "define MACRO with value EXPR", NULL, },
krh@215
   158
	{ OPTION_STRING, "eval", 'E', "EXPR", "print macro expansion of EXPR", NULL },
krh@215
   159
	{ OPTION_STRING, "macros", 0, "<FILE:...>", "read <FILE:...> instead of default file(s)", NULL },
krh@215
   160
	{ OPTION_BOOL, "nodigest", 0, NULL, "don't verify package digest(s)", NULL, },
krh@215
   161
	{ OPTION_BOOL, "nosignature", 0, NULL, "don't verify package signature(s)", NULL, },
krh@215
   162
	{ OPTION_STRING, "rcfile", 0, "<FILE:...>", "read <FILE:...> instead of default file(s)", NULL },
krh@215
   163
	{ OPTION_STRING, "root", 'r', "ROOT", "use ROOT as top level directory (default: \"/\")", NULL },
krh@215
   164
	{ OPTION_BOOL, "querytags", 0, NULL, "display known query tags", NULL, },
krh@215
   165
	{ OPTION_BOOL, "showrc", 0, NULL, "display final rpmrc and macro configuration", NULL, },
krh@215
   166
	{ OPTION_BOOL, "quiet", 0, NULL, "provide less detailed output", NULL },
krh@215
   167
	{ OPTION_BOOL, "verbose", 'v', NULL, "provide more detailed output", NULL },
krh@215
   168
	{ OPTION_BOOL, "version", 0, NULL, "print the version of rpm being used", &option_version },
krh@215
   169
	{ }
krh@215
   170
};
krh@215
   171
krh@215
   172
static const struct option alias_options[] = {
krh@215
   173
	{ OPTION_BOOL, "scripts", 0, NULL, "list install/erase scriptlets from package(s)", NULL, },
krh@215
   174
	{ OPTION_BOOL, "setperms", 0, NULL, "set permissions of files in a package", NULL, },
krh@215
   175
	{ OPTION_BOOL, "setugids", 0, NULL, "set user/group ownership of files in a package", NULL, },
krh@215
   176
	{ OPTION_BOOL, "conflicts", 0, NULL, "list capabilities this package conflicts with", NULL, },
krh@215
   177
	{ OPTION_BOOL, "obsoletes", 0, NULL, "list other packages removed by installing this package", NULL, },
krh@215
   178
	{ OPTION_BOOL, "provides", 0, NULL, "list capabilities that this package provides", NULL, },
krh@215
   179
	{ OPTION_BOOL, "requires", 0, NULL, "list capabilities required by package(s)", NULL, },
krh@215
   180
	{ OPTION_BOOL, "info", 0, NULL, "list descriptive information from package(s)", NULL, },
krh@215
   181
	{ OPTION_BOOL, "changelog", 0, NULL, "list change logs for this package", NULL, },
krh@215
   182
	{ OPTION_BOOL, "xml", 0, NULL, "list metadata in xml", NULL, },
krh@215
   183
	{ OPTION_BOOL, "triggers", 0, NULL, "list trigger scriptlets from package(s)", NULL, },
krh@215
   184
	{ OPTION_BOOL, "last", 0, NULL, "list package(s) by install time, most recent first", NULL, },
krh@215
   185
	{ OPTION_BOOL, "dupes", 0, NULL, "list duplicated packages", NULL, },
krh@215
   186
	{ OPTION_BOOL, "filesbypkg", 0, NULL, "list all files from each package", NULL, },
krh@215
   187
	{ OPTION_BOOL, "fileclass", 0, NULL, "list file names with classes", NULL, },
krh@215
   188
	{ OPTION_BOOL, "filecolor", 0, NULL, "list file names with colors", NULL, },
krh@215
   189
	{ OPTION_BOOL, "filecontext", 0, NULL, "list file names with security context from header", NULL, },
krh@215
   190
	{ OPTION_BOOL, "fscontext", 0, NULL, "list file names with security context from file system", NULL, },
krh@215
   191
	{ OPTION_BOOL, "recontext", 0, NULL, "list file names with security context from policy RE", NULL, },
krh@215
   192
	{ OPTION_BOOL, "fileprovide", 0, NULL, "list file names with provides", NULL, },
krh@215
   193
	{ OPTION_BOOL, "filerequire", 0, NULL, "list file names with requires", NULL, },
krh@215
   194
	{ OPTION_BOOL, "redhatprovides", 0, NULL, "find package name that contains a provided capability (needs rpmdb-redhat package installed)", NULL, },
krh@215
   195
	{ OPTION_BOOL, "redhatrequires", 0, NULL, "find package name that contains a required capability (needs rpmdb-redhat package installed)", NULL, },
krh@215
   196
	{ OPTION_STRING, "buildpolicy", 0, "<policy>", "set buildroot <policy> (e.g. compress man pages)", NULL, },
krh@215
   197
	{ OPTION_BOOL, "with", 0, "<option>", "enable configure <option> for build", NULL, },
krh@215
   198
	{ OPTION_BOOL, "without", 0, "<option>", "disable configure <option> for build", NULL },
krh@215
   199
	{ }
krh@215
   200
};
krh@215
   201
krh@215
   202
static int option_help, option_usage;
krh@215
   203
krh@215
   204
static const struct option help_options[] = {
krh@215
   205
	{ OPTION_BOOL, "help", '?', NULL, "Show this help message", &option_help },
krh@215
   206
	{ OPTION_BOOL, "usage", 0, NULL, "Display brief usage message", &option_usage},
krh@215
   207
	{ }
krh@215
   208
};
krh@215
   209
krh@215
   210
static int option_query, option_verify;
krh@215
   211
krh@215
   212
static const struct option rpm_options[] = {
krh@215
   213
	{ OPTION_BOOL, "query", 'q', NULL, "Query rpm database", &option_query },
krh@215
   214
	{ OPTION_BOOL, "verify", 'V', NULL, "Verify rpm database", &option_verify },
krh@215
   215
	{ OPTION_GROUP, NULL, 0, NULL, "Query options (with -q or --query):", &query_options },
krh@215
   216
	{ OPTION_GROUP, NULL, 0, NULL, "Verify options (with -V or --verify):", &verify_options },
krh@215
   217
	{ OPTION_GROUP, NULL, 0, NULL, "File tree walk options (with --ftswalk):", &ftw_options },
krh@215
   218
	{ OPTION_GROUP, NULL, 0, NULL, "Signature options:", &signature_options },
krh@215
   219
	{ OPTION_GROUP, NULL, 0, NULL, "Database options:", &database_options },
krh@215
   220
	{ OPTION_GROUP, NULL, 0, NULL, "Install/Upgrade/Erase options:", &install_options },
krh@215
   221
	{ OPTION_GROUP, NULL, 0, NULL, "Common options for all rpm modes and executables:", &common_options },
krh@215
   222
	{ OPTION_GROUP, NULL, 0, NULL, "Options implemented via popt alias/exec:", &alias_options },
krh@215
   223
	{ OPTION_GROUP, NULL, 0, NULL, "Help options", &help_options },
krh@215
   224
	{ }
krh@215
   225
};
krh@215
   226
krh@215
   227
static void
krh@215
   228
command_query(int argc, const char *argv[])
krh@215
   229
{
krh@215
   230
	if (argc == 0 && !option_all) {
krh@215
   231
		printf("no arguments given for query\n");
krh@215
   232
		exit(1);
krh@215
   233
	}
krh@215
   234
krh@215
   235
	printf("command query - not implemented\n");
krh@215
   236
}
krh@215
   237
krh@215
   238
static void
krh@215
   239
command_verify(int argc, const char *argv[])
krh@215
   240
{
krh@215
   241
	if (argc == 0) {
krh@215
   242
		printf("no arguments given for verify\n");
krh@215
   243
		exit(1);
krh@215
   244
	}
krh@215
   245
krh@215
   246
	printf("command verify - not implemented\n");
krh@215
   247
}
krh@215
   248
krh@215
   249
static void
krh@215
   250
command_erase(int argc, const char *argv[])
krh@215
   251
{
krh@215
   252
	if (argc == 0) {
krh@215
   253
		printf("no packages given for erase\n");
krh@215
   254
		exit(1);
krh@215
   255
	}
krh@215
   256
krh@215
   257
	printf("command erase - not implemented\n");
krh@215
   258
}
krh@215
   259
krh@215
   260
static void
krh@215
   261
command_install(int argc, const char *argv[])
krh@215
   262
{
krh@215
   263
	if (argc == 0) {
krh@215
   264
		printf("no packages given for install\n");
krh@215
   265
		exit(1);
krh@215
   266
	}
krh@215
   267
krh@215
   268
	printf("command install - not implemented\n");
krh@215
   269
}
krh@215
   270
krh@215
   271
static void
krh@215
   272
command_update(int argc, const char *argv[])
krh@215
   273
{
krh@215
   274
	if (argc == 0) {
krh@215
   275
		printf("no packages given for update\n");
krh@215
   276
		exit(1);
krh@215
   277
	}
krh@215
   278
krh@215
   279
	printf("command update - not implemented\n");
krh@215
   280
}
krh@215
   281
krh@215
   282
static const struct option *
krh@215
   283
find_option(const struct option *options, const char *s)
krh@215
   284
{
krh@215
   285
	const struct option *o;
krh@215
   286
	int i;
krh@215
   287
krh@215
   288
	for (i = 0; options[i].type != OPTION_LAST; i++) {
krh@215
   289
		switch (options[i].type) {
krh@215
   290
		case OPTION_GROUP:
krh@215
   291
			o = find_option(options[i].data, s);
krh@215
   292
			if (o != NULL)
krh@215
   293
				return o;
krh@215
   294
			break;
krh@215
   295
krh@215
   296
		case OPTION_BOOL:
krh@215
   297
		case OPTION_STRING:
krh@215
   298
			if (s[0] == '-' &&
krh@215
   299
			    s[1] == options[i].short_name && s[2] == '\0')
krh@215
   300
				return &options[i];
krh@215
   301
			if (s[0] == '-' && s[1] == '-' &&
krh@215
   302
			    strcmp(options[i].name, s + 2) == 0)
krh@215
   303
				return &options[i];
krh@215
   304
			break;
krh@215
   305
krh@215
   306
		case OPTION_LAST:
krh@215
   307
			break;
krh@215
   308
		}
krh@215
   309
	}
krh@215
   310
krh@215
   311
	return NULL;
krh@215
   312
}
krh@215
   313
krh@215
   314
static int
krh@215
   315
parse_options(const struct option *options, int argc, const char **argv)
krh@215
   316
{
krh@215
   317
	const struct option *o;
krh@215
   318
	int i, j;
krh@215
   319
krh@215
   320
	/* FIXME: Bundling... rpm -Uvh must work :) */
krh@215
   321
krh@215
   322
	for (i = 1, j = 0; i < argc; i++) {
krh@215
   323
		if (argv[i][0] != '-') {
krh@215
   324
			argv[j++] = argv[i];
krh@215
   325
			continue;
krh@215
   326
		}
krh@215
   327
		o = find_option(options, argv[i]);
krh@215
   328
		if (o == NULL) {
krh@215
   329
			printf("unknown option: \"%s\"\n", argv[i]);
krh@215
   330
			continue;
krh@215
   331
		}
krh@215
   332
		if (o->data == NULL) {
krh@215
   333
			printf("option \"%s\" not supported\n", argv[i]);
krh@215
   334
			continue;
krh@215
   335
		}
krh@215
   336
		switch (o->type) {
krh@215
   337
		case OPTION_BOOL:
krh@215
   338
			*(int *) o->data = 1;
krh@215
   339
			break;
krh@215
   340
krh@215
   341
		case OPTION_STRING:
krh@215
   342
			*(const char **) o->data =
krh@215
   343
				argv[i] + strlen(o->name) + 3;
krh@215
   344
			break;
krh@215
   345
krh@215
   346
		case OPTION_LAST:
krh@215
   347
		case OPTION_GROUP:
krh@215
   348
			/* Shouldn't happen. */
krh@215
   349
			break;
krh@215
   350
		}
krh@215
   351
	}
krh@215
   352
krh@215
   353
	return j;
krh@215
   354
}
krh@215
   355
krh@215
   356
static void
krh@215
   357
print_options_help(const struct option *options)
krh@215
   358
{
krh@215
   359
	int i;
krh@215
   360
krh@215
   361
	for (i = 0; options[i].type != OPTION_LAST; i++) {
krh@215
   362
		switch (options[i].type) {
krh@215
   363
		case OPTION_GROUP:
krh@215
   364
			printf("%s\n", options[i].description);
krh@215
   365
			print_options_help(options[i].data);
krh@215
   366
			printf("\n");
krh@215
   367
			break;
krh@215
   368
krh@215
   369
		case OPTION_BOOL:
krh@215
   370
		case OPTION_STRING:
krh@215
   371
			printf("  ");
krh@215
   372
			if (options[i].short_name)
krh@215
   373
				printf("-%c", options[i].short_name);
krh@215
   374
			if (options[i].short_name && options[i].name)
krh@215
   375
				printf(", ");
krh@215
   376
			if (options[i].name)
krh@215
   377
				printf("--%s", options[i].name);
krh@215
   378
			if (options[i].arg_name)
krh@215
   379
				printf("=%s", options[i].arg_name);
krh@215
   380
			if (options[i].description)
krh@215
   381
				printf("\t\t%s", options[i].description);
krh@215
   382
			printf("\n");
krh@215
   383
			break;
krh@215
   384
krh@215
   385
		case OPTION_LAST:
krh@215
   386
			break;
krh@215
   387
		}
krh@215
   388
	}
krh@215
   389
}
krh@215
   390
krh@215
   391
static void
krh@215
   392
print_options_usage(const struct option *options)
krh@215
   393
{
krh@215
   394
	int i;
krh@215
   395
krh@215
   396
	for (i = 0; options[i].type != OPTION_LAST; i++) {
krh@215
   397
		switch (options[i].type) {
krh@215
   398
		case OPTION_GROUP:
krh@215
   399
			print_options_usage(options[i].data);
krh@215
   400
			break;
krh@215
   401
krh@215
   402
		case OPTION_BOOL:
krh@215
   403
			printf("[");
krh@215
   404
			if (options[i].short_name)
krh@215
   405
				printf("-%c", options[i].short_name);
krh@215
   406
			if (options[i].short_name && options[i].name)
krh@215
   407
				printf("|");
krh@215
   408
			if (options[i].name)
krh@215
   409
				printf("--%s", options[i].name);
krh@215
   410
			printf("] ");
krh@215
   411
			break;
krh@215
   412
krh@215
   413
		case OPTION_STRING:
krh@215
   414
			printf("[");
krh@215
   415
			if (options[i].short_name)
krh@215
   416
				printf("-%c", options[i].short_name);
krh@215
   417
			if (options[i].short_name && options[i].name)
krh@215
   418
				printf("|");
krh@215
   419
			if (options[i].name)
krh@215
   420
				printf("--%s", options[i].name);
krh@215
   421
			if (options[i].arg_name)
krh@215
   422
				printf("=%s", options[i].arg_name);
krh@215
   423
			printf("] ");
krh@215
   424
			break;
krh@215
   425
krh@215
   426
krh@215
   427
			break;
krh@215
   428
krh@215
   429
		case OPTION_LAST:
krh@215
   430
			break;
krh@215
   431
		}
krh@215
   432
	}
krh@215
   433
}
krh@215
   434
krh@215
   435
int
krh@215
   436
main(int argc, const char *argv[])
krh@215
   437
{
krh@215
   438
	argc = parse_options(rpm_options, argc, argv);
krh@215
   439
krh@215
   440
	if (option_version) {
krh@215
   441
		printf("razor rpm version hoopla.\n");
krh@215
   442
		exit(0);
krh@215
   443
	}
krh@215
   444
krh@215
   445
	if (option_help) {
krh@215
   446
		printf("Usage: rpm [OPTION...]\n");
krh@215
   447
		print_options_help(rpm_options);
krh@215
   448
		exit(0);
krh@215
   449
	}
krh@215
   450
krh@215
   451
	if (option_usage) {
krh@215
   452
		printf("Usage: rpm [OPTION...]\n");
krh@215
   453
		print_options_usage(rpm_options);
krh@215
   454
		printf("\n");
krh@215
   455
		exit(0);
krh@215
   456
	}
krh@215
   457
krh@215
   458
	if (option_query + option_verify +
krh@215
   459
	    option_erase + option_install + option_upgrade > 1) {
krh@215
   460
		printf("specify only one of query, verify, erase, install "
krh@215
   461
		       "or upgrade\n");
krh@215
   462
		exit(1);
krh@215
   463
	}
krh@215
   464
	    
krh@215
   465
	if (option_query) {
krh@215
   466
		command_query(argc, argv);
krh@215
   467
	} else if (option_verify) {
krh@215
   468
		command_verify(argc, argv);
krh@215
   469
	} else if (option_erase) {
krh@215
   470
		command_erase(argc, argv);
krh@215
   471
	} else if (option_install) {
krh@215
   472
		command_install(argc, argv);
krh@215
   473
	} else if (option_upgrade) {
krh@215
   474
		command_update(argc, argv);
krh@215
   475
	} else {
krh@215
   476
		print_options_usage(rpm_options);
krh@215
   477
		printf("\n");
krh@215
   478
		exit(0);
krh@215
   479
	}
krh@215
   480
krh@215
   481
	return 0;
krh@215
   482
}