1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
24
25
26 /*
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 #pragma ident "%Z%%M% %I% %E% SMI"
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/types.h>
37 #include "libadm.h"
38
39 /*
40 * This file is the only one anywhere to need these functions,
41 * so we declare them here, not in libadm.h
42 */
43 extern char *__compile(char *, char *, const char *, int);
44 extern int __step(const char *, const char *);
45
46 #define ESIZE 1024
47
48 #define ERRMSG0 "Input is required."
49 #define ERRMSG1 "Please enter a string containing no more than %d characters."
50 #define ERRMSG2 \
51 "Pattern matching has failed."
52 #define ERRMSG3 \
53 "Please enter a string which contains no imbedded, \
54 leading or trailing spaces or tabs."
55
56 #define HLPMSG0 "Please enter a string"
57 #define HLPMSG1 "Please enter a string containing no more than %d characters"
58 #define HLPMSG2 "matches one of the following patterns:"
59 #define HLPMSG3 "matches the following pattern:"
60 #define HLPMSG4 "contains no imbedded, leading or trailing spaces or tabs."
61
62 static char *errstr;
63
64 static char *
sethlp(char * msg,char * regexp[],int length)65 sethlp(char *msg, char *regexp[], int length)
66 {
67 int i;
68
69 if (length)
70 (void) sprintf(msg, HLPMSG1, length);
71 else
72 (void) strcpy(msg, HLPMSG0);
73
74 (void) strcat(msg, length ? " and " : " which ");
75
76 if (regexp && regexp[0]) {
77 (void) strcat(msg, regexp[1] ? HLPMSG2 : HLPMSG3);
78 for (i = 0; regexp[i]; i++) {
79 (void) strcat(msg, "\\n\\t");
80 (void) strcat(msg, regexp[i]);
81 }
82 } else
83 (void) strcat(msg, HLPMSG4);
84 return (msg);
85 }
86
87 int
ckstr_val(char * regexp[],int length,char * input)88 ckstr_val(char *regexp[], int length, char *input)
89 {
90 char expbuf[ESIZE];
91 int i, valid;
92
93 valid = 1;
94 if (length && (strlen(input) > (size_t)length)) {
95 errstr = ERRMSG1;
96 return (1);
97 }
98 if (regexp && regexp[0]) {
99 valid = 0;
100 for (i = 0; !valid && regexp[i]; ++i) {
101 if (!__compile(regexp[i], expbuf, &expbuf[ESIZE], '\0'))
102 return (2);
103 valid = __step(input, expbuf);
104 }
105 if (!valid)
106 errstr = ERRMSG2;
107 } else if (strpbrk(input, " \t")) {
108 errstr = ERRMSG3;
109 valid = 0;
110 }
111 return (valid == 0);
112 }
113
114 void
ckstr_err(char * regexp[],int length,char * error,char * input)115 ckstr_err(char *regexp[], int length, char *error, char *input)
116 {
117 char *defhlp;
118 char temp[1024];
119
120 if (input) {
121 if (ckstr_val(regexp, length, input)) {
122 (void) sprintf(temp, errstr, length);
123 puterror(stdout, temp, error);
124 return;
125 }
126 }
127
128 defhlp = sethlp(temp, regexp, length);
129 puterror(stdout, defhlp, error);
130 }
131
132 void
ckstr_hlp(char * regexp[],int length,char * help)133 ckstr_hlp(char *regexp[], int length, char *help)
134 {
135 char *defhlp;
136 char hlpbuf[1024];
137
138 defhlp = sethlp(hlpbuf, regexp, length);
139 puthelp(stdout, defhlp, help);
140 }
141
142 int
ckstr(char * strval,char * regexp[],int length,char * defstr,char * error,char * help,char * prompt)143 ckstr(char *strval, char *regexp[], int length, char *defstr, char *error,
144 char *help, char *prompt)
145 {
146 int n;
147 char *defhlp;
148 char input[MAX_INPUT],
149 hlpbuf[1024],
150 errbuf[1024];
151
152 defhlp = NULL;
153 if (!prompt)
154 prompt = "Enter an appropriate value";
155
156 start:
157 putprmpt(stderr, prompt, NULL, defstr);
158 if (getinput(input))
159 return (1);
160
161 n = (int)strlen(input);
162 if (n == 0) {
163 if (defstr) {
164 (void) strcpy(strval, defstr);
165 return (0);
166 }
167 puterror(stderr, ERRMSG0, error);
168 goto start;
169 }
170 if (strcmp(input, "?") == 0) {
171 if (defhlp == NULL)
172 defhlp = sethlp(hlpbuf, regexp, length);
173 puthelp(stderr, defhlp, help);
174 goto start;
175 }
176 if (ckquit && (strcmp(input, "q") == 0)) {
177 (void) strcpy(strval, input);
178 return (3);
179 }
180 if (ckstr_val(regexp, length, input)) {
181 (void) sprintf(errbuf, errstr, length);
182 puterror(stderr, errbuf, error);
183 goto start;
184 }
185 (void) strcpy(strval, input);
186 return (0);
187 }
188