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