Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / info / show.c
blobc7cb172eb2f8ed7e384f63b54a4689fffcb796ce
1 /* $NetBSD: show.c,v 1.30 2009/08/02 17:56:45 joerg Exp $ */
3 #if HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 #include <nbcompat.h>
7 #if HAVE_SYS_CDEFS_H
8 #include <sys/cdefs.h>
9 #endif
10 __RCSID("$NetBSD: show.c,v 1.30 2009/08/02 17:56:45 joerg Exp $");
13 * FreeBSD install - a package for the installation and maintainance
14 * of non-core utilities.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * Jordan K. Hubbard
26 * 23 Aug 1993
28 * Various display routines for the info module.
31 /*-
32 * Copyright (c) 1999-2008 The NetBSD Foundation, Inc.
33 * All rights reserved.
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Hubert Feyrer <hubert@feyrer.de>.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
48 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
51 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
60 #if HAVE_ERR_H
61 #include <err.h>
62 #endif
64 #include "defs.h"
65 #include "lib.h"
66 #include "info.h"
68 /* Structure to define entries for the "show table" */
69 typedef struct show_t {
70 pl_ent_t sh_type; /* type of entry */
71 const char *sh_quiet; /* message when quiet */
72 const char *sh_verbose; /* message when verbose */
73 } show_t;
76 * The entries in this table must be ordered the same as
77 * pl_ent_t constants
79 static const show_t showv[] = {
80 {PLIST_FILE, "%s", "\tFile: %s"},
81 {PLIST_CWD, "@cwd %s", "\tCWD to: %s"},
82 {PLIST_CMD, "@exec %s", "\tEXEC '%s'"},
83 {PLIST_CHMOD, "@chmod %s", "\tCHMOD to %s"},
84 {PLIST_CHOWN, "@chown %s", "\tCHOWN to %s"},
85 {PLIST_CHGRP, "@chgrp %s", "\tCHGRP to %s"},
86 {PLIST_COMMENT, "@comment %s", "\tComment: %s"},
87 {PLIST_IGNORE, "@ignore", "Ignore next file:"},
88 {PLIST_NAME, "@name %s", "\tPackage name: %s"},
89 {PLIST_UNEXEC, "@unexec %s", "\tUNEXEC '%s'"},
90 {PLIST_SRC, "@src: %s", "\tSRC to: %s"},
91 {PLIST_DISPLAY, "@display %s", "\tInstall message file: %s"},
92 {PLIST_PKGDEP, "@pkgdep %s", "\tPackage depends on: %s"},
93 {PLIST_DIR_RM, "@dirrm %s", "\tObsolete deinstall directory removal hint: %s"},
94 {PLIST_OPTION, "@option %s", "\tPackage has option: %s"},
95 {PLIST_PKGCFL, "@pkgcfl %s", "\tPackage conflicts with: %s"},
96 {PLIST_BLDDEP, "@blddep %s", "\tPackage depends exactly on: %s"},
97 {PLIST_PKGDIR, "@pkgdir %s", "\tManaged directory: %s"},
98 {-1, NULL, NULL}
101 static int print_string_as_var(const char *, const char *);
103 void
104 show_file(const char *buf, const char *title, Boolean separator)
106 size_t len;
108 if (!Quiet)
109 printf("%s%s", InfoPrefix, title);
111 len = strlen(buf);
112 if (len == 0 || buf[len - 1] != '\n')
113 puts(buf);
114 else
115 fputs(buf, stdout);
117 if (!Quiet || separator)
118 printf("\n");
121 void
122 show_var(const char *buf, const char *variable)
124 char *value;
126 if (buf == NULL)
127 return;
129 if ((value = var_get_memory(buf, variable)) != NULL) {
130 (void) printf("%s\n", value);
131 free(value);
135 void
136 show_index(const char *buf, const char *title)
138 size_t len;
140 if (!Quiet)
141 printf("%s%s", InfoPrefix, title);
143 len = strlen(buf);
144 if (len == 0 || buf[len - 1] != '\n')
145 puts(buf);
146 else
147 fputs(buf, stdout);
151 * Show a packing list item type. If type is PLIST_SHOW_ALL, show all
153 void
154 show_plist(const char *title, package_t *plist, pl_ent_t type)
156 plist_t *p;
157 Boolean ign;
159 if (!Quiet) {
160 printf("%s%s", InfoPrefix, title);
162 for (ign = FALSE, p = plist->head; p; p = p->next) {
163 if (p->type == type || type == PLIST_SHOW_ALL) {
164 switch (p->type) {
165 case PLIST_FILE:
166 printf(Quiet ? showv[p->type].sh_quiet : showv[p->type].sh_verbose, p->name);
167 if (ign) {
168 if (!Quiet) {
169 printf(" (ignored)");
171 ign = FALSE;
173 break;
174 case PLIST_CHMOD:
175 case PLIST_CHOWN:
176 case PLIST_CHGRP:
177 printf(Quiet ? showv[p->type].sh_quiet : showv[p->type].sh_verbose,
178 p->name ? p->name : "(clear default)");
179 break;
180 case PLIST_IGNORE:
181 printf(Quiet ? showv[p->type].sh_quiet : showv[p->type].sh_verbose);
182 ign = TRUE;
183 break;
184 case PLIST_CWD:
185 case PLIST_CMD:
186 case PLIST_SRC:
187 case PLIST_UNEXEC:
188 case PLIST_COMMENT:
189 case PLIST_NAME:
190 case PLIST_DISPLAY:
191 case PLIST_PKGDEP:
192 case PLIST_DIR_RM:
193 case PLIST_OPTION:
194 case PLIST_PKGCFL:
195 case PLIST_BLDDEP:
196 case PLIST_PKGDIR:
197 printf(Quiet ? showv[p->type].sh_quiet : showv[p->type].sh_verbose, p->name);
198 break;
199 default:
200 warnx("unknown command type %d (%s)", p->type, p->name);
202 (void) fputc('\n', stdout);
208 * Show all files in the packing list (except ignored ones)
210 void
211 show_files(const char *title, package_t *plist)
213 plist_t *p;
214 Boolean ign;
215 const char *dir = ".";
217 if (!Quiet) {
218 printf("%s%s", InfoPrefix, title);
220 for (ign = FALSE, p = plist->head; p; p = p->next) {
221 switch (p->type) {
222 case PLIST_FILE:
223 if (!ign) {
224 printf("%s%s%s\n", dir,
225 (strcmp(dir, "/") == 0) ? "" : "/", p->name);
227 ign = FALSE;
228 break;
229 case PLIST_CWD:
230 dir = p->name;
231 break;
232 case PLIST_IGNORE:
233 ign = TRUE;
234 break;
235 default:
236 break;
242 * Show dependencies (packages this pkg requires)
244 void
245 show_depends(const char *title, package_t *plist)
247 plist_t *p;
248 int nodepends;
250 nodepends = 1;
251 for (p = plist->head; p && nodepends; p = p->next) {
252 switch (p->type) {
253 case PLIST_PKGDEP:
254 nodepends = 0;
255 break;
256 default:
257 break;
260 if (nodepends)
261 return;
263 if (!Quiet) {
264 printf("%s%s", InfoPrefix, title);
266 for (p = plist->head; p; p = p->next) {
267 switch (p->type) {
268 case PLIST_PKGDEP:
269 printf("%s\n", p->name);
270 break;
271 default:
272 break;
276 printf("\n");
280 * Show exact dependencies (packages this pkg was built with)
282 void
283 show_bld_depends(const char *title, package_t *plist)
285 plist_t *p;
286 int nodepends;
288 nodepends = 1;
289 for (p = plist->head; p && nodepends; p = p->next) {
290 switch (p->type) {
291 case PLIST_BLDDEP:
292 nodepends = 0;
293 break;
294 default:
295 break;
298 if (nodepends)
299 return;
301 if (!Quiet) {
302 printf("%s%s", InfoPrefix, title);
304 for (p = plist->head; p; p = p->next) {
305 switch (p->type) {
306 case PLIST_BLDDEP:
307 printf("%s\n", p->name);
308 break;
309 default:
310 break;
314 printf("\n");
319 * Show entry for pkg_summary.txt file.
321 void
322 show_summary(struct pkg_meta *meta, package_t *plist, const char *binpkgfile)
324 static const char *bi_vars[] = {
325 "PKGPATH",
326 "CATEGORIES",
327 "PROVIDES",
328 "REQUIRES",
329 "PKG_OPTIONS",
330 "OPSYS",
331 "OS_VERSION",
332 "MACHINE_ARCH",
333 "LICENSE",
334 "HOMEPAGE",
335 "PKGTOOLS_VERSION",
336 "BUILD_DATE",
337 "PREV_PKGPATH",
338 "SUPERSEDES",
339 NULL
342 plist_t *p;
343 struct stat st;
345 for (p = plist->head; p; p = p->next) {
346 switch (p->type) {
347 case PLIST_NAME:
348 printf("PKGNAME=%s\n", p->name);
349 break;
350 case PLIST_PKGDEP:
351 printf("DEPENDS=%s\n", p->name);
352 break;
353 case PLIST_PKGCFL:
354 printf("CONFLICTS=%s\n", p->name);
355 break;
357 default:
358 break;
362 print_string_as_var("COMMENT", meta->meta_comment);
363 print_string_as_var("SIZE_PKG", meta->meta_size_pkg);
365 if (meta->meta_build_info)
366 var_copy_list(meta->meta_build_info, bi_vars);
367 else
368 warnx("Build information missing");
370 if (binpkgfile != NULL && stat(binpkgfile, &st) == 0) {
371 const char *base;
373 base = strrchr(binpkgfile, '/');
374 if (base == NULL)
375 base = binpkgfile;
376 else
377 base++;
378 printf("FILE_NAME=%s\n", base);
379 printf("FILE_SIZE=%" MY_PRIu64 "\n", (uint64_t)st.st_size);
380 /* XXX: DIGETS */
383 print_string_as_var("DESCRIPTION", meta->meta_desc);
384 putc('\n', stdout);
388 * Print the contents of file fname as value of variable var to stdout.
390 static int
391 print_string_as_var(const char *var, const char *str)
393 const char *eol;
395 while ((eol = strchr(str, '\n')) != NULL) {
396 printf("%s=%.*s\n", var, (int)(eol - str), str);
397 str = eol + 1;
399 if (*str)
400 printf("%s=%s\n", var, str);
402 return 0;
405 void
406 show_list(lpkg_head_t *pkghead, const char *title)
408 lpkg_t *lpp;
410 if (!Quiet)
411 printf("%s%s", InfoPrefix, title);
413 while ((lpp = TAILQ_FIRST(pkghead)) != NULL) {
414 TAILQ_REMOVE(pkghead, lpp, lp_link);
415 puts(lpp->lp_name);
416 free_lpkg(lpp);
419 if (!Quiet)
420 printf("\n");