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