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 2004 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" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <limits.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 37*7c478bd9Sstevel@tonic-gate #include "libadm.h" 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate /* 40*7c478bd9Sstevel@tonic-gate * This file is the only one anywhere to need these functions, 41*7c478bd9Sstevel@tonic-gate * so we declare them here, not in libadm.h 42*7c478bd9Sstevel@tonic-gate */ 43*7c478bd9Sstevel@tonic-gate extern char *__compile(char *, char *, const char *, int); 44*7c478bd9Sstevel@tonic-gate extern int __step(const char *, const char *); 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #define ESIZE 1024 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #define ERRMSG0 "Input is required." 49*7c478bd9Sstevel@tonic-gate #define ERRMSG1 "Please enter a string containing no more than %d characters." 50*7c478bd9Sstevel@tonic-gate #define ERRMSG2 \ 51*7c478bd9Sstevel@tonic-gate "Pattern matching has failed." 52*7c478bd9Sstevel@tonic-gate #define ERRMSG3 \ 53*7c478bd9Sstevel@tonic-gate "Please enter a string which contains no imbedded, \ 54*7c478bd9Sstevel@tonic-gate leading or trailing spaces or tabs." 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate #define HLPMSG0 "Please enter a string" 57*7c478bd9Sstevel@tonic-gate #define HLPMSG1 "Please enter a string containing no more than %d characters" 58*7c478bd9Sstevel@tonic-gate #define HLPMSG2 "matches one of the following patterns:" 59*7c478bd9Sstevel@tonic-gate #define HLPMSG3 "matches the following pattern:" 60*7c478bd9Sstevel@tonic-gate #define HLPMSG4 "contains no imbedded, leading or trailing spaces or tabs." 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate static char *errstr; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate static char * 65*7c478bd9Sstevel@tonic-gate sethlp(char *msg, char *regexp[], int length) 66*7c478bd9Sstevel@tonic-gate { 67*7c478bd9Sstevel@tonic-gate int i; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate if (length) 70*7c478bd9Sstevel@tonic-gate (void) sprintf(msg, HLPMSG1, length); 71*7c478bd9Sstevel@tonic-gate else 72*7c478bd9Sstevel@tonic-gate (void) strcpy(msg, HLPMSG0); 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate (void) strcat(msg, length ? " and " : " which "); 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate if (regexp && regexp[0]) { 77*7c478bd9Sstevel@tonic-gate (void) strcat(msg, regexp[1] ? HLPMSG2 : HLPMSG3); 78*7c478bd9Sstevel@tonic-gate for (i = 0; regexp[i]; i++) { 79*7c478bd9Sstevel@tonic-gate (void) strcat(msg, "\\n\\t"); 80*7c478bd9Sstevel@tonic-gate (void) strcat(msg, regexp[i]); 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate } else 83*7c478bd9Sstevel@tonic-gate (void) strcat(msg, HLPMSG4); 84*7c478bd9Sstevel@tonic-gate return (msg); 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate int 88*7c478bd9Sstevel@tonic-gate ckstr_val(char *regexp[], int length, char *input) 89*7c478bd9Sstevel@tonic-gate { 90*7c478bd9Sstevel@tonic-gate char expbuf[ESIZE]; 91*7c478bd9Sstevel@tonic-gate int i, valid; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate valid = 1; 94*7c478bd9Sstevel@tonic-gate if (length && (strlen(input) > (size_t)length)) { 95*7c478bd9Sstevel@tonic-gate errstr = ERRMSG1; 96*7c478bd9Sstevel@tonic-gate return (1); 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate if (regexp && regexp[0]) { 99*7c478bd9Sstevel@tonic-gate valid = 0; 100*7c478bd9Sstevel@tonic-gate for (i = 0; !valid && regexp[i]; ++i) { 101*7c478bd9Sstevel@tonic-gate if (!__compile(regexp[i], expbuf, &expbuf[ESIZE], '\0')) 102*7c478bd9Sstevel@tonic-gate return (2); 103*7c478bd9Sstevel@tonic-gate valid = __step(input, expbuf); 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate if (!valid) 106*7c478bd9Sstevel@tonic-gate errstr = ERRMSG2; 107*7c478bd9Sstevel@tonic-gate } else if (strpbrk(input, " \t")) { 108*7c478bd9Sstevel@tonic-gate errstr = ERRMSG3; 109*7c478bd9Sstevel@tonic-gate valid = 0; 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate return (valid == 0); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate void 115*7c478bd9Sstevel@tonic-gate ckstr_err(char *regexp[], int length, char *error, char *input) 116*7c478bd9Sstevel@tonic-gate { 117*7c478bd9Sstevel@tonic-gate char *defhlp; 118*7c478bd9Sstevel@tonic-gate char temp[1024]; 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate if (input) { 121*7c478bd9Sstevel@tonic-gate if (ckstr_val(regexp, length, input)) { 122*7c478bd9Sstevel@tonic-gate (void) sprintf(temp, errstr, length); 123*7c478bd9Sstevel@tonic-gate puterror(stdout, temp, error); 124*7c478bd9Sstevel@tonic-gate return; 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate defhlp = sethlp(temp, regexp, length); 129*7c478bd9Sstevel@tonic-gate puterror(stdout, defhlp, error); 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate void 133*7c478bd9Sstevel@tonic-gate ckstr_hlp(char *regexp[], int length, char *help) 134*7c478bd9Sstevel@tonic-gate { 135*7c478bd9Sstevel@tonic-gate char *defhlp; 136*7c478bd9Sstevel@tonic-gate char hlpbuf[1024]; 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate defhlp = sethlp(hlpbuf, regexp, length); 139*7c478bd9Sstevel@tonic-gate puthelp(stdout, defhlp, help); 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate int 143*7c478bd9Sstevel@tonic-gate ckstr(char *strval, char *regexp[], int length, char *defstr, char *error, 144*7c478bd9Sstevel@tonic-gate char *help, char *prompt) 145*7c478bd9Sstevel@tonic-gate { 146*7c478bd9Sstevel@tonic-gate int n; 147*7c478bd9Sstevel@tonic-gate char *defhlp; 148*7c478bd9Sstevel@tonic-gate char input[MAX_INPUT], 149*7c478bd9Sstevel@tonic-gate hlpbuf[1024], 150*7c478bd9Sstevel@tonic-gate errbuf[1024]; 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate defhlp = NULL; 153*7c478bd9Sstevel@tonic-gate if (!prompt) 154*7c478bd9Sstevel@tonic-gate prompt = "Enter an appropriate value"; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate start: 157*7c478bd9Sstevel@tonic-gate putprmpt(stderr, prompt, NULL, defstr); 158*7c478bd9Sstevel@tonic-gate if (getinput(input)) 159*7c478bd9Sstevel@tonic-gate return (1); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate n = (int)strlen(input); 162*7c478bd9Sstevel@tonic-gate if (n == 0) { 163*7c478bd9Sstevel@tonic-gate if (defstr) { 164*7c478bd9Sstevel@tonic-gate (void) strcpy(strval, defstr); 165*7c478bd9Sstevel@tonic-gate return (0); 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate puterror(stderr, ERRMSG0, error); 168*7c478bd9Sstevel@tonic-gate goto start; 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate if (strcmp(input, "?") == 0) { 171*7c478bd9Sstevel@tonic-gate if (defhlp == NULL) 172*7c478bd9Sstevel@tonic-gate defhlp = sethlp(hlpbuf, regexp, length); 173*7c478bd9Sstevel@tonic-gate puthelp(stderr, defhlp, help); 174*7c478bd9Sstevel@tonic-gate goto start; 175*7c478bd9Sstevel@tonic-gate } 176*7c478bd9Sstevel@tonic-gate if (ckquit && (strcmp(input, "q") == 0)) { 177*7c478bd9Sstevel@tonic-gate (void) strcpy(strval, input); 178*7c478bd9Sstevel@tonic-gate return (3); 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate if (ckstr_val(regexp, length, input)) { 181*7c478bd9Sstevel@tonic-gate (void) sprintf(errbuf, errstr, length); 182*7c478bd9Sstevel@tonic-gate puterror(stderr, errbuf, error); 183*7c478bd9Sstevel@tonic-gate goto start; 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate (void) strcpy(strval, input); 186*7c478bd9Sstevel@tonic-gate return (0); 187*7c478bd9Sstevel@tonic-gate } 188