test: Quote command variables in case these contain spaces
[dpkg.git] / dselect / pkgtop.cc
blobe7bedc7f2f3704a79b5c080fce05da9f52f71582
1 /*
2 * dselect - Debian package maintenance user interface
3 * pkgtop.cc - handles (re)draw of package list windows colheads, list, thisstate
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2007-2014 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <string.h>
26 #include <stdio.h>
28 #include <dpkg/i18n.h>
29 #include <dpkg/c-ctype.h>
30 #include <dpkg/dpkg.h>
31 #include <dpkg/dpkg-db.h>
33 #include "dselect.h"
34 #include "pkglist.h"
36 static const char *
37 pkgprioritystring(const struct pkginfo *pkg)
39 if (pkg->priority == PKG_PRIO_UNSET) {
40 return nullptr;
41 } else if (pkg->priority == PKG_PRIO_OTHER) {
42 return pkg->otherpriority;
43 } else {
44 if (pkg->priority > PKG_PRIO_UNKNOWN)
45 internerr("package %s has out-of-range priority %d",
46 pkg_name_const(pkg, pnaw_always), pkg->priority);
47 return gettext(prioritystrings[pkg->priority]);
51 int packagelist::describemany(char buf[], const char *prioritystring,
52 const char *section,
53 const struct perpackagestate *pps) {
54 const char *ssostring, *ssoabbrev;
55 int statindent;
57 statindent= 0;
58 ssostring = nullptr;
59 ssoabbrev= _("All");
60 switch (statsortorder) {
61 case sso_avail:
62 if (pps->ssavail == -1) break;
63 ssostring= ssastrings[pps->ssavail];
64 ssoabbrev= ssaabbrevs[pps->ssavail];
65 statindent++;
66 break;
67 case sso_state:
68 if (pps->ssstate == -1) break;
69 ssostring= sssstrings[pps->ssstate];
70 ssoabbrev= sssabbrevs[pps->ssstate];
71 statindent++;
72 break;
73 case sso_unsorted:
74 break;
75 default:
76 internerr("unknown statsortrder %d", statsortorder);
79 if (!prioritystring) {
80 if (!section) {
81 strcpy(buf, ssostring ? gettext(ssostring) : _("All packages"));
82 return statindent;
83 } else {
84 if (!*section) {
85 sprintf(buf,_("%s packages without a section"),gettext(ssoabbrev));
86 } else {
87 sprintf(buf,_("%s packages in section %s"),gettext(ssoabbrev),section);
89 return statindent+1;
91 } else {
92 if (!section) {
93 sprintf(buf,_("%s %s packages"),gettext(ssoabbrev),prioritystring);
94 return statindent+1;
95 } else {
96 if (!*section) {
97 sprintf(buf,_("%s %s packages without a section"),gettext(ssoabbrev),prioritystring);
98 } else {
99 sprintf(buf,_("%s %s packages in section %s"),gettext(ssoabbrev),prioritystring,section);
101 return statindent+2;
106 void packagelist::redrawthisstate() {
107 if (!thisstate_height) return;
108 mywerase(thisstatepad);
110 const char *section= table[cursorline]->pkg->section;
111 const char *priority= pkgprioritystring(table[cursorline]->pkg);
112 char *buf= new char[500+
113 max((table[cursorline]->pkg->set->name ?
114 strlen(table[cursorline]->pkg->set->name) : 0),
115 (section ? strlen(section) : 0) +
116 (priority ? strlen(priority) : 0))];
118 if (table[cursorline]->pkg->set->name) {
119 sprintf(buf,
120 _("%-*s %s%s%s; %s (was: %s). %s"),
121 col_package.width,
122 table[cursorline]->pkg->set->name,
123 gettext(statusstrings[table[cursorline]->pkg->status]),
124 ((eflagstrings[table[cursorline]->pkg->eflag][0]==' ') &&
125 (eflagstrings[table[cursorline]->pkg->eflag][1]=='\0')) ? "" : " - ",
126 gettext(eflagstrings[table[cursorline]->pkg->eflag]),
127 gettext(wantstrings[table[cursorline]->selected]),
128 gettext(wantstrings[table[cursorline]->original]),
129 priority);
130 } else {
131 describemany(buf,priority,section,table[cursorline]->pkg->clientdata);
133 mvwaddnstr(thisstatepad,0,0, buf, total_width);
134 pnoutrefresh(thisstatepad, 0,leftofscreen, thisstate_row,0,
135 thisstate_row, min(total_width - 1, xmax - 1));
137 delete[] buf;
140 void packagelist::redraw1itemsel(int index, int selected) {
141 int i;
142 const char *p;
143 const struct pkginfo *pkg= table[index]->pkg;
144 int screenline = index - topofscreen;
146 wattrset(listpad, part_attr[selected ? listsel : list]);
148 if (pkg->set->name) {
149 if (verbose) {
150 draw_column_item(col_status_hold, screenline,
151 gettext(eflagstrings[pkg->eflag]));
153 draw_column_sep(col_status_status, screenline);
154 draw_column_item(col_status_status, screenline,
155 gettext(statusstrings[pkg->status]));
157 draw_column_sep(col_status_old_want, screenline);
158 draw_column_item(col_status_old_want, screenline,
159 /* FIXME: keep this? */
160 /*table[index]->original == table[index]->selected ? "(same)"
161 : */gettext(wantstrings[table[index]->original]));
163 draw_column_sep(col_status_new_want, screenline);
164 wattrset(listpad, part_attr[selected ? selstatesel : selstate]);
165 draw_column_item(col_status_new_want, screenline,
166 gettext(wantstrings[table[index]->selected]));
168 wattrset(listpad, part_attr[selected ? listsel : list]);
170 draw_column_sep(col_priority, screenline);
171 draw_column_item(col_priority, screenline,
172 pkg->priority == PKG_PRIO_OTHER ?
173 pkg->otherpriority :
174 gettext(prioritystrings[pkg->priority]));
175 } else {
176 mvwaddch(listpad, screenline, 0, eflagchars[pkg->eflag]);
177 waddch(listpad, statuschars[pkg->status]);
178 waddch(listpad,
179 /* FIXME: keep this feature? */
180 /*table[index]->original == table[index]->selected ? ' '
181 : */wantchars[table[index]->original]);
183 wattrset(listpad, part_attr[selected ? selstatesel : selstate]);
184 waddch(listpad, wantchars[table[index]->selected]);
185 wattrset(listpad, part_attr[selected ? listsel : list]);
187 wmove(listpad, screenline, col_priority.x - 1);
188 waddch(listpad, ' ');
189 if (pkg->priority == PKG_PRIO_OTHER) {
190 for (i = col_priority.width, p = pkg->otherpriority;
191 i > 0 && *p;
192 i--, p++)
193 waddch(listpad, c_tolower(*p));
194 while (i-- > 0) waddch(listpad,' ');
195 } else {
196 wprintw(listpad, "%-*.*s", col_priority.width, col_priority.width,
197 gettext(priorityabbrevs[pkg->priority]));
201 draw_column_sep(col_section, screenline);
202 draw_column_item(col_section, screenline,
203 pkg->section ? pkg->section : "?");
205 draw_column_sep(col_package, screenline);
206 draw_column_item(col_package, screenline,
207 pkg->set->name);
209 waddch(listpad, ' ');
211 if (col_archinstalled.width) {
212 draw_column_sep(col_archinstalled, screenline);
213 draw_column_item(col_archinstalled, screenline, pkg->installed.arch->name);
215 waddch(listpad, ' ');
217 if (col_archavailable.width) {
218 draw_column_sep(col_archavailable, screenline);
219 draw_column_item(col_archavailable, screenline, pkg->available.arch->name);
221 waddch(listpad, ' ');
224 if (col_versioninstalled.width) {
225 draw_column_item(col_versioninstalled, screenline,
226 versiondescribe(&pkg->installed.version, vdew_nonambig));
227 waddch(listpad, ' ');
229 if (col_versionavailable.width) {
230 if (dpkg_version_is_informative(&pkg->available.version) &&
231 dpkg_version_compare(&pkg->available.version,
232 &pkg->installed.version) > 0)
233 wattrset(listpad, part_attr[selected ? selstatesel : selstate]);
234 draw_column_item(col_versionavailable, screenline,
235 versiondescribe(&pkg->available.version, vdew_nonambig));
236 wattrset(listpad, part_attr[selected ? listsel : list]);
237 waddch(listpad,' ');
240 i = col_description.width;
241 p = pkg->available.description ? pkg->available.description :
242 pkg->installed.description ? pkg->installed.description : "";
243 while (i>0 && *p && *p != '\n') { waddnstr(listpad,p,1); i--; p++; }
244 } else {
245 int j, indent;
246 const char *section= pkg->section;
247 const char *priority= pkgprioritystring(pkg);
249 char *buf= new char[500+
250 (section ? strlen(section) : 0) +
251 (priority ? strlen(priority) : 0)];
253 indent= describemany(buf,priority,section,pkg->clientdata);
255 mvwaddstr(listpad, screenline, 0, " ");
256 i= total_width-7;
257 j= (indent<<1) + 1;
258 while (j-- >0) { waddch(listpad,ACS_HLINE); i--; }
259 waddch(listpad,' ');
261 wattrset(listpad, part_attr[selected ? selstatesel : selstate]);
262 p= buf;
263 while (i>0 && *p) { waddnstr(listpad, p,1); p++; i--; }
264 wattrset(listpad, part_attr[selected ? listsel : list]);
266 waddch(listpad,' ');
267 j= (indent<<1) + 1;
268 while (j-- >0) { waddch(listpad,ACS_HLINE); i--; }
270 delete[] buf;
273 while (i>0) { waddch(listpad,' '); i--; }
276 void packagelist::redrawcolheads() {
277 if (colheads_height) {
278 wattrset(colheadspad, part_attr[colheads]);
279 mywerase(colheadspad);
280 if (verbose) {
281 wmove(colheadspad,0,0);
282 for (int i = 0; i < col_status_old_want.width; i++)
283 waddch(colheadspad, '.');
284 draw_column_head(col_status_hold);
285 draw_column_head(col_status_status);
286 draw_column_head(col_status_old_want);
287 draw_column_head(col_status_new_want);
288 } else {
289 draw_column_head(col_status);
292 draw_column_head(col_section);
293 draw_column_head(col_priority);
294 draw_column_head(col_package);
296 if (col_archinstalled.width)
297 draw_column_head(col_archinstalled);
298 if (col_archavailable.width)
299 draw_column_head(col_archavailable);
301 if (col_versioninstalled.width)
302 draw_column_head(col_versioninstalled);
303 if (col_versionavailable.width)
304 draw_column_head(col_versionavailable);
306 draw_column_head(col_description);
308 refreshcolheads();