query.c 17 KB

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