-// Search - Perform a search /*{{{*/
-// ---------------------------------------------------------------------
-/* This searches the package names and package descriptions for a pattern */
-struct ExDescFile
-{
- pkgCache::DescFile *Df;
- bool NameMatch;
-};
-
-// Search - Perform a search /*{{{*/
-// ---------------------------------------------------------------------
-/* This searches the package names and package descriptions for a pattern */
-bool Search(CommandLine &CmdL)
-{
- pkgCache &Cache = *GCache;
- bool ShowFull = _config->FindB("APT::Cache::ShowFull",false);
- bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
- unsigned NumPatterns = CmdL.FileSize() -1;
-
- pkgDepCache::Policy Plcy;
-
- // Make sure there is at least one argument
- if (NumPatterns < 1)
- return _error->Error(_("You must give exactly one 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");
- }
- }
-
- // Create the text record parser
- pkgRecords Recs(Cache);
- if (_error->PendingError() == true)
- {
- for (unsigned I = 0; I != NumPatterns; I++)
- regfree(&Patterns[I]);
- return false;
- }
-
- ExDescFile *DFList = new ExDescFile[Cache.HeaderP->PackageCount+1];
- memset(DFList,0,sizeof(*DFList)*Cache.HeaderP->PackageCount+1);
-
- // Map versions that we want to write out onto the VerList array.
- for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
- {
- DFList[P->ID].NameMatch = NumPatterns != 0;
- for (unsigned I = 0; I != NumPatterns; I++)
- {
- if (regexec(&Patterns[I],P.Name(),0,0,0) == 0)
- DFList[P->ID].NameMatch &= true;
- else
- DFList[P->ID].NameMatch = false;
- }
-
- // Doing names only, drop any that dont match..
- if (NamesOnly == true && DFList[P->ID].NameMatch == false)
- continue;
-
- // Find the proper version to use.
- pkgCache::VerIterator V = Plcy.GetCandidateVer(P);
- if (V.end() == false)
- DFList[P->ID].Df = V.DescriptionList().FileList();
- }
-
- // Include all the packages that provide matching names too
- for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
- {
- if (DFList[P->ID].NameMatch == false)
- continue;
-
- for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; Prv++)
- {
- pkgCache::VerIterator V = Plcy.GetCandidateVer(Prv.OwnerPkg());
- if (V.end() == false)
- {
- DFList[Prv.OwnerPkg()->ID].Df = V.DescriptionList().FileList();
- DFList[Prv.OwnerPkg()->ID].NameMatch = true;
- }
- }
- }
-
- LocalitySort(&DFList->Df,Cache.HeaderP->PackageCount,sizeof(*DFList));
-
- // 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));
-
- bool Match = true;
- if (J->NameMatch == false)
- {
- string LongDesc = P.LongDesc();
- Match = NumPatterns != 0;
- for (unsigned I = 0; I != NumPatterns; I++)
- {
- if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
- Match &= true;
- else
- Match = false;
- }
- }
-
- if (Match == 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;
-}
- /*}}}*/
-// ShowPackage - Dump the package record to the screen /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool ShowPackage(CommandLine &CmdL)
-{
- pkgCache &Cache = *GCache;
- pkgDepCache::Policy Plcy;
-
- unsigned found = 0;
-
- for (const char **I = CmdL.FileList + 1; *I != 0; I++)
- {
- pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
- if (Pkg.end() == true)
- {
- _error->Warning(_("Unable to locate package %s"),*I);
- continue;
- }
-
- ++found;
-
- // Find the proper version to use.
- if (_config->FindB("APT::Cache::AllVersions","true") == true)
- {
- pkgCache::VerIterator V;
- for (V = Pkg.VersionList(); V.end() == false; V++)
- {
- if (DisplayRecord(V) == false)
- return false;
- }
- }
- else
- {
- pkgCache::VerIterator V = Plcy.GetCandidateVer(Pkg);
- if (V.end() == true || V.FileList().end() == true)
- continue;
- if (DisplayRecord(V) == false)
- return false;
- }
- }
-
- if (found > 0)
- return true;
- return _error->Error(_("No packages found"));
-}
- /*}}}*/