dpkg-trigger: Initialize opt_act to 1
[dpkg.git] / src / query / main.c
blobc6259574e060125a2833d22e4b4a16439bd0a3aa
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 #ifdef 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 thisarg= vb.buf;
360 if (!strpbrk(thisarg, "*[?\\")) {
361 /* Trim trailing ‘/’ and ‘/.’ from the argument if it is not
362 * a pattern, just a pathname. */
363 varbuf_set_str(&path, thisarg);
364 varbuf_trunc(&path, path_trim_slash_slashdot(path.buf));
366 namenode = fsys_hash_find_node(path.buf, FHFF_NONE);
367 found += searchoutput(namenode);
368 } else {
369 struct fsys_hash_iter *iter;
371 iter = fsys_hash_iter_new();
372 while ((namenode = fsys_hash_iter_next(iter)) != NULL) {
373 if (fnmatch(thisarg,namenode->name,0)) continue;
374 found+= searchoutput(namenode);
376 fsys_hash_iter_free(iter);
378 if (!found) {
379 notice(_("no path found matching pattern %s"), thisarg);
380 misses++;
381 m_output(stderr, _("<standard error>"));
382 } else {
383 m_output(stdout, _("<standard output>"));
386 modstatdb_shutdown();
388 varbuf_destroy(&path);
390 return !!misses;
393 static int
394 print_status(const char *const *argv)
396 int misses = 0;
398 modstatdb_open(msdbrw_readonly);
400 if (!*argv) {
401 writedb_stanzas(stdout, _("<standard output>"), 0);
402 } else {
403 const char *thisarg;
405 while ((thisarg = *argv++) != NULL) {
406 struct pkginfo *pkg;
408 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
410 if (pkg->status == PKG_STAT_NOTINSTALLED &&
411 pkg->priority == PKG_PRIO_UNKNOWN &&
412 str_is_unset(pkg->section) &&
413 !pkg->archives &&
414 pkg->want == PKG_WANT_UNKNOWN &&
415 !pkg_is_informative(pkg, &pkg->installed)) {
416 notice(_("package '%s' is not installed and no information is available"),
417 pkg_name(pkg, pnaw_nonambig));
418 misses++;
419 } else {
420 write_stanza(stdout, _("<standard output>"), pkg, &pkg->installed);
423 if (*argv != NULL)
424 putchar('\n');
428 m_output(stdout, _("<standard output>"));
429 if (misses) {
430 fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files.\n"),
431 stderr);
432 m_output(stderr, _("<standard error>"));
435 modstatdb_shutdown();
437 return !!misses;
440 static int
441 print_avail(const char *const *argv)
443 int misses = 0;
445 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
447 if (!*argv) {
448 writedb_stanzas(stdout, _("<standard output>"), wdb_dump_available);
449 } else {
450 const char *thisarg;
452 while ((thisarg = *argv++) != NULL) {
453 struct pkginfo *pkg;
455 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
457 if (!pkg_is_informative(pkg, &pkg->available)) {
458 notice(_("package '%s' is not available"),
459 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
460 misses++;
461 } else {
462 write_stanza(stdout, _("<standard output>"), pkg, &pkg->available);
465 if (*argv != NULL)
466 putchar('\n');
470 m_output(stdout, _("<standard output>"));
471 if (misses)
472 m_output(stderr, _("<standard error>"));
474 modstatdb_shutdown();
476 return !!misses;
479 static int
480 list_files(const char *const *argv)
482 const char *thisarg;
483 struct fsys_namenode_list *file;
484 struct pkginfo *pkg;
485 struct fsys_namenode *namenode;
486 int misses = 0;
488 if (!*argv)
489 badusage(_("--%s needs at least one package name argument"), cipaction->olong);
491 modstatdb_open(msdbrw_readonly);
493 while ((thisarg = *argv++) != NULL) {
494 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
496 switch (pkg->status) {
497 case PKG_STAT_NOTINSTALLED:
498 notice(_("package '%s' is not installed"),
499 pkg_name(pkg, pnaw_nonambig));
500 misses++;
501 break;
502 default:
503 ensure_packagefiles_available(pkg);
504 ensure_diversions();
505 file = pkg->files;
506 if (!file) {
507 printf(_("Package '%s' does not contain any files (!)\n"),
508 pkg_name(pkg, pnaw_nonambig));
509 } else {
510 while (file) {
511 namenode = file->namenode;
512 puts(namenode->name);
513 if (namenode->divert && !namenode->divert->camefrom) {
514 if (!namenode->divert->pkgset)
515 printf(_("locally diverted to: %s\n"),
516 namenode->divert->useinstead->name);
517 else if (pkg->set == namenode->divert->pkgset)
518 printf(_("package diverts others to: %s\n"),
519 namenode->divert->useinstead->name);
520 else
521 printf(_("diverted by %s to: %s\n"),
522 namenode->divert->pkgset->name,
523 namenode->divert->useinstead->name);
525 file = file->next;
528 break;
531 if (*argv != NULL)
532 putchar('\n');
535 m_output(stdout, _("<standard output>"));
536 if (misses) {
537 fputs(_("Use dpkg --contents (= dpkg-deb --contents) to list archive files contents.\n"),
538 stderr);
539 m_output(stderr, _("<standard error>"));
542 modstatdb_shutdown();
544 return !!misses;
547 static void
548 pkg_array_load_db_fsys(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
550 ensure_packagefiles_available(pkg);
553 static void
554 pkg_array_show_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
556 struct pkg_format_node *fmt = pkg_data;
558 pkg_format_show(fmt, pkg, &pkg->installed);
561 static int
562 showpackages(const char *const *argv)
564 struct dpkg_error err;
565 struct pkg_array array;
566 struct pkg_format_node *fmt;
567 bool fmt_needs_db_fsys;
568 int misses = 0;
570 fmt = pkg_format_parse(opt_showformat, &err);
571 if (!fmt) {
572 notice(_("error in show format: %s"), err.str);
573 dpkg_error_destroy(&err);
574 return 2;
577 fmt_needs_db_fsys = pkg_format_needs_db_fsys(fmt);
579 if (!opt_loadavail)
580 modstatdb_open(msdbrw_readonly);
581 else
582 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
584 pkg_array_init_from_hash(&array);
585 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
587 if (!*argv) {
588 int i;
590 if (fmt_needs_db_fsys)
591 ensure_allinstfiles_available_quiet();
592 for (i = 0; i < array.n_pkgs; i++) {
593 struct pkginfo *pkg;
595 pkg = array.pkgs[i];
596 if (pkg->status == PKG_STAT_NOTINSTALLED)
597 continue;
598 pkg_format_show(fmt, pkg, &pkg->installed);
600 } else {
601 if (fmt_needs_db_fsys)
602 pkg_array_foreach(&array, pkg_array_load_db_fsys, NULL);
603 misses = pkg_array_match_patterns(&array, pkg_array_show_item, fmt, argv);
606 m_output(stdout, _("<standard output>"));
607 m_output(stderr, _("<standard error>"));
609 pkg_array_destroy(&array);
610 pkg_format_free(fmt);
611 modstatdb_shutdown();
613 return !!misses;
616 static bool
617 pkg_infodb_is_internal(const char *filetype)
619 /* Do not expose internal database files. */
620 if (strcmp(filetype, LISTFILE) == 0 ||
621 strcmp(filetype, CONFFILESFILE) == 0)
622 return true;
624 if (strlen(filetype) > MAXCONTROLFILENAME)
625 return true;
627 return false;
630 static void
631 pkg_infodb_check_filetype(const char *filetype)
633 const char *c;
635 /* Validate control file name for sanity. */
636 for (c = "/."; *c; c++)
637 if (strchr(filetype, *c))
638 badusage(_("control file contains %c"), *c);
641 static void
642 pkg_infodb_print_filename(const char *filename, const char *filetype)
644 if (pkg_infodb_is_internal(filetype))
645 return;
647 printf("%s\n", filename);
650 static void
651 pkg_infodb_print_filetype(const char *filename, const char *filetype)
653 if (pkg_infodb_is_internal(filetype))
654 return;
656 printf("%s\n", filetype);
659 static void
660 control_path_file(struct pkginfo *pkg, const char *control_file)
662 const char *control_pathname;
663 struct stat st;
665 control_pathname = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
666 if (stat(control_pathname, &st) < 0)
667 return;
668 if (!S_ISREG(st.st_mode))
669 return;
671 pkg_infodb_print_filename(control_pathname, control_file);
674 static int
675 control_path(const char *const *argv)
677 struct pkginfo *pkg;
678 const char *pkgname;
679 const char *control_file;
681 pkgname = *argv++;
682 if (!pkgname)
683 badusage(_("--%s needs at least one package name argument"),
684 cipaction->olong);
686 control_file = *argv++;
687 if (control_file && *argv)
688 badusage(_("--%s takes at most two arguments"), cipaction->olong);
690 if (control_file)
691 pkg_infodb_check_filetype(control_file);
693 modstatdb_open(msdbrw_readonly);
695 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
696 if (pkg->status == PKG_STAT_NOTINSTALLED)
697 ohshit(_("package '%s' is not installed"),
698 pkg_name(pkg, pnaw_nonambig));
700 if (control_file)
701 control_path_file(pkg, control_file);
702 else
703 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filename);
705 modstatdb_shutdown();
707 return 0;
710 static int
711 control_list(const char *const *argv)
713 struct pkginfo *pkg;
714 const char *pkgname;
716 pkgname = *argv++;
717 if (!pkgname || *argv)
718 badusage(_("--%s takes one package name argument"), cipaction->olong);
720 modstatdb_open(msdbrw_readonly);
722 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
723 if (pkg->status == PKG_STAT_NOTINSTALLED)
724 ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
726 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filetype);
728 modstatdb_shutdown();
730 return 0;
733 static int
734 control_show(const char *const *argv)
736 struct pkginfo *pkg;
737 const char *pkgname;
738 const char *filename;
739 const char *control_file;
741 pkgname = *argv++;
742 if (!pkgname || !*argv)
743 badusage(_("--%s takes exactly two arguments"),
744 cipaction->olong);
746 control_file = *argv++;
747 if (!control_file || *argv)
748 badusage(_("--%s takes exactly two arguments"), cipaction->olong);
750 pkg_infodb_check_filetype(control_file);
752 modstatdb_open(msdbrw_readonly);
754 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
755 if (pkg->status == PKG_STAT_NOTINSTALLED)
756 ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
758 if (pkg_infodb_has_file(pkg, &pkg->installed, control_file))
759 filename = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
760 else
761 ohshit(_("control file '%s' does not exist"), control_file);
763 modstatdb_shutdown();
765 file_show(filename);
767 return 0;
770 static void
771 set_no_pager(const struct cmdinfo *ci, const char *value)
773 pager_enable(false);
776 static int
777 printversion(const char *const *argv)
779 printf(_("Debian %s package management program query tool version %s.\n"),
780 DPKGQUERY, PACKAGE_RELEASE);
781 printf(_(
782 "This is free software; see the GNU General Public License version 2 or\n"
783 "later for copying conditions. There is NO warranty.\n"));
785 m_output(stdout, _("<standard output>"));
787 return 0;
790 static int
791 usage(const char *const *argv)
793 printf(_(
794 "Usage: %s [<option>...] <command>\n"
795 "\n"), DPKGQUERY);
797 printf(_(
798 "Commands:\n"
799 " -s, --status [<package>...] Display package status details.\n"
800 " -p, --print-avail [<package>...] Display available version details.\n"
801 " -L, --listfiles <package>... List files 'owned' by package(s).\n"
802 " -l, --list [<pattern>...] List packages concisely.\n"
803 " -W, --show [<pattern>...] Show information on package(s).\n"
804 " -S, --search <pattern>... Find package(s) owning file(s).\n"
805 " --control-list <package> Print the package control file list.\n"
806 " --control-show <package> <file>\n"
807 " Show the package control file.\n"
808 " -c, --control-path <package> [<file>]\n"
809 " Print path for package control file.\n"
810 "\n"));
812 printf(_(
813 " -?, --help Show this help message.\n"
814 " --version Show the version.\n"
815 "\n"));
817 printf(_(
818 "Options:\n"
819 " --admindir=<directory> Use <directory> instead of %s.\n"
820 " --root=<directory> Use <directory> instead of %s.\n"
821 " --load-avail Use available file on --show and --list.\n"
822 " --no-pager Disables the use of any pager.\n"
823 " -f|--showformat=<format> Use alternative format for --show.\n"
824 "\n"), ADMINDIR, "/");
826 printf(_(
827 "Format syntax:\n"
828 " A format is a string that will be output for each package. The format\n"
829 " can include the standard escape sequences \\n (newline), \\r (carriage\n"
830 " return) or \\\\ (plain backslash). Package information can be included\n"
831 " by inserting variable references to package fields using the ${var[;width]}\n"
832 " syntax. Fields will be right-aligned unless the width is negative in which\n"
833 " case left alignment will be used.\n"));
835 m_output(stdout, _("<standard output>"));
837 return 0;
840 static const char printforhelp[] = N_(
841 "Use --help for help about querying packages.");
843 /* This table has both the action entries in it and the normal options.
844 * The action entries are made with the ACTION macro, as they all
845 * have a very similar structure. */
846 static const struct cmdinfo cmdinfos[]= {
847 ACTION( "listfiles", 'L', act_listfiles, list_files ),
848 ACTION( "status", 's', act_status, print_status ),
849 ACTION( "print-avail", 'p', act_printavail, print_avail ),
850 ACTION( "list", 'l', act_listpackages, listpackages ),
851 ACTION( "search", 'S', act_searchfiles, searchfiles ),
852 ACTION( "show", 'W', act_listpackages, showpackages ),
853 ACTION( "control-path", 'c', act_controlpath, control_path ),
854 ACTION( "control-list", 0, act_controllist, control_list ),
855 ACTION( "control-show", 0, act_controlshow, control_show ),
856 ACTION( "help", '?', act_help, usage ),
857 ACTION( "version", 0, act_version, printversion ),
859 { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
860 { "root", 0, 1, NULL, NULL, set_root, 0 },
861 { "load-avail", 0, 0, &opt_loadavail, NULL, NULL, 1 },
862 { "showformat", 'f', 1, NULL, &opt_showformat, NULL },
863 { "no-pager", 0, 0, NULL, NULL, set_no_pager },
864 { NULL, 0, 0, NULL, NULL, NULL }
867 int main(int argc, const char *const *argv) {
868 int ret;
870 dpkg_set_report_piped_mode(_IOFBF);
871 dpkg_locales_init(PACKAGE);
872 dpkg_program_init("dpkg-query");
873 dpkg_options_parse(&argv, cmdinfos, printforhelp);
875 debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
877 if (!cipaction) badusage(_("need an action option"));
879 ret = cipaction->action(argv);
881 dpkg_program_done();
882 dpkg_locales_done();
884 return ret;