Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / info / main.c
blob1ca01ce00cb650fa304b72cff5e00d8616d23388
1 /* $NetBSD: main.c,v 1.28 2009/09/11 18:00:13 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: main.c,v 1.28 2009/09/11 18:00:13 joerg Exp $");
14 * FreeBSD install - a package for the installation and maintainance
15 * of non-core utilities.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * Jordan K. Hubbard
27 * 18 July 1993
29 * This is the add module.
33 #if HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h>
35 #endif
37 #if HAVE_ERR_H
38 #include <err.h>
39 #endif
41 #include "lib.h"
42 #include "info.h"
44 static const char Options[] = ".aBbcDde:E:fFhIiK:kLl:mNnpQ:qrRsSuvVX";
46 int Flags = 0;
47 enum which Which = WHICH_LIST;
48 Boolean File2Pkg = FALSE;
49 Boolean Quiet = FALSE;
50 const char *InfoPrefix = "";
51 const char *BuildInfoVariable = "";
52 lpkg_head_t pkgs;
54 static void
55 usage(void)
57 fprintf(stderr, "%s\n%s\n%s\n%s\n",
58 "usage: pkg_info [-BbcDdFfhIikLmNnpqrRSsVvX] [-e package] [-E package]",
59 " [-K pkg_dbdir] [-l prefix] pkg-name ...",
60 " pkg_info [-a | -u] [flags]",
61 " pkg_info [-Q variable] pkg-name ...");
62 exit(1);
65 int
66 main(int argc, char **argv)
68 char *CheckPkg = NULL;
69 char *BestCheckPkg = NULL;
70 lpkg_t *lpp;
71 int ch;
72 int rc;
74 setprogname(argv[0]);
75 while ((ch = getopt(argc, argv, Options)) != -1)
76 switch (ch) {
77 case '.': /* for backward compatibility */
78 break;
80 case 'a':
81 Which = WHICH_ALL;
82 break;
84 case 'B':
85 Flags |= SHOW_BUILD_INFO;
86 break;
88 case 'b':
89 Flags |= SHOW_BUILD_VERSION;
90 break;
92 case 'c':
93 Flags |= SHOW_COMMENT;
94 break;
96 case 'D':
97 Flags |= SHOW_DISPLAY;
98 break;
100 case 'd':
101 Flags |= SHOW_DESC;
102 break;
104 case 'E':
105 BestCheckPkg = optarg;
106 break;
108 case 'e':
109 CheckPkg = optarg;
110 break;
112 case 'f':
113 Flags |= SHOW_PLIST;
114 break;
116 case 'F':
117 File2Pkg = 1;
118 break;
120 case 'I':
121 Flags |= SHOW_INDEX;
122 break;
124 case 'i':
125 Flags |= SHOW_INSTALL;
126 break;
128 case 'K':
129 _pkgdb_setPKGDB_DIR(optarg);
130 break;
132 case 'k':
133 Flags |= SHOW_DEINSTALL;
134 break;
136 case 'L':
137 Flags |= SHOW_FILES;
138 break;
140 case 'l':
141 InfoPrefix = optarg;
142 break;
144 case 'm':
145 Flags |= SHOW_MTREE;
146 break;
148 case 'N':
149 Flags |= SHOW_BLD_DEPENDS;
150 break;
152 case 'n':
153 Flags |= SHOW_DEPENDS;
154 break;
156 case 'p':
157 Flags |= SHOW_PREFIX;
158 break;
160 case 'Q':
161 Flags |= SHOW_BI_VAR;
162 BuildInfoVariable = optarg;
163 break;
165 case 'q':
166 Quiet = TRUE;
167 break;
169 case 'r':
170 Flags |= SHOW_FULL_REQBY;
171 break;
173 case 'R':
174 Flags |= SHOW_REQBY;
175 break;
177 case 's':
178 Flags |= SHOW_PKG_SIZE;
179 break;
181 case 'S':
182 Flags |= SHOW_ALL_SIZE;
183 break;
185 case 'u':
186 Which = WHICH_USER;
187 break;
189 case 'v':
190 Verbose = TRUE;
191 /* Reasonable definition of 'everything' */
192 Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
193 SHOW_DEINSTALL | SHOW_DISPLAY | SHOW_MTREE |
194 SHOW_REQBY | SHOW_BLD_DEPENDS | SHOW_DEPENDS | SHOW_PKG_SIZE | SHOW_ALL_SIZE;
195 break;
197 case 'V':
198 show_version();
199 /* NOTREACHED */
201 case 'X':
202 Flags |= SHOW_SUMMARY;
203 break;
205 case 'h':
206 case '?':
207 default:
208 usage();
209 /* NOTREACHED */
212 argc -= optind;
213 argv += optind;
215 if (argc == 0 && !Flags && !CheckPkg) {
216 /* No argument or relevant flags specified - assume -I */
217 Flags = SHOW_INDEX;
218 /* assume -a if neither -u nor -a is given */
219 if (Which == WHICH_LIST)
220 Which = WHICH_ALL;
223 if (CheckPkg != NULL && BestCheckPkg != NULL) {
224 warnx("-E and -e are mutally exlusive");
225 usage();
228 if (argc != 0 && CheckPkg != NULL) {
229 warnx("can't give any additional arguments to -e");
230 usage();
233 if (argc != 0 && BestCheckPkg != NULL) {
234 warnx("can't give any additional arguments to -E");
235 usage();
238 if (argc != 0 && Which != WHICH_LIST) {
239 warnx("can't use both -a/-u and package name");
240 usage();
243 /* Set some reasonable defaults */
244 if (!Flags)
245 Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY
246 | SHOW_DEPENDS | SHOW_DISPLAY;
248 /* -Fe /filename -> change CheckPkg to real packagename */
249 if (CheckPkg) {
250 if (File2Pkg) {
251 char *s;
253 if (!pkgdb_open(ReadOnly))
254 err(EXIT_FAILURE, "cannot open pkgdb");
256 s = pkgdb_retrieve(CheckPkg);
258 if (s == NULL)
259 errx(EXIT_FAILURE, "No matching pkg for %s.", CheckPkg);
260 CheckPkg = xstrdup(s);
262 pkgdb_close();
264 return CheckForPkg(CheckPkg);
267 if (BestCheckPkg)
268 return CheckForBestPkg(BestCheckPkg);
270 TAILQ_INIT(&pkgs);
272 /* Get all the remaining package names, if any */
273 if (File2Pkg && Which == WHICH_LIST)
274 if (!pkgdb_open(ReadOnly)) {
275 err(EXIT_FAILURE, "cannot open pkgdb");
277 while (*argv) {
278 /* pkgdb: if -F flag given, don't add pkgnames to the "pkgs"
279 * queue but rather resolve the given filenames to pkgnames
280 * using pkgdb_retrieve, then add them. */
281 if (File2Pkg) {
282 char *s;
284 s = pkgdb_retrieve(*argv);
286 if (s) {
287 lpp = alloc_lpkg(s);
288 TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link);
289 } else
290 errx(EXIT_FAILURE, "No matching pkg for %s.", *argv);
291 } else {
292 if (ispkgpattern(*argv)) {
293 switch (add_installed_pkgs_by_pattern(*argv, &pkgs)) {
294 case 0:
295 errx(EXIT_FAILURE, "No matching pkg for %s.", *argv);
296 case -1:
297 errx(EXIT_FAILURE, "Error during search in pkgdb for %s", *argv);
299 } else {
300 const char *dbdir;
302 dbdir = _pkgdb_getPKGDB_DIR();
303 if (**argv == '/' && strncmp(*argv, dbdir, strlen(dbdir)) == 0) {
304 *argv += strlen(dbdir) + 1;
305 if ((*argv)[strlen(*argv) - 1] == '/') {
306 (*argv)[strlen(*argv) - 1] = 0;
309 lpp = alloc_lpkg(*argv);
310 TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link);
313 argv++;
316 if (File2Pkg)
317 pkgdb_close();
319 /* If no packages, yelp */
320 if (TAILQ_FIRST(&pkgs) == NULL && Which == WHICH_LIST && !CheckPkg)
321 warnx("missing package name(s)"), usage();
323 rc = pkg_perform(&pkgs);
324 exit(rc);
325 /* NOTREACHED */