xref: /onnv-gate/usr/src/lib/libadm/common/ckitem.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4 */
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate #include <limits.h>
37*0Sstevel@tonic-gate #include "valtools.h"
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <strings.h>
41*0Sstevel@tonic-gate #include "libadm.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static int	insert(struct _choice_ *, CKMENU *);
44*0Sstevel@tonic-gate static char	*strtoki(char *, char *);
45*0Sstevel@tonic-gate static char	**match(CKMENU *, char *, int);
46*0Sstevel@tonic-gate static int	getstr(char *, char *, char *, char *, char *);
47*0Sstevel@tonic-gate static int	getnum(char *, int, int *, int *);
48*0Sstevel@tonic-gate static struct _choice_ *next(struct _choice_ *);
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static char	*deferr;
51*0Sstevel@tonic-gate static char	*errmsg;
52*0Sstevel@tonic-gate static char	*defhlp;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	PROMPT	"Enter selection"
55*0Sstevel@tonic-gate #define	MESG0	"Entry does not match available menu selection. "
56*0Sstevel@tonic-gate #define	MESG1	"the number of the menu item you wish to select, or "
57*0Sstevel@tonic-gate #define	MESG2	"the token which is associated with the menu item,\
58*0Sstevel@tonic-gate 		or a partial string which uniquely identifies the \
59*0Sstevel@tonic-gate 		token for the menu item. Enter ?? to reprint the menu."
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #define	TOOMANY	"Too many items selected from menu"
62*0Sstevel@tonic-gate #define	NOTUNIQ	"The entered text does not uniquely identify a menu choice."
63*0Sstevel@tonic-gate #define	BADNUM	"Bad numeric choice specification"
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static char *
setmsg(CKMENU * menup,short flag)66*0Sstevel@tonic-gate setmsg(CKMENU *menup, short flag)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	int	n;
69*0Sstevel@tonic-gate 	char	*msg;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	n = (int)(6 + sizeof (MESG2));
72*0Sstevel@tonic-gate 	if (flag)
73*0Sstevel@tonic-gate 		n += (int)(sizeof (MESG0));
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (menup->attr & CKUNNUM) {
76*0Sstevel@tonic-gate 		msg = calloc((size_t)n, sizeof (char));
77*0Sstevel@tonic-gate 		if (flag)
78*0Sstevel@tonic-gate 			(void) strcpy(msg, MESG0);
79*0Sstevel@tonic-gate 		else
80*0Sstevel@tonic-gate 			msg[0] = '\0';
81*0Sstevel@tonic-gate 		(void) strcat(msg, "Enter ");
82*0Sstevel@tonic-gate 		(void) strcat(msg, MESG2);
83*0Sstevel@tonic-gate 	} else {
84*0Sstevel@tonic-gate 		msg = calloc(n+sizeof (MESG1), sizeof (char));
85*0Sstevel@tonic-gate 		if (flag)
86*0Sstevel@tonic-gate 			(void) strcpy(msg, MESG0);
87*0Sstevel@tonic-gate 		else
88*0Sstevel@tonic-gate 			msg[0] = '\0';
89*0Sstevel@tonic-gate 		(void) strcat(msg, "Enter ");
90*0Sstevel@tonic-gate 		(void) strcat(msg, MESG1);
91*0Sstevel@tonic-gate 		(void) strcat(msg, MESG2);
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 	return (msg);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate CKMENU *
allocmenu(char * label,int attr)97*0Sstevel@tonic-gate allocmenu(char *label, int attr)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	CKMENU *pt;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if (pt = calloc(1, sizeof (CKMENU))) {
102*0Sstevel@tonic-gate 		pt->attr = attr;
103*0Sstevel@tonic-gate 		pt->label = label;
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	return (pt);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate void
ckitem_err(CKMENU * menup,char * error)109*0Sstevel@tonic-gate ckitem_err(CKMENU *menup, char *error)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	deferr = setmsg(menup, 1);
112*0Sstevel@tonic-gate 	puterror(stdout, deferr, error);
113*0Sstevel@tonic-gate 	free(deferr);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate void
ckitem_hlp(CKMENU * menup,char * help)117*0Sstevel@tonic-gate ckitem_hlp(CKMENU *menup, char *help)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	defhlp = setmsg(menup, 0);
120*0Sstevel@tonic-gate 	puthelp(stdout, defhlp, help);
121*0Sstevel@tonic-gate 	free(defhlp);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate int
ckitem(CKMENU * menup,char * item[],short max,char * defstr,char * error,char * help,char * prompt)125*0Sstevel@tonic-gate ckitem(CKMENU *menup, char *item[], short max, char *defstr, char *error,
126*0Sstevel@tonic-gate 	char *help, char *prompt)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	int	n, i;
129*0Sstevel@tonic-gate 	char	strval[MAX_INPUT];
130*0Sstevel@tonic-gate 	char	**list;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if ((menup->nchoices <= 0) && !menup->invis)
133*0Sstevel@tonic-gate 		return (4); /* nothing to choose from */
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if (menup->attr & CKONEFLAG) {
136*0Sstevel@tonic-gate 		if (((n = menup->nchoices) <= 1) && menup->invis) {
137*0Sstevel@tonic-gate 			for (i = 0; menup->invis[i]; ++i)
138*0Sstevel@tonic-gate 				n++;
139*0Sstevel@tonic-gate 		}
140*0Sstevel@tonic-gate 		if (n <= 1) {
141*0Sstevel@tonic-gate 			if (menup->choice)
142*0Sstevel@tonic-gate 				item[0] = menup->choice->token;
143*0Sstevel@tonic-gate 			else if (menup->invis)
144*0Sstevel@tonic-gate 				item[0] = menup->invis[0];
145*0Sstevel@tonic-gate 			item[1] = NULL;
146*0Sstevel@tonic-gate 			return (0);
147*0Sstevel@tonic-gate 		}
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if (max < 1)
151*0Sstevel@tonic-gate 		max = menup->nchoices;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (!prompt)
154*0Sstevel@tonic-gate 		prompt = PROMPT;
155*0Sstevel@tonic-gate 	defhlp = setmsg(menup, 0);
156*0Sstevel@tonic-gate 	deferr = setmsg(menup, 1);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate reprint:
159*0Sstevel@tonic-gate 	printmenu(menup);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate start:
162*0Sstevel@tonic-gate 	if (n = getstr(strval, defstr, error, help, prompt)) {
163*0Sstevel@tonic-gate 		free(defhlp);
164*0Sstevel@tonic-gate 		free(deferr);
165*0Sstevel@tonic-gate 		return (n);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 	if (strcmp(strval, "??") == 0) {
168*0Sstevel@tonic-gate 		goto reprint;
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 	if ((defstr) && (strcmp(strval, defstr) == 0)) {
171*0Sstevel@tonic-gate 		item[0] = defstr;
172*0Sstevel@tonic-gate 		item[1] = NULL;
173*0Sstevel@tonic-gate 	} else {
174*0Sstevel@tonic-gate 		list = match(menup, strval, (int)max);
175*0Sstevel@tonic-gate 		if (!list) {
176*0Sstevel@tonic-gate 			puterror(stderr, deferr, (errmsg ? errmsg : error));
177*0Sstevel@tonic-gate 			goto start;
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 		for (i = 0; (i < max); i++)
180*0Sstevel@tonic-gate 			item[i] = list[i];
181*0Sstevel@tonic-gate 		free(list);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	free(defhlp);
184*0Sstevel@tonic-gate 	free(deferr);
185*0Sstevel@tonic-gate 	return (0);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate static int
getnum(char * strval,int max,int * begin,int * end)189*0Sstevel@tonic-gate getnum(char *strval, int max, int *begin, int *end)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	int n;
192*0Sstevel@tonic-gate 	char *pt;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	*begin = *end = 0;
195*0Sstevel@tonic-gate 	pt = strval;
196*0Sstevel@tonic-gate 	for (;;) {
197*0Sstevel@tonic-gate 		if (*pt == '$') {
198*0Sstevel@tonic-gate 			n = max;
199*0Sstevel@tonic-gate 			pt++;
200*0Sstevel@tonic-gate 		} else {
201*0Sstevel@tonic-gate 			n = (int)strtol(pt, &pt, 10);
202*0Sstevel@tonic-gate 			if ((n <= 0) || (n > max))
203*0Sstevel@tonic-gate 				return (1);
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 		while (isspace((unsigned char)*pt))
206*0Sstevel@tonic-gate 			pt++;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 		if (!*begin && (*pt == '-')) {
209*0Sstevel@tonic-gate 			*begin = n;
210*0Sstevel@tonic-gate 			pt++;
211*0Sstevel@tonic-gate 			while (isspace((unsigned char)*pt))
212*0Sstevel@tonic-gate 				pt++;
213*0Sstevel@tonic-gate 			continue;
214*0Sstevel@tonic-gate 		} else if (*pt) {
215*0Sstevel@tonic-gate 			return (1); /* wasn't a number, or an invalid one */
216*0Sstevel@tonic-gate 		} else if (*begin) {
217*0Sstevel@tonic-gate 			*end = n;
218*0Sstevel@tonic-gate 			break;
219*0Sstevel@tonic-gate 		} else {
220*0Sstevel@tonic-gate 			*begin = n;
221*0Sstevel@tonic-gate 			break;
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 	if (!*end)
225*0Sstevel@tonic-gate 		*end = *begin;
226*0Sstevel@tonic-gate 	return ((*begin <= *end) ? 0 : 1);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate static char **
match(CKMENU * menup,char * strval,int max)230*0Sstevel@tonic-gate match(CKMENU *menup, char *strval, int max)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	struct _choice_ *chp;
233*0Sstevel@tonic-gate 	char **choice;
234*0Sstevel@tonic-gate 	int begin, end;
235*0Sstevel@tonic-gate 	char *pt, *found;
236*0Sstevel@tonic-gate 	int i, len, nchoice;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	nchoice = 0;
239*0Sstevel@tonic-gate 	choice = calloc((size_t)max, sizeof (char *));
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	do {
242*0Sstevel@tonic-gate 		if (pt = strpbrk(strval, " \t,")) {
243*0Sstevel@tonic-gate 			do {
244*0Sstevel@tonic-gate 				*pt++ = '\0';
245*0Sstevel@tonic-gate 			} while (strchr(" \t,", *pt));
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		if (nchoice >= max) {
249*0Sstevel@tonic-gate 			errmsg = TOOMANY;
250*0Sstevel@tonic-gate 			return (NULL);
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 		if (!(menup->attr & CKUNNUM) &&
253*0Sstevel@tonic-gate 			isdigit((unsigned char)*strval)) {
254*0Sstevel@tonic-gate 			if (getnum(strval, (int)menup->nchoices, &begin,
255*0Sstevel@tonic-gate 			    &end)) {
256*0Sstevel@tonic-gate 				errmsg = BADNUM;
257*0Sstevel@tonic-gate 				return (NULL);
258*0Sstevel@tonic-gate 			}
259*0Sstevel@tonic-gate 			chp = menup->choice;
260*0Sstevel@tonic-gate 			for (i = 1; chp; i++) {
261*0Sstevel@tonic-gate 				if ((i >= begin) && (i <= end)) {
262*0Sstevel@tonic-gate 					if (nchoice >= max) {
263*0Sstevel@tonic-gate 						errmsg = TOOMANY;
264*0Sstevel@tonic-gate 						return (NULL);
265*0Sstevel@tonic-gate 					}
266*0Sstevel@tonic-gate 					choice[nchoice++] = chp->token;
267*0Sstevel@tonic-gate 				}
268*0Sstevel@tonic-gate 				chp = chp->next;
269*0Sstevel@tonic-gate 			}
270*0Sstevel@tonic-gate 			continue;
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		found = NULL;
274*0Sstevel@tonic-gate 		chp = menup->choice;
275*0Sstevel@tonic-gate 		for (i = 0; chp; i++) {
276*0Sstevel@tonic-gate 			len = (int)strlen(strval);
277*0Sstevel@tonic-gate 			if (strncmp(chp->token, strval, (size_t)len) == 0) {
278*0Sstevel@tonic-gate 				if (chp->token[len] == '\0') {
279*0Sstevel@tonic-gate 					found = chp->token;
280*0Sstevel@tonic-gate 					break;
281*0Sstevel@tonic-gate 				} else if (found) {
282*0Sstevel@tonic-gate 					errmsg = NOTUNIQ;
283*0Sstevel@tonic-gate 					return (NULL); /* not unique */
284*0Sstevel@tonic-gate 				}
285*0Sstevel@tonic-gate 				found = chp->token;
286*0Sstevel@tonic-gate 			}
287*0Sstevel@tonic-gate 			chp = chp->next;
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 		if (menup->invis) {
291*0Sstevel@tonic-gate 			for (i = 0; menup->invis[i]; ++i) {
292*0Sstevel@tonic-gate 				len = (int)strlen(strval);
293*0Sstevel@tonic-gate 				if (strncmp(menup->invis[i], strval,
294*0Sstevel@tonic-gate 				    (size_t)len) == 0) {
295*0Sstevel@tonic-gate #if _3b2
296*0Sstevel@tonic-gate 					if (chp->token[len] == '\0') {
297*0Sstevel@tonic-gate #else
298*0Sstevel@tonic-gate 					if (menup->invis[i][len] == '\0') {
299*0Sstevel@tonic-gate #endif
300*0Sstevel@tonic-gate 						found = menup->invis[i];
301*0Sstevel@tonic-gate 						break;
302*0Sstevel@tonic-gate 					} else if (found) {
303*0Sstevel@tonic-gate 						errmsg = NOTUNIQ;
304*0Sstevel@tonic-gate 						return (NULL);
305*0Sstevel@tonic-gate 					}
306*0Sstevel@tonic-gate 					found = menup->invis[i];
307*0Sstevel@tonic-gate 				}
308*0Sstevel@tonic-gate 			}
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 		if (found) {
311*0Sstevel@tonic-gate 			choice[nchoice++] = found;
312*0Sstevel@tonic-gate 			continue;
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 		errmsg = NULL;
315*0Sstevel@tonic-gate 		return (NULL);
316*0Sstevel@tonic-gate 	} while (((strval = pt) != NULL) && *pt);
317*0Sstevel@tonic-gate 	return (choice);
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate int
321*0Sstevel@tonic-gate setitem(CKMENU *menup, char *choice)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	struct _choice_ *chp;
324*0Sstevel@tonic-gate 	int n;
325*0Sstevel@tonic-gate 	char *pt;
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	if (choice == NULL) {
328*0Sstevel@tonic-gate 		/* request to clear memory usage */
329*0Sstevel@tonic-gate 		chp = menup->choice;
330*0Sstevel@tonic-gate 		while (chp) {
331*0Sstevel@tonic-gate 			struct _choice_	*_chp = chp;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 			chp = chp->next;
334*0Sstevel@tonic-gate 			menup->longest = menup->nchoices = 0;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 			(void) free(_chp->token); /* free token and text */
337*0Sstevel@tonic-gate 			(void) free(_chp);
338*0Sstevel@tonic-gate 		}
339*0Sstevel@tonic-gate 		return (1);
340*0Sstevel@tonic-gate 	}
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	if ((chp = calloc(1, sizeof (struct _choice_))) == NULL)
343*0Sstevel@tonic-gate 		return (1);
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	if ((pt = strdup(choice)) == NULL) {
346*0Sstevel@tonic-gate 		free(chp);
347*0Sstevel@tonic-gate 		return (1);
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 	if (!*pt || isspace((unsigned char)*pt)) {
350*0Sstevel@tonic-gate 		free(chp);
351*0Sstevel@tonic-gate 		return (2);
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	chp->token = strtoki(pt, " \t\n");
355*0Sstevel@tonic-gate 	chp->text = strtoki(NULL, "");
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	if (chp->text) {
358*0Sstevel@tonic-gate 	    while (isspace((unsigned char)*chp->text))
359*0Sstevel@tonic-gate 		chp->text++;
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 	n = (int)strlen(chp->token);
362*0Sstevel@tonic-gate 	if (n > menup->longest)
363*0Sstevel@tonic-gate 		menup->longest = (short)n;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	if (insert(chp, menup))
366*0Sstevel@tonic-gate 		menup->nchoices++;
367*0Sstevel@tonic-gate 	else
368*0Sstevel@tonic-gate 		free(chp); /* duplicate entry */
369*0Sstevel@tonic-gate 	return (0);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate int
373*0Sstevel@tonic-gate setinvis(CKMENU *menup, char *choice)
374*0Sstevel@tonic-gate {
375*0Sstevel@tonic-gate 	int	index;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	index = 0;
378*0Sstevel@tonic-gate 	if (choice == NULL) {
379*0Sstevel@tonic-gate 		if (menup->invis == NULL)
380*0Sstevel@tonic-gate 			return (0);
381*0Sstevel@tonic-gate 		while (menup->invis[index])
382*0Sstevel@tonic-gate 			free(menup->invis[index]);
383*0Sstevel@tonic-gate 		free(menup->invis);
384*0Sstevel@tonic-gate 		return (0);
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	if (menup->invis == NULL)
388*0Sstevel@tonic-gate 		menup->invis = calloc(2, sizeof (char *));
389*0Sstevel@tonic-gate 	else {
390*0Sstevel@tonic-gate 		while (menup->invis[index])
391*0Sstevel@tonic-gate 			index++; /* count invisible choices */
392*0Sstevel@tonic-gate 		menup->invis = realloc(menup->invis,
393*0Sstevel@tonic-gate 			(index+2)* sizeof (char *));
394*0Sstevel@tonic-gate 		menup->invis[index+1] = NULL;
395*0Sstevel@tonic-gate 	}
396*0Sstevel@tonic-gate 	if (!menup->invis)
397*0Sstevel@tonic-gate 		return (-1);
398*0Sstevel@tonic-gate 	menup->invis[index] = strdup(choice);
399*0Sstevel@tonic-gate 	return (0);
400*0Sstevel@tonic-gate }
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate static int
403*0Sstevel@tonic-gate insert(struct _choice_ *chp, CKMENU *menup)
404*0Sstevel@tonic-gate {
405*0Sstevel@tonic-gate 	struct _choice_ *last, *base;
406*0Sstevel@tonic-gate 	int n;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	base = menup->choice;
409*0Sstevel@tonic-gate 	last = NULL;
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	if (!(menup->attr & CKALPHA)) {
412*0Sstevel@tonic-gate 		while (base) {
413*0Sstevel@tonic-gate 			if (strcmp(base->token, chp->token) == 0)
414*0Sstevel@tonic-gate 				return (0);
415*0Sstevel@tonic-gate 			last = base;
416*0Sstevel@tonic-gate 			base = base->next;
417*0Sstevel@tonic-gate 		}
418*0Sstevel@tonic-gate 		if (last)
419*0Sstevel@tonic-gate 			last->next = chp;
420*0Sstevel@tonic-gate 		else
421*0Sstevel@tonic-gate 			menup->choice = chp;
422*0Sstevel@tonic-gate 		return (1);
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	while (base) {
426*0Sstevel@tonic-gate 		if ((n = strcmp(base->token, chp->token)) == 0)
427*0Sstevel@tonic-gate 			return (0);
428*0Sstevel@tonic-gate 		if (n > 0) {
429*0Sstevel@tonic-gate 			/* should come before this one */
430*0Sstevel@tonic-gate 			break;
431*0Sstevel@tonic-gate 		}
432*0Sstevel@tonic-gate 		last = base;
433*0Sstevel@tonic-gate 		base = base->next;
434*0Sstevel@tonic-gate 	}
435*0Sstevel@tonic-gate 	if (last) {
436*0Sstevel@tonic-gate 		chp->next = last->next;
437*0Sstevel@tonic-gate 		last->next = chp;
438*0Sstevel@tonic-gate 	} else {
439*0Sstevel@tonic-gate 		chp->next = menup->choice;
440*0Sstevel@tonic-gate 		menup->choice = chp;
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate 	return (1);
443*0Sstevel@tonic-gate }
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate void
446*0Sstevel@tonic-gate printmenu(CKMENU *menup)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate 	int i;
449*0Sstevel@tonic-gate 	struct _choice_ *chp;
450*0Sstevel@tonic-gate 	char *pt;
451*0Sstevel@tonic-gate 	char format[16];
452*0Sstevel@tonic-gate 	int c;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	(void) fputc('\n', stderr);
455*0Sstevel@tonic-gate 	if (menup->label) {
456*0Sstevel@tonic-gate 		(void) puttext(stderr, menup->label, 0, 0);
457*0Sstevel@tonic-gate 		(void) fputc('\n', stderr);
458*0Sstevel@tonic-gate 	}
459*0Sstevel@tonic-gate 	(void) sprintf(format, "%%-%ds", menup->longest+5);
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	(void) next(NULL);
462*0Sstevel@tonic-gate 	chp = ((menup->attr & CKALPHA) ? next(menup->choice) : menup->choice);
463*0Sstevel@tonic-gate 	for (i = 1; chp; ++i) {
464*0Sstevel@tonic-gate 		if (!(menup->attr & CKUNNUM))
465*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%3d  ", i);
466*0Sstevel@tonic-gate 		(void) fprintf(stderr, format, chp->token);
467*0Sstevel@tonic-gate 		if (chp->text) {
468*0Sstevel@tonic-gate 			/* there is text associated with the token */
469*0Sstevel@tonic-gate 			pt = chp->text;
470*0Sstevel@tonic-gate 			while (*pt) {
471*0Sstevel@tonic-gate 				(void) fputc(*pt, stderr);
472*0Sstevel@tonic-gate 				if (*pt++ == '\n') {
473*0Sstevel@tonic-gate 					if (!(menup->attr & CKUNNUM))
474*0Sstevel@tonic-gate 						(void) fprintf(stderr,
475*0Sstevel@tonic-gate 						    "%5s", "");
476*0Sstevel@tonic-gate 					(void) fprintf(stderr, format, "");
477*0Sstevel@tonic-gate 					while (isspace((unsigned char)*pt))
478*0Sstevel@tonic-gate 						++pt;
479*0Sstevel@tonic-gate 				}
480*0Sstevel@tonic-gate 			}
481*0Sstevel@tonic-gate 		}
482*0Sstevel@tonic-gate 		(void) fputc('\n', stderr);
483*0Sstevel@tonic-gate 		chp = ((menup->attr & CKALPHA) ?
484*0Sstevel@tonic-gate 			next(menup->choice) : chp->next);
485*0Sstevel@tonic-gate 		if (chp && ((i % 10) == 0)) {
486*0Sstevel@tonic-gate 			/* page the choices */
487*0Sstevel@tonic-gate 			(void) fprintf(stderr,
488*0Sstevel@tonic-gate 			    "\n... %d more menu choices to follow;",
489*0Sstevel@tonic-gate 			    menup->nchoices - i);
490*0Sstevel@tonic-gate 			(void) fprintf(stderr,
491*0Sstevel@tonic-gate 			    /* CSTYLED */
492*0Sstevel@tonic-gate 			    "\n<RETURN> for more choices, <CTRL-D> to stop \
493*0Sstevel@tonic-gate display:");
494*0Sstevel@tonic-gate 			/* ignore other chars */
495*0Sstevel@tonic-gate 			while (((c = getc(stdin)) != EOF) && (c != '\n'))
496*0Sstevel@tonic-gate 				;
497*0Sstevel@tonic-gate 			(void) fputc('\n', stderr);
498*0Sstevel@tonic-gate 			if (c == EOF)
499*0Sstevel@tonic-gate 				break; /* stop printing menu */
500*0Sstevel@tonic-gate 		}
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate static int
505*0Sstevel@tonic-gate getstr(char *strval, char *defstr, char *error, char *help, char *prompt)
506*0Sstevel@tonic-gate {
507*0Sstevel@tonic-gate 	char input[MAX_INPUT];
508*0Sstevel@tonic-gate 	char *ept, end[MAX_INPUT];
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	*(ept = end) = '\0';
511*0Sstevel@tonic-gate 	if (defstr) {
512*0Sstevel@tonic-gate 		(void) sprintf(ept, "(default: %s) ", defstr);
513*0Sstevel@tonic-gate 		ept += strlen(ept);
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate 	if (ckquit) {
516*0Sstevel@tonic-gate 		(void) strcat(ept, "[?,??,q]");
517*0Sstevel@tonic-gate 	} else {
518*0Sstevel@tonic-gate 		(void) strcat(ept, "[?,??]");
519*0Sstevel@tonic-gate 	}
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate start:
522*0Sstevel@tonic-gate 	(void) fputc('\n', stderr);
523*0Sstevel@tonic-gate 	(void) puttext(stderr, prompt, 0, 0);
524*0Sstevel@tonic-gate 	(void) fprintf(stderr, " %s: ", end);
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	if (getinput(input))
527*0Sstevel@tonic-gate 		return (1);
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	if (strlen(input) == 0) {
530*0Sstevel@tonic-gate 		if (defstr) {
531*0Sstevel@tonic-gate 			(void) strcpy(strval, defstr);
532*0Sstevel@tonic-gate 			return (0);
533*0Sstevel@tonic-gate 		}
534*0Sstevel@tonic-gate 		puterror(stderr, deferr, (errmsg ? errmsg : error));
535*0Sstevel@tonic-gate 		goto start;
536*0Sstevel@tonic-gate 	} else if (strcmp(input, "?") == 0) {
537*0Sstevel@tonic-gate 		puthelp(stderr, defhlp, help);
538*0Sstevel@tonic-gate 		goto start;
539*0Sstevel@tonic-gate 	} else if (ckquit && (strcmp(input, "q") == 0)) {
540*0Sstevel@tonic-gate 		/* (void) strcpy(strval, input); */
541*0Sstevel@tonic-gate 		return (3);
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 	(void) strcpy(strval, input);
544*0Sstevel@tonic-gate 	return (0);
545*0Sstevel@tonic-gate }
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate static struct _choice_ *
548*0Sstevel@tonic-gate next(struct _choice_ *chp)
549*0Sstevel@tonic-gate {
550*0Sstevel@tonic-gate 	static char *last;
551*0Sstevel@tonic-gate 	static char *first;
552*0Sstevel@tonic-gate 	struct _choice_ *found;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	if (!chp) {
555*0Sstevel@tonic-gate 		last = NULL;
556*0Sstevel@tonic-gate 		return (NULL);
557*0Sstevel@tonic-gate 	}
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	found = NULL;
560*0Sstevel@tonic-gate 	for (first = NULL; chp; chp = chp->next) {
561*0Sstevel@tonic-gate 		if (last && strcmp(last, chp->token) >= 0)
562*0Sstevel@tonic-gate 			continue; /* lower than the last one we found */
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 		if (!first || strcmp(first, chp->token) > 0) {
565*0Sstevel@tonic-gate 			first = chp->token;
566*0Sstevel@tonic-gate 			found = chp;
567*0Sstevel@tonic-gate 		}
568*0Sstevel@tonic-gate 	}
569*0Sstevel@tonic-gate 	last = first;
570*0Sstevel@tonic-gate 	return (found);
571*0Sstevel@tonic-gate }
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate static char *
574*0Sstevel@tonic-gate strtoki(char *string, char *sepset)
575*0Sstevel@tonic-gate {
576*0Sstevel@tonic-gate 	char	*p, *q, *r;
577*0Sstevel@tonic-gate 	static char	*savept;
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 	/* first or subsequent call */
580*0Sstevel@tonic-gate 	p = (string == NULL)? savept: string;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	if (p == NULL)		/* return if no tokens remaining */
583*0Sstevel@tonic-gate 		return (NULL);
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	q = p + strspn(p, sepset);	/* skip leading separators */
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	if (*q == '\0')		/* return if no tokens remaining */
588*0Sstevel@tonic-gate 		return (NULL);
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL)	/* move past token */
591*0Sstevel@tonic-gate 		savept = 0;	/* indicate this is last token */
592*0Sstevel@tonic-gate 	else {
593*0Sstevel@tonic-gate 		*r = '\0';
594*0Sstevel@tonic-gate 		savept = ++r;
595*0Sstevel@tonic-gate 	}
596*0Sstevel@tonic-gate 	return (q);
597*0Sstevel@tonic-gate }
598