dpkg-query: Rename failures variables tracking no matches to misses
[dpkg.git] / src / query / main.c
blob66748967e6e07232fcc77afe4c4b8fb396070798
1 /*
2 * dpkg-query - program for query the dpkg database
3 * querycmd.c - status enquiry and listing options
5 * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
7 * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
8 * Copyright © 2011 Linaro Limited
9 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #include <config.h>
26 #include <compat.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #if HAVE_LOCALE_H
32 #include <locale.h>
33 #endif
34 #include <errno.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <dirent.h>
39 #include <fnmatch.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <stdio.h>
44 #include <dpkg/i18n.h>
45 #include <dpkg/dpkg.h>
46 #include <dpkg/dpkg-db.h>
47 #include <dpkg/debug.h>
48 #include <dpkg/pkg-array.h>
49 #include <dpkg/pkg-spec.h>
50 #include <dpkg/pkg-format.h>
51 #include <dpkg/pkg-show.h>
52 #include <dpkg/string.h>
53 #include <dpkg/path.h>
54 #include <dpkg/file.h>
55 #include <dpkg/pager.h>
56 #include <dpkg/options.h>
57 #include <dpkg/db-ctrl.h>
58 #include <dpkg/db-fsys.h>
60 #include "actions.h"
62 static const char *opt_showformat = "${binary:Package}\t${Version}\n";
64 static int opt_loadavail = 0;
66 static int
67 pkg_array_match_patterns(struct pkg_array *array,
68 pkg_array_visitor_func *pkg_visitor, void *pkg_data,
69 const char *const *argv)
71 int argc, i, ip, *found;
72 int misses = 0;
73 struct pkg_spec *ps;
75 for (argc = 0; argv[argc]; argc++);
76 found = m_calloc(argc, sizeof(int));
78 ps = m_malloc(sizeof(*ps) * argc);
79 for (ip = 0; ip < argc; ip++) {
80 pkg_spec_init(&ps[ip], PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD);
81 pkg_spec_parse(&ps[ip], argv[ip]);
84 for (i = 0; i < array->n_pkgs; i++) {
85 struct pkginfo *pkg;
86 bool pkg_found = false;
88 pkg = array->pkgs[i];
89 for (ip = 0; ip < argc; ip++) {
90 if (pkg_spec_match_pkg(&ps[ip], pkg, &pkg->installed)) {
91 pkg_found = true;
92 found[ip]++;
95 if (!pkg_found)
96 array->pkgs[i] = NULL;
99 pkg_array_foreach(array, pkg_visitor, pkg_data);
101 for (ip = 0; ip < argc; ip++) {
102 if (!found[ip]) {
103 notice(_("no packages found matching %s"), argv[ip]);
104 misses++;
106 pkg_spec_destroy(&ps[ip]);
109 free(ps);
110 free(found);
112 return misses;
115 struct list_format {
116 bool head;
117 int nw;
118 int vw;
119 int aw;
120 int dw;
123 static void
124 list_format_init(struct list_format *fmt, struct pkg_array *array)
126 int i;
128 if (fmt->nw != 0)
129 return;
131 fmt->nw = 14;
132 fmt->vw = 12;
133 fmt->aw = 12;
134 fmt->dw = 33;
136 for (i = 0; i < array->n_pkgs; i++) {
137 int plen, vlen, alen, dlen;
139 if (array->pkgs[i] == NULL)
140 continue;
142 plen = str_width(pkg_name(array->pkgs[i], pnaw_nonambig));
143 vlen = str_width(versiondescribe(&array->pkgs[i]->installed.version,
144 vdew_nonambig));
145 alen = str_width(dpkg_arch_describe(array->pkgs[i]->installed.arch));
146 pkg_synopsis(array->pkgs[i], &dlen);
148 if (plen > fmt->nw)
149 fmt->nw = plen;
150 if (vlen > fmt->vw)
151 fmt->vw = vlen;
152 if (alen > fmt->aw)
153 fmt->aw = alen;
154 if (dlen > fmt->dw)
155 fmt->dw = dlen;
159 static void
160 list_format_print(struct list_format *fmt,
161 int c_want, int c_status, int c_eflag,
162 const char *name, const char *version, const char *arch,
163 const char *desc, int desc_len)
165 struct str_crop_info ns, vs, as, ds;
167 str_gen_crop(name, fmt->nw, &ns);
168 str_gen_crop(version, fmt->vw, &vs);
169 str_gen_crop(arch, fmt->aw, &as);
170 str_gen_crop(desc, desc_len, &ds);
172 printf("%c%c%c %-*.*s %-*.*s %-*.*s %.*s\n", c_want, c_status, c_eflag,
173 ns.max_bytes, ns.str_bytes, name,
174 vs.max_bytes, vs.str_bytes, version,
175 as.max_bytes, as.str_bytes, arch,
176 ds.str_bytes, desc);
179 static void
180 list_format_print_header(struct list_format *fmt)
182 int l;
184 if (fmt->head)
185 return;
187 /* TRANSLATORS: This is the header that appears on 'dpkg-query -l'. The
188 * string should remain under 80 characters. The uppercase letters in
189 * the state values denote the abbreviated letter that will appear on
190 * the first three columns, which should ideally match the English one
191 * (e.g. Remove → supRimeix), see dpkg-query(1) for further details. The
192 * translated message can use additional lines if needed. */
193 fputs(_("\
194 Desired=Unknown/Install/Remove/Purge/Hold\n\
195 | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend\n\
196 |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n"), stdout);
197 list_format_print(fmt, '|', '|', '/', _("Name"), _("Version"),
198 _("Architecture"), _("Description"), fmt->dw);
200 /* Status */
201 printf("+++-");
203 /* Package name. */
204 for (l = 0; l < fmt->nw; l++)
205 printf("=");
206 printf("-");
208 /* Version. */
209 for (l = 0; l < fmt->vw; l++)
210 printf("=");
211 printf("-");
213 /* Architecture. */
214 for (l = 0; l < fmt->aw; l++)
215 printf("=");
216 printf("-");
218 /* Description. */
219 for (l = 0; l < fmt->dw; l++)
220 printf("=");
221 printf("\n");
223 fmt->head = true;
226 static void
227 pkg_array_list_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
229 struct list_format *fmt = pkg_data;
230 int l;
231 const char *pdesc;
233 list_format_init(fmt, array);
234 list_format_print_header(fmt);
236 pdesc = pkg_synopsis(pkg, &l);
237 l = min(l, fmt->dw);
239 list_format_print(fmt,
240 pkg_abbrev_want(pkg),
241 pkg_abbrev_status(pkg),
242 pkg_abbrev_eflag(pkg),
243 pkg_name(pkg, pnaw_nonambig),
244 versiondescribe(&pkg->installed.version, vdew_nonambig),
245 dpkg_arch_describe(pkg->installed.arch),
246 pdesc, l);
249 static int
250 listpackages(const char *const *argv)
252 struct pkg_array array;
253 int misses = 0;
254 struct list_format fmt;
255 struct pager *pager;
257 if (!opt_loadavail)
258 modstatdb_open(msdbrw_readonly);
259 else
260 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
262 pkg_array_init_from_hash(&array);
263 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
265 memset(&fmt, 0, sizeof(fmt));
267 pager = pager_spawn(_("showing package list on pager"));
269 if (!*argv) {
270 int i;
272 for (i = 0; i < array.n_pkgs; i++) {
273 struct pkginfo *pkg;
275 pkg = array.pkgs[i];
276 if (pkg->status == PKG_STAT_NOTINSTALLED)
277 array.pkgs[i] = NULL;
280 pkg_array_foreach(&array, pkg_array_list_item, &fmt);
281 } else {
282 misses = pkg_array_match_patterns(&array, pkg_array_list_item, &fmt, argv);
285 m_output(stdout, _("<standard output>"));
286 m_output(stderr, _("<standard error>"));
288 pager_reap(pager);
290 pkg_array_destroy(&array);
291 modstatdb_shutdown();
293 return !!misses;
296 static int
297 searchoutput(struct fsys_namenode *namenode)
299 struct fsys_node_pkgs_iter *iter;
300 struct pkginfo *pkg_owner;
301 int found;
303 if (namenode->divert) {
304 const char *name_from = namenode->divert->camefrom ?
305 namenode->divert->camefrom->name : namenode->name;
306 const char *name_to = namenode->divert->useinstead ?
307 namenode->divert->useinstead->name : namenode->name;
309 if (namenode->divert->pkgset) {
310 printf(_("diversion by %s from: %s\n"),
311 namenode->divert->pkgset->name, name_from);
312 printf(_("diversion by %s to: %s\n"),
313 namenode->divert->pkgset->name, name_to);
314 } else {
315 printf(_("local diversion from: %s\n"), name_from);
316 printf(_("local diversion to: %s\n"), name_to);
319 found= 0;
321 iter = fsys_node_pkgs_iter_new(namenode);
322 while ((pkg_owner = fsys_node_pkgs_iter_next(iter))) {
323 if (found)
324 fputs(", ", stdout);
325 fputs(pkg_name(pkg_owner, pnaw_nonambig), stdout);
326 found++;
328 fsys_node_pkgs_iter_free(iter);
330 if (found) printf(": %s\n",namenode->name);
331 return found + (namenode->divert ? 1 : 0);
334 static int
335 searchfiles(const char *const *argv)
337 const char *thisarg;
338 int misses = 0;
339 struct varbuf path = VARBUF_INIT;
340 static struct varbuf vb;
342 if (!*argv)
343 badusage(_("--search needs at least one file name pattern argument"));
345 modstatdb_open(msdbrw_readonly);
346 ensure_allinstfiles_available_quiet();
347 ensure_diversions();
349 while ((thisarg = *argv++) != NULL) {
350 struct fsys_namenode *namenode;
351 int found = 0;
353 if (!strchr("*[?/",*thisarg)) {
354 varbuf_reset(&vb);
355 varbuf_add_char(&vb, '*');
356 varbuf_add_str(&vb, thisarg);
357 varbuf_add_char(&vb, '*');
358 varbuf_end_str(&vb);
359 thisarg= vb.buf;
361 if (!strpbrk(thisarg, "*[?\\")) {
362 /* Trim trailing ‘/’ and ‘/.’ from the argument if it is not
363 * a pattern, just a pathname. */
364 varbuf_set_str(&path, thisarg);
365 varbuf_trunc(&path, path_trim_slash_slashdot(path.buf));
367 namenode = fsys_hash_find_node(path.buf, FHFF_NONE);
368 found += searchoutput(namenode);
369 } else {
370 struct fsys_hash_iter *iter;
372 iter = fsys_hash_iter_new();
373 while ((namenode = fsys_hash_iter_next(iter)) != NULL) {
374 if (fnmatch(thisarg,namenode->name,0)) continue;
375 found+= searchoutput(namenode);
377 fsys_hash_iter_free(iter);
379 if (!found) {
380 notice(_("no path found matching pattern %s"), thisarg);
381 misses++;
382 m_output(stderr, _("<standard error>"));
383 } else {
384 m_output(stdout, _("<standard output>"));
387 modstatdb_shutdown();
389 varbuf_destroy(&path);
391 return !!misses;
394 static int
395 print_status(const char *const *argv)
397 int misses = 0;
399 modstatdb_open(msdbrw_readonly);
401 if (!*argv) {
402 writedb_stanzas(stdout, _("<standard output>"), 0);
403 } else {
404 const char *thisarg;
406 while ((thisarg = *argv++) != NULL) {
407 struct pkginfo *pkg;
409 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
411 if (pkg->status == PKG_STAT_NOTINSTALLED &&
412 pkg->priority == PKG_PRIO_UNKNOWN &&
413 str_is_unset(pkg->section) &&
414 !pkg->archives &&
415 pkg->want == PKG_WANT_UNKNOWN &&
416 !pkg_is_informative(pkg, &pkg->installed)) {
417 notice(_("package '%s' is not installed and no information is available"),
418 pkg_name(pkg, pnaw_nonambig));
419 misses++;
420 } else {
421 write_stanza(stdout, _("<standard output>"), pkg, &pkg->installed);
424 if (*argv != NULL)
425 putchar('\n');
429 m_output(stdout, _("<standard output>"));
430 if (misses) {
431 fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files.\n"),
432 stderr);
433 m_output(stderr, _("<standard error>"));
436 modstatdb_shutdown();
438 return !!misses;
441 static int
442 print_avail(const char *const *argv)
444 int misses = 0;
446 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
448 if (!*argv) {
449 writedb_stanzas(stdout, _("<standard output>"), wdb_dump_available);
450 } else {
451 const char *thisarg;
453 while ((thisarg = *argv++) != NULL) {
454 struct pkginfo *pkg;
456 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
458 if (!pkg_is_informative(pkg, &pkg->available)) {
459 notice(_("package '%s' is not available"),
460 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
461 misses++;
462 } else {
463 write_stanza(stdout, _("<standard output>"), pkg, &pkg->available);
466 if (*argv != NULL)
467 putchar('\n');
471 m_output(stdout, _("<standard output>"));
472 if (misses)
473 m_output(stderr, _("<standard error>"));
475 modstatdb_shutdown();
477 return !!misses;
480 static int
481 list_files(const char *const *argv)
483 const char *thisarg;
484 struct fsys_namenode_list *file;
485 struct pkginfo *pkg;
486 struct fsys_namenode *namenode;
487 int misses = 0;
489 if (!*argv)
490 badusage(_("--%s needs at least one package name argument"), cipaction->olong);
492 modstatdb_open(msdbrw_readonly);
494 while ((thisarg = *argv++) != NULL) {
495 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
497 switch (pkg->status) {
498 case PKG_STAT_NOTINSTALLED:
499 notice(_("package '%s' is not installed"),
500 pkg_name(pkg, pnaw_nonambig));
501 misses++;
502 break;
503 default:
504 ensure_packagefiles_available(pkg);
505 ensure_diversions();
506 file = pkg->files;
507 if (!file) {
508 printf(_("Package '%s' does not contain any files (!)\n"),
509 pkg_name(pkg, pnaw_nonambig));
510 } else {
511 while (file) {
512 namenode = file->namenode;
513 puts(namenode->name);
514 if (namenode->divert && !namenode->divert->camefrom) {
515 if (!namenode->divert->pkgset)
516 printf(_("locally diverted to: %s\n"),
517 namenode->divert->useinstead->name);
518 else if (pkg->set == namenode->divert->pkgset)
519 printf(_("package diverts others to: %s\n"),
520 namenode->divert->useinstead->name);
521 else
522 printf(_("diverted by %s to: %s\n"),
523 namenode->divert->pkgset->name,
524 namenode->divert->useinstead->name);
526 file = file->next;
529 break;
532 if (*argv != NULL)
533 putchar('\n');
536 m_output(stdout, _("<standard output>"));
537 if (misses) {
538 fputs(_("Use dpkg --contents (= dpkg-deb --contents) to list archive files contents.\n"),
539 stderr);
540 m_output(stderr, _("<standard error>"));
543 modstatdb_shutdown();
545 return !!misses;
548 static void
549 pkg_array_load_db_fsys(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
551 ensure_packagefiles_available(pkg);
554 static void
555 pkg_array_show_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
557 struct pkg_format_node *fmt = pkg_data;
559 pkg_format_show(fmt, pkg, &pkg->installed);
562 static int
563 showpackages(const char *const *argv)
565 struct dpkg_error err;
566 struct pkg_array array;
567 struct pkg_format_node *fmt;
568 bool fmt_needs_db_fsys;
569 int misses = 0;
571 fmt = pkg_format_parse(opt_showformat, &err);
572 if (!fmt) {
573 notice(_("error in show format: %s"), err.str);
574 dpkg_error_destroy(&err);
575 return 2;
578 fmt_needs_db_fsys = pkg_format_needs_db_fsys(fmt);
580 if (!opt_loadavail)
581 modstatdb_open(msdbrw_readonly);
582 else
583 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
585 pkg_array_init_from_hash(&array);
586 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
588 if (!*argv) {
589 int i;
591 if (fmt_needs_db_fsys)
592 ensure_allinstfiles_available_quiet();
593 for (i = 0; i < array.n_pkgs; i++) {
594 struct pkginfo *pkg;
596 pkg = array.pkgs[i];
597 if (pkg->status == PKG_STAT_NOTINSTALLED)
598 continue;
599 pkg_format_show(fmt, pkg, &pkg->installed);
601 } else {
602 if (fmt_needs_db_fsys)
603 pkg_array_foreach(&array, pkg_array_load_db_fsys, NULL);
604 misses = pkg_array_match_patterns(&array, pkg_array_show_item, fmt, argv);
607 m_output(stdout, _("<standard output>"));
608 m_output(stderr, _("<standard error>"));
610 pkg_array_destroy(&array);
611 pkg_format_free(fmt);
612 modstatdb_shutdown();
614 return !!misses;
617 static bool
618 pkg_infodb_is_internal(const char *filetype)
620 /* Do not expose internal database files. */
621 if (strcmp(filetype, LISTFILE) == 0 ||
622 strcmp(filetype, CONFFILESFILE) == 0)
623 return true;
625 if (strlen(filetype) > MAXCONTROLFILENAME)
626 return true;
628 return false;
631 static void
632 pkg_infodb_check_filetype(const char *filetype)
634 const char *c;
636 /* Validate control file name for sanity. */
637 for (c = "/."; *c; c++)
638 if (strchr(filetype, *c))
639 badusage(_("control file contains %c"), *c);
642 static void
643 pkg_infodb_print_filename(const char *filename, const char *filetype)
645 if (pkg_infodb_is_internal(filetype))
646 return;
648 printf("%s\n", filename);
651 static void
652 pkg_infodb_print_filetype(const char *filename, const char *filetype)
654 if (pkg_infodb_is_internal(filetype))
655 return;
657 printf("%s\n", filetype);
660 static void
661 control_path_file(struct pkginfo *pkg, const char *control_file)
663 const char *control_pathname;
664 struct stat st;
666 control_pathname = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
667 if (stat(control_pathname, &st) < 0)
668 return;
669 if (!S_ISREG(st.st_mode))
670 return;
672 pkg_infodb_print_filename(control_pathname, control_file);
675 static int
676 control_path(const char *const *argv)
678 struct pkginfo *pkg;
679 const char *pkgname;
680 const char *control_file;
682 pkgname = *argv++;
683 if (!pkgname)
684 badusage(_("--%s needs at least one package name argument"),
685 cipaction->olong);
687 control_file = *argv++;
688 if (control_file && *argv)
689 badusage(_("--%s takes at most two arguments"), cipaction->olong);
691 if (control_file)
692 pkg_infodb_check_filetype(control_file);
694 modstatdb_open(msdbrw_readonly);
696 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
697 if (pkg->status == PKG_STAT_NOTINSTALLED)
698 ohshit(_("package '%s' is not installed"),
699 pkg_name(pkg, pnaw_nonambig));
701 if (control_file)
702 control_path_file(pkg, control_file);
703 else
704 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filename);
706 modstatdb_shutdown();
708 return 0;
711 static int
712 control_list(const char *const *argv)
714 struct pkginfo *pkg;
715 const char *pkgname;
717 pkgname = *argv++;
718 if (!pkgname || *argv)
719 badusage(_("--%s takes one package name argument"), cipaction->olong);
721 modstatdb_open(msdbrw_readonly);
723 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
724 if (pkg->status == PKG_STAT_NOTINSTALLED)
725 ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
727 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filetype);
729 modstatdb_shutdown();
731 return 0;
734 static int
735 control_show(const char *const *argv)
737 struct pkginfo *pkg;
738 const char *pkgname;
739 const char *filename;
740 const char *control_file;
742 pkgname = *argv++;
743 if (!pkgname || !*argv)
744 badusage(_("--%s takes exactly two arguments"),
745 cipaction->olong);
747 control_file = *argv++;
748 if (!control_file || *argv)
749 badusage(_("--%s takes exactly two arguments"), cipaction->olong);
751 pkg_infodb_check_filetype(control_file);
753 modstatdb_open(msdbrw_readonly);
755 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
756 if (pkg->status == PKG_STAT_NOTINSTALLED)
757 ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
759 if (pkg_infodb_has_file(pkg, &pkg->installed, control_file))
760 filename = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
761 else
762 ohshit(_("control file '%s' does not exist"), control_file);
764 modstatdb_shutdown();
766 file_show(filename);
768 return 0;
771 static void
772 set_no_pager(const struct cmdinfo *ci, const char *value)
774 pager_enable(false);
777 static int
778 printversion(const char *const *argv)
780 printf(_("Debian %s package management program query tool version %s.\n"),
781 DPKGQUERY, PACKAGE_RELEASE);
782 printf(_(
783 "This is free software; see the GNU General Public License version 2 or\n"
784 "later for copying conditions. There is NO warranty.\n"));
786 m_output(stdout, _("<standard output>"));
788 return 0;
791 static int
792 usage(const char *const *argv)
794 printf(_(
795 "Usage: %s [<option>...] <command>\n"
796 "\n"), DPKGQUERY);
798 printf(_(
799 "Commands:\n"
800 " -s, --status [<package>...] Display package status details.\n"
801 " -p, --print-avail [<package>...] Display available version details.\n"
802 " -L, --listfiles <package>... List files 'owned' by package(s).\n"
803 " -l, --list [<pattern>...] List packages concisely.\n"
804 " -W, --show [<pattern>...] Show information on package(s).\n"
805 " -S, --search <pattern>... Find package(s) owning file(s).\n"
806 " --control-list <package> Print the package control file list.\n"
807 " --control-show <package> <file>\n"
808 " Show the package control file.\n"
809 " -c, --control-path <package> [<file>]\n"
810 " Print path for package control file.\n"
811 "\n"));
813 printf(_(
814 " -?, --help Show this help message.\n"
815 " --version Show the version.\n"
816 "\n"));
818 printf(_(
819 "Options:\n"
820 " --admindir=<directory> Use <directory> instead of %s.\n"
821 " --root=<directory> Use <directory> instead of %s.\n"
822 " --load-avail Use available file on --show and --list.\n"
823 " --no-pager Disables the use of any pager.\n"
824 " -f|--showformat=<format> Use alternative format for --show.\n"
825 "\n"), ADMINDIR, "/");
827 printf(_(
828 "Format syntax:\n"
829 " A format is a string that will be output for each package. The format\n"
830 " can include the standard escape sequences \\n (newline), \\r (carriage\n"
831 " return) or \\\\ (plain backslash). Package information can be included\n"
832 " by inserting variable references to package fields using the ${var[;width]}\n"
833 " syntax. Fields will be right-aligned unless the width is negative in which\n"
834 " case left alignment will be used.\n"));
836 m_output(stdout, _("<standard output>"));
838 return 0;
841 static const char printforhelp[] = N_(
842 "Use --help for help about querying packages.");
844 /* This table has both the action entries in it and the normal options.
845 * The action entries are made with the ACTION macro, as they all
846 * have a very similar structure. */
847 static const struct cmdinfo cmdinfos[]= {
848 ACTION( "listfiles", 'L', act_listfiles, list_files ),
849 ACTION( "status", 's', act_status, print_status ),
850 ACTION( "print-avail", 'p', act_printavail, print_avail ),
851 ACTION( "list", 'l', act_listpackages, listpackages ),
852 ACTION( "search", 'S', act_searchfiles, searchfiles ),
853 ACTION( "show", 'W', act_listpackages, showpackages ),
854 ACTION( "control-path", 'c', act_controlpath, control_path ),
855 ACTION( "control-list", 0, act_controllist, control_list ),
856 ACTION( "control-show", 0, act_controlshow, control_show ),
857 ACTION( "help", '?', act_help, usage ),
858 ACTION( "version", 0, act_version, printversion ),
860 { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
861 { "root", 0, 1, NULL, NULL, set_root, 0 },
862 { "load-avail", 0, 0, &opt_loadavail, NULL, NULL, 1 },
863 { "showformat", 'f', 1, NULL, &opt_showformat, NULL },
864 { "no-pager", 0, 0, NULL, NULL, set_no_pager },
865 { NULL, 0, 0, NULL, NULL, NULL }
868 int main(int argc, const char *const *argv) {
869 int ret;
871 dpkg_set_report_piped_mode(_IOFBF);
872 dpkg_locales_init(PACKAGE);
873 dpkg_program_init("dpkg-query");
874 dpkg_options_parse(&argv, cmdinfos, printforhelp);
876 debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
878 if (!cipaction) badusage(_("need an action option"));
880 ret = cipaction->action(argv);
882 dpkg_program_done();
883 dpkg_locales_done();
885 return ret;