1327Sjkh/*
2327Sjkh *
3327Sjkh * FreeBSD install - a package for the installation and maintainance
4327Sjkh * of non-core utilities.
5327Sjkh *
6327Sjkh * Redistribution and use in source and binary forms, with or without
7327Sjkh * modification, are permitted provided that the following conditions
8327Sjkh * are met:
9327Sjkh * 1. Redistributions of source code must retain the above copyright
10327Sjkh *    notice, this list of conditions and the following disclaimer.
11327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
12327Sjkh *    notice, this list of conditions and the following disclaimer in the
13327Sjkh *    documentation and/or other materials provided with the distribution.
14327Sjkh *
15327Sjkh * Jordan K. Hubbard
16327Sjkh * 18 July 1993
17327Sjkh *
1831997Shoek * This is the info module.
19327Sjkh *
20327Sjkh */
21327Sjkh
2293520Sobrien#include <sys/cdefs.h>
2393520Sobrien__FBSDID("$FreeBSD$");
2493520Sobrien
25179433Sflz#include <getopt.h>
26179433Sflz#include <err.h>
27179433Sflz
28222035Sflz#include "lib.h"
29327Sjkh#include "info.h"
30327Sjkh
31327Sjkhint	Flags		= 0;
3272174Ssobomaxmatch_t	MatchType	= MATCH_GLOB;
33112572SmdoddBoolean QUIET		= FALSE;
34112579SmdoddBoolean UseBlkSz	= FALSE;
3584745Ssobomaxchar *InfoPrefix	= (char *)(uintptr_t)"";
3611780Sjkhchar PlayPen[FILENAME_MAX];
37392Sjkhchar *CheckPkg		= NULL;
3896030Ssobomaxchar *LookUpOrigin	= NULL;
39154145SflzBoolean KeepPackage	= FALSE;
4074699Ssobomaxstruct which_head *whead;
41327Sjkh
42173412Skevlostatic void usage(void);
4330221Scharnier
44179433Sflzstatic char opts[] = "abcdDe:EfgGhiIjkKl:LmoO:pPqQrRst:vVW:xX";
45179433Sflzstatic struct option longopts[] = {
46179433Sflz	{ "all",	no_argument,		NULL,		'a' },
47179433Sflz	{ "blocksize",	no_argument,		NULL,		'b' },
48179433Sflz	{ "exist",	required_argument,	NULL,		'X' },
49179433Sflz	{ "exists",	required_argument,	NULL,		'X' },
50179433Sflz	{ "extended",	no_argument,		NULL,		'e' },
51179433Sflz	{ "help",	no_argument,		NULL,		'h' },
52179433Sflz	{ "keep",	no_argument,		NULL,		'K' },
53179433Sflz	{ "no-glob",	no_argument,		NULL,		'G' },
54179433Sflz	{ "origin",	required_argument,	NULL,		'O' },
55179433Sflz	{ "quiet",	no_argument,		NULL,		'q' },
56179433Sflz	{ "regex",	no_argument,		NULL,		'x' },
57179433Sflz	{ "template",	required_argument,	NULL,		't' },
58179433Sflz	{ "verbose",	no_argument,		NULL,		'v' },
59179433Sflz	{ "version",	no_argument,		NULL,		'P' },
60179433Sflz	{ "which",	required_argument,	NULL,		'W' },
61193273Sjilles	{ NULL,		0,			NULL,		0 }
62179433Sflz};
63179433Sflz
64327Sjkhint
65327Sjkhmain(int argc, char **argv)
66327Sjkh{
67327Sjkh    int ch;
68327Sjkh    char **pkgs, **start;
6956001Sdan    char *pkgs_split;
70327Sjkh
7174699Ssobomax    whead = malloc(sizeof(struct which_head));
7274699Ssobomax    if (whead == NULL)
7374699Ssobomax	err(2, NULL);
7474699Ssobomax    TAILQ_INIT(whead);
7574699Ssobomax
76327Sjkh    pkgs = start = argv;
7746105Sjkh    if (argc == 1) {
7872174Ssobomax	MatchType = MATCH_ALL;
7946105Sjkh	Flags = SHOW_INDEX;
8046105Sjkh    }
81179433Sflz    else while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
82327Sjkh	switch(ch) {
83327Sjkh	case 'a':
8472174Ssobomax	    MatchType = MATCH_ALL;
85327Sjkh	    break;
86327Sjkh
87112579Smdodd	case 'b':
88112579Smdodd	    UseBlkSz = TRUE;
89112579Smdodd	    break;
90112579Smdodd
91327Sjkh	case 'v':
92159554Sobrien	    Verbose++;
93327Sjkh	    /* Reasonable definition of 'everything' */
94327Sjkh	    Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
954996Sjkh		SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE;
96327Sjkh	    break;
97327Sjkh
98131275Seik	case 'E':
99131275Seik	    Flags |= SHOW_PKGNAME;
100131275Seik	    break;
101131275Seik
102327Sjkh	case 'I':
103327Sjkh	    Flags |= SHOW_INDEX;
104327Sjkh	    break;
105327Sjkh
106327Sjkh	case 'p':
107327Sjkh	    Flags |= SHOW_PREFIX;
108327Sjkh	    break;
109327Sjkh
110327Sjkh	case 'c':
111327Sjkh	    Flags |= SHOW_COMMENT;
112327Sjkh	    break;
113327Sjkh
114327Sjkh	case 'd':
115327Sjkh	    Flags |= SHOW_DESC;
116327Sjkh	    break;
117327Sjkh
1184996Sjkh	case 'D':
1194996Sjkh	    Flags |= SHOW_DISPLAY;
1204996Sjkh	    break;
1214996Sjkh
122327Sjkh	case 'f':
123327Sjkh	    Flags |= SHOW_PLIST;
124327Sjkh	    break;
125327Sjkh
12671965Sjkh	case 'g':
12771965Sjkh	    Flags |= SHOW_CKSUM;
12871965Sjkh	    break;
12971965Sjkh
13072174Ssobomax	case 'G':
13172174Ssobomax	    MatchType = MATCH_EXACT;
13272174Ssobomax	    break;
13372174Ssobomax
134327Sjkh	case 'i':
135327Sjkh	    Flags |= SHOW_INSTALL;
136327Sjkh	    break;
137327Sjkh
138131280Seik	case 'j':
139131280Seik	    Flags |= SHOW_REQUIRE;
140131280Seik	    break;
141131280Seik
142327Sjkh	case 'k':
143327Sjkh	    Flags |= SHOW_DEINSTALL;
144327Sjkh	    break;
145327Sjkh
146154145Sflz	case 'K':
147154145Sflz	    KeepPackage = TRUE;
148154145Sflz	    break;
149154145Sflz
150327Sjkh	case 'r':
151131280Seik	    Flags |= SHOW_DEPEND;
152327Sjkh	    break;
153327Sjkh
1544996Sjkh	case 'R':
1554996Sjkh	    Flags |= SHOW_REQBY;
1564996Sjkh	    break;
1574996Sjkh
158411Sjkh	case 'L':
159411Sjkh	    Flags |= SHOW_FILES;
160411Sjkh	    break;
161411Sjkh
1624996Sjkh	case 'm':
1634996Sjkh	    Flags |= SHOW_MTREE;
1644996Sjkh	    break;
1654996Sjkh
16672174Ssobomax	case 's':
16772174Ssobomax	    Flags |= SHOW_SIZE;
16872174Ssobomax	    break;
16962775Ssobomax
17067454Ssobomax	case 'o':
17167454Ssobomax	    Flags |= SHOW_ORIGIN;
17267454Ssobomax	    break;
17367454Ssobomax
17496030Ssobomax	case 'O':
17596030Ssobomax	    LookUpOrigin = strdup(optarg);
17696030Ssobomax	    if (LookUpOrigin == NULL)
17796030Ssobomax		err(2, NULL);
17896030Ssobomax	    break;
17996030Ssobomax
18084750Ssobomax	case 'V':
18184750Ssobomax	    Flags |= SHOW_FMTREV;
18284750Ssobomax	    break;
18384750Ssobomax
184379Sjkh	case 'l':
185379Sjkh	    InfoPrefix = optarg;
186379Sjkh	    break;
187379Sjkh
188411Sjkh	case 'q':
189411Sjkh	    Quiet = TRUE;
190411Sjkh	    break;
191411Sjkh
192112572Smdodd	case 'Q':
193112572Smdodd	    Quiet = TRUE;
194112572Smdodd	    QUIET = TRUE;
195112572Smdodd	    break;
196112572Smdodd
197383Sjkh	case 't':
19885019Ssobomax	    strlcpy(PlayPen, optarg, sizeof(PlayPen));
199383Sjkh	    break;
200383Sjkh
20172174Ssobomax	case 'x':
20272174Ssobomax	    MatchType = MATCH_REGEX;
20372174Ssobomax	    break;
20472174Ssobomax
205131275Seik	case 'X':
206131275Seik	    MatchType = MATCH_EREGEX;
207131275Seik	    break;
208131275Seik
209392Sjkh	case 'e':
210392Sjkh	    CheckPkg = optarg;
211392Sjkh	    break;
212392Sjkh
21374699Ssobomax	case 'W':
21474699Ssobomax	    {
21574699Ssobomax		struct which_entry *entp;
21674699Ssobomax
21774699Ssobomax		entp = calloc(1, sizeof(struct which_entry));
21874699Ssobomax		if (entp == NULL)
21974699Ssobomax		    err(2, NULL);
22074699Ssobomax
22174699Ssobomax		strlcpy(entp->file, optarg, PATH_MAX);
22274699Ssobomax		entp->skip = FALSE;
22374699Ssobomax		TAILQ_INSERT_TAIL(whead, entp, next);
22474699Ssobomax		break;
22574699Ssobomax	    }
22674699Ssobomax
227103149Ssobomax	case 'P':
228222035Sflz	    Flags = SHOW_PTREV;
229103149Ssobomax	    break;
230103149Ssobomax
231327Sjkh	case 'h':
232327Sjkh	default:
23330221Scharnier	    usage();
234327Sjkh	    break;
235327Sjkh	}
23646105Sjkh    }
237327Sjkh
2388857Srgrimes    argc -= optind;
239327Sjkh    argv += optind;
240327Sjkh
241103149Ssobomax    if (Flags & SHOW_PTREV) {
242103149Ssobomax	if (!Quiet)
243103149Ssobomax	    printf("Package tools revision: ");
244103149Ssobomax	printf("%d\n", PKG_INSTALL_VERSION);
245103149Ssobomax	exit(0);
246103149Ssobomax    }
247103149Ssobomax
248327Sjkh    /* Set some reasonable defaults */
249327Sjkh    if (!Flags)
2504996Sjkh	Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY;
251327Sjkh
252327Sjkh    /* Get all the remaining package names, if any */
25360563Ssteve    while (*argv) {
25495934Ssobomax	/*
25595934Ssobomax	 * Don't try to apply heuristics if arguments are regexs or if
25695934Ssobomax	 * the argument refers to an existing file.
25795934Ssobomax	 */
258178246Sflz	if (MatchType != MATCH_REGEX && MatchType != MATCH_EREGEX && !isfile(*argv) && !isURL(*argv))
25972174Ssobomax	    while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
26072174Ssobomax		*pkgs_split++ = '\0';
26172174Ssobomax		/*
26272174Ssobomax		 * If character after the '/' is alphanumeric or shell
26372174Ssobomax		 * metachar, then we've found the package name.  Otherwise
26472174Ssobomax		 * we've come across a trailing '/' and need to continue our
26572174Ssobomax		 * quest.
26672174Ssobomax		 */
267151188Skrion		if (isalnum(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
26872174Ssobomax		    strpbrk(pkgs_split, "*?[]") != NULL)) {
26972174Ssobomax		    *argv = pkgs_split;
27072174Ssobomax		    break;
27172174Ssobomax		}
27260563Ssteve	    }
27360563Ssteve	*pkgs++ = *argv++;
27456001Sdan    }
275327Sjkh
276327Sjkh    /* If no packages, yelp */
27774699Ssobomax    if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg &&
27896030Ssobomax	TAILQ_EMPTY(whead) && LookUpOrigin == NULL)
27930221Scharnier	warnx("missing package name(s)"), usage();
280327Sjkh    *pkgs = NULL;
281327Sjkh    return pkg_perform(start);
282327Sjkh}
283327Sjkh
28430221Scharnierstatic void
285201226Sedusage(void)
286327Sjkh{
28796067Ssobomax    fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
288154145Sflz	"usage: pkg_info [-bcdDEfgGiIjkKLmopPqQrRsvVxX] [-e package] [-l prefix]",
289127641Scperciva	"                [-t template] -a | pkg-name ...",
290127641Scperciva	"       pkg_info [-qQ] -W filename",
291127641Scperciva	"       pkg_info [-qQ] -O origin",
292127641Scperciva	"       pkg_info");
293327Sjkh    exit(1);
294327Sjkh}
295