Skip to content

Commit c12f5aa

Browse files
committed
bin/xbps-query: cleanup mode handling
1 parent 8715af4 commit c12f5aa

1 file changed

Lines changed: 90 additions & 98 deletions

File tree

bin/xbps-query/main.c

Lines changed: 90 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -135,37 +135,45 @@ main(int argc, char **argv)
135135
{ "cat", required_argument, NULL, 2 },
136136
{ NULL, 0, NULL, 0 },
137137
};
138-
struct xbps_handle xh;
139-
const char *pkg, *rootdir, *cachedir, *confdir, *props, *catfile, *format;
140-
int c, flags, rv;
141-
bool list_pkgs, list_repos, orphans, own, list_repolock;
142-
bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps;
143-
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree;
138+
struct xbps_handle xh = {0};
139+
const char *pkg = NULL, *props = NULL, *catfile, *format;
140+
int c, rv;
141+
bool regex = false, repo_mode = false, fulldeptree = false;
144142
int json = 0;
143+
enum {
144+
CAT_FILE = 1,
145+
LIST_HOLD,
146+
LIST_INSTALLED,
147+
LIST_MANUAL,
148+
LIST_ORPHANS,
149+
LIST_REPOLOCK,
150+
SHOW_REPOS,
151+
SEARCH_FILE,
152+
SEARCH_PKG,
153+
SHOW_DEPS,
154+
SHOW_FILES,
155+
SHOW_PKG,
156+
SHOW_REVDEPS,
157+
} mode = 0;
145158

146-
rootdir = cachedir = confdir = props = pkg = catfile = format = NULL;
147-
flags = rv = c = 0;
148-
list_pkgs = list_repos = list_hold = orphans = pkg_search = own = false;
149-
list_manual = list_repolock = show_prop = show_files = false;
150-
regex = show = show_deps = show_rdeps = fulldeptree = false;
151-
repo_mode = opmode = false;
152-
153-
memset(&xh, 0, sizeof(xh));
159+
props = pkg = catfile = format = NULL;
160+
rv = c = 0;
161+
repo_mode = false;
154162

