Branch data Line data Source code
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 <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2006, 2008-2015 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 <https://www.gnu.org/licenses/>.
22 : : */
23 : :
24 : : #include <config.h>
25 : : #include <compat.h>
26 : :
27 : : #include <fnmatch.h>
28 : : #include <string.h>
29 : : #include <stdlib.h>
30 : : #include <stdio.h>
31 : :
32 : : #include <dpkg/i18n.h>
33 : : #include <dpkg/c-ctype.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 : : #include <dpkg/db-ctrl.h>
41 : : #include <dpkg/db-fsys.h>
42 : :
43 : : #include "main.h"
44 : :
45 : 0 : static void getsel1package(struct pkginfo *pkg) {
46 : : const char *pkgname;
47 : : int l;
48 : :
49 [ # # ]: 0 : if (pkg->want == PKG_WANT_UNKNOWN)
50 : 0 : return;
51 : 0 : pkgname = pkg_name(pkg, pnaw_nonambig);
52 : 0 : l = strlen(pkgname);
53 : 0 : l >>= 3;
54 : 0 : l = 6 - l;
55 [ # # ]: 0 : if (l < 1)
56 : 0 : l = 1;
57 : 0 : printf("%s%.*s%s\n", pkgname, l, "\t\t\t\t\t\t", pkg_want_name(pkg));
58 : : }
59 : :
60 : : int
61 : 0 : getselections(const char *const *argv)
62 : : {
63 : : struct pkg_array array;
64 : : struct pkginfo *pkg;
65 : : const char *thisarg;
66 : : int i, found;
67 : :
68 : 0 : modstatdb_open(msdbrw_readonly);
69 : :
70 : 0 : pkg_array_init_from_hash(&array);
71 : 0 : pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
72 : :
73 [ # # ]: 0 : if (!*argv) {
74 [ # # ]: 0 : for (i = 0; i < array.n_pkgs; i++) {
75 : 0 : pkg = array.pkgs[i];
76 [ # # ]: 0 : if (pkg->status == PKG_STAT_NOTINSTALLED)
77 : 0 : continue;
78 : 0 : getsel1package(pkg);
79 : : }
80 : : } else {
81 [ # # ]: 0 : while ((thisarg= *argv++)) {
82 : : struct pkg_spec pkgspec;
83 : :
84 : 0 : found= 0;
85 : 0 : pkg_spec_init(&pkgspec, PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD);
86 : 0 : pkg_spec_parse(&pkgspec, thisarg);
87 : :
88 [ # # ]: 0 : for (i = 0; i < array.n_pkgs; i++) {
89 : 0 : pkg = array.pkgs[i];
90 [ # # ]: 0 : if (!pkg_spec_match_pkg(&pkgspec, pkg, &pkg->installed))
91 : 0 : continue;
92 : 0 : getsel1package(pkg); found++;
93 : : }
94 [ # # ]: 0 : if (!found)
95 : 0 : notice(_("no packages found matching %s"), thisarg);
96 : :
97 : 0 : pkg_spec_destroy(&pkgspec);
98 : : }
99 : : }
100 : :
101 : 0 : m_output(stdout, _("<standard output>"));
102 : 0 : m_output(stderr, _("<standard error>"));
103 : :
104 : 0 : pkg_array_destroy(&array);
105 : :
106 : 0 : modstatdb_shutdown();
107 : :
108 : 0 : return 0;
109 : : }
110 : :
111 : : int
112 : 0 : setselections(const char *const *argv)
113 : : {
114 : : enum modstatdb_rw msdbflags;
115 : : const struct namevalue *nv;
116 : : struct pkginfo *pkg;
117 : : int c, lno;
118 : 0 : struct varbuf namevb = VARBUF_INIT;
119 : 0 : struct varbuf selvb = VARBUF_INIT;
120 : 0 : bool db_possibly_outdated = false;
121 : :
122 [ # # ]: 0 : if (*argv)
123 : 0 : badusage(_("--%s takes no arguments"), cipaction->olong);
124 : :
125 : 0 : msdbflags = msdbrw_available_readonly;
126 [ # # ]: 0 : if (f_noact)
127 : 0 : msdbflags |= msdbrw_readonly;
128 : : else
129 : 0 : msdbflags |= msdbrw_write;
130 : :
131 : 0 : modstatdb_open(msdbflags);
132 : 0 : pkg_infodb_upgrade();
133 : :
134 : 0 : lno= 1;
135 : 0 : for (;;) {
136 : : struct dpkg_error err;
137 : :
138 : : do {
139 : 0 : c = getchar();
140 [ # # ]: 0 : if (c == '\n')
141 : 0 : lno++;
142 [ # # # # ]: 0 : } while (c != EOF && c_isspace(c));
143 [ # # ]: 0 : if (c == EOF) break;
144 [ # # ]: 0 : if (c == '#') {
145 [ # # # # : 0 : do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
# # ]
146 : 0 : continue;
147 : : }
148 : :
149 : 0 : varbuf_reset(&namevb);
150 [ # # ]: 0 : while (!c_isspace(c)) {
151 : 0 : varbuf_add_char(&namevb, c);
152 : 0 : c= getchar();
153 [ # # ]: 0 : if (c == EOF)
154 : 0 : ohshit(_("unexpected end of file in package name at line %d"), lno);
155 [ # # ]: 0 : if (c == '\n') ohshit(_("unexpected end of line in package name at line %d"),lno);
156 : : }
157 : 0 : varbuf_end_str(&namevb);
158 : :
159 [ # # # # ]: 0 : while (c != EOF && c_isspace(c)) {
160 : 0 : c= getchar();
161 [ # # ]: 0 : if (c == EOF)
162 : 0 : ohshit(_("unexpected end of file after package name at line %d"), lno);
163 [ # # ]: 0 : if (c == '\n') ohshit(_("unexpected end of line after package name at line %d"),lno);
164 : : }
165 : :
166 : 0 : varbuf_reset(&selvb);
167 [ # # # # ]: 0 : while (c != EOF && !c_isspace(c)) {
168 : 0 : varbuf_add_char(&selvb, c);
169 : 0 : c= getchar();
170 : : }
171 : 0 : varbuf_end_str(&selvb);
172 : :
173 [ # # # # ]: 0 : while (c != EOF && c != '\n') {
174 : 0 : c= getchar();
175 [ # # ]: 0 : if (!c_isspace(c))
176 : 0 : ohshit(_("unexpected data after package and selection at line %d"),lno);
177 : : }
178 : 0 : pkg = pkg_spec_parse_pkg(namevb.buf, &err);
179 [ # # ]: 0 : if (pkg == NULL)
180 : 0 : ohshit(_("illegal package name at line %d: %.250s"), lno, err.str);
181 : :
182 [ # # ]: 0 : if (!pkg_is_informative(pkg, &pkg->installed) &&
183 [ # # ]: 0 : !pkg_is_informative(pkg, &pkg->available)) {
184 : 0 : db_possibly_outdated = true;
185 : 0 : warning(_("package not in status nor available database at line %d: %.250s"), lno, namevb.buf);
186 : 0 : lno++;
187 : 0 : continue;
188 : : }
189 : :
190 : 0 : nv = namevalue_find_by_name(wantinfos, selvb.buf);
191 [ # # ]: 0 : if (nv == NULL)
192 : 0 : ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
193 : :
194 : 0 : pkg_set_want(pkg, nv->value);
195 [ # # ]: 0 : if (c == EOF) break;
196 : 0 : lno++;
197 : : }
198 [ # # ]: 0 : if (ferror(stdin)) ohshite(_("read error on standard input"));
199 : 0 : modstatdb_shutdown();
200 : 0 : varbuf_destroy(&namevb);
201 : 0 : varbuf_destroy(&selvb);
202 : :
203 [ # # ]: 0 : if (db_possibly_outdated)
204 : 0 : warning(_("found unknown packages; this might mean the available database\n"
205 : : "is outdated, and needs to be updated through a frontend method;\n"
206 : : "please see the FAQ <https://wiki.debian.org/Teams/Dpkg/FAQ#set-selections>"));
207 : :
208 : 0 : return 0;
209 : : }
210 : :
211 : : int
212 : 0 : clearselections(const char *const *argv)
213 : : {
214 : : enum modstatdb_rw msdbflags;
215 : : struct pkg_hash_iter *iter;
216 : : struct pkginfo *pkg;
217 : :
218 [ # # ]: 0 : if (*argv)
219 : 0 : badusage(_("--%s takes no arguments"), cipaction->olong);
220 : :
221 [ # # ]: 0 : if (f_noact)
222 : 0 : msdbflags = msdbrw_readonly;
223 : : else
224 : 0 : msdbflags = msdbrw_write;
225 : :
226 : 0 : modstatdb_open(msdbflags);
227 : 0 : pkg_infodb_upgrade();
228 : :
229 : 0 : iter = pkg_hash_iter_new();
230 [ # # ]: 0 : while ((pkg = pkg_hash_iter_next_pkg(iter))) {
231 [ # # ]: 0 : if (!pkg->installed.essential &&
232 [ # # ]: 0 : !pkg->installed.is_protected &&
233 [ # # ]: 0 : pkg->want != PKG_WANT_UNKNOWN)
234 : 0 : pkg_set_want(pkg, PKG_WANT_DEINSTALL);
235 : : }
236 : 0 : pkg_hash_iter_free(iter);
237 : :
238 : 0 : modstatdb_shutdown();
239 : :
240 : 0 : return 0;
241 : : }
242 : :
|