2 * libdpkg - Debian packaging suite library routines
3 * pkg-show.c - primitives for pkg information display
5 * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-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/>.
27 #include <dpkg/macros.h>
28 #include <dpkg/i18n.h>
29 #include <dpkg/dpkg.h>
30 #include <dpkg/dpkg-db.h>
31 #include <dpkg/pkg-show.h>
34 pkgbin_name_needs_arch(const struct pkgbin
*pkgbin
,
35 enum pkg_name_arch_when pnaw
)
37 if (pkgbin
->arch
->type
== DPKG_ARCH_NONE
||
38 pkgbin
->arch
->type
== DPKG_ARCH_EMPTY
)
45 if (pkgbin
->multiarch
== PKG_MULTIARCH_SAME
)
49 if (pkgbin
->multiarch
== PKG_MULTIARCH_SAME
)
53 if (pkgbin
->arch
->type
== DPKG_ARCH_NATIVE
||
54 pkgbin
->arch
->type
== DPKG_ARCH_ALL
)
65 * Add a string representation of the package name to a varbuf.
67 * Works exactly like pkgbin_name() but acts on the varbuf instead of
68 * returning a string. It NUL terminates the varbuf.
70 * @param vb The varbuf struct to modify.
71 * @param pkg The package to consider.
72 * @param pkgbin The binary package instance to consider.
73 * @param pnaw When to display the architecture qualifier.
76 varbuf_add_pkgbin_name(struct varbuf
*vb
,
77 const struct pkginfo
*pkg
, const struct pkgbin
*pkgbin
,
78 enum pkg_name_arch_when pnaw
)
80 varbuf_add_str(vb
, pkg
->set
->name
);
81 if (pkgbin_name_needs_arch(pkgbin
, pnaw
))
82 varbuf_add_archqual(vb
, pkgbin
->arch
);
86 pkgbin_name_archqual(const struct pkginfo
*pkg
, const struct pkgbin
*pkgbin
)
90 if (pkgbin
->arch
->type
== DPKG_ARCH_NONE
||
91 pkgbin
->arch
->type
== DPKG_ARCH_EMPTY
)
92 return pkg
->set
->name
;
94 pkgname
= nfmalloc(strlen(pkg
->set
->name
) + 1 +
95 strlen(pkgbin
->arch
->name
) + 1);
96 str_concat(pkgname
, pkg
->set
->name
, ":",
97 pkgbin
->arch
->name
, NULL
);
103 * Return a string representation of the package name.
105 * The returned string must not be freed, and it's permanently allocated so
106 * can be used as long as the non-freeing memory pool has not been freed.
108 * Note, that this const variant will "leak" a new non-freeing string on
109 * each call if the internal cache has not been previously initialized,
110 * so it is advised to use it only in error reporting code paths.
112 * The pnaw parameter should be one of pnaw_never (never print arch),
113 * pnaw_foreign (print arch for foreign packages only), pnaw_nonambig (print
114 * arch for non ambiguous cases) or pnaw_always (always print arch),
116 * @param pkg The package to consider.
117 * @param pkgbin The binary package instance to consider.
118 * @param pnaw When to display the architecture qualifier.
120 * @return The string representation.
123 pkgbin_name_const(const struct pkginfo
*pkg
, const struct pkgbin
*pkgbin
,
124 enum pkg_name_arch_when pnaw
)
126 if (!pkgbin_name_needs_arch(pkgbin
, pnaw
))
127 return pkg
->set
->name
;
129 /* Return a non-freeing package name representation, which
130 * is intended to be used in error-handling code, as we will keep
131 * "leaking" them until the next memory pool flush. */
132 if (pkgbin
->pkgname_archqual
== NULL
)
133 return pkgbin_name_archqual(pkg
, pkgbin
);
135 return pkgbin
->pkgname_archqual
;
139 * Return a string representation of the installed package name.
141 * This is equivalent to pkgbin_name_const() but just for its installed pkgbin.
143 * @param pkg The package to consider.
144 * @param pnaw When to display the architecture qualifier.
146 * @return The string representation.
149 pkg_name_const(const struct pkginfo
*pkg
, enum pkg_name_arch_when pnaw
)
151 return pkgbin_name_const(pkg
, &pkg
->installed
, pnaw
);
155 * Return a string representation of the package name.
157 * The returned string must not be freed, and it's permanently allocated so
158 * can be used as long as the non-freeing memory pool has not been freed.
160 * The pnaw parameter should be one of pnaw_never (never print arch),
161 * pnaw_foreign (print arch for foreign packages only), pnaw_nonambig (print
162 * arch for non ambiguous cases) or pnaw_always (always print arch),
164 * @param pkg The package to consider.
165 * @param pkgbin The binary package instance to consider.
166 * @param pnaw When to display the architecture qualifier.
168 * @return The string representation.
171 pkgbin_name(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
172 enum pkg_name_arch_when pnaw
)
174 if (!pkgbin_name_needs_arch(pkgbin
, pnaw
))
175 return pkg
->set
->name
;
177 /* Cache the package name representation, for later reuse. */
178 if (pkgbin
->pkgname_archqual
== NULL
)
179 pkgbin
->pkgname_archqual
= pkgbin_name_archqual(pkg
, pkgbin
);
181 return pkgbin
->pkgname_archqual
;
185 * Return a string representation of the installed package name.
187 * This is equivalent to pkgbin_name() but just for its installed pkgbin.
189 * @param pkg The package to consider.
190 * @param pnaw When to display the architecture qualifier.
192 * @return The string representation.
195 pkg_name(struct pkginfo
*pkg
, enum pkg_name_arch_when pnaw
)
197 return pkgbin_name(pkg
, &pkg
->installed
, pnaw
);
201 * Return a string representation of the package synopsis.
203 * The returned string must not be freed, and it's permanently allocated so
204 * can be used as long as the non-freeing memory pool has not been freed.
206 * The package synopsis is the short description, but it is not NUL terminated,
207 * so the output len argument should be used to limit the string length.
209 * @param pkg The package to consider.
210 * @param pkgbin The binary package instance to consider.
211 * @param[out] len The length of the synopsis string within the description.
213 * @return The string representation.
216 pkgbin_synopsis(const struct pkginfo
*pkg
, const struct pkgbin
*pkgbin
, int *len
)
220 pdesc
= pkgbin
->description
;
222 pdesc
= _("(no description available)");
224 *len
= strcspn(pdesc
, "\n");
230 * Return a string representation of the package synopsis.
232 * The returned string must not be freed, and it's permanently allocated so
233 * can be used as long as the non-freeing memory pool has not been freed.
235 * It will try to use the installed version, otherwise it will fallback to
236 * use the available version.
238 * The package synopsis is the short description, but it is not NUL terminated,
239 * so the output len argument should be used to limit the string length.
241 * @param pkg The package to consider.
242 * @param[out] len The length of the synopsis string within the description.
244 * @return The string representation.
247 pkg_synopsis(const struct pkginfo
*pkg
, int *len
)
251 pdesc
= pkg
->installed
.description
;
253 pdesc
= pkg
->available
.description
;
255 pdesc
= _("(no description available)");
257 *len
= strcspn(pdesc
, "\n");
263 * Return a character abbreviated representation of the package want status.
265 * @param pkg The package to consider.
267 * @return The character abbreviated representation.
270 pkg_abbrev_want(const struct pkginfo
*pkg
)
272 return "uihrp"[pkg
->want
];
276 * Return a character abbreviated representation of the package current status.
278 * @param pkg The package to consider.
280 * @return The character abbreviated representation.
283 pkg_abbrev_status(const struct pkginfo
*pkg
)
285 return "ncHUFWti"[pkg
->status
];
289 * Return a character abbreviated representation of the package eflag status.
291 * @param pkg The package to consider.
293 * @return The character abbreviated representation.
296 pkg_abbrev_eflag(const struct pkginfo
*pkg
)
298 return " R"[pkg
->eflag
];
302 * Return a string representation of the package want status name.
304 * @param pkg The package to consider.
306 * @return The string representation.
309 pkg_want_name(const struct pkginfo
*pkg
)
311 return wantinfos
[pkg
->want
].name
;
315 * Return a string representation of the package eflag status name.
317 * @param pkg The package to consider.
319 * @return The string representation.
322 pkg_eflag_name(const struct pkginfo
*pkg
)
324 return eflaginfos
[pkg
->eflag
].name
;
328 * Return a string representation of the package current status name.
330 * @param pkg The package to consider.
332 * @return The string representation.
335 pkg_status_name(const struct pkginfo
*pkg
)
337 return statusinfos
[pkg
->status
].name
;
341 * Return a string representation of the package priority name.
343 * @param pkg The package to consider.
345 * @return The string representation.
348 pkg_priority_name(const struct pkginfo
*pkg
)
350 if (pkg
->priority
== PKG_PRIO_OTHER
)
351 return pkg
->otherpriority
;
353 return priorityinfos
[pkg
->priority
].name
;
357 * Compare a package to be sorted by non-ambiguous name and architecture.
359 * @param a A pointer of a pointer to a struct pkginfo.
360 * @param b A pointer of a pointer to a struct pkginfo.
362 * @return An integer with the result of the comparison.
363 * @retval -1 a is earlier than b.
364 * @retval 0 a is equal to b.
365 * @retval 1 a is later than b.
368 pkg_sorter_by_nonambig_name_arch(const void *a
, const void *b
)
370 const struct pkginfo
*pa
= *(const struct pkginfo
**)a
;
371 const struct pkginfo
*pb
= *(const struct pkginfo
**)b
;
372 const struct pkgbin
*pbina
= &pa
->installed
;
373 const struct pkgbin
*pbinb
= &pb
->installed
;
376 res
= strcmp(pa
->set
->name
, pb
->set
->name
);
380 if (pbina
->arch
== pbinb
->arch
)
383 if (pkgbin_name_needs_arch(pbina
, pnaw_nonambig
)) {
384 if (pkgbin_name_needs_arch(pbinb
, pnaw_nonambig
))
385 return strcmp(pbina
->arch
->name
, pbinb
->arch
->name
);
394 * Add a string representation of the source package version to a varbuf.
396 * It parses the Source field (if present), and extracts the optional
397 * version enclosed in parenthesis. Otherwise it falls back to use the
398 * binary package version. It NUL terminates the varbuf.
400 * @param vb The varbuf struct to modify.
401 * @param pkg The package to consider.
402 * @param pkgbin The binary package instance to consider.
405 varbuf_add_source_version(struct varbuf
*vb
,
406 const struct pkginfo
*pkg
, const struct pkgbin
*pkgbin
)
408 struct dpkg_version version
= DPKG_VERSION_INIT
;
410 pkg_source_version(&version
, pkg
, pkgbin
);
411 varbufversion(vb
, &version
, vdew_nonambig
);
415 pkg_source_version(struct dpkg_version
*version
,
416 const struct pkginfo
*pkg
, const struct pkgbin
*pkgbin
)
418 const char *version_str
;
421 version_str
= strchr(pkgbin
->source
, '(');
425 if (version_str
== NULL
) {
426 *version
= pkgbin
->version
;
428 struct dpkg_error err
;
429 struct varbuf vb
= VARBUF_INIT
;
433 len
= strcspn(version_str
, ")");
434 varbuf_add_buf(&vb
, version_str
, len
);
436 if (parseversion(version
, varbuf_str(&vb
), &err
) < 0)
437 ohshit(_("version '%s' has bad syntax: %s"),
438 varbuf_str(&vb
), err
.str
);