query.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. if (!strchr("*[?/",*thisarg)) {
  228. varbufreset(&vb);
  229. varbufaddc(&vb,'*');
  230. varbufaddstr(&vb,thisarg);
  231. varbufaddc(&vb,'*');
  232. varbufaddc(&vb,0);
  233. thisarg= vb.buf;
  234. }
  235. if (strcspn(thisarg,"*[?\\") == strlen(thisarg)) {
  236. namenode= findnamenode(thisarg, 0);
  237. found += searchoutput(namenode);
  238. } else {
  239. it= iterfilestart();
  240. while ((namenode= iterfilenext(it)) != 0) {
  241. if (fnmatch(thisarg,namenode->name,0)) continue;
  242. found+= searchoutput(namenode);
  243. }
  244. iterfileend(it);
  245. }
  246. if (!found) {
  247. fprintf(stderr,_("dpkg: %s not found.\n"),thisarg);
  248. nerrs++;
  249. if (ferror(stderr)) werr("stderr");
  250. } else {
  251. if (ferror(stdout)) werr("stdout");
  252. }
  253. }
  254. modstatdb_shutdown();
  255. }
  256. void enqperpackage(const char *const *argv) {
  257. int failures;
  258. const char *thisarg;
  259. struct fileinlist *file;
  260. struct pkginfo *pkg;
  261. struct filenamenode *namenode;
  262. if (!*argv)
  263. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  264. failures= 0;
  265. if (cipaction->arg==act_listfiles)
  266. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  267. else
  268. modstatdb_init(admindir,msdbrw_readonly);
  269. while ((thisarg= *argv++) != 0) {
  270. pkg= findpackage(thisarg);
  271. switch (cipaction->arg) {
  272. case act_status:
  273. if (pkg->status == stat_notinstalled &&
  274. pkg->priority == pri_unknown &&
  275. !(pkg->section && *pkg->section) &&
  276. !pkg->files &&
  277. pkg->want == want_unknown &&
  278. !informative(pkg,&pkg->installed)) {
  279. fprintf(stderr,_("Package `%s' is not installed and no info is available.\n"),pkg->name);
  280. failures++;
  281. } else {
  282. writerecord(stdout, "<stdout>", pkg, &pkg->installed);
  283. }
  284. break;
  285. case act_printavail:
  286. if (!informative(pkg,&pkg->available)) {
  287. fprintf(stderr,_("Package `%s' is not available.\n"),pkg->name);
  288. failures++;
  289. } else {
  290. writerecord(stdout, "<stdout>", pkg, &pkg->available);
  291. }
  292. break;
  293. case act_listfiles:
  294. switch (pkg->status) {
  295. case stat_notinstalled:
  296. fprintf(stderr,_("Package `%s' is not installed.\n"),pkg->name);
  297. failures++;
  298. break;
  299. default:
  300. ensure_packagefiles_available(pkg);
  301. ensure_diversions();
  302. file= pkg->clientdata->files;
  303. if (!file) {
  304. printf(_("Package `%s' does not contain any files (!)\n"),pkg->name);
  305. } else {
  306. while (file) {
  307. namenode= file->namenode;
  308. puts(namenode->name);
  309. if (namenode->divert && !namenode->divert->camefrom) {
  310. if (!namenode->divert->pkg)
  311. printf(_("locally diverted to: %s\n"),
  312. namenode->divert->useinstead->name);
  313. else if (pkg == namenode->divert->pkg)
  314. printf(_("package diverts others to: %s\n"),
  315. namenode->divert->useinstead->name);
  316. else
  317. printf(_("diverted by %s to: %s\n"),
  318. namenode->divert->pkg->name,
  319. namenode->divert->useinstead->name);
  320. }
  321. file= file->next;
  322. }
  323. }
  324. break;
  325. }
  326. break;
  327. default:
  328. internerr("unknown action");
  329. }
  330. if (*(argv + 1) == 0)
  331. putchar('\n');
  332. if (ferror(stdout)) werr("stdout");
  333. }
  334. if (failures) {
  335. nerrs++;
  336. fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  337. "and dpkg --contents (= dpkg-deb --contents) to list their contents.\n"),stderr);
  338. if (ferror(stdout)) werr("stdout");
  339. }
  340. modstatdb_shutdown();
  341. }
  342. void showpackages(const char *const *argv) {
  343. struct pkgiterator *it;
  344. struct pkginfo *pkg;
  345. struct pkginfo **pkgl;
  346. int np, i;
  347. struct lstitem* fmt = parseformat(showformat);
  348. if (!fmt) {
  349. nerrs++;
  350. return;
  351. }
  352. modstatdb_init(admindir,msdbrw_readonly);
  353. np= countpackages();
  354. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  355. it= iterpkgstart(); i=0;
  356. while ((pkg= iterpkgnext(it))) {
  357. assert(i<np);
  358. pkgl[i++]= pkg;
  359. }
  360. iterpkgend(it);
  361. assert(i==np);
  362. qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
  363. if (!*argv) {
  364. for (i=0; i<np; i++) {
  365. pkg= pkgl[i];
  366. if (pkg->status == stat_notinstalled) continue;
  367. show1package(fmt,pkg);
  368. }
  369. } else {
  370. int argc, ip, *found;
  371. for (argc = 0; argv[argc]; argc++);
  372. found = m_malloc(sizeof(int) * argc);
  373. memset(found, 0, sizeof(int) * argc);
  374. for (i = 0; i < np; i++) {
  375. pkg = pkgl[i];
  376. for (ip = 0; ip < argc; ip++) {
  377. if (!fnmatch(argv[ip], pkg->name, 0)) {
  378. show1package(fmt, pkg);
  379. found[ip]++;
  380. break;
  381. }
  382. }
  383. }
  384. /* FIXME: we might get non-matching messages for sub-patterns specified
  385. * after their super-patterns, due to us skipping on first match. */
  386. for (ip = 0; ip < argc; ip++) {
  387. if (!found[ip]) {
  388. fprintf(stderr, _("No packages found matching %s.\n"), argv[ip]);
  389. nerrs++;
  390. }
  391. }
  392. }
  393. if (ferror(stdout)) werr("stdout");
  394. if (ferror(stderr)) werr("stderr");
  395. freeformat(fmt);
  396. modstatdb_shutdown();
  397. }
  398. void
  399. printversion(void)
  400. {
  401. if (printf(_("Debian `%s' package management program query tool\n"),
  402. DPKGQUERY) < 0) werr("stdout");
  403. if (printf(_("This is free software; see the GNU General Public License version 2 or\n"
  404. "later for copying conditions. There is NO warranty.\n"
  405. "See %s --license for copyright and license details.\n"),
  406. DPKGQUERY) < 0) werr("stdout");
  407. }
  408. /*
  409. options that need fixing:
  410. dpkg --yet-to-unpack \n\
  411. */
  412. void
  413. usage(void)
  414. {
  415. if (printf(_(
  416. "Usage: %s [<option> ...] <command>\n"
  417. "\n"), DPKGQUERY) < 0) werr ("stdout");
  418. if (printf(_(
  419. "Commands:\n"
  420. " -s|--status <package> ... Display package status details.\n"
  421. " -p|--print-avail <package> ... Display available version details.\n"
  422. " -L|--listfiles <package> ... List files `owned' by package(s).\n"
  423. " -l|--list [<pattern> ...] List packages concisely.\n"
  424. " -W|--show <pattern> ... Show information on package(s).\n"
  425. " -S|--search <pattern> ... Find package(s) owning file(s).\n"
  426. "\n")) < 0) werr ("stdout");
  427. if (printf(_(
  428. " -h|--help Show this help message.\n"
  429. " --version Show the version.\n"
  430. " --license|--licence Show the copyright licensing terms.\n"
  431. "\n")) < 0) werr ("stdout");
  432. if (printf(_(
  433. "Options:\n"
  434. " --admindir=<directory> Use <directory> instead of %s.\n"
  435. " -f|--showformat=<format> Use alternative format for --show.\n"
  436. "\n"), ADMINDIR) < 0) werr ("stdout");
  437. if (printf(_(
  438. "Format syntax:\n"
  439. " A format is a string that will be output for each package. The format\n"
  440. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  441. " return) or \\\\ (plain backslash). Package information can be included\n"
  442. " by inserting variable references to package fields using the ${var[;width]}\n"
  443. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  444. " case left alignment will be used.\n")) < 0) werr ("stdout");
  445. }
  446. const char thisname[]= "dpkg-query";
  447. const char printforhelp[]= N_("\
  448. Use --help for help about querying packages;\n\
  449. Use --license for copyright license and lack of warranty (GNU GPL).\n\
  450. \n");
  451. const struct cmdinfo *cipaction= 0;
  452. int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
  453. int f_autodeconf=0, f_nodebsig=0;
  454. unsigned long f_debug=0;
  455. /* Change fc_overwrite to 1 to enable force-overwrite by default */
  456. int fc_hold=0;
  457. int fc_conflicts=0, fc_depends=0;
  458. int fc_badpath=0;
  459. int errabort = 50;
  460. const char *admindir= ADMINDIR;
  461. const char *instdir= "";
  462. struct packageinlist *ignoredependss=0;
  463. static void setaction(const struct cmdinfo *cip, const char *value) {
  464. if (cipaction)
  465. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  466. cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
  467. cipaction= cip;
  468. }
  469. static const struct cmdinfo cmdinfos[]= {
  470. /* This table has both the action entries in it and the normal options.
  471. * The action entries are made with the ACTION macro, as they all
  472. * have a very similar structure.
  473. */
  474. #define ACTION(longopt,shortopt,code,function) \
  475. { longopt, shortopt, 0,0,0, setaction, code, 0, (voidfnp)function }
  476. #define OBSOLETE(longopt,shortopt) \
  477. { longopt, shortopt, 0,0,0, setobsolete, 0, 0, 0 }
  478. ACTION( "listfiles", 'L', act_listfiles, enqperpackage ),
  479. ACTION( "status", 's', act_status, enqperpackage ),
  480. ACTION( "print-avail", 'p', act_printavail, enqperpackage ),
  481. ACTION( "list", 'l', act_listpackages, listpackages ),
  482. ACTION( "search", 'S', act_searchfiles, searchfiles ),
  483. ACTION( "show", 'W', act_listpackages, showpackages ),
  484. { "admindir", 0, 1, 0, &admindir, 0 },
  485. { "showformat", 'f', 1, 0, &showformat, 0 },
  486. { "help", 'h', 0, 0, 0, helponly },
  487. { "version", 0, 0, 0, 0, versiononly },
  488. { "licence",/* UK spelling */ 0,0,0,0, showcopyright },
  489. { "license",/* US spelling */ 0,0,0,0, showcopyright },
  490. { 0, 0 }
  491. };
  492. int main(int argc, const char *const *argv) {
  493. jmp_buf ejbuf;
  494. static void (*actionfunction)(const char *const *argv);
  495. standard_startup(&ejbuf, argc, &argv, NULL, 0, cmdinfos);
  496. if (!cipaction) badusage(_("need an action option"));
  497. setvbuf(stdout,0,_IONBF,0);
  498. filesdbinit();
  499. actionfunction= (void (*)(const char* const*))cipaction->farg;
  500. actionfunction(argv);
  501. standard_shutdown(0);
  502. return reportbroken_retexitstatus();
  503. }