xref: /titanic_51/usr/src/cmd/svr4pkg/pkginstall/getinst.c (revision 21ad40f5447a73ac8a7ed2b9b66dd73ff1b088c1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <valtools.h>
36 #include <locale.h>
37 #include <libintl.h>
38 #include <pkginfo.h>
39 #include "install.h"
40 #include <pkglib.h>
41 #include "libadm.h"
42 #include "libinst.h"
43 #include "pkginstall.h"
44 #include "messages.h"
45 
46 extern struct admin adm;
47 extern char	*pkgarch, *pkgvers, *msgtext, *pkgabrv;
48 extern int	maxinst;
49 
50 static char	newinst[PKGSIZ];
51 static char	*nextinst(void);
52 static char	*prompt(struct pkginfo *info, int npkgs);
53 static int	same_pkg;	/* same PKG, ARCH and VERSION */
54 
55 /*
56  * This returns the correct package instance based on how many packages are
57  * already installed. If there are none (npkgs == 0), it just returns the
58  * package abbreviation. Otherwise, it interacts with the user (or reads the
59  * admin file) to determine if we should overwrite an instance which is
60  * already installed, or possibly install a new instance of this package
61  */
62 char *
63 getinst(int *updatingExisting, struct pkginfo *info, int npkgs,
64 	boolean_t a_preinstallCheck)
65 {
66 	char	*inst;
67 	char	*sameinst;
68 	int	i;
69 	int	nsamearch;
70 	int	samearch;
71 
72 	/* entry debugging info */
73 
74 	same_pkg = 0;
75 
76 	/*
77 	 * If this is the first instance of the package, it's called the by
78 	 * the package abbreviation.
79 	 */
80 
81 	if (npkgs == 0) {
82 		return (pkgabrv);
83 	}
84 
85 	/*
86 	 * this package is already installed; determine how to handle the
87 	 * new instance of the package to install
88 	 */
89 
90 	if (ADM(instance, "newonly") || ADM(instance, "quit")) {
91 		/*
92 		 * new instance is required, or quit if not new
93 		 */
94 
95 		msgtext = MSG_NEWONLY;
96 		if (a_preinstallCheck == B_FALSE) {
97 			ptext(stderr, msgtext, pkgabrv);
98 		} else {
99 			(void) fprintf(stdout, "install-new-only=true\n");
100 			(void) fprintf(stdout, "ckinstance=4\n");
101 		}
102 		quit(4);
103 	}
104 
105 	/*
106 	 * package already installed and new instance not required
107 	 * see if updating the same instance of the package
108 	 */
109 
110 	samearch = nsamearch = 0;
111 	sameinst  = NULL;
112 	for (i = 0; i < npkgs; i++) {
113 		if (strcmp(info[i].arch, pkgarch) == NULL) {
114 			samearch = i;
115 			nsamearch++;
116 			if (strcmp(info[i].version, pkgvers) == NULL) {
117 				sameinst = info[i].pkginst;
118 			}
119 		}
120 	}
121 
122 	if (sameinst) {
123 		/* same instance of package */
124 		if (a_preinstallCheck == B_FALSE) {
125 			ptext(stderr, MSG_SAME);
126 		} else {
127 			(void) fprintf(stdout, "install-same-instance=true\n");
128 			(void) fprintf(stdout, "ckinstance=0\n");
129 		}
130 
131 		inst = sameinst;
132 		same_pkg++;
133 		(*updatingExisting)++;
134 		return (inst);
135 	}
136 
137 	if (ADM(instance, "overwrite")) {
138 		/* not the same instance of the package */
139 		if (npkgs == 1) {
140 			samearch = 0; /* use only package we know about */
141 		} else if (nsamearch != 1) {
142 			/*
143 			 * more than one instance of the same ARCH is already
144 			 * installed on this machine
145 			 */
146 			msgtext = MSG_OVERWRITE;
147 			if (a_preinstallCheck == B_FALSE) {
148 				ptext(stderr, msgtext);
149 			} else {
150 				(void) fprintf(stdout,
151 					"install-ovewrite=true\n");
152 				(void) fprintf(stdout, "ckinstance=4\n");
153 			}
154 			quit(4);
155 		}
156 
157 		inst = info[samearch].pkginst;
158 
159 		(*updatingExisting)++;
160 		return (inst);
161 	}
162 
163 	if (ADM(instance, "unique")) {
164 		if (maxinst <= npkgs) {
165 			/* too many instances */
166 			msgtext = MSG_UNIQ1;
167 			if (a_preinstallCheck == B_FALSE) {
168 				ptext(stderr, msgtext, pkgabrv);
169 			} else {
170 				(void) fprintf(stdout,
171 					"install-too-many-instances=true\n");
172 				(void) fprintf(stdout, "ckinstance=4\n");
173 			}
174 			quit(4);
175 		}
176 		inst = nextinst();
177 		return (inst);
178 	}
179 
180 	if (a_preinstallCheck == B_FALSE) {
181 		if (echoGetFlag() == B_FALSE) {
182 			msgtext = MSG_NOINTERACT;
183 			ptext(stderr, msgtext);
184 			quit(5);
185 		}
186 	} else {
187 		(void) fprintf(stdout, "install-new-instance=true\n");
188 		(void) fprintf(stdout, "ckinstance=1\n");
189 	}
190 
191 	inst = prompt(info, npkgs);
192 	if (strcmp(inst, "new") == NULL) {
193 		inst = nextinst();
194 		return (inst);
195 	}
196 
197 	(*updatingExisting)++;
198 
199 	return (inst);
200 }
201 
202 /*
203  * This informs the caller whether the package in question is the same
204  * version and architecture as an installed package of the same name.
205  */
206 
207 int
208 is_samepkg(void) {
209 	return (same_pkg);
210 }
211 
212 static char *
213 nextinst(void)
214 {
215 	struct pkginfo info;
216 	int	n;
217 
218 	n = 2; /* requirements say start at 2 */
219 
220 	info.pkginst = NULL;
221 	(void) strcpy(newinst, pkgabrv);
222 	while (pkginfo(&info, newinst, NULL, NULL) == 0) {
223 		(void) snprintf(newinst, sizeof (newinst),
224 				"%s.%d", pkgabrv, n++);
225 	}
226 	return (newinst);
227 }
228 
229 static char *
230 prompt(struct pkginfo *info, int npkgs)
231 {
232 	CKMENU	*menup;
233 	char	*inst;
234 	char	ans[MAX_INPUT];
235 	char	header[256];
236 	char	temp[256];
237 	int	i;
238 	int	n;
239 
240 	if (maxinst > npkgs) {
241 		/*
242 		 * the user may choose to install a completely new
243 		 * instance of this package
244 		 */
245 		n = ckyorn(ans, NULL, NULL, MSG_GETINST_HELP1,
246 			MSG_GETINST_PROMPT1);
247 		if (n != 0) {
248 			quit(n);
249 		}
250 		if (strchr("yY", *ans) != NULL) {
251 			return ("new");
252 		}
253 	}
254 
255 	(void) snprintf(header, sizeof (header), MSG_GETINST_HEADER, pkgabrv);
256 	menup = allocmenu(header, CKALPHA);
257 
258 	for (i = 0; i < npkgs; i++) {
259 		(void) snprintf(temp, sizeof (temp),
260 				"%s %s\n(%s) %s", info[i].pkginst,
261 			info[i].name, info[i].arch, info[i].version);
262 		if (setitem(menup, temp)) {
263 			progerr("no memory");
264 			quit(99);
265 		}
266 	}
267 
268 	if (npkgs == 1) {
269 		printmenu(menup);
270 		if (n = ckyorn(ans, NULL, NULL, NULL, MSG_GETINST_PROMPT0))
271 			quit(n);
272 		if (strchr("yY", *ans) == NULL)
273 			quit(3);
274 		(void) strcpy(newinst, info[0].pkginst);
275 	} else {
276 		if (n = ckitem(menup, &inst, 1, NULL, NULL, MSG_GETINST_HELP2,
277 		    MSG_GETINST_PROMPT2))
278 			quit(n);
279 		(void) strcpy(newinst, inst);
280 	}
281 	(void) setitem(menup, 0); /* clear resource usage */
282 	free(menup); /* clear resource usage */
283 
284 	return (newinst);
285 }
286