]> git.saurik.com Git - apt.git/blob - apt-private/private-search.cc
4f2bc491343b14ceb290912e3a31fbf9eaa28d4f
[apt.git] / apt-private / private-search.cc
1 // Includes /*{{{*/
2 #include <config.h>
3
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>
15
16 #include <apt-private/private-cacheset.h>
17 #include <apt-private/private-output.h>
18 #include <apt-private/private-search.h>
19 #include <apt-private/private-show.h>
20
21 #include <string.h>
22 #include <iostream>
23 #include <sstream>
24 #include <map>
25 #include <string>
26 #include <utility>
27
28 #include <apti18n.h>
29 /*}}}*/
30
31 static bool FullTextSearch(CommandLine &CmdL) /*{{{*/
32 {
33 pkgCacheFile CacheFile;
34 pkgCache *Cache = CacheFile.GetPkgCache();
35 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
36 if (unlikely(Cache == NULL || Plcy == NULL))
37 return false;
38
39 // Make sure there is at least one argument
40 unsigned int const NumPatterns = CmdL.FileSize() -1;
41 if (NumPatterns < 1)
42 return _error->Error(_("You must give at least one search pattern"));
43
44 #define APT_FREE_PATTERNS() for (std::vector<regex_t>::iterator P = Patterns.begin(); \
45 P != Patterns.end(); ++P) { regfree(&(*P)); }
46
47 // Compile the regex pattern
48 std::vector<regex_t> Patterns;
49 for (unsigned int I = 0; I != NumPatterns; ++I)
50 {
51 regex_t pattern;
52 if (regcomp(&pattern, CmdL.FileList[I + 1], REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
53 {
54 APT_FREE_PATTERNS();
55 return _error->Error("Regex compilation error");
56 }
57 Patterns.push_back(pattern);
58 }
59
60 std::map<std::string, std::string> output_map;
61
62 LocalitySortedVersionSet bag;
63 OpTextProgress progress(*_config);
64 progress.OverallProgress(0, 100, 50, _("Sorting"));
65 GetLocalitySortedVersionSet(CacheFile, &bag, &progress);
66 LocalitySortedVersionSet::iterator V = bag.begin();
67
68 progress.OverallProgress(50, 100, 50, _("Full Text Search"));
69 progress.SubProgress(bag.size());
70 pkgRecords records(CacheFile);
71
72 std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}\n";
73 if (_config->FindB("APT::Cache::ShowFull",false) == false)
74 format += " ${Description}\n";
75 else
76 format += " ${LongDescription}\n";
77
78 bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly", false);
79 int Done = 0;
80 std::vector<bool> PkgsDone(Cache->Head().PackageCount, false);
81 for ( ;V != bag.end(); ++V)
82 {
83 if (Done%500 == 0)
84 progress.Progress(Done);
85 ++Done;
86
87 // we want to list each package only once
88 pkgCache::PkgIterator const P = V.ParentPkg();
89 if (PkgsDone[P->ID] == true)
90 continue;
91
92 char const * const PkgName = P.Name();
93 pkgCache::DescIterator Desc = V.TranslatedDescription();
94 std::string LongDesc = "";
95 if (!Desc.end()) {
96 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
97 LongDesc = parser.LongDesc();
98 }
99
100 bool all_found = true;
101 for (std::vector<regex_t>::const_iterator pattern = Patterns.begin();
102 pattern != Patterns.end(); ++pattern)
103 {
104 if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0)
105 continue;
106 else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0)
107 continue;
108 // search patterns are AND, so one failing fails all
109 all_found = false;
110 break;
111 }
112 if (all_found == true)
113 {
114 PkgsDone[P->ID] = true;
115 std::stringstream outs;
116 ListSingleVersion(CacheFile, records, V, outs, format);
117 output_map.insert(std::make_pair<std::string, std::string>(
118 PkgName, outs.str()));
119 }
120 }
121 APT_FREE_PATTERNS();
122 progress.Done();
123
124 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
125 // output the sorted map
126 std::map<std::string, std::string>::const_iterator K;
127 for (K = output_map.begin(); K != output_map.end(); ++K)
128 std::cout << (*K).second << std::endl;
129
130 return true;
131 }
132 /*}}}*/
133 // LocalitySort - Sort a version list by package file locality /*{{{*/
134 static int LocalityCompare(const void * const a, const void * const b)
135 {
136 pkgCache::VerFile const * const A = *(pkgCache::VerFile const * const * const)a;
137 pkgCache::VerFile const * const B = *(pkgCache::VerFile const * const * const)b;
138
139 if (A == 0 && B == 0)
140 return 0;
141 if (A == 0)
142 return 1;
143 if (B == 0)
144 return -1;
145
146 if (A->File == B->File)
147 return A->Offset - B->Offset;
148 return A->File - B->File;
149 }
150 void LocalitySort(pkgCache::VerFile ** const begin, unsigned long long const Count,size_t const Size)
151 {
152 qsort(begin,Count,Size,LocalityCompare);
153 }
154 static void LocalitySort(pkgCache::DescFile ** const begin, unsigned long long const Count,size_t const Size)
155 {
156 qsort(begin,Count,Size,LocalityCompare);
157 }
158 /*}}}*/
159 // Search - Perform a search /*{{{*/
160 // ---------------------------------------------------------------------
161 /* This searches the package names and package descriptions for a pattern */
162 struct ExDescFile
163 {
164 pkgCache::DescFile *Df;
165 pkgCache::VerIterator V;
166 map_id_t ID;
167 };
168 static bool Search(CommandLine &CmdL)
169 {
170 bool const ShowFull = _config->FindB("APT::Cache::ShowFull",false);
171 unsigned int const NumPatterns = CmdL.FileSize() -1;
172
173 pkgCacheFile CacheFile;
174 pkgCache *Cache = CacheFile.GetPkgCache();
175 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
176 if (unlikely(Cache == NULL || Plcy == NULL))
177 return false;
178
179 // Make sure there is at least one argument
180 if (NumPatterns < 1)
181 return _error->Error(_("You must give at least one search pattern"));
182
183 // Compile the regex pattern
184 regex_t *Patterns = new regex_t[NumPatterns];
185 memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
186 for (unsigned I = 0; I != NumPatterns; I++)
187 {
188 if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
189 REG_NOSUB) != 0)
190 {
191 for (; I != 0; I--)
192 regfree(&Patterns[I]);
193 return _error->Error("Regex compilation error");
194 }
195 }
196
197 if (_error->PendingError() == true)
198 {
199 for (unsigned I = 0; I != NumPatterns; I++)
200 regfree(&Patterns[I]);
201 return false;
202 }
203
204 size_t const descCount = Cache->HeaderP->GroupCount + 1;
205 ExDescFile *DFList = new ExDescFile[descCount];
206 memset(DFList,0,sizeof(*DFList) * descCount);
207
208 bool *PatternMatch = new bool[descCount * NumPatterns];
209 memset(PatternMatch,false,sizeof(*PatternMatch) * descCount * NumPatterns);
210
211 // Map versions that we want to write out onto the VerList array.
212 bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
213 for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() == false; ++G)
214 {
215 size_t const PatternOffset = G->ID * NumPatterns;
216 size_t unmatched = 0, matched = 0;
217 for (unsigned I = 0; I < NumPatterns; ++I)
218 {
219 if (PatternMatch[PatternOffset + I] == true)
220 ++matched;
221 else if (regexec(&Patterns[I],G.Name(),0,0,0) == 0)
222 PatternMatch[PatternOffset + I] = true;
223 else
224 ++unmatched;
225 }
226
227 // already dealt with this package?
228 if (matched == NumPatterns)
229 continue;
230
231 // Doing names only, drop any that don't match..
232 if (NamesOnly == true && unmatched == NumPatterns)
233 continue;
234
235 // Find the proper version to use
236 pkgCache::PkgIterator P = G.FindPreferredPkg();
237 if (P.end() == true)
238 continue;
239 pkgCache::VerIterator V = Plcy->GetCandidateVer(P);
240 if (V.end() == false)
241 {
242 pkgCache::DescIterator const D = V.TranslatedDescription();
243 //FIXME: packages without a description can't be found
244 if (D.end() == true)
245 continue;
246 DFList[G->ID].Df = D.FileList();
247 DFList[G->ID].V = V;
248 DFList[G->ID].ID = G->ID;
249 }
250
251 if (unmatched == NumPatterns)
252 continue;
253
254 // Include all the packages that provide matching names too
255 for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; ++Prv)
256 {
257 pkgCache::VerIterator V = Plcy->GetCandidateVer(Prv.OwnerPkg());
258 if (V.end() == true)
259 continue;
260
261 unsigned long id = Prv.OwnerPkg().Group()->ID;
262 pkgCache::DescIterator const D = V.TranslatedDescription();
263 //FIXME: packages without a description can't be found
264 if (D.end() == true)
265 continue;
266 DFList[id].Df = D.FileList();
267 DFList[id].V = V;
268 DFList[id].ID = id;
269
270 size_t const PrvPatternOffset = id * NumPatterns;
271 for (unsigned I = 0; I < NumPatterns; ++I)
272 PatternMatch[PrvPatternOffset + I] |= PatternMatch[PatternOffset + I];
273 }
274 }
275
276 LocalitySort(&DFList->Df, Cache->HeaderP->GroupCount, sizeof(*DFList));
277
278 // Create the text record parser
279 pkgRecords Recs(*Cache);
280 // Iterate over all the version records and check them
281 for (ExDescFile *J = DFList; J->Df != 0; ++J)
282 {
283 pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(*Cache,J->Df));
284 size_t const PatternOffset = J->ID * NumPatterns;
285
286 if (NamesOnly == false)
287 {
288 std::string const LongDesc = P.LongDesc();
289 for (unsigned I = 0; I < NumPatterns; ++I)
290 {
291 if (PatternMatch[PatternOffset + I] == true)
292 continue;
293 else if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
294 PatternMatch[PatternOffset + I] = true;
295 }
296 }
297
298 bool matchedAll = true;
299 for (unsigned I = 0; I < NumPatterns; ++I)
300 if (PatternMatch[PatternOffset + I] == false)
301 {
302 matchedAll = false;
303 break;
304 }
305
306 if (matchedAll == true)
307 {
308 if (ShowFull == true)
309 DisplayRecordV1(CacheFile, J->V, std::cout);
310 else
311 printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
312 }
313 }
314
315 delete [] DFList;
316 delete [] PatternMatch;
317 for (unsigned I = 0; I != NumPatterns; I++)
318 regfree(&Patterns[I]);
319 delete [] Patterns;
320 if (ferror(stdout))
321 return _error->Error("Write to stdout failed");
322 return true;
323 }
324 /*}}}*/
325 bool DoSearch(CommandLine &CmdL) /*{{{*/
326 {
327 int const ShowVersion = _config->FindI("APT::Cache::Search::Version", 1);
328 if (ShowVersion <= 1)
329 return Search(CmdL);
330 return FullTextSearch(CmdL);
331 }
332