query.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * dpkg-query - program for query the dpkg database
  3. * query.c - status enquiry and listing options
  4. *
  5. * dpkg - main program for package management
  6. * enquiry.c - status enquiry and listing options
  7. *
  8. * Copyright (C) 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  9. * Copyright (C) 200,2001 Wichert Akkerman <wakkerma@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
  13. * published by the Free Software Foundation; either version 2,
  14. * or (at your option) any later version.
  15. *
  16. * This is distributed in the hope that it will be useful, but
  17. * 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
  22. * License along with dpkg; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <fnmatch.h>
  30. #include <assert.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/termios.h>
  36. #include <fcntl.h>
  37. #include <dpkg.h>
  38. #include <dpkg-db.h>
  39. #include <myopt.h>
  40. #include "filesdb.h"
  41. #include "main.h"
  42. static const char* showformat = "${Package}\t${Version}\n";
  43. int pkglistqsortcmp(const void *a, const void *b) {
  44. const struct pkginfo *pa= *(const struct pkginfo**)a;
  45. const struct pkginfo *pb= *(const struct pkginfo**)b;
  46. return strcmp(pa->name,pb->name);
  47. }
  48. static void limiteddescription(struct pkginfo *pkg, int maxl,
  49. const char **pdesc_r, int *l_r) {
  50. const char *pdesc, *p;
  51. int l;
  52. pdesc= pkg->installed.valid ? pkg->installed.description : 0;
  53. if (!pdesc) pdesc= _("(no description available)");
  54. p= strchr(pdesc,'\n');
  55. if (!p) p= pdesc+strlen(pdesc);
  56. l= (p - pdesc > maxl) ? maxl : (int)(p - pdesc);
  57. *pdesc_r=pdesc; *l_r=l;
  58. }
  59. static int getwidth(void) {
  60. int fd;
  61. int res;
  62. struct winsize ws;
  63. const char* columns;
  64. if ((columns=getenv("COLUMNS")) && ((res=atoi(columns))>0))
  65. return res;
  66. else if (!isatty(1))
  67. return -1;
  68. else {
  69. if ((fd=open("/dev/tty",O_RDONLY))!=-1) {
  70. if (ioctl(fd, TIOCGWINSZ, &ws)==-1)
  71. ws.ws_col=80;
  72. close(fd);
  73. }
  74. return ws.ws_col;
  75. }
  76. }
  77. static void list1package(struct pkginfo *pkg, int *head,
  78. struct pkginfo **pkgl, int np) {
  79. int i,l,w;
  80. static int nw,vw,dw;
  81. const char *pdesc;
  82. static char format[80] = "";
  83. if (format[0]==0) {
  84. w=getwidth();
  85. if (w == -1) {
  86. nw=14, vw=14, dw=44;
  87. for (i=0; i<np; i++) {
  88. const char *pdesc;
  89. int plen, vlen, dlen;
  90. pdesc= pkg->installed.valid ? pkg->installed.description : 0;
  91. if (!pdesc) pdesc= _("(no description available)");
  92. plen= strlen(pkgl[i]->name);
  93. vlen = strlen(versiondescribe(&pkgl[i]->installed.version, vdew_nonambig));
  94. dlen= strcspn(pdesc, "\n");
  95. if (plen > nw) nw = plen;
  96. if (vlen > vw) vw = vlen;
  97. if (dlen > dw) dw = dlen;
  98. }
  99. } else {
  100. w-=80;
  101. if (w<0) w=0; /* lets not try to deal with terminals that are too small */
  102. w>>=2; /* halve that so we can add that to the both the name and description */
  103. nw=(14+w); /* name width */
  104. vw=(14+w); /* version width */
  105. dw=(44+(2*w)); /* description width */
  106. }
  107. sprintf(format,"%%c%%c%%c %%-%d.%ds %%-%d.%ds %%.*s\n", nw, nw, vw, vw);
  108. }
  109. if (!*head) {
  110. fputs(_("\
  111. Desired=Unknown/Install/Remove/Purge/Hold\n\
  112. | Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend\n\
  113. |/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)\n"), stdout);
  114. printf(format,'|','|','/', _("Name"), _("Version"), 40, _("Description"));
  115. printf("+++-"); /* status */
  116. for (l=0;l<nw;l++) printf("="); printf("-"); /* packagename */
  117. for (l=0;l<vw;l++) printf("="); printf("-"); /* version */
  118. for (l=0;l<dw;l++) printf("="); /* description */
  119. printf("\n");
  120. *head= 1;
  121. }
  122. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  123. limiteddescription(pkg,dw,&pdesc,&l);
  124. printf(format,
  125. "uihrp"[pkg->want],
  126. "ncHUFWti"[pkg->status],
  127. " R?#"[pkg->eflag],
  128. pkg->name,
  129. versiondescribe(&pkg->installed.version, vdew_nonambig),
  130. l, pdesc);
  131. }
  132. void listpackages(const char *const *argv) {
  133. struct pkgiterator *it;
  134. struct pkginfo *pkg;
  135. struct pkginfo **pkgl;
  136. int np, i, head;
  137. modstatdb_init(admindir,msdbrw_readonly);
  138. np= countpackages();
  139. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  140. it= iterpkgstart(); i=0;
  141. while ((pkg= iterpkgnext(it))) {
  142. assert(i<np);
  143. pkgl[i++]= pkg;
  144. }
  145. iterpkgend(it);
  146. assert(i==np);
  147. qsort(pkgl, np, sizeof(struct pkginfo*), pkglistqsortcmp);
  148. head = 0;
  149. if (!*argv) {
  150. for (i=0; i<np; i++) {
  151. pkg= pkgl[i];
  152. if (pkg->status == stat_notinstalled) continue;
  153. list1package(pkg, &head, pkgl, np);
  154. }
  155. } else {
  156. int argc, ip, *found;
  157. for (argc = 0; argv[argc]; argc++);
  158. found = m_malloc(sizeof(int) * argc);
  159. memset(found, 0, sizeof(int) * argc);
  160. for (i = 0; i < np; i++) {
  161. pkg = pkgl[i];
  162. for (ip = 0; ip < argc; ip++) {
  163. if (!fnmatch(argv[ip], pkg->name, 0)) {
  164. list1package(pkg, &head, pkgl, np);
  165. found[ip]++;
  166. break;
  167. }
  168. }
  169. }
  170. /* FIXME: we might get non-matching messages for sub-patterns specified
  171. * after their super-patterns, due to us skipping on first match. */
  172. for (ip = 0; ip < argc; ip++) {
  173. if (!found[ip]) {
  174. fprintf(stderr, _("No packages found matching %s.\n"), argv[ip]);
  175. nerrs++;
  176. }
  177. }
  178. }
  179. if (ferror(stdout)) werr("stdout");
  180. if (ferror(stderr)) werr("stderr");
  181. modstatdb_shutdown();
  182. }
  183. static int searchoutput(struct filenamenode *namenode) {
  184. int found, i;
  185. struct filepackages *packageslump;
  186. if (namenode->divert) {
  187. const char *name_from = namenode->divert->camefrom ?
  188. namenode->divert->camefrom->name : namenode->name;
  189. const char *name_to = namenode->divert->useinstead ?
  190. namenode->divert->useinstead->name : namenode->name;
  191. if (namenode->divert->pkg) {
  192. printf(_("diversion by %s from: %s\n"),
  193. namenode->divert->pkg->name, name_from);
  194. printf(_("diversion by %s to: %s\n"),
  195. namenode->divert->pkg->name, name_to);
  196. } else {
  197. printf(_("local diversion from: %s\n"), name_from);
  198. printf(_("local diversion to: %s\n"), name_to);
  199. }
  200. }
  201. found= 0;
  202. for (packageslump= namenode->packages;
  203. packageslump;
  204. packageslump= packageslump->more) {
  205. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  206. if (found) fputs(", ",stdout);
  207. fputs(packageslump->pkgs[i]->name,stdout);
  208. found++;
  209. }
  210. }
  211. if (found) printf(": %s\n",namenode->name);
  212. return found + (namenode->divert ? 1 : 0);
  213. }
  214. void searchfiles(const char *const *argv) {
  215. struct filenamenode *namenode;
  216. struct fileiterator *it;
  217. const char *thisarg;
  218. int found;
  219. static struct varbuf vb;
  220. if (!*argv)
  221. badusage(_("--search needs at least one file name pattern argument"));
  222. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  223. ensure_allinstfiles_available_quiet();
  224. ensure_diversions();
  225. while ((thisarg= *argv++) != 0) {
  226. found= 0;
  227. rtrim_slash_slashdot(thisarg);
  228. if (!strchr("*[?/",*thisarg)) {
  229. varbufreset(&vb);
  230. varbufaddc(&vb,'*');
  231. varbufaddstr(&vb,thisarg);
  232. varbufaddc(&vb,'*');
  233. varbufaddc(&vb,0);
  234. thisarg= vb.buf;
  235. }
  236. if (strcspn(thisarg,"*[?\\") == strlen(thisarg)) {
  237. namenode= findnamenode(thisarg, 0);
  238. found += searchoutput(namenode);
  239. } else {
  240. it= iterfilestart();
  241. while ((namenode= iterfilenext(it)) != 0) {
  242. if (fnmatch(thisarg,namenode->name,0)) continue;
  243. found+= searchoutput(namenode);
  244. }
  245. iterfileend(it);
  246. }
  247. if (!found) {
  248. fprintf(stderr,_("dpkg: %s not found.\n"),thisarg);
  249. nerrs++;
  250. if (ferror(stderr)) werr("stderr");
  251. } else {
  252. if (ferror(stdout)) werr("stdout");
  253. }
  254. }
  255. modstatdb_shutdown();
  256. }
  257. void enqperpackage(const char *const *argv) {
  258. int failures;
  259. const char *thisarg;
  260. struct fileinlist *file;
  261. struct pkginfo *pkg;
  262. struct filenamenode *namenode;
  263. if (!*argv)
  264. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  265. failures= 0;
  266. if (cipaction->arg==act_listfiles)
  267. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  268. else
  269. modstatdb_init(admindir,msdbrw_readonly);
  270. while ((thisarg= *argv++) != 0) {
  271. pkg= findpackage(thisarg);
  272. switch (cipaction->arg) {
  273. case act_status:
  274. if (pkg->status == stat_notinstalled &&
  275. pkg->priority == pri_unknown &&
  276. !(pkg->section && *pkg->section) &&
  277. !pkg->files &&
  278. pkg->want == want_unknown &&
  279. !informative(pkg,&pkg->installed)) {
  280. fprintf(stderr,_("Package `%s' is not installed and no info is available.\n"),pkg->name);
  281. failures++;
  282. } else {
  283. writerecord(stdout, "<stdout>", pkg, &pkg->installed);
  284. }
  285. break;
  286. case act_printavail:
  287. if (!informative(pkg,&pkg->available)) {
  288. fprintf(stderr,_("Package `%s' is not available.\n"),pkg->name);
  289. failures++;
  290. } else {
  291. writerecord(stdout, "<stdout>", pkg, &pkg->available);
  292. }
  293. break;
  294. case act_listfiles:
  295. switch (pkg->status) {
  296. case stat_notinstalled:
  297. fprintf(stderr,_("Package `%s' is not installed.\n"),pkg->name);
  298. failures++;
  299. break;
  300. default:
  301. ensure_packagefiles_available(pkg);
  302. ensure_diversions();
  303. file= pkg->clientdata->files;
  304. if (!file) {
  305. printf(_("Package `%s' does not contain any files (!)\n"),pkg->name);
  306. } else {
  307. while (file) {
  308. namenode= file->namenode;
  309. puts(namenode->name);
  310. if (namenode->divert && !namenode->divert->camefrom) {
  311. if (!namenode->divert->pkg)
  312. printf(_("locally diverted to: %s\n"),
  313. namenode->divert->useinstead->name);
  314. else if (pkg == namenode->divert->pkg)
  315. printf(_("package diverts others to: %s\n"),
  316. namenode->divert->useinstead->name);
  317. else
  318. printf(_("diverted by %s to: %s\n"),
  319. namenode->divert->pkg->name,
  320. namenode->divert->useinstead->name);
  321. }
  322. file= file->next;
  323. }
  324. }
  325. break;
  326. }
  327. break;
  328. default:
  329. internerr("unknown action");
  330. }
  331. if (*(argv + 1) == 0)
  332. putchar('\n');
  333. if (ferror(stdout)) werr("stdout");
  334. }
  335. if (failures) {
  336. nerrs++;
  337. fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  338. "and dpkg --contents (= dpkg-deb --contents) to list their contents.\n"),stderr);
  339. if (ferror(stdout)) werr("stdout");
  340. }
  341. modstatdb_shutdown();
  342. }
  343. void showpackages(const char *const *argv) {
  344. struct pkgiterator *it;
  345. struct pkginfo *pkg;
  346. struct pkginfo **pkgl;
  347. int np, i;
  348. struct lstitem* fmt = parseformat(showformat);
  349. if (!fmt) {
  350. nerrs++;
  351. return;
  352. }
  353. modstatdb_init(admindir,msdbrw_readonly);
  354. np= countpackages();
  355. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  356. it= iterpkgstart(); i=0;
  357. while ((pkg= iterpkgnext(it))) {
  358. assert(i<np);
  359. pkgl[i++]= pkg;
  360. }
  361. iterpkgend(it);
  362. assert(i==np);
  363. qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
  364. if (!*argv) {
  365. for (i=0; i<np; i++) {
  366. pkg= pkgl[i];
  367. if (pkg->status == stat_notinstalled) continue;
  368. show1package(fmt,pkg);
  369. }
  370. } else {
  371. int argc, ip, *found;
  372. for (argc = 0; argv[argc]; argc++);
  373. found = m_malloc(sizeof(int) * argc);
  374. memset(found, 0, sizeof(int) * argc);
  375. for (i = 0; i < np; i++) {
  376. pkg = pkgl[i];
  377. for (ip = 0; ip < argc; ip++) {
  378. if (!fnmatch(argv[ip], pkg->name, 0)) {
  379. show1package(fmt, pkg);
  380. found[ip]++;
  381. break;
  382. }
  383. }
  384. }
  385. /* FIXME: we might get non-matching messages for sub-patterns specified
  386. * after their super-patterns, due to us skipping on first match. */
  387. for (ip = 0; ip < argc; ip++) {
  388. if (!found[ip]) {
  389. fprintf(stderr, _("No packages found matching %s.\n"), argv[ip]);
  390. nerrs++;
  391. }
  392. }
  393. }
  394. if (ferror(stdout)) werr("stdout");
  395. if (ferror(stderr)) werr("stderr");
  396. freeformat(fmt);
  397. modstatdb_shutdown();
  398. }
  399. void
  400. printversion(void)
  401. {
  402. if (printf(_("Debian `%s' package management program query tool\n"),
  403. DPKGQUERY) < 0) werr("stdout");
  404. if (printf(_("This is free software; see the GNU General Public License version 2 or\n"
  405. "later for copying conditions. There is NO warranty.\n"
  406. "See %s --license for copyright and license details.\n"),
  407. DPKGQUERY) < 0) werr("stdout");
  408. }
  409. /*
  410. options that need fixing:
  411. dpkg --yet-to-unpack \n\
  412. */
  413. void
  414. usage(void)
  415. {
  416. if (printf(_(
  417. "Usage: %s [<option> ...] <command>\n"
  418. "\n"), DPKGQUERY) < 0) werr ("stdout");
  419. if (printf(_(
  420. "Commands:\n"
  421. " -s|--status <package> ... Display package status details.\n"
  422. " -p|--print-avail <package> ... Display available version details.\n"
  423. " -L|--listfiles <package> ... List files `owned' by package(s).\n"
  424. " -l|--list [<pattern> ...] List packages concisely.\n"
  425. " -W|--show <pattern> ... Show information on package(s).\n"
  426. " -S|--search <pattern> ... Find package(s) owning file(s).\n"
  427. "\n")) < 0) werr ("stdout");
  428. if (printf(_(
  429. " -h|--help Show this help message.\n"
  430. " --version Show the version.\n"
  431. " --license|--licence Show the copyright licensing terms.\n"
  432. "\n")) < 0) werr ("stdout");
  433. if (printf(_(
  434. "Options:\n"
  435. " --admindir=<directory> Use <directory> instead of %s.\n"
  436. " -f|--showformat=<format> Use alternative format for --show.\n"
  437. "\n"), ADMINDIR) < 0) werr ("stdout");
  438. if (printf(_(
  439. "Format syntax:\n"
  440. " A format is a string that will be output for each package. The format\n"
  441. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  442. " return) or \\\\ (plain backslash). Package information can be included\n"
  443. " by inserting variable references to package fields using the ${var[;width]}\n"
  444. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  445. " case left alignment will be used.\n")) < 0) werr ("stdout");
  446. }
  447. const char thisname[]= "dpkg-query";
  448. const char printforhelp[]= N_("\
  449. Use --help for help about querying packages;\n\
  450. Use --license for copyright license and lack of warranty (GNU GPL).\n\
  451. \n");
  452. const struct cmdinfo *cipaction= 0;
  453. int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
  454. int f_autodeconf=0, f_nodebsig=0;
  455. unsigned long f_debug=0;
  456. /* Change fc_overwrite to 1 to enable force-overwrite by default */
  457. int fc_hold=0;
  458. int fc_conflicts=0, fc_depends=0;
  459. int fc_badpath=0;
  460. int errabort = 50;
  461. const char *admindir= ADMINDIR;
  462. const char *instdir= "";
  463. struct packageinlist *ignoredependss=0;
  464. static void setaction(const struct cmdinfo *cip, const char *value) {
  465. if (cipaction)
  466. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  467. cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
  468. cipaction= cip;
  469. }
  470. static const struct cmdinfo cmdinfos[]= {
  471. /* This table has both the action entries in it and the normal options.
  472. * The action entries are made with the ACTION macro, as they all
  473. * have a very similar structure.
  474. */
  475. #define ACTION(longopt,shortopt,code,function) \
  476. { longopt, shortopt, 0,0,0, setaction, code, 0, (voidfnp)function }
  477. #define OBSOLETE(longopt,shortopt) \
  478. { longopt, shortopt, 0,0,0, setobsolete, 0, 0, 0 }
  479. ACTION( "listfiles", 'L', act_listfiles, enqperpackage ),
  480. ACTION( "status", 's', act_status, enqperpackage ),
  481. ACTION( "print-avail", 'p', act_printavail, enqperpackage ),
  482. ACTION( "list", 'l', act_listpackages, listpackages ),
  483. ACTION( "search", 'S', act_searchfiles, searchfiles ),
  484. ACTION( "show", 'W', act_listpackages, showpackages ),
  485. { "admindir", 0, 1, 0, &admindir, 0 },
  486. { "showformat", 'f', 1, 0, &showformat, 0 },
  487. { "help", 'h', 0, 0, 0, helponly },
  488. { "version", 0, 0, 0, 0, versiononly },
  489. { "licence",/* UK spelling */ 0,0,0,0, showcopyright },
  490. { "license",/* US spelling */ 0,0,0,0, showcopyright },
  491. { 0, 0 }
  492. };
  493. int main(int argc, const char *const *argv) {
  494. jmp_buf ejbuf;
  495. static void (*actionfunction)(const char *const *argv);
  496. standard_startup(&ejbuf, argc, &argv, NULL, 0, cmdinfos);
  497. if (!cipaction) badusage(_("need an action option"));
  498. setvbuf(stdout,0,_IONBF,0);
  499. filesdbinit();
  500. actionfunction= (void (*)(const char* const*))cipaction->farg;
  501. actionfunction(argv);
  502. standard_shutdown(0);
  503. return reportbroken_retexitstatus();
  504. }