1.1 --- a/rpm-razor.c Wed Jun 04 15:04:45 2008 -0400
1.2 +++ b/rpm-razor.c Wed Jun 04 20:04:57 2008 -0400
1.3 @@ -38,6 +38,14 @@
1.4 void *data;
1.5 };
1.6
1.7 +/* A note about all these options: rpm allows options to mean
1.8 + * different things depending on what other options are present on the
1.9 + * command line. For example, if -q or --query is present, -i no
1.10 + * longer means install, but info. The way we handle this is by
1.11 + * setting all the options that may match (ie if -i is given we set
1.12 + * install and info), and then look at the relevent one depending on
1.13 + * what else in on the command line. */
1.14 +
1.15 static int option_all, option_whatrequires, option_whatprovides;
1.16 static int option_package;
1.17
1.18 @@ -170,7 +178,8 @@
1.19 { }
1.20 };
1.21
1.22 -static int option_conflicts, option_obsoletes, option_requires, option_provides;
1.23 +static int option_conflicts, option_obsoletes, option_requires;
1.24 +static int option_provides, option_info, option_changelog;
1.25
1.26 static const struct option alias_options[] = {
1.27 { OPTION_BOOL, "scripts", 0, NULL, "list install/erase scriptlets from package(s)", NULL, },
1.28 @@ -180,8 +189,8 @@
1.29 { OPTION_BOOL, "obsoletes", 0, NULL, "list other packages removed by installing this package", &option_obsoletes, },
1.30 { OPTION_BOOL, "provides", 0, NULL, "list capabilities that this package provides", &option_provides, },
1.31 { OPTION_BOOL, "requires", 0, NULL, "list capabilities required by package(s)", &option_requires, },
1.32 - { OPTION_BOOL, "info", 0, NULL, "list descriptive information from package(s)", NULL, },
1.33 - { OPTION_BOOL, "changelog", 0, NULL, "list change logs for this package", NULL, },
1.34 + { OPTION_BOOL, "info", 'i', NULL, "list descriptive information from package(s)", &option_info, },
1.35 + { OPTION_BOOL, "changelog", 0, NULL, "list change logs for this package", &option_changelog, },
1.36 { OPTION_BOOL, "xml", 0, NULL, "list metadata in xml", NULL, },
1.37 { OPTION_BOOL, "triggers", 0, NULL, "list trigger scriptlets from package(s)", NULL, },
1.38 { OPTION_BOOL, "last", 0, NULL, "list package(s) by install time, most recent first", NULL, },
1.39 @@ -328,6 +337,18 @@
1.40 razor_property_iterator_destroy(pi);
1.41 }
1.42
1.43 +static void
1.44 +print_package_info(struct razor_set *set, struct razor_package *package)
1.45 +{
1.46 + printf("FIXME: Package info not tracked.\n");
1.47 +}
1.48 +
1.49 +static void
1.50 +print_package_changelog(struct razor_set *set, struct razor_package *package)
1.51 +{
1.52 + printf("FIXME: Package changelog not tracked.\n");
1.53 +}
1.54 +
1.55 static struct razor_set *
1.56 create_set_from_command_line(int argc, const char *argv[])
1.57 {
1.58 @@ -407,8 +428,14 @@
1.59 if (option_provides)
1.60 print_package_properties(set, package,
1.61 RAZOR_PROPERTY_PROVIDES);
1.62 - if (!option_conflicts && !option_obsoletes &&
1.63 - !option_requires && !option_provides)
1.64 + if (option_info)
1.65 + print_package_info(set, package);
1.66 + if (option_changelog)
1.67 + print_package_changelog(set, package);
1.68 +
1.69 + if (option_conflicts + option_obsoletes +
1.70 + option_requires + option_provides +
1.71 + option_info + option_changelog == 0)
1.72 printf("%s-%s.%s\n", name, version, arch);
1.73 }
1.74
1.75 @@ -463,28 +490,31 @@
1.76 printf("command update - not implemented\n");
1.77 }
1.78
1.79 -static const struct option *
1.80 -find_option(const struct option *options, const char *s)
1.81 +static int
1.82 +for_each_option(const struct option *options, const char *s,
1.83 + void (*fn)(const struct option *o,
1.84 + const char *arg, void *data), void *data)
1.85 {
1.86 - const struct option *o;
1.87 - int i;
1.88 + int i, count = 0;
1.89
1.90 for (i = 0; options[i].type != OPTION_LAST; i++) {
1.91 switch (options[i].type) {
1.92 case OPTION_GROUP:
1.93 - o = find_option(options[i].data, s);
1.94 - if (o != NULL)
1.95 - return o;
1.96 + count += for_each_option(options[i].data, s, fn, data);
1.97 break;
1.98
1.99 case OPTION_BOOL:
1.100 case OPTION_STRING:
1.101 if (s[0] == '-' &&
1.102 - s[1] == options[i].short_name && s[2] == '\0')
1.103 - return &options[i];
1.104 + s[1] == options[i].short_name && s[2] == '\0') {
1.105 + fn(&options[i], s, data);
1.106 + count++;
1.107 + }
1.108 if (s[0] == '-' && s[1] == '-' &&
1.109 - strcmp(options[i].name, s + 2) == 0)
1.110 - return &options[i];
1.111 + strcmp(options[i].name, s + 2) == 0) {
1.112 + fn(&options[i], s, data);
1.113 + count++;
1.114 + }
1.115 break;
1.116
1.117 case OPTION_LAST:
1.118 @@ -492,13 +522,36 @@
1.119 }
1.120 }
1.121
1.122 - return NULL;
1.123 + return count;
1.124 +}
1.125 +
1.126 +static void
1.127 +handle_option(const struct option *o, const char *arg, void *data)
1.128 +{
1.129 + if (o->data == NULL) {
1.130 + printf("option \"%s\" not supported\n", arg);
1.131 + return;
1.132 + }
1.133 +
1.134 + switch (o->type) {
1.135 + case OPTION_BOOL:
1.136 + *(int *) o->data = 1;
1.137 + break;
1.138 +
1.139 + case OPTION_STRING:
1.140 + *(const char **) o->data = arg + strlen(o->name) + 3;
1.141 + break;
1.142 +
1.143 + case OPTION_LAST:
1.144 + case OPTION_GROUP:
1.145 + /* Shouldn't happen. */
1.146 + break;
1.147 + }
1.148 }
1.149
1.150 static int
1.151 parse_options(const struct option *options, int argc, const char **argv)
1.152 {
1.153 - const struct option *o;
1.154 int i, j;
1.155
1.156 /* FIXME: Bundling... rpm -Uvh must work :) */
1.157 @@ -508,29 +561,11 @@
1.158 argv[j++] = argv[i];
1.159 continue;
1.160 }
1.161 - o = find_option(options, argv[i]);
1.162 - if (o == NULL) {
1.163 +
1.164 + if (for_each_option(options, argv[i],
1.165 + handle_option, NULL) == 0) {
1.166 printf("unknown option: \"%s\"\n", argv[i]);
1.167 - continue;
1.168 - }
1.169 - if (o->data == NULL) {
1.170 - printf("option \"%s\" not supported\n", argv[i]);
1.171 - continue;
1.172 - }
1.173 - switch (o->type) {
1.174 - case OPTION_BOOL:
1.175 - *(int *) o->data = 1;
1.176 - break;
1.177 -
1.178 - case OPTION_STRING:
1.179 - *(const char **) o->data =
1.180 - argv[i] + strlen(o->name) + 3;
1.181 - break;
1.182 -
1.183 - case OPTION_LAST:
1.184 - case OPTION_GROUP:
1.185 - /* Shouldn't happen. */
1.186 - break;
1.187 + exit(1);
1.188 }
1.189 }
1.190
1.191 @@ -639,23 +674,16 @@
1.192 exit(0);
1.193 }
1.194
1.195 - if (option_query + option_verify +
1.196 - option_erase + option_install + option_upgrade > 1) {
1.197 - printf("specify only one of query, verify, erase, install "
1.198 - "or upgrade\n");
1.199 - exit(1);
1.200 - }
1.201 -
1.202 - if (option_query) {
1.203 + if (option_verify) {
1.204 + command_verify(argc, argv);
1.205 + } else if (option_query) {
1.206 command_query(argc, argv);
1.207 - } else if (option_verify) {
1.208 - command_verify(argc, argv);
1.209 - } else if (option_erase) {
1.210 - command_erase(argc, argv);
1.211 } else if (option_install) {
1.212 command_install(argc, argv);
1.213 } else if (option_upgrade) {
1.214 command_update(argc, argv);
1.215 + } else if (option_erase) {
1.216 + command_erase(argc, argv);
1.217 } else {
1.218 print_options_usage(rpm_options);
1.219 printf("\n");