dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libadm / common / ckstr.c
blobb72d6900b0248e4ba6052236728a2d2ff298edbc
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #include <stdio.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <sys/types.h>
35 #include "libadm.h"
38 * This file is the only one anywhere to need these functions,
39 * so we declare them here, not in libadm.h
41 extern char *__compile(char *, char *, const char *, int);
42 extern int __step(const char *, const char *);
44 #define ESIZE 1024
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."
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."
60 static char *errstr;
62 static char *
63 sethlp(char *msg, char *regexp[], int length)
65 int i;
67 if (length)
68 (void) sprintf(msg, HLPMSG1, length);
69 else
70 (void) strcpy(msg, HLPMSG0);
72 (void) strcat(msg, length ? " and " : " which ");
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]);
80 } else
81 (void) strcat(msg, HLPMSG4);
82 return (msg);
85 int
86 ckstr_val(char *regexp[], int length, char *input)
88 char expbuf[ESIZE];
89 int i, valid;
91 valid = 1;
92 if (length && (strlen(input) > (size_t)length)) {
93 errstr = ERRMSG1;
94 return (1);
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);
103 if (!valid)
104 errstr = ERRMSG2;
105 } else if (strpbrk(input, " \t")) {
106 errstr = ERRMSG3;
107 valid = 0;
109 return (valid == 0);
112 void
113 ckstr_err(char *regexp[], int length, char *error, char *input)
115 char *defhlp;
116 char temp[1024];
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;
127 defhlp = sethlp(temp, regexp, length);
128 puterror(stdout, defhlp, error);
131 void
132 ckstr_hlp(char *regexp[], int length, char *help)
134 char *defhlp;
135 char hlpbuf[1024];
137 defhlp = sethlp(hlpbuf, regexp, length);
138 puthelp(stdout, defhlp, help);
142 ckstr(char *strval, char *regexp[], int length, char *defstr, char *error,
143 char *help, char *prompt)
145 int n;
146 char *defhlp;
147 char input[MAX_INPUT],
148 hlpbuf[1024],
149 errbuf[1024];
151 defhlp = NULL;
152 if (!prompt)
153 prompt = "Enter an appropriate value";
155 start:
156 putprmpt(stderr, prompt, NULL, defstr);
157 if (getinput(input))
158 return (1);
160 n = (int)strlen(input);
161 if (n == 0) {
162 if (defstr) {
163 (void) strcpy(strval, defstr);
164 return (0);
166 puterror(stderr, ERRMSG0, error);
167 goto start;
169 if (strcmp(input, "?") == 0) {
170 if (defhlp == NULL)
171 defhlp = sethlp(hlpbuf, regexp, length);
172 puthelp(stderr, defhlp, help);
173 goto start;
175 if (ckquit && (strcmp(input, "q") == 0)) {
176 (void) strcpy(strval, input);
177 return (3);
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;
185 (void) strcpy(strval, input);
186 return (0);