+ pkgCacheFile CacheFile;
+ pkgCache *Cache = CacheFile.GetPkgCache();
+ pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
+ if (unlikely(Cache == NULL || Plcy == NULL))
+ return false;
+
+ // Make sure there is at least one argument
+ if (NumPatterns < 1)
+ return _error->Error(_("You must give at least one search pattern"));
+
+ // Compile the regex pattern
+ regex_t *Patterns = new regex_t[NumPatterns];
+ memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
+ for (unsigned I = 0; I != NumPatterns; I++)
+ {
+ if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
+ REG_NOSUB) != 0)
+ {
+ for (; I != 0; I--)
+ regfree(&Patterns[I]);
+ return _error->Error("Regex compilation error");
+ }
+ }
+
+ if (_error->PendingError() == true)
+ {
+ for (unsigned I = 0; I != NumPatterns; I++)
+ regfree(&Patterns[I]);
+ return false;
+ }
+
+ size_t const descCount = Cache->HeaderP->GroupCount + 1;
+ ExDescFile *DFList = new ExDescFile[descCount];
+ memset(DFList,0,sizeof(*DFList) * descCount);
+
+ bool PatternMatch[descCount * NumPatterns];
+ memset(PatternMatch,false,sizeof(PatternMatch));
+
+ // Map versions that we want to write out onto the VerList array.
+ for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() == false; ++G)
+ {
+ size_t const PatternOffset = G->ID * NumPatterns;
+ size_t unmatched = 0, matched = 0;
+ for (unsigned I = 0; I < NumPatterns; ++I)
+ {
+ if (PatternMatch[PatternOffset + I] == true)
+ ++matched;
+ else if (regexec(&Patterns[I],G.Name(),0,0,0) == 0)
+ PatternMatch[PatternOffset + I] = true;
+ else
+ ++unmatched;
+ }
+
+ // already dealt with this package?
+ if (matched == NumPatterns)
+ continue;
+
+ // Doing names only, drop any that don't match..
+ if (NamesOnly == true && unmatched == NumPatterns)
+ continue;
+
+ // Find the proper version to use
+ pkgCache::PkgIterator P = G.FindPreferredPkg();
+ if (P.end() == true)
+ continue;
+ pkgCache::VerIterator V = Plcy->GetCandidateVer(P);
+ if (V.end() == false)
+ {
+ pkgCache::DescIterator const D = V.TranslatedDescription();
+ //FIXME: packages without a description can't be found
+ if (D.end() == true)
+ continue;
+ DFList[G->ID].Df = D.FileList();
+ DFList[G->ID].ID = G->ID;
+ }
+
+ if (unmatched == NumPatterns)
+ continue;
+
+ // Include all the packages that provide matching names too
+ for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; ++Prv)
+ {
+ pkgCache::VerIterator V = Plcy->GetCandidateVer(Prv.OwnerPkg());
+ if (V.end() == true)
+ continue;
+
+ unsigned long id = Prv.OwnerPkg().Group()->ID;
+ pkgCache::DescIterator const D = V.TranslatedDescription();
+ //FIXME: packages without a description can't be found
+ if (D.end() == true)
+ continue;
+ DFList[id].Df = D.FileList();
+ DFList[id].ID = id;
+
+ size_t const PrvPatternOffset = id * NumPatterns;
+ for (unsigned I = 0; I < NumPatterns; ++I)
+ PatternMatch[PrvPatternOffset + I] = PatternMatch[PatternOffset + I];
+ }
+ }
+
+ LocalitySort(&DFList->Df,Cache->HeaderP->GroupCount,sizeof(*DFList));
+
+ // Create the text record parser
+ pkgRecords Recs(*Cache);
+ // Iterate over all the version records and check them
+ for (ExDescFile *J = DFList; J->Df != 0; ++J)
+ {
+ pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(*Cache,J->Df));
+ size_t const PatternOffset = J->ID * NumPatterns;
+
+ if (NamesOnly == false)
+ {
+ string const LongDesc = P.LongDesc();
+ for (unsigned I = 0; I < NumPatterns; ++I)
+ {
+ if (PatternMatch[PatternOffset + I] == true)
+ continue;
+ else if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
+ PatternMatch[PatternOffset + I] = true;
+ }
+ }
+
+ bool matchedAll = true;
+ for (unsigned I = 0; I < NumPatterns; ++I)
+ if (PatternMatch[PatternOffset + I] == false)
+ {
+ matchedAll = false;
+ break;
+ }
+
+ if (matchedAll == true)
+ {
+ if (ShowFull == true)
+ {
+ const char *Start;
+ const char *End;
+ P.GetRec(Start,End);
+ fwrite(Start,End-Start,1,stdout);
+ putc('\n',stdout);
+ }
+ else
+ printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
+ }
+ }
+
+ delete [] DFList;
+ for (unsigned I = 0; I != NumPatterns; I++)
+ regfree(&Patterns[I]);
+ if (ferror(stdout))
+ return _error->Error("Write to stdout failed");
+ return true;
+}
+ /*}}}*/
+/* ShowAuto - show automatically installed packages (sorted) {{{*/
+bool ShowAuto(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ pkgCache *Cache = CacheFile.GetPkgCache();
+ pkgDepCache *DepCache = CacheFile.GetDepCache();
+ if (unlikely(Cache == NULL || DepCache == NULL))
+ return false;
+
+ std::vector<string> packages;
+ packages.reserve(Cache->HeaderP->PackageCount / 3);
+
+ for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
+ if ((*DepCache)[P].Flags & pkgCache::Flag::Auto)
+ packages.push_back(P.Name());
+
+ std::sort(packages.begin(), packages.end());
+
+ for (vector<string>::iterator I = packages.begin(); I != packages.end(); ++I)
+ cout << *I << "\n";
+
+ _error->Notice(_("This command is deprecated. Please use 'apt-mark showauto' instead."));
+ return true;
+}
+ /*}}}*/
+// ShowPackage - Dump the package record to the screen /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool ShowPackage(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
+ APT::VersionList::Version const select = _config->FindB("APT::Cache::AllVersions", true) ?
+ APT::VersionList::ALL : APT::VersionList::CANDIDATE;
+ APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
+ for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
+ if (DisplayRecord(CacheFile, Ver) == false)
+ return false;
+
+ if (verset.empty() == true)
+ {
+ if (helper.virtualPkgs.empty() == true)
+ return _error->Error(_("No packages found"));
+ else
+ _error->Notice(_("No packages found"));
+ }
+ return true;
+}
+ /*}}}*/
+// ShowPkgNames - Show package names /*{{{*/
+// ---------------------------------------------------------------------
+/* This does a prefix match on the first argument */
+bool ShowPkgNames(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ if (unlikely(CacheFile.BuildCaches(NULL, false) == false))
+ return false;
+ pkgCache::GrpIterator I = CacheFile.GetPkgCache()->GrpBegin();
+ bool const All = _config->FindB("APT::Cache::AllNames","false");
+
+ if (CmdL.FileList[1] != 0)
+ {
+ for (;I.end() != true; ++I)
+ {
+ if (All == false && I->FirstPackage == 0)
+ continue;
+ if (I.FindPkg("any")->VersionList == 0)
+ continue;
+ if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
+ cout << I.Name() << endl;
+ }
+
+ return true;
+ }
+
+ // Show all pkgs
+ for (;I.end() != true; ++I)
+ {
+ if (All == false && I->FirstPackage == 0)
+ continue;
+ if (I.FindPkg("any")->VersionList == 0)
+ continue;
+ cout << I.Name() << endl;
+ }
+
+ return true;
+}
+ /*}}}*/
+// ShowSrcPackage - Show source package records /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool ShowSrcPackage(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ pkgSourceList *List = CacheFile.GetSourceList();
+ if (unlikely(List == NULL))
+ return false;
+
+ // Create the text record parsers
+ pkgSrcRecords SrcRecs(*List);
+ if (_error->PendingError() == true)
+ return false;
+
+ unsigned found = 0;
+ for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+ {
+ SrcRecs.Restart();
+
+ pkgSrcRecords::Parser *Parse;
+ unsigned found_this = 0;
+ while ((Parse = SrcRecs.Find(*I,false)) != 0) {
+ cout << Parse->AsStr() << endl;;
+ found++;
+ found_this++;
+ }
+ if (found_this == 0) {
+ _error->Warning(_("Unable to locate package %s"),*I);
+ continue;
+ }
+ }
+ if (found == 0)
+ _error->Notice(_("No packages found"));
+ return true;
+}
+ /*}}}*/
+// Policy - Show the results of the preferences file /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool Policy(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ pkgCache *Cache = CacheFile.GetPkgCache();
+ pkgPolicy *Plcy = CacheFile.GetPolicy();
+ pkgSourceList *SrcList = CacheFile.GetSourceList();
+ if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
+ return false;
+
+ /* Should the MultiArchKiller be run to see which pseudo packages for an
+ arch all package are currently installed? Activating it gives a speed
+ penality for no real gain beside enhanced debugging, so in general no. */
+ if (_config->FindB("APT::Cache::Policy::DepCache", false) == true)
+ CacheFile.GetDepCache();
+
+ // Print out all of the package files
+ if (CmdL.FileList[1] == 0)
+ {
+ cout << _("Package files:") << endl;
+ for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
+ {
+ // Locate the associated index files so we can derive a description
+ pkgIndexFile *Indx;
+ if (SrcList->FindIndex(F,Indx) == false &&
+ _system->FindIndex(F,Indx) == false)
+ return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
+
+ printf("%4i %s\n",
+ Plcy->GetPriority(F),Indx->Describe(true).c_str());
+
+ // Print the reference information for the package
+ string Str = F.RelStr();
+ if (Str.empty() == false)
+ printf(" release %s\n",F.RelStr().c_str());
+ if (F.Site() != 0 && F.Site()[0] != 0)
+ printf(" origin %s\n",F.Site());
+ }
+
+ // Show any packages have explicit pins
+ cout << _("Pinned packages:") << endl;
+ pkgCache::PkgIterator I = Cache->PkgBegin();
+ for (;I.end() != true; ++I)
+ {
+ if (Plcy->GetPriority(I) == 0)
+ continue;
+
+ // Print the package name and the version we are forcing to
+ cout << " " << I.FullName(true) << " -> ";
+
+ pkgCache::VerIterator V = Plcy->GetMatch(I);
+ if (V.end() == true)
+ cout << _("(not found)") << endl;
+ else
+ cout << V.VerStr() << endl;
+ }
+
+ return true;
+ }
+
+ char const * const msgInstalled = _(" Installed: ");
+ char const * const msgCandidate = _(" Candidate: ");
+ short const InstalledLessCandidate =
+ mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
+ short const deepInstalled =
+ (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
+ short const deepCandidate =
+ (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
+
+ // Print out detailed information for each package
+ APT::CacheSetHelper helper(true, GlobalError::NOTICE);
+ APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
+ for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
+ {
+ cout << Pkg.FullName(true) << ":" << endl;
+
+ // Installed version
+ cout << msgInstalled << OutputInDepth(deepInstalled, " ");
+ if (Pkg->CurrentVer == 0)
+ cout << _("(none)") << endl;
+ else
+ cout << Pkg.CurrentVer().VerStr() << endl;
+
+ // Candidate Version
+ cout << msgCandidate << OutputInDepth(deepCandidate, " ");
+ pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
+ if (V.end() == true)
+ cout << _("(none)") << endl;
+ else
+ cout << V.VerStr() << endl;
+
+ // Pinned version
+ if (Plcy->GetPriority(Pkg) != 0)
+ {
+ cout << _(" Package pin: ");
+ V = Plcy->GetMatch(Pkg);
+ if (V.end() == true)
+ cout << _("(not found)") << endl;
+ else
+ cout << V.VerStr() << endl;
+ }
+
+ // Show the priority tables
+ cout << _(" Version table:") << endl;
+ for (V = Pkg.VersionList(); V.end() == false; ++V)
+ {
+ if (Pkg.CurrentVer() == V)
+ cout << " *** " << V.VerStr();
+ else
+ cout << " " << V.VerStr();
+ cout << " " << Plcy->GetPriority(Pkg) << endl;
+ for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
+ {
+ // Locate the associated index files so we can derive a description
+ pkgIndexFile *Indx;
+ if (SrcList->FindIndex(VF.File(),Indx) == false &&
+ _system->FindIndex(VF.File(),Indx) == false)
+ return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
+ printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
+ Indx->Describe(true).c_str());
+ }
+ }
+ }
+
+ return true;
+}
+ /*}}}*/
+// Madison - Look a bit like katie's madison /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool Madison(CommandLine &CmdL)
+{
+ pkgCacheFile CacheFile;
+ pkgSourceList *SrcList = CacheFile.GetSourceList();
+
+ if (SrcList == 0)
+ return false;
+
+ // Create the src text record parsers and ignore errors about missing
+ // deb-src lines that are generated from pkgSrcRecords::pkgSrcRecords
+ pkgSrcRecords SrcRecs(*SrcList);
+ if (_error->PendingError() == true)
+ _error->Discard();
+
+ APT::CacheSetHelper helper(true, GlobalError::NOTICE);
+ for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+ {
+ _error->PushToStack();
+ APT::PackageList pkgset = APT::PackageList::FromString(CacheFile, *I, helper);
+ for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
+ {
+ for (pkgCache::VerIterator V = Pkg.VersionList(); V.end() == false; ++V)
+ {
+ for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
+ {
+// This might be nice, but wouldn't uniquely identify the source -mdz
+// if (VF.File().Archive() != 0)
+// {
+// cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
+// << VF.File().Archive() << endl;
+// }
+
+ // Locate the associated index files so we can derive a description
+ for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
+ {
+ vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
+ for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
+ IF != Indexes->end(); ++IF)
+ {
+ if ((*IF)->FindInCache(*(VF.File().Cache())) == VF.File())
+ {
+ cout << setw(10) << Pkg.FullName(true) << " | " << setw(10) << V.VerStr() << " | "
+ << (*IF)->Describe(true) << endl;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ SrcRecs.Restart();
+ pkgSrcRecords::Parser *SrcParser;
+ bool foundSomething = false;
+ while ((SrcParser = SrcRecs.Find(*I, false)) != 0)
+ {
+ foundSomething = true;
+ // Maybe support Release info here too eventually
+ cout << setw(10) << SrcParser->Package() << " | "
+ << setw(10) << SrcParser->Version() << " | "
+ << SrcParser->Index().Describe(true) << endl;
+ }
+ if (foundSomething == true)
+ _error->RevertToStack();
+ else
+ _error->MergeWithStack();
+ }
+
+ return true;
+}
+ /*}}}*/
+// GenCaches - Call the main cache generator /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool GenCaches(CommandLine &Cmd)
+{
+ OpTextProgress Progress(*_config);
+
+ pkgCacheFile CacheFile;
+ return CacheFile.BuildCaches(&Progress, true);
+}
+ /*}}}*/
+// ShowHelp - Show a help screen /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool ShowHelp(CommandLine &Cmd)
+{
+ ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
+ COMMON_ARCH,__DATE__,__TIME__);
+
+ if (_config->FindB("version") == true)
+ return true;
+
+ cout <<
+ _("Usage: apt-cache [options] command\n"
+ " apt-cache [options] showpkg pkg1 [pkg2 ...]\n"
+ " apt-cache [options] showsrc pkg1 [pkg2 ...]\n"
+ "\n"
+ "apt-cache is a low-level tool used to query information\n"
+ "from APT's binary cache files\n"
+ "\n"
+ "Commands:\n"
+ " gencaches - Build both the package and source cache\n"
+ " showpkg - Show some general information for a single package\n"
+ " showsrc - Show source records\n"
+ " stats - Show some basic statistics\n"
+ " dump - Show the entire file in a terse form\n"
+ " dumpavail - Print an available file to stdout\n"
+ " unmet - Show unmet dependencies\n"
+ " search - Search the package list for a regex pattern\n"
+ " show - Show a readable record for the package\n"
+ " depends - Show raw dependency information for a package\n"
+ " rdepends - Show reverse dependency information for a package\n"
+ " pkgnames - List the names of all packages in the system\n"
+ " dotty - Generate package graphs for GraphViz\n"
+ " xvcg - Generate package graphs for xvcg\n"
+ " policy - Show policy settings\n"
+ "\n"
+ "Options:\n"
+ " -h This help text.\n"
+ " -p=? The package cache.\n"
+ " -s=? The source cache.\n"
+ " -q Disable progress indicator.\n"
+ " -i Show only important deps for the unmet command.\n"
+ " -c=? Read this configuration file\n"
+ " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
+ "See the apt-cache(8) and apt.conf(5) manual pages for more information.\n");
+ return true;
+}
+ /*}}}*/
+int main(int argc,const char *argv[]) /*{{{*/
+{
+ CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
+ {"gencaches",&GenCaches},
+ {"showsrc",&ShowSrcPackage},
+ {"showpkg",&DumpPackage},
+ {"stats",&Stats},
+ {"dump",&Dump},
+ {"dumpavail",&DumpAvail},
+ {"unmet",&UnMet},
+ {"search",&Search},
+ {"depends",&Depends},
+ {"rdepends",&RDepends},
+ {"dotty",&Dotty},
+ {"xvcg",&XVcg},
+ {"show",&ShowPackage},
+ {"pkgnames",&ShowPkgNames},
+ {"showauto",&ShowAuto},
+ {"policy",&Policy},
+ {"madison",&Madison},
+ {0,0}};
+
+ std::vector<CommandLine::Args> Args = getCommandArgs("apt-cache", CommandLine::GetCommand(Cmds, argc, argv));
+
+ // Set up gettext support
+ setlocale(LC_ALL,"");
+ textdomain(PACKAGE);
+
+ // Parse the command line and initialize the package library
+ CommandLine CmdL(Args.data(),_config);
+ if (pkgInitConfig(*_config) == false ||
+ CmdL.Parse(argc,argv) == false ||
+ pkgInitSystem(*_config,_system) == false)