implement --full in apt search
[ntk/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
20 #include <string.h>
21 #include <iostream>
22 #include <sstream>
23 #include <map>
24 #include <string>
25 #include <utility>
26
27 #include <apti18n.h>
28 /*}}}*/
29
30 bool FullTextSearch(CommandLine &CmdL) /*{{{*/
31 {
32 pkgCacheFile CacheFile;
33 pkgCache *Cache = CacheFile.GetPkgCache();
34 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
35 if (unlikely(Cache == NULL || Plcy == NULL))
36 return false;
37
38 // Make sure there is at least one argument
39 unsigned int const NumPatterns = CmdL.FileSize() -1;
40 if (NumPatterns < 1)
41 return _error->Error(_("You must give at least one search pattern"));
42
43 #define APT_FREE_PATTERNS() for (std::vector<regex_t>::iterator P = Patterns.begin(); \
44 P != Patterns.end(); ++P) { regfree(&(*P)); }
45
46 // Compile the regex pattern
47 std::vector<regex_t> Patterns;
48 for (unsigned int I = 0; I != NumPatterns; ++I)
49 {
50 regex_t pattern;
51 if (regcomp(&pattern, CmdL.FileList[I + 1], REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
52 {
53 APT_FREE_PATTERNS();
54 return _error->Error("Regex compilation error");
55 }
56 Patterns.push_back(pattern);
57 }
58
59 bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly", false);
60
61 std::map<std::string, std::string> output_map;
62
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();
68
69 progress.OverallProgress(50, 100, 50, _("Full Text Search"));
70 progress.SubProgress(bag.size());
71 pkgRecords records(CacheFile);
72
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";
76 else
77 format += " ${LongDescription}\n";
78
79 int Done = 0;
80 for ( ;V != bag.end(); ++V)
81 {
82 if (Done%500 == 0)
83 progress.Progress(Done);
84 ++Done;
85
86 // we want to list each package only once
87 char const * const PkgName = V.ParentPkg().Name();
88 if (output_map.find(PkgName) != output_map.end())
89 continue;
90
91 pkgCache::DescIterator Desc = V.TranslatedDescription();
92 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
93 std::string const LongDesc = parser.LongDesc();
94
95 bool all_found = true;
96 for (std::vector<regex_t>::const_iterator pattern = Patterns.begin();
97 pattern != Patterns.end(); ++pattern)
98 {
99 if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0)
100 continue;
101 else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0)
102 continue;
103 // search patterns are AND, so one failing fails all
104 all_found = false;
105 break;
106 }
107 if (all_found == true)
108 {
109 std::stringstream outs;
110 ListSingleVersion(CacheFile, records, V, outs, format);
111 output_map.insert(std::make_pair<std::string, std::string>(
112 PkgName, outs.str()));
113 }
114 }
115 APT_FREE_PATTERNS();
116 progress.Done();
117
118 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
119 // output the sorted map
120 std::map<std::string, std::string>::const_iterator K;
121 for (K = output_map.begin(); K != output_map.end(); ++K)
122 std::cout << (*K).second << std::endl;
123
124 return true;
125 }
126 /*}}}*/