summaryrefslogtreecommitdiff
path: root/src/cmdline/cmdline_user_tag.cc
blob: 3cc296186f1f8c9a5c86b51f49671e64d7439947 (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
// cmdline_user_tag.cc
//
//   Copyright (C) 2008-2010 Daniel Burrows
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.


// Local includes:
#include "cmdline_user_tag.h"

#include "cmdline_util.h"
#include "terminal.h"
#include "text_progress.h"

#include <aptitude.h>

#include <apt-pkg/error.h>

#include <generic/apt/apt.h>
#include <generic/apt/aptcache.h>
#include <generic/apt/matching/match.h>
#include <generic/apt/matching/parse.h>
#include <generic/apt/matching/pattern.h>
#include <generic/util/util.h>

#include <stdio.h>
#include <string.h>

using aptitude::cmdline::create_terminal;
using aptitude::cmdline::make_text_progress;
using aptitude::cmdline::terminal_io;
using boost::shared_ptr;

namespace aptitude
{
  namespace cmdline
  {
    namespace
    {
      enum user_tag_action { action_add, action_remove };

      void do_user_tag(user_tag_action act,
		       const std::string &tag,
		       const pkgCache::PkgIterator &pkg,
		       int verbose)
      {
	switch(act)
	  {
	  case action_add:
	    if(verbose > 0)
				// Sometimes also "user-tag" is used!
	      printf(_("Adding user tag \"%s\" to the package \"%s\".\n"),
		     tag.c_str(), pkg.Name());

	    (*apt_cache_file)->attach_user_tag(pkg, tag, NULL);
	    break;
	  case action_remove:
	    if(verbose > 0)
	      printf(_("Removing user tag \"%s\" from the package \"%s\".\n"),
		     tag.c_str(), pkg.Name());

	    (*apt_cache_file)->detach_user_tag(pkg, tag, NULL);
	    break;
	  default:
	    fprintf(stderr, "Internal error: bad user tag action %d.", act);
	    break;
	  }
      }
    }

    int cmdline_user_tag(int argc, char *argv[], int quiet, int verbose)
    {
      const shared_ptr<terminal_io> term = create_terminal();

      user_tag_action action = (user_tag_action)-1;

      if(strcmp(argv[0], "add-user-tag") == 0)
	action = action_add;
      else if(strcmp(argv[0], "remove-user-tag") == 0)
	action = action_remove;
      else
	{
	  fprintf(stderr, "Internal error: cmdline_user_tag encountered an unknown command name \"%s\"\n",
		  argv[0]);
	  return -1;
	}

      if(argc < 3)
	{
	  fprintf(stderr,
		  _("%s: too few arguments; expected at least a tag name and a package.\n"),
		  argv[0]);
	  return -1;
	}

      _error->DumpErrors();

      OpProgress progress;

      apt_init(&progress, true);
      if(_error->PendingError())
	{
	  _error->DumpErrors();
	  return -1;
	}

      std::string tag(argv[1]);

      bool all_ok = true;
      for(int i = 2; i < argc; ++i)
	{
	  if(!aptitude::matching::is_pattern(argv[i]))
	    {
	      pkgCache::PkgIterator pkg = (*apt_cache_file)->FindPkg(argv[i]);
	      if(pkg.end())
		{
		  if(quiet == 0)
                    std::cerr << ssprintf(_("No such package \"%s\""), argv[i])
                              << std::endl;
		  all_ok = false;
		}
	      else
		do_user_tag(action, tag, pkg, verbose);
	    }
	  else
	    {
	      using namespace aptitude::matching;
	      using cwidget::util::ref_ptr;

	      ref_ptr<pattern> p(parse(argv[i]));

	      if(!p.valid())
		{
		  _error->DumpErrors();
		  all_ok = false;
		}
	      else
		{
		  pkg_results_list matches;
		  ref_ptr<search_cache> search_info(search_cache::create());
		  search(p, search_info,
			 matches,
			 *apt_cache_file,
			 *apt_package_records);

                  for(pkg_results_list::const_iterator it = matches.begin();
                      it != matches.end();
                      ++it)
		    do_user_tag(action, tag, it->first, verbose);
		}
	    }
	}

      shared_ptr<OpProgress> text_progress = make_text_progress(false, term, term, term);
      if(!(*apt_cache_file)->save_selection_list(*text_progress))
	return 1;

      if(!all_ok)
	return 2;

      return 0;
    }
  }
}