summaryrefslogtreecommitdiff
path: root/src/cmdline/cmdline_search.cc
blob: e76376c9ab62049b67814ef26796899d615e8ac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// cmdline_search.cc
//
//   Copyright 2004 Daniel Burrows

#include "cmdline_search.h"

#include "cmdline_common.h"

#include <aptitude.h>
#include <load_sortpolicy.h>
#include <pkg_columnizer.h>
#include <pkg_item.h>
#include <pkg_sortpolicy.h>

#include <generic/apt/apt.h>
#include <generic/apt/matchers.h>

#include <cwidget/config/column_definition.h>
#include <cwidget/generic/util/transcode.h>

#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>

#include <algorithm>

using namespace std;
namespace cw = cwidget;
using cwidget::util::transcode;
using namespace aptitude::matching;
using namespace cwidget::config;

class search_result_parameters : public column_parameters
{
  pkg_match_result *r;
public:
  search_result_parameters(pkg_match_result *_r)
    :r(_r)
  {
  }

  ~search_result_parameters()
  {
    delete r;
  }

  int param_count()
  {
    return r->num_groups();
  }

  wstring get_param(int n)
  {
    return cw::util::transcode(r->group(n));
  }
};

// compare...uses the candidate version of each package.
class compare
{
  pkg_sortpolicy *s;
public:
  compare(pkg_sortpolicy *_s):s(_s) {}

  bool operator()(const pair<pkgCache::PkgIterator, pkg_match_result *> &a,
		  const pair<pkgCache::PkgIterator, pkg_match_result *> &b)
  {
    pkgCache::VerIterator av=(*apt_cache_file)[a.first].CandidateVerIter(*apt_cache_file);
    pkgCache::VerIterator bv=(*apt_cache_file)[b.first].CandidateVerIter(*apt_cache_file);

    return s->compare(a.first, av, b.first, bv)<0;
  }
};

/** \brief operator== for match results. */
class result_equality
{
  pkg_sortpolicy *s;
public:
  result_equality(pkg_sortpolicy *_s):s(_s) {}

  bool operator()(const pair<pkgCache::PkgIterator, pkg_match_result *> &a,
		  const pair<pkgCache::PkgIterator, pkg_match_result *> &b)
  {
    pkgCache::VerIterator av =
      (*apt_cache_file)[a.first].CandidateVerIter(*apt_cache_file);
    pkgCache::VerIterator bv =
      (*apt_cache_file)[b.first].CandidateVerIter(*apt_cache_file);

    return s->compare(a.first, av, b.first, bv) == 0;
  }
};

// FIXME: apt-cache does lots of tricks to make this fast.  Should I?
int cmdline_search(int argc, char *argv[], const char *status_fname,
		   string display_format, string width, string sort,
		   bool disable_columns)
{
  int real_width=-1;

  pkg_item::pkg_columnizer::setup_columns();

  pkg_sortpolicy *s=parse_sortpolicy(sort);

  if(!s)
    {
      _error->DumpErrors();
      return -1;
    }

  _error->DumpErrors();

  if(!width.empty())
    {
      unsigned long tmp=screen_width;
      StrToNum(width.c_str(), tmp, width.size());
      real_width=tmp;
    }

  wstring wdisplay_format;

  if(!cw::util::transcode(display_format.c_str(), wdisplay_format))
    {
      _error->DumpErrors();
      fprintf(stderr, _("iconv of %s failed.\n"), display_format.c_str());
      return -1;
    }

  column_definition_list *columns =
    parse_columns(wdisplay_format,
		  pkg_item::pkg_columnizer::parse_column_type,
		  pkg_item::pkg_columnizer::defaults);

  if(!columns)
    {
      _error->DumpErrors();
      return -1;
    }

  if(argc<=1)
    {
      fprintf(stderr, _("search: You must provide at least one search term\n"));
      delete columns;
      return -1;
    }

  OpProgress progress;

  apt_init(&progress, true, status_fname);

  if(_error->PendingError())
    {
      _error->DumpErrors();
      delete columns;
      return -1;
    }

  vector<pkg_matcher *> matchers;

  for(int i=1; i<argc; ++i)
    {
      pkg_matcher *m=parse_pattern(argv[i]);
      if(!m)
	{
	  while(!matchers.empty())
	    {
	      delete matchers.back();
	      matchers.pop_back();
	    }

	  _error->DumpErrors();

	  delete columns;
	  return -1;
	}

      matchers.push_back(m);
    }

  vector<pair<pkgCache::PkgIterator, pkg_match_result *> > output;
  for(pkgCache::PkgIterator pkg=(*apt_cache_file)->PkgBegin();
      !pkg.end(); ++pkg)
    {
      // Ignore packages that exist only due to dependencies.
      if(pkg.VersionList().end() && pkg.ProvidesList().end())
	continue;

      for(vector<pkg_matcher *>::iterator m=matchers.begin();
	  m!=matchers.end(); ++m)
	{
	  pkg_match_result *r = get_match(*m, pkg,
					  *apt_cache_file,
					  *apt_package_records);

	  if(r != NULL)
	    output.push_back(pair<pkgCache::PkgIterator, pkg_match_result *>(pkg, r));
	}
    }

  std::sort(output.begin(), output.end(), compare(s));
  output.erase(std::unique(output.begin(), output.end(), result_equality(s)),
	       output.end());

  for(vector<pair<pkgCache::PkgIterator, pkg_match_result *> >::iterator i=output.begin();
      i!=output.end(); ++i)
    {
      column_parameters *p =
	new search_result_parameters(i->second);
      pkg_item::pkg_columnizer columnizer(i->first,
					  i->first.VersionList(),
					  *columns,
					  0);
      if(disable_columns)
	{
	  // Instantiate the format string without clipping or
	  // expanding columns.
	  //
	  // TODO: this should move into cwidget in the future, as a new
	  // mode of operation for layout_columns().
	  std::wstring output;

	  for(column_definition_list::iterator it = columns->begin();
	      it != columns->end();
	      ++it)
	    {
	      if(it->type == column_definition::COLUMN_LITERAL)
		output += it->arg;
	      else
		{
		  eassert(it->type == column_definition::COLUMN_GENERATED ||
			  it->type == column_definition::COLUMN_PARAM);

		  if(it->type == column_definition::COLUMN_GENERATED)
		    {
		      cwidget::column_disposition disp = columnizer.setup_column(it->ival);
		      output += disp.text;
		    }
		  else
		    {
		      if(p->param_count() <= it->ival)
			output += L"###";
		      else
			output += p->get_param(it->ival);
		    }
		}
	    }

	  printf("%ls\n", output.c_str());
	}
      else
	printf("%ls\n",
	       columnizer.layout_columns(real_width==-1?screen_width:real_width,
					 *p).c_str());

      // Note that this deletes the whole result, so we can't re-use
      // the list.
      delete p;
    }

  delete columns;

  return 0;
}