query.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * dpkg - main program for package management
  3. * enquiry.c - status enquiry and listing options
  4. *
  5. * Copyright (C) 1995,1996 Ian Jackson <ijackson@gnu.ai.mit.edu>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /* fixme: per-package audit */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <fnmatch.h>
  26. #include <assert.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/termios.h>
  32. #include <fcntl.h>
  33. #include <config.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include <myopt.h>
  37. #include "filesdb.h"
  38. #include "main.h"
  39. void ensure_package_clientdata(struct pkginfo *pkg) {
  40. if (pkg->clientdata) return;
  41. pkg->clientdata= nfmalloc(sizeof(struct perpackagestate));
  42. pkg->clientdata->istobe= itb_normal;
  43. pkg->clientdata->fileslistvalid= 0;
  44. pkg->clientdata->files= 0;
  45. }
  46. const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile) {
  47. static struct varbuf vb;
  48. varbufreset(&vb);
  49. varbufaddstr(&vb,admindir);
  50. varbufaddstr(&vb,"/" INFODIR);
  51. varbufaddstr(&vb,pkg->name);
  52. varbufaddc(&vb,'.');
  53. varbufaddstr(&vb,whichfile);
  54. varbufaddc(&vb,0);
  55. return vb.buf;
  56. }
  57. void cu_closepipe(int argc, void **argv) {
  58. int *p1= (int*)argv[0];
  59. close(p1[0]); close(p1[1]);
  60. }
  61. void cu_closefile(int argc, void **argv) {
  62. FILE *f= (FILE*)(argv[0]);
  63. fclose(f);
  64. }
  65. void cu_closefd(int argc, void **argv) {
  66. int ip= *(int*)argv;
  67. close(ip);
  68. }
  69. int pkglistqsortcmp(const void *a, const void *b) {
  70. const struct pkginfo *pa= *(const struct pkginfo**)a;
  71. const struct pkginfo *pb= *(const struct pkginfo**)b;
  72. return strcmp(pa->name,pb->name);
  73. }
  74. static void limiteddescription(struct pkginfo *pkg, int maxl,
  75. const char **pdesc_r, int *l_r) {
  76. const char *pdesc, *p;
  77. int l;
  78. pdesc= pkg->installed.valid ? pkg->installed.description : 0;
  79. if (!pdesc) pdesc= _("(no description available)");
  80. p= strchr(pdesc,'\n');
  81. if (!p) p= pdesc+strlen(pdesc);
  82. l= (p - pdesc > maxl) ? maxl : (int)(p - pdesc);
  83. *pdesc_r=pdesc; *l_r=l;
  84. }
  85. static int getwidth() {
  86. int fd;
  87. int res;
  88. struct winsize ws;
  89. const char* columns;
  90. if ((columns=getenv("COLUMNS")) && ((res=atoi(columns))>0))
  91. ws.ws_col=res;
  92. else if (!isatty(1))
  93. ws.ws_col=80;
  94. else {
  95. if ((fd=open("/dev/tty",O_RDONLY))!=-1) {
  96. if (ioctl(fd, TIOCGWINSZ, &ws)==-1)
  97. ws.ws_col=80;
  98. close(fd);
  99. }
  100. }
  101. return ws.ws_col;
  102. }
  103. static void list1package(struct pkginfo *pkg, int *head) {
  104. int l,w;
  105. static int nw,vw,dw;
  106. const char *pdesc;
  107. static char format[80] = "";
  108. if (format[0]==0) {
  109. w=getwidth()-80; /* get spare width */
  110. if (w<0) w=0; /* lets not try to deal with terminals that are too small */
  111. w>>=2; /* halve that so we can add that to the both the name and description */
  112. nw=(14+w); /* name width */
  113. vw=(14+w); /* version width */
  114. dw=(44+(2*w)); /* description width */
  115. sprintf(format,"%%c%%c%%c %%-%d.%ds %%-%d.%ds %%.*s\n", nw, nw, vw, vw);
  116. }
  117. if (!*head) {
  118. fputs(_("\
  119. Desired=Unknown/Install/Remove/Purge/Hold\n\
  120. | Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed\n\
  121. |/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)\n"), stdout);
  122. printf(format,'|','|','/', _("Name"), _("Version"), 40, _("Description"));
  123. printf("+++-"); /* status */
  124. for (l=0;l<nw;l++) printf("="); printf("-"); /* packagename */
  125. for (l=0;l<vw;l++) printf("="); printf("-"); /* version */
  126. for (l=0;l<dw;l++) printf("="); /* description */
  127. printf("\n");
  128. *head= 1;
  129. }
  130. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  131. limiteddescription(pkg,dw,&pdesc,&l);
  132. printf(format,
  133. "uihrp"[pkg->want],
  134. "nUFiHc"[pkg->status],
  135. " R?#"[pkg->eflag],
  136. pkg->name,
  137. versiondescribe(&pkg->installed.version,vdew_never),
  138. l, pdesc);
  139. }
  140. void listpackages(const char *const *argv) {
  141. struct pkgiterator *it;
  142. struct pkginfo *pkg;
  143. struct pkginfo **pkgl;
  144. const char *thisarg;
  145. int np, i, head, found;
  146. modstatdb_init(admindir,msdbrw_readonly);
  147. np= countpackages();
  148. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  149. it= iterpkgstart(); i=0;
  150. while ((pkg= iterpkgnext(it))) {
  151. assert(i<np);
  152. pkgl[i++]= pkg;
  153. }
  154. iterpkgend(it);
  155. assert(i==np);
  156. qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
  157. head=0;
  158. if (!*argv) {
  159. for (i=0; i<np; i++) {
  160. pkg= pkgl[i];
  161. if (pkg->status == stat_notinstalled) continue;
  162. list1package(pkg,&head);
  163. }
  164. } else {
  165. while ((thisarg= *argv++)) {
  166. found= 0;
  167. for (i=0; i<np; i++) {
  168. pkg= pkgl[i];
  169. if (fnmatch(thisarg,pkg->name,0)) continue;
  170. list1package(pkg,&head); found++;
  171. }
  172. if (!found) {
  173. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  174. nerrs++;
  175. }
  176. }
  177. }
  178. if (ferror(stdout)) werr("stdout");
  179. if (ferror(stderr)) werr("stderr");
  180. }
  181. static int searchoutput(struct filenamenode *namenode) {
  182. int found, i;
  183. struct filepackages *packageslump;
  184. if (namenode->divert) {
  185. for (i=0; i<2; i++) {
  186. if (namenode->divert->pkg) printf(_("diversion by %s"),namenode->divert->pkg->name);
  187. else printf(_("local diversion"));
  188. printf(" %s: %s\n", i ? _("to") : _("from"),
  189. i ?
  190. (namenode->divert->useinstead
  191. ? namenode->divert->useinstead->name
  192. : namenode->name)
  193. :
  194. (namenode->divert->camefrom
  195. ? namenode->divert->camefrom->name
  196. : namenode->name));
  197. }
  198. }
  199. found= 0;
  200. for (packageslump= namenode->packages;
  201. packageslump;
  202. packageslump= packageslump->more) {
  203. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  204. if (found) fputs(", ",stdout);
  205. fputs(packageslump->pkgs[i]->name,stdout);
  206. found++;
  207. }
  208. }
  209. if (found) printf(": %s\n",namenode->name);
  210. return found + (namenode->divert ? 1 : 0);
  211. }
  212. void searchfiles(const char *const *argv) {
  213. struct filenamenode *namenode;
  214. struct fileiterator *it;
  215. const char *thisarg;
  216. int found;
  217. static struct varbuf vb;
  218. if (!*argv)
  219. badusage(_("--search needs at least one file name pattern argument"));
  220. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  221. ensure_allinstfiles_available_quiet();
  222. ensure_diversions();
  223. while ((thisarg= *argv++) != 0) {
  224. found= 0;
  225. if (!strchr("*[?/",*thisarg)) {
  226. varbufreset(&vb);
  227. varbufaddc(&vb,'*');
  228. varbufaddstr(&vb,thisarg);
  229. varbufaddc(&vb,'*');
  230. varbufaddc(&vb,0);
  231. thisarg= vb.buf;
  232. }
  233. if (strcspn(thisarg,"*[?\\") == strlen(thisarg)) {
  234. namenode= findnamenode(thisarg, 0);
  235. found += searchoutput(namenode);
  236. } else {
  237. it= iterfilestart();
  238. while ((namenode= iterfilenext(it)) != 0) {
  239. if (fnmatch(thisarg,namenode->name,0)) continue;
  240. found+= searchoutput(namenode);
  241. }
  242. iterfileend(it);
  243. }
  244. if (!found) {
  245. fprintf(stderr,_("dpkg: %s not found.\n"),thisarg);
  246. nerrs++;
  247. if (ferror(stderr)) werr("stderr");
  248. } else {
  249. if (ferror(stdout)) werr("stdout");
  250. }
  251. }
  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++) != 0) {
  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) printf(_("locally diverted"));
  308. else if (pkg == namenode->divert->pkg) printf(_("package diverts others"));
  309. else printf(_("diverted by %s"),namenode->divert->pkg->name);
  310. printf(_(" to: %s\n"),namenode->divert->useinstead->name);
  311. }
  312. file= file->next;
  313. }
  314. }
  315. break;
  316. }
  317. break;
  318. default:
  319. internerr("unknown action");
  320. }
  321. putchar('\n');
  322. if (ferror(stdout)) werr("stdout");
  323. }
  324. if (failures) {
  325. nerrs++;
  326. fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  327. "and dpkg --contents (= dpkg-deb --contents) to list their contents.\n"),stderr);
  328. if (ferror(stdout)) werr("stdout");
  329. }
  330. }
  331. static void printversion(void) {
  332. if (fputs(_("Debian GNU/Linux `"), stdout) < 0) werr("stdout");
  333. if (fputs(DPKG, stdout) < 0) werr("stdout");
  334. if (fputs(_("' package management program version "), stdout) < 0) werr("stdout");
  335. // if (fputs( DPKG_VERSION_ARCH ".\n", stdout) < 0) werr("stdout");
  336. if (fputs(_( "This is free software; see the GNU General Public Licence version 2 or\n"
  337. "later for copying conditions. There is NO warranty.\n"
  338. "See dpkg --licence for copyright and license details.\n"),
  339. stdout) < 0) werr("stdout");
  340. }
  341. /*
  342. options that need fixing:
  343. dpkg --yet-to-unpack \n\
  344. */
  345. static void usage(void) {
  346. if (fprintf (stdout, _("\
  347. Usage: \n\
  348. dpkg -s|--status <package-name> ... display package status details\n\
  349. dpkg -p|--print-avail <package-name> ... display available version details\n\
  350. dpkg -L|--listfiles <package-name> ... list files `owned' by package(s)\n\
  351. dpkg -l|--list [<pattern> ...] list packages concisely\n\
  352. dpkg -S|--search <pattern> ... find package(s) owning file(s)\n\
  353. dpkg --help | --version show this help / version number\n\
  354. dpkg --licence print copyright licensing terms\n\
  355. \n\
  356. Options:\n\
  357. --admindir=<directory> Use <directory> instead of %s\n\
  358. -D|--debug=<octal> Enable debugging - see -Dhelp or --debug=help\n"),
  359. ADMINDIR) < 0) werr ("stdout");
  360. }
  361. const char thisname[]= "dpkg";
  362. const char printforhelp[]= N_("\
  363. Type dpkg-query --help for help about installing and deinstalling packages [*];\n\
  364. Use dselect for user-friendly package management;\n\
  365. Type dpkg-query --licence for copyright licence and lack of warranty (GNU GPL) [*].\n\
  366. \n");
  367. const struct cmdinfo *cipaction= 0;
  368. int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
  369. int f_autodeconf=0, f_nodebsig=0;
  370. unsigned long f_debug=0;
  371. /* Change fc_overwrite to 1 to enable force-overwrite by default */
  372. int fc_hold=0;
  373. int fc_conflicts=0, fc_depends=0;
  374. int fc_badpath=0;
  375. int errabort = 50;
  376. const char *admindir= ADMINDIR;
  377. const char *instdir= "";
  378. struct packageinlist *ignoredependss=0;
  379. static void helponly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  380. static void helponly(const struct cmdinfo *cip, const char *value) {
  381. usage(); exit(0);
  382. }
  383. static void versiononly(const struct cmdinfo *cip, const char *value) NONRETURNING;
  384. static void versiononly(const struct cmdinfo *cip, const char *value) {
  385. printversion(); exit(0);
  386. }
  387. static void setaction(const struct cmdinfo *cip, const char *value) {
  388. if (cipaction)
  389. badusage(_("conflicting actions --%s and --%s"),cip->olong,cipaction->olong);
  390. cipaction= cip;
  391. }
  392. static const struct cmdinfo cmdinfos[]= {
  393. /* This table has both the action entries in it and the normal options.
  394. * The action entries are made with the ACTION macro, as they all
  395. * have a very similar structure.
  396. */
  397. #define ACTION(longopt,shortopt,code,function) \
  398. { longopt, shortopt, 0,0,0, setaction, code, 0, (voidfnp)function }
  399. #define OBSOLETE(longopt,shortopt) \
  400. { longopt, shortopt, 0,0,0, setobsolete, 0, 0, 0 }
  401. ACTION( "listfiles", 'L', act_listfiles, enqperpackage ), //
  402. ACTION( "status", 's', act_status, enqperpackage ), //
  403. ACTION( "print-avail", 'p', act_printavail, enqperpackage ), //
  404. ACTION( "list", 'l', act_listpackages, listpackages ), //
  405. ACTION( "search", 'S', act_searchfiles, searchfiles ), //
  406. { "admindir", 0, 1, 0, &admindir, 0 },
  407. { "help", 'h', 0, 0, 0, helponly },
  408. { "version", 0, 0, 0, 0, versiononly },
  409. { "licence",/* UK spelling */ 0,0,0,0, showcopyright },
  410. { "license",/* US spelling */ 0,0,0,0, showcopyright },
  411. { 0, 0 }
  412. };
  413. int main(int argc, const char *const *argv) {
  414. jmp_buf ejbuf;
  415. static void (*actionfunction)(const char *const *argv);
  416. setlocale(LC_ALL, "");
  417. bindtextdomain(PACKAGE, LOCALEDIR);
  418. textdomain(PACKAGE);
  419. if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
  420. error_unwind(ehflag_bombout); exit(2);
  421. }
  422. push_error_handler(&ejbuf,print_error_fatal,0);
  423. umask(022); /* Make sure all our status databases are readable. */
  424. myopt(&argv,cmdinfos);
  425. if (!cipaction) badusage(_("need an action option"));
  426. setvbuf(stdout,0,_IONBF,0);
  427. filesdbinit();
  428. actionfunction= (void (*)(const char* const*))cipaction->farg;
  429. actionfunction(argv);
  430. set_error_display(0,0);
  431. error_unwind(ehflag_normaltidy);
  432. return reportbroken_retexitstatus();
  433. }