xref: /netbsd-src/external/bsd/pkg_install/dist/add/main.c (revision 782713e6c126f1866c6d9cfdee4ceb49483b5828)
1 /*	$NetBSD: main.c,v 1.4 2021/04/10 22:59:46 wiz 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.4 2021/04/10 22:59:46 wiz 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 #include "lib.h"
37 #include "add.h"
38 
39 static char Options[] = "AC:DIK:P:RVfhm:np:t:Uuv";
40 
41 char   *Destdir = NULL;
42 char   *OverrideMachine = NULL;
43 char   *Prefix = NULL;
44 Boolean NoInstall = FALSE;
45 Boolean NoRecord = FALSE;
46 Boolean Automatic = FALSE;
47 Boolean ForceDepends = FALSE;
48 /*
49  * Normally, updating fails if the dependencies of a depending package
50  * are not satisfied by the package to be updated.  ForceDepending
51  * turns that failure into a warning.
52  */
53 Boolean ForceDepending = 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",
63 	    "usage: pkg_add [-AfhInRuVv] [-C config] [-P destdir] [-K pkg_dbdir]",
64 	    "               [-m machine] [-p prefix]",
65 	    "               [[ftp|http]://[user[:password]@]host[:port]][/path/]pkg-name ...");
66 	exit(1);
67 }
68 
69 int
70 main(int argc, char **argv)
71 {
72 	int     ch, error=0;
73 	lpkg_head_t pkgs;
74 
75 	setprogname(argv[0]);
76 	while ((ch = getopt(argc, argv, Options)) != -1) {
77 		switch (ch) {
78 		case 'A':
79 			Automatic = TRUE;
80 			break;
81 
82 		case 'C':
83 			config_file = optarg;
84 			break;
85 
86 		case 'D':
87 			ForceDepending = TRUE;
88 			break;
89 
90 		case 'P':
91 			Destdir = optarg;
92 			break;
93 
94 		case 'f':
95 			Force = TRUE;
96 			ForceDepends = TRUE;
97 			ForceDepending = TRUE;
98 			break;
99 
100 		case 'I':
101 			NoInstall = TRUE;
102 			break;
103 
104 		case 'K':
105 			pkgdb_set_dir(optarg, 3);
106 			break;
107 
108 		case 'R':
109 			NoRecord = TRUE;
110 			break;
111 
112 		case 'm':
113 			OverrideMachine = optarg;
114 			break;
115 
116 		case 'n':
117 			Fake = TRUE;
118 			Verbose = TRUE;
119 			break;
120 
121 		case 'p':
122 			Prefix = optarg;
123 			break;
124 
125 		case 'U':
126 			ReplaceSame = 1;
127 			Replace = 1;
128 			break;
129 
130 		case 'u':
131 			Replace = 1;
132 			break;
133 
134 		case 'V':
135 			show_version();
136 			/* NOTREACHED */
137 
138 		case 'v':
139 			Verbose = TRUE;
140 			break;
141 
142 		case 'h':
143 		case '?':
144 		default:
145 			usage();
146 			break;
147 		}
148 	}
149 	argc -= optind;
150 	argv += optind;
151 
152 	pkg_install_config();
153 
154 	if (Destdir != NULL) {
155 		char *pkgdbdir;
156 
157 		pkgdbdir = xasprintf("%s/%s", Destdir, config_pkg_dbdir);
158 		pkgdb_set_dir(pkgdbdir, 4);
159 		free(pkgdbdir);
160 	}
161 
162 #ifndef BOOTSTRAP
163 	process_pkg_path();
164 #endif
165 
166 	TAILQ_INIT(&pkgs);
167 
168 	if (argc == 0) {
169 		/* If no packages, yelp */
170 		warnx("missing package name(s)");
171 		usage();
172 	}
173 
174 #ifndef BOOTSTRAP
175 	if (strcasecmp(do_license_check, "no") == 0)
176 		LicenseCheck = 0;
177 	else if (strcasecmp(do_license_check, "yes") == 0)
178 		LicenseCheck = 1;
179 	else if (strcasecmp(do_license_check, "always") == 0)
180 		LicenseCheck = 2;
181 	else
182 		errx(1, "Unknown value of the configuration variable"
183 		    "CHECK_LICENSE");
184 
185 	if (LicenseCheck)
186 		load_license_lists();
187 #endif
188 
189 	/* Get all the remaining package names, if any */
190 	for (; argc > 0; --argc, ++argv) {
191 		lpkg_t *lpp;
192 
193 		if (IS_STDIN(*argv))
194 			lpp = alloc_lpkg("-");
195 		else
196 			lpp = alloc_lpkg(*argv);
197 
198 		TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link);
199 	}
200 
201 	error += pkg_perform(&pkgs);
202 	if (error != 0) {
203 		warnx("%d package addition%s failed", error, error == 1 ? "" : "s");
204 		exit(1);
205 	}
206 	exit(0);
207 }
208