query.c 18 KB

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