8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / svr4pkg / pkginstall / getinst.c
blob1e4bbfec711cc61b8aab642794a48aa863688249
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
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"
46 extern struct admin adm;
47 extern char *pkgarch, *pkgvers, *msgtext, *pkgabrv;
48 extern int maxinst;
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 */
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
62 char *
63 getinst(int *updatingExisting, struct pkginfo *info, int npkgs,
64 boolean_t a_preinstallCheck)
66 char *inst;
67 char *sameinst;
68 int i;
69 int nsamearch;
70 int samearch;
72 /* entry debugging info */
74 same_pkg = 0;
77 * If this is the first instance of the package, it's called the by
78 * the package abbreviation.
81 if (npkgs == 0) {
82 return (pkgabrv);
86 * this package is already installed; determine how to handle the
87 * new instance of the package to install
90 if (ADM(instance, "newonly") || ADM(instance, "quit")) {
92 * new instance is required, or quit if not new
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");
102 quit(4);
106 * package already installed and new instance not required
107 * see if updating the same instance of the package
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;
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");
131 inst = sameinst;
132 same_pkg++;
133 (*updatingExisting)++;
134 return (inst);
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) {
143 * more than one instance of the same ARCH is already
144 * installed on this machine
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");
154 quit(4);
157 inst = info[samearch].pkginst;
159 (*updatingExisting)++;
160 return (inst);
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");
174 quit(4);
176 inst = nextinst();
177 return (inst);
180 if (a_preinstallCheck == B_FALSE) {
181 if (echoGetFlag() == B_FALSE) {
182 msgtext = MSG_NOINTERACT;
183 ptext(stderr, msgtext);
184 quit(5);
186 } else {
187 (void) fprintf(stdout, "install-new-instance=true\n");
188 (void) fprintf(stdout, "ckinstance=1\n");
191 inst = prompt(info, npkgs);
192 if (strcmp(inst, "new") == NULL) {
193 inst = nextinst();
194 return (inst);
197 (*updatingExisting)++;
199 return (inst);
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.
208 is_samepkg(void) {
209 return (same_pkg);
212 static char *
213 nextinst(void)
215 struct pkginfo info;
216 int n;
218 n = 2; /* requirements say start at 2 */
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++);
226 return (newinst);
229 static char *
230 prompt(struct pkginfo *info, int npkgs)
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;
240 if (maxinst > npkgs) {
242 * the user may choose to install a completely new
243 * instance of this package
245 n = ckyorn(ans, NULL, NULL, MSG_GETINST_HELP1,
246 MSG_GETINST_PROMPT1);
247 if (n != 0) {
248 quit(n);
250 if (strchr("yY", *ans) != NULL) {
251 return ("new");
255 (void) snprintf(header, sizeof (header), MSG_GETINST_HEADER, pkgabrv);
256 menup = allocmenu(header, CKALPHA);
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);
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);
281 (void) setitem(menup, 0); /* clear resource usage */
282 free(menup); /* clear resource usage */
284 return (newinst);