main.c revision 180361
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
5 * Jordan K. Hubbard
6 * 18 July 1993
7 *
8 * This is the create module.
9 *
10 */
11
12#include <sys/cdefs.h>
13__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/create/main.c 180361 2008-07-08 03:21:05Z keramida $");
14
15#include <getopt.h>
16#include <err.h>
17
18#include "lib.h"
19#include "create.h"
20
21match_t	MatchType	= MATCH_GLOB;
22char	*Prefix		= NULL;
23char	*Comment        = NULL;
24char	*Desc		= NULL;
25char	*SrcDir		= NULL;
26char	*BaseDir	= NULL;
27char	*Display	= NULL;
28char	*Install	= NULL;
29char	*PostInstall	= NULL;
30char	*DeInstall	= NULL;
31char	*PostDeInstall	= NULL;
32char	*Contents	= NULL;
33char	*Require	= NULL;
34char	*ExcludeFrom	= NULL;
35char	*Mtree		= NULL;
36char	*Pkgdeps	= NULL;
37char	*Conflicts	= NULL;
38char	*Origin		= NULL;
39char	*InstalledPkg	= NULL;
40char	PlayPen[FILENAME_MAX];
41int	Dereference	= FALSE;
42int	PlistOnly	= FALSE;
43int	Recursive	= FALSE;
44int	Regenerate	= TRUE;
45int	Help		= FALSE;
46enum zipper	Zipper  = BZIP2;
47
48
49static void usage(void);
50
51static char opts[] = "EGYNnORhjvxyzf:p:P:C:c:d:i:I:k:K:r:t:X:D:m:s:S:o:b:";
52static struct option longopts[] = {
53	{ "backup",	required_argument,	NULL,		'b' },
54	{ "extended",	no_argument,		NULL,		'E' },
55	{ "help",	no_argument,		&Help,		TRUE },
56	{ "no",		no_argument,		NULL,		'N' },
57	{ "no-glob",	no_argument,		NULL,		'G' },
58	{ "origin",	required_argument,	NULL,		'o' },
59	{ "plist-only",	no_argument,		NULL,		'O' },
60	{ "prefix",	required_argument,	NULL,		'p' },
61	{ "recursive",	no_argument,		NULL,		'R' },
62	{ "regex",	no_argument,		NULL,		'x' },
63	{ "template",	required_argument,	NULL,		't' },
64	{ "verbose",	no_argument,		NULL,		'v' },
65	{ "yes",	no_argument,		NULL,		'Y' },
66	{ NULL,		0,			NULL,		0 },
67};
68
69int
70main(int argc, char **argv)
71{
72    int ch;
73    char **pkgs, **start, *tmp;
74
75    pkgs = start = argv;
76    while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1)
77	switch(ch) {
78	case 'v':
79	    Verbose++;
80	    break;
81
82	case 'x':
83	    MatchType = MATCH_REGEX;
84	    break;
85
86	case 'E':
87	    MatchType = MATCH_EREGEX;
88	    break;
89
90	case 'G':
91	    MatchType = MATCH_EXACT;
92	    break;
93
94	case 'N':
95	    AutoAnswer = NO;
96	    break;
97
98	case 'Y':
99	    AutoAnswer = YES;
100	    break;
101
102	case 'O':
103	    PlistOnly = TRUE;
104	    break;
105
106	case 'p':
107	    Prefix = optarg;
108	    break;
109
110	case 's':
111	    SrcDir = optarg;
112	    break;
113
114	case 'S':
115	    BaseDir = optarg;
116	    break;
117
118	case 'f':
119	    Contents = optarg;
120	    break;
121
122	case 'C':
123	    Conflicts = optarg;
124	    break;
125
126	case 'c':
127	    Comment = optarg;
128	    break;
129
130	case 'd':
131	    Desc = optarg;
132	    break;
133
134	case 'i':
135	    Install = optarg;
136	    break;
137
138	case 'I':
139	    PostInstall = optarg;
140	    break;
141
142	case 'k':
143	    DeInstall = optarg;
144	    break;
145
146	case 'K':
147	    PostDeInstall = optarg;
148	    break;
149
150	case 'r':
151	    Require = optarg;
152	    break;
153
154	case 't':
155	    strlcpy(PlayPen, optarg, sizeof(PlayPen));
156	    break;
157
158	case 'X':
159	    ExcludeFrom = optarg;
160	    break;
161
162	case 'h':
163	    Dereference = TRUE;
164	    break;
165
166	case 'D':
167	    Display = optarg;
168	    break;
169
170	case 'm':
171	    Mtree = optarg;
172	    break;
173
174	case 'P':
175	    Pkgdeps = optarg;
176	    break;
177
178	case 'o':
179	    Origin = optarg;
180	    break;
181
182	case 'y':
183	case 'j':
184	    Zipper = BZIP2;
185	    break;
186
187	case 'z':
188	    Zipper = GZIP;
189	    break;
190
191	case 'b':
192	    InstalledPkg = optarg;
193	    while ((tmp = strrchr(optarg, (int)'/')) != NULL) {
194		*tmp++ = '\0';
195		/*
196		 * If character after the '/' is alphanumeric, then we've
197		 * found the package name.  Otherwise we've come across
198		 * a trailing '/' and need to continue our quest.
199		 */
200		if (isalpha(*tmp)) {
201		    InstalledPkg = tmp;
202		    break;
203		}
204	    }
205	    break;
206
207	case 'R':
208	    Recursive = TRUE;
209	    break;
210
211	case 'n':
212	    Regenerate = FALSE;
213	    break;
214
215	case 0:
216	    if (Help)
217		usage();
218	    break;
219
220	default:
221	    usage();
222	    break;
223	}
224
225    argc -= optind;
226    argv += optind;
227
228    /* Get all the remaining package names, if any */
229    while (*argv)
230	*pkgs++ = *argv++;
231
232    /* If no packages, yelp */
233    if ((pkgs == start) && (InstalledPkg == NULL))
234	warnx("missing package name"), usage();
235    *pkgs = NULL;
236    if ((start[0] != NULL) && (start[1] != NULL)) {
237	warnx("only one package name allowed ('%s' extraneous)", start[1]);
238	usage();
239    }
240    if (start[0] == NULL)
241	start[0] = InstalledPkg;
242    if (!pkg_perform(start)) {
243	if (Verbose)
244	    warnx("package creation failed");
245	return 1;
246    }
247    else
248	return 0;
249}
250
251static void
252usage()
253{
254    fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
255"usage: pkg_create [-YNOhjnvyz] [-C conflicts] [-P pkgs] [-p prefix]",
256"                  [-i iscript] [-I piscript] [-k dscript] [-K pdscript]",
257"                  [-r rscript] [-s srcdir] [-S basedir]",
258"                  [-t template] [-X excludefile]",
259"                  [-D displayfile] [-m mtreefile] [-o originpath]",
260"                  -c comment -d description -f packlist pkg-filename",
261"       pkg_create [-EGYNRhnvxy] -b pkg-name [pkg-filename]");
262    exit(1);
263}
264