Debian Codegraph @

src/query/main.c

raw
1 /*
2 * dpkg-query - program for query the dpkg database
3 * querycmd.c - status enquiry and listing options
4 *
5 * Copyright © 1995,1996 Ian Jackson <[email protected]>
6 * Copyright © 2000,2001 Wichert Akkerman <[email protected]>
7 * Copyright © 2006-2015 Guillem Jover <[email protected]>
8 * Copyright © 2011 Linaro Limited
9 * Copyright © 2011 Raphaël Hertzog <[email protected]>
10 *
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.
15 *
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.
20 *
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/>.
23 */
24
25 #include <config.h>
26 #include <compat.h>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
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>
43
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>
59
60 #include "actions.h"
61
62 static const char *opt_showformat = "${binary:Package}\t${Version}\n";
63
64 static int opt_loadavail = 0;
65
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)
70 {
71 int argc, i, ip, *found;
72 int misses = 0;
73 struct pkg_spec *ps;
74
75 for (argc = 0; argv[argc]; argc++)
76 ;
77 found = m_calloc(argc, sizeof(int));
78
79 ps = m_malloc(sizeof(*ps) * argc);
80 for (ip = 0; ip < argc; ip++) {
81 pkg_spec_init(&ps[ip], PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD);
82 pkg_spec_parse(&ps[ip], argv[ip]);
83 }
84
85 for (i = 0; i < array->n_pkgs; i++) {
86 struct pkginfo *pkg;
87 bool pkg_found = false;
88
89 pkg = array->pkgs[i];
90 for (ip = 0; ip < argc; ip++) {
91 if (pkg_spec_match_pkg(&ps[ip], pkg, &pkg->installed)) {
92 pkg_found = true;
93 found[ip]++;
94 }
95 }
96 if (!pkg_found)
97 array->pkgs[i] = NULL;
98 }
99
100 pkg_array_foreach(array, pkg_visitor, pkg_data);
101
102 for (ip = 0; ip < argc; ip++) {
103 if (!found[ip]) {
104 notice(_("no packages found matching %s"), argv[ip]);
105 misses++;
106 }
107 pkg_spec_destroy(&ps[ip]);
108 }
109
110 free(ps);
111 free(found);
112
113 return misses;
114 }
115
116 struct list_format {
117 bool head;
118 int nw;
119 int vw;
120 int aw;
121 int dw;
122 };
123
124 /* TODO: Refactor to reduce lines length. */
125 static void
126 list_format_init(struct list_format *fmt, struct pkg_array *array)
127 {
128 int i;
129
130 if (fmt->nw != 0)
131 return;
132
133 fmt->nw = 14;
134 fmt->vw = 12;
135 fmt->aw = 12;
136 fmt->dw = 33;
137
138 for (i = 0; i < array->n_pkgs; i++) {
139 int plen, vlen, alen, dlen;
140
141 if (array->pkgs[i] == NULL)
142 continue;
143
144 plen = str_width(pkg_name(array->pkgs[i], pnaw_nonambig));
145 vlen = str_width(versiondescribe(&array->pkgs[i]->installed.version,
146 vdew_nonambig));
147 alen = str_width(dpkg_arch_describe(array->pkgs[i]->installed.arch));
148 pkg_synopsis(array->pkgs[i], &dlen);
149
150 if (plen > fmt->nw)
151 fmt->nw = plen;
152 if (vlen > fmt->vw)
153 fmt->vw = vlen;
154 if (alen > fmt->aw)
155 fmt->aw = alen;
156 if (dlen > fmt->dw)
157 fmt->dw = dlen;
158 }
159 }
160
161 static void
162 list_format_print(struct list_format *fmt,
163 const char *c_want, const char *c_status, const char *c_eflag,
164 const char *name, const char *version, const char *arch,
165 const char *desc, int desc_len)
166 {
167 struct str_crop_info ns, vs, as, ds;
168
169 str_gen_crop(name, fmt->nw, &ns);
170 str_gen_crop(version, fmt->vw, &vs);
171 str_gen_crop(arch, fmt->aw, &as);
172 str_gen_crop(desc, desc_len, &ds);
173
174 printf("%s%s%s %-*.*s %-*.*s %-*.*s %.*s\n", c_want, c_status, c_eflag,
175 ns.max_bytes, ns.str_bytes, name,
176 vs.max_bytes, vs.str_bytes, version,
177 as.max_bytes, as.str_bytes, arch,
178 ds.str_bytes, desc);
179 }
180
181 static void
182 list_format_print_header(struct list_format *fmt)
183 {
184 int l;
185
186 if (fmt->head)
187 return;
188
189 /*
190 * TRANSLATORS: This is the header that appears on 'dpkg-query -l'. The
191 * string should remain under 80 characters. The uppercase letters in
192 * the state values denote the abbreviated letter that will appear on
193 * the first three columns, which should ideally match the English one
194 * (e.g. Remove → supRimeix), see dpkg-query(1) for further details. The
195 * translated message can use additional lines if needed. The ASCII art
196 * can use Unicode box characters.
197 */
198 fputs(_("\
199 +-- Desired=Unknown/Install/Remove/Purge/Hold\n\
200 |+- Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend\n\
201 ||+ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n"), stdout);
202 list_format_print(fmt,
203 C_("query-list-header", "|"),
204 C_("query-list-header", "|"),
205 C_("query-list-header", "|"),
206 _("Name"),
207 _("Version"),
208 _("Architecture"),
209 _("Description"), fmt->dw);
210
211 /* Status */
212 fputs(C_("query-list-header", "+++-"), stdout);
213
214 /* Package name. */
215 for (l = 0; l < fmt->nw; l++)
216 fputs(C_("query-list-header", "="), stdout);
217 fputs(C_("query-list-header", "-"), stdout);
218
219 /* Version. */
220 for (l = 0; l < fmt->vw; l++)
221 fputs(C_("query-list-header", "="), stdout);
222 fputs(C_("query-list-header", "-"), stdout);
223
224 /* Architecture. */
225 for (l = 0; l < fmt->aw; l++)
226 fputs(C_("query-list-header", "="), stdout);
227 fputs(C_("query-list-header", "-"), stdout);
228
229 /* Description. */
230 for (l = 0; l < fmt->dw; l++)
231 fputs(C_("query-list-header", "="), stdout);
232 printf("\n");
233
234 fmt->head = true;
235 }
236
237 static void
238 pkg_array_list_item(struct pkg_array *array, struct pkginfo *pkg,
239 void *pkg_data)
240 {
241 struct list_format *fmt = pkg_data;
242 int l;
243 const char *pdesc;
244
245 list_format_init(fmt, array);
246 list_format_print_header(fmt);
247
248 pdesc = pkg_synopsis(pkg, &l);
249 l = min(l, fmt->dw);
250
251 list_format_print(fmt,
252 pkg_abbrev_want(pkg),
253 pkg_abbrev_status(pkg),
254 pkg_abbrev_eflag(pkg),
255 pkg_name(pkg, pnaw_nonambig),
256 versiondescribe(&pkg->installed.version, vdew_nonambig),
257 dpkg_arch_describe(pkg->installed.arch),
258 pdesc, l);
259 }
260
261 static int
262 listpackages(const char *const *argv)
263 {
264 struct pkg_array array;
265 int misses = 0;
266 struct list_format fmt;
267 struct pager *pager;
268
269 if (!opt_loadavail)
270 modstatdb_open(msdbrw_readonly);
271 else
272 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
273
274 pkg_array_init_from_hash(&array);
275 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
276
277 memset(&fmt, 0, sizeof(fmt));
278
279 pager = pager_spawn(_("showing package list on pager"));
280
281 if (!*argv) {
282 int i;
283
284 for (i = 0; i < array.n_pkgs; i++) {
285 struct pkginfo *pkg;
286
287 pkg = array.pkgs[i];
288 if (pkg->status == PKG_STAT_NOTINSTALLED)
289 array.pkgs[i] = NULL;
290 }
291
292 pkg_array_foreach(&array, pkg_array_list_item, &fmt);
293 } else {
294 misses = pkg_array_match_patterns(&array, pkg_array_list_item,
295 &fmt, argv);
296 }
297
298 m_output(stdout, _("<standard output>"));
299 m_output(stderr, _("<standard error>"));
300
301 pager_reap(pager);
302
303 pkg_array_destroy(&array);
304 modstatdb_shutdown();
305
306 return !!misses;
307 }
308
309 /* TODO: Refactor diversion accesses. */
310 static int
311 searchoutput(struct fsys_namenode *namenode)
312 {
313 struct fsys_node_pkgs_iter *iter;
314 struct pkginfo *pkg_owner;
315 int found;
316
317 if (namenode->divert) {
318 const char *name_from = namenode->divert->camefrom ?
319 namenode->divert->camefrom->name :
320 namenode->name;
321 const char *name_to = namenode->divert->useinstead ?
322 namenode->divert->useinstead->name :
323 namenode->name;
324
325 if (namenode->divert->pkgset) {
326 printf(_("diversion by %s from: %s\n"),
327 namenode->divert->pkgset->name, name_from);
328 printf(_("diversion by %s to: %s\n"),
329 namenode->divert->pkgset->name, name_to);
330 } else {
331 printf(_("local diversion from: %s\n"), name_from);
332 printf(_("local diversion to: %s\n"), name_to);
333 }
334 }
335 found = 0;
336
337 iter = fsys_node_pkgs_iter_new(namenode);
338 while ((pkg_owner = fsys_node_pkgs_iter_next(iter))) {
339 if (found)
340 fputs(", ", stdout);
341 fputs(pkg_name(pkg_owner, pnaw_nonambig), stdout);
342 found++;
343 }
344 fsys_node_pkgs_iter_free(iter);
345
346 if (found)
347 printf(": %s\n", namenode->name);
348
349 return found + (namenode->divert ? 1 : 0);
350 }
351
352 static int
353 searchfiles(const char *const *argv)
354 {
355 const char *thisarg;
356 int misses = 0;
357 struct varbuf path = VARBUF_INIT;
358 static struct varbuf vb;
359
360 if (!*argv)
361 badusage(_("--search needs at least one file name pattern argument"));
362
363 modstatdb_open(msdbrw_readonly);
364 ensure_allinstfiles_available_quiet();
365 ensure_diversions();
366
367 while ((thisarg = *argv++) != NULL) {
368 struct fsys_namenode *namenode;
369 int found = 0;
370
371 if (!strchr("*[?/", *thisarg)) {
372 varbuf_reset(&vb);
373 varbuf_add_char(&vb, '*');
374 varbuf_add_str(&vb, thisarg);
375 varbuf_add_char(&vb, '*');
376 thisarg = vb.buf;
377 }
378 if (!strpbrk(thisarg, "*[?\\")) {
379 /* Trim trailing ‘/’ and ‘/.’ from the argument if it
380 * is not a pattern, just a pathname. */
381 varbuf_set_str(&path, thisarg);
382 varbuf_trunc(&path, path_trim_slash_slashdot(path.buf));
383
384 namenode = fsys_hash_find_node(varbuf_str(&path),
385 FHFF_NONE);
386 found += searchoutput(namenode);
387 } else {
388 struct fsys_hash_iter *iter;
389
390 iter = fsys_hash_iter_new();
391 while ((namenode = fsys_hash_iter_next(iter)) != NULL) {
392 if (fnmatch(thisarg, namenode->name, 0))
393 continue;
394 found += searchoutput(namenode);
395 }
396 fsys_hash_iter_free(iter);
397 }
398 if (!found) {
399 notice(_("no path found matching pattern %s"), thisarg);
400 misses++;
401 m_output(stderr, _("<standard error>"));
402 } else {
403 m_output(stdout, _("<standard output>"));
404 }
405 }
406 modstatdb_shutdown();
407
408 varbuf_destroy(&path);
409
410 return !!misses;
411 }
412
413 /* TODO: Refactor is-informative conditional. */
414 static int
415 print_status(const char *const *argv)
416 {
417 int misses = 0;
418
419 modstatdb_open(msdbrw_readonly);
420
421 if (!*argv) {
422 writedb_stanzas(stdout, _("<standard output>"), 0);
423 } else {
424 const char *thisarg;
425
426 while ((thisarg = *argv++) != NULL) {
427 struct pkginfo *pkg;
428
429 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
430
431 if (pkg->status == PKG_STAT_NOTINSTALLED &&
432 pkg->priority == PKG_PRIO_UNKNOWN &&
433 str_is_unset(pkg->section) &&
434 !pkg->archives &&
435 pkg->want == PKG_WANT_UNKNOWN &&
436 !pkg_is_informative(pkg, &pkg->installed)) {
437 notice(_("package '%s' is not installed and no information is available"),
438 pkg_name(pkg, pnaw_nonambig));
439 misses++;
440 } else {
441 write_stanza(stdout, _("<standard output>"),
442 pkg, &pkg->installed);
443 }
444
445 if (*argv != NULL)
446 putchar('\n');
447 }
448 }
449
450 m_output(stdout, _("<standard output>"));
451 if (misses) {
452 fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files.\n"),
453 stderr);
454 m_output(stderr, _("<standard error>"));
455 }
456
457 modstatdb_shutdown();
458
459 return !!misses;
460 }
461
462 /* TODO: Refactor to reduce lines length. */
463 static int
464 print_avail(const char *const *argv)
465 {
466 int misses = 0;
467
468 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
469
470 if (!*argv) {
471 writedb_stanzas(stdout, _("<standard output>"),
472 wdb_dump_available);
473 } else {
474 const char *thisarg;
475
476 while ((thisarg = *argv++) != NULL) {
477 struct pkginfo *pkg;
478
479 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
480
481 if (!pkg_is_informative(pkg, &pkg->available)) {
482 notice(_("package '%s' is not available"),
483 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
484 misses++;
485 } else {
486 write_stanza(stdout, _("<standard output>"),
487 pkg, &pkg->available);
488 }
489
490 if (*argv != NULL)
491 putchar('\n');
492 }
493 }
494
495 m_output(stdout, _("<standard output>"));
496 if (misses)
497 m_output(stderr, _("<standard error>"));
498
499 modstatdb_shutdown();
500
501 return !!misses;
502 }
503
504 /* TODO: Refactor to reduce nesting levels. */
505 static int
506 list_files(const char *const *argv)
507 {
508 const char *thisarg;
509 struct fsys_namenode_list *file;
510 struct pkginfo *pkg;
511 struct fsys_namenode *namenode;
512 int misses = 0;
513
514 if (!*argv)
515 badusage(_("--%s needs at least one package name argument"),
516 cipaction->olong);
517
518 modstatdb_open(msdbrw_readonly);
519
520 while ((thisarg = *argv++) != NULL) {
521 pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
522
523 switch (pkg->status) {
524 case PKG_STAT_NOTINSTALLED:
525 notice(_("package '%s' is not installed"),
526 pkg_name(pkg, pnaw_nonambig));
527 misses++;
528 break;
529 default:
530 ensure_packagefiles_available(pkg);
531 ensure_diversions();
532 file = pkg->files;
533 if (!file) {
534 printf(_("Package '%s' does not contain any files (!)\n"),
535 pkg_name(pkg, pnaw_nonambig));
536 } else {
537 while (file) {
538 namenode = file->namenode;
539 puts(namenode->name);
540 if (namenode->divert && !namenode->divert->camefrom) {
541 if (!namenode->divert->pkgset)
542 printf(_("locally diverted to: %s\n"),
543 namenode->divert->useinstead->name);
544 else if (pkg->set == namenode->divert->pkgset)
545 printf(_("package diverts others to: %s\n"),
546 namenode->divert->useinstead->name);
547 else
548 printf(_("diverted by %s to: %s\n"),
549 namenode->divert->pkgset->name,
550 namenode->divert->useinstead->name);
551 }
552 file = file->next;
553 }
554 }
555 break;
556 }
557
558 if (*argv != NULL)
559 putchar('\n');
560 }
561
562 m_output(stdout, _("<standard output>"));
563 if (misses) {
564 fputs(_("Use dpkg --contents (= dpkg-deb --contents) to list archive files contents.\n"),
565 stderr);
566 m_output(stderr, _("<standard error>"));
567 }
568
569 modstatdb_shutdown();
570
571 return !!misses;
572 }
573
574 static void
575 pkg_array_load_db_fsys(struct pkg_array *array, struct pkginfo *pkg,
576 void *pkg_data)
577 {
578 ensure_packagefiles_available(pkg);
579 }
580
581 static void
582 pkg_array_show_item(struct pkg_array *array, struct pkginfo *pkg,
583 void *pkg_data)
584 {
585 struct pkg_format_node *fmt = pkg_data;
586
587 pkg_format_show(fmt, pkg, &pkg->installed);
588 }
589
590 static int
591 showpackages(const char *const *argv)
592 {
593 struct dpkg_error err;
594 struct pkg_array array;
595 struct pkg_format_node *fmt;
596 bool fmt_needs_db_fsys;
597 int misses = 0;
598
599 fmt = pkg_format_parse(opt_showformat, &err);
600 if (!fmt) {
601 notice(_("error in show format: %s"), err.str);
602 dpkg_error_destroy(&err);
603 return 2;
604 }
605
606 fmt_needs_db_fsys = pkg_format_needs_db_fsys(fmt);
607
608 if (!opt_loadavail)
609 modstatdb_open(msdbrw_readonly);
610 else
611 modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
612
613 pkg_array_init_from_hash(&array);
614 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
615
616 if (!*argv) {
617 int i;
618
619 if (fmt_needs_db_fsys)
620 ensure_allinstfiles_available_quiet();
621 for (i = 0; i < array.n_pkgs; i++) {
622 struct pkginfo *pkg;
623
624 pkg = array.pkgs[i];
625 if (pkg->status == PKG_STAT_NOTINSTALLED)
626 continue;
627 pkg_format_show(fmt, pkg, &pkg->installed);
628 }
629 } else {
630 if (fmt_needs_db_fsys)
631 pkg_array_foreach(&array, pkg_array_load_db_fsys, NULL);
632 misses = pkg_array_match_patterns(&array, pkg_array_show_item,
633 fmt, argv);
634 }
635
636 m_output(stdout, _("<standard output>"));
637 m_output(stderr, _("<standard error>"));
638
639 pkg_array_destroy(&array);
640 pkg_format_free(fmt);
641 modstatdb_shutdown();
642
643 return !!misses;
644 }
645
646 static bool
647 pkg_infodb_is_internal(const char *filetype)
648 {
649 /* Do not expose internal database files. */
650 if (strcmp(filetype, LISTFILE) == 0 ||
651 strcmp(filetype, CONFFILESFILE) == 0)
652 return true;
653
654 if (strlen(filetype) > MAXCONTROLFILENAME)
655 return true;
656
657 return false;
658 }
659
660 static void
661 pkg_infodb_check_filetype(const char *filetype)
662 {
663 const char *c;
664
665 /* Validate control file name for sanity. */
666 for (c = "/."; *c; c++)
667 if (strchr(filetype, *c))
668 badusage(_("control file contains %c"), *c);
669 }
670
671 static void
672 pkg_infodb_print_filename(const char *filename, const char *filetype)
673 {
674 if (pkg_infodb_is_internal(filetype))
675 return;
676
677 printf("%s\n", filename);
678 }
679
680 static void
681 pkg_infodb_print_filetype(const char *filename, const char *filetype)
682 {
683 if (pkg_infodb_is_internal(filetype))
684 return;
685
686 printf("%s\n", filetype);
687 }
688
689 static void
690 control_path_file(struct pkginfo *pkg, const char *control_file)
691 {
692 const char *control_pathname;
693 struct stat st;
694
695 control_pathname = pkg_infodb_get_file(pkg, &pkg->installed,
696 control_file);
697 if (stat(control_pathname, &st) < 0)
698 return;
699 if (!S_ISREG(st.st_mode))
700 return;
701
702 pkg_infodb_print_filename(control_pathname, control_file);
703 }
704
705 static int
706 control_path(const char *const *argv)
707 {
708 struct pkginfo *pkg;
709 const char *pkgname;
710 const char *control_file;
711
712 pkgname = *argv++;
713 if (!pkgname)
714 badusage(_("--%s needs at least one package name argument"),
715 cipaction->olong);
716
717 control_file = *argv++;
718 if (control_file && *argv)
719 badusage(_("--%s takes at most two arguments"),
720 cipaction->olong);
721
722 if (control_file)
723 pkg_infodb_check_filetype(control_file);
724
725 modstatdb_open(msdbrw_readonly);
726
727 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
728 if (pkg->status == PKG_STAT_NOTINSTALLED)
729 ohshit(_("package '%s' is not installed"),
730 pkg_name(pkg, pnaw_nonambig));
731
732 if (control_file)
733 control_path_file(pkg, control_file);
734 else
735 pkg_infodb_foreach(pkg, &pkg->installed,
736 pkg_infodb_print_filename);
737
738 modstatdb_shutdown();
739
740 return 0;
741 }
742
743 static int
744 control_list(const char *const *argv)
745 {
746 struct pkginfo *pkg;
747 const char *pkgname;
748
749 pkgname = *argv++;
750 if (!pkgname || *argv)
751 badusage(_("--%s takes one package name argument"),
752 cipaction->olong);
753
754 modstatdb_open(msdbrw_readonly);
755
756 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
757 if (pkg->status == PKG_STAT_NOTINSTALLED)
758 ohshit(_("package '%s' is not installed"),
759 pkg_name(pkg, pnaw_nonambig));
760
761 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filetype);
762
763 modstatdb_shutdown();
764
765 return 0;
766 }
767
768 static int
769 control_show(const char *const *argv)
770 {
771 struct pkginfo *pkg;
772 const char *pkgname;
773 const char *filename;
774 const char *control_file;
775
776 pkgname = *argv++;
777 if (!pkgname || !*argv)
778 badusage(_("--%s takes exactly two arguments"),
779 cipaction->olong);
780
781 control_file = *argv++;
782 if (!control_file || *argv)
783 badusage(_("--%s takes exactly two arguments"),
784 cipaction->olong);
785
786 pkg_infodb_check_filetype(control_file);
787
788 modstatdb_open(msdbrw_readonly);
789
790 pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
791 if (pkg->status == PKG_STAT_NOTINSTALLED)
792 ohshit(_("package '%s' is not installed"),
793 pkg_name(pkg, pnaw_nonambig));
794
795 if (pkg_infodb_has_file(pkg, &pkg->installed, control_file))
796 filename = pkg_infodb_get_file(pkg, &pkg->installed,
797 control_file);
798 else
799 ohshit(_("control file '%s' does not exist"), control_file);
800
801 modstatdb_shutdown();
802
803 file_show(filename);
804
805 return 0;
806 }
807
808 static void
809 set_no_pager(const struct cmdinfo *ci, const char *value)
810 {
811 pager_enable(false);
812 }
813
814 static int
815 printversion(const char *const *argv)
816 {
817 printf(_("Debian %s package management program query tool version %s.\n"),
818 DPKGQUERY, PACKAGE_RELEASE);
819 printf(_(
820 "This is free software; see the GNU General Public License version 2 or\n"
821 "later for copying conditions. There is NO warranty.\n"));
822
823 m_output(stdout, _("<standard output>"));
824
825 return 0;
826 }
827
828 static int
829 usage(const char *const *argv)
830 {
831 printf(_(
832 "Usage: %s [<option>...] <command>\n"
833 "\n"), DPKGQUERY);
834
835 printf(_(
836 "Commands:\n"
837 " -s, --status [<package>...] Display package status details.\n"
838 " -p, --print-avail [<package>...] Display available version details.\n"
839 " -L, --listfiles <package>... List files 'owned' by package(s).\n"
840 " -l, --list [<pattern>...] List packages concisely.\n"
841 " -W, --show [<pattern>...] Show information on package(s).\n"
842 " -S, --search <pattern>... Find package(s) owning file(s).\n"
843 " --control-list <package> Print the package control file list.\n"
844 " --control-show <package> <file>\n"
845 " Show the package control file.\n"
846 " -c, --control-path <package> [<file>]\n"
847 " Print path for package control file.\n"
848 "\n"));
849
850 printf(_(
851 " -?, --help Show this help message.\n"
852 " --version Show the version.\n"
853 "\n"));
854
855 printf(_(
856 "Options:\n"
857 " --admindir=<directory> Use <directory> instead of %s.\n"
858 " --root=<directory> Use <directory> instead of %s.\n"
859 " --load-avail Use available file on --show and --list.\n"
860 " --no-pager Disables the use of any pager.\n"
861 " -f|--showformat=<format> Use alternative format for --show.\n"
862 "\n"), ADMINDIR, "/");
863
864 printf(_(
865 "Format syntax:\n"
866 " A format is a string that will be output for each package. The format\n"
867 " can include the standard escape sequences \\n (newline), \\r (carriage\n"
868 " return) or \\\\ (plain backslash). Package information can be included\n"
869 " by inserting variable references to package fields using the ${var[;width]}\n"
870 " syntax. Fields will be right-aligned unless the width is negative in which\n"
871 " case left alignment will be used.\n"));
872
873 m_output(stdout, _("<standard output>"));
874
875 return 0;
876 }
877
878 static const char printforhelp[] = N_(
879 "Use --help for help about querying packages.");
880
881 /* This table has both the action entries in it and the normal options.
882 * The action entries are made with the ACTION macro, as they all
883 * have a very similar structure. */
884 static const struct cmdinfo cmdinfos[] = {
885 ACTION( "listfiles", 'L', act_listfiles, list_files ),
886 ACTION( "status", 's', act_status, print_status ),
887 ACTION( "print-avail", 'p', act_printavail, print_avail ),
888 ACTION( "list", 'l', act_listpackages, listpackages ),
889 ACTION( "search", 'S', act_searchfiles, searchfiles ),
890 ACTION( "show", 'W', act_listpackages, showpackages ),
891 ACTION( "control-path", 'c', act_controlpath, control_path ),
892 ACTION( "control-list", 0, act_controllist, control_list ),
893 ACTION( "control-show", 0, act_controlshow, control_show ),
894 ACTION( "help", '?', act_help, usage ),
895 ACTION( "version", 0, act_version, printversion ),
896
897 { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
898 { "root", 0, 1, NULL, NULL, set_root, 0 },
899 { "load-avail", 0, 0, &opt_loadavail, NULL, NULL, 1 },
900 { "showformat", 'f', 1, NULL, &opt_showformat, NULL },
901 { "no-pager", 0, 0, NULL, NULL, set_no_pager },
902 { NULL, 0, 0, NULL, NULL, NULL }
903 };
904
905 int
906 main(int argc, const char *const *argv)
907 {
908 int ret;
909
910 dpkg_set_report_piped_mode(_IOFBF);
911 dpkg_locales_init(PACKAGE);
912 dpkg_program_init("dpkg-query");
913 dpkg_options_parse(&argv, cmdinfos, printforhelp);
914
915 debug(dbg_general, "root=%s admindir=%s",
916 dpkg_fsys_get_dir(), dpkg_db_get_dir());
917
918 if (!cipaction)
919 badusage(_("need an action option"));
920
921 ret = cipaction->action(argv);
922
923 dpkg_program_done();
924 dpkg_locales_done();
925
926 return ret;
927 }