xref: /illumos-gate/usr/src/lib/libadm/common/ckkeywd.c (revision 1da57d55)
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 (c) 1997, by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/types.h>
37 #include "libadm.h"
38 
39 static int
match(char * strval,char * set[])40 match(char *strval, char *set[])
41 {
42 	char *found;
43 	int i, len;
44 
45 	len = (int)strlen(strval);
46 
47 	found = NULL;
48 	for (i = 0; set[i]; i++) {
49 		if (strncmp(set[i], strval, len) == 0) {
50 			if (found)
51 				return (-1); /* not unique */
52 			found = set[i];
53 		}
54 	}
55 
56 	if (found) {
57 		(void) strcpy(strval, found);
58 		return (0);
59 	}
60 	return (1);
61 }
62 
63 int
ckkeywd(char * strval,char * keyword[],char * defstr,char * error,char * help,char * prompt)64 ckkeywd(char *strval, char *keyword[], char *defstr, char *error, char *help,
65 	char *prompt)
66 {
67 	int valid, i, n;
68 	char input[MAX_INPUT];
69 	char defmesg[512];
70 	char *ept;
71 
72 	(void) sprintf(defmesg, "Please enter one of the following keywords: ");
73 	ept = defmesg + strlen(defmesg);
74 	for (i = 0; keyword[i]; ) {
75 		if (i)
76 			(void) strcat(ept, ", ");
77 		(void) strcat(ept, keyword[i++]);
78 	}
79 	(void) strcat(ept, ckquit ? ", q." : ".");
80 
81 	if (!prompt)
82 		prompt = "Enter appropriate value";
83 
84 start:
85 	putprmpt(stderr, prompt, keyword, defstr);
86 	if (getinput(input))
87 		return (1);
88 
89 	n = (int)strlen(input);
90 	if (n == 0) {
91 		if (defstr) {
92 			(void) strcpy(strval, defstr);
93 			return (0);
94 		}
95 		puterror(stderr, defmesg, error);
96 		goto start;
97 	}
98 	if (strcmp(input, "?") == 0) {
99 		puthelp(stderr, defmesg, help);
100 		goto start;
101 	}
102 	if (ckquit && (strcmp(input, "q") == 0)) {
103 		(void) strcpy(strval, input);
104 		return (3);
105 	}
106 
107 	valid = 1;
108 	if (keyword)
109 		valid = !match(input, keyword);
110 
111 	if (!valid) {
112 		puterror(stderr, defmesg, error);
113 		goto start;
114 	}
115 	(void) strcpy(strval, input);
116 	return (0);
117 }
118