enquiry.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. int pkglistqsortcmp(const void *a, const void *b) {
  40. struct pkginfo *pa= *(struct pkginfo**)a;
  41. struct pkginfo *pb= *(struct pkginfo**)b;
  42. return strcmp(pa->name,pb->name);
  43. }
  44. static void limiteddescription(struct pkginfo *pkg, int maxl,
  45. const char **pdesc_r, int *l_r) {
  46. const char *pdesc, *p;
  47. int l;
  48. pdesc= pkg->installed.valid ? pkg->installed.description : 0;
  49. if (!pdesc) pdesc= _("(no description available)");
  50. p= strchr(pdesc,'\n');
  51. if (!p) p= pdesc+strlen(pdesc);
  52. l= (p - pdesc > maxl) ? maxl : (int)(p - pdesc);
  53. *pdesc_r=pdesc; *l_r=l;
  54. }
  55. static int getwidth() {
  56. int fd;
  57. int res;
  58. struct winsize ws;
  59. const char* columns;
  60. if ((columns=getenv("COLUMNS")) && ((res=atoi(columns))>0))
  61. ws.ws_col=res;
  62. else if (!isatty(1))
  63. ws.ws_col=80;
  64. else {
  65. if ((fd=open("/dev/tty",O_RDONLY))!=-1) {
  66. if (ioctl(fd, TIOCGWINSZ, &ws)==-1)
  67. ws.ws_col=80;
  68. close(fd);
  69. }
  70. }
  71. return ws.ws_col;
  72. }
  73. static void list1package(struct pkginfo *pkg, int *head) {
  74. int l,w;
  75. static int nw,vw,dw;
  76. const char *pdesc;
  77. static char format[80] = "";
  78. if (format[0]==0) {
  79. w=getwidth()-80; /* get spare width */
  80. if (w<0) w=0; /* lets not try to deal with terminals that are too small */
  81. w>>=2; /* halve that so we can add that to the both the name and description */
  82. nw=(14+w); /* name width */
  83. vw=(14+w); /* version width */
  84. dw=(44+(2*w)); /* description width */
  85. sprintf(format,"%%c%%c%%c %%-%d.%ds %%-%d.%ds %%.*s\n", nw, nw, vw, vw);
  86. }
  87. if (!*head) {
  88. fputs(_("\
  89. Desired=Unknown/Install/Remove/Purge/Hold\n\
  90. | Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed\n\
  91. |/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)\n"), stdout);
  92. printf(format,'|','|','/', _("Name"), _("Version"), 40, _("Description"));
  93. printf("+++-"); /* status */
  94. for (l=0;l<nw;l++) printf("="); printf("-"); /* packagename */
  95. for (l=0;l<vw;l++) printf("="); printf("-"); /* version */
  96. for (l=0;l<dw;l++) printf("="); /* description */
  97. printf("\n");
  98. *head= 1;
  99. }
  100. if (!pkg->installed.valid) blankpackageperfile(&pkg->installed);
  101. limiteddescription(pkg,dw,&pdesc,&l);
  102. printf(format,
  103. "uihrp"[pkg->want],
  104. "nUFiHc"[pkg->status],
  105. " R?#"[pkg->eflag],
  106. pkg->name,
  107. versiondescribe(&pkg->installed.version,vdew_never),
  108. l, pdesc);
  109. }
  110. void listpackages(const char *const *argv) {
  111. struct pkgiterator *it;
  112. struct pkginfo *pkg;
  113. struct pkginfo **pkgl;
  114. const char *thisarg;
  115. int np, i, head, found;
  116. modstatdb_init(admindir,msdbrw_readonly);
  117. np= countpackages();
  118. pkgl= m_malloc(sizeof(struct pkginfo*)*np);
  119. it= iterpkgstart(); i=0;
  120. while ((pkg= iterpkgnext(it))) {
  121. assert(i<np);
  122. pkgl[i++]= pkg;
  123. }
  124. iterpkgend(it);
  125. assert(i==np);
  126. qsort(pkgl,np,sizeof(struct pkginfo*),pkglistqsortcmp);
  127. head=0;
  128. if (!*argv) {
  129. for (i=0; i<np; i++) {
  130. pkg= pkgl[i];
  131. if (pkg->status == stat_notinstalled) continue;
  132. list1package(pkg,&head);
  133. }
  134. } else {
  135. while ((thisarg= *argv++)) {
  136. found= 0;
  137. for (i=0; i<np; i++) {
  138. pkg= pkgl[i];
  139. if (fnmatch(thisarg,pkg->name,0)) continue;
  140. list1package(pkg,&head); found++;
  141. }
  142. if (!found)
  143. fprintf(stderr,_("No packages found matching %s.\n"),thisarg);
  144. }
  145. }
  146. if (ferror(stdout)) werr("stdout");
  147. if (ferror(stderr)) werr("stderr");
  148. }
  149. struct badstatinfo {
  150. int (*yesno)(struct pkginfo*, const struct badstatinfo *bsi);
  151. int val;
  152. const char *explanation;
  153. };
  154. static int bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  155. return pkg->eflag &= eflagf_reinstreq;
  156. }
  157. static int bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  158. if (pkg->eflag &= eflagf_reinstreq) return 0;
  159. return pkg->status == bsi->val;
  160. }
  161. static const struct badstatinfo badstatinfos[]= {
  162. {
  163. bsyn_reinstreq, 0,
  164. N_("The following packages are in a mess due to serious problems during\n"
  165. "installation. They must be reinstalled for them (and any packages\n"
  166. "that depend on them) to function properly:\n")
  167. }, {
  168. bsyn_status, stat_unpacked,
  169. N_("The following packages have been unpacked but not yet configured.\n"
  170. "They must be configured using dpkg --configure or the configure\n"
  171. "menu option in dselect for them to work:\n")
  172. }, {
  173. bsyn_status, stat_halfconfigured,
  174. N_("The following packages are only half configured, probably due to problems\n"
  175. "configuring them the first time. The configuration should be retried using\n"
  176. "dpkg --configure <package> or the configure menu option in dselect:\n")
  177. }, {
  178. bsyn_status, stat_halfinstalled,
  179. N_("The following packages are only half installed, due to problems during\n"
  180. "installation. The installation can probably be completed by retrying it;\n"
  181. "the packages can be removed using dselect or dpkg --remove:\n")
  182. }, {
  183. 0
  184. }
  185. };
  186. static void describebriefly(struct pkginfo *pkg) {
  187. int maxl, l;
  188. const char *pdesc;
  189. maxl= 57;
  190. l= strlen(pkg->name);
  191. if (l>20) maxl -= (l-20);
  192. limiteddescription(pkg,maxl,&pdesc,&l);
  193. printf(" %-20s %.*s\n",pkg->name,l,pdesc);
  194. }
  195. void audit(const char *const *argv) {
  196. struct pkgiterator *it;
  197. struct pkginfo *pkg;
  198. const struct badstatinfo *bsi;
  199. int head;
  200. if (*argv) badusage(_("--audit does not take any arguments"));
  201. modstatdb_init(admindir,msdbrw_readonly);
  202. for (bsi= badstatinfos; bsi->yesno; bsi++) {
  203. head= 0;
  204. it= iterpkgstart();
  205. while ((pkg= iterpkgnext(it))) {
  206. if (!bsi->yesno(pkg,bsi)) continue;
  207. if (!head) {
  208. fputs(gettext(bsi->explanation),stdout);
  209. head= 1;
  210. }
  211. describebriefly(pkg);
  212. }
  213. iterpkgend(it);
  214. if (head) putchar('\n');
  215. }
  216. if (ferror(stderr)) werr("stderr");
  217. }
  218. struct sectionentry {
  219. struct sectionentry *next;
  220. const char *name;
  221. int count;
  222. };
  223. static int yettobeunpacked(struct pkginfo *pkg, const char **thissect) {
  224. if (pkg->want != want_install) return 0;
  225. switch (pkg->status) {
  226. case stat_unpacked: case stat_installed: case stat_halfconfigured:
  227. return 0;
  228. case stat_notinstalled: case stat_halfinstalled: case stat_configfiles:
  229. if (thissect)
  230. *thissect= pkg->section && *pkg->section ? pkg->section : _("<unknown>");
  231. return 1;
  232. default:
  233. internerr("unknown status checking for unpackedness");
  234. }
  235. return 0;
  236. }
  237. void unpackchk(const char *const *argv) {
  238. int totalcount, sects;
  239. struct sectionentry *sectionentries, *se, **sep;
  240. struct pkgiterator *it;
  241. struct pkginfo *pkg;
  242. const char *thissect;
  243. char buf[20];
  244. int width;
  245. if (*argv) badusage(_("--yet-to-unpack does not take any arguments"));
  246. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  247. totalcount= 0;
  248. sectionentries= 0;
  249. sects= 0;
  250. it= iterpkgstart();
  251. while ((pkg= iterpkgnext(it))) {
  252. if (!yettobeunpacked(pkg, &thissect)) continue;
  253. for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
  254. if (!se) {
  255. se= nfmalloc(sizeof(struct sectionentry));
  256. for (sep= &sectionentries;
  257. *sep && strcasecmp(thissect,(*sep)->name) > 0;
  258. sep= &(*sep)->next);
  259. se->name= thissect;
  260. se->count= 0;
  261. se->next= *sep;
  262. *sep= se;
  263. sects++;
  264. }
  265. se->count++; totalcount++;
  266. }
  267. iterpkgend(it);
  268. if (totalcount == 0) exit(0);
  269. if (totalcount <= 12) {
  270. it= iterpkgstart();
  271. while ((pkg= iterpkgnext(it))) {
  272. if (!yettobeunpacked(pkg,0)) continue;
  273. describebriefly(pkg);
  274. }
  275. iterpkgend(it);
  276. } else if (sects <= 12) {
  277. for (se= sectionentries; se; se= se->next) {
  278. sprintf(buf,"%d",se->count);
  279. printf(_(" %d in %s: "),se->count,se->name);
  280. width= 70-strlen(se->name)-strlen(buf);
  281. while (width > 59) { putchar(' '); width--; }
  282. it= iterpkgstart();
  283. while ((pkg= iterpkgnext(it))) {
  284. if (!yettobeunpacked(pkg,&thissect)) continue;
  285. if (strcasecmp(thissect,se->name)) continue;
  286. width -= strlen(pkg->name); width--;
  287. if (width < 4) { printf(" ..."); break; }
  288. printf(" %s",pkg->name);
  289. }
  290. iterpkgend(it);
  291. putchar('\n');
  292. }
  293. } else {
  294. printf(_(" %d packages, from the following sections:"),totalcount);
  295. width= 0;
  296. for (se= sectionentries; se; se= se->next) {
  297. sprintf(buf,"%d",se->count);
  298. width -= (6 + strlen(se->name) + strlen(buf));
  299. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  300. printf(" %s (%d)",se->name,se->count);
  301. }
  302. putchar('\n');
  303. }
  304. fflush(stdout);
  305. if (ferror(stdout)) werr("stdout");
  306. }
  307. static int searchoutput(struct filenamenode *namenode) {
  308. int found, i;
  309. struct filepackages *packageslump;
  310. if (namenode->divert) {
  311. for (i=0; i<2; i++) {
  312. if (namenode->divert->pkg) printf(_("diversion by %s"),namenode->divert->pkg->name);
  313. else printf(_("local diversion"));
  314. printf(" %s: %s\n", i ? _("to") : _("from"),
  315. i ?
  316. (namenode->divert->useinstead
  317. ? namenode->divert->useinstead->name
  318. : namenode->name)
  319. :
  320. (namenode->divert->camefrom
  321. ? namenode->divert->camefrom->name
  322. : namenode->name));
  323. }
  324. }
  325. found= 0;
  326. for (packageslump= namenode->packages;
  327. packageslump;
  328. packageslump= packageslump->more) {
  329. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  330. if (found) fputs(", ",stdout);
  331. fputs(packageslump->pkgs[i]->name,stdout);
  332. found++;
  333. }
  334. }
  335. if (found) printf(": %s\n",namenode->name);
  336. return found + (namenode->divert ? 1 : 0);
  337. }
  338. void searchfiles(const char *const *argv) {
  339. struct filenamenode *namenode;
  340. struct fileiterator *it;
  341. const char *thisarg;
  342. int found;
  343. static struct varbuf vb;
  344. if (!*argv)
  345. badusage(_("--search needs at least one file name pattern argument"));
  346. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  347. ensure_allinstfiles_available_quiet();
  348. ensure_diversions();
  349. while ((thisarg= *argv++) != 0) {
  350. found= 0;
  351. if (!strchr("*[?/",*thisarg)) {
  352. varbufreset(&vb);
  353. varbufaddc(&vb,'*');
  354. varbufaddstr(&vb,thisarg);
  355. varbufaddc(&vb,'*');
  356. varbufaddc(&vb,0);
  357. thisarg= vb.buf;
  358. }
  359. if (strcspn(thisarg,"*[?\\") == strlen(thisarg)) {
  360. namenode= findnamenode(thisarg, 0);
  361. found += searchoutput(namenode);
  362. } else {
  363. it= iterfilestart();
  364. while ((namenode= iterfilenext(it)) != 0) {
  365. if (fnmatch(thisarg,namenode->name,0)) continue;
  366. found+= searchoutput(namenode);
  367. }
  368. iterfileend(it);
  369. }
  370. if (!found) {
  371. fprintf(stderr,_("dpkg: %s not found.\n"),thisarg);
  372. if (ferror(stderr)) werr("stderr");
  373. } else {
  374. if (ferror(stdout)) werr("stdout");
  375. }
  376. }
  377. }
  378. void enqperpackage(const char *const *argv) {
  379. int failures;
  380. const char *thisarg;
  381. struct fileinlist *file;
  382. struct pkginfo *pkg;
  383. struct filenamenode *namenode;
  384. if (!*argv)
  385. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  386. failures= 0;
  387. if (cipaction->arg==act_listfiles)
  388. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  389. else
  390. modstatdb_init(admindir,msdbrw_readonly);
  391. while ((thisarg= *argv++) != 0) {
  392. pkg= findpackage(thisarg);
  393. switch (cipaction->arg) {
  394. case act_status:
  395. if (pkg->status == stat_notinstalled &&
  396. pkg->priority == pri_unknown &&
  397. !(pkg->section && *pkg->section) &&
  398. !pkg->files &&
  399. pkg->want == want_unknown &&
  400. !informative(pkg,&pkg->installed)) {
  401. printf(_("Package `%s' is not installed and no info is available.\n"),pkg->name);
  402. failures++;
  403. } else {
  404. writerecord(stdout, "<stdout>", pkg, &pkg->installed);
  405. }
  406. break;
  407. case act_printavail:
  408. if (!informative(pkg,&pkg->available)) {
  409. printf(_("Package `%s' is not available.\n"),pkg->name);
  410. failures++;
  411. } else {
  412. writerecord(stdout, "<stdout>", pkg, &pkg->available);
  413. }
  414. break;
  415. case act_listfiles:
  416. switch (pkg->status) {
  417. case stat_notinstalled:
  418. printf(_("Package `%s' is not installed.\n"),pkg->name);
  419. failures++;
  420. break;
  421. default:
  422. ensure_packagefiles_available(pkg);
  423. ensure_diversions();
  424. file= pkg->clientdata->files;
  425. if (!file) {
  426. printf(_("Package `%s' does not contain any files (!)\n"),pkg->name);
  427. } else {
  428. while (file) {
  429. namenode= file->namenode;
  430. puts(namenode->name);
  431. if (namenode->divert && !namenode->divert->camefrom) {
  432. if (!namenode->divert->pkg) printf(_("locally diverted"));
  433. else if (pkg == namenode->divert->pkg) printf(_("package diverts others"));
  434. else printf(_("diverted by %s"),namenode->divert->pkg->name);
  435. printf(_(" to: %s\n"),namenode->divert->useinstead->name);
  436. }
  437. file= file->next;
  438. }
  439. }
  440. break;
  441. }
  442. break;
  443. default:
  444. internerr("unknown action");
  445. }
  446. putchar('\n');
  447. if (ferror(stdout)) werr("stdout");
  448. }
  449. if (failures) {
  450. puts(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  451. "and dpkg --contents (= dpkg-deb --contents) to list their contents."));
  452. if (ferror(stdout)) werr("stdout");
  453. }
  454. }
  455. static void assertversion(const char *const *argv,
  456. struct versionrevision *verrev_buf,
  457. const char *reqversion) {
  458. struct pkginfo *pkg;
  459. if (*argv) badusage(_("--assert-* does not take any arguments"));
  460. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  461. if (verrev_buf->epoch == ~0UL) {
  462. verrev_buf->epoch= 0;
  463. verrev_buf->version= nfstrsave(reqversion);
  464. verrev_buf->revision= 0;
  465. }
  466. pkg= findpackage("dpkg");
  467. switch (pkg->status) {
  468. case stat_installed:
  469. break;
  470. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  471. if (versionsatisfied3(&pkg->configversion,verrev_buf,dvr_laterequal))
  472. break;
  473. printf(_("Version of dpkg with working epoch support not yet configured.\n"
  474. " Please use `dpkg --configure dpkg', and then try again.\n"));
  475. exit(1);
  476. default:
  477. printf(_("dpkg not recorded as installed, cannot check for epoch support !\n"));
  478. exit(1);
  479. }
  480. }
  481. void assertpredep(const char *const *argv) {
  482. static struct versionrevision predepversion = {~0UL,0,0};
  483. assertversion(argv,&predepversion,"1.1.0");
  484. }
  485. void assertepoch(const char *const *argv) {
  486. static struct versionrevision epochversion = {~0UL,0,0};
  487. assertversion(argv,&epochversion,"1.4.0.7");
  488. }
  489. void assertlongfilenames(const char *const *argv) {
  490. static struct versionrevision epochversion = {~0UL,0,0};
  491. assertversion(argv,&epochversion,"1.4.1.17");
  492. }
  493. void assertmulticonrep(const char *const *argv) {
  494. static struct versionrevision epochversion = {~0UL,0,0};
  495. assertversion(argv,&epochversion,"1.4.1.19");
  496. }
  497. void predeppackage(const char *const *argv) {
  498. /* Print a single package which:
  499. * (a) is the target of one or more relevant predependencies.
  500. * (b) has itself no unsatisfied pre-dependencies.
  501. * If such a package is present output is the Packages file entry,
  502. * which can be massaged as appropriate.
  503. * Exit status:
  504. * 0 = a package printed, OK
  505. * 1 = no suitable package available
  506. * 2 = error
  507. */
  508. static struct varbuf vb;
  509. struct pkgiterator *it;
  510. struct pkginfo *pkg= 0, *startpkg, *trypkg;
  511. struct dependency *dep;
  512. struct deppossi *possi, *provider;
  513. if (*argv) badusage(_("--predep-package does not take any argument"));
  514. modstatdb_init(admindir,msdbrw_readonly);
  515. clear_istobes(); /* We use clientdata->istobe to detect loops */
  516. for (it=iterpkgstart(), dep=0;
  517. !dep && (pkg=iterpkgnext(it));
  518. ) {
  519. if (pkg->want != want_install) continue; /* Ignore packages user doesn't want */
  520. if (!pkg->files) continue; /* Ignore packages not available */
  521. pkg->clientdata->istobe= itb_preinstall;
  522. for (dep= pkg->available.depends; dep; dep= dep->next) {
  523. if (dep->type != dep_predepends) continue;
  524. if (depisok(dep,&vb,0,1)) continue;
  525. break; /* This will leave dep non-NULL, and so exit the loop. */
  526. }
  527. pkg->clientdata->istobe= itb_normal;
  528. /* If dep is NULL we go and get the next package. */
  529. }
  530. if (!dep) exit(1); /* Not found */
  531. assert(pkg);
  532. startpkg= pkg;
  533. pkg->clientdata->istobe= itb_preinstall;
  534. /* OK, we have found an unsatisfied predependency.
  535. * Now go and find the first thing we need to install, as a first step
  536. * towards satisfying it.
  537. */
  538. do {
  539. /* We search for a package which would satisfy dep, and put it in pkg */
  540. for (possi=dep->list, pkg=0;
  541. !pkg && possi;
  542. possi=possi->next) {
  543. trypkg= possi->ed;
  544. if (!trypkg->available.valid) continue;
  545. if (trypkg->files && versionsatisfied(&trypkg->available,possi)) {
  546. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  547. }
  548. if (possi->verrel != dvr_none) continue;
  549. for (provider=possi->ed->available.depended;
  550. !pkg && provider;
  551. provider=provider->next) {
  552. if (provider->up->type != dep_provides) continue;
  553. trypkg= provider->up->up;
  554. if (!trypkg->available.valid || !trypkg->files) continue;
  555. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  556. }
  557. }
  558. if (!pkg) {
  559. varbufreset(&vb);
  560. describedepcon(&vb,dep);
  561. varbufaddc(&vb,0);
  562. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  563. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  564. dep->up->name,startpkg->name);
  565. }
  566. pkg->clientdata->istobe= itb_preinstall;
  567. for (dep= pkg->available.depends; dep; dep= dep->next) {
  568. if (dep->type != dep_predepends) continue;
  569. if (depisok(dep,&vb,0,1)) continue;
  570. break; /* This will leave dep non-NULL, and so exit the loop. */
  571. }
  572. } while (dep);
  573. /* OK, we've found it - pkg has no unsatisfied pre-dependencies ! */
  574. writerecord(stdout,"<stdout>",pkg,&pkg->available);
  575. if (fflush(stdout)) werr("stdout");
  576. }
  577. static void badlgccfn(const char *compiler, const char *output, const char *why)
  578. NONRETURNING;
  579. static void badlgccfn(const char *compiler, const char *output, const char *why) {
  580. fprintf(stderr,
  581. _("dpkg: unexpected output from `%s --print-libgcc-file-name':\n"
  582. " `%s'\n"),
  583. compiler,output);
  584. ohshit(_("compiler libgcc filename not understood: %.250s"),why);
  585. }
  586. void printinstarch(const char *const *argv) {
  587. if (*argv) badusage(_("--print-installation-architecture does not take any argument"));
  588. if (printf("%s\n",architecture) == EOF) werr("stdout");
  589. if (fflush(stdout)) werr("stdout");
  590. }
  591. void printarch(const char *const *argv) {
  592. static const struct { const char *from, *to, *gnu; } archtable[]= {
  593. #include "archtable.h"
  594. { 0,0,0 }
  595. }, *archp;
  596. const char *ccompiler, *arch= NULL;
  597. int p1[2];
  598. pid_t c1;
  599. FILE *ccpipe;
  600. struct varbuf vb;
  601. ptrdiff_t ll;
  602. char *p, *q;
  603. if (*argv) badusage(_("--print-architecture does not take any argument"));
  604. ccompiler= getenv("CC");
  605. if (!ccompiler) ccompiler= "gcc";
  606. varbufinit(&vb);
  607. m_pipe(p1);
  608. ccpipe= fdopen(p1[0],"r"); if (!ccpipe) ohshite(_("failed to fdopen CC pipe"));
  609. if (!(c1= m_fork())) {
  610. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  611. execlp(ccompiler,ccompiler,"--print-libgcc-file-name",(char*)0);
  612. /* if we have a problem excuting the C compiler, we don't
  613. * want to fail. If there is a problem with the compiler,
  614. * like not being installed, or CC being set incorrectly,
  615. * then important problems will show up elsewhere, not in
  616. * dpkg. If a C compiler is not important to the reason we
  617. * are being called, then we should just give them the built
  618. * in arch.
  619. */
  620. if (printf("/usr/lib/gcc-lib/%s-none/0.0.0/libgcc.a\n",architecture) == EOF)
  621. werr("stdout");
  622. if (fflush(stdout)) werr("stdout");
  623. exit(0);
  624. }
  625. close(p1[1]);
  626. fd_vbuf_copy(fileno(ccpipe), &vb, -1, _("error reading from CC pipe"));
  627. waitsubproc(c1,"gcc --print-libgcc-file-name",0,0);
  628. if (!vb.used) badlgccfn(ccompiler,"",_("empty output"));
  629. varbufaddc(&vb,0);
  630. if (vb.buf[vb.used-2] != '\n') badlgccfn(ccompiler,vb.buf,_("no newline"));
  631. vb.used-= 2; varbufaddc(&vb,0);
  632. p= strstr(vb.buf,"/gcc-lib/");
  633. if (!p) badlgccfn(ccompiler,vb.buf,_("no gcc-lib component"));
  634. p+= 9;
  635. q= strchr(p,'/'); if (!q) badlgccfn(ccompiler,vb.buf,_("no slash after gcc-lib"));
  636. ll= q-p;
  637. for (archp=archtable;
  638. archp->from && strncmp(archp->from,p,ll);
  639. archp++);
  640. switch (cipaction->arg) {
  641. case act_printarch: arch= archp->to; break;
  642. case act_printgnuarch: arch= archp->gnu; break;
  643. default: internerr("unknown action in printarch");
  644. }
  645. if (!arch) {
  646. *q= 0; arch= p;
  647. fprintf(stderr, _("dpkg: warning, architecture `%s' not in remapping table\n"),arch);
  648. }
  649. if (printf("%s\n",arch) == EOF) werr("stdout");
  650. if (fflush(stdout)) werr("stdout");
  651. }
  652. void cmpversions(const char *const *argv) {
  653. struct relationinfo {
  654. const char *string;
  655. /* These values are exit status codes, so 0=true, 1=false */
  656. int if_lesser, if_equal, if_greater;
  657. int if_none_a, if_none_both, if_none_b;
  658. };
  659. static const struct relationinfo relationinfos[]= {
  660. /* < = > !a!2!b */
  661. { "le", 0,0,1, 0,0,1 },
  662. { "lt", 0,1,1, 0,1,1 },
  663. { "eq", 1,0,1, 1,0,1 },
  664. { "ne", 0,1,0, 0,1,0 },
  665. { "ge", 1,0,0, 1,0,0 },
  666. { "gt", 1,1,0, 1,1,0 },
  667. { "le-nl", 0,0,1, 1,0,0 }, /* Here none */
  668. { "lt-nl", 0,1,1, 1,1,0 }, /* is counted */
  669. { "ge-nl", 1,0,0, 0,0,1 }, /* than any version.*/
  670. { "gt-nl", 1,1,0, 0,1,1 }, /* */
  671. { "<", 0,0,1, 0,0,1 }, /* For compatibility*/
  672. { "<=", 0,0,1, 0,0,1 }, /* with dpkg */
  673. { "<<", 0,1,1, 0,1,1 }, /* control file */
  674. { "=", 1,0,1, 1,0,1 }, /* syntax */
  675. { ">", 1,0,0, 1,0,0 }, /* */
  676. { ">=", 1,0,0, 1,0,0 },
  677. { ">>", 1,1,0, 1,1,0 },
  678. { 0 }
  679. };
  680. const struct relationinfo *rip;
  681. const char *emsg;
  682. struct versionrevision a, b;
  683. int r;
  684. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  685. badusage(_("--compare-versions takes three arguments:"
  686. " <version> <relation> <version>"));
  687. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  688. if (!rip->string) badusage(_("--compare-versions bad relation"));
  689. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  690. emsg= parseversion(&a,argv[0]);
  691. if (emsg) {
  692. if (printf(_("version a has bad syntax: %s\n"),emsg) == EOF) werr("stdout");
  693. if (fflush(stdout)) werr("stdout");
  694. exit(1);
  695. }
  696. } else {
  697. blankversion(&a);
  698. }
  699. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  700. emsg= parseversion(&b,argv[2]);
  701. if (emsg) {
  702. if (printf(_("version b has bad syntax: %s\n"),emsg) == EOF) werr("stdout");
  703. if (fflush(stdout)) werr("stdout");
  704. exit(1);
  705. }
  706. } else {
  707. blankversion(&b);
  708. }
  709. if (!informativeversion(&a)) {
  710. exit(informativeversion(&b) ? rip->if_none_a : rip->if_none_both);
  711. } else if (!informativeversion(&b)) {
  712. exit(rip->if_none_b);
  713. }
  714. r= versioncompare(&a,&b);
  715. debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
  716. versiondescribe(&a,vdew_always),
  717. versiondescribe(&b,vdew_always),
  718. r);
  719. if (r>0) exit(rip->if_greater);
  720. else if (r<0) exit(rip->if_lesser);
  721. else exit(rip->if_equal);
  722. }