xref: /titanic_51/usr/src/lib/libadm/common/ckrange.c (revision 5a7763bf3e9db4cfe6cb523b096cb74af71e3793)
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.3 */
32 /*LINTLIBRARY*/
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include "libadm.h"
40 
41 #define	MSGSIZ	256
42 #define	PROMPT10	"Enter an integer between %ld and %ld"
43 #define	PROMPT		"Enter a base %d integer between %ld and %ld"
44 #define	MESG10		"Please enter an integer between %ld and %ld."
45 #define	MESG		"Please enter a base %d integer between %ld and %ld."
46 
47 static void
48 setmsg(char *msg, long lower, long upper, int base)
49 {
50 	if ((base == 10) || (base == 0))
51 		(void) sprintf(msg, MESG10, lower, upper);
52 	else
53 		(void) sprintf(msg, MESG, base, lower, upper);
54 }
55 
56 void
57 ckrange_err(long lower, long upper, int base, char *error)
58 {
59 	char	defmesg[MSGSIZ];
60 
61 	setmsg(defmesg, lower, upper, base);
62 	puterror(stdout, defmesg, error);
63 }
64 
65 void
66 ckrange_hlp(long lower, long upper, int base, char *help)
67 {
68 	char	defmesg[MSGSIZ];
69 
70 	setmsg(defmesg, lower, upper, base);
71 	puthelp(stdout, defmesg, help);
72 }
73 
74 int
75 ckrange_val(long lower, long upper, int base, char *input)
76 {
77 	char	*ptr;
78 	long	value;
79 
80 	value = strtol(input, &ptr, base);
81 	if ((*ptr != '\0') || (value < lower) || (value > upper))
82 		return (1);
83 	return (0);
84 }
85 
86 int
87 ckrange(long *rngval, long lower, long upper, short base, char *defstr,
88 	char *error, char *help, char *prompt)
89 {
90 	int	valid, n;
91 	long	value;
92 	char	*ptr;
93 	char	input[MAX_INPUT];
94 	char	defmesg[MSGSIZ];
95 	char	defpmpt[128];
96 	char	buffer[64];
97 	char	*choices[2];
98 
99 	if (lower >= upper)
100 		return (2);
101 
102 	(void) sprintf(buffer, "%ld-%ld", lower, upper);
103 
104 	if (base == 0)
105 		base = 10;
106 
107 	if (!prompt) {
108 		if (base == 10)
109 			(void) sprintf(defpmpt, PROMPT10, lower, upper);
110 		else
111 			(void) sprintf(defpmpt, PROMPT, base, lower, upper);
112 		prompt = defpmpt;
113 	}
114 
115 	setmsg(defmesg, lower, upper, base);
116 	choices[0] = buffer;
117 	choices[1] = NULL;
118 
119 start:
120 	putprmpt(stderr, prompt, choices, defstr);
121 	if (getinput(input))
122 		return (1);
123 
124 	n = (int)strlen(input);
125 	if (n == 0) {
126 		if (defstr) {
127 			*rngval = strtol(defstr, NULL, base);
128 			return (0);
129 		}
130 		puterror(stderr, defmesg, error);
131 		goto start;
132 	}
133 	if (strcmp(input, "?") == 0) {
134 		puthelp(stderr, defmesg, help);
135 		goto start;
136 	}
137 	if (ckquit && (strcmp(input, "q") == 0))
138 		return (3);
139 
140 	value = strtol(input, &ptr, base);
141 	if (*ptr == '\0')
142 		valid = ((value >= lower) && (value <= upper));
143 	else
144 		valid = 0;
145 	if (!valid) {
146 		puterror(stderr, defmesg, error);
147 		goto start;
148 	}
149 	*rngval = value;
150 	return (0);
151 }
152