2 * dpkg - main program for package management
3 * select.c - by-hand (rather than dselect-based) package selection
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>
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.
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.
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/>.
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>
45 static void getsel1package(struct pkginfo
*pkg
) {
49 if (pkg
->want
== PKG_WANT_UNKNOWN
)
51 pkgname
= pkg_name(pkg
, pnaw_nonambig
);
57 printf("%s%.*s%s\n", pkgname
, l
, "\t\t\t\t\t\t", pkg_want_name(pkg
));
61 getselections(const char *const *argv
)
63 struct pkg_array array
;
67 modstatdb_open(msdbrw_readonly
);
69 pkg_array_init_from_hash(&array
);
70 pkg_array_sort(&array
, pkg_sorter_by_nonambig_name_arch
);
73 for (i
= 0; i
< array
.n_pkgs
; i
++) {
75 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
82 while ((thisarg
= *argv
++)) {
83 struct pkg_spec pkgspec
;
87 pkg_spec_init(&pkgspec
, PKG_SPEC_PATTERNS
| PKG_SPEC_ARCH_WILDCARD
);
88 pkg_spec_parse(&pkgspec
, thisarg
);
90 for (i
= 0; i
< array
.n_pkgs
; i
++) {
92 if (!pkg_spec_match_pkg(&pkgspec
, pkg
, &pkg
->installed
))
94 getsel1package(pkg
); found
++;
97 notice(_("no packages found matching %s"), thisarg
);
99 pkg_spec_destroy(&pkgspec
);
103 m_output(stdout
, _("<standard output>"));
104 m_output(stderr
, _("<standard error>"));
106 pkg_array_destroy(&array
);
108 modstatdb_shutdown();
114 setselections(const char *const *argv
)
116 enum modstatdb_rw msdbflags
;
117 const struct namevalue
*nv
;
120 struct varbuf namevb
= VARBUF_INIT
;
121 struct varbuf selvb
= VARBUF_INIT
;
122 bool db_possibly_outdated
= false;
125 badusage(_("--%s takes no arguments"), cipaction
->olong
);
127 msdbflags
= msdbrw_available_readonly
;
129 msdbflags
|= msdbrw_readonly
;
131 msdbflags
|= msdbrw_write
;
133 modstatdb_open(msdbflags
);
134 pkg_infodb_upgrade();
138 struct dpkg_error err
;
144 } while (c
!= EOF
&& c_isspace(c
));
147 do { c
= getchar(); if (c
== '\n') lno
++; } while (c
!= EOF
&& c
!= '\n');
151 varbuf_reset(&namevb
);
152 while (!c_isspace(c
)) {
153 varbuf_add_char(&namevb
, c
);
156 ohshit(_("unexpected end of file in package name at line %d"), lno
);
157 if (c
== '\n') ohshit(_("unexpected end of line in package name at line %d"),lno
);
159 varbuf_end_str(&namevb
);
161 while (c
!= EOF
&& c_isspace(c
)) {
164 ohshit(_("unexpected end of file after package name at line %d"), lno
);
165 if (c
== '\n') ohshit(_("unexpected end of line after package name at line %d"),lno
);
168 varbuf_reset(&selvb
);
169 while (c
!= EOF
&& !c_isspace(c
)) {
170 varbuf_add_char(&selvb
, c
);
173 varbuf_end_str(&selvb
);
175 while (c
!= EOF
&& c
!= '\n') {
178 ohshit(_("unexpected data after package and selection at line %d"),lno
);
180 pkg
= pkg_spec_parse_pkg(namevb
.buf
, &err
);
182 ohshit(_("illegal package name at line %d: %.250s"), lno
, err
.str
);
184 if (!pkg_is_informative(pkg
, &pkg
->installed
) &&
185 !pkg_is_informative(pkg
, &pkg
->available
)) {
186 db_possibly_outdated
= true;
187 warning(_("package not in status nor available database at line %d: %.250s"), lno
, namevb
.buf
);
192 nv
= namevalue_find_by_name(wantinfos
, selvb
.buf
);
194 ohshit(_("unknown wanted status at line %d: %.250s"), lno
, selvb
.buf
);
196 pkg_set_want(pkg
, nv
->value
);
200 if (ferror(stdin
)) ohshite(_("read error on standard input"));
201 modstatdb_shutdown();
202 varbuf_destroy(&namevb
);
203 varbuf_destroy(&selvb
);
205 if (db_possibly_outdated
)
206 warning(_("found unknown packages; this might mean the available database\n"
207 "is outdated, and needs to be updated through a frontend method;\n"
208 "please see the FAQ <https://wiki.debian.org/Teams/Dpkg/FAQ#set-selections>"));
214 clearselections(const char *const *argv
)
216 enum modstatdb_rw msdbflags
;
217 struct pkg_hash_iter
*iter
;
221 badusage(_("--%s takes no arguments"), cipaction
->olong
);
224 msdbflags
= msdbrw_readonly
;
226 msdbflags
= msdbrw_write
;
228 modstatdb_open(msdbflags
);
229 pkg_infodb_upgrade();
231 iter
= pkg_hash_iter_new();
232 while ((pkg
= pkg_hash_iter_next_pkg(iter
))) {
233 if (!pkg
->installed
.essential
&&
234 !pkg
->installed
.is_protected
&&
235 pkg
->want
!= PKG_WANT_UNKNOWN
)
236 pkg_set_want(pkg
, PKG_WANT_DEINSTALL
);
238 pkg_hash_iter_free(iter
);
240 modstatdb_shutdown();