enquiry.c 24 KB

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