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. }
  236. void unpackchk(const char *const *argv) {
  237. int totalcount, sects;
  238. struct sectionentry *sectionentries, *se, **sep;
  239. struct pkgiterator *it;
  240. struct pkginfo *pkg;
  241. const char *thissect;
  242. char buf[20];
  243. int width;
  244. if (*argv) badusage(_("--yet-to-unpack does not take any arguments"));
  245. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  246. totalcount= 0;
  247. sectionentries= 0;
  248. sects= 0;
  249. it= iterpkgstart();
  250. while ((pkg= iterpkgnext(it))) {
  251. if (!yettobeunpacked(pkg, &thissect)) continue;
  252. for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
  253. if (!se) {
  254. se= nfmalloc(sizeof(struct sectionentry));
  255. for (sep= &sectionentries;
  256. *sep && strcasecmp(thissect,(*sep)->name) > 0;
  257. sep= &(*sep)->next);
  258. se->name= thissect;
  259. se->count= 0;
  260. se->next= *sep;
  261. *sep= se;
  262. sects++;
  263. }
  264. se->count++; totalcount++;
  265. }
  266. iterpkgend(it);
  267. if (totalcount == 0) exit(0);
  268. if (totalcount <= 12) {
  269. it= iterpkgstart();
  270. while ((pkg= iterpkgnext(it))) {
  271. if (!yettobeunpacked(pkg,0)) continue;
  272. describebriefly(pkg);
  273. }
  274. iterpkgend(it);
  275. } else if (sects <= 12) {
  276. for (se= sectionentries; se; se= se->next) {
  277. sprintf(buf,"%d",se->count);
  278. printf(_(" %d in %s: "),se->count,se->name);
  279. width= 70-strlen(se->name)-strlen(buf);
  280. while (width > 59) { putchar(' '); width--; }
  281. it= iterpkgstart();
  282. while ((pkg= iterpkgnext(it))) {
  283. if (!yettobeunpacked(pkg,&thissect)) continue;
  284. if (strcasecmp(thissect,se->name)) continue;
  285. width -= strlen(pkg->name); width--;
  286. if (width < 4) { printf(" ..."); break; }
  287. printf(" %s",pkg->name);
  288. }
  289. iterpkgend(it);
  290. putchar('\n');
  291. }
  292. } else {
  293. printf(_(" %d packages, from the following sections:"),totalcount);
  294. width= 0;
  295. for (se= sectionentries; se; se= se->next) {
  296. sprintf(buf,"%d",se->count);
  297. width -= (6 + strlen(se->name) + strlen(buf));
  298. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  299. printf(" %s (%d)",se->name,se->count);
  300. }
  301. putchar('\n');
  302. }
  303. fflush(stdout);
  304. if (ferror(stdout)) werr("stdout");
  305. }
  306. static int searchoutput(struct filenamenode *namenode) {
  307. int found, i;
  308. struct filepackages *packageslump;
  309. if (namenode->divert) {
  310. for (i=0; i<2; i++) {
  311. if (namenode->divert->pkg) printf(_("diversion by %s"),namenode->divert->pkg->name);
  312. else printf(_("local diversion"));
  313. printf(" %s: %s\n", i ? _("to") : _("from"),
  314. i ?
  315. (namenode->divert->useinstead
  316. ? namenode->divert->useinstead->name
  317. : namenode->name)
  318. :
  319. (namenode->divert->camefrom
  320. ? namenode->divert->camefrom->name
  321. : namenode->name));
  322. }
  323. }
  324. found= 0;
  325. for (packageslump= namenode->packages;
  326. packageslump;
  327. packageslump= packageslump->more) {
  328. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  329. if (found) fputs(", ",stdout);
  330. fputs(packageslump->pkgs[i]->name,stdout);
  331. found++;
  332. }
  333. }
  334. if (found) printf(": %s\n",namenode->name);
  335. return found + (namenode->divert ? 1 : 0);
  336. }
  337. void searchfiles(const char *const *argv) {
  338. struct filenamenode *namenode;
  339. struct fileiterator *it;
  340. const char *thisarg;
  341. int found;
  342. static struct varbuf vb;
  343. if (!*argv)
  344. badusage(_("--search needs at least one file name pattern argument"));
  345. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  346. ensure_allinstfiles_available_quiet();
  347. ensure_diversions();
  348. while ((thisarg= *argv++) != 0) {
  349. found= 0;
  350. if (!strchr("*[?/",*thisarg)) {
  351. varbufreset(&vb);
  352. varbufaddc(&vb,'*');
  353. varbufaddstr(&vb,thisarg);
  354. varbufaddc(&vb,'*');
  355. varbufaddc(&vb,0);
  356. thisarg= vb.buf;
  357. }
  358. if (strcspn(thisarg,"*[?\\") == strlen(thisarg)) {
  359. namenode= findnamenode(thisarg, 0);
  360. found += searchoutput(namenode);
  361. } else {
  362. it= iterfilestart();
  363. while ((namenode= iterfilenext(it)) != 0) {
  364. if (fnmatch(thisarg,namenode->name,0)) continue;
  365. found+= searchoutput(namenode);
  366. }
  367. iterfileend(it);
  368. }
  369. if (!found) {
  370. fprintf(stderr,_("dpkg: %s not found.\n"),thisarg);
  371. if (ferror(stderr)) werr("stderr");
  372. } else {
  373. if (ferror(stdout)) werr("stdout");
  374. }
  375. }
  376. }
  377. void enqperpackage(const char *const *argv) {
  378. int failures;
  379. const char *thisarg;
  380. struct fileinlist *file;
  381. struct pkginfo *pkg;
  382. struct filenamenode *namenode;
  383. if (!*argv)
  384. badusage(_("--%s needs at least one package name argument"), cipaction->olong);
  385. failures= 0;
  386. if (cipaction->arg==act_listfiles)
  387. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  388. else
  389. modstatdb_init(admindir,msdbrw_readonly);
  390. while ((thisarg= *argv++) != 0) {
  391. pkg= findpackage(thisarg);
  392. switch (cipaction->arg) {
  393. case act_status:
  394. if (pkg->status == stat_notinstalled &&
  395. pkg->priority == pri_unknown &&
  396. !(pkg->section && *pkg->section) &&
  397. !pkg->files &&
  398. pkg->want == want_unknown &&
  399. !informative(pkg,&pkg->installed)) {
  400. printf(_("Package `%s' is not installed and no info is available.\n"),pkg->name);
  401. failures++;
  402. } else {
  403. writerecord(stdout, "<stdout>", pkg, &pkg->installed);
  404. }
  405. break;
  406. case act_printavail:
  407. if (!informative(pkg,&pkg->available)) {
  408. printf(_("Package `%s' is not available.\n"),pkg->name);
  409. failures++;
  410. } else {
  411. writerecord(stdout, "<stdout>", pkg, &pkg->available);
  412. }
  413. break;
  414. case act_listfiles:
  415. switch (pkg->status) {
  416. case stat_notinstalled:
  417. printf(_("Package `%s' is not installed.\n"),pkg->name);
  418. failures++;
  419. break;
  420. default:
  421. ensure_packagefiles_available(pkg);
  422. ensure_diversions();
  423. file= pkg->clientdata->files;
  424. if (!file) {
  425. printf(_("Package `%s' does not contain any files (!)\n"),pkg->name);
  426. } else {
  427. while (file) {
  428. namenode= file->namenode;
  429. puts(namenode->name);
  430. if (namenode->divert && !namenode->divert->camefrom) {
  431. if (!namenode->divert->pkg) printf(_("locally diverted"));
  432. else if (pkg == namenode->divert->pkg) printf(_("package diverts others"));
  433. else printf(_("diverted by %s"),namenode->divert->pkg->name);
  434. printf(_(" to: %s\n"),namenode->divert->useinstead->name);
  435. }
  436. file= file->next;
  437. }
  438. }
  439. break;
  440. }
  441. break;
  442. default:
  443. internerr("unknown action");
  444. }
  445. putchar('\n');
  446. if (ferror(stdout)) werr("stdout");
  447. }
  448. if (failures) {
  449. puts(_("Use dpkg --info (= dpkg-deb --info) to examine archive files,\n"
  450. "and dpkg --contents (= dpkg-deb --contents) to list their contents."));
  451. if (ferror(stdout)) werr("stdout");
  452. }
  453. }
  454. static void assertversion(const char *const *argv,
  455. struct versionrevision *verrev_buf,
  456. const char *reqversion) {
  457. struct pkginfo *pkg;
  458. if (*argv) badusage(_("--assert-* does not take any arguments"));
  459. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  460. if (verrev_buf->epoch == ~0UL) {
  461. verrev_buf->epoch= 0;
  462. verrev_buf->version= nfstrsave(reqversion);
  463. verrev_buf->revision= 0;
  464. }
  465. pkg= findpackage("dpkg");
  466. switch (pkg->status) {
  467. case stat_installed:
  468. break;
  469. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  470. if (versionsatisfied3(&pkg->configversion,verrev_buf,dvr_laterequal))
  471. break;
  472. printf(_("Version of dpkg with working epoch support not yet configured.\n"
  473. " Please use `dpkg --configure dpkg', and then try again.\n"));
  474. exit(1);
  475. default:
  476. printf(_("dpkg not recorded as installed, cannot check for epoch support !\n"));
  477. exit(1);
  478. }
  479. }
  480. void assertpredep(const char *const *argv) {
  481. static struct versionrevision predepversion = {~0UL,0,0};
  482. assertversion(argv,&predepversion,"1.1.0");
  483. }
  484. void assertepoch(const char *const *argv) {
  485. static struct versionrevision epochversion = {~0UL,0,0};
  486. assertversion(argv,&epochversion,"1.4.0.7");
  487. }
  488. void assertlongfilenames(const char *const *argv) {
  489. static struct versionrevision epochversion = {~0UL,0,0};
  490. assertversion(argv,&epochversion,"1.4.1.17");
  491. }
  492. void assertmulticonrep(const char *const *argv) {
  493. static struct versionrevision epochversion = {~0UL,0,0};
  494. assertversion(argv,&epochversion,"1.4.1.19");
  495. }
  496. void predeppackage(const char *const *argv) {
  497. /* Print a single package which:
  498. * (a) is the target of one or more relevant predependencies.
  499. * (b) has itself no unsatisfied pre-dependencies.
  500. * If such a package is present output is the Packages file entry,
  501. * which can be massaged as appropriate.
  502. * Exit status:
  503. * 0 = a package printed, OK
  504. * 1 = no suitable package available
  505. * 2 = error
  506. */
  507. static struct varbuf vb;
  508. struct pkgiterator *it;
  509. struct pkginfo *pkg= 0, *startpkg, *trypkg;
  510. struct dependency *dep;
  511. struct deppossi *possi, *provider;
  512. if (*argv) badusage(_("--predep-package does not take any argument"));
  513. modstatdb_init(admindir,msdbrw_readonly);
  514. clear_istobes(); /* We use clientdata->istobe to detect loops */
  515. for (it=iterpkgstart(), dep=0;
  516. !dep && (pkg=iterpkgnext(it));
  517. ) {
  518. if (pkg->want != want_install) continue; /* Ignore packages user doesn't want */
  519. if (!pkg->files) continue; /* Ignore packages not available */
  520. pkg->clientdata->istobe= itb_preinstall;
  521. for (dep= pkg->available.depends; dep; dep= dep->next) {
  522. if (dep->type != dep_predepends) continue;
  523. if (depisok(dep,&vb,0,1)) continue;
  524. break; /* This will leave dep non-NULL, and so exit the loop. */
  525. }
  526. pkg->clientdata->istobe= itb_normal;
  527. /* If dep is NULL we go and get the next package. */
  528. }
  529. if (!dep) exit(1); /* Not found */
  530. assert(pkg);
  531. startpkg= pkg;
  532. pkg->clientdata->istobe= itb_preinstall;
  533. /* OK, we have found an unsatisfied predependency.
  534. * Now go and find the first thing we need to install, as a first step
  535. * towards satisfying it.
  536. */
  537. do {
  538. /* We search for a package which would satisfy dep, and put it in pkg */
  539. for (possi=dep->list, pkg=0;
  540. !pkg && possi;
  541. possi=possi->next) {
  542. trypkg= possi->ed;
  543. if (!trypkg->available.valid) continue;
  544. if (trypkg->files && versionsatisfied(&trypkg->available,possi)) {
  545. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  546. }
  547. if (possi->verrel != dvr_none) continue;
  548. for (provider=possi->ed->available.depended;
  549. !pkg && provider;
  550. provider=provider->next) {
  551. if (provider->up->type != dep_provides) continue;
  552. trypkg= provider->up->up;
  553. if (!trypkg->available.valid || !trypkg->files) continue;
  554. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  555. }
  556. }
  557. if (!pkg) {
  558. varbufreset(&vb);
  559. describedepcon(&vb,dep);
  560. varbufaddc(&vb,0);
  561. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  562. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  563. dep->up->name,startpkg->name);
  564. }
  565. pkg->clientdata->istobe= itb_preinstall;
  566. for (dep= pkg->available.depends; dep; dep= dep->next) {
  567. if (dep->type != dep_predepends) continue;
  568. if (depisok(dep,&vb,0,1)) continue;
  569. break; /* This will leave dep non-NULL, and so exit the loop. */
  570. }
  571. } while (dep);
  572. /* OK, we've found it - pkg has no unsatisfied pre-dependencies ! */
  573. writerecord(stdout,"<stdout>",pkg,&pkg->available);
  574. if (fflush(stdout)) werr("stdout");
  575. }
  576. static void badlgccfn(const char *compiler, const char *output, const char *why)
  577. NONRETURNING;
  578. static void badlgccfn(const char *compiler, const char *output, const char *why) {
  579. fprintf(stderr,
  580. _("dpkg: unexpected output from `%s --print-libgcc-file-name':\n"
  581. " `%s'\n"),
  582. compiler,output);
  583. ohshit(_("compiler libgcc filename not understood: %.250s"),why);
  584. }
  585. void printinstarch(const char *const *argv) {
  586. if (*argv) badusage(_("--print-installation-architecture does not take any argument"));
  587. if (printf("%s\n",architecture) == EOF) werr("stdout");
  588. if (fflush(stdout)) werr("stdout");
  589. }
  590. void printarch(const char *const *argv) {
  591. static const struct { const char *from, *to, *gnu; } archtable[]= {
  592. #include "archtable.h"
  593. { 0,0,0 }
  594. }, *archp;
  595. const char *ccompiler, *arch;
  596. int p1[2], c;
  597. pid_t c1;
  598. FILE *ccpipe;
  599. struct varbuf vb;
  600. ptrdiff_t ll;
  601. char *p, *q;
  602. if (*argv) badusage(_("--print-architecture does not take any argument"));
  603. ccompiler= getenv("CC");
  604. if (!ccompiler) ccompiler= "gcc";
  605. varbufinit(&vb);
  606. m_pipe(p1);
  607. ccpipe= fdopen(p1[0],"r"); if (!ccpipe) ohshite(_("failed to fdopen CC pipe"));
  608. if (!(c1= m_fork())) {
  609. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  610. execlp(ccompiler,ccompiler,"--print-libgcc-file-name",(char*)0);
  611. /* if we have a problem excuting the C compiler, we don't
  612. * want to fail. If there is a problem with the compiler,
  613. * like not being installed, or CC being set incorrectly,
  614. * then important problems will show up elsewhere, not in
  615. * dpkg. If a C compiler is not important to the reason we
  616. * are being called, then we should just give them the built
  617. * in arch.
  618. */
  619. if (printf("/usr/lib/gcc-lib/%s-none/0.0.0/libgcc.a\n",architecture) == EOF)
  620. werr("stdout");
  621. if (fflush(stdout)) werr("stdout");
  622. exit(0);
  623. }
  624. close(p1[1]);
  625. while ((c= getc(ccpipe)) != EOF) varbufaddc(&vb,c);
  626. if (ferror(ccpipe)) ohshite(_("error reading from CC pipe"));
  627. waitsubproc(c1,"gcc --print-libgcc-file-name",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(_("--cmpversions takes three arguments:"
  686. " <version> <relation> <version>"));
  687. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  688. if (!rip->string) badusage(_("--cmpversions 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. }