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