]> git.saurik.com Git - apt.git/blame - cmdline/apt-cache.cc
* ftparchive/writer.cc:
[apt.git] / cmdline / apt-cache.cc
CommitLineData
1164783d
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
ea7f6363 3// $Id: apt-cache.cc,v 1.72 2004/04/30 04:34:03 mdz Exp $
1164783d
AL
4/* ######################################################################
5
e1b74f61 6 apt-cache - Manages the cache files
1164783d 7
e1b74f61 8 apt-cache provides some functions fo manipulating the cache files.
b2e465d6 9 It uses the command line interface common to all the APT tools.
1164783d
AL
10
11 Returns 100 on failure, 0 on success.
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#include <apt-pkg/error.h>
17#include <apt-pkg/pkgcachegen.h>
8efa2a3b 18#include <apt-pkg/init.h>
404ec98e 19#include <apt-pkg/progress.h>
880e9be4 20#include <apt-pkg/sourcelist.h>
08e8f724 21#include <apt-pkg/cmndline.h>
cdcc6d34 22#include <apt-pkg/strutl.h>
9dbb421f 23#include <apt-pkg/pkgrecords.h>
f8f410f5 24#include <apt-pkg/srcrecords.h>
3e94da1b 25#include <apt-pkg/version.h>
b2e465d6
AL
26#include <apt-pkg/policy.h>
27#include <apt-pkg/tagfile.h>
28#include <apt-pkg/algorithms.h>
29#include <apt-pkg/sptr.h>
30
43981212 31#include <config.h>
b2e465d6 32#include <apti18n.h>
1164783d 33
233c2b66 34#include <locale.h>
90f057fd 35#include <iostream>
cdb970c7 36#include <unistd.h>
43981212 37#include <errno.h>
9dbb421f 38#include <regex.h>
3e94da1b 39#include <stdio.h>
bd3d53ef
AL
40
41#include <iomanip>
1164783d
AL
42 /*}}}*/
43
8f312f45
AL
44using namespace std;
45
b0b4efb9 46pkgCache *GCache = 0;
af87ab54 47pkgSourceList *SrcList = 0;
b0b4efb9 48
b2e465d6
AL
49// LocalitySort - Sort a version list by package file locality /*{{{*/
50// ---------------------------------------------------------------------
51/* */
52int LocalityCompare(const void *a, const void *b)
53{
54 pkgCache::VerFile *A = *(pkgCache::VerFile **)a;
55 pkgCache::VerFile *B = *(pkgCache::VerFile **)b;
56
57 if (A == 0 && B == 0)
58 return 0;
59 if (A == 0)
60 return 1;
61 if (B == 0)
62 return -1;
63
64 if (A->File == B->File)
65 return A->Offset - B->Offset;
66 return A->File - B->File;
67}
68
69void LocalitySort(pkgCache::VerFile **begin,
70 unsigned long Count,size_t Size)
71{
72 qsort(begin,Count,Size,LocalityCompare);
73}
a52f938b
OS
74
75void LocalitySort(pkgCache::DescFile **begin,
76 unsigned long Count,size_t Size)
77{
78 qsort(begin,Count,Size,LocalityCompare);
79}
b2e465d6 80 /*}}}*/
cc718e9a
AL
81// UnMet - Show unmet dependencies /*{{{*/
82// ---------------------------------------------------------------------
83/* */
b0b4efb9 84bool UnMet(CommandLine &CmdL)
cc718e9a 85{
b0b4efb9 86 pkgCache &Cache = *GCache;
76fbce56 87 bool Important = _config->FindB("APT::Cache::Important",false);
018f1533 88
cc718e9a
AL
89 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
90 {
91 for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
92 {
93 bool Header = false;
018f1533 94 for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
cc718e9a
AL
95 {
96 // Collect or groups
97 pkgCache::DepIterator Start;
98 pkgCache::DepIterator End;
99 D.GlobOr(Start,End);
100
018f1533 101 // Skip conflicts and replaces
cc718e9a
AL
102 if (End->Type != pkgCache::Dep::PreDepends &&
103 End->Type != pkgCache::Dep::Depends &&
104 End->Type != pkgCache::Dep::Suggests &&
7146a53c 105 End->Type != pkgCache::Dep::Recommends)
cc718e9a
AL
106 continue;
107
018f1533
AL
108 // Important deps only
109 if (Important == true)
110 if (End->Type != pkgCache::Dep::PreDepends &&
7146a53c 111 End->Type != pkgCache::Dep::Depends)
018f1533
AL
112 continue;
113
cc718e9a
AL
114 // Verify the or group
115 bool OK = false;
116 pkgCache::DepIterator RealStart = Start;
117 do
118 {
119 // See if this dep is Ok
120 pkgCache::Version **VList = Start.AllTargets();
121 if (*VList != 0)
122 {
123 OK = true;
124 delete [] VList;
125 break;
126 }
127 delete [] VList;
128
129 if (Start == End)
130 break;
131 Start++;
132 }
133 while (1);
134
135 // The group is OK
136 if (OK == true)
137 continue;
138
139 // Oops, it failed..
140 if (Header == false)
b2e465d6 141 ioprintf(cout,_("Package %s version %s has an unmet dep:\n"),
75ce2062 142 P.FullName(true).c_str(),V.VerStr());
cc718e9a
AL
143 Header = true;
144
145 // Print out the dep type
146 cout << " " << End.DepType() << ": ";
147
148 // Show the group
149 Start = RealStart;
150 do
151 {
75ce2062 152 cout << Start.TargetPkg().FullName(true);
cc718e9a
AL
153 if (Start.TargetVer() != 0)
154 cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
155 ")";
156 if (Start == End)
157 break;
158 cout << " | ";
159 Start++;
160 }
161 while (1);
162
163 cout << endl;
164 }
165 }
166 }
167 return true;
168}
169 /*}}}*/
1164783d
AL
170// DumpPackage - Show a dump of a package record /*{{{*/
171// ---------------------------------------------------------------------
172/* */
b0b4efb9 173bool DumpPackage(CommandLine &CmdL)
ad00ae81 174{
b0b4efb9 175 pkgCache &Cache = *GCache;
e1b74f61 176 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1164783d 177 {
e1b74f61 178 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1164783d
AL
179 if (Pkg.end() == true)
180 {
b2e465d6 181 _error->Warning(_("Unable to locate package %s"),*I);
1164783d
AL
182 continue;
183 }
184
75ce2062 185 cout << "Package: " << Pkg.FullName(true) << endl;
b2e465d6 186 cout << "Versions: " << endl;
1164783d 187 for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
03e39e59
AL
188 {
189 cout << Cur.VerStr();
190 for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; Vf++)
a52f938b
OS
191 cout << " (" << Vf.File().FileName() << ")";
192 cout << endl;
193 for (pkgCache::DescIterator D = Cur.DescriptionList(); D.end() == false; D++)
194 {
195 cout << " Description Language: " << D.LanguageCode() << endl
196 << " File: " << D.FileList().File().FileName() << endl
197 << " MD5: " << D.md5() << endl;
198 }
b2e465d6 199 cout << endl;
03e39e59
AL
200 }
201
1164783d
AL
202 cout << endl;
203
204 cout << "Reverse Depends: " << endl;
205 for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; D++)
b2e465d6 206 {
75ce2062 207 cout << " " << D.ParentPkg().FullName(true) << ',' << D.TargetPkg().FullName(true);
b2e465d6 208 if (D->Version != 0)
b1b663d1 209 cout << ' ' << DeNull(D.TargetVer()) << endl;
b2e465d6
AL
210 else
211 cout << endl;
212 }
213
1164783d
AL
214 cout << "Dependencies: " << endl;
215 for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
216 {
217 cout << Cur.VerStr() << " - ";
218 for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; Dep++)
75ce2062 219 cout << Dep.TargetPkg().FullName(true) << " (" << (int)Dep->CompareOp << " " << DeNull(Dep.TargetVer()) << ") ";
1164783d
AL
220 cout << endl;
221 }
222
223 cout << "Provides: " << endl;
224 for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
225 {
226 cout << Cur.VerStr() << " - ";
227 for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; Prv++)
75ce2062 228 cout << Prv.ParentPkg().FullName(true) << " ";
1164783d 229 cout << endl;
8efa2a3b
AL
230 }
231 cout << "Reverse Provides: " << endl;
232 for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
75ce2062 233 cout << Prv.OwnerPkg().FullName(true) << " " << Prv.OwnerVer().VerStr() << endl;
1164783d
AL
234 }
235
236 return true;
237}
238 /*}}}*/
239// Stats - Dump some nice statistics /*{{{*/
240// ---------------------------------------------------------------------
241/* */
b0b4efb9 242bool Stats(CommandLine &Cmd)
1164783d 243{
b0b4efb9 244 pkgCache &Cache = *GCache;
12bffed7 245 cout << _("Total package names: ") << Cache.Head().PackageCount << " (" <<
f826cfaa 246 SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl;
b2e465d6 247
1164783d
AL
248 int Normal = 0;
249 int Virtual = 0;
250 int NVirt = 0;
251 int DVirt = 0;
252 int Missing = 0;
b2e465d6 253 pkgCache::PkgIterator I = Cache.PkgBegin();
1164783d
AL
254 for (;I.end() != true; I++)
255 {
256 if (I->VersionList != 0 && I->ProvidesList == 0)
257 {
258 Normal++;
259 continue;
260 }
261
262 if (I->VersionList != 0 && I->ProvidesList != 0)
263 {
264 NVirt++;
265 continue;
266 }
267
268 if (I->VersionList == 0 && I->ProvidesList != 0)
269 {
270 // Only 1 provides
271 if (I.ProvidesList()->NextProvides == 0)
272 {
273 DVirt++;
274 }
275 else
276 Virtual++;
277 continue;
278 }
279 if (I->VersionList == 0 && I->ProvidesList == 0)
280 {
281 Missing++;
282 continue;
283 }
284 }
db0db9fe
CP
285 cout << _(" Normal packages: ") << Normal << endl;
286 cout << _(" Pure virtual packages: ") << Virtual << endl;
287 cout << _(" Single virtual packages: ") << DVirt << endl;
288 cout << _(" Mixed virtual packages: ") << NVirt << endl;
b2e465d6 289 cout << _(" Missing: ") << Missing << endl;
1164783d 290
db0db9fe 291 cout << _("Total distinct versions: ") << Cache.Head().VersionCount << " (" <<
f826cfaa 292 SizeToStr(Cache.Head().VersionCount*Cache.Head().VersionSz) << ')' << endl;
12bffed7 293 cout << _("Total distinct descriptions: ") << Cache.Head().DescriptionCount << " (" <<
a52f938b 294 SizeToStr(Cache.Head().DescriptionCount*Cache.Head().DescriptionSz) << ')' << endl;
db0db9fe 295 cout << _("Total dependencies: ") << Cache.Head().DependsCount << " (" <<
f826cfaa
AL
296 SizeToStr(Cache.Head().DependsCount*Cache.Head().DependencySz) << ')' << endl;
297
db0db9fe 298 cout << _("Total ver/file relations: ") << Cache.Head().VerFileCount << " (" <<
a7e66b17 299 SizeToStr(Cache.Head().VerFileCount*Cache.Head().VerFileSz) << ')' << endl;
a52f938b
OS
300 cout << _("Total Desc/File relations: ") << Cache.Head().DescFileCount << " (" <<
301 SizeToStr(Cache.Head().DescFileCount*Cache.Head().DescFileSz) << ')' << endl;
db0db9fe 302 cout << _("Total Provides mappings: ") << Cache.Head().ProvidesCount << " (" <<
a7e66b17 303 SizeToStr(Cache.Head().ProvidesCount*Cache.Head().ProvidesSz) << ')' << endl;
f826cfaa
AL
304
305 // String list stats
306 unsigned long Size = 0;
307 unsigned long Count = 0;
308 for (pkgCache::StringItem *I = Cache.StringItemP + Cache.Head().StringList;
309 I!= Cache.StringItemP; I = Cache.StringItemP + I->NextItem)
310 {
311 Count++;
b2e465d6 312 Size += strlen(Cache.StrP + I->String) + 1;
f826cfaa 313 }
db0db9fe 314 cout << _("Total globbed strings: ") << Count << " (" << SizeToStr(Size) << ')' << endl;
b2e465d6
AL
315
316 unsigned long DepVerSize = 0;
317 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
318 {
319 for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
320 {
321 for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
322 {
323 if (D->Version != 0)
324 DepVerSize += strlen(D.TargetVer()) + 1;
325 }
326 }
327 }
db0db9fe 328 cout << _("Total dependency version space: ") << SizeToStr(DepVerSize) << endl;
b2e465d6 329
f826cfaa
AL
330 unsigned long Slack = 0;
331 for (int I = 0; I != 7; I++)
332 Slack += Cache.Head().Pools[I].ItemSize*Cache.Head().Pools[I].Count;
db0db9fe 333 cout << _("Total slack space: ") << SizeToStr(Slack) << endl;
f826cfaa
AL
334
335 unsigned long Total = 0;
336 Total = Slack + Size + Cache.Head().DependsCount*Cache.Head().DependencySz +
337 Cache.Head().VersionCount*Cache.Head().VersionSz +
a7e66b17
AL
338 Cache.Head().PackageCount*Cache.Head().PackageSz +
339 Cache.Head().VerFileCount*Cache.Head().VerFileSz +
340 Cache.Head().ProvidesCount*Cache.Head().ProvidesSz;
db0db9fe 341 cout << _("Total space accounted for: ") << SizeToStr(Total) << endl;
f826cfaa 342
83d89a9f
AL
343 return true;
344}
345 /*}}}*/
1164783d
AL
346// Dump - show everything /*{{{*/
347// ---------------------------------------------------------------------
b2e465d6 348/* This is worthless except fer debugging things */
b0b4efb9 349bool Dump(CommandLine &Cmd)
1164783d 350{
b0b4efb9 351 pkgCache &Cache = *GCache;
b2e465d6
AL
352 cout << "Using Versioning System: " << Cache.VS->Label << endl;
353
1164783d
AL
354 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
355 {
75ce2062 356 cout << "Package: " << P.FullName(true) << endl;
1164783d
AL
357 for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
358 {
359 cout << " Version: " << V.VerStr() << endl;
360 cout << " File: " << V.FileList().File().FileName() << endl;
361 for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
75ce2062 362 cout << " Depends: " << D.TargetPkg().FullName(true) << ' ' <<
076d01b0 363 DeNull(D.TargetVer()) << endl;
a52f938b
OS
364 for (pkgCache::DescIterator D = V.DescriptionList(); D.end() == false; D++)
365 {
366 cout << " Description Language: " << D.LanguageCode() << endl
367 << " File: " << D.FileList().File().FileName() << endl
368 << " MD5: " << D.md5() << endl;
369 }
1164783d
AL
370 }
371 }
372
b2e465d6 373 for (pkgCache::PkgFileIterator F = Cache.FileBegin(); F.end() == false; F++)
1164783d
AL
374 {
375 cout << "File: " << F.FileName() << endl;
b2e465d6 376 cout << " Type: " << F.IndexType() << endl;
1164783d
AL
377 cout << " Size: " << F->Size << endl;
378 cout << " ID: " << F->ID << endl;
379 cout << " Flags: " << F->Flags << endl;
b0b4efb9 380 cout << " Time: " << TimeRFC1123(F->mtime) << endl;
076d01b0
AL
381 cout << " Archive: " << DeNull(F.Archive()) << endl;
382 cout << " Component: " << DeNull(F.Component()) << endl;
383 cout << " Version: " << DeNull(F.Version()) << endl;
384 cout << " Origin: " << DeNull(F.Origin()) << endl;
385 cout << " Site: " << DeNull(F.Site()) << endl;
386 cout << " Label: " << DeNull(F.Label()) << endl;
387 cout << " Architecture: " << DeNull(F.Architecture()) << endl;
1164783d
AL
388 }
389
390 return true;
391}
392 /*}}}*/
393// DumpAvail - Print out the available list /*{{{*/
394// ---------------------------------------------------------------------
b2e465d6
AL
395/* This is needed to make dpkg --merge happy.. I spent a bit of time to
396 make this run really fast, perhaps I went a little overboard.. */
b0b4efb9 397bool DumpAvail(CommandLine &Cmd)
1164783d 398{
b0b4efb9 399 pkgCache &Cache = *GCache;
1164783d 400
b2e465d6 401 pkgPolicy Plcy(&Cache);
e68ca100 402 if (ReadPinFile(Plcy) == false || ReadPinDir(Plcy) == false)
b2e465d6
AL
403 return false;
404
fe648919
AL
405 unsigned long Count = Cache.HeaderP->PackageCount+1;
406 pkgCache::VerFile **VFList = new pkgCache::VerFile *[Count];
407 memset(VFList,0,sizeof(*VFList)*Count);
b2e465d6
AL
408
409 // Map versions that we want to write out onto the VerList array.
410 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
411 {
412 if (P->VersionList == 0)
5b8c90bf
AL
413 continue;
414
b2e465d6
AL
415 /* Find the proper version to use. If the policy says there are no
416 possible selections we return the installed version, if available..
417 This prevents dselect from making it obsolete. */
418 pkgCache::VerIterator V = Plcy.GetCandidateVer(P);
419 if (V.end() == true)
ad00ae81 420 {
b2e465d6
AL
421 if (P->CurrentVer == 0)
422 continue;
423 V = P.CurrentVer();
ad00ae81 424 }
1164783d 425
b2e465d6
AL
426 pkgCache::VerFileIterator VF = V.FileList();
427 for (; VF.end() == false ; VF++)
428 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
429 break;
430
431 /* Okay, here we have a bit of a problem.. The policy has selected the
432 currently installed package - however it only exists in the
433 status file.. We need to write out something or dselect will mark
434 the package as obsolete! Thus we emit the status file entry, but
435 below we remove the status line to make it valid for the
436 available file. However! We only do this if their do exist *any*
437 non-source versions of the package - that way the dselect obsolete
438 handling works OK. */
439 if (VF.end() == true)
1164783d 440 {
b2e465d6
AL
441 for (pkgCache::VerIterator Cur = P.VersionList(); Cur.end() != true; Cur++)
442 {
443 for (VF = Cur.FileList(); VF.end() == false; VF++)
444 {
445 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
446 {
447 VF = V.FileList();
448 break;
449 }
450 }
451
452 if (VF.end() == false)
453 break;
454 }
ad00ae81 455 }
b2e465d6
AL
456
457 VFList[P->ID] = VF;
458 }
459
fe648919 460 LocalitySort(VFList,Count,sizeof(*VFList));
ad00ae81 461
b2e465d6
AL
462 // Iterate over all the package files and write them out.
463 char *Buffer = new char[Cache.HeaderP->MaxVerFileSize+10];
464 for (pkgCache::VerFile **J = VFList; *J != 0;)
465 {
466 pkgCache::PkgFileIterator File(Cache,(*J)->File + Cache.PkgFileP);
467 if (File.IsOk() == false)
ad00ae81 468 {
b2e465d6
AL
469 _error->Error(_("Package file %s is out of sync."),File.FileName());
470 break;
471 }
bd432be3 472
b2e465d6
AL
473 FileFd PkgF(File.FileName(),FileFd::ReadOnly);
474 if (_error->PendingError() == true)
475 break;
476
477 /* Write all of the records from this package file, since we
478 already did locality sorting we can now just seek through the
479 file in read order. We apply 1 more optimization here, since often
480 there will be < 1 byte gaps between records (for the \n) we read that
481 into the next buffer and offset a bit.. */
482 unsigned long Pos = 0;
483 for (; *J != 0; J++)
484 {
485 if ((*J)->File + Cache.PkgFileP != File)
486 break;
bd432be3 487
b2e465d6
AL
488 const pkgCache::VerFile &VF = **J;
489
bd432be3 490 // Read the record and then write it out again.
b2e465d6
AL
491 unsigned long Jitter = VF.Offset - Pos;
492 if (Jitter > 8)
1164783d 493 {
b2e465d6
AL
494 if (PkgF.Seek(VF.Offset) == false)
495 break;
496 Jitter = 0;
497 }
498
499 if (PkgF.Read(Buffer,VF.Size + Jitter) == false)
500 break;
501 Buffer[VF.Size + Jitter] = '\n';
502
503 // See above..
504 if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
505 {
506 pkgTagSection Tags;
e8cbb49f 507 TFRewriteData RW[] = {{"Status",0},{"Config-Version",0},{}};
b2e465d6
AL
508 const char *Zero = 0;
509 if (Tags.Scan(Buffer+Jitter,VF.Size+1) == false ||
510 TFRewrite(stdout,Tags,&Zero,RW) == false)
511 {
512 _error->Error("Internal Error, Unable to parse a package record");
513 break;
514 }
515 fputc('\n',stdout);
516 }
517 else
518 {
519 if (fwrite(Buffer+Jitter,VF.Size+1,1,stdout) != 1)
520 break;
521 }
522
523 Pos = VF.Offset + VF.Size;
1164783d 524 }
b2e465d6
AL
525
526 fflush(stdout);
527 if (_error->PendingError() == true)
528 break;
ad00ae81
AL
529 }
530
b2e465d6
AL
531 delete [] Buffer;
532 delete [] VFList;
533 return !_error->PendingError();
349cd3b8
AL
534}
535 /*}}}*/
4b1b89c5 536// Depends - Print out a dependency tree /*{{{*/
349cd3b8
AL
537// ---------------------------------------------------------------------
538/* */
539bool Depends(CommandLine &CmdL)
540{
541 pkgCache &Cache = *GCache;
b2e465d6
AL
542 SPtrArray<unsigned> Colours = new unsigned[Cache.Head().PackageCount];
543 memset(Colours,0,sizeof(*Colours)*Cache.Head().PackageCount);
349cd3b8
AL
544
545 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
546 {
547 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
548 if (Pkg.end() == true)
549 {
b2e465d6 550 _error->Warning(_("Unable to locate package %s"),*I);
349cd3b8
AL
551 continue;
552 }
b2e465d6
AL
553 Colours[Pkg->ID] = 1;
554 }
555
556 bool Recurse = _config->FindB("APT::Cache::RecurseDepends",false);
eba2b51d 557 bool Installed = _config->FindB("APT::Cache::Installed",false);
5aa95c86 558 bool Important = _config->FindB("APT::Cache::Important",false);
b2e465d6
AL
559 bool DidSomething;
560 do
561 {
562 DidSomething = false;
563 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
349cd3b8 564 {
b2e465d6
AL
565 if (Colours[Pkg->ID] != 1)
566 continue;
567 Colours[Pkg->ID] = 2;
568 DidSomething = true;
349cd3b8 569
b2e465d6
AL
570 pkgCache::VerIterator Ver = Pkg.VersionList();
571 if (Ver.end() == true)
349cd3b8 572 {
75ce2062 573 cout << '<' << Pkg.FullName(true) << '>' << endl;
b2e465d6 574 continue;
349cd3b8 575 }
b2e465d6 576
75ce2062 577 cout << Pkg.FullName(true) << endl;
b2e465d6
AL
578
579 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
580 {
5aa95c86
MV
581 // Important deps only
582 if (Important == true)
583 if (D->Type != pkgCache::Dep::PreDepends &&
584 D->Type != pkgCache::Dep::Depends)
585 continue;
586
b2e465d6 587 pkgCache::PkgIterator Trg = D.TargetPkg();
eba2b51d
AL
588
589 if((Installed && Trg->CurrentVer != 0) || !Installed)
590 {
591
592 if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
593 cout << " |";
594 else
595 cout << " ";
b2e465d6 596
eba2b51d
AL
597 // Show the package
598 if (Trg->VersionList == 0)
75ce2062 599 cout << D.DepType() << ": <" << Trg.FullName(true) << ">" << endl;
eba2b51d 600 else
75ce2062 601 cout << D.DepType() << ": " << Trg.FullName(true) << endl;
eba2b51d
AL
602
603 if (Recurse == true)
604 Colours[D.TargetPkg()->ID]++;
605
606 }
b2e465d6
AL
607
608 // Display all solutions
609 SPtrArray<pkgCache::Version *> List = D.AllTargets();
610 pkgPrioSortList(Cache,List);
611 for (pkgCache::Version **I = List; *I != 0; I++)
612 {
613 pkgCache::VerIterator V(Cache,*I);
614 if (V != Cache.VerP + V.ParentPkg()->VersionList ||
615 V->ParentPkg == D->Package)
616 continue;
75ce2062 617 cout << " " << V.ParentPkg().FullName(true) << endl;
b2e465d6
AL
618
619 if (Recurse == true)
620 Colours[D.ParentPkg()->ID]++;
621 }
622 }
623 }
349cd3b8 624 }
b2e465d6 625 while (DidSomething == true);
349cd3b8 626
3e94da1b
AL
627 return true;
628}
92fcbfc1 629 /*}}}*/
eba2b51d
AL
630// RDepends - Print out a reverse dependency tree - mbc /*{{{*/
631// ---------------------------------------------------------------------
632/* */
633bool RDepends(CommandLine &CmdL)
634{
635 pkgCache &Cache = *GCache;
636 SPtrArray<unsigned> Colours = new unsigned[Cache.Head().PackageCount];
637 memset(Colours,0,sizeof(*Colours)*Cache.Head().PackageCount);
638
639 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
640 {
641 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
642 if (Pkg.end() == true)
643 {
644 _error->Warning(_("Unable to locate package %s"),*I);
645 continue;
646 }
647 Colours[Pkg->ID] = 1;
648 }
649
650 bool Recurse = _config->FindB("APT::Cache::RecurseDepends",false);
651 bool Installed = _config->FindB("APT::Cache::Installed",false);
652 bool DidSomething;
653 do
654 {
655 DidSomething = false;
656 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
657 {
658 if (Colours[Pkg->ID] != 1)
659 continue;
660 Colours[Pkg->ID] = 2;
661 DidSomething = true;
662
663 pkgCache::VerIterator Ver = Pkg.VersionList();
664 if (Ver.end() == true)
665 {
75ce2062 666 cout << '<' << Pkg.FullName(true) << '>' << endl;
eba2b51d
AL
667 continue;
668 }
669
75ce2062 670 cout << Pkg.FullName(true) << endl;
eba2b51d
AL
671
672 cout << "Reverse Depends:" << endl;
673 for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() == false; D++)
674 {
675 // Show the package
676 pkgCache::PkgIterator Trg = D.ParentPkg();
677
678 if((Installed && Trg->CurrentVer != 0) || !Installed)
679 {
680
681 if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
682 cout << " |";
683 else
684 cout << " ";
685
686 if (Trg->VersionList == 0)
75ce2062 687 cout << D.DepType() << ": <" << Trg.FullName(true) << ">" << endl;
eba2b51d 688 else
75ce2062 689 cout << Trg.FullName(true) << endl;
eba2b51d
AL
690
691 if (Recurse == true)
692 Colours[D.ParentPkg()->ID]++;
693
694 }
695
696 // Display all solutions
697 SPtrArray<pkgCache::Version *> List = D.AllTargets();
698 pkgPrioSortList(Cache,List);
699 for (pkgCache::Version **I = List; *I != 0; I++)
700 {
701 pkgCache::VerIterator V(Cache,*I);
702 if (V != Cache.VerP + V.ParentPkg()->VersionList ||
703 V->ParentPkg == D->Package)
704 continue;
75ce2062 705 cout << " " << V.ParentPkg().FullName(true) << endl;
eba2b51d
AL
706
707 if (Recurse == true)
708 Colours[D.ParentPkg()->ID]++;
709 }
710 }
711 }
712 }
713 while (DidSomething == true);
714
715 return true;
716}
3e94da1b 717 /*}}}*/
fff4b7f3
AL
718// xvcg - Generate a graph for xvcg /*{{{*/
719// ---------------------------------------------------------------------
720// Code contributed from Junichi Uekawa <dancer@debian.org> on 20 June 2002.
721
722bool XVcg(CommandLine &CmdL)
723{
724 pkgCache &Cache = *GCache;
725 bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
726
727 /* Normal packages are boxes
728 Pure Provides are triangles
729 Mixed are diamonds
730 rhomb are missing packages*/
731 const char *Shapes[] = {"ellipse","triangle","box","rhomb"};
732
733 /* Initialize the list of packages to show.
734 1 = To Show
735 2 = To Show no recurse
736 3 = Emitted no recurse
737 4 = Emitted
738 0 = None */
739 enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
740 enum TheFlags {ForceNR=(1<<0)};
741 unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
742 unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
743 unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
744
745 // Show everything if no arguments given
746 if (CmdL.FileList[1] == 0)
747 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
748 Show[I] = ToShow;
749 else
750 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
751 Show[I] = None;
752 memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
753
754 // Map the shapes
755 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
756 {
757 if (Pkg->VersionList == 0)
758 {
759 // Missing
760 if (Pkg->ProvidesList == 0)
761 ShapeMap[Pkg->ID] = 0;
762 else
763 ShapeMap[Pkg->ID] = 1;
764 }
765 else
766 {
767 // Normal
768 if (Pkg->ProvidesList == 0)
769 ShapeMap[Pkg->ID] = 2;
770 else
771 ShapeMap[Pkg->ID] = 3;
772 }
773 }
774
775 // Load the list of packages from the command line into the show list
776 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
777 {
778 // Process per-package flags
779 string P = *I;
780 bool Force = false;
781 if (P.length() > 3)
782 {
783 if (P.end()[-1] == '^')
784 {
785 Force = true;
786 P.erase(P.end()-1);
787 }
788
789 if (P.end()[-1] == ',')
790 P.erase(P.end()-1);
791 }
792
793 // Locate the package
794 pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
795 if (Pkg.end() == true)
796 {
797 _error->Warning(_("Unable to locate package %s"),*I);
798 continue;
799 }
800 Show[Pkg->ID] = ToShow;
801
802 if (Force == true)
803 Flags[Pkg->ID] |= ForceNR;
804 }
805
806 // Little header
807 cout << "graph: { title: \"packages\"" << endl <<
808 "xmax: 700 ymax: 700 x: 30 y: 30" << endl <<
809 "layout_downfactor: 8" << endl;
810
811 bool Act = true;
812 while (Act == true)
813 {
814 Act = false;
815 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
816 {
817 // See we need to show this package
818 if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
819 continue;
820
821 //printf ("node: { title: \"%s\" label: \"%s\" }\n", Pkg.Name(), Pkg.Name());
822
823 // Colour as done
824 if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
825 {
826 // Pure Provides and missing packages have no deps!
827 if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
828 Show[Pkg->ID] = Done;
829 else
830 Show[Pkg->ID] = DoneNR;
831 }
832 else
833 Show[Pkg->ID] = Done;
834 Act = true;
835
836 // No deps to map out
837 if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
838 continue;
839
840 pkgCache::VerIterator Ver = Pkg.VersionList();
841 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
842 {
843 // See if anything can meet this dep
844 // Walk along the actual package providing versions
845 bool Hit = false;
846 pkgCache::PkgIterator DPkg = D.TargetPkg();
847 for (pkgCache::VerIterator I = DPkg.VersionList();
848 I.end() == false && Hit == false; I++)
849 {
850 if (Cache.VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
851 Hit = true;
852 }
853
854 // Follow all provides
855 for (pkgCache::PrvIterator I = DPkg.ProvidesList();
856 I.end() == false && Hit == false; I++)
857 {
858 if (Cache.VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
859 Hit = true;
860 }
861
862
863 // Only graph critical deps
864 if (D.IsCritical() == true)
865 {
75ce2062 866 printf ("edge: { sourcename: \"%s\" targetname: \"%s\" class: 2 ",Pkg.FullName(true).c_str(), D.TargetPkg().FullName(true).c_str() );
fff4b7f3
AL
867
868 // Colour the node for recursion
869 if (Show[D.TargetPkg()->ID] <= DoneNR)
870 {
871 /* If a conflicts does not meet anything in the database
872 then show the relation but do not recurse */
873 if (Hit == false &&
874 (D->Type == pkgCache::Dep::Conflicts ||
308c7d30 875 D->Type == pkgCache::Dep::DpkgBreaks ||
fff4b7f3
AL
876 D->Type == pkgCache::Dep::Obsoletes))
877 {
878 if (Show[D.TargetPkg()->ID] == None &&
879 Show[D.TargetPkg()->ID] != ToShow)
880 Show[D.TargetPkg()->ID] = ToShowNR;
881 }
882 else
883 {
884 if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
885 Show[D.TargetPkg()->ID] = ToShowNR;
886 else
887 Show[D.TargetPkg()->ID] = ToShow;
888 }
889 }
890
891 // Edge colour
892 switch(D->Type)
893 {
894 case pkgCache::Dep::Conflicts:
895 printf("label: \"conflicts\" color: lightgreen }\n");
896 break;
308c7d30
IJ
897 case pkgCache::Dep::DpkgBreaks:
898 printf("label: \"breaks\" color: lightgreen }\n");
899 break;
fff4b7f3
AL
900 case pkgCache::Dep::Obsoletes:
901 printf("label: \"obsoletes\" color: lightgreen }\n");
902 break;
903
904 case pkgCache::Dep::PreDepends:
905 printf("label: \"predepends\" color: blue }\n");
906 break;
907
908 default:
909 printf("}\n");
910 break;
911 }
912 }
913 }
914 }
915 }
916
917 /* Draw the box colours after the fact since we can not tell what colour
918 they should be until everything is finished drawing */
919 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
920 {
921 if (Show[Pkg->ID] < DoneNR)
922 continue;
923
924 if (Show[Pkg->ID] == DoneNR)
75ce2062 925 printf("node: { title: \"%s\" label: \"%s\" color: orange shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
fff4b7f3
AL
926 Shapes[ShapeMap[Pkg->ID]]);
927 else
75ce2062 928 printf("node: { title: \"%s\" label: \"%s\" shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
fff4b7f3
AL
929 Shapes[ShapeMap[Pkg->ID]]);
930
931 }
03496041
DK
932
933 delete[] Show;
934 delete[] Flags;
935 delete[] ShapeMap;
936
fff4b7f3
AL
937 printf("}\n");
938 return true;
939}
940 /*}}}*/
3e94da1b
AL
941// Dotty - Generate a graph for Dotty /*{{{*/
942// ---------------------------------------------------------------------
943/* Dotty is the graphvis program for generating graphs. It is a fairly
944 simple queuing algorithm that just writes dependencies and nodes.
945 http://www.research.att.com/sw/tools/graphviz/ */
946bool Dotty(CommandLine &CmdL)
947{
948 pkgCache &Cache = *GCache;
949 bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
950
951 /* Normal packages are boxes
952 Pure Provides are triangles
953 Mixed are diamonds
954 Hexagons are missing packages*/
955 const char *Shapes[] = {"hexagon","triangle","box","diamond"};
956
957 /* Initialize the list of packages to show.
958 1 = To Show
959 2 = To Show no recurse
960 3 = Emitted no recurse
961 4 = Emitted
962 0 = None */
963 enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
964 enum TheFlags {ForceNR=(1<<0)};
965 unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
966 unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
967 unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
968
969 // Show everything if no arguments given
970 if (CmdL.FileList[1] == 0)
971 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
972 Show[I] = ToShow;
973 else
974 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
975 Show[I] = None;
976 memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
977
978 // Map the shapes
979 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
980 {
981 if (Pkg->VersionList == 0)
982 {
983 // Missing
984 if (Pkg->ProvidesList == 0)
985 ShapeMap[Pkg->ID] = 0;
986 else
987 ShapeMap[Pkg->ID] = 1;
988 }
989 else
990 {
991 // Normal
992 if (Pkg->ProvidesList == 0)
993 ShapeMap[Pkg->ID] = 2;
994 else
995 ShapeMap[Pkg->ID] = 3;
996 }
997 }
998
999 // Load the list of packages from the command line into the show list
1000 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1001 {
1002 // Process per-package flags
1003 string P = *I;
1004 bool Force = false;
1005 if (P.length() > 3)
1006 {
1007 if (P.end()[-1] == '^')
1008 {
1009 Force = true;
1010 P.erase(P.end()-1);
1011 }
1012
1013 if (P.end()[-1] == ',')
1014 P.erase(P.end()-1);
1015 }
1016
1017 // Locate the package
1018 pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
1019 if (Pkg.end() == true)
1020 {
b2e465d6 1021 _error->Warning(_("Unable to locate package %s"),*I);
3e94da1b
AL
1022 continue;
1023 }
1024 Show[Pkg->ID] = ToShow;
1025
1026 if (Force == true)
1027 Flags[Pkg->ID] |= ForceNR;
1028 }
1029
1030 // Little header
1031 printf("digraph packages {\n");
1032 printf("concentrate=true;\n");
1033 printf("size=\"30,40\";\n");
1034
1035 bool Act = true;
1036 while (Act == true)
1037 {
1038 Act = false;
1039 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
1040 {
1041 // See we need to show this package
1042 if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
1043 continue;
1044
1045 // Colour as done
1046 if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
1047 {
1048 // Pure Provides and missing packages have no deps!
1049 if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
1050 Show[Pkg->ID] = Done;
1051 else
1052 Show[Pkg->ID] = DoneNR;
1053 }
1054 else
1055 Show[Pkg->ID] = Done;
1056 Act = true;
1057
1058 // No deps to map out
1059 if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
1060 continue;
1061
1062 pkgCache::VerIterator Ver = Pkg.VersionList();
1063 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
1064 {
1065 // See if anything can meet this dep
1066 // Walk along the actual package providing versions
1067 bool Hit = false;
1068 pkgCache::PkgIterator DPkg = D.TargetPkg();
1069 for (pkgCache::VerIterator I = DPkg.VersionList();
1070 I.end() == false && Hit == false; I++)
1071 {
b2e465d6 1072 if (Cache.VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
3e94da1b
AL
1073 Hit = true;
1074 }
1075
1076 // Follow all provides
1077 for (pkgCache::PrvIterator I = DPkg.ProvidesList();
1078 I.end() == false && Hit == false; I++)
1079 {
b2e465d6 1080 if (Cache.VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
3e94da1b
AL
1081 Hit = true;
1082 }
1083
1084 // Only graph critical deps
1085 if (D.IsCritical() == true)
1086 {
75ce2062 1087 printf("\"%s\" -> \"%s\"",Pkg.FullName(true).c_str(),D.TargetPkg().FullName(true).c_str());
3e94da1b
AL
1088
1089 // Colour the node for recursion
1090 if (Show[D.TargetPkg()->ID] <= DoneNR)
1091 {
1092 /* If a conflicts does not meet anything in the database
1093 then show the relation but do not recurse */
b2e465d6
AL
1094 if (Hit == false &&
1095 (D->Type == pkgCache::Dep::Conflicts ||
1096 D->Type == pkgCache::Dep::Obsoletes))
3e94da1b
AL
1097 {
1098 if (Show[D.TargetPkg()->ID] == None &&
1099 Show[D.TargetPkg()->ID] != ToShow)
1100 Show[D.TargetPkg()->ID] = ToShowNR;
1101 }
1102 else
1103 {
1104 if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
1105 Show[D.TargetPkg()->ID] = ToShowNR;
1106 else
1107 Show[D.TargetPkg()->ID] = ToShow;
1108 }
1109 }
1110
1111 // Edge colour
1112 switch(D->Type)
1113 {
1114 case pkgCache::Dep::Conflicts:
b2e465d6 1115 case pkgCache::Dep::Obsoletes:
3e94da1b
AL
1116 printf("[color=springgreen];\n");
1117 break;
1118
1119 case pkgCache::Dep::PreDepends:
1120 printf("[color=blue];\n");
1121 break;
1122
1123 default:
1124 printf(";\n");
1125 break;
1126 }
1127 }
1128 }
1129 }
1130 }
1131
1132 /* Draw the box colours after the fact since we can not tell what colour
1133 they should be until everything is finished drawing */
1134 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
1135 {
1136 if (Show[Pkg->ID] < DoneNR)
1137 continue;
1138
1139 // Orange box for early recursion stoppage
1140 if (Show[Pkg->ID] == DoneNR)
75ce2062 1141 printf("\"%s\" [color=orange,shape=%s];\n",Pkg.FullName(true).c_str(),
3e94da1b
AL
1142 Shapes[ShapeMap[Pkg->ID]]);
1143 else
75ce2062 1144 printf("\"%s\" [shape=%s];\n",Pkg.FullName(true).c_str(),
3e94da1b
AL
1145 Shapes[ShapeMap[Pkg->ID]]);
1146 }
1147
1148 printf("}\n");
ad00ae81
AL
1149 return true;
1150}
1151 /*}}}*/
1152// DoAdd - Perform an adding operation /*{{{*/
1153// ---------------------------------------------------------------------
1154/* */
e1b74f61 1155bool DoAdd(CommandLine &CmdL)
ad00ae81 1156{
b2e465d6
AL
1157 return _error->Error("Unimplemented");
1158#if 0
e1b74f61
AL
1159 // Make sure there is at least one argument
1160 if (CmdL.FileSize() <= 1)
1161 return _error->Error("You must give at least one file name");
ad00ae81
AL
1162
1163 // Open the cache
018f1533 1164 FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny);
ad00ae81
AL
1165 if (_error->PendingError() == true)
1166 return false;
1167
1168 DynamicMMap Map(CacheF,MMap::Public);
1169 if (_error->PendingError() == true)
1170 return false;
404ec98e 1171
0a8e3465 1172 OpTextProgress Progress(*_config);
404ec98e 1173 pkgCacheGenerator Gen(Map,Progress);
ad00ae81
AL
1174 if (_error->PendingError() == true)
1175 return false;
1176
e1b74f61
AL
1177 unsigned long Length = CmdL.FileSize() - 1;
1178 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
ad00ae81 1179 {
e1b74f61 1180 Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
018f1533
AL
1181 Progress.SubProgress(Length);
1182
ad00ae81 1183 // Do the merge
e1b74f61 1184 FileFd TagF(*I,FileFd::ReadOnly);
ad00ae81
AL
1185 debListParser Parser(TagF);
1186 if (_error->PendingError() == true)
e1b74f61 1187 return _error->Error("Problem opening %s",*I);
ad00ae81 1188
b2e465d6 1189 if (Gen.SelectFile(*I,"") == false)
ad00ae81
AL
1190 return _error->Error("Problem with SelectFile");
1191
1192 if (Gen.MergeList(Parser) == false)
1193 return _error->Error("Problem with MergeList");
1164783d 1194 }
404ec98e
AL
1195
1196 Progress.Done();
b0b4efb9
AL
1197 GCache = &Gen.GetCache();
1198 Stats(CmdL);
ad00ae81 1199
7e2e2d5d 1200 return true;
b2e465d6 1201#endif
7e2e2d5d
AL
1202}
1203 /*}}}*/
1204// DisplayRecord - Displays the complete record for the package /*{{{*/
1205// ---------------------------------------------------------------------
1206/* This displays the package record from the proper package index file.
1207 It is not used by DumpAvail for performance reasons. */
1208bool DisplayRecord(pkgCache::VerIterator V)
1209{
1210 // Find an appropriate file
1211 pkgCache::VerFileIterator Vf = V.FileList();
1212 for (; Vf.end() == false; Vf++)
1213 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
1214 break;
1215 if (Vf.end() == true)
1216 Vf = V.FileList();
1217
1218 // Check and load the package list file
1219 pkgCache::PkgFileIterator I = Vf.File();
1220 if (I.IsOk() == false)
b2e465d6 1221 return _error->Error(_("Package file %s is out of sync."),I.FileName());
7e2e2d5d
AL
1222
1223 FileFd PkgF(I.FileName(),FileFd::ReadOnly);
1224 if (_error->PendingError() == true)
1225 return false;
1226
a52f938b 1227 // Read the record
b2e465d6
AL
1228 unsigned char *Buffer = new unsigned char[GCache->HeaderP->MaxVerFileSize+1];
1229 Buffer[V.FileList()->Size] = '\n';
7e2e2d5d 1230 if (PkgF.Seek(V.FileList()->Offset) == false ||
a52f938b 1231 PkgF.Read(Buffer,V.FileList()->Size) == false)
7e2e2d5d
AL
1232 {
1233 delete [] Buffer;
1234 return false;
1235 }
a52f938b 1236
e5e2d176 1237 // Get a pointer to start of Description field
e011829d 1238 const unsigned char *DescP = (unsigned char*)strstr((char*)Buffer, "Description:");
e5e2d176
MV
1239
1240 // Write all but Description
487d7faa 1241 if (fwrite(Buffer,1,DescP - Buffer,stdout) < (size_t)(DescP - Buffer))
a52f938b
OS
1242 {
1243 delete [] Buffer;
1244 return false;
1245 }
1246
a52f938b
OS
1247 // Show the right description
1248 pkgRecords Recs(*GCache);
012b102a 1249 pkgCache::DescIterator Desc = V.TranslatedDescription();
a52f938b 1250 pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
e011829d 1251 cout << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc();
a52f938b 1252
e011829d
MV
1253 // Find the first field after the description (if there is any)
1254 for(DescP++;DescP != &Buffer[V.FileList()->Size];DescP++)
1255 {
1256 if(*DescP == '\n' && *(DescP+1) != ' ')
1257 {
1258 // write the rest of the buffer
1259 const unsigned char *end=&Buffer[V.FileList()->Size];
1260 if (fwrite(DescP,1,end-DescP,stdout) < (size_t)(end-DescP))
1261 {
1262 delete [] Buffer;
1263 return false;
1264 }
1265
1266 break;
1267 }
1268 }
1269 // write a final newline (after the description)
1270 cout<<endl;
7e2e2d5d
AL
1271 delete [] Buffer;
1272
9dbb421f
AL
1273 return true;
1274}
1275 /*}}}*/
92fcbfc1 1276
a52f938b 1277struct ExDescFile
b2e465d6 1278{
a52f938b 1279 pkgCache::DescFile *Df;
b2e465d6
AL
1280 bool NameMatch;
1281};
1282
92fcbfc1
DK
1283// Search - Perform a search /*{{{*/
1284// ---------------------------------------------------------------------
1285/* This searches the package names and package descriptions for a pattern */
9dbb421f
AL
1286bool Search(CommandLine &CmdL)
1287{
1288 pkgCache &Cache = *GCache;
7e2e2d5d
AL
1289 bool ShowFull = _config->FindB("APT::Cache::ShowFull",false);
1290 bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
b2e465d6
AL
1291 unsigned NumPatterns = CmdL.FileSize() -1;
1292
1293 pkgDepCache::Policy Plcy;
9dbb421f
AL
1294
1295 // Make sure there is at least one argument
b2e465d6
AL
1296 if (NumPatterns < 1)
1297 return _error->Error(_("You must give exactly one pattern"));
9dbb421f
AL
1298
1299 // Compile the regex pattern
b2e465d6
AL
1300 regex_t *Patterns = new regex_t[NumPatterns];
1301 memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
1302 for (unsigned I = 0; I != NumPatterns; I++)
1303 {
1304 if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
1305 REG_NOSUB) != 0)
1306 {
1307 for (; I != 0; I--)
1308 regfree(&Patterns[I]);
1309 return _error->Error("Regex compilation error");
1310 }
1311 }
9dbb421f
AL
1312
1313 // Create the text record parser
1314 pkgRecords Recs(Cache);
1315 if (_error->PendingError() == true)
b2e465d6
AL
1316 {
1317 for (unsigned I = 0; I != NumPatterns; I++)
1318 regfree(&Patterns[I]);
9dbb421f 1319 return false;
b2e465d6 1320 }
9dbb421f 1321
a52f938b
OS
1322 ExDescFile *DFList = new ExDescFile[Cache.HeaderP->PackageCount+1];
1323 memset(DFList,0,sizeof(*DFList)*Cache.HeaderP->PackageCount+1);
b2e465d6
AL
1324
1325 // Map versions that we want to write out onto the VerList array.
1326 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
9dbb421f 1327 {
a52f938b 1328 DFList[P->ID].NameMatch = NumPatterns != 0;
b2e465d6
AL
1329 for (unsigned I = 0; I != NumPatterns; I++)
1330 {
1331 if (regexec(&Patterns[I],P.Name(),0,0,0) == 0)
a52f938b 1332 DFList[P->ID].NameMatch &= true;
0f2fa322 1333 else
a52f938b 1334 DFList[P->ID].NameMatch = false;
b2e465d6 1335 }
c29652b0 1336
b2e465d6 1337 // Doing names only, drop any that dont match..
a52f938b 1338 if (NamesOnly == true && DFList[P->ID].NameMatch == false)
b2e465d6
AL
1339 continue;
1340
1341 // Find the proper version to use.
1342 pkgCache::VerIterator V = Plcy.GetCandidateVer(P);
c29652b0 1343 if (V.end() == false)
a52f938b 1344 DFList[P->ID].Df = V.DescriptionList().FileList();
c29652b0
AL
1345 }
1346
1347 // Include all the packages that provide matching names too
1348 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
1349 {
a52f938b 1350 if (DFList[P->ID].NameMatch == false)
7e2e2d5d 1351 continue;
c29652b0
AL
1352
1353 for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; Prv++)
1354 {
1355 pkgCache::VerIterator V = Plcy.GetCandidateVer(Prv.OwnerPkg());
1356 if (V.end() == false)
1357 {
a52f938b
OS
1358 DFList[Prv.OwnerPkg()->ID].Df = V.DescriptionList().FileList();
1359 DFList[Prv.OwnerPkg()->ID].NameMatch = true;
c29652b0
AL
1360 }
1361 }
b2e465d6 1362 }
a52f938b
OS
1363
1364 LocalitySort(&DFList->Df,Cache.HeaderP->PackageCount,sizeof(*DFList));
7e2e2d5d 1365
b2e465d6 1366 // Iterate over all the version records and check them
a52f938b 1367 for (ExDescFile *J = DFList; J->Df != 0; J++)
b2e465d6 1368 {
a52f938b 1369 pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(Cache,J->Df));
0f2fa322
AL
1370
1371 bool Match = true;
1372 if (J->NameMatch == false)
1373 {
1374 string LongDesc = P.LongDesc();
1375 Match = NumPatterns != 0;
1376 for (unsigned I = 0; I != NumPatterns; I++)
1377 {
1378 if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
1379 Match &= true;
1380 else
1381 Match = false;
1382 }
1383 }
b2e465d6
AL
1384
1385 if (Match == true)
9dbb421f 1386 {
7e2e2d5d 1387 if (ShowFull == true)
b2e465d6
AL
1388 {
1389 const char *Start;
1390 const char *End;
1391 P.GetRec(Start,End);
1392 fwrite(Start,End-Start,1,stdout);
1393 putc('\n',stdout);
1394 }
7e2e2d5d 1395 else
b2e465d6
AL
1396 printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
1397 }
9dbb421f
AL
1398 }
1399
a52f938b 1400 delete [] DFList;
b2e465d6
AL
1401 for (unsigned I = 0; I != NumPatterns; I++)
1402 regfree(&Patterns[I]);
1403 if (ferror(stdout))
1404 return _error->Error("Write to stdout failed");
1164783d
AL
1405 return true;
1406}
1407 /*}}}*/
7e2e2d5d
AL
1408// ShowPackage - Dump the package record to the screen /*{{{*/
1409// ---------------------------------------------------------------------
1410/* */
1411bool ShowPackage(CommandLine &CmdL)
1412{
1413 pkgCache &Cache = *GCache;
b2e465d6 1414 pkgDepCache::Policy Plcy;
9d366c89
AL
1415
1416 unsigned found = 0;
b2e465d6 1417
7e2e2d5d
AL
1418 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1419 {
6293e04f 1420 // FIXME: Handle the case in which pkgname name:arch is not found
7e2e2d5d
AL
1421 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1422 if (Pkg.end() == true)
1423 {
6293e04f
DK
1424 Pkg = Cache.FindPkg(*I, "any");
1425 if (Pkg.end() == true) {
1426 _error->Warning(_("Unable to locate package %s"),*I);
1427 continue;
1428 }
7e2e2d5d 1429 }
b2e465d6 1430
9d366c89
AL
1431 ++found;
1432
b2e465d6 1433 // Find the proper version to use.
648e3cb4
AL
1434 if (_config->FindB("APT::Cache::AllVersions","true") == true)
1435 {
1436 pkgCache::VerIterator V;
1437 for (V = Pkg.VersionList(); V.end() == false; V++)
1438 {
1439 if (DisplayRecord(V) == false)
1440 return false;
1441 }
1442 }
1443 else
1444 {
b2e465d6 1445 pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
648e3cb4
AL
1446 if (V.end() == true || V.FileList().end() == true)
1447 continue;
1448 if (DisplayRecord(V) == false)
1449 return false;
1450 }
7e2e2d5d 1451 }
9d366c89
AL
1452
1453 if (found > 0)
1454 return true;
1455 return _error->Error(_("No packages found"));
7c1133fe
AL
1456}
1457 /*}}}*/
1458// ShowPkgNames - Show package names /*{{{*/
1459// ---------------------------------------------------------------------
1460/* This does a prefix match on the first argument */
1461bool ShowPkgNames(CommandLine &CmdL)
1462{
1463 pkgCache &Cache = *GCache;
6293e04f
DK
1464 pkgCache::GrpIterator I = Cache.GrpBegin();
1465 bool const All = _config->FindB("APT::Cache::AllNames","false");
1466
7c1133fe
AL
1467 if (CmdL.FileList[1] != 0)
1468 {
1469 for (;I.end() != true; I++)
1470 {
6293e04f
DK
1471 if (All == false && I->FirstPackage == 0)
1472 continue;
1473 if (I.FindPkg("any")->VersionList == 0)
7c1133fe 1474 continue;
7c1133fe
AL
1475 if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
1476 cout << I.Name() << endl;
1477 }
1478
1479 return true;
1480 }
1481
1482 // Show all pkgs
1483 for (;I.end() != true; I++)
1484 {
6293e04f
DK
1485 if (All == false && I->FirstPackage == 0)
1486 continue;
1487 if (I.FindPkg("any")->VersionList == 0)
7c1133fe
AL
1488 continue;
1489 cout << I.Name() << endl;
1490 }
1491
7e2e2d5d
AL
1492 return true;
1493}
1494 /*}}}*/
f8f410f5
AL
1495// ShowSrcPackage - Show source package records /*{{{*/
1496// ---------------------------------------------------------------------
1497/* */
1498bool ShowSrcPackage(CommandLine &CmdL)
1499{
1500 pkgSourceList List;
1501 List.ReadMainList();
1502
1503 // Create the text record parsers
1504 pkgSrcRecords SrcRecs(List);
1505 if (_error->PendingError() == true)
1506 return false;
1507
0458a811 1508 unsigned found = 0;
f8f410f5
AL
1509 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1510 {
aaee8293
AL
1511 SrcRecs.Restart();
1512
f8f410f5 1513 pkgSrcRecords::Parser *Parse;
0458a811
JAK
1514 unsigned found_this = 0;
1515 while ((Parse = SrcRecs.Find(*I,false)) != 0) {
1516 cout << Parse->AsStr() << endl;;
1517 found++;
1518 found_this++;
1519 }
1520 if (found_this == 0) {
1521 _error->Warning(_("Unable to locate package %s"),*I);
1522 continue;
1523 }
f8f410f5 1524 }
0458a811
JAK
1525 if (found > 0)
1526 return true;
1527 return _error->Error(_("No packages found"));
af87ab54
AL
1528}
1529 /*}}}*/
1530// Policy - Show the results of the preferences file /*{{{*/
1531// ---------------------------------------------------------------------
1532/* */
1533bool Policy(CommandLine &CmdL)
1534{
1535 if (SrcList == 0)
1536 return _error->Error("Generate must be enabled for this function");
1537
1538 pkgCache &Cache = *GCache;
1539 pkgPolicy Plcy(&Cache);
e68ca100 1540 if (ReadPinFile(Plcy) == false || ReadPinDir(Plcy) == false)
af87ab54
AL
1541 return false;
1542
1543 // Print out all of the package files
1544 if (CmdL.FileList[1] == 0)
1545 {
db0db9fe 1546 cout << _("Package files:") << endl;
af87ab54
AL
1547 for (pkgCache::PkgFileIterator F = Cache.FileBegin(); F.end() == false; F++)
1548 {
1549 // Locate the associated index files so we can derive a description
1550 pkgIndexFile *Indx;
1551 if (SrcList->FindIndex(F,Indx) == false &&
1552 _system->FindIndex(F,Indx) == false)
1553 return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
78acd650
MV
1554
1555 printf("%4i %s\n",
af87ab54
AL
1556 Plcy.GetPriority(F),Indx->Describe(true).c_str());
1557
1558 // Print the reference information for the package
1559 string Str = F.RelStr();
1560 if (Str.empty() == false)
1561 printf(" release %s\n",F.RelStr().c_str());
1562 if (F.Site() != 0 && F.Site()[0] != 0)
1563 printf(" origin %s\n",F.Site());
1564 }
1565
1566 // Show any packages have explicit pins
db0db9fe 1567 cout << _("Pinned packages:") << endl;
af87ab54
AL
1568 pkgCache::PkgIterator I = Cache.PkgBegin();
1569 for (;I.end() != true; I++)
1570 {
1571 if (Plcy.GetPriority(I) == 0)
1572 continue;
1573
1574 // Print the package name and the version we are forcing to
75ce2062 1575 cout << " " << I.FullName(true) << " -> ";
af87ab54
AL
1576
1577 pkgCache::VerIterator V = Plcy.GetMatch(I);
1578 if (V.end() == true)
1579 cout << _("(not found)") << endl;
1580 else
1581 cout << V.VerStr() << endl;
1582 }
1583
1584 return true;
1585 }
6293e04f
DK
1586
1587 string const myArch = _config->Find("APT::Architecture");
1588
af87ab54
AL
1589 // Print out detailed information for each package
1590 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1591 {
6293e04f
DK
1592 pkgCache::GrpIterator Grp = Cache.FindGrp(*I);
1593 pkgCache::PkgIterator Pkg = Grp.FindPkg("any");
af87ab54
AL
1594 if (Pkg.end() == true)
1595 {
1596 _error->Warning(_("Unable to locate package %s"),*I);
1597 continue;
1598 }
6293e04f
DK
1599
1600 for (; Pkg.end() != true; Pkg = Grp.NextPkg(Pkg)) {
803ea2a8
DK
1601 if (strcmp(Pkg.Arch(),"all") == 0)
1602 continue;
6293e04f 1603
75ce2062 1604 cout << Pkg.FullName(true) << ":" << endl;
6293e04f 1605
af87ab54
AL
1606 // Installed version
1607 cout << _(" Installed: ");
1608 if (Pkg->CurrentVer == 0)
1609 cout << _("(none)") << endl;
1610 else
1611 cout << Pkg.CurrentVer().VerStr() << endl;
1612
1613 // Candidate Version
1614 cout << _(" Candidate: ");
1615 pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
1616 if (V.end() == true)
1617 cout << _("(none)") << endl;
1618 else
1619 cout << V.VerStr() << endl;
1620
1621 // Pinned version
1622 if (Plcy.GetPriority(Pkg) != 0)
1623 {
db0db9fe 1624 cout << _(" Package pin: ");
af87ab54
AL
1625 V = Plcy.GetMatch(Pkg);
1626 if (V.end() == true)
1627 cout << _("(not found)") << endl;
1628 else
1629 cout << V.VerStr() << endl;
1630 }
1631
1632 // Show the priority tables
db0db9fe 1633 cout << _(" Version table:") << endl;
af87ab54
AL
1634 for (V = Pkg.VersionList(); V.end() == false; V++)
1635 {
1636 if (Pkg.CurrentVer() == V)
1637 cout << " *** " << V.VerStr();
1638 else
1639 cout << " " << V.VerStr();
1640 cout << " " << Plcy.GetPriority(Pkg) << endl;
1641 for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; VF++)
1642 {
1643 // Locate the associated index files so we can derive a description
1644 pkgIndexFile *Indx;
1645 if (SrcList->FindIndex(VF.File(),Indx) == false &&
1646 _system->FindIndex(VF.File(),Indx) == false)
1647 return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
46e39c8e 1648 printf(" %4i %s\n",Plcy.GetPriority(VF.File()),
af87ab54 1649 Indx->Describe(true).c_str());
6293e04f
DK
1650 }
1651 }
1652 }
af87ab54
AL
1653 }
1654
f8f410f5
AL
1655 return true;
1656}
bd3d53ef
AL
1657 /*}}}*/
1658// Madison - Look a bit like katie's madison /*{{{*/
1659// ---------------------------------------------------------------------
1660/* */
1661bool Madison(CommandLine &CmdL)
1662{
1663 if (SrcList == 0)
1664 return _error->Error("Generate must be enabled for this function");
1665
1666 SrcList->ReadMainList();
1667
1668 pkgCache &Cache = *GCache;
1669
03cd434b
MV
1670 // Create the src text record parsers and ignore errors about missing
1671 // deb-src lines that are generated from pkgSrcRecords::pkgSrcRecords
bd3d53ef
AL
1672 pkgSrcRecords SrcRecs(*SrcList);
1673 if (_error->PendingError() == true)
03cd434b 1674 _error->Discard();
bd3d53ef
AL
1675
1676 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1677 {
1678 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1679
1680 if (Pkg.end() == false)
1681 {
1682 for (pkgCache::VerIterator V = Pkg.VersionList(); V.end() == false; V++)
1683 {
1684 for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; VF++)
1685 {
a0e710af
AL
1686// This might be nice, but wouldn't uniquely identify the source -mdz
1687// if (VF.File().Archive() != 0)
1688// {
1689// cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
1690// << VF.File().Archive() << endl;
1691// }
1692
1693 // Locate the associated index files so we can derive a description
1694 for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); S++)
bd3d53ef 1695 {
7db98ffc
MZ
1696 vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
1697 for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
1698 IF != Indexes->end(); IF++)
1699 {
1700 if ((*IF)->FindInCache(*(VF.File().Cache())) == VF.File())
1701 {
75ce2062 1702 cout << setw(10) << Pkg.FullName(true) << " | " << setw(10) << V.VerStr() << " | "
7db98ffc
MZ
1703 << (*IF)->Describe(true) << endl;
1704 }
1705 }
bd3d53ef
AL
1706 }
1707 }
1708 }
1709 }
1710
1711
1712 SrcRecs.Restart();
1713 pkgSrcRecords::Parser *SrcParser;
1714 while ((SrcParser = SrcRecs.Find(*I,false)) != 0)
1715 {
1716 // Maybe support Release info here too eventually
1717 cout << setw(10) << SrcParser->Package() << " | "
1718 << setw(10) << SrcParser->Version() << " | "
1719 << SrcParser->Index().Describe(true) << endl;
1720 }
1721 }
1722
1723 return true;
1724}
f8f410f5 1725 /*}}}*/
880e9be4
AL
1726// GenCaches - Call the main cache generator /*{{{*/
1727// ---------------------------------------------------------------------
1728/* */
b0b4efb9 1729bool GenCaches(CommandLine &Cmd)
880e9be4 1730{
0a8e3465
AL
1731 OpTextProgress Progress(*_config);
1732
880e9be4 1733 pkgSourceList List;
b2e465d6
AL
1734 if (List.ReadMainList() == false)
1735 return false;
0a8e3465 1736 return pkgMakeStatusCache(List,Progress);
880e9be4
AL
1737}
1738 /*}}}*/
e1b74f61
AL
1739// ShowHelp - Show a help screen /*{{{*/
1740// ---------------------------------------------------------------------
1741/* */
b0b4efb9 1742bool ShowHelp(CommandLine &Cmd)
e1b74f61 1743{
5b28c804
OS
1744 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
1745 COMMON_ARCH,__DATE__,__TIME__);
e1b74f61 1746
b13af988
AL
1747 if (_config->FindB("version") == true)
1748 return true;
1749
b2e465d6
AL
1750 cout <<
1751 _("Usage: apt-cache [options] command\n"
b9d2ece3 1752 " apt-cache [options] add file1 [file2 ...]\n"
b2e465d6 1753 " apt-cache [options] showpkg pkg1 [pkg2 ...]\n"
2d425135 1754 " apt-cache [options] showsrc pkg1 [pkg2 ...]\n"
b2e465d6
AL
1755 "\n"
1756 "apt-cache is a low-level tool used to manipulate APT's binary\n"
1757 "cache files, and query information from them\n"
1758 "\n"
1759 "Commands:\n"
bac2e715 1760 " add - Add a package file to the source cache\n"
b2e465d6
AL
1761 " gencaches - Build both the package and source cache\n"
1762 " showpkg - Show some general information for a single package\n"
2d425135 1763 " showsrc - Show source records\n"
b2e465d6
AL
1764 " stats - Show some basic statistics\n"
1765 " dump - Show the entire file in a terse form\n"
1766 " dumpavail - Print an available file to stdout\n"
1767 " unmet - Show unmet dependencies\n"
b2e465d6
AL
1768 " search - Search the package list for a regex pattern\n"
1769 " show - Show a readable record for the package\n"
1770 " depends - Show raw dependency information for a package\n"
eba2b51d 1771 " rdepends - Show reverse dependency information for a package\n"
2c120e24
EL
1772 " pkgnames - List the names of all packages in the system\n"
1773 " dotty - Generate package graphs for GraphViz\n"
fff4b7f3 1774 " xvcg - Generate package graphs for xvcg\n"
eba05d54 1775 " policy - Show policy settings\n"
b2e465d6
AL
1776 "\n"
1777 "Options:\n"
1778 " -h This help text.\n"
1779 " -p=? The package cache.\n"
1780 " -s=? The source cache.\n"
1781 " -q Disable progress indicator.\n"
1782 " -i Show only important deps for the unmet command.\n"
1783 " -c=? Read this configuration file\n"
a2884e32 1784 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
1785 "See the apt-cache(8) and apt.conf(5) manual pages for more information.\n");
1786 return true;
e1b74f61
AL
1787}
1788 /*}}}*/
0a8e3465
AL
1789// CacheInitialize - Initialize things for apt-cache /*{{{*/
1790// ---------------------------------------------------------------------
1791/* */
1792void CacheInitialize()
1793{
1794 _config->Set("quiet",0);
1795 _config->Set("help",false);
1796}
1797 /*}}}*/
92fcbfc1 1798int main(int argc,const char *argv[]) /*{{{*/
1164783d 1799{
08e8f724
AL
1800 CommandLine::Args Args[] = {
1801 {'h',"help","help",0},
04aa15a8 1802 {'v',"version","version",0},
e1b74f61
AL
1803 {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
1804 {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
1805 {'q',"quiet","quiet",CommandLine::IntLevel},
76fbce56 1806 {'i',"important","APT::Cache::Important",0},
7e2e2d5d 1807 {'f',"full","APT::Cache::ShowFull",0},
b2e465d6 1808 {'g',"generate","APT::Cache::Generate",0},
648e3cb4 1809 {'a',"all-versions","APT::Cache::AllVersions",0},
fe6fc1c2
AL
1810 {'n',"names-only","APT::Cache::NamesOnly",0},
1811 {0,"all-names","APT::Cache::AllNames",0},
b2e465d6 1812 {0,"recurse","APT::Cache::RecurseDepends",0},
e1b74f61
AL
1813 {'c',"config-file",0,CommandLine::ConfigFile},
1814 {'o',"option",0,CommandLine::ArbItem},
fe6fc1c2 1815 {0,"installed","APT::Cache::Installed",0},
08e8f724 1816 {0,0,0,0}};
b0b4efb9
AL
1817 CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp},
1818 {"add",&DoAdd},
1819 {"gencaches",&GenCaches},
f8f410f5 1820 {"showsrc",&ShowSrcPackage},
b0b4efb9
AL
1821 {0,0}};
1822 CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage},
1823 {"stats",&Stats},
1824 {"dump",&Dump},
1825 {"dumpavail",&DumpAvail},
1826 {"unmet",&UnMet},
9dbb421f 1827 {"search",&Search},
349cd3b8 1828 {"depends",&Depends},
eba2b51d 1829 {"rdepends",&RDepends},
3e94da1b 1830 {"dotty",&Dotty},
fff4b7f3 1831 {"xvcg",&XVcg},
7e2e2d5d 1832 {"show",&ShowPackage},
7c1133fe 1833 {"pkgnames",&ShowPkgNames},
af87ab54 1834 {"policy",&Policy},
bd3d53ef 1835 {"madison",&Madison},
b0b4efb9 1836 {0,0}};
0a8e3465
AL
1837
1838 CacheInitialize();
67111687
AL
1839
1840 // Set up gettext support
1841 setlocale(LC_ALL,"");
1842 textdomain(PACKAGE);
1843
e1b74f61
AL
1844 // Parse the command line and initialize the package library
1845 CommandLine CmdL(Args,_config);
b2e465d6
AL
1846 if (pkgInitConfig(*_config) == false ||
1847 CmdL.Parse(argc,argv) == false ||
1848 pkgInitSystem(*_config,_system) == false)
08e8f724
AL
1849 {
1850 _error->DumpErrors();
1851 return 100;
1164783d 1852 }
8efa2a3b 1853
e1b74f61
AL
1854 // See if the help should be shown
1855 if (_config->FindB("help") == true ||
1856 CmdL.FileSize() == 0)
b2e465d6
AL
1857 {
1858 ShowHelp(CmdL);
1859 return 0;
1860 }
1861
a9a5908d 1862 // Deal with stdout not being a tty
a3f6ea20 1863 if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
a9a5908d
AL
1864 _config->Set("quiet","1");
1865
b0b4efb9 1866 if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false)
4b1b89c5 1867 {
2a3f3893 1868 MMap *Map = 0;
b2e465d6 1869 if (_config->FindB("APT::Cache::Generate",true) == false)
4b1b89c5
AL
1870 {
1871 Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
1872 FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
1873 }
1874 else
1875 {
1876 // Open the cache file
af87ab54
AL
1877 SrcList = new pkgSourceList;
1878 SrcList->ReadMainList();
f8f410f5 1879
4b1b89c5
AL
1880 // Generate it and map it
1881 OpProgress Prog;
af87ab54 1882 pkgMakeStatusCache(*SrcList,Prog,&Map,true);
4b1b89c5
AL
1883 }
1884
b0b4efb9 1885 if (_error->PendingError() == false)
1164783d 1886 {
b2e465d6 1887 pkgCache Cache(Map);
b0b4efb9
AL
1888 GCache = &Cache;
1889 if (_error->PendingError() == false)
1890 CmdL.DispatchArg(CmdsB);
803fafcb
AL
1891 }
1892 delete Map;
1164783d
AL
1893 }
1894
1895 // Print any errors or warnings found during parsing
1896 if (_error->empty() == false)
1897 {
0a8e3465 1898 bool Errors = _error->PendingError();
1164783d 1899 _error->DumpErrors();
0a8e3465 1900 return Errors == true?100:0;
1164783d
AL
1901 }
1902
1903 return 0;
1904}
92fcbfc1 1905 /*}}}*/