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