xref: /illumos-gate/usr/src/cmd/valtools/ckitem.c (revision bc54f855)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31*bc54f855SJohn Levon /*
32*bc54f855SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
33*bc54f855SJohn Levon  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <valtools.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <libintl.h>
437c478bd9Sstevel@tonic-gate #include <limits.h>
447c478bd9Sstevel@tonic-gate #include <wchar.h>
457c478bd9Sstevel@tonic-gate #include "usage.h"
467c478bd9Sstevel@tonic-gate #include "libadm.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	BADPID	(-2)
497c478bd9Sstevel@tonic-gate #define	INVISMAXSIZE 36
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char	*prog;
527c478bd9Sstevel@tonic-gate static char	*deflt = NULL, *prompt = NULL, *error = NULL, *help = NULL;
537c478bd9Sstevel@tonic-gate static int	kpid = BADPID;
547c478bd9Sstevel@tonic-gate static int	signo;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static char	*label, **invis;
577c478bd9Sstevel@tonic-gate static int	ninvis = 0;
587c478bd9Sstevel@tonic-gate static int	max = 1;
597c478bd9Sstevel@tonic-gate static int	attr = CKALPHA;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	MAXSIZE	128
627c478bd9Sstevel@tonic-gate #define	LSIZE	1024
637c478bd9Sstevel@tonic-gate #define	INTERR	\
647c478bd9Sstevel@tonic-gate 	"%s: ERROR: internal error occurred while attempting menu setup\n"
657c478bd9Sstevel@tonic-gate #define	MYOPTS \
667c478bd9Sstevel@tonic-gate 	"\t-f file             #file containing choices\n" \
677c478bd9Sstevel@tonic-gate 	"\t-l label            #menu label\n" \
687c478bd9Sstevel@tonic-gate 	"\t-i invis [, ...]    #invisible menu choices\n" \
697c478bd9Sstevel@tonic-gate 	"\t-m max              #maximum choices user may select\n" \
707c478bd9Sstevel@tonic-gate 	"\t-n                  #do not sort choices alphabetically\n" \
717c478bd9Sstevel@tonic-gate 	"\t-o                  #don't prompt if only one choice\n" \
727c478bd9Sstevel@tonic-gate 	"\t-u                  #unnumbered choices\n"
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static const char	husage[] = "Wh";
757c478bd9Sstevel@tonic-gate static const char	eusage[] = "We";
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void
usage(void)787c478bd9Sstevel@tonic-gate usage(void)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	switch (*prog) {
817c478bd9Sstevel@tonic-gate 	default:
827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
837c478bd9Sstevel@tonic-gate 			gettext("usage: %s [options] [choice [...]]\n"), prog);
847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(OPTMESG));
857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(MYOPTS));
867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(STDOPTS));
877c478bd9Sstevel@tonic-gate 		break;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	case 'h':
907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
917c478bd9Sstevel@tonic-gate 			gettext("usage: %s [options] [choice [...]]\n"), prog);
927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(OPTMESG));
937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
947c478bd9Sstevel@tonic-gate 			gettext("\t-W width\n\t-h help\n"));
957c478bd9Sstevel@tonic-gate 		break;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	case 'e':
987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
997c478bd9Sstevel@tonic-gate 			gettext("usage: %s [options] [choice [...]]\n"), prog);
1007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(OPTMESG));
1017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1027c478bd9Sstevel@tonic-gate 			gettext("\t-W width\n\t-e error\n"));
1037c478bd9Sstevel@tonic-gate 		break;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	exit(1);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Given argv[0], return a pointer to the basename of the program.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate static char *
prog_name(char * arg0)1127c478bd9Sstevel@tonic-gate prog_name(char *arg0)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	char *str;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* first strip trailing '/' characters (exec() allows these!) */
1177c478bd9Sstevel@tonic-gate 	str = arg0 + strlen(arg0);
1187c478bd9Sstevel@tonic-gate 	while (str > arg0 && *--str == '/')
1197c478bd9Sstevel@tonic-gate 		*str = '\0';
1207c478bd9Sstevel@tonic-gate 	if ((str = strrchr(arg0, '/')) != NULL)
1217c478bd9Sstevel@tonic-gate 		return (str + 1);
1227c478bd9Sstevel@tonic-gate 	return (arg0);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1267c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	CKMENU	*mp;
1297c478bd9Sstevel@tonic-gate 	FILE	*fp = NULL;
1307c478bd9Sstevel@tonic-gate 	int	c, i;
1317c478bd9Sstevel@tonic-gate 	char	**item;
1327c478bd9Sstevel@tonic-gate 	char	temp[LSIZE * MB_LEN_MAX];
1337c478bd9Sstevel@tonic-gate 	size_t	mmax;
1347c478bd9Sstevel@tonic-gate 	size_t invismaxsize = INVISMAXSIZE;
1357c478bd9Sstevel@tonic-gate 	size_t	n, r;
1367c478bd9Sstevel@tonic-gate 	wchar_t	wline[LSIZE], wtemp[LSIZE];
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
1417c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	prog = prog_name(argv[0]);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	invis = (char **)calloc(invismaxsize, sizeof (char *));
1487c478bd9Sstevel@tonic-gate 	if (!invis) {
1497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1507c478bd9Sstevel@tonic-gate 			gettext("Not enough memory\n"));
151*bc54f855SJohn Levon 		exit(1);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "m:oni:l:f:ud:p:e:h:k:s:QW:?")) != EOF) {
1547c478bd9Sstevel@tonic-gate 		/* check for invalid option */
1557c478bd9Sstevel@tonic-gate 		if ((*prog == 'e') && !strchr(eusage, c))
1567c478bd9Sstevel@tonic-gate 			usage(); /* no valid options */
1577c478bd9Sstevel@tonic-gate 		if ((*prog == 'h') && !strchr(husage, c))
1587c478bd9Sstevel@tonic-gate 			usage();
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		switch (c) {
1617c478bd9Sstevel@tonic-gate 		case 'Q':
1627c478bd9Sstevel@tonic-gate 			ckquit = 0;
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		case 'W':
1667c478bd9Sstevel@tonic-gate 			ckwidth = atol(optarg);
1677c478bd9Sstevel@tonic-gate 			if (ckwidth < 0) {
1687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1697c478bd9Sstevel@tonic-gate 		gettext("%s: ERROR: negative display width specified\n"),
1707c478bd9Sstevel@tonic-gate 					prog);
1717c478bd9Sstevel@tonic-gate 				exit(1);
1727c478bd9Sstevel@tonic-gate 			}
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		case 'm':
1767c478bd9Sstevel@tonic-gate 			max = atoi(optarg);
1777c478bd9Sstevel@tonic-gate 			if (max > SHRT_MAX || max < SHRT_MIN) {
1787c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1797c478bd9Sstevel@tonic-gate 	gettext("%s: ERROR: too large or too small max value specified\n"),
1807c478bd9Sstevel@tonic-gate 					prog);
1817c478bd9Sstevel@tonic-gate 				exit(1);
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		case 'o':
1867c478bd9Sstevel@tonic-gate 			attr |= CKONEFLAG;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		case 'n':
1907c478bd9Sstevel@tonic-gate 			attr &= ~CKALPHA;
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case 'i':
1947c478bd9Sstevel@tonic-gate 			invis[ninvis++] = optarg;
1957c478bd9Sstevel@tonic-gate 			if (ninvis == invismaxsize) {
1967c478bd9Sstevel@tonic-gate 				invismaxsize += INVISMAXSIZE;
1977c478bd9Sstevel@tonic-gate 				invis = (char **)realloc(invis,
1987c478bd9Sstevel@tonic-gate 						invismaxsize * sizeof (char *));
1997c478bd9Sstevel@tonic-gate 				if (!invis) {
2007c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
2017c478bd9Sstevel@tonic-gate 						gettext("Not enough memory\n"));
202*bc54f855SJohn Levon 					exit(1);
2037c478bd9Sstevel@tonic-gate 				}
2047c478bd9Sstevel@tonic-gate 				(void) memset(invis + ninvis, 0,
2057c478bd9Sstevel@tonic-gate 					(invismaxsize - ninvis) *
2067c478bd9Sstevel@tonic-gate 					sizeof (char *));
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		case 'l':
2117c478bd9Sstevel@tonic-gate 			label = optarg;
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		case 'f':
2157c478bd9Sstevel@tonic-gate 			if ((fp = fopen(optarg, "r")) == NULL) {
2167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2177c478bd9Sstevel@tonic-gate 					gettext("%s: ERROR: can't open %s\n"),
2187c478bd9Sstevel@tonic-gate 					prog, optarg);
2197c478bd9Sstevel@tonic-gate 				exit(1);
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		case 'u':
2247c478bd9Sstevel@tonic-gate 			attr |= CKUNNUM;
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		case 'd':
2287c478bd9Sstevel@tonic-gate 			deflt = optarg;
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		case 'p':
2327c478bd9Sstevel@tonic-gate 			prompt = optarg;
2337c478bd9Sstevel@tonic-gate 			break;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		case 'e':
2367c478bd9Sstevel@tonic-gate 			error = optarg;
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		case 'h':
2407c478bd9Sstevel@tonic-gate 			help = optarg;
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		case 'k':
2447c478bd9Sstevel@tonic-gate 			kpid = atoi(optarg);
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		case 's':
2487c478bd9Sstevel@tonic-gate 			signo = atoi(optarg);
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		default:
2527c478bd9Sstevel@tonic-gate 			usage();
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (signo) {
2577c478bd9Sstevel@tonic-gate 		if (kpid == BADPID)
2587c478bd9Sstevel@tonic-gate 			usage();
2597c478bd9Sstevel@tonic-gate 	} else
2607c478bd9Sstevel@tonic-gate 		signo = SIGTERM;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	mp = allocmenu(label, attr);
2637c478bd9Sstevel@tonic-gate 	if (fp) {
2647c478bd9Sstevel@tonic-gate 		*wtemp = L'\0';
2657c478bd9Sstevel@tonic-gate 		while (fgetws(wline, LSIZE, fp)) {
2667c478bd9Sstevel@tonic-gate 			/*
2677c478bd9Sstevel@tonic-gate 			 * Skip comment lines, those beginning with '#'.
2687c478bd9Sstevel@tonic-gate 			 * Note:  AT&T forgot this & needs the next 2 lines.
2697c478bd9Sstevel@tonic-gate 			 */
2707c478bd9Sstevel@tonic-gate 			if (*wline == L'#')
2717c478bd9Sstevel@tonic-gate 				continue;
2727c478bd9Sstevel@tonic-gate 			n = wcslen(wline);
2737c478bd9Sstevel@tonic-gate 			if ((n != 0) && (wline[n - 1] == L'\n'))
2747c478bd9Sstevel@tonic-gate 				wline[n - 1] = L'\0';
2757c478bd9Sstevel@tonic-gate 			/*
2767c478bd9Sstevel@tonic-gate 			 * if the line begins with a space character,
2777c478bd9Sstevel@tonic-gate 			 * this is a continuous line to the previous line.
2787c478bd9Sstevel@tonic-gate 			 */
2797c478bd9Sstevel@tonic-gate 			if (iswspace(*wline)) {
2807c478bd9Sstevel@tonic-gate 				(void) wcscat(wtemp, L"\n");
2817c478bd9Sstevel@tonic-gate 				(void) wcscat(wtemp, wline);
2827c478bd9Sstevel@tonic-gate 			} else {
2837c478bd9Sstevel@tonic-gate 				if (*wtemp) {
2847c478bd9Sstevel@tonic-gate 					n = wcslen(wtemp);
2857c478bd9Sstevel@tonic-gate 					r = wcstombs(temp, wtemp,
2867c478bd9Sstevel@tonic-gate 						n * MB_LEN_MAX);
2877c478bd9Sstevel@tonic-gate 					if (r == (size_t)-1) {
2887c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
2897c478bd9Sstevel@tonic-gate 			gettext("Invalid character in the menu definition.\n"));
2907c478bd9Sstevel@tonic-gate 						exit(1);
2917c478bd9Sstevel@tonic-gate 					}
2927c478bd9Sstevel@tonic-gate 					if (setitem(mp, temp)) {
2937c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
2947c478bd9Sstevel@tonic-gate 							gettext(INTERR), prog);
2957c478bd9Sstevel@tonic-gate 						exit(1);
2967c478bd9Sstevel@tonic-gate 					}
2977c478bd9Sstevel@tonic-gate 				}
2987c478bd9Sstevel@tonic-gate 				(void) wcscpy(wtemp, wline);
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 		if (*wtemp) {
3027c478bd9Sstevel@tonic-gate 			n = wcslen(wtemp);
3037c478bd9Sstevel@tonic-gate 			r = wcstombs(temp, wtemp, n * MB_LEN_MAX);
3047c478bd9Sstevel@tonic-gate 			if (r == (size_t)-1) {
3057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3067c478bd9Sstevel@tonic-gate 			gettext("Invalid character in the menu definition.\n"));
3077c478bd9Sstevel@tonic-gate 				exit(1);
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 			if (setitem(mp, temp)) {
3107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(INTERR), prog);
3117c478bd9Sstevel@tonic-gate 				exit(1);
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	while (optind < argc) {
3177c478bd9Sstevel@tonic-gate 		if (setitem(mp, argv[optind++])) {
3187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(INTERR), prog);
3197c478bd9Sstevel@tonic-gate 			exit(1);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	for (n = 0; n < ninvis; ) {
3247c478bd9Sstevel@tonic-gate 		if (setinvis(mp, invis[n++])) {
3257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(INTERR), prog);
3267c478bd9Sstevel@tonic-gate 			exit(1);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (*prog == 'e') {
3317c478bd9Sstevel@tonic-gate 		ckindent = 0;
3327c478bd9Sstevel@tonic-gate 		ckitem_err(mp, error);
3337c478bd9Sstevel@tonic-gate 		exit(0);
3347c478bd9Sstevel@tonic-gate 	} else if (*prog == 'h') {
3357c478bd9Sstevel@tonic-gate 		ckindent = 0;
3367c478bd9Sstevel@tonic-gate 		ckitem_hlp(mp, help);
3377c478bd9Sstevel@tonic-gate 		exit(0);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (max < 1) {
3417c478bd9Sstevel@tonic-gate 		mmax = mp->nchoices;
3427c478bd9Sstevel@tonic-gate 	} else {
3437c478bd9Sstevel@tonic-gate 		mmax = max;
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * if -o option is specified, mp->nchoices is 1, and if no invisible
3487c478bd9Sstevel@tonic-gate  * item is specified, ckitem() will consume two entries of item,
3497c478bd9Sstevel@tonic-gate  * even though 'max' is set to 1. So to take care of that problem, we
3507c478bd9Sstevel@tonic-gate  * allocate one extra element for item
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate 	item = (char **)calloc(mmax+1, sizeof (char *));
3537c478bd9Sstevel@tonic-gate 	if (!item) {
3547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3557c478bd9Sstevel@tonic-gate 			gettext("Not enough memory\n"));
3567c478bd9Sstevel@tonic-gate 		exit(1);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	n = ckitem(mp, item, max, deflt, error, help, prompt);
3597c478bd9Sstevel@tonic-gate 	if (n == 3) {
3607c478bd9Sstevel@tonic-gate 		if (kpid > -2)
3617c478bd9Sstevel@tonic-gate 			(void) kill(kpid, signo);
3627c478bd9Sstevel@tonic-gate 		(void) puts("q");
3637c478bd9Sstevel@tonic-gate 	} else if (n == 0) {
3647c478bd9Sstevel@tonic-gate 		i = 0;
3657c478bd9Sstevel@tonic-gate 		while (item[i])
3667c478bd9Sstevel@tonic-gate 			(void) puts(item[i++]);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	return (n);
3697c478bd9Sstevel@tonic-gate }
370