Imported Upstream version 1.16.10
[services/dpkg.git] / src / select.c
1 /*
2  * dpkg - main program for package management
3  * select.c - by-hand (rather than dselect-based) package selection
4  *
5  * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
6  * Copyright © 2006,2008-2012 Guillem Jover <guillem@debian.org>
7  * Copyright © 2011 Linaro Limited
8  * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
9  *
10  * This is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include <config.h>
25 #include <compat.h>
26
27 #include <fnmatch.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include <dpkg/i18n.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/pkg-array.h>
37 #include <dpkg/pkg-show.h>
38 #include <dpkg/pkg-spec.h>
39 #include <dpkg/options.h>
40
41 #include "filesdb.h"
42 #include "infodb.h"
43 #include "main.h"
44
45 static void getsel1package(struct pkginfo *pkg) {
46   const char *pkgname;
47   int l;
48
49   if (pkg->want == want_unknown) return;
50   pkgname = pkg_name(pkg, pnaw_nonambig);
51   l = strlen(pkgname);
52   l >>= 3;
53   l = 6 - l;
54   if (l < 1)
55     l = 1;
56   printf("%s%.*s%s\n", pkgname, l, "\t\t\t\t\t\t", wantinfos[pkg->want].name);
57 }
58
59 int
60 getselections(const char *const *argv)
61 {
62   struct pkg_array array;
63   struct pkginfo *pkg;
64   const char *thisarg;
65   int i, found;
66
67   modstatdb_open(msdbrw_readonly);
68
69   pkg_array_init_from_db(&array);
70   pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
71
72   if (!*argv) {
73     for (i = 0; i < array.n_pkgs; i++) {
74       pkg = array.pkgs[i];
75       if (pkg->status == stat_notinstalled) continue;
76       getsel1package(pkg);
77     }
78   } else {
79     while ((thisarg= *argv++)) {
80       struct pkg_spec pkgspec;
81
82       found= 0;
83       pkg_spec_init(&pkgspec, psf_patterns | psf_arch_def_wildcard);
84       pkg_spec_parse(&pkgspec, thisarg);
85
86       for (i = 0; i < array.n_pkgs; i++) {
87         pkg = array.pkgs[i];
88         if (!pkg_spec_match_pkg(&pkgspec, pkg, &pkg->installed))
89           continue;
90         getsel1package(pkg); found++;
91       }
92       if (!found)
93         notice(_("no packages found matching %s"), thisarg);
94
95       pkg_spec_destroy(&pkgspec);
96     }
97   }
98
99   m_output(stdout, _("<standard output>"));
100   m_output(stderr, _("<standard error>"));
101
102   pkg_array_destroy(&array);
103
104   return 0;
105 }
106
107 int
108 setselections(const char *const *argv)
109 {
110   const struct namevalue *nv;
111   struct pkginfo *pkg;
112   int c, lno;
113   struct varbuf namevb = VARBUF_INIT;
114   struct varbuf selvb = VARBUF_INIT;
115
116   if (*argv)
117     badusage(_("--%s takes no arguments"), cipaction->olong);
118
119   modstatdb_open(msdbrw_write | msdbrw_available_readonly);
120   pkg_infodb_upgrade();
121
122   lno= 1;
123   for (;;) {
124     struct dpkg_error err;
125
126     do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && isspace(c));
127     if (c == EOF) break;
128     if (c == '#') {
129       do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
130       continue;
131     }
132
133     varbuf_reset(&namevb);
134     while (!isspace(c)) {
135       varbuf_add_char(&namevb, c);
136       c= getchar();
137       if (c == EOF) ohshit(_("unexpected eof in package name at line %d"),lno);
138       if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
139     }
140     varbuf_end_str(&namevb);
141
142     while (c != EOF && isspace(c)) {
143       c= getchar();
144       if (c == EOF) ohshit(_("unexpected eof after package name at line %d"),lno);
145       if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
146     }
147
148     varbuf_reset(&selvb);
149     while (c != EOF && !isspace(c)) {
150       varbuf_add_char(&selvb, c);
151       c= getchar();
152     }
153     varbuf_end_str(&selvb);
154
155     while (c != EOF && c != '\n') {
156       c= getchar();
157       if (!isspace(c))
158         ohshit(_("unexpected data after package and selection at line %d"),lno);
159     }
160     pkg = pkg_spec_parse_pkg(namevb.buf, &err);
161     if (pkg == NULL)
162       ohshit(_("illegal package name at line %d: %.250s"), lno, err.str);
163
164     if (!pkg_is_informative(pkg, &pkg->installed) &&
165         !pkg_is_informative(pkg, &pkg->available)) {
166       warning(_("package not in database at line %d: %.250s"), lno, namevb.buf);
167       continue;
168     }
169
170     nv = namevalue_find_by_name(wantinfos, selvb.buf);
171     if (nv == NULL)
172       ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
173
174     pkg_set_want(pkg, nv->value);
175     if (c == EOF) break;
176     lno++;
177   }
178   if (ferror(stdin)) ohshite(_("read error on standard input"));
179   modstatdb_shutdown();
180   varbuf_destroy(&namevb);
181   varbuf_destroy(&selvb);
182
183   return 0;
184 }
185
186 int
187 clearselections(const char *const *argv)
188 {
189   struct pkgiterator *it;
190   struct pkginfo *pkg;
191
192   if (*argv)
193     badusage(_("--%s takes no arguments"), cipaction->olong);
194
195   modstatdb_open(msdbrw_write);
196   pkg_infodb_upgrade();
197
198   it = pkg_db_iter_new();
199   while ((pkg = pkg_db_iter_next_pkg(it))) {
200     if (!pkg->installed.essential)
201       pkg_set_want(pkg, want_deinstall);
202   }
203   pkg_db_iter_free(it);
204
205   modstatdb_shutdown();
206
207   return 0;
208 }
209