xref: /netbsd/external/bsd/pkg_install/dist/add/main.c (revision 6550d01e)
1 /*	$NetBSD: main.c,v 1.1.1.9 2010/06/26 00:14:26 joerg Exp $	*/
2 
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.1.1.9 2010/06/26 00:14:26 joerg Exp $");
11 
12 /*
13  *
14  * FreeBSD install - a package for the installation and maintainance
15  * of non-core utilities.
16  *
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.
25  *
26  * Jordan K. Hubbard
27  * 18 July 1993
28  *
29  * This is the add module.
30  *
31  */
32 
33 #if HAVE_ERR_H
34 #include <err.h>
35 #endif
36 #if HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
39 #include "lib.h"
40 #include "add.h"
41 
42 static char Options[] = "AC:IK:LP:RVW:fhm:np:t:Uuvw:";
43 
44 char   *Destdir = NULL;
45 char   *OverrideMachine = NULL;
46 char   *Prefix = NULL;
47 char   *View = NULL;
48 char   *Viewbase = NULL;
49 Boolean NoView = FALSE;
50 Boolean NoInstall = FALSE;
51 Boolean NoRecord = FALSE;
52 Boolean Automatic = FALSE;
53 Boolean ForceDepends = FALSE;
54 
55 int	LicenseCheck = 0;
56 int     Replace = 0;
57 int	ReplaceSame = 0;
58 
59 static void
60 usage(void)
61 {
62 	(void) fprintf(stderr, "%s\n%s\n%s\n%s\n",
63 	    "usage: pkg_add [-AfhILnRuVv] [-C config] [-P destdir] [-K pkg_dbdir]",
64 	    "               [-m machine] [-p prefix] [-s verification-type",
65 	    "               [-W viewbase] [-w view]\n",
66 	    "               [[ftp|http]://[user[:password]@]host[:port]][/path/]pkg-name ...");
67 	exit(1);
68 }
69 
70 int
71 main(int argc, char **argv)
72 {
73 	int     ch, error=0;
74 	lpkg_head_t pkgs;
75 
76 	setprogname(argv[0]);
77 	while ((ch = getopt(argc, argv, Options)) != -1) {
78 		switch (ch) {
79 		case 'A':
80 			Automatic = TRUE;
81 			break;
82 
83 		case 'C':
84 			config_file = optarg;
85 
86 		case 'P':
87 			Destdir = optarg;
88 			break;
89 
90 		case 'f':
91 			Force = TRUE;
92 			ForceDepends = TRUE;
93 			break;
94 
95 		case 'I':
96 			NoInstall = TRUE;
97 			break;
98 
99 		case 'K':
100 			pkgdb_set_dir(optarg, 3);
101 			break;
102 
103 		case 'L':
104 			NoView = TRUE;
105 			break;
106 
107 		case 'R':
108 			NoRecord = TRUE;
109 			break;
110 
111 		case 'm':
112 			OverrideMachine = optarg;
113 			break;
114 
115 		case 'n':
116 			Fake = TRUE;
117 			Verbose = TRUE;
118 			break;
119 
120 		case 'p':
121 			Prefix = optarg;
122 			break;
123 
124 		case 'U':
125 			ReplaceSame = 1;
126 			Replace = 1;
127 			break;
128 
129 		case 'u':
130 			Replace = 1;
131 			break;
132 
133 		case 'V':
134 			show_version();
135 			/* NOTREACHED */
136 
137 		case 'v':
138 			Verbose = TRUE;
139 			break;
140 
141 		case 'W':
142 			Viewbase = optarg;
143 			break;
144 
145 		case 'w':
146 			View = optarg;
147 			break;
148 
149 		case 'h':
150 		case '?':
151 		default:
152 			usage();
153 			break;
154 		}
155 	}
156 	argc -= optind;
157 	argv += optind;
158 
159 	pkg_install_config();
160 
161 	if (Destdir != NULL) {
162 		char *pkgdbdir;
163 
164 		pkgdbdir = xasprintf("%s/%s", Destdir, config_pkg_dbdir);
165 		pkgdb_set_dir(pkgdbdir, 4);
166 		free(pkgdbdir);
167 	}
168 
169 	process_pkg_path();
170 	TAILQ_INIT(&pkgs);
171 
172 	if (argc == 0) {
173 		/* If no packages, yelp */
174 		warnx("missing package name(s)");
175 		usage();
176 	}
177 
178 	if (strcasecmp(do_license_check, "no") == 0)
179 		LicenseCheck = 0;
180 	else if (strcasecmp(do_license_check, "yes") == 0)
181 		LicenseCheck = 1;
182 	else if (strcasecmp(do_license_check, "always") == 0)
183 		LicenseCheck = 2;
184 	else
185 		errx(1, "Unknown value of the configuration variable"
186 		    "CHECK_LICENSE");
187 
188 	if (LicenseCheck)
189 		load_license_lists();
190 
191 	/* Get all the remaining package names, if any */
192 	for (; argc > 0; --argc, ++argv) {
193 		lpkg_t *lpp;
194 
195 		if (IS_STDIN(*argv))
196 			lpp = alloc_lpkg("-");
197 		else
198 			lpp = alloc_lpkg(*argv);
199 
200 		TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link);
201 	}
202 
203 	error += pkg_perform(&pkgs);
204 	if (error != 0) {
205 		warnx("%d package addition%s failed", error, error == 1 ? "" : "s");
206 		exit(1);
207 	}
208 	exit(0);
209 }
210