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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
287c478bd9Sstevel@tonic-gate * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate */
30*4656d474SGarrett D'Amore /*
31*4656d474SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
32*4656d474SGarrett D'Amore */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate #include "valtools.h"
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <strings.h>
437c478bd9Sstevel@tonic-gate #include "libadm.h"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static int insert(struct _choice_ *, CKMENU *);
467c478bd9Sstevel@tonic-gate static char *strtoki(char *, char *);
477c478bd9Sstevel@tonic-gate static char **match(CKMENU *, char *, int);
487c478bd9Sstevel@tonic-gate static int getstr(char *, char *, char *, char *, char *);
497c478bd9Sstevel@tonic-gate static int getnum(char *, int, int *, int *);
507c478bd9Sstevel@tonic-gate static struct _choice_ *next(struct _choice_ *);
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static char *deferr;
537c478bd9Sstevel@tonic-gate static char *errmsg;
547c478bd9Sstevel@tonic-gate static char *defhlp;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate #define PROMPT "Enter selection"
577c478bd9Sstevel@tonic-gate #define MESG0 "Entry does not match available menu selection. "
587c478bd9Sstevel@tonic-gate #define MESG1 "the number of the menu item you wish to select, or "
597c478bd9Sstevel@tonic-gate #define MESG2 "the token which is associated with the menu item,\
607c478bd9Sstevel@tonic-gate or a partial string which uniquely identifies the \
617c478bd9Sstevel@tonic-gate token for the menu item. Enter ?? to reprint the menu."
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #define TOOMANY "Too many items selected from menu"
647c478bd9Sstevel@tonic-gate #define NOTUNIQ "The entered text does not uniquely identify a menu choice."
657c478bd9Sstevel@tonic-gate #define BADNUM "Bad numeric choice specification"
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate static char *
setmsg(CKMENU * menup,short flag)687c478bd9Sstevel@tonic-gate setmsg(CKMENU *menup, short flag)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate int n;
717c478bd9Sstevel@tonic-gate char *msg;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate n = (int)(6 + sizeof (MESG2));
747c478bd9Sstevel@tonic-gate if (flag)
757c478bd9Sstevel@tonic-gate n += (int)(sizeof (MESG0));
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate if (menup->attr & CKUNNUM) {
787c478bd9Sstevel@tonic-gate msg = calloc((size_t)n, sizeof (char));
797c478bd9Sstevel@tonic-gate if (flag)
807c478bd9Sstevel@tonic-gate (void) strcpy(msg, MESG0);
817c478bd9Sstevel@tonic-gate else
827c478bd9Sstevel@tonic-gate msg[0] = '\0';
837c478bd9Sstevel@tonic-gate (void) strcat(msg, "Enter ");
847c478bd9Sstevel@tonic-gate (void) strcat(msg, MESG2);
857c478bd9Sstevel@tonic-gate } else {
867c478bd9Sstevel@tonic-gate msg = calloc(n+sizeof (MESG1), sizeof (char));
877c478bd9Sstevel@tonic-gate if (flag)
887c478bd9Sstevel@tonic-gate (void) strcpy(msg, MESG0);
897c478bd9Sstevel@tonic-gate else
907c478bd9Sstevel@tonic-gate msg[0] = '\0';
917c478bd9Sstevel@tonic-gate (void) strcat(msg, "Enter ");
927c478bd9Sstevel@tonic-gate (void) strcat(msg, MESG1);
937c478bd9Sstevel@tonic-gate (void) strcat(msg, MESG2);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate return (msg);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate CKMENU *
allocmenu(char * label,int attr)997c478bd9Sstevel@tonic-gate allocmenu(char *label, int attr)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate CKMENU *pt;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if (pt = calloc(1, sizeof (CKMENU))) {
1047c478bd9Sstevel@tonic-gate pt->attr = attr;
1057c478bd9Sstevel@tonic-gate pt->label = label;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate return (pt);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate void
ckitem_err(CKMENU * menup,char * error)1117c478bd9Sstevel@tonic-gate ckitem_err(CKMENU *menup, char *error)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate deferr = setmsg(menup, 1);
1147c478bd9Sstevel@tonic-gate puterror(stdout, deferr, error);
1157c478bd9Sstevel@tonic-gate free(deferr);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate void
ckitem_hlp(CKMENU * menup,char * help)1197c478bd9Sstevel@tonic-gate ckitem_hlp(CKMENU *menup, char *help)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate defhlp = setmsg(menup, 0);
1227c478bd9Sstevel@tonic-gate puthelp(stdout, defhlp, help);
1237c478bd9Sstevel@tonic-gate free(defhlp);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate int
ckitem(CKMENU * menup,char * item[],short max,char * defstr,char * error,char * help,char * prompt)1277c478bd9Sstevel@tonic-gate ckitem(CKMENU *menup, char *item[], short max, char *defstr, char *error,
1287c478bd9Sstevel@tonic-gate char *help, char *prompt)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate int n, i;
1317c478bd9Sstevel@tonic-gate char strval[MAX_INPUT];
1327c478bd9Sstevel@tonic-gate char **list;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if ((menup->nchoices <= 0) && !menup->invis)
1357c478bd9Sstevel@tonic-gate return (4); /* nothing to choose from */
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate if (menup->attr & CKONEFLAG) {
1387c478bd9Sstevel@tonic-gate if (((n = menup->nchoices) <= 1) && menup->invis) {
1397c478bd9Sstevel@tonic-gate for (i = 0; menup->invis[i]; ++i)
1407c478bd9Sstevel@tonic-gate n++;
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate if (n <= 1) {
1437c478bd9Sstevel@tonic-gate if (menup->choice)
1447c478bd9Sstevel@tonic-gate item[0] = menup->choice->token;
1457c478bd9Sstevel@tonic-gate else if (menup->invis)
1467c478bd9Sstevel@tonic-gate item[0] = menup->invis[0];
1477c478bd9Sstevel@tonic-gate item[1] = NULL;
1487c478bd9Sstevel@tonic-gate return (0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (max < 1)
1537c478bd9Sstevel@tonic-gate max = menup->nchoices;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if (!prompt)
1567c478bd9Sstevel@tonic-gate prompt = PROMPT;
1577c478bd9Sstevel@tonic-gate defhlp = setmsg(menup, 0);
1587c478bd9Sstevel@tonic-gate deferr = setmsg(menup, 1);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate reprint:
1617c478bd9Sstevel@tonic-gate printmenu(menup);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate start:
1647c478bd9Sstevel@tonic-gate if (n = getstr(strval, defstr, error, help, prompt)) {
1657c478bd9Sstevel@tonic-gate free(defhlp);
1667c478bd9Sstevel@tonic-gate free(deferr);
1677c478bd9Sstevel@tonic-gate return (n);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate if (strcmp(strval, "??") == 0) {
1707c478bd9Sstevel@tonic-gate goto reprint;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate if ((defstr) && (strcmp(strval, defstr) == 0)) {
1737c478bd9Sstevel@tonic-gate item[0] = defstr;
1747c478bd9Sstevel@tonic-gate item[1] = NULL;
1757c478bd9Sstevel@tonic-gate } else {
1767c478bd9Sstevel@tonic-gate list = match(menup, strval, (int)max);
1777c478bd9Sstevel@tonic-gate if (!list) {
1787c478bd9Sstevel@tonic-gate puterror(stderr, deferr, (errmsg ? errmsg : error));
1797c478bd9Sstevel@tonic-gate goto start;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate for (i = 0; (i < max); i++)
1827c478bd9Sstevel@tonic-gate item[i] = list[i];
1837c478bd9Sstevel@tonic-gate free(list);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate free(defhlp);
1867c478bd9Sstevel@tonic-gate free(deferr);
1877c478bd9Sstevel@tonic-gate return (0);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate static int
getnum(char * strval,int max,int * begin,int * end)1917c478bd9Sstevel@tonic-gate getnum(char *strval, int max, int *begin, int *end)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate int n;
1947c478bd9Sstevel@tonic-gate char *pt;
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate *begin = *end = 0;
1977c478bd9Sstevel@tonic-gate pt = strval;
1987c478bd9Sstevel@tonic-gate for (;;) {
1997c478bd9Sstevel@tonic-gate if (*pt == '$') {
2007c478bd9Sstevel@tonic-gate n = max;
2017c478bd9Sstevel@tonic-gate pt++;
2027c478bd9Sstevel@tonic-gate } else {
2037c478bd9Sstevel@tonic-gate n = (int)strtol(pt, &pt, 10);
2047c478bd9Sstevel@tonic-gate if ((n <= 0) || (n > max))
2057c478bd9Sstevel@tonic-gate return (1);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate while (isspace((unsigned char)*pt))
2087c478bd9Sstevel@tonic-gate pt++;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if (!*begin && (*pt == '-')) {
2117c478bd9Sstevel@tonic-gate *begin = n;
2127c478bd9Sstevel@tonic-gate pt++;
2137c478bd9Sstevel@tonic-gate while (isspace((unsigned char)*pt))
2147c478bd9Sstevel@tonic-gate pt++;
2157c478bd9Sstevel@tonic-gate continue;
2167c478bd9Sstevel@tonic-gate } else if (*pt) {
2177c478bd9Sstevel@tonic-gate return (1); /* wasn't a number, or an invalid one */
2187c478bd9Sstevel@tonic-gate } else if (*begin) {
2197c478bd9Sstevel@tonic-gate *end = n;
2207c478bd9Sstevel@tonic-gate break;
2217c478bd9Sstevel@tonic-gate } else {
2227c478bd9Sstevel@tonic-gate *begin = n;
2237c478bd9Sstevel@tonic-gate break;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate if (!*end)
2277c478bd9Sstevel@tonic-gate *end = *begin;
2287c478bd9Sstevel@tonic-gate return ((*begin <= *end) ? 0 : 1);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate static char **
match(CKMENU * menup,char * strval,int max)2327c478bd9Sstevel@tonic-gate match(CKMENU *menup, char *strval, int max)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate struct _choice_ *chp;
2357c478bd9Sstevel@tonic-gate char **choice;
2367c478bd9Sstevel@tonic-gate int begin, end;
2377c478bd9Sstevel@tonic-gate char *pt, *found;
2387c478bd9Sstevel@tonic-gate int i, len, nchoice;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate nchoice = 0;
2417c478bd9Sstevel@tonic-gate choice = calloc((size_t)max, sizeof (char *));
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate do {
2447c478bd9Sstevel@tonic-gate if (pt = strpbrk(strval, " \t,")) {
2457c478bd9Sstevel@tonic-gate do {
2467c478bd9Sstevel@tonic-gate *pt++ = '\0';
2477c478bd9Sstevel@tonic-gate } while (strchr(" \t,", *pt));
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate if (nchoice >= max) {
2517c478bd9Sstevel@tonic-gate errmsg = TOOMANY;
2527c478bd9Sstevel@tonic-gate return (NULL);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate if (!(menup->attr & CKUNNUM) &&
2557c478bd9Sstevel@tonic-gate isdigit((unsigned char)*strval)) {
2567c478bd9Sstevel@tonic-gate if (getnum(strval, (int)menup->nchoices, &begin,
2577c478bd9Sstevel@tonic-gate &end)) {
2587c478bd9Sstevel@tonic-gate errmsg = BADNUM;
2597c478bd9Sstevel@tonic-gate return (NULL);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate chp = menup->choice;
2627c478bd9Sstevel@tonic-gate for (i = 1; chp; i++) {
2637c478bd9Sstevel@tonic-gate if ((i >= begin) && (i <= end)) {
2647c478bd9Sstevel@tonic-gate if (nchoice >= max) {
2657c478bd9Sstevel@tonic-gate errmsg = TOOMANY;
2667c478bd9Sstevel@tonic-gate return (NULL);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate choice[nchoice++] = chp->token;
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate chp = chp->next;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate continue;
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate found = NULL;
2767c478bd9Sstevel@tonic-gate chp = menup->choice;
2777c478bd9Sstevel@tonic-gate for (i = 0; chp; i++) {
2787c478bd9Sstevel@tonic-gate len = (int)strlen(strval);
2797c478bd9Sstevel@tonic-gate if (strncmp(chp->token, strval, (size_t)len) == 0) {
2807c478bd9Sstevel@tonic-gate if (chp->token[len] == '\0') {
2817c478bd9Sstevel@tonic-gate found = chp->token;
2827c478bd9Sstevel@tonic-gate break;
2837c478bd9Sstevel@tonic-gate } else if (found) {
2847c478bd9Sstevel@tonic-gate errmsg = NOTUNIQ;
2857c478bd9Sstevel@tonic-gate return (NULL); /* not unique */
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate found = chp->token;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate chp = chp->next;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate if (menup->invis) {
2937c478bd9Sstevel@tonic-gate for (i = 0; menup->invis[i]; ++i) {
2947c478bd9Sstevel@tonic-gate len = (int)strlen(strval);
2957c478bd9Sstevel@tonic-gate if (strncmp(menup->invis[i], strval,
2967c478bd9Sstevel@tonic-gate (size_t)len) == 0) {
2977c478bd9Sstevel@tonic-gate #if _3b2
2987c478bd9Sstevel@tonic-gate if (chp->token[len] == '\0') {
2997c478bd9Sstevel@tonic-gate #else
3007c478bd9Sstevel@tonic-gate if (menup->invis[i][len] == '\0') {
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate found = menup->invis[i];
3037c478bd9Sstevel@tonic-gate break;
3047c478bd9Sstevel@tonic-gate } else if (found) {
3057c478bd9Sstevel@tonic-gate errmsg = NOTUNIQ;
3067c478bd9Sstevel@tonic-gate return (NULL);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate found = menup->invis[i];
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate if (found) {
3137c478bd9Sstevel@tonic-gate choice[nchoice++] = found;
3147c478bd9Sstevel@tonic-gate continue;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate errmsg = NULL;
3177c478bd9Sstevel@tonic-gate return (NULL);
3187c478bd9Sstevel@tonic-gate } while (((strval = pt) != NULL) && *pt);
3197c478bd9Sstevel@tonic-gate return (choice);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate setitem(CKMENU *menup, char *choice)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate struct _choice_ *chp;
3267c478bd9Sstevel@tonic-gate int n;
3277c478bd9Sstevel@tonic-gate char *pt;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate if (choice == NULL) {
3307c478bd9Sstevel@tonic-gate /* request to clear memory usage */
3317c478bd9Sstevel@tonic-gate chp = menup->choice;
3327c478bd9Sstevel@tonic-gate while (chp) {
3337c478bd9Sstevel@tonic-gate struct _choice_ *_chp = chp;
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate chp = chp->next;
3367c478bd9Sstevel@tonic-gate menup->longest = menup->nchoices = 0;
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate (void) free(_chp->token); /* free token and text */
3397c478bd9Sstevel@tonic-gate (void) free(_chp);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate return (1);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate if ((chp = calloc(1, sizeof (struct _choice_))) == NULL)
3457c478bd9Sstevel@tonic-gate return (1);
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if ((pt = strdup(choice)) == NULL) {
3487c478bd9Sstevel@tonic-gate free(chp);
3497c478bd9Sstevel@tonic-gate return (1);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate if (!*pt || isspace((unsigned char)*pt)) {
3527c478bd9Sstevel@tonic-gate free(chp);
3537c478bd9Sstevel@tonic-gate return (2);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate chp->token = strtoki(pt, " \t\n");
3577c478bd9Sstevel@tonic-gate chp->text = strtoki(NULL, "");
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate if (chp->text) {
3607c478bd9Sstevel@tonic-gate while (isspace((unsigned char)*chp->text))
3617c478bd9Sstevel@tonic-gate chp->text++;
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate n = (int)strlen(chp->token);
3647c478bd9Sstevel@tonic-gate if (n > menup->longest)
3657c478bd9Sstevel@tonic-gate menup->longest = (short)n;
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate if (insert(chp, menup))
3687c478bd9Sstevel@tonic-gate menup->nchoices++;
3697c478bd9Sstevel@tonic-gate else
3707c478bd9Sstevel@tonic-gate free(chp); /* duplicate entry */
3717c478bd9Sstevel@tonic-gate return (0);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate int
3757c478bd9Sstevel@tonic-gate setinvis(CKMENU *menup, char *choice)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate int index;
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate index = 0;
3807c478bd9Sstevel@tonic-gate if (choice == NULL) {
3817c478bd9Sstevel@tonic-gate if (menup->invis == NULL)
3827c478bd9Sstevel@tonic-gate return (0);
3837c478bd9Sstevel@tonic-gate while (menup->invis[index])
3847c478bd9Sstevel@tonic-gate free(menup->invis[index]);
3857c478bd9Sstevel@tonic-gate free(menup->invis);
3867c478bd9Sstevel@tonic-gate return (0);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate if (menup->invis == NULL)
3907c478bd9Sstevel@tonic-gate menup->invis = calloc(2, sizeof (char *));
3917c478bd9Sstevel@tonic-gate else {
3927c478bd9Sstevel@tonic-gate while (menup->invis[index])
3937c478bd9Sstevel@tonic-gate index++; /* count invisible choices */
3947c478bd9Sstevel@tonic-gate menup->invis = realloc(menup->invis,
3957c478bd9Sstevel@tonic-gate (index+2)* sizeof (char *));
3967c478bd9Sstevel@tonic-gate menup->invis[index+1] = NULL;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate if (!menup->invis)
3997c478bd9Sstevel@tonic-gate return (-1);
4007c478bd9Sstevel@tonic-gate menup->invis[index] = strdup(choice);
4017c478bd9Sstevel@tonic-gate return (0);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate static int
4057c478bd9Sstevel@tonic-gate insert(struct _choice_ *chp, CKMENU *menup)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate struct _choice_ *last, *base;
4087c478bd9Sstevel@tonic-gate int n;
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate base = menup->choice;
4117c478bd9Sstevel@tonic-gate last = NULL;
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate if (!(menup->attr & CKALPHA)) {
4147c478bd9Sstevel@tonic-gate while (base) {
4157c478bd9Sstevel@tonic-gate if (strcmp(base->token, chp->token) == 0)
4167c478bd9Sstevel@tonic-gate return (0);
4177c478bd9Sstevel@tonic-gate last = base;
4187c478bd9Sstevel@tonic-gate base = base->next;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate if (last)
4217c478bd9Sstevel@tonic-gate last->next = chp;
4227c478bd9Sstevel@tonic-gate else
4237c478bd9Sstevel@tonic-gate menup->choice = chp;
4247c478bd9Sstevel@tonic-gate return (1);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate while (base) {
4287c478bd9Sstevel@tonic-gate if ((n = strcmp(base->token, chp->token)) == 0)
4297c478bd9Sstevel@tonic-gate return (0);
4307c478bd9Sstevel@tonic-gate if (n > 0) {
4317c478bd9Sstevel@tonic-gate /* should come before this one */
4327c478bd9Sstevel@tonic-gate break;
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate last = base;
4357c478bd9Sstevel@tonic-gate base = base->next;
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate if (last) {
4387c478bd9Sstevel@tonic-gate chp->next = last->next;
4397c478bd9Sstevel@tonic-gate last->next = chp;
4407c478bd9Sstevel@tonic-gate } else {
4417c478bd9Sstevel@tonic-gate chp->next = menup->choice;
4427c478bd9Sstevel@tonic-gate menup->choice = chp;
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate return (1);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate void
4487c478bd9Sstevel@tonic-gate printmenu(CKMENU *menup)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate int i;
4517c478bd9Sstevel@tonic-gate struct _choice_ *chp;
4527c478bd9Sstevel@tonic-gate char *pt;
4537c478bd9Sstevel@tonic-gate char format[16];
4547c478bd9Sstevel@tonic-gate int c;
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
4577c478bd9Sstevel@tonic-gate if (menup->label) {
4587c478bd9Sstevel@tonic-gate (void) puttext(stderr, menup->label, 0, 0);
4597c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate (void) sprintf(format, "%%-%ds", menup->longest+5);
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate (void) next(NULL);
4647c478bd9Sstevel@tonic-gate chp = ((menup->attr & CKALPHA) ? next(menup->choice) : menup->choice);
4657c478bd9Sstevel@tonic-gate for (i = 1; chp; ++i) {
4667c478bd9Sstevel@tonic-gate if (!(menup->attr & CKUNNUM))
4677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%3d ", i);
468*4656d474SGarrett D'Amore /* LINTED E_SEC_PRINTF_VAR_FMT */
4697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, format, chp->token);
4707c478bd9Sstevel@tonic-gate if (chp->text) {
4717c478bd9Sstevel@tonic-gate /* there is text associated with the token */
4727c478bd9Sstevel@tonic-gate pt = chp->text;
4737c478bd9Sstevel@tonic-gate while (*pt) {
4747c478bd9Sstevel@tonic-gate (void) fputc(*pt, stderr);
4757c478bd9Sstevel@tonic-gate if (*pt++ == '\n') {
4767c478bd9Sstevel@tonic-gate if (!(menup->attr & CKUNNUM))
4777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4787c478bd9Sstevel@tonic-gate "%5s", "");
479*4656d474SGarrett D'Amore /* LINTED E_SEC_PRINTF_VAR_FMT */
4807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, format, "");
4817c478bd9Sstevel@tonic-gate while (isspace((unsigned char)*pt))
4827c478bd9Sstevel@tonic-gate ++pt;
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
4877c478bd9Sstevel@tonic-gate chp = ((menup->attr & CKALPHA) ?
4887c478bd9Sstevel@tonic-gate next(menup->choice) : chp->next);
4897c478bd9Sstevel@tonic-gate if (chp && ((i % 10) == 0)) {
4907c478bd9Sstevel@tonic-gate /* page the choices */
4917c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4927c478bd9Sstevel@tonic-gate "\n... %d more menu choices to follow;",
4937c478bd9Sstevel@tonic-gate menup->nchoices - i);
4947c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4957c478bd9Sstevel@tonic-gate /* CSTYLED */
4967c478bd9Sstevel@tonic-gate "\n<RETURN> for more choices, <CTRL-D> to stop \
4977c478bd9Sstevel@tonic-gate display:");
4987c478bd9Sstevel@tonic-gate /* ignore other chars */
4997c478bd9Sstevel@tonic-gate while (((c = getc(stdin)) != EOF) && (c != '\n'))
5007c478bd9Sstevel@tonic-gate ;
5017c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
5027c478bd9Sstevel@tonic-gate if (c == EOF)
5037c478bd9Sstevel@tonic-gate break; /* stop printing menu */
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate static int
5097c478bd9Sstevel@tonic-gate getstr(char *strval, char *defstr, char *error, char *help, char *prompt)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate char input[MAX_INPUT];
512*4656d474SGarrett D'Amore char end[MAX_INPUT];
5137c478bd9Sstevel@tonic-gate
514*4656d474SGarrett D'Amore *end = '\0';
5157c478bd9Sstevel@tonic-gate if (defstr) {
516*4656d474SGarrett D'Amore (void) snprintf(end, MAX_INPUT, "(default: %s) ", defstr);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate if (ckquit) {
519*4656d474SGarrett D'Amore (void) strlcat(end, "[?,??,q]", MAX_INPUT);
5207c478bd9Sstevel@tonic-gate } else {
521*4656d474SGarrett D'Amore (void) strlcat(end, "[?,??]", MAX_INPUT);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate start:
5257c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
5267c478bd9Sstevel@tonic-gate (void) puttext(stderr, prompt, 0, 0);
5277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " %s: ", end);
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate if (getinput(input))
5307c478bd9Sstevel@tonic-gate return (1);
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate if (strlen(input) == 0) {
5337c478bd9Sstevel@tonic-gate if (defstr) {
5347c478bd9Sstevel@tonic-gate (void) strcpy(strval, defstr);
5357c478bd9Sstevel@tonic-gate return (0);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate puterror(stderr, deferr, (errmsg ? errmsg : error));
5387c478bd9Sstevel@tonic-gate goto start;
5397c478bd9Sstevel@tonic-gate } else if (strcmp(input, "?") == 0) {
5407c478bd9Sstevel@tonic-gate puthelp(stderr, defhlp, help);
5417c478bd9Sstevel@tonic-gate goto start;
5427c478bd9Sstevel@tonic-gate } else if (ckquit && (strcmp(input, "q") == 0)) {
5437c478bd9Sstevel@tonic-gate /* (void) strcpy(strval, input); */
5447c478bd9Sstevel@tonic-gate return (3);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate (void) strcpy(strval, input);
5477c478bd9Sstevel@tonic-gate return (0);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate static struct _choice_ *
5517c478bd9Sstevel@tonic-gate next(struct _choice_ *chp)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate static char *last;
5547c478bd9Sstevel@tonic-gate static char *first;
5557c478bd9Sstevel@tonic-gate struct _choice_ *found;
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate if (!chp) {
5587c478bd9Sstevel@tonic-gate last = NULL;
5597c478bd9Sstevel@tonic-gate return (NULL);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate found = NULL;
5637c478bd9Sstevel@tonic-gate for (first = NULL; chp; chp = chp->next) {
5647c478bd9Sstevel@tonic-gate if (last && strcmp(last, chp->token) >= 0)
5657c478bd9Sstevel@tonic-gate continue; /* lower than the last one we found */
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate if (!first || strcmp(first, chp->token) > 0) {
5687c478bd9Sstevel@tonic-gate first = chp->token;
5697c478bd9Sstevel@tonic-gate found = chp;
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate last = first;
5737c478bd9Sstevel@tonic-gate return (found);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate static char *
5777c478bd9Sstevel@tonic-gate strtoki(char *string, char *sepset)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate char *p, *q, *r;
5807c478bd9Sstevel@tonic-gate static char *savept;
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate /* first or subsequent call */
5837c478bd9Sstevel@tonic-gate p = (string == NULL)? savept: string;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate if (p == NULL) /* return if no tokens remaining */
5867c478bd9Sstevel@tonic-gate return (NULL);
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate q = p + strspn(p, sepset); /* skip leading separators */
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate if (*q == '\0') /* return if no tokens remaining */
5917c478bd9Sstevel@tonic-gate return (NULL);
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate if ((r = strpbrk(q, sepset)) == NULL) /* move past token */
5947c478bd9Sstevel@tonic-gate savept = 0; /* indicate this is last token */
5957c478bd9Sstevel@tonic-gate else {
5967c478bd9Sstevel@tonic-gate *r = '\0';
5977c478bd9Sstevel@tonic-gate savept = ++r;
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate return (q);
6007c478bd9Sstevel@tonic-gate }
601