doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / src / query / main.c
blob24dccc8c4f764eaa15ea85af1e35320d4d1d88a3
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 rc = 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 rc++;
106 pkg_spec_destroy(&ps[ip]);
109 free(ps);
110 free(found);
112 return rc;
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 rc = 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 rc = 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 rc;
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 failures = 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_reset(&path);
365 varbuf_add_str(&path, thisarg);
366 varbuf_end_str(&path);
367 varbuf_trunc(&path, path_trim_slash_slashdot(path.buf));
369 namenode = fsys_hash_find_node(path.buf, 0);
370 found += searchoutput(namenode);
371 } else {
372 struct fsys_hash_iter *iter;
374 iter = fsys_hash_iter_new();
375 while ((namenode = fsys_hash_iter_next(iter)) != NULL) {
376 if (fnmatch(thisarg,namenode->name,0)) continue;
377 found+= searchoutput(namenode);
379 fsys_hash_iter_free(iter);
381 if (!found) {
382 notice(_("no path found matching pattern %s"), thisarg);
383 failures++;
384 m_output(stderr, _("<standard error>"));
385 } else {
386 m_output(stdout, _("<standard output>"));
389 modstatdb_shutdown();
391 varbuf_destroy(&path);
393 return failures;
396 static int
397 print_status(const char *const *argv)
399 int failures = 0;
401 modstatdb_open(msdbrw_readonly);
403 if (!*argv) {
404 writedb_stanzas(stdout, _("<standard output>"), 0);
405 } else {
406 const char *thisarg;
408 while ((thisarg = *argv++) != NULL) {
409 struct pkginfo *pkg;
411 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
413 if (pkg->status == PKG_STAT_NOTINSTALLED &&
414 pkg->priority == PKG_PRIO_UNKNOWN &&
415 str_is_unset(pkg->section) &&
416 !pkg->archives &&
417 pkg->want == PKG_WANT_UNKNOWN &&
418 !pkg_is_informative(pkg, &pkg->installed)) {
419 notice(_("package '%s' is not installed and no information is available"),
420 pkg_name(pkg, pnaw_nonambig));
421 failures++;
422 } else {
423 write_stanza(stdout, _("<standard output>"), pkg, &pkg->installed);
426 if (*argv != NULL)
427 putchar('\n');
431 m_output(stdout, _("<standard output>"));
432 if (failures) {
433 fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files.\n"),
434 stderr);
435 m_output(stderr, _("<standard error>"));
438 modstatdb_shutdown();
440 return failures;
443 static int
444 print_avail(const char *const *argv)
446 int failures = 0;
448 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
450 if (!*argv) {
451 writedb_stanzas(stdout, _("<standard output>"), wdb_dump_available);
452 } else {
453 const char *thisarg;
455 while ((thisarg = *argv++) != NULL) {
456 struct pkginfo *pkg;
458 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
460 if (!pkg_is_informative(pkg, &pkg->available)) {
461 notice(_("package '%s' is not available"),
462 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
463 failures++;
464 } else {
465 write_stanza(stdout, _("<standard output>"), pkg, &pkg->available);
468 if (*argv != NULL)
469 putchar('\n');
473 m_output(stdout, _("<standard output>"));
474 if (failures)
475 m_output(stderr, _("<standard error>"));
477 modstatdb_shutdown();
479 return failures;
482 static int
483 list_files(const char *const *argv)
485 const char *thisarg;
486 struct fsys_namenode_list *file;
487 struct pkginfo *pkg;
488 struct fsys_namenode *namenode;
489 int failures = 0;
491 if (!*argv)
492 badusage(_("--%s needs at least one package name argument"), cipaction->olong);
494 modstatdb_open(msdbrw_readonly);
496 while ((thisarg = *argv++) != NULL) {
497 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
499 switch (pkg->status) {
500 case PKG_STAT_NOTINSTALLED:
501 notice(_("package '%s' is not installed"),
502 pkg_name(pkg, pnaw_nonambig));
503 failures++;
504 break;
505 default:
506 ensure_packagefiles_available(pkg);
507 ensure_diversions();
508 file = pkg->files;
509 if (!file) {
510 printf(_("Package '%s' does not contain any files (!)\n"),
511 pkg_name(pkg, pnaw_nonambig));
512 } else {
513 while (file) {
514 namenode = file->namenode;
515 puts(namenode->name);
516 if (namenode->divert && !namenode->divert->camefrom) {
517 if (!namenode->divert->pkgset)
518 printf(_("locally diverted to: %s\n"),
519 namenode->divert->useinstead->name);
520 else if (pkg->set == namenode->divert->pkgset)
521 printf(_("package diverts others to: %s\n"),
522 namenode->divert->useinstead->name);
523 else
524 printf(_("diverted by %s to: %s\n"),
525 namenode->divert->pkgset->name,
526 namenode->divert->useinstead->name);
528 file = file->next;
531 break;
534 if (*argv != NULL)
535 putchar('\n');
538 m_output(stdout, _("<standard output>"));
539 if (failures) {
540 fputs(_("Use dpkg --contents (= dpkg-deb --contents) to list archive files contents.\n"),
541 stderr);
542 m_output(stderr, _("<standard error>"));
545 modstatdb_shutdown();
547 return failures;
550 static void
551 pkg_array_load_db_fsys(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
553 ensure_packagefiles_available(pkg);
556 static void
557 pkg_array_show_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
559 struct pkg_format_node *fmt = pkg_data;
561 pkg_format_show(fmt, pkg, &pkg->installed);
564 static int
565 showpackages(const char *const *argv)
567 struct dpkg_error err;
568 struct pkg_array array;
569 struct pkg_format_node *fmt;
570 bool fmt_needs_db_fsys;
571 int rc = 0;
573 fmt = pkg_format_parse(opt_showformat, &err);
574 if (!fmt) {
575 notice(_("error in show format: %s"), err.str);
576 dpkg_error_destroy(&err);
577 rc++;
578 return rc;
581 fmt_needs_db_fsys = pkg_format_needs_db_fsys(fmt);
583 if (!opt_loadavail)
584 modstatdb_open(msdbrw_readonly);
585 else
586 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
588 pkg_array_init_from_hash(&array);
589 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
591 if (!*argv) {
592 int i;
594 if (fmt_needs_db_fsys)
595 ensure_allinstfiles_available_quiet();
596 for (i = 0; i < array.n_pkgs; i++) {
597 struct pkginfo *pkg;
599 pkg = array.pkgs[i];
600 if (pkg->status == PKG_STAT_NOTINSTALLED)
601 continue;
602 pkg_format_show(fmt, pkg, &pkg->installed);
604 } else {
605 if (fmt_needs_db_fsys)
606 pkg_array_foreach(&array, pkg_array_load_db_fsys, NULL);
607 rc = pkg_array_match_patterns(&array, pkg_array_show_item, fmt, argv);
610 m_output(stdout, _("<standard output>"));
611 m_output(stderr, _("<standard error>"));
613 pkg_array_destroy(&array);
614 pkg_format_free(fmt);
615 modstatdb_shutdown();
617 return rc;
620 static bool
621 pkg_infodb_is_internal(const char *filetype)
623 /* Do not expose internal database files. */
624 if (strcmp(filetype, LISTFILE) == 0 ||
625 strcmp(filetype, CONFFILESFILE) == 0)
626 return true;
628 if (strlen(filetype) > MAXCONTROLFILENAME)
629 return true;
631 return false;
634 static void
635 pkg_infodb_check_filetype(const char *filetype)
637 const char *c;
639 /* Validate control file name for sanity. */
640 for (c = "/."; *c; c++)
641 if (strchr(filetype, *c))
642 badusage(_("control file contains %c"), *c);
645 static void
646 pkg_infodb_print_filename(const char *filename, const char *filetype)
648 if (pkg_infodb_is_internal(filetype))
649 return;
651 printf("%s\n", filename);
654 static void
655 pkg_infodb_print_filetype(const char *filename, const char *filetype)
657 if (pkg_infodb_is_internal(filetype))
658 return;
660 printf("%s\n", filetype);
663 static void
664 control_path_file(struct pkginfo *pkg, const char *control_file)
666 const char *control_pathname;
667 struct stat st;
669 control_pathname = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
670 if (stat(control_pathname, &st) < 0)
671 return;
672 if (!S_ISREG(st.st_mode))
673 return;
675 pkg_infodb_print_filename(control_pathname, control_file);
678 static int
679 control_path(const char *const *argv)
681 struct pkginfo *pkg;
682 const char *pkgname;
683 const char *control_file;
685 pkgname = *argv++;
686 if (!pkgname)
687 badusage(_("--%s needs at least one package name argument"),
688 cipaction->olong);
690 control_file = *argv++;
691 if (control_file && *argv)
692 badusage(_("--%s takes at most two arguments"), cipaction->olong);
694 if (control_file)
695 pkg_infodb_check_filetype(control_file);
697 modstatdb_open(msdbrw_readonly);
699 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
700 if (pkg->status == PKG_STAT_NOTINSTALLED)
701 ohshit(_("package '%s' is not installed"),
702 pkg_name(pkg, pnaw_nonambig));
704 if (control_file)
705 control_path_file(pkg, control_file);
706 else
707 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filename);
709 modstatdb_shutdown();
711 return 0;
714 static int
715 control_list(const char *const *argv)
717 struct pkginfo *pkg;
718 const char *pkgname;
720 pkgname = *argv++;
721 if (!pkgname || *argv)
722 badusage(_("--%s takes one package name argument"), cipaction->olong);
724 modstatdb_open(msdbrw_readonly);
726 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
727 if (pkg->status == PKG_STAT_NOTINSTALLED)
728 ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
730 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filetype);
732 modstatdb_shutdown();
734 return 0;
737 static int
738 control_show(const char *const *argv)
740 struct pkginfo *pkg;
741 const char *pkgname;
742 const char *filename;
743 const char *control_file;
745 pkgname = *argv++;
746 if (!pkgname || !*argv)
747 badusage(_("--%s takes exactly two arguments"),
748 cipaction->olong);
750 control_file = *argv++;
751 if (!control_file || *argv)
752 badusage(_("--%s takes exactly two arguments"), cipaction->olong);
754 pkg_infodb_check_filetype(control_file);
756 modstatdb_open(msdbrw_readonly);
758 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
759 if (pkg->status == PKG_STAT_NOTINSTALLED)
760 ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
762 if (pkg_infodb_has_file(pkg, &pkg->installed, control_file))
763 filename = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
764 else
765 ohshit(_("control file '%s' does not exist"), control_file);
767 modstatdb_shutdown();
769 file_show(filename);
771 return 0;
774 static void
775 set_no_pager(const struct cmdinfo *ci, const char *value)
777 pager_enable(false);
780 static int
781 printversion(const char *const *argv)
783 printf(_("Debian %s package management program query tool version %s.\n"),
784 DPKGQUERY, PACKAGE_RELEASE);
785 printf(_(
786 "This is free software; see the GNU General Public License version 2 or\n"
787 "later for copying conditions. There is NO warranty.\n"));
789 m_output(stdout, _("<standard output>"));
791 return 0;
794 static int
795 usage(const char *const *argv)
797 printf(_(
798 "Usage: %s [<option>...] <command>\n"
799 "\n"), DPKGQUERY);
801 printf(_(
802 "Commands:\n"
803 " -s, --status [<package>...] Display package status details.\n"
804 " -p, --print-avail [<package>...] Display available version details.\n"
805 " -L, --listfiles <package>... List files 'owned' by package(s).\n"
806 " -l, --list [<pattern>...] List packages concisely.\n"
807 " -W, --show [<pattern>...] Show information on package(s).\n"
808 " -S, --search <pattern>... Find package(s) owning file(s).\n"
809 " --control-list <package> Print the package control file list.\n"
810 " --control-show <package> <file>\n"
811 " Show the package control file.\n"
812 " -c, --control-path <package> [<file>]\n"
813 " Print path for package control file.\n"
814 "\n"));
816 printf(_(
817 " -?, --help Show this help message.\n"
818 " --version Show the version.\n"
819 "\n"));
821 printf(_(
822 "Options:\n"
823 " --admindir=<directory> Use <directory> instead of %s.\n"
824 " --root=<directory> Use <directory> instead of %s.\n"
825 " --load-avail Use available file on --show and --list.\n"
826 " --no-pager Disables the use of any pager.\n"
827 " -f|--showformat=<format> Use alternative format for --show.\n"
828 "\n"), ADMINDIR, "/");
830 printf(_(
831 "Format syntax:\n"
832 " A format is a string that will be output for each package. The format\n"
833 " can include the standard escape sequences \\n (newline), \\r (carriage\n"
834 " return) or \\\\ (plain backslash). Package information can be included\n"
835 " by inserting variable references to package fields using the ${var[;width]}\n"
836 " syntax. Fields will be right-aligned unless the width is negative in which\n"
837 " case left alignment will be used.\n"));
839 m_output(stdout, _("<standard output>"));
841 return 0;
844 static const char printforhelp[] = N_(
845 "Use --help for help about querying packages.");
847 /* This table has both the action entries in it and the normal options.
848 * The action entries are made with the ACTION macro, as they all
849 * have a very similar structure. */
850 static const struct cmdinfo cmdinfos[]= {
851 ACTION( "listfiles", 'L', act_listfiles, list_files ),
852 ACTION( "status", 's', act_status, print_status ),
853 ACTION( "print-avail", 'p', act_printavail, print_avail ),
854 ACTION( "list", 'l', act_listpackages, listpackages ),
855 ACTION( "search", 'S', act_searchfiles, searchfiles ),
856 ACTION( "show", 'W', act_listpackages, showpackages ),
857 ACTION( "control-path", 'c', act_controlpath, control_path ),
858 ACTION( "control-list", 0, act_controllist, control_list ),
859 ACTION( "control-show", 0, act_controlshow, control_show ),
860 ACTION( "help", '?', act_help, usage ),
861 ACTION( "version", 0, act_version, printversion ),
863 { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
864 { "root", 0, 1, NULL, NULL, set_root, 0 },
865 { "load-avail", 0, 0, &opt_loadavail, NULL, NULL, 1 },
866 { "showformat", 'f', 1, NULL, &opt_showformat, NULL },
867 { "no-pager", 0, 0, NULL, NULL, set_no_pager },
868 { NULL, 0, 0, NULL, NULL, NULL }
871 int main(int argc, const char *const *argv) {
872 int ret;
874 dpkg_set_report_piped_mode(_IOFBF);
875 dpkg_locales_init(PACKAGE);
876 dpkg_program_init("dpkg-query");
877 dpkg_options_parse(&argv, cmdinfos, printforhelp);
879 debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
881 if (!cipaction) badusage(_("need an action option"));
883 ret = cipaction->action(argv);
885 dpkg_program_done();
886 dpkg_locales_done();
888 return !!ret;