4 #include <apt-pkg/cachefile.h>
5 #include <apt-pkg/cacheset.h>
6 #include <apt-pkg/cmndline.h>
7 #include <apt-pkg/pkgrecords.h>
8 #include <apt-pkg/policy.h>
9 #include <apt-pkg/progress.h>
10 #include <apt-pkg/cacheiterators.h>
11 #include <apt-pkg/configuration.h>
12 #include <apt-pkg/depcache.h>
13 #include <apt-pkg/macros.h>
14 #include <apt-pkg/pkgcache.h>
16 #include <apt-private/private-cacheset.h>
17 #include <apt-private/private-output.h>
18 #include <apt-private/private-search.h>
30 bool FullTextSearch(CommandLine
&CmdL
) /*{{{*/
32 pkgCacheFile CacheFile
;
33 pkgCache
*Cache
= CacheFile
.GetPkgCache();
34 pkgDepCache::Policy
*Plcy
= CacheFile
.GetPolicy();
35 if (unlikely(Cache
== NULL
|| Plcy
== NULL
))
38 // Make sure there is at least one argument
39 unsigned int const NumPatterns
= CmdL
.FileSize() -1;
41 return _error
->Error(_("You must give at least one search pattern"));
43 #define APT_FREE_PATTERNS() for (std::vector<regex_t>::iterator P = Patterns.begin(); \
44 P != Patterns.end(); ++P) { regfree(&(*P)); }
46 // Compile the regex pattern
47 std::vector
<regex_t
> Patterns
;
48 for (unsigned int I
= 0; I
!= NumPatterns
; ++I
)
51 if (regcomp(&pattern
, CmdL
.FileList
[I
+ 1], REG_EXTENDED
| REG_ICASE
| REG_NOSUB
) != 0)
54 return _error
->Error("Regex compilation error");
56 Patterns
.push_back(pattern
);
59 bool const NamesOnly
= _config
->FindB("APT::Cache::NamesOnly", false);
61 std::map
<std::string
, std::string
> output_map
;
63 LocalitySortedVersionSet bag
;
64 OpTextProgress
progress(*_config
);
65 progress
.OverallProgress(0, 100, 50, _("Sorting"));
66 GetLocalitySortedVersionSet(CacheFile
, &bag
, &progress
);
67 LocalitySortedVersionSet::iterator V
= bag
.begin();
69 progress
.OverallProgress(50, 100, 50, _("Full Text Search"));
70 progress
.SubProgress(bag
.size());
71 pkgRecords
records(CacheFile
);
73 std::string format
= "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}\n";
74 if (_config
->FindB("APT::Cache::ShowFull",false) == false)
75 format
+= " ${Description}\n";
77 format
+= " ${LongDescription}\n";
80 std::vector
<bool> PkgsDone(Cache
->Head().PackageCount
, false);
81 for ( ;V
!= bag
.end(); ++V
)
84 progress
.Progress(Done
);
87 // we want to list each package only once
88 pkgCache::PkgIterator
const P
= V
.ParentPkg();
89 if (PkgsDone
[P
->ID
] == true)
92 char const * const PkgName
= P
.Name();
93 pkgCache::DescIterator Desc
= V
.TranslatedDescription();
94 pkgRecords::Parser
&parser
= records
.Lookup(Desc
.FileList());
95 std::string
const LongDesc
= parser
.LongDesc();
97 bool all_found
= true;
98 for (std::vector
<regex_t
>::const_iterator pattern
= Patterns
.begin();
99 pattern
!= Patterns
.end(); ++pattern
)
101 if (regexec(&(*pattern
), PkgName
, 0, 0, 0) == 0)
103 else if (NamesOnly
== false && regexec(&(*pattern
), LongDesc
.c_str(), 0, 0, 0) == 0)
105 // search patterns are AND, so one failing fails all
109 if (all_found
== true)
111 PkgsDone
[P
->ID
] = true;
112 std::stringstream outs
;
113 ListSingleVersion(CacheFile
, records
, V
, outs
, format
);
114 output_map
.insert(std::make_pair
<std::string
, std::string
>(
115 PkgName
, outs
.str()));
121 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
122 // output the sorted map
123 std::map
<std::string
, std::string
>::const_iterator K
;
124 for (K
= output_map
.begin(); K
!= output_map
.end(); ++K
)
125 std::cout
<< (*K
).second
<< std::endl
;