main.c revision 84745
1251018Sgonzo/*
2251018Sgonzo *
3251018Sgonzo * FreeBSD install - a package for the installation and maintainance
4251018Sgonzo * of non-core utilities.
5251018Sgonzo *
6251018Sgonzo * Redistribution and use in source and binary forms, with or without
7251018Sgonzo * modification, are permitted provided that the following conditions
8251018Sgonzo * are met:
9251018Sgonzo * 1. Redistributions of source code must retain the above copyright
10251018Sgonzo *    notice, this list of conditions and the following disclaimer.
11251018Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12251018Sgonzo *    notice, this list of conditions and the following disclaimer in the
13251018Sgonzo *    documentation and/or other materials provided with the distribution.
14251018Sgonzo *
15251018Sgonzo * Jordan K. Hubbard
16251018Sgonzo * 18 July 1993
17251018Sgonzo *
18251018Sgonzo * This is the info module.
19251018Sgonzo *
20251018Sgonzo */
21251018Sgonzo
22251018Sgonzo#include "lib.h"
23251018Sgonzo#include "info.h"
24251018Sgonzo#include <err.h>
25251018Sgonzo
26251018Sgonzo#ifndef lint
27251018Sgonzostatic const char rcsid[] =
28251018Sgonzo  "$FreeBSD: head/usr.sbin/pkg_install/info/main.c 84745 2001-10-10 06:58:42Z sobomax $";
29251018Sgonzo#endif
30251018Sgonzo
31251018Sgonzostatic char Options[] = "acdDe:fgGhiIkl:LmopqrRst:vW:x";
32251018Sgonzo
33251018Sgonzoint	Flags		= 0;
34251018Sgonzomatch_t	MatchType	= MATCH_GLOB;
35251018SgonzoBoolean Quiet		= FALSE;
36251018Sgonzochar *InfoPrefix	= (char *)(uintptr_t)"";
37251018Sgonzochar PlayPen[FILENAME_MAX];
38251018Sgonzochar *CheckPkg		= NULL;
39251018Sgonzostruct which_head *whead;
40251018Sgonzo
41251018Sgonzostatic void usage __P((void));
42251018Sgonzo
43251018Sgonzoint
44251018Sgonzomain(int argc, char **argv)
45252282Sgonzo{
46252282Sgonzo    int ch;
47252282Sgonzo    char **pkgs, **start;
48252282Sgonzo    char *pkgs_split;
49251018Sgonzo
50251018Sgonzo    whead = malloc(sizeof(struct which_head));
51251018Sgonzo    if (whead == NULL)
52251018Sgonzo	err(2, NULL);
53251018Sgonzo    TAILQ_INIT(whead);
54251018Sgonzo
55251018Sgonzo    pkgs = start = argv;
56252282Sgonzo    if (argc == 1) {
57252282Sgonzo	MatchType = MATCH_ALL;
58252282Sgonzo	Flags = SHOW_INDEX;
59251018Sgonzo    }
60251018Sgonzo    else while ((ch = getopt(argc, argv, Options)) != -1) {
61251018Sgonzo	switch(ch) {
62251018Sgonzo	case 'a':
63251018Sgonzo	    MatchType = MATCH_ALL;
64251018Sgonzo	    break;
65251018Sgonzo
66251018Sgonzo	case 'v':
67251018Sgonzo	    Verbose = TRUE;
68251018Sgonzo	    /* Reasonable definition of 'everything' */
69251018Sgonzo	    Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
70251018Sgonzo		SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE;
71251018Sgonzo	    break;
72251018Sgonzo
73251018Sgonzo	case 'I':
74251018Sgonzo	    Flags |= SHOW_INDEX;
75251018Sgonzo	    break;
76251018Sgonzo
77251018Sgonzo	case 'p':
78251018Sgonzo	    Flags |= SHOW_PREFIX;
79251018Sgonzo	    break;
80251018Sgonzo
81251018Sgonzo	case 'c':
82251018Sgonzo	    Flags |= SHOW_COMMENT;
83251018Sgonzo	    break;
84251018Sgonzo
85251018Sgonzo	case 'd':
86251018Sgonzo	    Flags |= SHOW_DESC;
87251018Sgonzo	    break;
88251018Sgonzo
89251018Sgonzo	case 'D':
90251018Sgonzo	    Flags |= SHOW_DISPLAY;
91251018Sgonzo	    break;
92251018Sgonzo
93251018Sgonzo	case 'f':
94251018Sgonzo	    Flags |= SHOW_PLIST;
95251018Sgonzo	    break;
96251018Sgonzo
97251018Sgonzo	case 'g':
98251018Sgonzo	    Flags |= SHOW_CKSUM;
99251018Sgonzo	    break;
100251018Sgonzo
101251018Sgonzo	case 'G':
102251018Sgonzo	    MatchType = MATCH_EXACT;
103251018Sgonzo	    break;
104251018Sgonzo
105251018Sgonzo	case 'i':
106251018Sgonzo	    Flags |= SHOW_INSTALL;
107251018Sgonzo	    break;
108251018Sgonzo
109251018Sgonzo	case 'k':
110251018Sgonzo	    Flags |= SHOW_DEINSTALL;
111251018Sgonzo	    break;
112251018Sgonzo
113251018Sgonzo	case 'r':
114251018Sgonzo	    Flags |= SHOW_REQUIRE;
115251018Sgonzo	    break;
116251018Sgonzo
117251018Sgonzo	case 'R':
118251018Sgonzo	    Flags |= SHOW_REQBY;
119251018Sgonzo	    break;
120251018Sgonzo
121251018Sgonzo	case 'L':
122251018Sgonzo	    Flags |= SHOW_FILES;
123251018Sgonzo	    break;
124251018Sgonzo
125251018Sgonzo	case 'm':
126251018Sgonzo	    Flags |= SHOW_MTREE;
127251018Sgonzo	    break;
128251018Sgonzo
129251018Sgonzo	case 's':
130251018Sgonzo	    Flags |= SHOW_SIZE;
131251018Sgonzo	    break;
132251018Sgonzo
133251018Sgonzo	case 'o':
134251018Sgonzo	    Flags |= SHOW_ORIGIN;
135251018Sgonzo	    break;
136251018Sgonzo
137251018Sgonzo	case 'l':
138251018Sgonzo	    InfoPrefix = optarg;
139251018Sgonzo	    break;
140251018Sgonzo
141251018Sgonzo	case 'q':
142251018Sgonzo	    Quiet = TRUE;
143251018Sgonzo	    break;
144251018Sgonzo
145251018Sgonzo	case 't':
146251018Sgonzo	    strcpy(PlayPen, optarg);
147251018Sgonzo	    break;
148251018Sgonzo
149251018Sgonzo	case 'x':
150251018Sgonzo	    MatchType = MATCH_REGEX;
151251018Sgonzo	    break;
152251018Sgonzo
153277405Sgonzo	case 'e':
154251018Sgonzo	    CheckPkg = optarg;
155251018Sgonzo	    break;
156251018Sgonzo
157251018Sgonzo	case 'W':
158251018Sgonzo	    {
159251018Sgonzo		struct which_entry *entp;
160251018Sgonzo
161251018Sgonzo		entp = calloc(1, sizeof(struct which_entry));
162251018Sgonzo		if (entp == NULL)
163251018Sgonzo		    err(2, NULL);
164251018Sgonzo
165251018Sgonzo		strlcpy(entp->file, optarg, PATH_MAX);
166251018Sgonzo		entp->skip = FALSE;
167251018Sgonzo		TAILQ_INSERT_TAIL(whead, entp, next);
168251018Sgonzo		break;
169251018Sgonzo	    }
170251018Sgonzo
171251018Sgonzo	case 'h':
172251018Sgonzo	case '?':
173251018Sgonzo	default:
174251018Sgonzo	    usage();
175251018Sgonzo	    break;
176251018Sgonzo	}
177251018Sgonzo    }
178251018Sgonzo
179251018Sgonzo    argc -= optind;
180251018Sgonzo    argv += optind;
181251018Sgonzo
182251018Sgonzo    /* Set some reasonable defaults */
183251018Sgonzo    if (!Flags)
184251018Sgonzo	Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY;
185251018Sgonzo
186251018Sgonzo    /* Get all the remaining package names, if any */
187251018Sgonzo    while (*argv) {
188251018Sgonzo	/* Don't try to apply heuristics if arguments are regexs */
189251018Sgonzo	if (MatchType != MATCH_REGEX)
190251018Sgonzo	    while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
191251018Sgonzo		*pkgs_split++ = '\0';
192251018Sgonzo		/*
193251018Sgonzo		 * If character after the '/' is alphanumeric or shell
194251018Sgonzo		 * metachar, then we've found the package name.  Otherwise
195251018Sgonzo		 * we've come across a trailing '/' and need to continue our
196251018Sgonzo		 * quest.
197251018Sgonzo		 */
198251018Sgonzo		if (isalpha(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
199251018Sgonzo		    strpbrk(pkgs_split, "*?[]") != NULL)) {
200251018Sgonzo		    *argv = pkgs_split;
201251018Sgonzo		    break;
202251018Sgonzo		}
203251018Sgonzo	    }
204251018Sgonzo	*pkgs++ = *argv++;
205251018Sgonzo    }
206251018Sgonzo
207251018Sgonzo    /* If no packages, yelp */
208251018Sgonzo    if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg &&
209251018Sgonzo	TAILQ_EMPTY(whead))
210251018Sgonzo	warnx("missing package name(s)"), usage();
211251018Sgonzo    *pkgs = NULL;
212251018Sgonzo    return pkg_perform(start);
213251018Sgonzo}
214251018Sgonzo
215251018Sgonzostatic void
216251018Sgonzousage()
217251018Sgonzo{
218251018Sgonzo    fprintf(stderr, "%s\n%s\n%s\n",
219251018Sgonzo	"usage: pkg_info [-cdDfGiIkLmopqrRsvx] [-e package] [-l prefix]",
220251018Sgonzo	"                [-t template] [-W filename] [pkg-name ...]",
221251018Sgonzo	"       pkg_info -a [flags]");
222251018Sgonzo    exit(1);
223251018Sgonzo}
224251018Sgonzo