Makefile: Fix, committed wrong version.
[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 &&
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;
173ae2a4
DK
245 cout << _("Total package names: ") << Cache.Head().GroupCount << " (" <<
246 SizeToStr(Cache.Head().GroupCount*Cache.Head().GroupSz) << ')' << endl
247 << _("Total package structures: ") << 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 {
75ce2062 358 cout << "Package: " << P.FullName(true) << endl;
1164783d
AL
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++)
75ce2062 364 cout << " Depends: " << D.TargetPkg().FullName(true) << ' ' <<
076d01b0 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 403 pkgPolicy Plcy(&Cache);
e68ca100 404 if (ReadPinFile(Plcy) == false || ReadPinDir(Plcy) == false)
b2e465d6
AL
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);
5aa95c86 560 bool Important = _config->FindB("APT::Cache::Important",false);
b2e465d6
AL
561 bool DidSomething;
562 do
563 {
564 DidSomething = false;
565 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
349cd3b8 566 {
b2e465d6
AL
567 if (Colours[Pkg->ID] != 1)
568 continue;
569 Colours[Pkg->ID] = 2;
570 DidSomething = true;
349cd3b8 571
b2e465d6
AL
572 pkgCache::VerIterator Ver = Pkg.VersionList();
573 if (Ver.end() == true)
349cd3b8 574 {
75ce2062 575 cout << '<' << Pkg.FullName(true) << '>' << endl;
b2e465d6 576 continue;
349cd3b8 577 }
b2e465d6 578
75ce2062 579 cout << Pkg.FullName(true) << endl;
b2e465d6
AL
580
581 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
582 {
5aa95c86
MV
583 // Important deps only
584 if (Important == true)
585 if (D->Type != pkgCache::Dep::PreDepends &&
586 D->Type != pkgCache::Dep::Depends)
587 continue;
588
b2e465d6 589 pkgCache::PkgIterator Trg = D.TargetPkg();
eba2b51d
AL
590
591 if((Installed && Trg->CurrentVer != 0) || !Installed)
592 {
593
594 if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
595 cout << " |";
596 else
597 cout << " ";
b2e465d6 598
eba2b51d
AL
599 // Show the package
600 if (Trg->VersionList == 0)
75ce2062 601 cout << D.DepType() << ": <" << Trg.FullName(true) << ">" << endl;
eba2b51d 602 else
75ce2062 603 cout << D.DepType() << ": " << Trg.FullName(true) << endl;
eba2b51d
AL
604
605 if (Recurse == true)
606 Colours[D.TargetPkg()->ID]++;
607
608 }
b2e465d6
AL
609
610 // Display all solutions
611 SPtrArray<pkgCache::Version *> List = D.AllTargets();
612 pkgPrioSortList(Cache,List);
613 for (pkgCache::Version **I = List; *I != 0; I++)
614 {
615 pkgCache::VerIterator V(Cache,*I);
616 if (V != Cache.VerP + V.ParentPkg()->VersionList ||
617 V->ParentPkg == D->Package)
618 continue;
75ce2062 619 cout << " " << V.ParentPkg().FullName(true) << endl;
b2e465d6
AL
620
621 if (Recurse == true)
622 Colours[D.ParentPkg()->ID]++;
623 }
624 }
625 }
349cd3b8 626 }
b2e465d6 627 while (DidSomething == true);
349cd3b8 628
3e94da1b
AL
629 return true;
630}
92fcbfc1 631 /*}}}*/
eba2b51d
AL
632// RDepends - Print out a reverse dependency tree - mbc /*{{{*/
633// ---------------------------------------------------------------------
634/* */
635bool RDepends(CommandLine &CmdL)
636{
637 pkgCache &Cache = *GCache;
638 SPtrArray<unsigned> Colours = new unsigned[Cache.Head().PackageCount];
639 memset(Colours,0,sizeof(*Colours)*Cache.Head().PackageCount);
640
641 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
642 {
643 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
644 if (Pkg.end() == true)
645 {
646 _error->Warning(_("Unable to locate package %s"),*I);
647 continue;
648 }
649 Colours[Pkg->ID] = 1;
650 }
651
652 bool Recurse = _config->FindB("APT::Cache::RecurseDepends",false);
653 bool Installed = _config->FindB("APT::Cache::Installed",false);
654 bool DidSomething;
655 do
656 {
657 DidSomething = false;
658 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
659 {
660 if (Colours[Pkg->ID] != 1)
661 continue;
662 Colours[Pkg->ID] = 2;
663 DidSomething = true;
664
665 pkgCache::VerIterator Ver = Pkg.VersionList();
666 if (Ver.end() == true)
667 {
75ce2062 668 cout << '<' << Pkg.FullName(true) << '>' << endl;
eba2b51d
AL
669 continue;
670 }
671
75ce2062 672 cout << Pkg.FullName(true) << endl;
eba2b51d
AL
673
674 cout << "Reverse Depends:" << endl;
675 for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() == false; D++)
676 {
677 // Show the package
678 pkgCache::PkgIterator Trg = D.ParentPkg();
679
680 if((Installed && Trg->CurrentVer != 0) || !Installed)
681 {
682
683 if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
684 cout << " |";
685 else
686 cout << " ";
687
688 if (Trg->VersionList == 0)
75ce2062 689 cout << D.DepType() << ": <" << Trg.FullName(true) << ">" << endl;
eba2b51d 690 else
75ce2062 691 cout << Trg.FullName(true) << endl;
eba2b51d
AL
692
693 if (Recurse == true)
694 Colours[D.ParentPkg()->ID]++;
695
696 }
697
698 // Display all solutions
699 SPtrArray<pkgCache::Version *> List = D.AllTargets();
700 pkgPrioSortList(Cache,List);
701 for (pkgCache::Version **I = List; *I != 0; I++)
702 {
703 pkgCache::VerIterator V(Cache,*I);
704 if (V != Cache.VerP + V.ParentPkg()->VersionList ||
705 V->ParentPkg == D->Package)
706 continue;
75ce2062 707 cout << " " << V.ParentPkg().FullName(true) << endl;
eba2b51d
AL
708
709 if (Recurse == true)
710 Colours[D.ParentPkg()->ID]++;
711 }
712 }
713 }
714 }
715 while (DidSomething == true);
716
717 return true;
718}
3e94da1b 719 /*}}}*/
fff4b7f3
AL
720// xvcg - Generate a graph for xvcg /*{{{*/
721// ---------------------------------------------------------------------
722// Code contributed from Junichi Uekawa <dancer@debian.org> on 20 June 2002.
723
724bool XVcg(CommandLine &CmdL)
725{
726 pkgCache &Cache = *GCache;
727 bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
728
729 /* Normal packages are boxes
730 Pure Provides are triangles
731 Mixed are diamonds
732 rhomb are missing packages*/
733 const char *Shapes[] = {"ellipse","triangle","box","rhomb"};
734
735 /* Initialize the list of packages to show.
736 1 = To Show
737 2 = To Show no recurse
738 3 = Emitted no recurse
739 4 = Emitted
740 0 = None */
741 enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
742 enum TheFlags {ForceNR=(1<<0)};
743 unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
744 unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
745 unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
746
747 // Show everything if no arguments given
748 if (CmdL.FileList[1] == 0)
749 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
750 Show[I] = ToShow;
751 else
752 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
753 Show[I] = None;
754 memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
755
756 // Map the shapes
757 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
758 {
759 if (Pkg->VersionList == 0)
760 {
761 // Missing
762 if (Pkg->ProvidesList == 0)
763 ShapeMap[Pkg->ID] = 0;
764 else
765 ShapeMap[Pkg->ID] = 1;
766 }
767 else
768 {
769 // Normal
770 if (Pkg->ProvidesList == 0)
771 ShapeMap[Pkg->ID] = 2;
772 else
773 ShapeMap[Pkg->ID] = 3;
774 }
775 }
776
777 // Load the list of packages from the command line into the show list
778 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
779 {
780 // Process per-package flags
781 string P = *I;
782 bool Force = false;
783 if (P.length() > 3)
784 {
785 if (P.end()[-1] == '^')
786 {
787 Force = true;
788 P.erase(P.end()-1);
789 }
790
791 if (P.end()[-1] == ',')
792 P.erase(P.end()-1);
793 }
794
795 // Locate the package
796 pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
797 if (Pkg.end() == true)
798 {
799 _error->Warning(_("Unable to locate package %s"),*I);
800 continue;
801 }
802 Show[Pkg->ID] = ToShow;
803
804 if (Force == true)
805 Flags[Pkg->ID] |= ForceNR;
806 }
807
808 // Little header
809 cout << "graph: { title: \"packages\"" << endl <<
810 "xmax: 700 ymax: 700 x: 30 y: 30" << endl <<
811 "layout_downfactor: 8" << endl;
812
813 bool Act = true;
814 while (Act == true)
815 {
816 Act = false;
817 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
818 {
819 // See we need to show this package
820 if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
821 continue;
822
823 //printf ("node: { title: \"%s\" label: \"%s\" }\n", Pkg.Name(), Pkg.Name());
824
825 // Colour as done
826 if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
827 {
828 // Pure Provides and missing packages have no deps!
829 if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
830 Show[Pkg->ID] = Done;
831 else
832 Show[Pkg->ID] = DoneNR;
833 }
834 else
835 Show[Pkg->ID] = Done;
836 Act = true;
837
838 // No deps to map out
839 if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
840 continue;
841
842 pkgCache::VerIterator Ver = Pkg.VersionList();
843 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
844 {
845 // See if anything can meet this dep
846 // Walk along the actual package providing versions
847 bool Hit = false;
848 pkgCache::PkgIterator DPkg = D.TargetPkg();
849 for (pkgCache::VerIterator I = DPkg.VersionList();
850 I.end() == false && Hit == false; I++)
851 {
852 if (Cache.VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
853 Hit = true;
854 }
855
856 // Follow all provides
857 for (pkgCache::PrvIterator I = DPkg.ProvidesList();
858 I.end() == false && Hit == false; I++)
859 {
860 if (Cache.VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
861 Hit = true;
862 }
863
864
865 // Only graph critical deps
866 if (D.IsCritical() == true)
867 {
75ce2062 868 printf ("edge: { sourcename: \"%s\" targetname: \"%s\" class: 2 ",Pkg.FullName(true).c_str(), D.TargetPkg().FullName(true).c_str() );
fff4b7f3
AL
869
870 // Colour the node for recursion
871 if (Show[D.TargetPkg()->ID] <= DoneNR)
872 {
873 /* If a conflicts does not meet anything in the database
874 then show the relation but do not recurse */
875 if (Hit == false &&
876 (D->Type == pkgCache::Dep::Conflicts ||
308c7d30 877 D->Type == pkgCache::Dep::DpkgBreaks ||
fff4b7f3
AL
878 D->Type == pkgCache::Dep::Obsoletes))
879 {
880 if (Show[D.TargetPkg()->ID] == None &&
881 Show[D.TargetPkg()->ID] != ToShow)
882 Show[D.TargetPkg()->ID] = ToShowNR;
883 }
884 else
885 {
886 if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
887 Show[D.TargetPkg()->ID] = ToShowNR;
888 else
889 Show[D.TargetPkg()->ID] = ToShow;
890 }
891 }
892
893 // Edge colour
894 switch(D->Type)
895 {
896 case pkgCache::Dep::Conflicts:
897 printf("label: \"conflicts\" color: lightgreen }\n");
898 break;
308c7d30
IJ
899 case pkgCache::Dep::DpkgBreaks:
900 printf("label: \"breaks\" color: lightgreen }\n");
901 break;
fff4b7f3
AL
902 case pkgCache::Dep::Obsoletes:
903 printf("label: \"obsoletes\" color: lightgreen }\n");
904 break;
905
906 case pkgCache::Dep::PreDepends:
907 printf("label: \"predepends\" color: blue }\n");
908 break;
909
910 default:
911 printf("}\n");
912 break;
913 }
914 }
915 }
916 }
917 }
918
919 /* Draw the box colours after the fact since we can not tell what colour
920 they should be until everything is finished drawing */
921 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
922 {
923 if (Show[Pkg->ID] < DoneNR)
924 continue;
925
926 if (Show[Pkg->ID] == DoneNR)
75ce2062 927 printf("node: { title: \"%s\" label: \"%s\" color: orange shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
fff4b7f3
AL
928 Shapes[ShapeMap[Pkg->ID]]);
929 else
75ce2062 930 printf("node: { title: \"%s\" label: \"%s\" shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
fff4b7f3
AL
931 Shapes[ShapeMap[Pkg->ID]]);
932
933 }
03496041
DK
934
935 delete[] Show;
936 delete[] Flags;
937 delete[] ShapeMap;
938
fff4b7f3
AL
939 printf("}\n");
940 return true;
941}
942 /*}}}*/
3e94da1b
AL
943// Dotty - Generate a graph for Dotty /*{{{*/
944// ---------------------------------------------------------------------
945/* Dotty is the graphvis program for generating graphs. It is a fairly
946 simple queuing algorithm that just writes dependencies and nodes.
947 http://www.research.att.com/sw/tools/graphviz/ */
948bool Dotty(CommandLine &CmdL)
949{
950 pkgCache &Cache = *GCache;
951 bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
952
953 /* Normal packages are boxes
954 Pure Provides are triangles
955 Mixed are diamonds
956 Hexagons are missing packages*/
957 const char *Shapes[] = {"hexagon","triangle","box","diamond"};
958
959 /* Initialize the list of packages to show.
960 1 = To Show
961 2 = To Show no recurse
962 3 = Emitted no recurse
963 4 = Emitted
964 0 = None */
965 enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
966 enum TheFlags {ForceNR=(1<<0)};
967 unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
968 unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
969 unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
970
971 // Show everything if no arguments given
972 if (CmdL.FileList[1] == 0)
973 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
974 Show[I] = ToShow;
975 else
976 for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
977 Show[I] = None;
978 memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
979
980 // Map the shapes
981 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
982 {
983 if (Pkg->VersionList == 0)
984 {
985 // Missing
986 if (Pkg->ProvidesList == 0)
987 ShapeMap[Pkg->ID] = 0;
988 else
989 ShapeMap[Pkg->ID] = 1;
990 }
991 else
992 {
993 // Normal
994 if (Pkg->ProvidesList == 0)
995 ShapeMap[Pkg->ID] = 2;
996 else
997 ShapeMap[Pkg->ID] = 3;
998 }
999 }
1000
1001 // Load the list of packages from the command line into the show list
1002 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1003 {
1004 // Process per-package flags
1005 string P = *I;
1006 bool Force = false;
1007 if (P.length() > 3)
1008 {
1009 if (P.end()[-1] == '^')
1010 {
1011 Force = true;
1012 P.erase(P.end()-1);
1013 }
1014
1015 if (P.end()[-1] == ',')
1016 P.erase(P.end()-1);
1017 }
1018
1019 // Locate the package
1020 pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
1021 if (Pkg.end() == true)
1022 {
b2e465d6 1023 _error->Warning(_("Unable to locate package %s"),*I);
3e94da1b
AL
1024 continue;
1025 }
1026 Show[Pkg->ID] = ToShow;
1027
1028 if (Force == true)
1029 Flags[Pkg->ID] |= ForceNR;
1030 }
1031
1032 // Little header
1033 printf("digraph packages {\n");
1034 printf("concentrate=true;\n");
1035 printf("size=\"30,40\";\n");
1036
1037 bool Act = true;
1038 while (Act == true)
1039 {
1040 Act = false;
1041 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
1042 {
1043 // See we need to show this package
1044 if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
1045 continue;
1046
1047 // Colour as done
1048 if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
1049 {
1050 // Pure Provides and missing packages have no deps!
1051 if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
1052 Show[Pkg->ID] = Done;
1053 else
1054 Show[Pkg->ID] = DoneNR;
1055 }
1056 else
1057 Show[Pkg->ID] = Done;
1058 Act = true;
1059
1060 // No deps to map out
1061 if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
1062 continue;
1063
1064 pkgCache::VerIterator Ver = Pkg.VersionList();
1065 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
1066 {
1067 // See if anything can meet this dep
1068 // Walk along the actual package providing versions
1069 bool Hit = false;
1070 pkgCache::PkgIterator DPkg = D.TargetPkg();
1071 for (pkgCache::VerIterator I = DPkg.VersionList();
1072 I.end() == false && Hit == false; I++)
1073 {
b2e465d6 1074 if (Cache.VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
3e94da1b
AL
1075 Hit = true;
1076 }
1077
1078 // Follow all provides
1079 for (pkgCache::PrvIterator I = DPkg.ProvidesList();
1080 I.end() == false && Hit == false; I++)
1081 {
b2e465d6 1082 if (Cache.VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
3e94da1b
AL
1083 Hit = true;
1084 }
1085
1086 // Only graph critical deps
1087 if (D.IsCritical() == true)
1088 {
75ce2062 1089 printf("\"%s\" -> \"%s\"",Pkg.FullName(true).c_str(),D.TargetPkg().FullName(true).c_str());
3e94da1b
AL
1090
1091 // Colour the node for recursion
1092 if (Show[D.TargetPkg()->ID] <= DoneNR)
1093 {
1094 /* If a conflicts does not meet anything in the database
1095 then show the relation but do not recurse */
b2e465d6
AL
1096 if (Hit == false &&
1097 (D->Type == pkgCache::Dep::Conflicts ||
1098 D->Type == pkgCache::Dep::Obsoletes))
3e94da1b
AL
1099 {
1100 if (Show[D.TargetPkg()->ID] == None &&
1101 Show[D.TargetPkg()->ID] != ToShow)
1102 Show[D.TargetPkg()->ID] = ToShowNR;
1103 }
1104 else
1105 {
1106 if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
1107 Show[D.TargetPkg()->ID] = ToShowNR;
1108 else
1109 Show[D.TargetPkg()->ID] = ToShow;
1110 }
1111 }
1112
1113 // Edge colour
1114 switch(D->Type)
1115 {
1116 case pkgCache::Dep::Conflicts:
b2e465d6 1117 case pkgCache::Dep::Obsoletes:
3e94da1b
AL
1118 printf("[color=springgreen];\n");
1119 break;
1120
1121 case pkgCache::Dep::PreDepends:
1122 printf("[color=blue];\n");
1123 break;
1124
1125 default:
1126 printf(";\n");
1127 break;
1128 }
1129 }
1130 }
1131 }
1132 }
1133
1134 /* Draw the box colours after the fact since we can not tell what colour
1135 they should be until everything is finished drawing */
1136 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
1137 {
1138 if (Show[Pkg->ID] < DoneNR)
1139 continue;
1140
1141 // Orange box for early recursion stoppage
1142 if (Show[Pkg->ID] == DoneNR)
75ce2062 1143 printf("\"%s\" [color=orange,shape=%s];\n",Pkg.FullName(true).c_str(),
3e94da1b
AL
1144 Shapes[ShapeMap[Pkg->ID]]);
1145 else
75ce2062 1146 printf("\"%s\" [shape=%s];\n",Pkg.FullName(true).c_str(),
3e94da1b
AL
1147 Shapes[ShapeMap[Pkg->ID]]);
1148 }
1149
1150 printf("}\n");
ad00ae81
AL
1151 return true;
1152}
1153 /*}}}*/
1154// DoAdd - Perform an adding operation /*{{{*/
1155// ---------------------------------------------------------------------
1156/* */
e1b74f61 1157bool DoAdd(CommandLine &CmdL)
ad00ae81 1158{
b2e465d6
AL
1159 return _error->Error("Unimplemented");
1160#if 0
e1b74f61
AL
1161 // Make sure there is at least one argument
1162 if (CmdL.FileSize() <= 1)
1163 return _error->Error("You must give at least one file name");
ad00ae81
AL
1164
1165 // Open the cache
018f1533 1166 FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny);
ad00ae81
AL
1167 if (_error->PendingError() == true)
1168 return false;
1169
1170 DynamicMMap Map(CacheF,MMap::Public);
1171 if (_error->PendingError() == true)
1172 return false;
404ec98e 1173
0a8e3465 1174 OpTextProgress Progress(*_config);
404ec98e 1175 pkgCacheGenerator Gen(Map,Progress);
ad00ae81
AL
1176 if (_error->PendingError() == true)
1177 return false;
1178
e1b74f61
AL
1179 unsigned long Length = CmdL.FileSize() - 1;
1180 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
ad00ae81 1181 {
e1b74f61 1182 Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
018f1533
AL
1183 Progress.SubProgress(Length);
1184
ad00ae81 1185 // Do the merge
e1b74f61 1186 FileFd TagF(*I,FileFd::ReadOnly);
ad00ae81
AL
1187 debListParser Parser(TagF);
1188 if (_error->PendingError() == true)
e1b74f61 1189 return _error->Error("Problem opening %s",*I);
ad00ae81 1190
b2e465d6 1191 if (Gen.SelectFile(*I,"") == false)
ad00ae81
AL
1192 return _error->Error("Problem with SelectFile");
1193
1194 if (Gen.MergeList(Parser) == false)
1195 return _error->Error("Problem with MergeList");
1164783d 1196 }
404ec98e
AL
1197
1198 Progress.Done();
b0b4efb9
AL
1199 GCache = &Gen.GetCache();
1200 Stats(CmdL);
ad00ae81 1201
7e2e2d5d 1202 return true;
b2e465d6 1203#endif
7e2e2d5d
AL
1204}
1205 /*}}}*/
1206// DisplayRecord - Displays the complete record for the package /*{{{*/
1207// ---------------------------------------------------------------------
1208/* This displays the package record from the proper package index file.
1209 It is not used by DumpAvail for performance reasons. */
1210bool DisplayRecord(pkgCache::VerIterator V)
1211{
1212 // Find an appropriate file
1213 pkgCache::VerFileIterator Vf = V.FileList();
1214 for (; Vf.end() == false; Vf++)
1215 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
1216 break;
1217 if (Vf.end() == true)
1218 Vf = V.FileList();
1219
1220 // Check and load the package list file
1221 pkgCache::PkgFileIterator I = Vf.File();
1222 if (I.IsOk() == false)
b2e465d6 1223 return _error->Error(_("Package file %s is out of sync."),I.FileName());
7e2e2d5d
AL
1224
1225 FileFd PkgF(I.FileName(),FileFd::ReadOnly);
1226 if (_error->PendingError() == true)
1227 return false;
1228
a52f938b 1229 // Read the record
b2e465d6
AL
1230 unsigned char *Buffer = new unsigned char[GCache->HeaderP->MaxVerFileSize+1];
1231 Buffer[V.FileList()->Size] = '\n';
7e2e2d5d 1232 if (PkgF.Seek(V.FileList()->Offset) == false ||
a52f938b 1233 PkgF.Read(Buffer,V.FileList()->Size) == false)
7e2e2d5d
AL
1234 {
1235 delete [] Buffer;
1236 return false;
1237 }
a52f938b 1238
e5e2d176 1239 // Get a pointer to start of Description field
e011829d 1240 const unsigned char *DescP = (unsigned char*)strstr((char*)Buffer, "Description:");
e5e2d176
MV
1241
1242 // Write all but Description
487d7faa 1243 if (fwrite(Buffer,1,DescP - Buffer,stdout) < (size_t)(DescP - Buffer))
a52f938b
OS
1244 {
1245 delete [] Buffer;
1246 return false;
1247 }
1248
a52f938b
OS
1249 // Show the right description
1250 pkgRecords Recs(*GCache);
012b102a 1251 pkgCache::DescIterator Desc = V.TranslatedDescription();
a52f938b 1252 pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
e011829d 1253 cout << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc();
a52f938b 1254
e011829d
MV
1255 // Find the first field after the description (if there is any)
1256 for(DescP++;DescP != &Buffer[V.FileList()->Size];DescP++)
1257 {
1258 if(*DescP == '\n' && *(DescP+1) != ' ')
1259 {
1260 // write the rest of the buffer
1261 const unsigned char *end=&Buffer[V.FileList()->Size];
1262 if (fwrite(DescP,1,end-DescP,stdout) < (size_t)(end-DescP))
1263 {
1264 delete [] Buffer;
1265 return false;
1266 }
1267
1268 break;
1269 }
1270 }
1271 // write a final newline (after the description)
1272 cout<<endl;
7e2e2d5d
AL
1273 delete [] Buffer;
1274
9dbb421f
AL
1275 return true;
1276}
1277 /*}}}*/
92fcbfc1 1278
a52f938b 1279struct ExDescFile
b2e465d6 1280{
a52f938b 1281 pkgCache::DescFile *Df;
b2e465d6
AL
1282 bool NameMatch;
1283};
1284
92fcbfc1
DK
1285// Search - Perform a search /*{{{*/
1286// ---------------------------------------------------------------------
1287/* This searches the package names and package descriptions for a pattern */
9dbb421f
AL
1288bool Search(CommandLine &CmdL)
1289{
1290 pkgCache &Cache = *GCache;
7e2e2d5d
AL
1291 bool ShowFull = _config->FindB("APT::Cache::ShowFull",false);
1292 bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
b2e465d6
AL
1293 unsigned NumPatterns = CmdL.FileSize() -1;
1294
1295 pkgDepCache::Policy Plcy;
9dbb421f
AL
1296
1297 // Make sure there is at least one argument
b2e465d6
AL
1298 if (NumPatterns < 1)
1299 return _error->Error(_("You must give exactly one pattern"));
9dbb421f
AL
1300
1301 // Compile the regex pattern
b2e465d6
AL
1302 regex_t *Patterns = new regex_t[NumPatterns];
1303 memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
1304 for (unsigned I = 0; I != NumPatterns; I++)
1305 {
1306 if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
1307 REG_NOSUB) != 0)
1308 {
1309 for (; I != 0; I--)
1310 regfree(&Patterns[I]);
1311 return _error->Error("Regex compilation error");
1312 }
1313 }
9dbb421f
AL
1314
1315 // Create the text record parser
1316 pkgRecords Recs(Cache);
1317 if (_error->PendingError() == true)
b2e465d6
AL
1318 {
1319 for (unsigned I = 0; I != NumPatterns; I++)
1320 regfree(&Patterns[I]);
9dbb421f 1321 return false;
b2e465d6 1322 }
9dbb421f 1323
a52f938b
OS
1324 ExDescFile *DFList = new ExDescFile[Cache.HeaderP->PackageCount+1];
1325 memset(DFList,0,sizeof(*DFList)*Cache.HeaderP->PackageCount+1);
b2e465d6
AL
1326
1327 // Map versions that we want to write out onto the VerList array.
1328 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
9dbb421f 1329 {
a52f938b 1330 DFList[P->ID].NameMatch = NumPatterns != 0;
b2e465d6
AL
1331 for (unsigned I = 0; I != NumPatterns; I++)
1332 {
1333 if (regexec(&Patterns[I],P.Name(),0,0,0) == 0)
a52f938b 1334 DFList[P->ID].NameMatch &= true;
0f2fa322 1335 else
a52f938b 1336 DFList[P->ID].NameMatch = false;
b2e465d6 1337 }
c29652b0 1338
b2e465d6 1339 // Doing names only, drop any that dont match..
a52f938b 1340 if (NamesOnly == true && DFList[P->ID].NameMatch == false)
b2e465d6
AL
1341 continue;
1342
1343 // Find the proper version to use.
1344 pkgCache::VerIterator V = Plcy.GetCandidateVer(P);
c29652b0 1345 if (V.end() == false)
a52f938b 1346 DFList[P->ID].Df = V.DescriptionList().FileList();
c29652b0
AL
1347 }
1348
1349 // Include all the packages that provide matching names too
1350 for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
1351 {
a52f938b 1352 if (DFList[P->ID].NameMatch == false)
7e2e2d5d 1353 continue;
c29652b0
AL
1354
1355 for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; Prv++)
1356 {
1357 pkgCache::VerIterator V = Plcy.GetCandidateVer(Prv.OwnerPkg());
1358 if (V.end() == false)
1359 {
a52f938b
OS
1360 DFList[Prv.OwnerPkg()->ID].Df = V.DescriptionList().FileList();
1361 DFList[Prv.OwnerPkg()->ID].NameMatch = true;
c29652b0
AL
1362 }
1363 }
b2e465d6 1364 }
a52f938b
OS
1365
1366 LocalitySort(&DFList->Df,Cache.HeaderP->PackageCount,sizeof(*DFList));
7e2e2d5d 1367
b2e465d6 1368 // Iterate over all the version records and check them
a52f938b 1369 for (ExDescFile *J = DFList; J->Df != 0; J++)
b2e465d6 1370 {
a52f938b 1371 pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(Cache,J->Df));
0f2fa322
AL
1372
1373 bool Match = true;
1374 if (J->NameMatch == false)
1375 {
1376 string LongDesc = P.LongDesc();
1377 Match = NumPatterns != 0;
1378 for (unsigned I = 0; I != NumPatterns; I++)
1379 {
1380 if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
1381 Match &= true;
1382 else
1383 Match = false;
1384 }
1385 }
b2e465d6
AL
1386
1387 if (Match == true)
9dbb421f 1388 {
7e2e2d5d 1389 if (ShowFull == true)
b2e465d6
AL
1390 {
1391 const char *Start;
1392 const char *End;
1393 P.GetRec(Start,End);
1394 fwrite(Start,End-Start,1,stdout);
1395 putc('\n',stdout);
1396 }
7e2e2d5d 1397 else
b2e465d6
AL
1398 printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
1399 }
9dbb421f
AL
1400 }
1401
a52f938b 1402 delete [] DFList;
b2e465d6
AL
1403 for (unsigned I = 0; I != NumPatterns; I++)
1404 regfree(&Patterns[I]);
1405 if (ferror(stdout))
1406 return _error->Error("Write to stdout failed");
1164783d
AL
1407 return true;
1408}
1409 /*}}}*/
7e2e2d5d
AL
1410// ShowPackage - Dump the package record to the screen /*{{{*/
1411// ---------------------------------------------------------------------
1412/* */
1413bool ShowPackage(CommandLine &CmdL)
1414{
1415 pkgCache &Cache = *GCache;
b2e465d6 1416 pkgDepCache::Policy Plcy;
9d366c89
AL
1417
1418 unsigned found = 0;
b2e465d6 1419
7e2e2d5d
AL
1420 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1421 {
6293e04f 1422 // FIXME: Handle the case in which pkgname name:arch is not found
7e2e2d5d
AL
1423 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1424 if (Pkg.end() == true)
1425 {
6293e04f
DK
1426 Pkg = Cache.FindPkg(*I, "any");
1427 if (Pkg.end() == true) {
1428 _error->Warning(_("Unable to locate package %s"),*I);
1429 continue;
1430 }
7e2e2d5d 1431 }
b2e465d6 1432
9d366c89
AL
1433 ++found;
1434
b2e465d6 1435 // Find the proper version to use.
648e3cb4
AL
1436 if (_config->FindB("APT::Cache::AllVersions","true") == true)
1437 {
1438 pkgCache::VerIterator V;
1439 for (V = Pkg.VersionList(); V.end() == false; V++)
1440 {
1441 if (DisplayRecord(V) == false)
1442 return false;
1443 }
1444 }
1445 else
1446 {
b2e465d6 1447 pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
648e3cb4
AL
1448 if (V.end() == true || V.FileList().end() == true)
1449 continue;
1450 if (DisplayRecord(V) == false)
1451 return false;
1452 }
7e2e2d5d 1453 }
9d366c89
AL
1454
1455 if (found > 0)
1456 return true;
1457 return _error->Error(_("No packages found"));
7c1133fe
AL
1458}
1459 /*}}}*/
1460// ShowPkgNames - Show package names /*{{{*/
1461// ---------------------------------------------------------------------
1462/* This does a prefix match on the first argument */
1463bool ShowPkgNames(CommandLine &CmdL)
1464{
1465 pkgCache &Cache = *GCache;
6293e04f
DK
1466 pkgCache::GrpIterator I = Cache.GrpBegin();
1467 bool const All = _config->FindB("APT::Cache::AllNames","false");
1468
7c1133fe
AL
1469 if (CmdL.FileList[1] != 0)
1470 {
1471 for (;I.end() != true; I++)
1472 {
6293e04f
DK
1473 if (All == false && I->FirstPackage == 0)
1474 continue;
1475 if (I.FindPkg("any")->VersionList == 0)
7c1133fe 1476 continue;
7c1133fe
AL
1477 if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
1478 cout << I.Name() << endl;
1479 }
1480
1481 return true;
1482 }
1483
1484 // Show all pkgs
1485 for (;I.end() != true; I++)
1486 {
6293e04f
DK
1487 if (All == false && I->FirstPackage == 0)
1488 continue;
1489 if (I.FindPkg("any")->VersionList == 0)
7c1133fe
AL
1490 continue;
1491 cout << I.Name() << endl;
1492 }
1493
7e2e2d5d
AL
1494 return true;
1495}
1496 /*}}}*/
f8f410f5
AL
1497// ShowSrcPackage - Show source package records /*{{{*/
1498// ---------------------------------------------------------------------
1499/* */
1500bool ShowSrcPackage(CommandLine &CmdL)
1501{
1502 pkgSourceList List;
1503 List.ReadMainList();
1504
1505 // Create the text record parsers
1506 pkgSrcRecords SrcRecs(List);
1507 if (_error->PendingError() == true)
1508 return false;
1509
0458a811 1510 unsigned found = 0;
f8f410f5
AL
1511 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1512 {
aaee8293
AL
1513 SrcRecs.Restart();
1514
f8f410f5 1515 pkgSrcRecords::Parser *Parse;
0458a811
JAK
1516 unsigned found_this = 0;
1517 while ((Parse = SrcRecs.Find(*I,false)) != 0) {
1518 cout << Parse->AsStr() << endl;;
1519 found++;
1520 found_this++;
1521 }
1522 if (found_this == 0) {
1523 _error->Warning(_("Unable to locate package %s"),*I);
1524 continue;
1525 }
f8f410f5 1526 }
0458a811
JAK
1527 if (found > 0)
1528 return true;
1529 return _error->Error(_("No packages found"));
af87ab54
AL
1530}
1531 /*}}}*/
1532// Policy - Show the results of the preferences file /*{{{*/
1533// ---------------------------------------------------------------------
1534/* */
1535bool Policy(CommandLine &CmdL)
1536{
1537 if (SrcList == 0)
1538 return _error->Error("Generate must be enabled for this function");
1539
1540 pkgCache &Cache = *GCache;
1541 pkgPolicy Plcy(&Cache);
e68ca100 1542 if (ReadPinFile(Plcy) == false || ReadPinDir(Plcy) == false)
af87ab54
AL
1543 return false;
1544
1545 // Print out all of the package files
1546 if (CmdL.FileList[1] == 0)
1547 {
db0db9fe 1548 cout << _("Package files:") << endl;
af87ab54
AL
1549 for (pkgCache::PkgFileIterator F = Cache.FileBegin(); F.end() == false; F++)
1550 {
1551 // Locate the associated index files so we can derive a description
1552 pkgIndexFile *Indx;
1553 if (SrcList->FindIndex(F,Indx) == false &&
1554 _system->FindIndex(F,Indx) == false)
1555 return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
78acd650
MV
1556
1557 printf("%4i %s\n",
af87ab54
AL
1558 Plcy.GetPriority(F),Indx->Describe(true).c_str());
1559
1560 // Print the reference information for the package
1561 string Str = F.RelStr();
1562 if (Str.empty() == false)
1563 printf(" release %s\n",F.RelStr().c_str());
1564 if (F.Site() != 0 && F.Site()[0] != 0)
1565 printf(" origin %s\n",F.Site());
1566 }
1567
1568 // Show any packages have explicit pins
db0db9fe 1569 cout << _("Pinned packages:") << endl;
af87ab54
AL
1570 pkgCache::PkgIterator I = Cache.PkgBegin();
1571 for (;I.end() != true; I++)
1572 {
1573 if (Plcy.GetPriority(I) == 0)
1574 continue;
1575
1576 // Print the package name and the version we are forcing to
75ce2062 1577 cout << " " << I.FullName(true) << " -> ";
af87ab54
AL
1578
1579 pkgCache::VerIterator V = Plcy.GetMatch(I);
1580 if (V.end() == true)
1581 cout << _("(not found)") << endl;
1582 else
1583 cout << V.VerStr() << endl;
1584 }
1585
1586 return true;
1587 }
6293e04f
DK
1588
1589 string const myArch = _config->Find("APT::Architecture");
ca964703
DK
1590 char const * const msgInstalled = _(" Installed: ");
1591 char const * const msgCandidate = _(" Candidate: ");
1592 short const InstalledLessCandidate =
1593 mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
1594 short const deepInstalled =
1595 (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
1596 short const deepCandidate =
1597 (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
6293e04f 1598
af87ab54
AL
1599 // Print out detailed information for each package
1600 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1601 {
6293e04f
DK
1602 pkgCache::GrpIterator Grp = Cache.FindGrp(*I);
1603 pkgCache::PkgIterator Pkg = Grp.FindPkg("any");
af87ab54
AL
1604 if (Pkg.end() == true)
1605 {
1606 _error->Warning(_("Unable to locate package %s"),*I);
1607 continue;
1608 }
6293e04f
DK
1609
1610 for (; Pkg.end() != true; Pkg = Grp.NextPkg(Pkg)) {
803ea2a8
DK
1611 if (strcmp(Pkg.Arch(),"all") == 0)
1612 continue;
6293e04f 1613
75ce2062 1614 cout << Pkg.FullName(true) << ":" << endl;
6293e04f 1615
af87ab54 1616 // Installed version
ca964703 1617 cout << msgInstalled << OutputInDepth(deepInstalled, " ");
af87ab54
AL
1618 if (Pkg->CurrentVer == 0)
1619 cout << _("(none)") << endl;
1620 else
1621 cout << Pkg.CurrentVer().VerStr() << endl;
1622
1623 // Candidate Version
ca964703 1624 cout << msgCandidate << OutputInDepth(deepCandidate, " ");
af87ab54
AL
1625 pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
1626 if (V.end() == true)
1627 cout << _("(none)") << endl;
1628 else
1629 cout << V.VerStr() << endl;
1630
1631 // Pinned version
1632 if (Plcy.GetPriority(Pkg) != 0)
1633 {
db0db9fe 1634 cout << _(" Package pin: ");
af87ab54
AL
1635 V = Plcy.GetMatch(Pkg);
1636 if (V.end() == true)
1637 cout << _("(not found)") << endl;
1638 else
1639 cout << V.VerStr() << endl;
1640 }
1641
1642 // Show the priority tables
db0db9fe 1643 cout << _(" Version table:") << endl;
af87ab54
AL
1644 for (V = Pkg.VersionList(); V.end() == false; V++)
1645 {
1646 if (Pkg.CurrentVer() == V)
1647 cout << " *** " << V.VerStr();
1648 else
1649 cout << " " << V.VerStr();
1650 cout << " " << Plcy.GetPriority(Pkg) << endl;
1651 for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; VF++)
1652 {
1653 // Locate the associated index files so we can derive a description
1654 pkgIndexFile *Indx;
1655 if (SrcList->FindIndex(VF.File(),Indx) == false &&
1656 _system->FindIndex(VF.File(),Indx) == false)
1657 return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
46e39c8e 1658 printf(" %4i %s\n",Plcy.GetPriority(VF.File()),
af87ab54 1659 Indx->Describe(true).c_str());
6293e04f
DK
1660 }
1661 }
1662 }
af87ab54
AL
1663 }
1664
f8f410f5
AL
1665 return true;
1666}
bd3d53ef
AL
1667 /*}}}*/
1668// Madison - Look a bit like katie's madison /*{{{*/
1669// ---------------------------------------------------------------------
1670/* */
1671bool Madison(CommandLine &CmdL)
1672{
1673 if (SrcList == 0)
1674 return _error->Error("Generate must be enabled for this function");
1675
1676 SrcList->ReadMainList();
1677
1678 pkgCache &Cache = *GCache;
1679
03cd434b
MV
1680 // Create the src text record parsers and ignore errors about missing
1681 // deb-src lines that are generated from pkgSrcRecords::pkgSrcRecords
bd3d53ef
AL
1682 pkgSrcRecords SrcRecs(*SrcList);
1683 if (_error->PendingError() == true)
03cd434b 1684 _error->Discard();
bd3d53ef
AL
1685
1686 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1687 {
1688 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
1689
1690 if (Pkg.end() == false)
1691 {
1692 for (pkgCache::VerIterator V = Pkg.VersionList(); V.end() == false; V++)
1693 {
1694 for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; VF++)
1695 {
a0e710af
AL
1696// This might be nice, but wouldn't uniquely identify the source -mdz
1697// if (VF.File().Archive() != 0)
1698// {
1699// cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
1700// << VF.File().Archive() << endl;
1701// }
1702
1703 // Locate the associated index files so we can derive a description
1704 for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); S++)
bd3d53ef 1705 {
7db98ffc
MZ
1706 vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
1707 for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
1708 IF != Indexes->end(); IF++)
1709 {
1710 if ((*IF)->FindInCache(*(VF.File().Cache())) == VF.File())
1711 {
75ce2062 1712 cout << setw(10) << Pkg.FullName(true) << " | " << setw(10) << V.VerStr() << " | "
7db98ffc
MZ
1713 << (*IF)->Describe(true) << endl;
1714 }
1715 }
bd3d53ef
AL
1716 }
1717 }
1718 }
1719 }
1720
1721
1722 SrcRecs.Restart();
1723 pkgSrcRecords::Parser *SrcParser;
1724 while ((SrcParser = SrcRecs.Find(*I,false)) != 0)
1725 {
1726 // Maybe support Release info here too eventually
1727 cout << setw(10) << SrcParser->Package() << " | "
1728 << setw(10) << SrcParser->Version() << " | "
1729 << SrcParser->Index().Describe(true) << endl;
1730 }
1731 }
1732
1733 return true;
1734}
f8f410f5 1735 /*}}}*/
880e9be4
AL
1736// GenCaches - Call the main cache generator /*{{{*/
1737// ---------------------------------------------------------------------
1738/* */
b0b4efb9 1739bool GenCaches(CommandLine &Cmd)
880e9be4 1740{
0a8e3465
AL
1741 OpTextProgress Progress(*_config);
1742
880e9be4 1743 pkgSourceList List;
b2e465d6
AL
1744 if (List.ReadMainList() == false)
1745 return false;
0a8e3465 1746 return pkgMakeStatusCache(List,Progress);
880e9be4
AL
1747}
1748 /*}}}*/
e1b74f61
AL
1749// ShowHelp - Show a help screen /*{{{*/
1750// ---------------------------------------------------------------------
1751/* */
b0b4efb9 1752bool ShowHelp(CommandLine &Cmd)
e1b74f61 1753{
5b28c804
OS
1754 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
1755 COMMON_ARCH,__DATE__,__TIME__);
e1b74f61 1756
b13af988
AL
1757 if (_config->FindB("version") == true)
1758 return true;
1759
b2e465d6
AL
1760 cout <<
1761 _("Usage: apt-cache [options] command\n"
b9d2ece3 1762 " apt-cache [options] add file1 [file2 ...]\n"
b2e465d6 1763 " apt-cache [options] showpkg pkg1 [pkg2 ...]\n"
2d425135 1764 " apt-cache [options] showsrc pkg1 [pkg2 ...]\n"
b2e465d6
AL
1765 "\n"
1766 "apt-cache is a low-level tool used to manipulate APT's binary\n"
1767 "cache files, and query information from them\n"
1768 "\n"
1769 "Commands:\n"
bac2e715 1770 " add - Add a package file to the source cache\n"
b2e465d6
AL
1771 " gencaches - Build both the package and source cache\n"
1772 " showpkg - Show some general information for a single package\n"
2d425135 1773 " showsrc - Show source records\n"
b2e465d6
AL
1774 " stats - Show some basic statistics\n"
1775 " dump - Show the entire file in a terse form\n"
1776 " dumpavail - Print an available file to stdout\n"
1777 " unmet - Show unmet dependencies\n"
b2e465d6
AL
1778 " search - Search the package list for a regex pattern\n"
1779 " show - Show a readable record for the package\n"
1780 " depends - Show raw dependency information for a package\n"
eba2b51d 1781 " rdepends - Show reverse dependency information for a package\n"
2c120e24
EL
1782 " pkgnames - List the names of all packages in the system\n"
1783 " dotty - Generate package graphs for GraphViz\n"
fff4b7f3 1784 " xvcg - Generate package graphs for xvcg\n"
eba05d54 1785 " policy - Show policy settings\n"
b2e465d6
AL
1786 "\n"
1787 "Options:\n"
1788 " -h This help text.\n"
1789 " -p=? The package cache.\n"
1790 " -s=? The source cache.\n"
1791 " -q Disable progress indicator.\n"
1792 " -i Show only important deps for the unmet command.\n"
1793 " -c=? Read this configuration file\n"
a2884e32 1794 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
1795 "See the apt-cache(8) and apt.conf(5) manual pages for more information.\n");
1796 return true;
e1b74f61
AL
1797}
1798 /*}}}*/
0a8e3465
AL
1799// CacheInitialize - Initialize things for apt-cache /*{{{*/
1800// ---------------------------------------------------------------------
1801/* */
1802void CacheInitialize()
1803{
1804 _config->Set("quiet",0);
1805 _config->Set("help",false);
1806}
1807 /*}}}*/
92fcbfc1 1808int main(int argc,const char *argv[]) /*{{{*/
1164783d 1809{
08e8f724
AL
1810 CommandLine::Args Args[] = {
1811 {'h',"help","help",0},
04aa15a8 1812 {'v',"version","version",0},
e1b74f61
AL
1813 {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
1814 {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
1815 {'q',"quiet","quiet",CommandLine::IntLevel},
76fbce56 1816 {'i',"important","APT::Cache::Important",0},
7e2e2d5d 1817 {'f',"full","APT::Cache::ShowFull",0},
b2e465d6 1818 {'g',"generate","APT::Cache::Generate",0},
648e3cb4 1819 {'a',"all-versions","APT::Cache::AllVersions",0},
fe6fc1c2
AL
1820 {'n',"names-only","APT::Cache::NamesOnly",0},
1821 {0,"all-names","APT::Cache::AllNames",0},
b2e465d6 1822 {0,"recurse","APT::Cache::RecurseDepends",0},
e1b74f61
AL
1823 {'c',"config-file",0,CommandLine::ConfigFile},
1824 {'o',"option",0,CommandLine::ArbItem},
fe6fc1c2 1825 {0,"installed","APT::Cache::Installed",0},
08e8f724 1826 {0,0,0,0}};
b0b4efb9
AL
1827 CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp},
1828 {"add",&DoAdd},
1829 {"gencaches",&GenCaches},
f8f410f5 1830 {"showsrc",&ShowSrcPackage},
b0b4efb9
AL
1831 {0,0}};
1832 CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage},
1833 {"stats",&Stats},
1834 {"dump",&Dump},
1835 {"dumpavail",&DumpAvail},
1836 {"unmet",&UnMet},
9dbb421f 1837 {"search",&Search},
349cd3b8 1838 {"depends",&Depends},
eba2b51d 1839 {"rdepends",&RDepends},
3e94da1b 1840 {"dotty",&Dotty},
fff4b7f3 1841 {"xvcg",&XVcg},
7e2e2d5d 1842 {"show",&ShowPackage},
7c1133fe 1843 {"pkgnames",&ShowPkgNames},
af87ab54 1844 {"policy",&Policy},
bd3d53ef 1845 {"madison",&Madison},
b0b4efb9 1846 {0,0}};
0a8e3465
AL
1847
1848 CacheInitialize();
67111687
AL
1849
1850 // Set up gettext support
1851 setlocale(LC_ALL,"");
1852 textdomain(PACKAGE);
1853
e1b74f61
AL
1854 // Parse the command line and initialize the package library
1855 CommandLine CmdL(Args,_config);
b2e465d6
AL
1856 if (pkgInitConfig(*_config) == false ||
1857 CmdL.Parse(argc,argv) == false ||
1858 pkgInitSystem(*_config,_system) == false)
08e8f724
AL
1859 {
1860 _error->DumpErrors();
1861 return 100;
1164783d 1862 }
8efa2a3b 1863
e1b74f61
AL
1864 // See if the help should be shown
1865 if (_config->FindB("help") == true ||
1866 CmdL.FileSize() == 0)
b2e465d6
AL
1867 {
1868 ShowHelp(CmdL);
1869 return 0;
1870 }
1871
a9a5908d 1872 // Deal with stdout not being a tty
a3f6ea20 1873 if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
a9a5908d
AL
1874 _config->Set("quiet","1");
1875
b0b4efb9 1876 if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false)
4b1b89c5 1877 {
2a3f3893 1878 MMap *Map = 0;
b2e465d6 1879 if (_config->FindB("APT::Cache::Generate",true) == false)
4b1b89c5
AL
1880 {
1881 Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
1882 FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
1883 }
1884 else
1885 {
1886 // Open the cache file
af87ab54
AL
1887 SrcList = new pkgSourceList;
1888 SrcList->ReadMainList();
f8f410f5 1889
4b1b89c5
AL
1890 // Generate it and map it
1891 OpProgress Prog;
af87ab54 1892 pkgMakeStatusCache(*SrcList,Prog,&Map,true);
4b1b89c5
AL
1893 }
1894
b0b4efb9 1895 if (_error->PendingError() == false)
1164783d 1896 {
b2e465d6 1897 pkgCache Cache(Map);
b0b4efb9
AL
1898 GCache = &Cache;
1899 if (_error->PendingError() == false)
1900 CmdL.DispatchArg(CmdsB);
803fafcb
AL
1901 }
1902 delete Map;
1164783d
AL
1903 }
1904
1905 // Print any errors or warnings found during parsing
1906 if (_error->empty() == false)
1907 {
0a8e3465 1908 bool Errors = _error->PendingError();
1164783d 1909 _error->DumpErrors();
0a8e3465 1910 return Errors == true?100:0;
1164783d
AL
1911 }
1912
1913 return 0;
1914}
92fcbfc1 1915 /*}}}*/