155163
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
156164
switch (c) {
157165
case 'C':
158-
confdir = optarg;
166+
xbps_strlcpy(xh.confdir, optarg, sizeof(xh.confdir));
159167
break;
160168
case 'c':
161-
cachedir = optarg;
169+
xbps_strlcpy(xh.cachedir, optarg, sizeof(xh.cachedir));
162170
break;
163171
case 'd':
164-
flags |= XBPS_FLAG_DEBUG;
172+
xh.flags |= XBPS_FLAG_DEBUG;
165173
break;
166174
case 'f':
167175
pkg = optarg;
168-
show_files = opmode = true;
176+
mode = SHOW_FILES;
169177
break;
170178
case 'F':
171179
format = optarg;
@@ -174,36 +182,35 @@ main(int argc, char **argv)
174182
json++;
175183
break;
176184
case 'H':
177-
list_hold = opmode = true;
185+
mode = LIST_HOLD;
178186
break;
179187
case 'h':
180188
usage(false);
181189
/* NOTREACHED */
182190
case 'i':
183-
flags |= XBPS_FLAG_IGNORE_CONF_REPOS;
191+
xh.flags |= XBPS_FLAG_IGNORE_CONF_REPOS;
184192
break;
185193
case 'L':
186-
list_repos = opmode = true;
194+
mode = SHOW_REPOS;
187195
break;
188196
case 'l':
189-
list_pkgs = opmode = true;
197+
mode = LIST_INSTALLED;
190198
break;
191199
case 'M':
192-
flags |= XBPS_FLAG_REPOS_MEMSYNC;
200+
xh.flags |= XBPS_FLAG_REPOS_MEMSYNC;
193201
break;
194202
case 'm':
195-
list_manual = opmode = true;
203+
mode = LIST_MANUAL;
196204
break;
197205
case 'O':
198-
orphans = opmode = true;
206+
mode = LIST_ORPHANS;
199207
break;
200208
case 'o':
201209
pkg = optarg;
202-
own = opmode = true;
210+
mode = SEARCH_FILE;
203211
break;
204212
case 'p':
205213
props = optarg;
206-
show_prop = true;
207214
break;
208215
case 'R':
209216
if (optarg != NULL) {
@@ -212,29 +219,29 @@ main(int argc, char **argv)
212219
repo_mode = true;
213220
break;
214221
case 'r':
215-
rootdir = optarg;
222+
xbps_strlcpy(xh.rootdir, optarg, sizeof(xh.rootdir));
216223
break;
217224
case 'S':
218225
pkg = optarg;
219-
show = opmode = true;
226+
mode = SHOW_PKG;
220227
break;
221228
case 's':
222229
pkg = optarg;
223-
pkg_search = opmode = true;
230+
mode = SEARCH_PKG;
224231
break;
225232
case 'v':
226-
flags |= XBPS_FLAG_VERBOSE;
233+
xh.flags |= XBPS_FLAG_VERBOSE;
227234
break;
228235
case 'V':
229236
printf("%s\n", XBPS_RELVER);
230237
exit(EXIT_SUCCESS);
231238
case 'x':
232239
pkg = optarg;
233-
show_deps = opmode = true;
240+
mode = SHOW_DEPS;
234241
break;
235242
case 'X':
236243
pkg = optarg;
237-
show_rdeps = opmode = true;
244+
mode = SHOW_REVDEPS;
238245
break;
239246
case 0:
240247
regex = true;
@@ -243,10 +250,11 @@ main(int argc, char **argv)
243250
fulldeptree = true;
244251
break;
245252
case 2:
253+
mode = CAT_FILE;
246254
catfile = optarg;
247255
break;
248256
case 3:
249-
list_repolock = opmode = true;
257+
mode = LIST_REPOLOCK;
250258
break;
251259
case '?':
252260
default:
@@ -257,99 +265,83 @@ main(int argc, char **argv)
257265
argc -= optind;
258266
argv += optind;
259267

260-
if (!argc && !opmode) {
261-
usage(true);
262-
/* NOTREACHED */
263-
} else if (!opmode) {
264-
/* show mode by default */
265-
show = opmode = true;
268+
/* no mode (defaults to show) and cat mode take a trailing argv */
269+
if (mode == 0 || mode == CAT_FILE) {
270+
if (argc == 0)
271+
usage(true);
272+
if (mode == 0)
273+
mode = SHOW_PKG;
266274
pkg = *(argv++);
267275
argc--;
268276
}
269-
if (argc) {
270-
/* trailing parameters */
277+
278+
/* trailing parameters */
279+
if (argc != 0)
271280
usage(true);
272-
/* NOTREACHED */
273-
}
281+
274282
/*
275283
* Initialize libxbps.
276284
*/
277-
if (rootdir)
278-
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
279-
if (cachedir)
280-
xbps_strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
281-
if (confdir)
282-
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
283-
284-
xh.flags = flags;
285-
286285
if ((rv = xbps_init(&xh)) != 0) {
287286
xbps_error_printf("Failed to initialize libxbps: %s\n",
288287
strerror(rv));
289288
exit(EXIT_FAILURE);
290289
}
291290

292-
if (list_repos) {
293-
/* list repositories */
294-
rv = repo_list(&xh);
295-
296-
} else if (list_hold) {
291+
switch (mode) {
292+
case LIST_HOLD:
297293
rv = list_pkgdb(&xh, filter_hold, format ? format : "{pkgver}\n", json) < 0;
298-
299-
} else if (list_repolock) {
300-
rv = list_pkgdb(&xh, filter_repolock, format ? format : "{pkgver}\n", json) < 0;
301-
302-
} else if (list_manual) {
303-
rv = list_pkgdb(&xh, filter_manual, format ? format : "{pkgver}\n", json) < 0;
304-
305-
} else if (list_pkgs) {
306-
/* list available pkgs */
294+
break;
295+
case LIST_INSTALLED:
307296
if (format || json > 0) {
308297
rv = list_pkgdb(&xh, NULL, format, json);
309298
} else {
310299
rv = list_pkgs_pkgdb(&xh);
311300
}
312-
313-
} else if (orphans) {
314-
/* list pkg orphans */
301+
break;
302+
case LIST_MANUAL:
303+
rv = list_pkgdb(&xh, filter_manual, format ? format : "{pkgver}\n", json) < 0;
304+
break;
305+
case LIST_ORPHANS:
315306
rv = list_orphans(&xh, format ? format : "{pkgver}\n") < 0;
316-
317-
} else if (own) {
318-
/* ownedby mode */
307+
break;
308+
case LIST_REPOLOCK:
309+
rv = list_pkgdb(&xh, filter_repolock, format ? format : "{pkgver}\n", json) < 0;
310+
break;
311+
case SHOW_REPOS:
312+
rv = repo_list(&xh);
313+
break;
314+
case SEARCH_FILE:
319315
rv = ownedby(&xh, pkg, repo_mode, regex);
320-
321-
} else if (pkg_search) {
322-
/* search mode */
316+
break;
317+
case SEARCH_PKG:
323318
rv = search(&xh, repo_mode, pkg, props, regex);
324-
325-
} else if (catfile) {
326-
/* repo cat file mode */
319+
break;
320+
case SHOW_DEPS:
321+
rv = show_pkg_deps(&xh, pkg, repo_mode, fulldeptree);
322+
break;
323+
case SHOW_FILES:
327324
if (repo_mode)
328-
rv = repo_cat_file(&xh, pkg, catfile);
325+
rv = repo_show_pkg_files(&xh, pkg);
329326
else
330-
rv = cat_file(&xh, pkg, catfile);
331-
} else if (show || show_prop) {
332-
/* show mode */
327+
rv = show_pkg_files_from_metadir(&xh, pkg);
328+
break;
329+
case CAT_FILE:
333330
if (repo_mode)
334-
rv = repo_show_pkg_info(&xh, pkg, props);
331+
rv = repo_cat_file(&xh, pkg, catfile);
335332
else
336-
rv = show_pkg_info_from_metadir(&xh, pkg, props);
337-
338-
} else if (show_files) {
339-
/* show-files mode */
333+
rv = cat_file(&xh, pkg, catfile);
334+
break;
335+
case SHOW_PKG:
340336
if (repo_mode)
341-
rv = repo_show_pkg_files(&xh, pkg);
337+
rv = repo_show_pkg_info(&xh, pkg, props);
342338
else
343-
rv = show_pkg_files_from_metadir(&xh, pkg);
344-
345-
} else if (show_deps) {
346-
/* show-deps mode */
347-
rv = show_pkg_deps(&xh, pkg, repo_mode, fulldeptree);
348-
349-
} else if (show_rdeps) {
350-
/* show-rdeps mode */
339+
rv = show_pkg_info_from_metadir(&xh, pkg, props);
340+
break;
341+
case SHOW_REVDEPS:
351342
rv = show_pkg_revdeps(&xh, pkg, repo_mode);
352-
}
343+
break;
344+
}
353345

354346
xbps_end(&xh);
355347
exit(rv);

0 commit comments

Comments
 (0)