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