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