main.c revision 173412
1/*
2 *
3 * FreeBSD install - a package for the installation and maintainance
4 * of non-core utilities.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * Jordan K. Hubbard
16 * 18 July 1993
17 *
18 * This is the info module.
19 *
20 */
21
22#include <sys/cdefs.h>
23__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/info/main.c 173412 2007-11-07 10:53:41Z kevlo $");
24
25#include "lib.h"
26#include "info.h"
27#include <err.h>
28
29static char Options[] = "abcdDe:EfgGhiIjkKl:LmoO:pPqQrRst:vVW:xX";
30
31int	Flags		= 0;
32match_t	MatchType	= MATCH_GLOB;
33Boolean QUIET		= FALSE;
34Boolean UseBlkSz	= FALSE;
35char *InfoPrefix	= (char *)(uintptr_t)"";
36char PlayPen[FILENAME_MAX];
37char *CheckPkg		= NULL;
38char *LookUpOrigin	= NULL;
39Boolean KeepPackage	= FALSE;
40struct which_head *whead;
41
42static void usage(void);
43
44int
45main(int argc, char **argv)
46{
47    int ch;
48    char **pkgs, **start;
49    char *pkgs_split;
50
51    whead = malloc(sizeof(struct which_head));
52    if (whead == NULL)
53	err(2, NULL);
54    TAILQ_INIT(whead);
55
56    pkgs = start = argv;
57    if (argc == 1) {
58	MatchType = MATCH_ALL;
59	Flags = SHOW_INDEX;
60    }
61    else while ((ch = getopt(argc, argv, Options)) != -1) {
62	switch(ch) {
63	case 'a':
64	    MatchType = MATCH_ALL;
65	    break;
66
67	case 'b':
68	    UseBlkSz = TRUE;
69	    break;
70
71	case 'v':
72	    Verbose++;
73	    /* Reasonable definition of 'everything' */
74	    Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
75		SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE;
76	    break;
77
78	case 'E':
79	    Flags |= SHOW_PKGNAME;
80	    break;
81
82	case 'I':
83	    Flags |= SHOW_INDEX;
84	    break;
85
86	case 'p':
87	    Flags |= SHOW_PREFIX;
88	    break;
89
90	case 'c':
91	    Flags |= SHOW_COMMENT;
92	    break;
93
94	case 'd':
95	    Flags |= SHOW_DESC;
96	    break;
97
98	case 'D':
99	    Flags |= SHOW_DISPLAY;
100	    break;
101
102	case 'f':
103	    Flags |= SHOW_PLIST;
104	    break;
105
106	case 'g':
107	    Flags |= SHOW_CKSUM;
108	    break;
109
110	case 'G':
111	    MatchType = MATCH_EXACT;
112	    break;
113
114	case 'i':
115	    Flags |= SHOW_INSTALL;
116	    break;
117
118	case 'j':
119	    Flags |= SHOW_REQUIRE;
120	    break;
121
122	case 'k':
123	    Flags |= SHOW_DEINSTALL;
124	    break;
125
126	case 'K':
127	    KeepPackage = TRUE;
128	    break;
129
130	case 'r':
131	    Flags |= SHOW_DEPEND;
132	    break;
133
134	case 'R':
135	    Flags |= SHOW_REQBY;
136	    break;
137
138	case 'L':
139	    Flags |= SHOW_FILES;
140	    break;
141
142	case 'm':
143	    Flags |= SHOW_MTREE;
144	    break;
145
146	case 's':
147	    Flags |= SHOW_SIZE;
148	    break;
149
150	case 'o':
151	    Flags |= SHOW_ORIGIN;
152	    break;
153
154	case 'O':
155	    LookUpOrigin = strdup(optarg);
156	    if (LookUpOrigin == NULL)
157		err(2, NULL);
158	    break;
159
160	case 'V':
161	    Flags |= SHOW_FMTREV;
162	    break;
163
164	case 'l':
165	    InfoPrefix = optarg;
166	    break;
167
168	case 'q':
169	    Quiet = TRUE;
170	    break;
171
172	case 'Q':
173	    Quiet = TRUE;
174	    QUIET = TRUE;
175	    break;
176
177	case 't':
178	    strlcpy(PlayPen, optarg, sizeof(PlayPen));
179	    break;
180
181	case 'x':
182	    MatchType = MATCH_REGEX;
183	    break;
184
185	case 'X':
186	    MatchType = MATCH_EREGEX;
187	    break;
188
189	case 'e':
190	    CheckPkg = optarg;
191	    break;
192
193	case 'W':
194	    {
195		struct which_entry *entp;
196
197		entp = calloc(1, sizeof(struct which_entry));
198		if (entp == NULL)
199		    err(2, NULL);
200
201		strlcpy(entp->file, optarg, PATH_MAX);
202		entp->skip = FALSE;
203		TAILQ_INSERT_TAIL(whead, entp, next);
204		break;
205	    }
206
207	case 'P':
208	    Flags = SHOW_PTREV;
209	    break;
210
211	case 'h':
212	case '?':
213	default:
214	    usage();
215	    break;
216	}
217    }
218
219    argc -= optind;
220    argv += optind;
221
222    if (Flags & SHOW_PTREV) {
223	if (!Quiet)
224	    printf("Package tools revision: ");
225	printf("%d\n", PKG_INSTALL_VERSION);
226	exit(0);
227    }
228
229    /* Set some reasonable defaults */
230    if (!Flags)
231	Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY;
232
233    /* Get all the remaining package names, if any */
234    while (*argv) {
235	/*
236	 * Don't try to apply heuristics if arguments are regexs or if
237	 * the argument refers to an existing file.
238	 */
239	if (MatchType != MATCH_REGEX && MatchType != MATCH_EREGEX && !isfile(*argv))
240	    while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
241		*pkgs_split++ = '\0';
242		/*
243		 * If character after the '/' is alphanumeric or shell
244		 * metachar, then we've found the package name.  Otherwise
245		 * we've come across a trailing '/' and need to continue our
246		 * quest.
247		 */
248		if (isalnum(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
249		    strpbrk(pkgs_split, "*?[]") != NULL)) {
250		    *argv = pkgs_split;
251		    break;
252		}
253	    }
254	*pkgs++ = *argv++;
255    }
256
257    /* If no packages, yelp */
258    if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg &&
259	TAILQ_EMPTY(whead) && LookUpOrigin == NULL)
260	warnx("missing package name(s)"), usage();
261    *pkgs = NULL;
262    return pkg_perform(start);
263}
264
265static void
266usage()
267{
268    fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
269	"usage: pkg_info [-bcdDEfgGiIjkKLmopPqQrRsvVxX] [-e package] [-l prefix]",
270	"                [-t template] -a | pkg-name ...",
271	"       pkg_info [-qQ] -W filename",
272	"       pkg_info [-qQ] -O origin",
273	"       pkg_info");
274    exit(1);
275}
276