doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / src / main / select.c
blobd71202e30c9c4ab0415749c38a96313e40be8a4b
1 /*
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/>.
24 #include <config.h>
25 #include <compat.h>
27 #include <fnmatch.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
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>
43 #include "main.h"
45 static void getsel1package(struct pkginfo *pkg) {
46 const char *pkgname;
47 int l;
49 if (pkg->want == PKG_WANT_UNKNOWN)
50 return;
51 pkgname = pkg_name(pkg, pnaw_nonambig);
52 l = strlen(pkgname);
53 l >>= 3;
54 l = 6 - l;
55 if (l < 1)
56 l = 1;
57 printf("%s%.*s%s\n", pkgname, l, "\t\t\t\t\t\t", pkg_want_name(pkg));
60 int
61 getselections(const char *const *argv)
63 struct pkg_array array;
64 struct pkginfo *pkg;
65 int i;
67 modstatdb_open(msdbrw_readonly);
69 pkg_array_init_from_hash(&array);
70 pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
72 if (!*argv) {
73 for (i = 0; i < array.n_pkgs; i++) {
74 pkg = array.pkgs[i];
75 if (pkg->status == PKG_STAT_NOTINSTALLED)
76 continue;
77 getsel1package(pkg);
79 } else {
80 const char *thisarg;
82 while ((thisarg= *argv++)) {
83 struct pkg_spec pkgspec;
84 int found;
86 found= 0;
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++) {
91 pkg = array.pkgs[i];
92 if (!pkg_spec_match_pkg(&pkgspec, pkg, &pkg->installed))
93 continue;
94 getsel1package(pkg); found++;
96 if (!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();
110 return 0;
114 setselections(const char *const *argv)
116 enum modstatdb_rw msdbflags;
117 const struct namevalue *nv;
118 struct pkginfo *pkg;
119 int c, lno;
120 struct varbuf namevb = VARBUF_INIT;
121 struct varbuf selvb = VARBUF_INIT;
122 bool db_possibly_outdated = false;
124 if (*argv)
125 badusage(_("--%s takes no arguments"), cipaction->olong);
127 msdbflags = msdbrw_available_readonly;
128 if (f_noact)
129 msdbflags |= msdbrw_readonly;
130 else
131 msdbflags |= msdbrw_write;
133 modstatdb_open(msdbflags);
134 pkg_infodb_upgrade();
136 lno= 1;
137 for (;;) {
138 struct dpkg_error err;
140 do {
141 c = getchar();
142 if (c == '\n')
143 lno++;
144 } while (c != EOF && c_isspace(c));
145 if (c == EOF) break;
146 if (c == '#') {
147 do { c= getchar(); if (c == '\n') lno++; } while (c != EOF && c != '\n');
148 continue;
151 varbuf_reset(&namevb);
152 while (!c_isspace(c)) {
153 varbuf_add_char(&namevb, c);
154 c= getchar();
155 if (c == EOF)
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)) {
162 c= getchar();
163 if (c == EOF)
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);
171 c= getchar();
173 varbuf_end_str(&selvb);
175 while (c != EOF && c != '\n') {
176 c= getchar();
177 if (!c_isspace(c))
178 ohshit(_("unexpected data after package and selection at line %d"),lno);
180 pkg = pkg_spec_parse_pkg(namevb.buf, &err);
181 if (pkg == NULL)
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);
188 lno++;
189 continue;
192 nv = namevalue_find_by_name(wantinfos, selvb.buf);
193 if (nv == NULL)
194 ohshit(_("unknown wanted status at line %d: %.250s"), lno, selvb.buf);
196 pkg_set_want(pkg, nv->value);
197 if (c == EOF) break;
198 lno++;
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>"));
210 return 0;
214 clearselections(const char *const *argv)
216 enum modstatdb_rw msdbflags;
217 struct pkg_hash_iter *iter;
218 struct pkginfo *pkg;
220 if (*argv)
221 badusage(_("--%s takes no arguments"), cipaction->olong);
223 if (f_noact)
224 msdbflags = msdbrw_readonly;
225 else
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();
242 return 0;