querycmd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 <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>
  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. #include <config.h>
  25. #include <compat.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/ioctl.h>
  29. #if HAVE_LOCALE_H
  30. #include <locale.h>
  31. #endif
  32. #include <errno.h>
  33. #include <limits.h>
  34. #include <string.h>
  35. #include <fcntl.h>
  36. #include <dirent.h>
  37. #include <fnmatch.h>
  38. #include <termios.h>
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <dpkg/i18n.h>
  43. #include <dpkg/dpkg.h>
  44. #include <dpkg/dpkg-db.h>
  45. #include <dpkg/pkg-array.h>
  46. #include <dpkg/pkg-spec.h>
  47. #include <dpkg/pkg-format.h>
  48. #include <dpkg/pkg-show.h>
  49. #include <dpkg/string.h>
  50. #include <dpkg/path.h>
  51. #include <dpkg/file.h>
  52. #include <dpkg/options.h>
  53. #include "filesdb.h"
  54. #include "infodb.h"
  55. #include "main.h"
  56. static const char *showformat = "${binary:Package}\t${Version}\n";
  57. static int opt_loadavail = 0;
  58. static int getwidth(void) {
  59. int fd;
  60. long res;
  61. struct winsize ws;
  62. const char *columns;
  63. char *endptr;
  64. columns = getenv("COLUMNS");
  65. if (columns) {
  66. errno = 0;
  67. res = strtol(columns, &endptr, 10);
  68. if (errno == 0 && columns != endptr && *endptr == '\0' &&
  69. res > 0 && res < INT_MAX)
  70. return res;
  71. }
  72. if (!isatty(1))
  73. return -1;
  74. else {
  75. res = 80;
  76. fd = open("/dev/tty", O_RDONLY);
  77. if (fd != -1) {
  78. if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
  79. res = ws.ws_col;
  80. close(fd);
  81. }
  82. return res;
  83. }
  84. }
  85. static int
  86. pkg_array_match_patterns(struct pkg_array *array,
  87. pkg_array_visitor_func *pkg_visitor, void *pkg_data,
  88. const char *const *argv)
  89. {
  90. int argc, i, ip, *found;
  91. int rc = 0;
  92. struct pkg_spec *ps;
  93. for (argc = 0; argv[argc]; argc++);
  94. found = m_calloc(argc, sizeof(int));
  95. ps = m_malloc(sizeof(*ps) * argc);
  96. for (ip = 0; ip < argc; ip++) {
  97. pkg_spec_init(&ps[ip], PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD);
  98. pkg_spec_parse(&ps[ip], argv[ip]);
  99. }
  100. for (i = 0; i < array->n_pkgs; i++) {
  101. struct pkginfo *pkg;
  102. bool pkg_found = false;
  103. pkg = array->pkgs[i];
  104. for (ip = 0; ip < argc; ip++) {
  105. if (pkg_spec_match_pkg(&ps[ip], pkg, &pkg->installed)) {
  106. pkg_found = true;
  107. found[ip]++;
  108. }
  109. }
  110. if (!pkg_found)
  111. array->pkgs[i] = NULL;
  112. }
  113. pkg_array_foreach(array, pkg_visitor, pkg_data);
  114. for (ip = 0; ip < argc; ip++) {
  115. if (!found[ip]) {
  116. notice(_("no packages found matching %s"), argv[ip]);
  117. rc++;
  118. }
  119. pkg_spec_destroy(&ps[ip]);
  120. }
  121. free(ps);
  122. free(found);
  123. return rc;
  124. }
  125. struct list_format {
  126. bool head;
  127. int nw;
  128. int vw;
  129. int aw;
  130. int dw;
  131. };
  132. static void
  133. list_format_init(struct list_format *fmt, struct pkg_array *array)
  134. {
  135. int w;
  136. if (fmt->nw != 0)
  137. return;
  138. w = getwidth();
  139. if (w == -1) {
  140. int i;
  141. fmt->nw = 14;
  142. fmt->vw = 12;
  143. fmt->aw = 12;
  144. fmt->dw = 33;
  145. for (i = 0; i < array->n_pkgs; i++) {
  146. int plen, vlen, alen, dlen;
  147. if (array->pkgs[i] == NULL)
  148. continue;
  149. plen = str_width(pkg_name(array->pkgs[i], pnaw_nonambig));
  150. vlen = str_width(versiondescribe(&array->pkgs[i]->installed.version,
  151. vdew_nonambig));
  152. alen = str_width(dpkg_arch_describe(array->pkgs[i]->installed.arch));
  153. pkgbin_summary(array->pkgs[i], &array->pkgs[i]->installed, &dlen);
  154. if (plen > fmt->nw)
  155. fmt->nw = plen;
  156. if (vlen > fmt->vw)
  157. fmt->vw = vlen;
  158. if (alen > fmt->aw)
  159. fmt->aw = alen;
  160. if (dlen > fmt->dw)
  161. fmt->dw = dlen;
  162. }
  163. } else {
  164. w -= 80;
  165. /* Let's not try to deal with terminals that are too small. */
  166. if (w < 0)
  167. w = 0;
  168. /* Halve that so we can add it to both the name and description. */
  169. w >>= 1;
  170. /* Name width. */
  171. fmt->nw = (14 + (w / 2));
  172. /* Version width. */
  173. fmt->vw = (12 + (w / 4));
  174. /* Architecture width. */
  175. fmt->aw = (12 + (w / 4));
  176. /* Description width. */
  177. fmt->dw = (33 + w);
  178. }
  179. }
  180. static void
  181. list_format_print(struct list_format *fmt,
  182. int c_want, int c_status, int c_eflag,
  183. const char *name, const char *version, const char *arch,
  184. const char *desc, int desc_len)
  185. {
  186. struct str_crop_info ns, vs, as, ds;
  187. str_gen_crop(name, fmt->nw, &ns);
  188. str_gen_crop(version, fmt->vw, &vs);
  189. str_gen_crop(arch, fmt->aw, &as);
  190. str_gen_crop(desc, desc_len, &ds);
  191. printf("%c%c%c %-*.*s %-*.*s %-*.*s %.*s\n", c_want, c_status, c_eflag,
  192. ns.max_bytes, ns.str_bytes, name,
  193. vs.max_bytes, vs.str_bytes, version,
  194. as.max_bytes, as.str_bytes, arch,
  195. ds.str_bytes, desc);
  196. }
  197. static void
  198. list_format_print_header(struct list_format *fmt)
  199. {
  200. int l;
  201. if (fmt->head)
  202. return;
  203. /* TRANSLATORS: This is the header that appears on 'dpkg-query -l'. The
  204. * string should remain under 80 characters. The uppercase letters in
  205. * the state values denote the abbreviated letter that will appear on
  206. * the first three columns, which should ideally match the English one
  207. * (e.g. Remove → supRimeix), see dpkg-query(1) for further details. The
  208. * translated message can use additional lines if needed. */
  209. fputs(_("\
  210. Desired=Unknown/Install/Remove/Purge/Hold\n\
  211. | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend\n\
  212. |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n"), stdout);
  213. list_format_print(fmt, '|', '|', '/', _("Name"), _("Version"),
  214. _("Architecture"), _("Description"), fmt->dw);
  215. /* Status */
  216. printf("+++-");
  217. /* Package name. */
  218. for (l = 0; l < fmt->nw; l++)
  219. printf("=");
  220. printf("-");
  221. /* Version. */
  222. for (l = 0; l < fmt->vw; l++)
  223. printf("=");
  224. printf("-");
  225. /* Architecture. */
  226. for (l = 0; l < fmt->aw; l++)
  227. printf("=");
  228. printf("-");
  229. /* Description. */
  230. for (l = 0; l < fmt->dw; l++)
  231. printf("=");
  232. printf("\n");
  233. fmt->head = true;
  234. }
  235. static void
  236. pkg_array_list_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
  237. {
  238. struct list_format *fmt = pkg_data;
  239. int l;
  240. const char *pdesc;
  241. list_format_init(fmt, array);
  242. list_format_print_header(fmt);
  243. pdesc = pkgbin_summary(pkg, &pkg->installed, &l);
  244. l = min(l, fmt->dw);
  245. list_format_print(fmt,
  246. pkg_abbrev_want(pkg),
  247. pkg_abbrev_status(pkg),
  248. pkg_abbrev_eflag(pkg),
  249. pkg_name(pkg, pnaw_nonambig),
  250. versiondescribe(&pkg->installed.version, vdew_nonambig),
  251. dpkg_arch_describe(pkg->installed.arch),
  252. pdesc, l);
  253. }
  254. static int
  255. listpackages(const char *const *argv)
  256. {
  257. struct pkg_array array;
  258. struct pkginfo *pkg;
  259. int i;
  260. int rc = 0;
  261. struct list_format fmt;
  262. if (!opt_loadavail)
  263. modstatdb_open(msdbrw_readonly);
  264. else
  265. modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
  266. pkg_array_init_from_db(&array);
  267. pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
  268. memset(&fmt, 0, sizeof(fmt));
  269. if (!*argv) {
  270. for (i = 0; i < array.n_pkgs; i++) {
  271. pkg = array.pkgs[i];
  272. if (pkg->status == PKG_STAT_NOTINSTALLED)
  273. array.pkgs[i] = NULL;
  274. }
  275. pkg_array_foreach(&array, pkg_array_list_item, &fmt);
  276. } else {
  277. rc = pkg_array_match_patterns(&array, pkg_array_list_item, &fmt, argv);
  278. }
  279. m_output(stdout, _("<standard output>"));
  280. m_output(stderr, _("<standard error>"));
  281. pkg_array_destroy(&array);
  282. modstatdb_shutdown();
  283. return rc;
  284. }
  285. static int searchoutput(struct filenamenode *namenode) {
  286. struct filepackages_iterator *iter;
  287. struct pkginfo *pkg_owner;
  288. int found;
  289. if (namenode->divert) {
  290. const char *name_from = namenode->divert->camefrom ?
  291. namenode->divert->camefrom->name : namenode->name;
  292. const char *name_to = namenode->divert->useinstead ?
  293. namenode->divert->useinstead->name : namenode->name;
  294. if (namenode->divert->pkgset) {
  295. printf(_("diversion by %s from: %s\n"),
  296. namenode->divert->pkgset->name, name_from);
  297. printf(_("diversion by %s to: %s\n"),
  298. namenode->divert->pkgset->name, name_to);
  299. } else {
  300. printf(_("local diversion from: %s\n"), name_from);
  301. printf(_("local diversion to: %s\n"), name_to);
  302. }
  303. }
  304. found= 0;
  305. iter = filepackages_iter_new(namenode);
  306. while ((pkg_owner = filepackages_iter_next(iter))) {
  307. if (found)
  308. fputs(", ", stdout);
  309. fputs(pkg_name(pkg_owner, pnaw_nonambig), stdout);
  310. found++;
  311. }
  312. filepackages_iter_free(iter);
  313. if (found) printf(": %s\n",namenode->name);
  314. return found + (namenode->divert ? 1 : 0);
  315. }
  316. static int
  317. searchfiles(const char *const *argv)
  318. {
  319. struct filenamenode *namenode;
  320. struct fileiterator *iter;
  321. const char *thisarg;
  322. int found;
  323. int failures = 0;
  324. struct varbuf path = VARBUF_INIT;
  325. static struct varbuf vb;
  326. if (!*argv)
  327. badusage(_("--search needs at least one file name pattern argument"));
  328. modstatdb_open(msdbrw_readonly);
  329. ensure_allinstfiles_available_quiet();
  330. ensure_diversions();
  331. while ((thisarg = *argv++) != NULL) {
  332. found= 0;
  333. if (!strchr("*[?/",*thisarg)) {
  334. varbuf_reset(&vb);
  335. varbuf_add_char(&vb, '*');
  336. varbuf_add_str(&vb, thisarg);
  337. varbuf_add_char(&vb, '*');
  338. varbuf_end_str(&vb);
  339. thisarg= vb.buf;
  340. }
  341. if (!strpbrk(thisarg, "*[?\\")) {
  342. /* Trim trailing ‘/’ and ‘/.’ from the argument if it is not
  343. * a pattern, just a pathname. */
  344. varbuf_reset(&path);
  345. varbuf_add_str(&path, thisarg);
  346. varbuf_end_str(&path);
  347. varbuf_trunc(&path, path_trim_slash_slashdot(path.buf));
  348. namenode = findnamenode(path.buf, 0);
  349. found += searchoutput(namenode);
  350. } else {
  351. iter = files_db_iter_new();
  352. while ((namenode = files_db_iter_next(iter)) != NULL) {
  353. if (fnmatch(thisarg,namenode->name,0)) continue;
  354. found+= searchoutput(namenode);
  355. }
  356. files_db_iter_free(iter);
  357. }
  358. if (!found) {
  359. notice(_("no path found matching pattern %s"), thisarg);
  360. failures++;
  361. m_output(stderr, _("<standard error>"));
  362. } else {
  363. m_output(stdout, _("<standard output>"));
  364. }
  365. }
  366. modstatdb_shutdown();
  367. varbuf_destroy(&path);
  368. return failures;
  369. }
  370. static int
  371. enqperpackage(const char *const *argv)
  372. {
  373. const char *thisarg;
  374. struct fileinlist *file;
  375. struct pkginfo *pkg;
  376. struct filenamenode *namenode;
  377. int failures = 0;
  378. if (!*argv)
  379. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  380. if (cipaction->arg_int == act_printavail)
  381. modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
  382. else
  383. modstatdb_open(msdbrw_readonly);
  384. while ((thisarg = *argv++) != NULL) {
  385. pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
  386. switch (cipaction->arg_int) {
  387. case act_status:
  388. if (pkg->status == PKG_STAT_NOTINSTALLED &&
  389. pkg->priority == PKG_PRIO_UNKNOWN &&
  390. str_is_unset(pkg->section) &&
  391. !pkg->files &&
  392. pkg->want == PKG_WANT_UNKNOWN &&
  393. !pkg_is_informative(pkg, &pkg->installed)) {
  394. notice(_("package '%s' is not installed and no information is available"),
  395. pkg_name(pkg, pnaw_nonambig));
  396. failures++;
  397. } else {
  398. writerecord(stdout, _("<standard output>"), pkg, &pkg->installed);
  399. }
  400. break;
  401. case act_printavail:
  402. if (!pkg_is_informative(pkg, &pkg->available)) {
  403. notice(_("package '%s' is not available"),
  404. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  405. failures++;
  406. } else {
  407. writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
  408. }
  409. break;
  410. case act_listfiles:
  411. switch (pkg->status) {
  412. case PKG_STAT_NOTINSTALLED:
  413. notice(_("package '%s' is not installed"),
  414. pkg_name(pkg, pnaw_nonambig));
  415. failures++;
  416. break;
  417. default:
  418. ensure_packagefiles_available(pkg);
  419. ensure_diversions();
  420. file= pkg->clientdata->files;
  421. if (!file) {
  422. printf(_("Package '%s' does not contain any files (!)\n"),
  423. pkg_name(pkg, pnaw_nonambig));
  424. } else {
  425. while (file) {
  426. namenode= file->namenode;
  427. puts(namenode->name);
  428. if (namenode->divert && !namenode->divert->camefrom) {
  429. if (!namenode->divert->pkgset)
  430. printf(_("locally diverted to: %s\n"),
  431. namenode->divert->useinstead->name);
  432. else if (pkg->set == namenode->divert->pkgset)
  433. printf(_("package diverts others to: %s\n"),
  434. namenode->divert->useinstead->name);
  435. else
  436. printf(_("diverted by %s to: %s\n"),
  437. namenode->divert->pkgset->name,
  438. namenode->divert->useinstead->name);
  439. }
  440. file= file->next;
  441. }
  442. }
  443. break;
  444. }
  445. break;
  446. default:
  447. internerr("unknown action '%d'", cipaction->arg_int);
  448. }
  449. if (*argv != NULL)
  450. putchar('\n');
  451. m_output(stdout, _("<standard output>"));
  452. }
  453. if (failures) {
  454. fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  455. "and dpkg --contents (= dpkg-deb --contents) to list their contents.\n"),stderr);
  456. m_output(stderr, _("<standard error>"));
  457. }
  458. modstatdb_shutdown();
  459. return failures;
  460. }
  461. static void
  462. pkg_array_show_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
  463. {
  464. struct pkg_format_node *fmt = pkg_data;
  465. pkg_format_show(fmt, pkg, &pkg->installed);
  466. }
  467. static int
  468. showpackages(const char *const *argv)
  469. {
  470. struct dpkg_error err;
  471. struct pkg_array array;
  472. struct pkginfo *pkg;
  473. struct pkg_format_node *fmt;
  474. int i;
  475. int rc = 0;
  476. fmt = pkg_format_parse(showformat, &err);
  477. if (!fmt) {
  478. notice(_("error in show format: %s"), err.str);
  479. dpkg_error_destroy(&err);
  480. rc++;
  481. return rc;
  482. }
  483. if (!opt_loadavail)
  484. modstatdb_open(msdbrw_readonly);
  485. else
  486. modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
  487. pkg_array_init_from_db(&array);
  488. pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
  489. if (!*argv) {
  490. for (i = 0; i < array.n_pkgs; i++) {
  491. pkg = array.pkgs[i];
  492. if (pkg->status == PKG_STAT_NOTINSTALLED)
  493. continue;
  494. pkg_format_show(fmt, pkg, &pkg->installed);
  495. }
  496. } else {
  497. rc = pkg_array_match_patterns(&array, pkg_array_show_item, fmt, argv);
  498. }
  499. m_output(stdout, _("<standard output>"));
  500. m_output(stderr, _("<standard error>"));
  501. pkg_array_destroy(&array);
  502. pkg_format_free(fmt);
  503. modstatdb_shutdown();
  504. return rc;
  505. }
  506. static bool
  507. pkg_infodb_is_internal(const char *filetype)
  508. {
  509. /* Do not expose internal database files. */
  510. if (strcmp(filetype, LISTFILE) == 0 ||
  511. strcmp(filetype, CONFFILESFILE) == 0)
  512. return true;
  513. if (strlen(filetype) > MAXCONTROLFILENAME)
  514. return true;
  515. return false;
  516. }
  517. static void
  518. pkg_infodb_check_filetype(const char *filetype)
  519. {
  520. const char *c;
  521. /* Validate control file name for sanity. */
  522. for (c = "/."; *c; c++)
  523. if (strchr(filetype, *c))
  524. badusage(_("control file contains %c"), *c);
  525. }
  526. static void
  527. pkg_infodb_print_filename(const char *filename, const char *filetype)
  528. {
  529. if (pkg_infodb_is_internal(filetype))
  530. return;
  531. printf("%s\n", filename);
  532. }
  533. static void
  534. pkg_infodb_print_filetype(const char *filename, const char *filetype)
  535. {
  536. if (pkg_infodb_is_internal(filetype))
  537. return;
  538. printf("%s\n", filetype);
  539. }
  540. static void
  541. control_path_file(struct pkginfo *pkg, const char *control_file)
  542. {
  543. const char *control_path;
  544. struct stat st;
  545. control_path = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
  546. if (stat(control_path, &st) < 0)
  547. return;
  548. if (!S_ISREG(st.st_mode))
  549. return;
  550. pkg_infodb_print_filename(control_path, control_file);
  551. }
  552. static int
  553. control_path(const char *const *argv)
  554. {
  555. struct pkginfo *pkg;
  556. const char *pkgname;
  557. const char *control_file;
  558. pkgname = *argv++;
  559. if (!pkgname)
  560. badusage(_("--%s needs at least one package name argument"),
  561. cipaction->olong);
  562. control_file = *argv++;
  563. if (control_file && *argv)
  564. badusage(_("--%s takes at most two arguments"), cipaction->olong);
  565. if (control_file)
  566. pkg_infodb_check_filetype(control_file);
  567. modstatdb_open(msdbrw_readonly);
  568. pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
  569. if (pkg->status == PKG_STAT_NOTINSTALLED)
  570. ohshit(_("package '%s' is not installed"),
  571. pkg_name(pkg, pnaw_nonambig));
  572. if (control_file)
  573. control_path_file(pkg, control_file);
  574. else
  575. pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filename);
  576. modstatdb_shutdown();
  577. return 0;
  578. }
  579. static int
  580. control_list(const char *const *argv)
  581. {
  582. struct pkginfo *pkg;
  583. const char *pkgname;
  584. pkgname = *argv++;
  585. if (!pkgname || *argv)
  586. badusage(_("--%s takes one package name argument"), cipaction->olong);
  587. modstatdb_open(msdbrw_readonly);
  588. pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
  589. if (pkg->status == PKG_STAT_NOTINSTALLED)
  590. ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
  591. pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filetype);
  592. modstatdb_shutdown();
  593. return 0;
  594. }
  595. static int
  596. control_show(const char *const *argv)
  597. {
  598. struct pkginfo *pkg;
  599. const char *pkgname;
  600. const char *filename;
  601. const char *control_file;
  602. pkgname = *argv++;
  603. if (!pkgname || !*argv)
  604. badusage(_("--%s takes exactly two arguments"),
  605. cipaction->olong);
  606. control_file = *argv++;
  607. if (!control_file || *argv)
  608. badusage(_("--%s takes exactly two arguments"), cipaction->olong);
  609. pkg_infodb_check_filetype(control_file);
  610. modstatdb_open(msdbrw_readonly);
  611. pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
  612. if (pkg->status == PKG_STAT_NOTINSTALLED)
  613. ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
  614. if (pkg_infodb_has_file(pkg, &pkg->installed, control_file))
  615. filename = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
  616. else
  617. ohshit(_("control file '%s' does not exist"), control_file);
  618. modstatdb_shutdown();
  619. file_show(filename);
  620. return 0;
  621. }
  622. static void DPKG_ATTR_NORET
  623. printversion(const struct cmdinfo *ci, const char *value)
  624. {
  625. printf(_("Debian %s package management program query tool version %s.\n"),
  626. DPKGQUERY, PACKAGE_RELEASE);
  627. printf(_(
  628. "This is free software; see the GNU General Public License version 2 or\n"
  629. "later for copying conditions. There is NO warranty.\n"));
  630. m_output(stdout, _("<standard output>"));
  631. exit(0);
  632. }
  633. static void DPKG_ATTR_NORET
  634. usage(const struct cmdinfo *ci, const char *value)
  635. {
  636. printf(_(
  637. "Usage: %s [<option> ...] <command>\n"
  638. "\n"), DPKGQUERY);
  639. printf(_(
  640. "Commands:\n"
  641. " -s|--status <package> ... Display package status details.\n"
  642. " -p|--print-avail <package> ... Display available version details.\n"
  643. " -L|--listfiles <package> ... List files 'owned' by package(s).\n"
  644. " -l|--list [<pattern> ...] List packages concisely.\n"
  645. " -W|--show [<pattern> ...] Show information on package(s).\n"
  646. " -S|--search <pattern> ... Find package(s) owning file(s).\n"
  647. " --control-list <package> Print the package control file list.\n"
  648. " --control-show <package> <file>\n"
  649. " Show the package control file.\n"
  650. " -c|--control-path <package> [<file>]\n"
  651. " Print path for package control file.\n"
  652. "\n"));
  653. printf(_(
  654. " -?, --help Show this help message.\n"
  655. " --version Show the version.\n"
  656. "\n"));
  657. printf(_(
  658. "Options:\n"
  659. " --admindir=<directory> Use <directory> instead of %s.\n"
  660. " --load-avail Use available file on --show and --list.\n"
  661. " -f|--showformat=<format> Use alternative format for --show.\n"
  662. "\n"), ADMINDIR);
  663. printf(_(
  664. "Format syntax:\n"
  665. " A format is a string that will be output for each package. The format\n"
  666. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  667. " return) or \\\\ (plain backslash). Package information can be included\n"
  668. " by inserting variable references to package fields using the ${var[;width]}\n"
  669. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  670. " case left alignment will be used.\n"));
  671. m_output(stdout, _("<standard output>"));
  672. exit(0);
  673. }
  674. static const char printforhelp[] = N_(
  675. "Use --help for help about querying packages.");
  676. static const char *admindir;
  677. /* This table has both the action entries in it and the normal options.
  678. * The action entries are made with the ACTION macro, as they all
  679. * have a very similar structure. */
  680. static const struct cmdinfo cmdinfos[]= {
  681. ACTION( "listfiles", 'L', act_listfiles, enqperpackage ),
  682. ACTION( "status", 's', act_status, enqperpackage ),
  683. ACTION( "print-avail", 'p', act_printavail, enqperpackage ),
  684. ACTION( "list", 'l', act_listpackages, listpackages ),
  685. ACTION( "search", 'S', act_searchfiles, searchfiles ),
  686. ACTION( "show", 'W', act_listpackages, showpackages ),
  687. ACTION( "control-path", 'c', act_controlpath, control_path ),
  688. ACTION( "control-list", 0, act_controllist, control_list ),
  689. ACTION( "control-show", 0, act_controlshow, control_show ),
  690. { "admindir", 0, 1, NULL, &admindir, NULL },
  691. { "load-avail", 0, 0, &opt_loadavail, NULL, NULL, 1 },
  692. { "showformat", 'f', 1, NULL, &showformat, NULL },
  693. { "help", '?', 0, NULL, NULL, usage },
  694. { "version", 0, 0, NULL, NULL, printversion },
  695. { NULL, 0, 0, NULL, NULL, NULL }
  696. };
  697. int main(int argc, const char *const *argv) {
  698. int ret;
  699. dpkg_set_report_piped_mode(_IOFBF);
  700. dpkg_locales_init(PACKAGE);
  701. dpkg_program_init("dpkg-query");
  702. dpkg_options_parse(&argv, cmdinfos, printforhelp);
  703. admindir = dpkg_db_set_dir(admindir);
  704. if (!cipaction) badusage(_("need an action option"));
  705. filesdbinit();
  706. ret = cipaction->action(argv);
  707. dpkg_program_done();
  708. return !!ret;
  709. }